2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_alloc_btree.h"
31 #include "xfs_ialloc_btree.h"
32 #include "xfs_dinode.h"
33 #include "xfs_inode.h"
34 #include "xfs_btree.h"
35 #include "xfs_mount.h"
36 #include "xfs_itable.h"
37 #include "xfs_inode_item.h"
38 #include "xfs_extfree_item.h"
39 #include "xfs_alloc.h"
41 #include "xfs_rtalloc.h"
42 #include "xfs_error.h"
43 #include "xfs_attr_leaf.h"
45 #include "xfs_quota.h"
46 #include "xfs_trans_space.h"
47 #include "xfs_buf_item.h"
48 #include "xfs_filestream.h"
49 #include "xfs_vnodeops.h"
50 #include "xfs_trace.h"
55 xfs_bmap_check_leaf_extents(xfs_btree_cur_t
*cur
, xfs_inode_t
*ip
, int whichfork
);
58 kmem_zone_t
*xfs_bmap_free_item_zone
;
61 * Prototypes for internal bmap routines.
66 * Called from xfs_bmap_add_attrfork to handle extents format files.
68 STATIC
int /* error */
69 xfs_bmap_add_attrfork_extents(
70 xfs_trans_t
*tp
, /* transaction pointer */
71 xfs_inode_t
*ip
, /* incore inode pointer */
72 xfs_fsblock_t
*firstblock
, /* first block allocated */
73 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
74 int *flags
); /* inode logging flags */
77 * Called from xfs_bmap_add_attrfork to handle local format files.
79 STATIC
int /* error */
80 xfs_bmap_add_attrfork_local(
81 xfs_trans_t
*tp
, /* transaction pointer */
82 xfs_inode_t
*ip
, /* incore inode pointer */
83 xfs_fsblock_t
*firstblock
, /* first block allocated */
84 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
85 int *flags
); /* inode logging flags */
88 * Called by xfs_bmap_add_extent to handle cases converting a delayed
89 * allocation to a real allocation.
91 STATIC
int /* error */
92 xfs_bmap_add_extent_delay_real(
93 struct xfs_trans
*tp
, /* transaction pointer */
94 xfs_inode_t
*ip
, /* incore inode pointer */
95 xfs_extnum_t
*idx
, /* extent number to update/insert */
96 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
97 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
98 xfs_filblks_t
*dnew
, /* new delayed-alloc indirect blocks */
99 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
100 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
101 int *logflagsp
); /* inode logging flags */
104 * Called by xfs_bmap_add_extent to handle cases converting a hole
105 * to a delayed allocation.
107 STATIC
int /* error */
108 xfs_bmap_add_extent_hole_delay(
109 xfs_inode_t
*ip
, /* incore inode pointer */
110 xfs_extnum_t
*idx
, /* extent number to update/insert */
111 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
112 int *logflagsp
); /* inode logging flags */
115 * Called by xfs_bmap_add_extent to handle cases converting a hole
116 * to a real allocation.
118 STATIC
int /* error */
119 xfs_bmap_add_extent_hole_real(
120 xfs_inode_t
*ip
, /* incore inode pointer */
121 xfs_extnum_t
*idx
, /* extent number to update/insert */
122 xfs_btree_cur_t
*cur
, /* if null, not a btree */
123 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
124 int *logflagsp
, /* inode logging flags */
125 int whichfork
); /* data or attr fork */
128 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
129 * allocation to a real allocation or vice versa.
131 STATIC
int /* error */
132 xfs_bmap_add_extent_unwritten_real(
133 xfs_inode_t
*ip
, /* incore inode pointer */
134 xfs_extnum_t
*idx
, /* extent number to update/insert */
135 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
136 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
137 int *logflagsp
); /* inode logging flags */
140 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
141 * It figures out where to ask the underlying allocator to put the new extent.
143 STATIC
int /* error */
145 xfs_bmalloca_t
*ap
); /* bmap alloc argument struct */
148 * Transform a btree format file with only one leaf node, where the
149 * extents list will fit in the inode, into an extents format file.
150 * Since the file extents are already in-core, all we have to do is
151 * give up the space for the btree root and pitch the leaf block.
153 STATIC
int /* error */
154 xfs_bmap_btree_to_extents(
155 xfs_trans_t
*tp
, /* transaction pointer */
156 xfs_inode_t
*ip
, /* incore inode pointer */
157 xfs_btree_cur_t
*cur
, /* btree cursor */
158 int *logflagsp
, /* inode logging flags */
159 int whichfork
); /* data or attr fork */
162 * Remove the entry "free" from the free item list. Prev points to the
163 * previous entry, unless "free" is the head of the list.
167 xfs_bmap_free_t
*flist
, /* free item list header */
168 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
169 xfs_bmap_free_item_t
*free
); /* list item to be freed */
172 * Convert an extents-format file into a btree-format file.
173 * The new file will have a root block (in the inode) and a single child block.
175 STATIC
int /* error */
176 xfs_bmap_extents_to_btree(
177 xfs_trans_t
*tp
, /* transaction pointer */
178 xfs_inode_t
*ip
, /* incore inode pointer */
179 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
180 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
181 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
182 int wasdel
, /* converting a delayed alloc */
183 int *logflagsp
, /* inode logging flags */
184 int whichfork
); /* data or attr fork */
187 * Convert a local file to an extents file.
188 * This code is sort of bogus, since the file data needs to get
189 * logged so it won't be lost. The bmap-level manipulations are ok, though.
191 STATIC
int /* error */
192 xfs_bmap_local_to_extents(
193 xfs_trans_t
*tp
, /* transaction pointer */
194 xfs_inode_t
*ip
, /* incore inode pointer */
195 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
196 xfs_extlen_t total
, /* total blocks needed by transaction */
197 int *logflagsp
, /* inode logging flags */
198 int whichfork
); /* data or attr fork */
201 * Search the extents list for the inode, for the extent containing bno.
202 * If bno lies in a hole, point to the next entry. If bno lies past eof,
203 * *eofp will be set, and *prevp will contain the last entry (null if none).
204 * Else, *lastxp will be set to the index of the found
205 * entry; *gotp will contain the entry.
207 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
208 xfs_bmap_search_extents(
209 xfs_inode_t
*ip
, /* incore inode pointer */
210 xfs_fileoff_t bno
, /* block number searched for */
211 int whichfork
, /* data or attr fork */
212 int *eofp
, /* out: end of file found */
213 xfs_extnum_t
*lastxp
, /* out: last extent index */
214 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
215 xfs_bmbt_irec_t
*prevp
); /* out: previous extent entry found */
218 * Check the last inode extent to determine whether this allocation will result
219 * in blocks being allocated at the end of the file. When we allocate new data
220 * blocks at the end of the file which do not start at the previous data block,
221 * we will try to align the new blocks at stripe unit boundaries.
223 STATIC
int /* error */
225 xfs_inode_t
*ip
, /* incore inode pointer */
226 xfs_fileoff_t off
, /* file offset in fsblocks */
227 int whichfork
, /* data or attribute fork */
228 char *aeof
); /* return value */
231 * Compute the worst-case number of indirect blocks that will be used
232 * for ip's delayed extent of length "len".
235 xfs_bmap_worst_indlen(
236 xfs_inode_t
*ip
, /* incore inode pointer */
237 xfs_filblks_t len
); /* delayed extent length */
241 * Perform various validation checks on the values being returned
245 xfs_bmap_validate_ret(
249 xfs_bmbt_irec_t
*mval
,
253 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
261 xfs_fsblock_t blockno
,
266 xfs_bmap_count_leaves(
273 xfs_bmap_disk_count_leaves(
274 struct xfs_mount
*mp
,
275 struct xfs_btree_block
*block
,
280 * Bmap internal routines.
283 STATIC
int /* error */
285 struct xfs_btree_cur
*cur
,
289 int *stat
) /* success/failure */
291 cur
->bc_rec
.b
.br_startoff
= off
;
292 cur
->bc_rec
.b
.br_startblock
= bno
;
293 cur
->bc_rec
.b
.br_blockcount
= len
;
294 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
297 STATIC
int /* error */
299 struct xfs_btree_cur
*cur
,
303 int *stat
) /* success/failure */
305 cur
->bc_rec
.b
.br_startoff
= off
;
306 cur
->bc_rec
.b
.br_startblock
= bno
;
307 cur
->bc_rec
.b
.br_blockcount
= len
;
308 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
312 * Update the record referred to by cur to the value given
313 * by [off, bno, len, state].
314 * This either works (return 0) or gets an EFSCORRUPTED error.
318 struct xfs_btree_cur
*cur
,
324 union xfs_btree_rec rec
;
326 xfs_bmbt_disk_set_allf(&rec
.bmbt
, off
, bno
, len
, state
);
327 return xfs_btree_update(cur
, &rec
);
331 * Called from xfs_bmap_add_attrfork to handle btree format files.
333 STATIC
int /* error */
334 xfs_bmap_add_attrfork_btree(
335 xfs_trans_t
*tp
, /* transaction pointer */
336 xfs_inode_t
*ip
, /* incore inode pointer */
337 xfs_fsblock_t
*firstblock
, /* first block allocated */
338 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
339 int *flags
) /* inode logging flags */
341 xfs_btree_cur_t
*cur
; /* btree cursor */
342 int error
; /* error return value */
343 xfs_mount_t
*mp
; /* file system mount struct */
344 int stat
; /* newroot status */
347 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
348 *flags
|= XFS_ILOG_DBROOT
;
350 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
351 cur
->bc_private
.b
.flist
= flist
;
352 cur
->bc_private
.b
.firstblock
= *firstblock
;
353 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
355 /* must be at least one entry */
356 XFS_WANT_CORRUPTED_GOTO(stat
== 1, error0
);
357 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
360 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
361 return XFS_ERROR(ENOSPC
);
363 *firstblock
= cur
->bc_private
.b
.firstblock
;
364 cur
->bc_private
.b
.allocated
= 0;
365 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
369 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
374 * Called from xfs_bmap_add_attrfork to handle extents format files.
376 STATIC
int /* error */
377 xfs_bmap_add_attrfork_extents(
378 xfs_trans_t
*tp
, /* transaction pointer */
379 xfs_inode_t
*ip
, /* incore inode pointer */
380 xfs_fsblock_t
*firstblock
, /* first block allocated */
381 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
382 int *flags
) /* inode logging flags */
384 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
385 int error
; /* error return value */
387 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
390 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
, &cur
, 0,
391 flags
, XFS_DATA_FORK
);
393 cur
->bc_private
.b
.allocated
= 0;
394 xfs_btree_del_cursor(cur
,
395 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
401 * Called from xfs_bmap_add_attrfork to handle local format files.
403 STATIC
int /* error */
404 xfs_bmap_add_attrfork_local(
405 xfs_trans_t
*tp
, /* transaction pointer */
406 xfs_inode_t
*ip
, /* incore inode pointer */
407 xfs_fsblock_t
*firstblock
, /* first block allocated */
408 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
409 int *flags
) /* inode logging flags */
411 xfs_da_args_t dargs
; /* args for dir/attr code */
412 int error
; /* error return value */
413 xfs_mount_t
*mp
; /* mount structure pointer */
415 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
417 if (S_ISDIR(ip
->i_d
.di_mode
)) {
419 memset(&dargs
, 0, sizeof(dargs
));
421 dargs
.firstblock
= firstblock
;
423 dargs
.total
= mp
->m_dirblkfsbs
;
424 dargs
.whichfork
= XFS_DATA_FORK
;
426 error
= xfs_dir2_sf_to_block(&dargs
);
428 error
= xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1, flags
,
434 * Called by xfs_bmapi to update file extent records and the btree
435 * after allocating space (or doing a delayed allocation).
437 STATIC
int /* error */
439 struct xfs_trans
*tp
, /* transaction pointer */
440 xfs_inode_t
*ip
, /* incore inode pointer */
441 xfs_extnum_t
*idx
, /* extent number to update/insert */
442 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
443 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
444 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
445 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
446 int *logflagsp
, /* inode logging flags */
447 int whichfork
) /* data or attr fork */
449 xfs_btree_cur_t
*cur
; /* btree cursor or null */
450 xfs_filblks_t da_new
; /* new count del alloc blocks used */
451 xfs_filblks_t da_old
; /* old count del alloc blocks used */
452 int error
; /* error return value */
453 xfs_ifork_t
*ifp
; /* inode fork ptr */
454 int logflags
; /* returned value */
455 xfs_extnum_t nextents
; /* number of extents in file now */
457 XFS_STATS_INC(xs_add_exlist
);
460 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
461 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
466 ASSERT(*idx
<= nextents
);
469 * Any kind of new delayed allocation goes here.
471 if (isnullstartblock(new->br_startblock
)) {
473 ASSERT((cur
->bc_private
.b
.flags
&
474 XFS_BTCUR_BPRV_WASDEL
) == 0);
475 error
= xfs_bmap_add_extent_hole_delay(ip
, idx
, new,
479 * Real allocation off the end of the file.
481 else if (*idx
== nextents
) {
483 ASSERT((cur
->bc_private
.b
.flags
&
484 XFS_BTCUR_BPRV_WASDEL
) == 0);
485 error
= xfs_bmap_add_extent_hole_real(ip
, idx
, cur
, new,
486 &logflags
, whichfork
);
488 xfs_bmbt_irec_t prev
; /* old extent at offset idx */
491 * Get the record referred to by idx.
493 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &prev
);
495 * If it's a real allocation record, and the new allocation ends
496 * after the start of the referred to record, then we're filling
497 * in a delayed or unwritten allocation with a real one, or
498 * converting real back to unwritten.
500 if (!isnullstartblock(new->br_startblock
) &&
501 new->br_startoff
+ new->br_blockcount
> prev
.br_startoff
) {
502 if (prev
.br_state
!= XFS_EXT_UNWRITTEN
&&
503 isnullstartblock(prev
.br_startblock
)) {
504 da_old
= startblockval(prev
.br_startblock
);
506 ASSERT(cur
->bc_private
.b
.flags
&
507 XFS_BTCUR_BPRV_WASDEL
);
508 error
= xfs_bmap_add_extent_delay_real(tp
, ip
,
509 idx
, &cur
, new, &da_new
,
510 first
, flist
, &logflags
);
512 ASSERT(new->br_state
== XFS_EXT_NORM
||
513 new->br_state
== XFS_EXT_UNWRITTEN
);
515 error
= xfs_bmap_add_extent_unwritten_real(ip
,
516 idx
, &cur
, new, &logflags
);
522 * Otherwise we're filling in a hole with an allocation.
526 ASSERT((cur
->bc_private
.b
.flags
&
527 XFS_BTCUR_BPRV_WASDEL
) == 0);
528 error
= xfs_bmap_add_extent_hole_real(ip
, idx
, cur
,
529 new, &logflags
, whichfork
);
535 ASSERT(*curp
== cur
|| *curp
== NULL
);
538 * Convert to a btree if necessary.
540 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
541 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ifp
->if_ext_max
) {
542 int tmp_logflags
; /* partial log flag return val */
545 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
,
546 flist
, &cur
, da_old
> 0, &tmp_logflags
, whichfork
);
547 logflags
|= tmp_logflags
;
552 * Adjust for changes in reserved delayed indirect blocks.
553 * Nothing to do for disk quotas here.
555 if (da_old
|| da_new
) {
560 nblks
+= cur
->bc_private
.b
.allocated
;
561 ASSERT(nblks
<= da_old
);
563 xfs_icsb_modify_counters(ip
->i_mount
, XFS_SBS_FDBLOCKS
,
564 (int64_t)(da_old
- nblks
), 0);
567 * Clear out the allocated field, done with it now in any case.
570 cur
->bc_private
.b
.allocated
= 0;
576 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
578 *logflagsp
= logflags
;
583 * Called by xfs_bmap_add_extent to handle cases converting a delayed
584 * allocation to a real allocation.
586 STATIC
int /* error */
587 xfs_bmap_add_extent_delay_real(
588 struct xfs_trans
*tp
, /* transaction pointer */
589 xfs_inode_t
*ip
, /* incore inode pointer */
590 xfs_extnum_t
*idx
, /* extent number to update/insert */
591 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
592 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
593 xfs_filblks_t
*dnew
, /* new delayed-alloc indirect blocks */
594 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
595 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
596 int *logflagsp
) /* inode logging flags */
598 xfs_btree_cur_t
*cur
; /* btree cursor */
599 int diff
; /* temp value */
600 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
601 int error
; /* error return value */
602 int i
; /* temp state */
603 xfs_ifork_t
*ifp
; /* inode fork pointer */
604 xfs_fileoff_t new_endoff
; /* end offset of new entry */
605 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
606 /* left is 0, right is 1, prev is 2 */
607 int rval
=0; /* return value (logging flags) */
608 int state
= 0;/* state bits, accessed thru macros */
609 xfs_filblks_t temp
=0; /* value for dnew calculations */
610 xfs_filblks_t temp2
=0;/* value for dnew calculations */
611 int tmp_rval
; /* partial logging flags */
618 * Set up a bunch of variables to make the tests simpler.
621 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
622 ep
= xfs_iext_get_ext(ifp
, *idx
);
623 xfs_bmbt_get_all(ep
, &PREV
);
624 new_endoff
= new->br_startoff
+ new->br_blockcount
;
625 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
626 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
629 * Set flags determining what part of the previous delayed allocation
630 * extent is being replaced by a real allocation.
632 if (PREV
.br_startoff
== new->br_startoff
)
633 state
|= BMAP_LEFT_FILLING
;
634 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
635 state
|= BMAP_RIGHT_FILLING
;
638 * Check and set flags if this segment has a left neighbor.
639 * Don't set contiguous if the combined extent would be too large.
642 state
|= BMAP_LEFT_VALID
;
643 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
645 if (isnullstartblock(LEFT
.br_startblock
))
646 state
|= BMAP_LEFT_DELAY
;
649 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
650 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
651 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
652 LEFT
.br_state
== new->br_state
&&
653 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
654 state
|= BMAP_LEFT_CONTIG
;
657 * Check and set flags if this segment has a right neighbor.
658 * Don't set contiguous if the combined extent would be too large.
659 * Also check for all-three-contiguous being too large.
661 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
662 state
|= BMAP_RIGHT_VALID
;
663 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
665 if (isnullstartblock(RIGHT
.br_startblock
))
666 state
|= BMAP_RIGHT_DELAY
;
669 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
670 new_endoff
== RIGHT
.br_startoff
&&
671 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
672 new->br_state
== RIGHT
.br_state
&&
673 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
674 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
675 BMAP_RIGHT_FILLING
)) !=
676 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
677 BMAP_RIGHT_FILLING
) ||
678 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
680 state
|= BMAP_RIGHT_CONTIG
;
684 * Switch out based on the FILLING and CONTIG state bits.
686 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
687 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
688 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
689 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
691 * Filling in all of a previously delayed allocation extent.
692 * The left and right neighbors are both contiguous with new.
695 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
696 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
697 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
698 RIGHT
.br_blockcount
);
699 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
701 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
702 ip
->i_d
.di_nextents
--;
704 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
706 rval
= XFS_ILOG_CORE
;
707 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
709 RIGHT
.br_blockcount
, &i
)))
711 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
712 if ((error
= xfs_btree_delete(cur
, &i
)))
714 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
715 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
717 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
718 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
722 RIGHT
.br_blockcount
, LEFT
.br_state
)))
728 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
730 * Filling in all of a previously delayed allocation extent.
731 * The left neighbor is contiguous, the right is not.
735 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
736 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
737 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
738 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
740 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
742 rval
= XFS_ILOG_DEXT
;
745 if ((error
= xfs_bmbt_lookup_eq(cur
, LEFT
.br_startoff
,
746 LEFT
.br_startblock
, LEFT
.br_blockcount
,
749 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
750 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
753 PREV
.br_blockcount
, LEFT
.br_state
)))
759 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
761 * Filling in all of a previously delayed allocation extent.
762 * The right neighbor is contiguous, the left is not.
764 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
765 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
766 xfs_bmbt_set_blockcount(ep
,
767 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
768 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
770 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
772 rval
= XFS_ILOG_DEXT
;
775 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
777 RIGHT
.br_blockcount
, &i
)))
779 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
780 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
783 RIGHT
.br_blockcount
, PREV
.br_state
)))
790 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
792 * Filling in all of a previously delayed allocation extent.
793 * Neither the left nor right neighbors are contiguous with
796 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
797 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
798 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
800 ip
->i_d
.di_nextents
++;
802 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
804 rval
= XFS_ILOG_CORE
;
805 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
806 new->br_startblock
, new->br_blockcount
,
809 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
810 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
811 if ((error
= xfs_btree_insert(cur
, &i
)))
813 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
819 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
821 * Filling in the first part of a previous delayed allocation.
822 * The left neighbor is contiguous.
824 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
825 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
826 LEFT
.br_blockcount
+ new->br_blockcount
);
827 xfs_bmbt_set_startoff(ep
,
828 PREV
.br_startoff
+ new->br_blockcount
);
829 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
831 temp
= PREV
.br_blockcount
- new->br_blockcount
;
832 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
833 xfs_bmbt_set_blockcount(ep
, temp
);
835 rval
= XFS_ILOG_DEXT
;
838 if ((error
= xfs_bmbt_lookup_eq(cur
, LEFT
.br_startoff
,
839 LEFT
.br_startblock
, LEFT
.br_blockcount
,
842 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
843 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
850 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
851 startblockval(PREV
.br_startblock
));
852 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
853 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
859 case BMAP_LEFT_FILLING
:
861 * Filling in the first part of a previous delayed allocation.
862 * The left neighbor is not contiguous.
864 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
865 xfs_bmbt_set_startoff(ep
, new_endoff
);
866 temp
= PREV
.br_blockcount
- new->br_blockcount
;
867 xfs_bmbt_set_blockcount(ep
, temp
);
868 xfs_iext_insert(ip
, *idx
, 1, new, state
);
869 ip
->i_d
.di_nextents
++;
871 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
873 rval
= XFS_ILOG_CORE
;
874 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
875 new->br_startblock
, new->br_blockcount
,
878 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
879 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
880 if ((error
= xfs_btree_insert(cur
, &i
)))
882 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
884 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
885 ip
->i_d
.di_nextents
> ip
->i_df
.if_ext_max
) {
886 error
= xfs_bmap_extents_to_btree(tp
, ip
,
887 first
, flist
, &cur
, 1, &tmp_rval
,
893 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
894 startblockval(PREV
.br_startblock
) -
895 (cur
? cur
->bc_private
.b
.allocated
: 0));
896 ep
= xfs_iext_get_ext(ifp
, *idx
+ 1);
897 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
898 trace_xfs_bmap_post_update(ip
, *idx
+ 1, state
, _THIS_IP_
);
903 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
905 * Filling in the last part of a previous delayed allocation.
906 * The right neighbor is contiguous with the new allocation.
908 temp
= PREV
.br_blockcount
- new->br_blockcount
;
909 trace_xfs_bmap_pre_update(ip
, *idx
+ 1, state
, _THIS_IP_
);
910 xfs_bmbt_set_blockcount(ep
, temp
);
911 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
+ 1),
912 new->br_startoff
, new->br_startblock
,
913 new->br_blockcount
+ RIGHT
.br_blockcount
,
915 trace_xfs_bmap_post_update(ip
, *idx
+ 1, state
, _THIS_IP_
);
917 rval
= XFS_ILOG_DEXT
;
920 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
922 RIGHT
.br_blockcount
, &i
)))
924 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
925 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
933 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
934 startblockval(PREV
.br_startblock
));
935 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
936 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
937 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
943 case BMAP_RIGHT_FILLING
:
945 * Filling in the last part of a previous delayed allocation.
946 * The right neighbor is not contiguous.
948 temp
= PREV
.br_blockcount
- new->br_blockcount
;
949 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
950 xfs_bmbt_set_blockcount(ep
, temp
);
951 xfs_iext_insert(ip
, *idx
+ 1, 1, new, state
);
952 ip
->i_d
.di_nextents
++;
954 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
956 rval
= XFS_ILOG_CORE
;
957 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
958 new->br_startblock
, new->br_blockcount
,
961 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
962 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
963 if ((error
= xfs_btree_insert(cur
, &i
)))
965 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
967 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
968 ip
->i_d
.di_nextents
> ip
->i_df
.if_ext_max
) {
969 error
= xfs_bmap_extents_to_btree(tp
, ip
,
970 first
, flist
, &cur
, 1, &tmp_rval
,
976 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
977 startblockval(PREV
.br_startblock
) -
978 (cur
? cur
->bc_private
.b
.allocated
: 0));
979 ep
= xfs_iext_get_ext(ifp
, *idx
);
980 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
981 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
989 * Filling in the middle part of a previous delayed allocation.
990 * Contiguity is impossible here.
991 * This case is avoided almost all the time.
993 * We start with a delayed allocation:
995 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
998 * and we are allocating:
999 * +rrrrrrrrrrrrrrrrr+
1002 * and we set it up for insertion as:
1003 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1005 * PREV @ idx LEFT RIGHT
1006 * inserted at idx + 1
1008 temp
= new->br_startoff
- PREV
.br_startoff
;
1009 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1010 trace_xfs_bmap_pre_update(ip
, *idx
, 0, _THIS_IP_
);
1011 xfs_bmbt_set_blockcount(ep
, temp
); /* truncate PREV */
1013 RIGHT
.br_state
= PREV
.br_state
;
1014 RIGHT
.br_startblock
= nullstartblock(
1015 (int)xfs_bmap_worst_indlen(ip
, temp2
));
1016 RIGHT
.br_startoff
= new_endoff
;
1017 RIGHT
.br_blockcount
= temp2
;
1018 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
1019 xfs_iext_insert(ip
, *idx
+ 1, 2, &LEFT
, state
);
1020 ip
->i_d
.di_nextents
++;
1022 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1024 rval
= XFS_ILOG_CORE
;
1025 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1026 new->br_startblock
, new->br_blockcount
,
1029 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
1030 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1031 if ((error
= xfs_btree_insert(cur
, &i
)))
1033 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1035 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
1036 ip
->i_d
.di_nextents
> ip
->i_df
.if_ext_max
) {
1037 error
= xfs_bmap_extents_to_btree(tp
, ip
,
1038 first
, flist
, &cur
, 1, &tmp_rval
,
1044 temp
= xfs_bmap_worst_indlen(ip
, temp
);
1045 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
1046 diff
= (int)(temp
+ temp2
- startblockval(PREV
.br_startblock
) -
1047 (cur
? cur
->bc_private
.b
.allocated
: 0));
1049 error
= xfs_icsb_modify_counters(ip
->i_mount
,
1051 -((int64_t)diff
), 0);
1057 ep
= xfs_iext_get_ext(ifp
, *idx
);
1058 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
1059 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1060 trace_xfs_bmap_pre_update(ip
, *idx
+ 2, state
, _THIS_IP_
);
1061 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
+ 2),
1062 nullstartblock((int)temp2
));
1063 trace_xfs_bmap_post_update(ip
, *idx
+ 2, state
, _THIS_IP_
);
1066 *dnew
= temp
+ temp2
;
1069 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1070 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1071 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
1072 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1073 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1074 case BMAP_LEFT_CONTIG
:
1075 case BMAP_RIGHT_CONTIG
:
1077 * These cases are all impossible.
1091 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1092 * allocation to a real allocation or vice versa.
1094 STATIC
int /* error */
1095 xfs_bmap_add_extent_unwritten_real(
1096 xfs_inode_t
*ip
, /* incore inode pointer */
1097 xfs_extnum_t
*idx
, /* extent number to update/insert */
1098 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
1099 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
1100 int *logflagsp
) /* inode logging flags */
1102 xfs_btree_cur_t
*cur
; /* btree cursor */
1103 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
1104 int error
; /* error return value */
1105 int i
; /* temp state */
1106 xfs_ifork_t
*ifp
; /* inode fork pointer */
1107 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1108 xfs_exntst_t newext
; /* new extent state */
1109 xfs_exntst_t oldext
; /* old extent state */
1110 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1111 /* left is 0, right is 1, prev is 2 */
1112 int rval
=0; /* return value (logging flags) */
1113 int state
= 0;/* state bits, accessed thru macros */
1119 * Set up a bunch of variables to make the tests simpler.
1123 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
1124 ep
= xfs_iext_get_ext(ifp
, *idx
);
1125 xfs_bmbt_get_all(ep
, &PREV
);
1126 newext
= new->br_state
;
1127 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
1128 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
1129 ASSERT(PREV
.br_state
== oldext
);
1130 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1131 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1132 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1135 * Set flags determining what part of the previous oldext allocation
1136 * extent is being replaced by a newext allocation.
1138 if (PREV
.br_startoff
== new->br_startoff
)
1139 state
|= BMAP_LEFT_FILLING
;
1140 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
1141 state
|= BMAP_RIGHT_FILLING
;
1144 * Check and set flags if this segment has a left neighbor.
1145 * Don't set contiguous if the combined extent would be too large.
1148 state
|= BMAP_LEFT_VALID
;
1149 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
1151 if (isnullstartblock(LEFT
.br_startblock
))
1152 state
|= BMAP_LEFT_DELAY
;
1155 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1156 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1157 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1158 LEFT
.br_state
== newext
&&
1159 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1160 state
|= BMAP_LEFT_CONTIG
;
1163 * Check and set flags if this segment has a right neighbor.
1164 * Don't set contiguous if the combined extent would be too large.
1165 * Also check for all-three-contiguous being too large.
1167 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
1168 state
|= BMAP_RIGHT_VALID
;
1169 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
1170 if (isnullstartblock(RIGHT
.br_startblock
))
1171 state
|= BMAP_RIGHT_DELAY
;
1174 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1175 new_endoff
== RIGHT
.br_startoff
&&
1176 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1177 newext
== RIGHT
.br_state
&&
1178 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1179 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1180 BMAP_RIGHT_FILLING
)) !=
1181 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1182 BMAP_RIGHT_FILLING
) ||
1183 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1185 state
|= BMAP_RIGHT_CONTIG
;
1188 * Switch out based on the FILLING and CONTIG state bits.
1190 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1191 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1192 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1193 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1195 * Setting all of a previous oldext extent to newext.
1196 * The left and right neighbors are both contiguous with new.
1200 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1201 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
1202 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1203 RIGHT
.br_blockcount
);
1204 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1206 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
1207 ip
->i_d
.di_nextents
-= 2;
1209 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1211 rval
= XFS_ILOG_CORE
;
1212 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
1213 RIGHT
.br_startblock
,
1214 RIGHT
.br_blockcount
, &i
)))
1216 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1217 if ((error
= xfs_btree_delete(cur
, &i
)))
1219 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1220 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
1222 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1223 if ((error
= xfs_btree_delete(cur
, &i
)))
1225 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1226 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
1228 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1229 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
1231 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1232 RIGHT
.br_blockcount
, LEFT
.br_state
)))
1237 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1239 * Setting all of a previous oldext extent to newext.
1240 * The left neighbor is contiguous, the right is not.
1244 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1245 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
1246 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
1247 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1249 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
1250 ip
->i_d
.di_nextents
--;
1252 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1254 rval
= XFS_ILOG_CORE
;
1255 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1256 PREV
.br_startblock
, PREV
.br_blockcount
,
1259 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1260 if ((error
= xfs_btree_delete(cur
, &i
)))
1262 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1263 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
1265 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1266 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
1268 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
1274 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1276 * Setting all of a previous oldext extent to newext.
1277 * The right neighbor is contiguous, the left is not.
1279 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1280 xfs_bmbt_set_blockcount(ep
,
1281 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
1282 xfs_bmbt_set_state(ep
, newext
);
1283 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1284 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
1285 ip
->i_d
.di_nextents
--;
1287 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1289 rval
= XFS_ILOG_CORE
;
1290 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
1291 RIGHT
.br_startblock
,
1292 RIGHT
.br_blockcount
, &i
)))
1294 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1295 if ((error
= xfs_btree_delete(cur
, &i
)))
1297 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1298 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
1300 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1301 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
1303 new->br_blockcount
+ RIGHT
.br_blockcount
,
1309 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1311 * Setting all of a previous oldext extent to newext.
1312 * Neither the left nor right neighbors are contiguous with
1315 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1316 xfs_bmbt_set_state(ep
, newext
);
1317 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1320 rval
= XFS_ILOG_DEXT
;
1323 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1324 new->br_startblock
, new->br_blockcount
,
1327 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1328 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
1329 new->br_startblock
, new->br_blockcount
,
1335 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1337 * Setting the first part of a previous oldext extent to newext.
1338 * The left neighbor is contiguous.
1340 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
1341 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
1342 LEFT
.br_blockcount
+ new->br_blockcount
);
1343 xfs_bmbt_set_startoff(ep
,
1344 PREV
.br_startoff
+ new->br_blockcount
);
1345 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
1347 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1348 xfs_bmbt_set_startblock(ep
,
1349 new->br_startblock
+ new->br_blockcount
);
1350 xfs_bmbt_set_blockcount(ep
,
1351 PREV
.br_blockcount
- new->br_blockcount
);
1352 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1357 rval
= XFS_ILOG_DEXT
;
1360 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1361 PREV
.br_startblock
, PREV
.br_blockcount
,
1364 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1365 if ((error
= xfs_bmbt_update(cur
,
1366 PREV
.br_startoff
+ new->br_blockcount
,
1367 PREV
.br_startblock
+ new->br_blockcount
,
1368 PREV
.br_blockcount
- new->br_blockcount
,
1371 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
1373 if (xfs_bmbt_update(cur
, LEFT
.br_startoff
,
1375 LEFT
.br_blockcount
+ new->br_blockcount
,
1381 case BMAP_LEFT_FILLING
:
1383 * Setting the first part of a previous oldext extent to newext.
1384 * The left neighbor is not contiguous.
1386 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1387 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
1388 xfs_bmbt_set_startoff(ep
, new_endoff
);
1389 xfs_bmbt_set_blockcount(ep
,
1390 PREV
.br_blockcount
- new->br_blockcount
);
1391 xfs_bmbt_set_startblock(ep
,
1392 new->br_startblock
+ new->br_blockcount
);
1393 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1395 xfs_iext_insert(ip
, *idx
, 1, new, state
);
1396 ip
->i_d
.di_nextents
++;
1398 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1400 rval
= XFS_ILOG_CORE
;
1401 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1402 PREV
.br_startblock
, PREV
.br_blockcount
,
1405 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1406 if ((error
= xfs_bmbt_update(cur
,
1407 PREV
.br_startoff
+ new->br_blockcount
,
1408 PREV
.br_startblock
+ new->br_blockcount
,
1409 PREV
.br_blockcount
- new->br_blockcount
,
1412 cur
->bc_rec
.b
= *new;
1413 if ((error
= xfs_btree_insert(cur
, &i
)))
1415 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1419 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1421 * Setting the last part of a previous oldext extent to newext.
1422 * The right neighbor is contiguous with the new allocation.
1424 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1425 xfs_bmbt_set_blockcount(ep
,
1426 PREV
.br_blockcount
- new->br_blockcount
);
1427 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1431 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1432 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
1433 new->br_startoff
, new->br_startblock
,
1434 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
1435 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1438 rval
= XFS_ILOG_DEXT
;
1441 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1443 PREV
.br_blockcount
, &i
)))
1445 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1446 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
1448 PREV
.br_blockcount
- new->br_blockcount
,
1451 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
1453 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
1455 new->br_blockcount
+ RIGHT
.br_blockcount
,
1461 case BMAP_RIGHT_FILLING
:
1463 * Setting the last part of a previous oldext extent to newext.
1464 * The right neighbor is not contiguous.
1466 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1467 xfs_bmbt_set_blockcount(ep
,
1468 PREV
.br_blockcount
- new->br_blockcount
);
1469 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1472 xfs_iext_insert(ip
, *idx
, 1, new, state
);
1474 ip
->i_d
.di_nextents
++;
1476 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1478 rval
= XFS_ILOG_CORE
;
1479 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1480 PREV
.br_startblock
, PREV
.br_blockcount
,
1483 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1484 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
1486 PREV
.br_blockcount
- new->br_blockcount
,
1489 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1490 new->br_startblock
, new->br_blockcount
,
1493 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
1494 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1495 if ((error
= xfs_btree_insert(cur
, &i
)))
1497 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1503 * Setting the middle part of a previous oldext extent to
1504 * newext. Contiguity is impossible here.
1505 * One extent becomes three extents.
1507 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1508 xfs_bmbt_set_blockcount(ep
,
1509 new->br_startoff
- PREV
.br_startoff
);
1510 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1513 r
[1].br_startoff
= new_endoff
;
1514 r
[1].br_blockcount
=
1515 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1516 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
1517 r
[1].br_state
= oldext
;
1520 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
1522 ip
->i_d
.di_nextents
+= 2;
1524 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1526 rval
= XFS_ILOG_CORE
;
1527 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1528 PREV
.br_startblock
, PREV
.br_blockcount
,
1531 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1532 /* new right extent - oldext */
1533 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
1534 r
[1].br_startblock
, r
[1].br_blockcount
,
1537 /* new left extent - oldext */
1538 cur
->bc_rec
.b
= PREV
;
1539 cur
->bc_rec
.b
.br_blockcount
=
1540 new->br_startoff
- PREV
.br_startoff
;
1541 if ((error
= xfs_btree_insert(cur
, &i
)))
1543 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1545 * Reset the cursor to the position of the new extent
1546 * we are about to insert as we can't trust it after
1547 * the previous insert.
1549 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1550 new->br_startblock
, new->br_blockcount
,
1553 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
1554 /* new middle extent - newext */
1555 cur
->bc_rec
.b
.br_state
= new->br_state
;
1556 if ((error
= xfs_btree_insert(cur
, &i
)))
1558 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1562 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1563 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1564 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
1565 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1566 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1567 case BMAP_LEFT_CONTIG
:
1568 case BMAP_RIGHT_CONTIG
:
1570 * These cases are all impossible.
1584 * Called by xfs_bmap_add_extent to handle cases converting a hole
1585 * to a delayed allocation.
1588 STATIC
int /* error */
1589 xfs_bmap_add_extent_hole_delay(
1590 xfs_inode_t
*ip
, /* incore inode pointer */
1591 xfs_extnum_t
*idx
, /* extent number to update/insert */
1592 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
1593 int *logflagsp
) /* inode logging flags */
1595 xfs_ifork_t
*ifp
; /* inode fork pointer */
1596 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
1597 xfs_filblks_t newlen
=0; /* new indirect size */
1598 xfs_filblks_t oldlen
=0; /* old indirect size */
1599 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
1600 int state
; /* state bits, accessed thru macros */
1601 xfs_filblks_t temp
=0; /* temp for indirect calculations */
1603 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
1605 ASSERT(isnullstartblock(new->br_startblock
));
1608 * Check and set flags if this segment has a left neighbor
1611 state
|= BMAP_LEFT_VALID
;
1612 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
1614 if (isnullstartblock(left
.br_startblock
))
1615 state
|= BMAP_LEFT_DELAY
;
1619 * Check and set flags if the current (right) segment exists.
1620 * If it doesn't exist, we're converting the hole at end-of-file.
1622 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
1623 state
|= BMAP_RIGHT_VALID
;
1624 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
1626 if (isnullstartblock(right
.br_startblock
))
1627 state
|= BMAP_RIGHT_DELAY
;
1631 * Set contiguity flags on the left and right neighbors.
1632 * Don't let extents get too large, even if the pieces are contiguous.
1634 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
1635 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
1636 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1637 state
|= BMAP_LEFT_CONTIG
;
1639 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
1640 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
1641 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
1642 (!(state
& BMAP_LEFT_CONTIG
) ||
1643 (left
.br_blockcount
+ new->br_blockcount
+
1644 right
.br_blockcount
<= MAXEXTLEN
)))
1645 state
|= BMAP_RIGHT_CONTIG
;
1648 * Switch out based on the contiguity flags.
1650 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
1651 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1653 * New allocation is contiguous with delayed allocations
1654 * on the left and on the right.
1655 * Merge all three into a single extent record.
1658 temp
= left
.br_blockcount
+ new->br_blockcount
+
1659 right
.br_blockcount
;
1661 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1662 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
1663 oldlen
= startblockval(left
.br_startblock
) +
1664 startblockval(new->br_startblock
) +
1665 startblockval(right
.br_startblock
);
1666 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
1667 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
1668 nullstartblock((int)newlen
));
1669 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1671 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
1674 case BMAP_LEFT_CONTIG
:
1676 * New allocation is contiguous with a delayed allocation
1678 * Merge the new allocation with the left neighbor.
1681 temp
= left
.br_blockcount
+ new->br_blockcount
;
1683 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1684 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
1685 oldlen
= startblockval(left
.br_startblock
) +
1686 startblockval(new->br_startblock
);
1687 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
1688 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
1689 nullstartblock((int)newlen
));
1690 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1693 case BMAP_RIGHT_CONTIG
:
1695 * New allocation is contiguous with a delayed allocation
1697 * Merge the new allocation with the right neighbor.
1699 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1700 temp
= new->br_blockcount
+ right
.br_blockcount
;
1701 oldlen
= startblockval(new->br_startblock
) +
1702 startblockval(right
.br_startblock
);
1703 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
1704 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
1706 nullstartblock((int)newlen
), temp
, right
.br_state
);
1707 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1712 * New allocation is not contiguous with another
1713 * delayed allocation.
1714 * Insert a new entry.
1716 oldlen
= newlen
= 0;
1717 xfs_iext_insert(ip
, *idx
, 1, new, state
);
1720 if (oldlen
!= newlen
) {
1721 ASSERT(oldlen
> newlen
);
1722 xfs_icsb_modify_counters(ip
->i_mount
, XFS_SBS_FDBLOCKS
,
1723 (int64_t)(oldlen
- newlen
), 0);
1725 * Nothing to do for disk quota accounting here.
1733 * Called by xfs_bmap_add_extent to handle cases converting a hole
1734 * to a real allocation.
1736 STATIC
int /* error */
1737 xfs_bmap_add_extent_hole_real(
1738 xfs_inode_t
*ip
, /* incore inode pointer */
1739 xfs_extnum_t
*idx
, /* extent number to update/insert */
1740 xfs_btree_cur_t
*cur
, /* if null, not a btree */
1741 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
1742 int *logflagsp
, /* inode logging flags */
1743 int whichfork
) /* data or attr fork */
1745 int error
; /* error return value */
1746 int i
; /* temp state */
1747 xfs_ifork_t
*ifp
; /* inode fork pointer */
1748 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
1749 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
1750 int rval
=0; /* return value (logging flags) */
1751 int state
; /* state bits, accessed thru macros */
1753 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1754 ASSERT(*idx
<= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
));
1757 if (whichfork
== XFS_ATTR_FORK
)
1758 state
|= BMAP_ATTRFORK
;
1761 * Check and set flags if this segment has a left neighbor.
1764 state
|= BMAP_LEFT_VALID
;
1765 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
1766 if (isnullstartblock(left
.br_startblock
))
1767 state
|= BMAP_LEFT_DELAY
;
1771 * Check and set flags if this segment has a current value.
1772 * Not true if we're inserting into the "hole" at eof.
1774 if (*idx
< ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
1775 state
|= BMAP_RIGHT_VALID
;
1776 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
1777 if (isnullstartblock(right
.br_startblock
))
1778 state
|= BMAP_RIGHT_DELAY
;
1782 * We're inserting a real allocation between "left" and "right".
1783 * Set the contiguity flags. Don't let extents get too large.
1785 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1786 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
1787 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
1788 left
.br_state
== new->br_state
&&
1789 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1790 state
|= BMAP_LEFT_CONTIG
;
1792 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1793 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
1794 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
1795 new->br_state
== right
.br_state
&&
1796 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
1797 (!(state
& BMAP_LEFT_CONTIG
) ||
1798 left
.br_blockcount
+ new->br_blockcount
+
1799 right
.br_blockcount
<= MAXEXTLEN
))
1800 state
|= BMAP_RIGHT_CONTIG
;
1804 * Select which case we're in here, and implement it.
1806 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
1807 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1809 * New allocation is contiguous with real allocations on the
1810 * left and on the right.
1811 * Merge all three into a single extent record.
1814 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1815 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
1816 left
.br_blockcount
+ new->br_blockcount
+
1817 right
.br_blockcount
);
1818 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1820 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
1822 XFS_IFORK_NEXT_SET(ip
, whichfork
,
1823 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
1825 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
1827 rval
= XFS_ILOG_CORE
;
1828 if ((error
= xfs_bmbt_lookup_eq(cur
,
1830 right
.br_startblock
,
1831 right
.br_blockcount
, &i
)))
1833 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1834 if ((error
= xfs_btree_delete(cur
, &i
)))
1836 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1837 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
1839 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1840 if ((error
= xfs_bmbt_update(cur
, left
.br_startoff
,
1842 left
.br_blockcount
+
1843 new->br_blockcount
+
1844 right
.br_blockcount
,
1850 case BMAP_LEFT_CONTIG
:
1852 * New allocation is contiguous with a real allocation
1854 * Merge the new allocation with the left neighbor.
1857 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1858 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
1859 left
.br_blockcount
+ new->br_blockcount
);
1860 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1863 rval
= xfs_ilog_fext(whichfork
);
1866 if ((error
= xfs_bmbt_lookup_eq(cur
,
1869 left
.br_blockcount
, &i
)))
1871 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1872 if ((error
= xfs_bmbt_update(cur
, left
.br_startoff
,
1874 left
.br_blockcount
+
1881 case BMAP_RIGHT_CONTIG
:
1883 * New allocation is contiguous with a real allocation
1885 * Merge the new allocation with the right neighbor.
1887 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1888 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
1889 new->br_startoff
, new->br_startblock
,
1890 new->br_blockcount
+ right
.br_blockcount
,
1892 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1895 rval
= xfs_ilog_fext(whichfork
);
1898 if ((error
= xfs_bmbt_lookup_eq(cur
,
1900 right
.br_startblock
,
1901 right
.br_blockcount
, &i
)))
1903 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1904 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
1906 new->br_blockcount
+
1907 right
.br_blockcount
,
1915 * New allocation is not contiguous with another
1917 * Insert a new entry.
1919 xfs_iext_insert(ip
, *idx
, 1, new, state
);
1920 XFS_IFORK_NEXT_SET(ip
, whichfork
,
1921 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
1923 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
1925 rval
= XFS_ILOG_CORE
;
1926 if ((error
= xfs_bmbt_lookup_eq(cur
,
1929 new->br_blockcount
, &i
)))
1931 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
1932 cur
->bc_rec
.b
.br_state
= new->br_state
;
1933 if ((error
= xfs_btree_insert(cur
, &i
)))
1935 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1945 * Adjust the size of the new extent based on di_extsize and rt extsize.
1948 xfs_bmap_extsize_align(
1950 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
1951 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
1952 xfs_extlen_t extsz
, /* align to this extent size */
1953 int rt
, /* is this a realtime inode? */
1954 int eof
, /* is extent at end-of-file? */
1955 int delay
, /* creating delalloc extent? */
1956 int convert
, /* overwriting unwritten extent? */
1957 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
1958 xfs_extlen_t
*lenp
) /* in/out: aligned length */
1960 xfs_fileoff_t orig_off
; /* original offset */
1961 xfs_extlen_t orig_alen
; /* original length */
1962 xfs_fileoff_t orig_end
; /* original off+len */
1963 xfs_fileoff_t nexto
; /* next file offset */
1964 xfs_fileoff_t prevo
; /* previous file offset */
1965 xfs_fileoff_t align_off
; /* temp for offset */
1966 xfs_extlen_t align_alen
; /* temp for length */
1967 xfs_extlen_t temp
; /* temp for calculations */
1972 orig_off
= align_off
= *offp
;
1973 orig_alen
= align_alen
= *lenp
;
1974 orig_end
= orig_off
+ orig_alen
;
1977 * If this request overlaps an existing extent, then don't
1978 * attempt to perform any additional alignment.
1980 if (!delay
&& !eof
&&
1981 (orig_off
>= gotp
->br_startoff
) &&
1982 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
1987 * If the file offset is unaligned vs. the extent size
1988 * we need to align it. This will be possible unless
1989 * the file was previously written with a kernel that didn't
1990 * perform this alignment, or if a truncate shot us in the
1993 temp
= do_mod(orig_off
, extsz
);
1999 * Same adjustment for the end of the requested area.
2001 if ((temp
= (align_alen
% extsz
))) {
2002 align_alen
+= extsz
- temp
;
2005 * If the previous block overlaps with this proposed allocation
2006 * then move the start forward without adjusting the length.
2008 if (prevp
->br_startoff
!= NULLFILEOFF
) {
2009 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
2010 prevo
= prevp
->br_startoff
;
2012 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
2015 if (align_off
!= orig_off
&& align_off
< prevo
)
2018 * If the next block overlaps with this proposed allocation
2019 * then move the start back without adjusting the length,
2020 * but not before offset 0.
2021 * This may of course make the start overlap previous block,
2022 * and if we hit the offset 0 limit then the next block
2023 * can still overlap too.
2025 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
2026 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
2027 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
2028 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
2030 nexto
= gotp
->br_startoff
;
2032 nexto
= NULLFILEOFF
;
2034 align_off
+ align_alen
!= orig_end
&&
2035 align_off
+ align_alen
> nexto
)
2036 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
2038 * If we're now overlapping the next or previous extent that
2039 * means we can't fit an extsz piece in this hole. Just move
2040 * the start forward to the first valid spot and set
2041 * the length so we hit the end.
2043 if (align_off
!= orig_off
&& align_off
< prevo
)
2045 if (align_off
+ align_alen
!= orig_end
&&
2046 align_off
+ align_alen
> nexto
&&
2047 nexto
!= NULLFILEOFF
) {
2048 ASSERT(nexto
> prevo
);
2049 align_alen
= nexto
- align_off
;
2053 * If realtime, and the result isn't a multiple of the realtime
2054 * extent size we need to remove blocks until it is.
2056 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
2058 * We're not covering the original request, or
2059 * we won't be able to once we fix the length.
2061 if (orig_off
< align_off
||
2062 orig_end
> align_off
+ align_alen
||
2063 align_alen
- temp
< orig_alen
)
2064 return XFS_ERROR(EINVAL
);
2066 * Try to fix it by moving the start up.
2068 if (align_off
+ temp
<= orig_off
) {
2073 * Try to fix it by moving the end in.
2075 else if (align_off
+ align_alen
- temp
>= orig_end
)
2078 * Set the start to the minimum then trim the length.
2081 align_alen
-= orig_off
- align_off
;
2082 align_off
= orig_off
;
2083 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
2086 * Result doesn't cover the request, fail it.
2088 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
2089 return XFS_ERROR(EINVAL
);
2091 ASSERT(orig_off
>= align_off
);
2092 ASSERT(orig_end
<= align_off
+ align_alen
);
2096 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
2097 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
2098 if (prevp
->br_startoff
!= NULLFILEOFF
)
2099 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
2107 #define XFS_ALLOC_GAP_UNITS 4
2111 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
2113 xfs_fsblock_t adjust
; /* adjustment to block numbers */
2114 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
2115 xfs_mount_t
*mp
; /* mount point structure */
2116 int nullfb
; /* true if ap->firstblock isn't set */
2117 int rt
; /* true if inode is realtime */
2119 #define ISVALID(x,y) \
2121 (x) < mp->m_sb.sb_rblocks : \
2122 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2123 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2124 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2126 mp
= ap
->ip
->i_mount
;
2127 nullfb
= ap
->firstblock
== NULLFSBLOCK
;
2128 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
;
2129 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, ap
->firstblock
);
2131 * If allocating at eof, and there's a previous real block,
2132 * try to use its last block as our starting point.
2134 if (ap
->eof
&& ap
->prevp
->br_startoff
!= NULLFILEOFF
&&
2135 !isnullstartblock(ap
->prevp
->br_startblock
) &&
2136 ISVALID(ap
->prevp
->br_startblock
+ ap
->prevp
->br_blockcount
,
2137 ap
->prevp
->br_startblock
)) {
2138 ap
->rval
= ap
->prevp
->br_startblock
+ ap
->prevp
->br_blockcount
;
2140 * Adjust for the gap between prevp and us.
2143 (ap
->prevp
->br_startoff
+ ap
->prevp
->br_blockcount
);
2145 ISVALID(ap
->rval
+ adjust
, ap
->prevp
->br_startblock
))
2149 * If not at eof, then compare the two neighbor blocks.
2150 * Figure out whether either one gives us a good starting point,
2151 * and pick the better one.
2153 else if (!ap
->eof
) {
2154 xfs_fsblock_t gotbno
; /* right side block number */
2155 xfs_fsblock_t gotdiff
=0; /* right side difference */
2156 xfs_fsblock_t prevbno
; /* left side block number */
2157 xfs_fsblock_t prevdiff
=0; /* left side difference */
2160 * If there's a previous (left) block, select a requested
2161 * start block based on it.
2163 if (ap
->prevp
->br_startoff
!= NULLFILEOFF
&&
2164 !isnullstartblock(ap
->prevp
->br_startblock
) &&
2165 (prevbno
= ap
->prevp
->br_startblock
+
2166 ap
->prevp
->br_blockcount
) &&
2167 ISVALID(prevbno
, ap
->prevp
->br_startblock
)) {
2169 * Calculate gap to end of previous block.
2171 adjust
= prevdiff
= ap
->off
-
2172 (ap
->prevp
->br_startoff
+
2173 ap
->prevp
->br_blockcount
);
2175 * Figure the startblock based on the previous block's
2176 * end and the gap size.
2178 * If the gap is large relative to the piece we're
2179 * allocating, or using it gives us an invalid block
2180 * number, then just use the end of the previous block.
2182 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->alen
&&
2183 ISVALID(prevbno
+ prevdiff
,
2184 ap
->prevp
->br_startblock
))
2189 * If the firstblock forbids it, can't use it,
2192 if (!rt
&& !nullfb
&&
2193 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
2194 prevbno
= NULLFSBLOCK
;
2197 * No previous block or can't follow it, just default.
2200 prevbno
= NULLFSBLOCK
;
2202 * If there's a following (right) block, select a requested
2203 * start block based on it.
2205 if (!isnullstartblock(ap
->gotp
->br_startblock
)) {
2207 * Calculate gap to start of next block.
2209 adjust
= gotdiff
= ap
->gotp
->br_startoff
- ap
->off
;
2211 * Figure the startblock based on the next block's
2212 * start and the gap size.
2214 gotbno
= ap
->gotp
->br_startblock
;
2217 * If the gap is large relative to the piece we're
2218 * allocating, or using it gives us an invalid block
2219 * number, then just use the start of the next block
2220 * offset by our length.
2222 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->alen
&&
2223 ISVALID(gotbno
- gotdiff
, gotbno
))
2225 else if (ISVALID(gotbno
- ap
->alen
, gotbno
)) {
2227 gotdiff
+= adjust
- ap
->alen
;
2231 * If the firstblock forbids it, can't use it,
2234 if (!rt
&& !nullfb
&&
2235 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
2236 gotbno
= NULLFSBLOCK
;
2239 * No next block, just default.
2242 gotbno
= NULLFSBLOCK
;
2244 * If both valid, pick the better one, else the only good
2245 * one, else ap->rval is already set (to 0 or the inode block).
2247 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
2248 ap
->rval
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
2249 else if (prevbno
!= NULLFSBLOCK
)
2251 else if (gotbno
!= NULLFSBLOCK
)
2259 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
2261 xfs_alloctype_t atype
= 0; /* type for allocation routines */
2262 int error
; /* error return value */
2263 xfs_mount_t
*mp
; /* mount point structure */
2264 xfs_extlen_t prod
= 0; /* product factor for allocators */
2265 xfs_extlen_t ralen
= 0; /* realtime allocation length */
2266 xfs_extlen_t align
; /* minimum allocation alignment */
2269 mp
= ap
->ip
->i_mount
;
2270 align
= xfs_get_extsz_hint(ap
->ip
);
2271 prod
= align
/ mp
->m_sb
.sb_rextsize
;
2272 error
= xfs_bmap_extsize_align(mp
, ap
->gotp
, ap
->prevp
,
2273 align
, 1, ap
->eof
, 0,
2274 ap
->conv
, &ap
->off
, &ap
->alen
);
2278 ASSERT(ap
->alen
% mp
->m_sb
.sb_rextsize
== 0);
2281 * If the offset & length are not perfectly aligned
2282 * then kill prod, it will just get us in trouble.
2284 if (do_mod(ap
->off
, align
) || ap
->alen
% align
)
2287 * Set ralen to be the actual requested length in rtextents.
2289 ralen
= ap
->alen
/ mp
->m_sb
.sb_rextsize
;
2291 * If the old value was close enough to MAXEXTLEN that
2292 * we rounded up to it, cut it back so it's valid again.
2293 * Note that if it's a really large request (bigger than
2294 * MAXEXTLEN), we don't hear about that number, and can't
2295 * adjust the starting point to match it.
2297 if (ralen
* mp
->m_sb
.sb_rextsize
>= MAXEXTLEN
)
2298 ralen
= MAXEXTLEN
/ mp
->m_sb
.sb_rextsize
;
2301 * Lock out other modifications to the RT bitmap inode.
2303 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
);
2304 xfs_trans_ijoin_ref(ap
->tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
2307 * If it's an allocation to an empty file at offset 0,
2308 * pick an extent that will space things out in the rt area.
2310 if (ap
->eof
&& ap
->off
== 0) {
2311 xfs_rtblock_t
uninitialized_var(rtx
); /* realtime extent no */
2313 error
= xfs_rtpick_extent(mp
, ap
->tp
, ralen
, &rtx
);
2316 ap
->rval
= rtx
* mp
->m_sb
.sb_rextsize
;
2321 xfs_bmap_adjacent(ap
);
2324 * Realtime allocation, done through xfs_rtallocate_extent.
2326 atype
= ap
->rval
== 0 ? XFS_ALLOCTYPE_ANY_AG
: XFS_ALLOCTYPE_NEAR_BNO
;
2327 do_div(ap
->rval
, mp
->m_sb
.sb_rextsize
);
2330 if ((error
= xfs_rtallocate_extent(ap
->tp
, ap
->rval
, 1, ap
->alen
,
2331 &ralen
, atype
, ap
->wasdel
, prod
, &rtb
)))
2333 if (rtb
== NULLFSBLOCK
&& prod
> 1 &&
2334 (error
= xfs_rtallocate_extent(ap
->tp
, ap
->rval
, 1,
2335 ap
->alen
, &ralen
, atype
,
2336 ap
->wasdel
, 1, &rtb
)))
2339 if (ap
->rval
!= NULLFSBLOCK
) {
2340 ap
->rval
*= mp
->m_sb
.sb_rextsize
;
2341 ralen
*= mp
->m_sb
.sb_rextsize
;
2343 ap
->ip
->i_d
.di_nblocks
+= ralen
;
2344 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
2346 ap
->ip
->i_delayed_blks
-= ralen
;
2348 * Adjust the disk quota also. This was reserved
2351 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
2352 ap
->wasdel
? XFS_TRANS_DQ_DELRTBCOUNT
:
2353 XFS_TRANS_DQ_RTBCOUNT
, (long) ralen
);
2361 xfs_bmap_btalloc_nullfb(
2362 struct xfs_bmalloca
*ap
,
2363 struct xfs_alloc_arg
*args
,
2366 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
2367 struct xfs_perag
*pag
;
2368 xfs_agnumber_t ag
, startag
;
2372 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
))
2373 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
2375 args
->type
= XFS_ALLOCTYPE_START_BNO
;
2376 args
->total
= ap
->total
;
2379 * Search for an allocation group with a single extent large enough
2380 * for the request. If one isn't found, then adjust the minimum
2381 * allocation size to the largest space found.
2383 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2384 if (startag
== NULLAGNUMBER
)
2387 pag
= xfs_perag_get(mp
, ag
);
2388 while (*blen
< args
->maxlen
) {
2389 if (!pag
->pagf_init
) {
2390 error
= xfs_alloc_pagf_init(mp
, args
->tp
, ag
,
2391 XFS_ALLOC_FLAG_TRYLOCK
);
2399 * See xfs_alloc_fix_freelist...
2401 if (pag
->pagf_init
) {
2402 xfs_extlen_t longest
;
2403 longest
= xfs_alloc_longest_free_extent(mp
, pag
);
2404 if (*blen
< longest
)
2409 if (xfs_inode_is_filestream(ap
->ip
)) {
2410 if (*blen
>= args
->maxlen
)
2415 * If startag is an invalid AG, we've
2416 * come here once before and
2417 * xfs_filestream_new_ag picked the
2418 * best currently available.
2420 * Don't continue looping, since we
2421 * could loop forever.
2423 if (startag
== NULLAGNUMBER
)
2426 error
= xfs_filestream_new_ag(ap
, &ag
);
2431 /* loop again to set 'blen'*/
2432 startag
= NULLAGNUMBER
;
2433 pag
= xfs_perag_get(mp
, ag
);
2437 if (++ag
== mp
->m_sb
.sb_agcount
)
2442 pag
= xfs_perag_get(mp
, ag
);
2447 * Since the above loop did a BUF_TRYLOCK, it is
2448 * possible that there is space for this request.
2450 if (notinit
|| *blen
< ap
->minlen
)
2451 args
->minlen
= ap
->minlen
;
2453 * If the best seen length is less than the request
2454 * length, use the best as the minimum.
2456 else if (*blen
< args
->maxlen
)
2457 args
->minlen
= *blen
;
2459 * Otherwise we've seen an extent as big as maxlen,
2460 * use that as the minimum.
2463 args
->minlen
= args
->maxlen
;
2466 * set the failure fallback case to look in the selected
2467 * AG as the stream may have moved.
2469 if (xfs_inode_is_filestream(ap
->ip
))
2470 ap
->rval
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
2477 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
2479 xfs_mount_t
*mp
; /* mount point structure */
2480 xfs_alloctype_t atype
= 0; /* type for allocation routines */
2481 xfs_extlen_t align
; /* minimum allocation alignment */
2482 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
2484 xfs_alloc_arg_t args
;
2486 xfs_extlen_t nextminlen
= 0;
2487 int nullfb
; /* true if ap->firstblock isn't set */
2492 mp
= ap
->ip
->i_mount
;
2493 align
= ap
->userdata
? xfs_get_extsz_hint(ap
->ip
) : 0;
2494 if (unlikely(align
)) {
2495 error
= xfs_bmap_extsize_align(mp
, ap
->gotp
, ap
->prevp
,
2496 align
, 0, ap
->eof
, 0, ap
->conv
,
2497 &ap
->off
, &ap
->alen
);
2501 nullfb
= ap
->firstblock
== NULLFSBLOCK
;
2502 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, ap
->firstblock
);
2504 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
)) {
2505 ag
= xfs_filestream_lookup_ag(ap
->ip
);
2506 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
2507 ap
->rval
= XFS_AGB_TO_FSB(mp
, ag
, 0);
2509 ap
->rval
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
2512 ap
->rval
= ap
->firstblock
;
2514 xfs_bmap_adjacent(ap
);
2517 * If allowed, use ap->rval; otherwise must use firstblock since
2518 * it's in the right allocation group.
2520 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->rval
) == fb_agno
)
2523 ap
->rval
= ap
->firstblock
;
2525 * Normal allocation, done through xfs_alloc_vextent.
2527 tryagain
= isaligned
= 0;
2530 args
.fsbno
= ap
->rval
;
2532 /* Trim the allocation back to the maximum an AG can fit. */
2533 args
.maxlen
= MIN(ap
->alen
, XFS_ALLOC_AG_MAX_USABLE(mp
));
2534 args
.firstblock
= ap
->firstblock
;
2537 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
2540 } else if (ap
->low
) {
2541 if (xfs_inode_is_filestream(ap
->ip
))
2542 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
2544 args
.type
= XFS_ALLOCTYPE_START_BNO
;
2545 args
.total
= args
.minlen
= ap
->minlen
;
2547 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
2548 args
.total
= ap
->total
;
2549 args
.minlen
= ap
->minlen
;
2551 /* apply extent size hints if obtained earlier */
2552 if (unlikely(align
)) {
2554 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->off
, args
.prod
)))
2555 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
2556 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_CACHE_SIZE
) {
2560 args
.prod
= PAGE_CACHE_SIZE
>> mp
->m_sb
.sb_blocklog
;
2561 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->off
, args
.prod
))))
2562 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
2565 * If we are not low on available data blocks, and the
2566 * underlying logical volume manager is a stripe, and
2567 * the file offset is zero then try to allocate data
2568 * blocks on stripe unit boundary.
2569 * NOTE: ap->aeof is only set if the allocation length
2570 * is >= the stripe unit and the allocation offset is
2571 * at the end of file.
2573 if (!ap
->low
&& ap
->aeof
) {
2575 args
.alignment
= mp
->m_dalign
;
2579 * Adjust for alignment
2581 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
2582 args
.minlen
= blen
- args
.alignment
;
2583 args
.minalignslop
= 0;
2586 * First try an exact bno allocation.
2587 * If it fails then do a near or start bno
2588 * allocation with alignment turned on.
2592 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
2595 * Compute the minlen+alignment for the
2596 * next case. Set slop so that the value
2597 * of minlen+alignment+slop doesn't go up
2598 * between the calls.
2600 if (blen
> mp
->m_dalign
&& blen
<= args
.maxlen
)
2601 nextminlen
= blen
- mp
->m_dalign
;
2603 nextminlen
= args
.minlen
;
2604 if (nextminlen
+ mp
->m_dalign
> args
.minlen
+ 1)
2606 nextminlen
+ mp
->m_dalign
-
2609 args
.minalignslop
= 0;
2613 args
.minalignslop
= 0;
2615 args
.minleft
= ap
->minleft
;
2616 args
.wasdel
= ap
->wasdel
;
2618 args
.userdata
= ap
->userdata
;
2619 if ((error
= xfs_alloc_vextent(&args
)))
2621 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
2623 * Exact allocation failed. Now try with alignment
2627 args
.fsbno
= ap
->rval
;
2628 args
.alignment
= mp
->m_dalign
;
2629 args
.minlen
= nextminlen
;
2630 args
.minalignslop
= 0;
2632 if ((error
= xfs_alloc_vextent(&args
)))
2635 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
2637 * allocation failed, so turn off alignment and
2641 args
.fsbno
= ap
->rval
;
2643 if ((error
= xfs_alloc_vextent(&args
)))
2646 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
2647 args
.minlen
> ap
->minlen
) {
2648 args
.minlen
= ap
->minlen
;
2649 args
.type
= XFS_ALLOCTYPE_START_BNO
;
2650 args
.fsbno
= ap
->rval
;
2651 if ((error
= xfs_alloc_vextent(&args
)))
2654 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
2656 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
2657 args
.total
= ap
->minlen
;
2659 if ((error
= xfs_alloc_vextent(&args
)))
2663 if (args
.fsbno
!= NULLFSBLOCK
) {
2664 ap
->firstblock
= ap
->rval
= args
.fsbno
;
2665 ASSERT(nullfb
|| fb_agno
== args
.agno
||
2666 (ap
->low
&& fb_agno
< args
.agno
));
2667 ap
->alen
= args
.len
;
2668 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
2669 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
2671 ap
->ip
->i_delayed_blks
-= args
.len
;
2673 * Adjust the disk quota also. This was reserved
2676 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
2677 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
2678 XFS_TRANS_DQ_BCOUNT
,
2681 ap
->rval
= NULLFSBLOCK
;
2688 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2689 * It figures out where to ask the underlying allocator to put the new extent.
2693 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
2695 if (XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
)
2696 return xfs_bmap_rtalloc(ap
);
2697 return xfs_bmap_btalloc(ap
);
2701 * Transform a btree format file with only one leaf node, where the
2702 * extents list will fit in the inode, into an extents format file.
2703 * Since the file extents are already in-core, all we have to do is
2704 * give up the space for the btree root and pitch the leaf block.
2706 STATIC
int /* error */
2707 xfs_bmap_btree_to_extents(
2708 xfs_trans_t
*tp
, /* transaction pointer */
2709 xfs_inode_t
*ip
, /* incore inode pointer */
2710 xfs_btree_cur_t
*cur
, /* btree cursor */
2711 int *logflagsp
, /* inode logging flags */
2712 int whichfork
) /* data or attr fork */
2715 struct xfs_btree_block
*cblock
;/* child btree block */
2716 xfs_fsblock_t cbno
; /* child block number */
2717 xfs_buf_t
*cbp
; /* child block's buffer */
2718 int error
; /* error return value */
2719 xfs_ifork_t
*ifp
; /* inode fork data */
2720 xfs_mount_t
*mp
; /* mount point structure */
2721 __be64
*pp
; /* ptr to block address */
2722 struct xfs_btree_block
*rblock
;/* root btree block */
2725 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2726 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
2727 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
2728 rblock
= ifp
->if_broot
;
2729 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
2730 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
2731 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
2732 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
2733 cbno
= be64_to_cpu(*pp
);
2736 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
2739 if ((error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
,
2740 XFS_BMAP_BTREE_REF
)))
2742 cblock
= XFS_BUF_TO_BLOCK(cbp
);
2743 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
2745 xfs_bmap_add_free(cbno
, 1, cur
->bc_private
.b
.flist
, mp
);
2746 ip
->i_d
.di_nblocks
--;
2747 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
2748 xfs_trans_binval(tp
, cbp
);
2749 if (cur
->bc_bufs
[0] == cbp
)
2750 cur
->bc_bufs
[0] = NULL
;
2751 xfs_iroot_realloc(ip
, -1, whichfork
);
2752 ASSERT(ifp
->if_broot
== NULL
);
2753 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
2754 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
2755 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2760 * Called by xfs_bmapi to update file extent records and the btree
2761 * after removing space (or undoing a delayed allocation).
2763 STATIC
int /* error */
2764 xfs_bmap_del_extent(
2765 xfs_inode_t
*ip
, /* incore inode pointer */
2766 xfs_trans_t
*tp
, /* current transaction pointer */
2767 xfs_extnum_t
*idx
, /* extent number to update/delete */
2768 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
2769 xfs_btree_cur_t
*cur
, /* if null, not a btree */
2770 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
2771 int *logflagsp
, /* inode logging flags */
2772 int whichfork
) /* data or attr fork */
2774 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
2775 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
2776 xfs_fsblock_t del_endblock
=0; /* first block past del */
2777 xfs_fileoff_t del_endoff
; /* first offset past del */
2778 int delay
; /* current block is delayed allocated */
2779 int do_fx
; /* free extent at end of routine */
2780 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
2781 int error
; /* error return value */
2782 int flags
; /* inode logging flags */
2783 xfs_bmbt_irec_t got
; /* current extent entry */
2784 xfs_fileoff_t got_endoff
; /* first offset past got */
2785 int i
; /* temp state */
2786 xfs_ifork_t
*ifp
; /* inode fork pointer */
2787 xfs_mount_t
*mp
; /* mount structure */
2788 xfs_filblks_t nblks
; /* quota/sb block count */
2789 xfs_bmbt_irec_t
new; /* new record to be inserted */
2791 uint qfield
; /* quota field to update */
2792 xfs_filblks_t temp
; /* for indirect length calculations */
2793 xfs_filblks_t temp2
; /* for indirect length calculations */
2796 XFS_STATS_INC(xs_del_exlist
);
2798 if (whichfork
== XFS_ATTR_FORK
)
2799 state
|= BMAP_ATTRFORK
;
2802 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2803 ASSERT((*idx
>= 0) && (*idx
< ifp
->if_bytes
/
2804 (uint
)sizeof(xfs_bmbt_rec_t
)));
2805 ASSERT(del
->br_blockcount
> 0);
2806 ep
= xfs_iext_get_ext(ifp
, *idx
);
2807 xfs_bmbt_get_all(ep
, &got
);
2808 ASSERT(got
.br_startoff
<= del
->br_startoff
);
2809 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
2810 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
2811 ASSERT(got_endoff
>= del_endoff
);
2812 delay
= isnullstartblock(got
.br_startblock
);
2813 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
2818 * If deleting a real allocation, must free up the disk space.
2821 flags
= XFS_ILOG_CORE
;
2823 * Realtime allocation. Free it and record di_nblocks update.
2825 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
2829 ASSERT(do_mod(del
->br_blockcount
,
2830 mp
->m_sb
.sb_rextsize
) == 0);
2831 ASSERT(do_mod(del
->br_startblock
,
2832 mp
->m_sb
.sb_rextsize
) == 0);
2833 bno
= del
->br_startblock
;
2834 len
= del
->br_blockcount
;
2835 do_div(bno
, mp
->m_sb
.sb_rextsize
);
2836 do_div(len
, mp
->m_sb
.sb_rextsize
);
2837 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
2841 nblks
= len
* mp
->m_sb
.sb_rextsize
;
2842 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
2845 * Ordinary allocation.
2849 nblks
= del
->br_blockcount
;
2850 qfield
= XFS_TRANS_DQ_BCOUNT
;
2853 * Set up del_endblock and cur for later.
2855 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
2857 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
2858 got
.br_startblock
, got
.br_blockcount
,
2861 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2863 da_old
= da_new
= 0;
2865 da_old
= startblockval(got
.br_startblock
);
2871 * Set flag value to use in switch statement.
2872 * Left-contig is 2, right-contig is 1.
2874 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
2875 (got_endoff
== del_endoff
)) {
2878 * Matches the whole extent. Delete the entry.
2880 xfs_iext_remove(ip
, *idx
, 1,
2881 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
2886 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2887 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2888 flags
|= XFS_ILOG_CORE
;
2890 flags
|= xfs_ilog_fext(whichfork
);
2893 if ((error
= xfs_btree_delete(cur
, &i
)))
2895 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2900 * Deleting the first part of the extent.
2902 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2903 xfs_bmbt_set_startoff(ep
, del_endoff
);
2904 temp
= got
.br_blockcount
- del
->br_blockcount
;
2905 xfs_bmbt_set_blockcount(ep
, temp
);
2907 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2909 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2910 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2914 xfs_bmbt_set_startblock(ep
, del_endblock
);
2915 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2917 flags
|= xfs_ilog_fext(whichfork
);
2920 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
2921 got
.br_blockcount
- del
->br_blockcount
,
2928 * Deleting the last part of the extent.
2930 temp
= got
.br_blockcount
- del
->br_blockcount
;
2931 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2932 xfs_bmbt_set_blockcount(ep
, temp
);
2934 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2936 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2937 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2941 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2943 flags
|= xfs_ilog_fext(whichfork
);
2946 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
2948 got
.br_blockcount
- del
->br_blockcount
,
2955 * Deleting the middle of the extent.
2957 temp
= del
->br_startoff
- got
.br_startoff
;
2958 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2959 xfs_bmbt_set_blockcount(ep
, temp
);
2960 new.br_startoff
= del_endoff
;
2961 temp2
= got_endoff
- del_endoff
;
2962 new.br_blockcount
= temp2
;
2963 new.br_state
= got
.br_state
;
2965 new.br_startblock
= del_endblock
;
2966 flags
|= XFS_ILOG_CORE
;
2968 if ((error
= xfs_bmbt_update(cur
,
2970 got
.br_startblock
, temp
,
2973 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2975 cur
->bc_rec
.b
= new;
2976 error
= xfs_btree_insert(cur
, &i
);
2977 if (error
&& error
!= ENOSPC
)
2980 * If get no-space back from btree insert,
2981 * it tried a split, and we have a zero
2982 * block reservation.
2983 * Fix up our state and return the error.
2985 if (error
== ENOSPC
) {
2987 * Reset the cursor, don't trust
2988 * it after any insert operation.
2990 if ((error
= xfs_bmbt_lookup_eq(cur
,
2995 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2997 * Update the btree record back
2998 * to the original value.
3000 if ((error
= xfs_bmbt_update(cur
,
3007 * Reset the extent record back
3008 * to the original value.
3010 xfs_bmbt_set_blockcount(ep
,
3013 error
= XFS_ERROR(ENOSPC
);
3016 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
3018 flags
|= xfs_ilog_fext(whichfork
);
3019 XFS_IFORK_NEXT_SET(ip
, whichfork
,
3020 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
3022 ASSERT(whichfork
== XFS_DATA_FORK
);
3023 temp
= xfs_bmap_worst_indlen(ip
, temp
);
3024 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
3025 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
3026 new.br_startblock
= nullstartblock((int)temp2
);
3027 da_new
= temp
+ temp2
;
3028 while (da_new
> da_old
) {
3032 xfs_bmbt_set_startblock(ep
,
3033 nullstartblock((int)temp
));
3035 if (da_new
== da_old
)
3041 nullstartblock((int)temp2
);
3045 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
3046 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
3051 * If we need to, add to list of extents to delete.
3054 xfs_bmap_add_free(del
->br_startblock
, del
->br_blockcount
, flist
,
3057 * Adjust inode # blocks in the file.
3060 ip
->i_d
.di_nblocks
-= nblks
;
3062 * Adjust quota data.
3065 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
3068 * Account for change in delayed indirect blocks.
3069 * Nothing to do for disk quota accounting here.
3071 ASSERT(da_old
>= da_new
);
3072 if (da_old
> da_new
) {
3073 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
3074 (int64_t)(da_old
- da_new
), 0);
3082 * Remove the entry "free" from the free item list. Prev points to the
3083 * previous entry, unless "free" is the head of the list.
3087 xfs_bmap_free_t
*flist
, /* free item list header */
3088 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
3089 xfs_bmap_free_item_t
*free
) /* list item to be freed */
3092 prev
->xbfi_next
= free
->xbfi_next
;
3094 flist
->xbf_first
= free
->xbfi_next
;
3096 kmem_zone_free(xfs_bmap_free_item_zone
, free
);
3100 * Convert an extents-format file into a btree-format file.
3101 * The new file will have a root block (in the inode) and a single child block.
3103 STATIC
int /* error */
3104 xfs_bmap_extents_to_btree(
3105 xfs_trans_t
*tp
, /* transaction pointer */
3106 xfs_inode_t
*ip
, /* incore inode pointer */
3107 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
3108 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
3109 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
3110 int wasdel
, /* converting a delayed alloc */
3111 int *logflagsp
, /* inode logging flags */
3112 int whichfork
) /* data or attr fork */
3114 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
3115 xfs_buf_t
*abp
; /* buffer for ablock */
3116 xfs_alloc_arg_t args
; /* allocation arguments */
3117 xfs_bmbt_rec_t
*arp
; /* child record pointer */
3118 struct xfs_btree_block
*block
; /* btree root block */
3119 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
3120 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
3121 int error
; /* error return value */
3122 xfs_extnum_t i
, cnt
; /* extent record index */
3123 xfs_ifork_t
*ifp
; /* inode fork pointer */
3124 xfs_bmbt_key_t
*kp
; /* root block key pointer */
3125 xfs_mount_t
*mp
; /* mount structure */
3126 xfs_extnum_t nextents
; /* number of file extents */
3127 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
3129 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3130 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
3131 ASSERT(ifp
->if_ext_max
==
3132 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
3134 * Make space in the inode incore.
3136 xfs_iroot_realloc(ip
, 1, whichfork
);
3137 ifp
->if_flags
|= XFS_IFBROOT
;
3142 block
= ifp
->if_broot
;
3143 block
->bb_magic
= cpu_to_be32(XFS_BMAP_MAGIC
);
3144 block
->bb_level
= cpu_to_be16(1);
3145 block
->bb_numrecs
= cpu_to_be16(1);
3146 block
->bb_u
.l
.bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
3147 block
->bb_u
.l
.bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
3150 * Need a cursor. Can't allocate until bb_level is filled in.
3153 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
3154 cur
->bc_private
.b
.firstblock
= *firstblock
;
3155 cur
->bc_private
.b
.flist
= flist
;
3156 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
3158 * Convert to a btree with two levels, one record in root.
3160 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
3163 args
.firstblock
= *firstblock
;
3164 if (*firstblock
== NULLFSBLOCK
) {
3165 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3166 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
3167 } else if (flist
->xbf_low
) {
3168 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3169 args
.fsbno
= *firstblock
;
3171 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3172 args
.fsbno
= *firstblock
;
3174 args
.minlen
= args
.maxlen
= args
.prod
= 1;
3175 args
.total
= args
.minleft
= args
.alignment
= args
.mod
= args
.isfl
=
3176 args
.minalignslop
= 0;
3177 args
.wasdel
= wasdel
;
3179 if ((error
= xfs_alloc_vextent(&args
))) {
3180 xfs_iroot_realloc(ip
, -1, whichfork
);
3181 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
3185 * Allocation can't fail, the space was reserved.
3187 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
3188 ASSERT(*firstblock
== NULLFSBLOCK
||
3189 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
3191 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
3192 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
3193 cur
->bc_private
.b
.allocated
++;
3194 ip
->i_d
.di_nblocks
++;
3195 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
3196 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
3198 * Fill in the child block.
3200 ablock
= XFS_BUF_TO_BLOCK(abp
);
3201 ablock
->bb_magic
= cpu_to_be32(XFS_BMAP_MAGIC
);
3202 ablock
->bb_level
= 0;
3203 ablock
->bb_u
.l
.bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
3204 ablock
->bb_u
.l
.bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
3205 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
3206 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
3207 for (cnt
= i
= 0; i
< nextents
; i
++) {
3208 ep
= xfs_iext_get_ext(ifp
, i
);
3209 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
3210 arp
->l0
= cpu_to_be64(ep
->l0
);
3211 arp
->l1
= cpu_to_be64(ep
->l1
);
3215 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
3216 xfs_btree_set_numrecs(ablock
, cnt
);
3219 * Fill in the root key and pointer.
3221 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
3222 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
3223 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
3224 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
3225 be16_to_cpu(block
->bb_level
)));
3226 *pp
= cpu_to_be64(args
.fsbno
);
3229 * Do all this logging at the end so that
3230 * the root is at the right level.
3232 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
3233 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
3234 ASSERT(*curp
== NULL
);
3236 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
3241 * Calculate the default attribute fork offset for newly created inodes.
3244 xfs_default_attroffset(
3245 struct xfs_inode
*ip
)
3247 struct xfs_mount
*mp
= ip
->i_mount
;
3250 if (mp
->m_sb
.sb_inodesize
== 256) {
3251 offset
= XFS_LITINO(mp
) -
3252 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
3254 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
3257 ASSERT(offset
< XFS_LITINO(mp
));
3262 * Helper routine to reset inode di_forkoff field when switching
3263 * attribute fork from local to extent format - we reset it where
3264 * possible to make space available for inline data fork extents.
3267 xfs_bmap_forkoff_reset(
3272 if (whichfork
== XFS_ATTR_FORK
&&
3273 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
3274 ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
&&
3275 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
3276 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
3278 if (dfl_forkoff
> ip
->i_d
.di_forkoff
) {
3279 ip
->i_d
.di_forkoff
= dfl_forkoff
;
3280 ip
->i_df
.if_ext_max
=
3281 XFS_IFORK_DSIZE(ip
) / sizeof(xfs_bmbt_rec_t
);
3282 ip
->i_afp
->if_ext_max
=
3283 XFS_IFORK_ASIZE(ip
) / sizeof(xfs_bmbt_rec_t
);
3289 * Convert a local file to an extents file.
3290 * This code is out of bounds for data forks of regular files,
3291 * since the file data needs to get logged so things will stay consistent.
3292 * (The bmap-level manipulations are ok, though).
3294 STATIC
int /* error */
3295 xfs_bmap_local_to_extents(
3296 xfs_trans_t
*tp
, /* transaction pointer */
3297 xfs_inode_t
*ip
, /* incore inode pointer */
3298 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
3299 xfs_extlen_t total
, /* total blocks needed by transaction */
3300 int *logflagsp
, /* inode logging flags */
3301 int whichfork
) /* data or attr fork */
3303 int error
; /* error return value */
3304 int flags
; /* logging flags returned */
3305 xfs_ifork_t
*ifp
; /* inode fork pointer */
3308 * We don't want to deal with the case of keeping inode data inline yet.
3309 * So sending the data fork of a regular inode is invalid.
3311 ASSERT(!(S_ISREG(ip
->i_d
.di_mode
) && whichfork
== XFS_DATA_FORK
));
3312 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3313 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
3316 if (ifp
->if_bytes
) {
3317 xfs_alloc_arg_t args
; /* allocation arguments */
3318 xfs_buf_t
*bp
; /* buffer for extent block */
3319 xfs_bmbt_rec_host_t
*ep
;/* extent record pointer */
3322 args
.mp
= ip
->i_mount
;
3323 args
.firstblock
= *firstblock
;
3324 ASSERT((ifp
->if_flags
&
3325 (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) == XFS_IFINLINE
);
3327 * Allocate a block. We know we need only one, since the
3328 * file currently fits in an inode.
3330 if (*firstblock
== NULLFSBLOCK
) {
3331 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
3332 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3334 args
.fsbno
= *firstblock
;
3335 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3338 args
.mod
= args
.minleft
= args
.alignment
= args
.wasdel
=
3339 args
.isfl
= args
.minalignslop
= 0;
3340 args
.minlen
= args
.maxlen
= args
.prod
= 1;
3341 if ((error
= xfs_alloc_vextent(&args
)))
3344 * Can't fail, the space was reserved.
3346 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
3347 ASSERT(args
.len
== 1);
3348 *firstblock
= args
.fsbno
;
3349 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
3350 memcpy(bp
->b_addr
, ifp
->if_u1
.if_data
, ifp
->if_bytes
);
3351 xfs_trans_log_buf(tp
, bp
, 0, ifp
->if_bytes
- 1);
3352 xfs_bmap_forkoff_reset(args
.mp
, ip
, whichfork
);
3353 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
3354 xfs_iext_add(ifp
, 0, 1);
3355 ep
= xfs_iext_get_ext(ifp
, 0);
3356 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
3357 trace_xfs_bmap_post_update(ip
, 0,
3358 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
3360 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
3361 ip
->i_d
.di_nblocks
= 1;
3362 xfs_trans_mod_dquot_byino(tp
, ip
,
3363 XFS_TRANS_DQ_BCOUNT
, 1L);
3364 flags
|= xfs_ilog_fext(whichfork
);
3366 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
3367 xfs_bmap_forkoff_reset(ip
->i_mount
, ip
, whichfork
);
3369 ifp
->if_flags
&= ~XFS_IFINLINE
;
3370 ifp
->if_flags
|= XFS_IFEXTENTS
;
3371 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
3372 flags
|= XFS_ILOG_CORE
;
3379 * Search the extent records for the entry containing block bno.
3380 * If bno lies in a hole, point to the next entry. If bno lies
3381 * past eof, *eofp will be set, and *prevp will contain the last
3382 * entry (null if none). Else, *lastxp will be set to the index
3383 * of the found entry; *gotp will contain the entry.
3385 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
3386 xfs_bmap_search_multi_extents(
3387 xfs_ifork_t
*ifp
, /* inode fork pointer */
3388 xfs_fileoff_t bno
, /* block number searched for */
3389 int *eofp
, /* out: end of file found */
3390 xfs_extnum_t
*lastxp
, /* out: last extent index */
3391 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
3392 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
3394 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
3395 xfs_extnum_t lastx
; /* last extent index */
3398 * Initialize the extent entry structure to catch access to
3399 * uninitialized br_startblock field.
3401 gotp
->br_startoff
= 0xffa5a5a5a5a5a5a5LL
;
3402 gotp
->br_blockcount
= 0xa55a5a5a5a5a5a5aLL
;
3403 gotp
->br_state
= XFS_EXT_INVALID
;
3405 gotp
->br_startblock
= 0xffffa5a5a5a5a5a5LL
;
3407 gotp
->br_startblock
= 0xffffa5a5;
3409 prevp
->br_startoff
= NULLFILEOFF
;
3411 ep
= xfs_iext_bno_to_ext(ifp
, bno
, &lastx
);
3413 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
- 1), prevp
);
3415 if (lastx
< (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
3416 xfs_bmbt_get_all(ep
, gotp
);
3430 * Search the extents list for the inode, for the extent containing bno.
3431 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3432 * *eofp will be set, and *prevp will contain the last entry (null if none).
3433 * Else, *lastxp will be set to the index of the found
3434 * entry; *gotp will contain the entry.
3436 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
3437 xfs_bmap_search_extents(
3438 xfs_inode_t
*ip
, /* incore inode pointer */
3439 xfs_fileoff_t bno
, /* block number searched for */
3440 int fork
, /* data or attr fork */
3441 int *eofp
, /* out: end of file found */
3442 xfs_extnum_t
*lastxp
, /* out: last extent index */
3443 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
3444 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
3446 xfs_ifork_t
*ifp
; /* inode fork pointer */
3447 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
3449 XFS_STATS_INC(xs_look_exlist
);
3450 ifp
= XFS_IFORK_PTR(ip
, fork
);
3452 ep
= xfs_bmap_search_multi_extents(ifp
, bno
, eofp
, lastxp
, gotp
, prevp
);
3454 if (unlikely(!(gotp
->br_startblock
) && (*lastxp
!= NULLEXTNUM
) &&
3455 !(XFS_IS_REALTIME_INODE(ip
) && fork
== XFS_DATA_FORK
))) {
3456 xfs_alert_tag(ip
->i_mount
, XFS_PTAG_FSBLOCK_ZERO
,
3457 "Access to block zero in inode %llu "
3458 "start_block: %llx start_off: %llx "
3459 "blkcnt: %llx extent-state: %x lastx: %x\n",
3460 (unsigned long long)ip
->i_ino
,
3461 (unsigned long long)gotp
->br_startblock
,
3462 (unsigned long long)gotp
->br_startoff
,
3463 (unsigned long long)gotp
->br_blockcount
,
3464 gotp
->br_state
, *lastxp
);
3465 *lastxp
= NULLEXTNUM
;
3473 * Compute the worst-case number of indirect blocks that will be used
3474 * for ip's delayed extent of length "len".
3476 STATIC xfs_filblks_t
3477 xfs_bmap_worst_indlen(
3478 xfs_inode_t
*ip
, /* incore inode pointer */
3479 xfs_filblks_t len
) /* delayed extent length */
3481 int level
; /* btree level number */
3482 int maxrecs
; /* maximum record count at this level */
3483 xfs_mount_t
*mp
; /* mount structure */
3484 xfs_filblks_t rval
; /* return value */
3487 maxrecs
= mp
->m_bmap_dmxr
[0];
3488 for (level
= 0, rval
= 0;
3489 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
3492 do_div(len
, maxrecs
);
3495 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
3498 maxrecs
= mp
->m_bmap_dmxr
[1];
3504 * Convert inode from non-attributed to attributed.
3505 * Must not be in a transaction, ip must not be locked.
3507 int /* error code */
3508 xfs_bmap_add_attrfork(
3509 xfs_inode_t
*ip
, /* incore inode pointer */
3510 int size
, /* space new attribute needs */
3511 int rsvd
) /* xact may use reserved blks */
3513 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
3514 xfs_bmap_free_t flist
; /* freed extent records */
3515 xfs_mount_t
*mp
; /* mount structure */
3516 xfs_trans_t
*tp
; /* transaction pointer */
3517 int blks
; /* space reservation */
3518 int version
= 1; /* superblock attr version */
3519 int committed
; /* xaction was committed */
3520 int logflags
; /* logging flags */
3521 int error
; /* error return value */
3523 ASSERT(XFS_IFORK_Q(ip
) == 0);
3524 ASSERT(ip
->i_df
.if_ext_max
==
3525 XFS_IFORK_DSIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
));
3528 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
3529 tp
= xfs_trans_alloc(mp
, XFS_TRANS_ADDAFORK
);
3530 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
3532 tp
->t_flags
|= XFS_TRANS_RESERVE
;
3533 if ((error
= xfs_trans_reserve(tp
, blks
, XFS_ADDAFORK_LOG_RES(mp
), 0,
3534 XFS_TRANS_PERM_LOG_RES
, XFS_ADDAFORK_LOG_COUNT
)))
3536 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3537 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
3538 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
3539 XFS_QMOPT_RES_REGBLKS
);
3541 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3542 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
);
3545 if (XFS_IFORK_Q(ip
))
3547 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
3549 * For inodes coming from pre-6.2 filesystems.
3551 ASSERT(ip
->i_d
.di_aformat
== 0);
3552 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
3554 ASSERT(ip
->i_d
.di_anextents
== 0);
3556 xfs_trans_ijoin_ref(tp
, ip
, XFS_ILOCK_EXCL
);
3557 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
3559 switch (ip
->i_d
.di_format
) {
3560 case XFS_DINODE_FMT_DEV
:
3561 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
3563 case XFS_DINODE_FMT_UUID
:
3564 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
3566 case XFS_DINODE_FMT_LOCAL
:
3567 case XFS_DINODE_FMT_EXTENTS
:
3568 case XFS_DINODE_FMT_BTREE
:
3569 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
3570 if (!ip
->i_d
.di_forkoff
)
3571 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
3572 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
3577 error
= XFS_ERROR(EINVAL
);
3580 ip
->i_df
.if_ext_max
=
3581 XFS_IFORK_DSIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
);
3582 ASSERT(ip
->i_afp
== NULL
);
3583 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
3584 ip
->i_afp
->if_ext_max
=
3585 XFS_IFORK_ASIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
);
3586 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
3588 xfs_bmap_init(&flist
, &firstblock
);
3589 switch (ip
->i_d
.di_format
) {
3590 case XFS_DINODE_FMT_LOCAL
:
3591 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &flist
,
3594 case XFS_DINODE_FMT_EXTENTS
:
3595 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
3598 case XFS_DINODE_FMT_BTREE
:
3599 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &flist
,
3607 xfs_trans_log_inode(tp
, ip
, logflags
);
3610 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
3611 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
3612 __int64_t sbfields
= 0;
3614 spin_lock(&mp
->m_sb_lock
);
3615 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
3616 xfs_sb_version_addattr(&mp
->m_sb
);
3617 sbfields
|= XFS_SB_VERSIONNUM
;
3619 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
3620 xfs_sb_version_addattr2(&mp
->m_sb
);
3621 sbfields
|= (XFS_SB_VERSIONNUM
| XFS_SB_FEATURES2
);
3624 spin_unlock(&mp
->m_sb_lock
);
3625 xfs_mod_sb(tp
, sbfields
);
3627 spin_unlock(&mp
->m_sb_lock
);
3629 if ((error
= xfs_bmap_finish(&tp
, &flist
, &committed
)))
3631 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
3632 ASSERT(ip
->i_df
.if_ext_max
==
3633 XFS_IFORK_DSIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
));
3636 xfs_bmap_cancel(&flist
);
3638 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3640 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
3641 ASSERT(ip
->i_df
.if_ext_max
==
3642 XFS_IFORK_DSIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
));
3647 * Add the extent to the list of extents to be free at transaction end.
3648 * The list is maintained sorted (by block number).
3653 xfs_fsblock_t bno
, /* fs block number of extent */
3654 xfs_filblks_t len
, /* length of extent */
3655 xfs_bmap_free_t
*flist
, /* list of extents */
3656 xfs_mount_t
*mp
) /* mount point structure */
3658 xfs_bmap_free_item_t
*cur
; /* current (next) element */
3659 xfs_bmap_free_item_t
*new; /* new element */
3660 xfs_bmap_free_item_t
*prev
; /* previous element */
3662 xfs_agnumber_t agno
;
3663 xfs_agblock_t agbno
;
3665 ASSERT(bno
!= NULLFSBLOCK
);
3667 ASSERT(len
<= MAXEXTLEN
);
3668 ASSERT(!isnullstartblock(bno
));
3669 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
3670 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
3671 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
3672 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
3673 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
3674 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
3676 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
3677 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
3678 new->xbfi_startblock
= bno
;
3679 new->xbfi_blockcount
= (xfs_extlen_t
)len
;
3680 for (prev
= NULL
, cur
= flist
->xbf_first
;
3682 prev
= cur
, cur
= cur
->xbfi_next
) {
3683 if (cur
->xbfi_startblock
>= bno
)
3687 prev
->xbfi_next
= new;
3689 flist
->xbf_first
= new;
3690 new->xbfi_next
= cur
;
3695 * Compute and fill in the value of the maximum depth of a bmap btree
3696 * in this filesystem. Done once, during mount.
3699 xfs_bmap_compute_maxlevels(
3700 xfs_mount_t
*mp
, /* file system mount structure */
3701 int whichfork
) /* data or attr fork */
3703 int level
; /* btree level */
3704 uint maxblocks
; /* max blocks at this level */
3705 uint maxleafents
; /* max leaf entries possible */
3706 int maxrootrecs
; /* max records in root block */
3707 int minleafrecs
; /* min records in leaf block */
3708 int minnoderecs
; /* min records in node block */
3709 int sz
; /* root block size */
3712 * The maximum number of extents in a file, hence the maximum
3713 * number of leaf entries, is controlled by the type of di_nextents
3714 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3715 * (a signed 16-bit number, xfs_aextnum_t).
3717 * Note that we can no longer assume that if we are in ATTR1 that
3718 * the fork offset of all the inodes will be
3719 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3720 * with ATTR2 and then mounted back with ATTR1, keeping the
3721 * di_forkoff's fixed but probably at various positions. Therefore,
3722 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3723 * of a minimum size available.
3725 if (whichfork
== XFS_DATA_FORK
) {
3726 maxleafents
= MAXEXTNUM
;
3727 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
3729 maxleafents
= MAXAEXTNUM
;
3730 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
3732 maxrootrecs
= xfs_bmdr_maxrecs(mp
, sz
, 0);
3733 minleafrecs
= mp
->m_bmap_dmnr
[0];
3734 minnoderecs
= mp
->m_bmap_dmnr
[1];
3735 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
3736 for (level
= 1; maxblocks
> 1; level
++) {
3737 if (maxblocks
<= maxrootrecs
)
3740 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
3742 mp
->m_bm_maxlevels
[whichfork
] = level
;
3746 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3747 * caller. Frees all the extents that need freeing, which must be done
3748 * last due to locking considerations. We never free any extents in
3749 * the first transaction.
3751 * Return 1 if the given transaction was committed and a new one
3752 * started, and 0 otherwise in the committed parameter.
3756 xfs_trans_t
**tp
, /* transaction pointer addr */
3757 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
3758 int *committed
) /* xact committed or not */
3760 xfs_efd_log_item_t
*efd
; /* extent free data */
3761 xfs_efi_log_item_t
*efi
; /* extent free intention */
3762 int error
; /* error return value */
3763 xfs_bmap_free_item_t
*free
; /* free extent item */
3764 unsigned int logres
; /* new log reservation */
3765 unsigned int logcount
; /* new log count */
3766 xfs_mount_t
*mp
; /* filesystem mount structure */
3767 xfs_bmap_free_item_t
*next
; /* next item on free list */
3768 xfs_trans_t
*ntp
; /* new transaction pointer */
3770 ASSERT((*tp
)->t_flags
& XFS_TRANS_PERM_LOG_RES
);
3771 if (flist
->xbf_count
== 0) {
3776 efi
= xfs_trans_get_efi(ntp
, flist
->xbf_count
);
3777 for (free
= flist
->xbf_first
; free
; free
= free
->xbfi_next
)
3778 xfs_trans_log_efi_extent(ntp
, efi
, free
->xbfi_startblock
,
3779 free
->xbfi_blockcount
);
3780 logres
= ntp
->t_log_res
;
3781 logcount
= ntp
->t_log_count
;
3782 ntp
= xfs_trans_dup(*tp
);
3783 error
= xfs_trans_commit(*tp
, 0);
3787 * We have a new transaction, so we should return committed=1,
3788 * even though we're returning an error.
3794 * transaction commit worked ok so we can drop the extra ticket
3795 * reference that we gained in xfs_trans_dup()
3797 xfs_log_ticket_put(ntp
->t_ticket
);
3799 if ((error
= xfs_trans_reserve(ntp
, 0, logres
, 0, XFS_TRANS_PERM_LOG_RES
,
3802 efd
= xfs_trans_get_efd(ntp
, efi
, flist
->xbf_count
);
3803 for (free
= flist
->xbf_first
; free
!= NULL
; free
= next
) {
3804 next
= free
->xbfi_next
;
3805 if ((error
= xfs_free_extent(ntp
, free
->xbfi_startblock
,
3806 free
->xbfi_blockcount
))) {
3808 * The bmap free list will be cleaned up at a
3809 * higher level. The EFI will be canceled when
3810 * this transaction is aborted.
3811 * Need to force shutdown here to make sure it
3812 * happens, since this transaction may not be
3816 if (!XFS_FORCED_SHUTDOWN(mp
))
3817 xfs_force_shutdown(mp
,
3818 (error
== EFSCORRUPTED
) ?
3819 SHUTDOWN_CORRUPT_INCORE
:
3820 SHUTDOWN_META_IO_ERROR
);
3823 xfs_trans_log_efd_extent(ntp
, efd
, free
->xbfi_startblock
,
3824 free
->xbfi_blockcount
);
3825 xfs_bmap_del_free(flist
, NULL
, free
);
3831 * Free up any items left in the list.
3835 xfs_bmap_free_t
*flist
) /* list of bmap_free_items */
3837 xfs_bmap_free_item_t
*free
; /* free list item */
3838 xfs_bmap_free_item_t
*next
;
3840 if (flist
->xbf_count
== 0)
3842 ASSERT(flist
->xbf_first
!= NULL
);
3843 for (free
= flist
->xbf_first
; free
; free
= next
) {
3844 next
= free
->xbfi_next
;
3845 xfs_bmap_del_free(flist
, NULL
, free
);
3847 ASSERT(flist
->xbf_count
== 0);
3851 * Returns the file-relative block number of the first unused block(s)
3852 * in the file with at least "len" logically contiguous blocks free.
3853 * This is the lowest-address hole if the file has holes, else the first block
3854 * past the end of file.
3855 * Return 0 if the file is currently local (in-inode).
3858 xfs_bmap_first_unused(
3859 xfs_trans_t
*tp
, /* transaction pointer */
3860 xfs_inode_t
*ip
, /* incore inode */
3861 xfs_extlen_t len
, /* size of hole to find */
3862 xfs_fileoff_t
*first_unused
, /* unused block */
3863 int whichfork
) /* data or attr fork */
3865 int error
; /* error return value */
3866 int idx
; /* extent record index */
3867 xfs_ifork_t
*ifp
; /* inode fork pointer */
3868 xfs_fileoff_t lastaddr
; /* last block number seen */
3869 xfs_fileoff_t lowest
; /* lowest useful block */
3870 xfs_fileoff_t max
; /* starting useful block */
3871 xfs_fileoff_t off
; /* offset for this block */
3872 xfs_extnum_t nextents
; /* number of extent entries */
3874 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
3875 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
3876 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
3877 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
3881 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3882 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
3883 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
3885 lowest
= *first_unused
;
3886 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
3887 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
3888 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
3889 off
= xfs_bmbt_get_startoff(ep
);
3891 * See if the hole before this extent will work.
3893 if (off
>= lowest
+ len
&& off
- max
>= len
) {
3894 *first_unused
= max
;
3897 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
3898 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
3900 *first_unused
= max
;
3905 * Returns the file-relative block number of the last block + 1 before
3906 * last_block (input value) in the file.
3907 * This is not based on i_size, it is based on the extent records.
3908 * Returns 0 for local files, as they do not have extent records.
3911 xfs_bmap_last_before(
3912 xfs_trans_t
*tp
, /* transaction pointer */
3913 xfs_inode_t
*ip
, /* incore inode */
3914 xfs_fileoff_t
*last_block
, /* last block */
3915 int whichfork
) /* data or attr fork */
3917 xfs_fileoff_t bno
; /* input file offset */
3918 int eof
; /* hit end of file */
3919 xfs_bmbt_rec_host_t
*ep
; /* pointer to last extent */
3920 int error
; /* error return value */
3921 xfs_bmbt_irec_t got
; /* current extent value */
3922 xfs_ifork_t
*ifp
; /* inode fork pointer */
3923 xfs_extnum_t lastx
; /* last extent used */
3924 xfs_bmbt_irec_t prev
; /* previous extent value */
3926 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
3927 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3928 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
3929 return XFS_ERROR(EIO
);
3930 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
3934 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3935 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
3936 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
3938 bno
= *last_block
- 1;
3939 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
3941 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
3942 if (prev
.br_startoff
== NULLFILEOFF
)
3945 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
3948 * Otherwise *last_block is already the right answer.
3954 * Returns the file-relative block number of the first block past eof in
3955 * the file. This is not based on i_size, it is based on the extent records.
3956 * Returns 0 for local files, as they do not have extent records.
3959 xfs_bmap_last_offset(
3960 xfs_trans_t
*tp
, /* transaction pointer */
3961 xfs_inode_t
*ip
, /* incore inode */
3962 xfs_fileoff_t
*last_block
, /* last block */
3963 int whichfork
) /* data or attr fork */
3965 xfs_bmbt_rec_host_t
*ep
; /* pointer to last extent */
3966 int error
; /* error return value */
3967 xfs_ifork_t
*ifp
; /* inode fork pointer */
3968 xfs_extnum_t nextents
; /* number of extent entries */
3970 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
3971 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3972 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
3973 return XFS_ERROR(EIO
);
3974 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
3978 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3979 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
3980 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
3982 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
3987 ep
= xfs_iext_get_ext(ifp
, nextents
- 1);
3988 *last_block
= xfs_bmbt_get_startoff(ep
) + xfs_bmbt_get_blockcount(ep
);
3993 * Returns whether the selected fork of the inode has exactly one
3994 * block or not. For the data fork we check this matches di_size,
3995 * implying the file's range is 0..bsize-1.
3997 int /* 1=>1 block, 0=>otherwise */
3999 xfs_inode_t
*ip
, /* incore inode */
4000 int whichfork
) /* data or attr fork */
4002 xfs_bmbt_rec_host_t
*ep
; /* ptr to fork's extent */
4003 xfs_ifork_t
*ifp
; /* inode fork pointer */
4004 int rval
; /* return value */
4005 xfs_bmbt_irec_t s
; /* internal version of extent */
4008 if (whichfork
== XFS_DATA_FORK
) {
4009 return S_ISREG(ip
->i_d
.di_mode
) ?
4010 (ip
->i_size
== ip
->i_mount
->m_sb
.sb_blocksize
) :
4011 (ip
->i_d
.di_size
== ip
->i_mount
->m_sb
.sb_blocksize
);
4014 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
4016 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4018 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4019 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
4020 ep
= xfs_iext_get_ext(ifp
, 0);
4021 xfs_bmbt_get_all(ep
, &s
);
4022 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
4023 if (rval
&& whichfork
== XFS_DATA_FORK
)
4024 ASSERT(ip
->i_size
== ip
->i_mount
->m_sb
.sb_blocksize
);
4029 xfs_bmap_sanity_check(
4030 struct xfs_mount
*mp
,
4034 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
4036 if (block
->bb_magic
!= cpu_to_be32(XFS_BMAP_MAGIC
) ||
4037 be16_to_cpu(block
->bb_level
) != level
||
4038 be16_to_cpu(block
->bb_numrecs
) == 0 ||
4039 be16_to_cpu(block
->bb_numrecs
) > mp
->m_bmap_dmxr
[level
!= 0])
4045 * Read in the extents to if_extents.
4046 * All inode fields are set up by caller, we just traverse the btree
4047 * and copy the records in. If the file system cannot contain unwritten
4048 * extents, the records are checked for no "state" flags.
4051 xfs_bmap_read_extents(
4052 xfs_trans_t
*tp
, /* transaction pointer */
4053 xfs_inode_t
*ip
, /* incore inode */
4054 int whichfork
) /* data or attr fork */
4056 struct xfs_btree_block
*block
; /* current btree block */
4057 xfs_fsblock_t bno
; /* block # of "block" */
4058 xfs_buf_t
*bp
; /* buffer for "block" */
4059 int error
; /* error return value */
4060 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
4061 xfs_extnum_t i
, j
; /* index into the extents list */
4062 xfs_ifork_t
*ifp
; /* fork structure */
4063 int level
; /* btree level, for checking */
4064 xfs_mount_t
*mp
; /* file system mount structure */
4065 __be64
*pp
; /* pointer to block address */
4067 xfs_extnum_t room
; /* number of entries there's room for */
4071 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4072 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
4073 XFS_EXTFMT_INODE(ip
);
4074 block
= ifp
->if_broot
;
4076 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4078 level
= be16_to_cpu(block
->bb_level
);
4080 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
4081 bno
= be64_to_cpu(*pp
);
4082 ASSERT(bno
!= NULLDFSBNO
);
4083 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
4084 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
4086 * Go down the tree until leaf level is reached, following the first
4087 * pointer (leftmost) at each level.
4089 while (level
-- > 0) {
4090 if ((error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
4091 XFS_BMAP_BTREE_REF
)))
4093 block
= XFS_BUF_TO_BLOCK(bp
);
4094 XFS_WANT_CORRUPTED_GOTO(
4095 xfs_bmap_sanity_check(mp
, bp
, level
),
4099 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
4100 bno
= be64_to_cpu(*pp
);
4101 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
4102 xfs_trans_brelse(tp
, bp
);
4105 * Here with bp and block set to the leftmost leaf node in the tree.
4107 room
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
4110 * Loop over all leaf nodes. Copy information to the extent records.
4113 xfs_bmbt_rec_t
*frp
;
4114 xfs_fsblock_t nextbno
;
4115 xfs_extnum_t num_recs
;
4119 num_recs
= xfs_btree_get_numrecs(block
);
4120 if (unlikely(i
+ num_recs
> room
)) {
4121 ASSERT(i
+ num_recs
<= room
);
4122 xfs_warn(ip
->i_mount
,
4123 "corrupt dinode %Lu, (btree extents).",
4124 (unsigned long long) ip
->i_ino
);
4125 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
4126 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
4129 XFS_WANT_CORRUPTED_GOTO(
4130 xfs_bmap_sanity_check(mp
, bp
, 0),
4133 * Read-ahead the next leaf block, if any.
4135 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
4136 if (nextbno
!= NULLFSBLOCK
)
4137 xfs_btree_reada_bufl(mp
, nextbno
, 1);
4139 * Copy records into the extent records.
4141 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
4143 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
4144 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
4145 trp
->l0
= be64_to_cpu(frp
->l0
);
4146 trp
->l1
= be64_to_cpu(frp
->l1
);
4148 if (exntf
== XFS_EXTFMT_NOSTATE
) {
4150 * Check all attribute bmap btree records and
4151 * any "older" data bmap btree records for a
4152 * set bit in the "extent flag" position.
4154 if (unlikely(xfs_check_nostate_extents(ifp
,
4155 start
, num_recs
))) {
4156 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4162 xfs_trans_brelse(tp
, bp
);
4165 * If we've reached the end, stop.
4167 if (bno
== NULLFSBLOCK
)
4169 if ((error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
4170 XFS_BMAP_BTREE_REF
)))
4172 block
= XFS_BUF_TO_BLOCK(bp
);
4174 ASSERT(i
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
4175 ASSERT(i
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
4176 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
4179 xfs_trans_brelse(tp
, bp
);
4180 return XFS_ERROR(EFSCORRUPTED
);
4185 * Add bmap trace insert entries for all the contents of the extent records.
4188 xfs_bmap_trace_exlist(
4189 xfs_inode_t
*ip
, /* incore inode pointer */
4190 xfs_extnum_t cnt
, /* count of entries in the list */
4191 int whichfork
, /* data or attr fork */
4192 unsigned long caller_ip
)
4194 xfs_extnum_t idx
; /* extent record index */
4195 xfs_ifork_t
*ifp
; /* inode fork pointer */
4198 if (whichfork
== XFS_ATTR_FORK
)
4199 state
|= BMAP_ATTRFORK
;
4201 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4202 ASSERT(cnt
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
4203 for (idx
= 0; idx
< cnt
; idx
++)
4204 trace_xfs_extlist(ip
, idx
, whichfork
, caller_ip
);
4208 * Validate that the bmbt_irecs being returned from bmapi are valid
4209 * given the callers original parameters. Specifically check the
4210 * ranges of the returned irecs to ensure that they only extent beyond
4211 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4214 xfs_bmap_validate_ret(
4218 xfs_bmbt_irec_t
*mval
,
4222 int i
; /* index to map values */
4224 ASSERT(ret_nmap
<= nmap
);
4226 for (i
= 0; i
< ret_nmap
; i
++) {
4227 ASSERT(mval
[i
].br_blockcount
> 0);
4228 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
4229 ASSERT(mval
[i
].br_startoff
>= bno
);
4230 ASSERT(mval
[i
].br_blockcount
<= len
);
4231 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
4234 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
4235 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
4239 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
4240 mval
[i
].br_startoff
);
4241 if ((flags
& XFS_BMAPI_WRITE
) && !(flags
& XFS_BMAPI_DELAY
))
4242 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
4243 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
4244 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
4245 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
4252 * Map file blocks to filesystem blocks.
4253 * File range is given by the bno/len pair.
4254 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4255 * into a hole or past eof.
4256 * Only allocates blocks from a single allocation group,
4257 * to avoid locking problems.
4258 * The returned value in "firstblock" from the first call in a transaction
4259 * must be remembered and presented to subsequent calls in "firstblock".
4260 * An upper bound for the number of blocks to be allocated is supplied to
4261 * the first call in "total"; if no allocation group has that many free
4262 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4266 xfs_trans_t
*tp
, /* transaction pointer */
4267 xfs_inode_t
*ip
, /* incore inode */
4268 xfs_fileoff_t bno
, /* starting file offs. mapped */
4269 xfs_filblks_t len
, /* length to map in file */
4270 int flags
, /* XFS_BMAPI_... */
4271 xfs_fsblock_t
*firstblock
, /* first allocated block
4272 controls a.g. for allocs */
4273 xfs_extlen_t total
, /* total blocks needed */
4274 xfs_bmbt_irec_t
*mval
, /* output: map values */
4275 int *nmap
, /* i/o: mval size/count */
4276 xfs_bmap_free_t
*flist
) /* i/o: list extents to free */
4278 xfs_fsblock_t abno
; /* allocated block number */
4279 xfs_extlen_t alen
; /* allocated extent length */
4280 xfs_fileoff_t aoff
; /* allocated file offset */
4281 xfs_bmalloca_t bma
= { 0 }; /* args for xfs_bmap_alloc */
4282 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
4283 xfs_fileoff_t end
; /* end of mapped file region */
4284 int eof
; /* we've hit the end of extents */
4285 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
4286 int error
; /* error return */
4287 xfs_bmbt_irec_t got
; /* current file extent record */
4288 xfs_ifork_t
*ifp
; /* inode fork pointer */
4289 xfs_extlen_t indlen
; /* indirect blocks length */
4290 xfs_extnum_t lastx
; /* last useful extent number */
4291 int logflags
; /* flags for transaction logging */
4292 xfs_extlen_t minleft
; /* min blocks left after allocation */
4293 xfs_extlen_t minlen
; /* min allocation size */
4294 xfs_mount_t
*mp
; /* xfs mount structure */
4295 int n
; /* current extent index */
4296 int nallocs
; /* number of extents alloc'd */
4297 xfs_fileoff_t obno
; /* old block number (offset) */
4298 xfs_bmbt_irec_t prev
; /* previous file extent record */
4299 int tmp_logflags
; /* temp flags holder */
4300 int whichfork
; /* data or attr fork */
4301 char inhole
; /* current location is hole in file */
4302 char wasdelay
; /* old extent was delayed */
4303 char wr
; /* this is a write request */
4304 char rt
; /* this is a realtime file */
4306 xfs_fileoff_t orig_bno
; /* original block number value */
4307 int orig_flags
; /* original flags arg value */
4308 xfs_filblks_t orig_len
; /* original value of len arg */
4309 xfs_bmbt_irec_t
*orig_mval
; /* original value of mval */
4310 int orig_nmap
; /* original value of *nmap */
4319 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
|| !(flags
& XFS_BMAPI_WRITE
));
4320 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4321 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4323 if (unlikely(XFS_TEST_ERROR(
4324 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4325 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
4326 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
),
4327 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4328 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW
, mp
);
4329 return XFS_ERROR(EFSCORRUPTED
);
4331 if (XFS_FORCED_SHUTDOWN(mp
))
4332 return XFS_ERROR(EIO
);
4333 rt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4334 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4335 ASSERT(ifp
->if_ext_max
==
4336 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
4337 if ((wr
= (flags
& XFS_BMAPI_WRITE
)) != 0)
4338 XFS_STATS_INC(xs_blk_mapw
);
4340 XFS_STATS_INC(xs_blk_mapr
);
4342 * IGSTATE flag is used to combine extents which
4343 * differ only due to the state of the extents.
4344 * This technique is used from xfs_getbmap()
4345 * when the caller does not wish to see the
4346 * separation (which is the default).
4348 * This technique is also used when writing a
4349 * buffer which has been partially written,
4350 * (usually by being flushed during a chunkread),
4351 * to ensure one write takes place. This also
4352 * prevents a change in the xfs inode extents at
4353 * this time, intentionally. This change occurs
4354 * on completion of the write operation, in
4355 * xfs_strat_comp(), where the xfs_bmapi() call
4356 * is transactioned, and the extents combined.
4358 if ((flags
& XFS_BMAPI_IGSTATE
) && wr
) /* if writing unwritten space */
4359 wr
= 0; /* no allocations are allowed */
4360 ASSERT(wr
|| !(flags
& XFS_BMAPI_DELAY
));
4364 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
4366 if ((error
= xfs_bmap_local_to_extents(tp
, ip
,
4367 firstblock
, total
, &logflags
, whichfork
)))
4370 if (wr
&& *firstblock
== NULLFSBLOCK
) {
4371 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4372 minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4377 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
4378 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
4380 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
4387 while (bno
< end
&& n
< *nmap
) {
4389 * Reading past eof, act as though there's a hole
4393 got
.br_startoff
= end
;
4394 inhole
= eof
|| got
.br_startoff
> bno
;
4395 wasdelay
= wr
&& !inhole
&& !(flags
& XFS_BMAPI_DELAY
) &&
4396 isnullstartblock(got
.br_startblock
);
4398 * First, deal with the hole before the allocated space
4399 * that we found, if any.
4401 if (wr
&& (inhole
|| wasdelay
)) {
4403 * For the wasdelay case, we could also just
4404 * allocate the stuff asked for in this bmap call
4405 * but that wouldn't be as good.
4408 alen
= (xfs_extlen_t
)got
.br_blockcount
;
4409 aoff
= got
.br_startoff
;
4410 if (lastx
!= NULLEXTNUM
&& lastx
) {
4411 ep
= xfs_iext_get_ext(ifp
, lastx
- 1);
4412 xfs_bmbt_get_all(ep
, &prev
);
4415 alen
= (xfs_extlen_t
)
4416 XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
4418 alen
= (xfs_extlen_t
)
4419 XFS_FILBLKS_MIN(alen
,
4420 got
.br_startoff
- bno
);
4423 minlen
= (flags
& XFS_BMAPI_CONTIG
) ? alen
: 1;
4424 if (flags
& XFS_BMAPI_DELAY
) {
4427 /* Figure out the extent size, adjust alen */
4428 extsz
= xfs_get_extsz_hint(ip
);
4431 * make sure we don't exceed a single
4432 * extent length when we align the
4433 * extent by reducing length we are
4434 * going to allocate by the maximum
4435 * amount extent size aligment may
4438 alen
= XFS_FILBLKS_MIN(len
,
4439 MAXEXTLEN
- (2 * extsz
- 1));
4440 error
= xfs_bmap_extsize_align(mp
,
4443 flags
&XFS_BMAPI_DELAY
,
4444 flags
&XFS_BMAPI_CONVERT
,
4450 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4453 * Make a transaction-less quota reservation for
4454 * delayed allocation blocks. This number gets
4455 * adjusted later. We return if we haven't
4456 * allocated blocks already inside this loop.
4458 error
= xfs_trans_reserve_quota_nblks(
4459 NULL
, ip
, (long)alen
, 0,
4460 rt
? XFS_QMOPT_RES_RTBLKS
:
4461 XFS_QMOPT_RES_REGBLKS
);
4465 ASSERT(cur
== NULL
);
4472 * Split changing sb for alen and indlen since
4473 * they could be coming from different places.
4475 indlen
= (xfs_extlen_t
)
4476 xfs_bmap_worst_indlen(ip
, alen
);
4480 error
= xfs_mod_incore_sb(mp
,
4482 -((int64_t)extsz
), 0);
4484 error
= xfs_icsb_modify_counters(mp
,
4486 -((int64_t)alen
), 0);
4489 error
= xfs_icsb_modify_counters(mp
,
4491 -((int64_t)indlen
), 0);
4493 xfs_mod_incore_sb(mp
,
4497 xfs_icsb_modify_counters(mp
,
4503 if (XFS_IS_QUOTA_ON(mp
))
4504 /* unreserve the blocks now */
4506 xfs_trans_unreserve_quota_nblks(
4509 XFS_QMOPT_RES_RTBLKS
:
4510 XFS_QMOPT_RES_REGBLKS
);
4514 ip
->i_delayed_blks
+= alen
;
4515 abno
= nullstartblock(indlen
);
4518 * If first time, allocate and fill in
4519 * once-only bma fields.
4521 if (bma
.ip
== NULL
) {
4529 /* Indicate if this is the first user data
4530 * in the file, or just any user data.
4532 if (!(flags
& XFS_BMAPI_METADATA
)) {
4533 bma
.userdata
= (aoff
== 0) ?
4534 XFS_ALLOC_INITIAL_USER_DATA
:
4538 * Fill in changeable bma fields.
4541 bma
.firstblock
= *firstblock
;
4544 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4545 bma
.wasdel
= wasdelay
;
4546 bma
.minlen
= minlen
;
4547 bma
.low
= flist
->xbf_low
;
4548 bma
.minleft
= minleft
;
4550 * Only want to do the alignment at the
4551 * eof if it is userdata and allocation length
4552 * is larger than a stripe unit.
4554 if (mp
->m_dalign
&& alen
>= mp
->m_dalign
&&
4555 (!(flags
& XFS_BMAPI_METADATA
)) &&
4556 (whichfork
== XFS_DATA_FORK
)) {
4557 if ((error
= xfs_bmap_isaeof(ip
, aoff
,
4558 whichfork
, &bma
.aeof
)))
4565 if ((error
= xfs_bmap_alloc(&bma
)))
4568 * Copy out result fields.
4571 if ((flist
->xbf_low
= bma
.low
))
4575 ASSERT(*firstblock
== NULLFSBLOCK
||
4576 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
4577 XFS_FSB_TO_AGNO(mp
, bma
.firstblock
) ||
4579 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
4580 XFS_FSB_TO_AGNO(mp
, bma
.firstblock
)));
4581 *firstblock
= bma
.firstblock
;
4583 cur
->bc_private
.b
.firstblock
=
4585 if (abno
== NULLFSBLOCK
)
4587 if ((ifp
->if_flags
& XFS_IFBROOT
) && !cur
) {
4588 cur
= xfs_bmbt_init_cursor(mp
, tp
,
4590 cur
->bc_private
.b
.firstblock
=
4592 cur
->bc_private
.b
.flist
= flist
;
4595 * Bump the number of extents we've allocated
4601 cur
->bc_private
.b
.flags
=
4602 wasdelay
? XFS_BTCUR_BPRV_WASDEL
: 0;
4603 got
.br_startoff
= aoff
;
4604 got
.br_startblock
= abno
;
4605 got
.br_blockcount
= alen
;
4606 got
.br_state
= XFS_EXT_NORM
; /* assume normal */
4608 * Determine state of extent, and the filesystem.
4609 * A wasdelay extent has been initialized, so
4610 * shouldn't be flagged as unwritten.
4612 if (wr
&& xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
4613 if (!wasdelay
&& (flags
& XFS_BMAPI_PREALLOC
))
4614 got
.br_state
= XFS_EXT_UNWRITTEN
;
4616 error
= xfs_bmap_add_extent(tp
, ip
, &lastx
, &cur
, &got
,
4617 firstblock
, flist
, &tmp_logflags
,
4619 logflags
|= tmp_logflags
;
4622 ep
= xfs_iext_get_ext(ifp
, lastx
);
4623 xfs_bmbt_get_all(ep
, &got
);
4624 ASSERT(got
.br_startoff
<= aoff
);
4625 ASSERT(got
.br_startoff
+ got
.br_blockcount
>=
4628 if (flags
& XFS_BMAPI_DELAY
) {
4629 ASSERT(isnullstartblock(got
.br_startblock
));
4630 ASSERT(startblockval(got
.br_startblock
) > 0);
4632 ASSERT(got
.br_state
== XFS_EXT_NORM
||
4633 got
.br_state
== XFS_EXT_UNWRITTEN
);
4636 * Fall down into the found allocated space case.
4638 } else if (inhole
) {
4640 * Reading in a hole.
4642 mval
->br_startoff
= bno
;
4643 mval
->br_startblock
= HOLESTARTBLOCK
;
4644 mval
->br_blockcount
=
4645 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4646 mval
->br_state
= XFS_EXT_NORM
;
4647 bno
+= mval
->br_blockcount
;
4648 len
-= mval
->br_blockcount
;
4654 * Then deal with the allocated space we found.
4657 if (!(flags
& XFS_BMAPI_ENTIRE
) &&
4658 (got
.br_startoff
+ got
.br_blockcount
> obno
)) {
4661 ASSERT((bno
>= obno
) || (n
== 0));
4663 mval
->br_startoff
= bno
;
4664 if (isnullstartblock(got
.br_startblock
)) {
4665 ASSERT(!wr
|| (flags
& XFS_BMAPI_DELAY
));
4666 mval
->br_startblock
= DELAYSTARTBLOCK
;
4668 mval
->br_startblock
=
4670 (bno
- got
.br_startoff
);
4672 * Return the minimum of what we got and what we
4673 * asked for for the length. We can use the len
4674 * variable here because it is modified below
4675 * and we could have been there before coming
4676 * here if the first part of the allocation
4677 * didn't overlap what was asked for.
4679 mval
->br_blockcount
=
4680 XFS_FILBLKS_MIN(end
- bno
, got
.br_blockcount
-
4681 (bno
- got
.br_startoff
));
4682 mval
->br_state
= got
.br_state
;
4683 ASSERT(mval
->br_blockcount
<= len
);
4686 if (isnullstartblock(mval
->br_startblock
)) {
4687 ASSERT(!wr
|| (flags
& XFS_BMAPI_DELAY
));
4688 mval
->br_startblock
= DELAYSTARTBLOCK
;
4693 * Check if writing previously allocated but
4694 * unwritten extents.
4697 ((mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4698 ((flags
& (XFS_BMAPI_PREALLOC
|XFS_BMAPI_DELAY
)) == 0)) ||
4699 (mval
->br_state
== XFS_EXT_NORM
&&
4700 ((flags
& (XFS_BMAPI_PREALLOC
|XFS_BMAPI_CONVERT
)) ==
4701 (XFS_BMAPI_PREALLOC
|XFS_BMAPI_CONVERT
))))) {
4703 * Modify (by adding) the state flag, if writing.
4705 ASSERT(mval
->br_blockcount
<= len
);
4706 if ((ifp
->if_flags
& XFS_IFBROOT
) && !cur
) {
4707 cur
= xfs_bmbt_init_cursor(mp
,
4709 cur
->bc_private
.b
.firstblock
=
4711 cur
->bc_private
.b
.flist
= flist
;
4713 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4715 : XFS_EXT_UNWRITTEN
;
4716 error
= xfs_bmap_add_extent(tp
, ip
, &lastx
, &cur
, mval
,
4717 firstblock
, flist
, &tmp_logflags
,
4719 logflags
|= tmp_logflags
;
4722 ep
= xfs_iext_get_ext(ifp
, lastx
);
4723 xfs_bmbt_get_all(ep
, &got
);
4725 * We may have combined previously unwritten
4726 * space with written space, so generate
4729 if (mval
->br_blockcount
< len
)
4733 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
4734 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
4735 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
4736 (mval
->br_blockcount
<= len
) ||
4737 (mval
->br_startoff
< obno
));
4738 bno
= mval
->br_startoff
+ mval
->br_blockcount
;
4740 if (n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
4741 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
4742 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
4743 ASSERT(mval
->br_state
== mval
[-1].br_state
);
4744 mval
[-1].br_blockcount
= mval
->br_blockcount
;
4745 mval
[-1].br_state
= mval
->br_state
;
4746 } else if (n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
4747 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
4748 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
4749 mval
->br_startblock
==
4750 mval
[-1].br_startblock
+ mval
[-1].br_blockcount
&&
4751 ((flags
& XFS_BMAPI_IGSTATE
) ||
4752 mval
[-1].br_state
== mval
->br_state
)) {
4753 ASSERT(mval
->br_startoff
==
4754 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
4755 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4757 mval
->br_startblock
== DELAYSTARTBLOCK
&&
4758 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
4759 mval
->br_startoff
==
4760 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
4761 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4762 mval
[-1].br_state
= mval
->br_state
;
4763 } else if (!((n
== 0) &&
4764 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
4770 * If we're done, stop now. Stop when we've allocated
4771 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4772 * the transaction may get too big.
4774 if (bno
>= end
|| n
>= *nmap
|| nallocs
>= *nmap
)
4777 * Else go on to the next record.
4780 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
)) {
4781 ep
= xfs_iext_get_ext(ifp
, lastx
);
4782 xfs_bmbt_get_all(ep
, &got
);
4789 * Transform from btree to extents, give it cur.
4791 if (tp
&& XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
4792 XFS_IFORK_NEXTENTS(ip
, whichfork
) <= ifp
->if_ext_max
) {
4794 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
,
4795 &tmp_logflags
, whichfork
);
4796 logflags
|= tmp_logflags
;
4800 ASSERT(ifp
->if_ext_max
==
4801 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
4802 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4803 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ifp
->if_ext_max
);
4807 * Log everything. Do this after conversion, there's no point in
4808 * logging the extent records if we've converted to btree format.
4810 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
4811 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4812 logflags
&= ~xfs_ilog_fext(whichfork
);
4813 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
4814 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4815 logflags
&= ~xfs_ilog_fbroot(whichfork
);
4817 * Log whatever the flags say, even if error. Otherwise we might miss
4818 * detecting a case where the data is changed, there's an error,
4819 * and it's not logged so we don't shutdown when we should.
4823 xfs_trans_log_inode(tp
, ip
, logflags
);
4827 ASSERT(*firstblock
== NULLFSBLOCK
||
4828 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
4830 cur
->bc_private
.b
.firstblock
) ||
4832 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
4834 cur
->bc_private
.b
.firstblock
)));
4835 *firstblock
= cur
->bc_private
.b
.firstblock
;
4837 xfs_btree_del_cursor(cur
,
4838 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4841 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4847 * Map file blocks to filesystem blocks, simple version.
4848 * One block (extent) only, read-only.
4849 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
4850 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
4851 * was set and all the others were clear.
4855 xfs_trans_t
*tp
, /* transaction pointer */
4856 xfs_inode_t
*ip
, /* incore inode */
4857 int whichfork
, /* data or attr fork */
4858 xfs_fsblock_t
*fsb
, /* output: mapped block */
4859 xfs_fileoff_t bno
) /* starting file offs. mapped */
4861 int eof
; /* we've hit the end of extents */
4862 int error
; /* error return */
4863 xfs_bmbt_irec_t got
; /* current file extent record */
4864 xfs_ifork_t
*ifp
; /* inode fork pointer */
4865 xfs_extnum_t lastx
; /* last useful extent number */
4866 xfs_bmbt_irec_t prev
; /* previous file extent record */
4868 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4870 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
4871 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)) {
4872 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW
,
4874 return XFS_ERROR(EFSCORRUPTED
);
4876 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
4877 return XFS_ERROR(EIO
);
4878 XFS_STATS_INC(xs_blk_mapr
);
4879 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
4880 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
4882 (void)xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
4885 * Reading past eof, act as though there's a hole
4888 if (eof
|| got
.br_startoff
> bno
) {
4892 ASSERT(!isnullstartblock(got
.br_startblock
));
4893 ASSERT(bno
< got
.br_startoff
+ got
.br_blockcount
);
4894 *fsb
= got
.br_startblock
+ (bno
- got
.br_startoff
);
4899 * Unmap (remove) blocks from a file.
4900 * If nexts is nonzero then the number of extents to remove is limited to
4901 * that value. If not all extents in the block range can be removed then
4906 xfs_trans_t
*tp
, /* transaction pointer */
4907 struct xfs_inode
*ip
, /* incore inode */
4908 xfs_fileoff_t bno
, /* starting offset to unmap */
4909 xfs_filblks_t len
, /* length to unmap in file */
4910 int flags
, /* misc flags */
4911 xfs_extnum_t nexts
, /* number of extents max */
4912 xfs_fsblock_t
*firstblock
, /* first allocated block
4913 controls a.g. for allocs */
4914 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
4915 int *done
) /* set if not done yet */
4917 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
4918 xfs_bmbt_irec_t del
; /* extent being deleted */
4919 int eof
; /* is deleting at eof */
4920 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
4921 int error
; /* error return value */
4922 xfs_extnum_t extno
; /* extent number in list */
4923 xfs_bmbt_irec_t got
; /* current extent record */
4924 xfs_ifork_t
*ifp
; /* inode fork pointer */
4925 int isrt
; /* freeing in rt area */
4926 xfs_extnum_t lastx
; /* last extent index used */
4927 int logflags
; /* transaction logging flags */
4928 xfs_extlen_t mod
; /* rt extent offset */
4929 xfs_mount_t
*mp
; /* mount structure */
4930 xfs_extnum_t nextents
; /* number of file extents */
4931 xfs_bmbt_irec_t prev
; /* previous extent record */
4932 xfs_fileoff_t start
; /* first file offset deleted */
4933 int tmp_logflags
; /* partial logging flags */
4934 int wasdel
; /* was a delayed alloc extent */
4935 int whichfork
; /* data or attribute fork */
4938 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
4940 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4941 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4942 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4944 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4945 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
4946 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
4948 return XFS_ERROR(EFSCORRUPTED
);
4951 if (XFS_FORCED_SHUTDOWN(mp
))
4952 return XFS_ERROR(EIO
);
4956 ASSERT(ifp
->if_ext_max
==
4957 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
4958 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
4959 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
4961 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
4962 if (nextents
== 0) {
4966 XFS_STATS_INC(xs_blk_unmap
);
4967 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4969 bno
= start
+ len
- 1;
4970 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
4974 * Check to see if the given block number is past the end of the
4975 * file, back up to the last block if so...
4978 ep
= xfs_iext_get_ext(ifp
, --lastx
);
4979 xfs_bmbt_get_all(ep
, &got
);
4980 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
4983 if (ifp
->if_flags
& XFS_IFBROOT
) {
4984 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
4985 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
4986 cur
->bc_private
.b
.firstblock
= *firstblock
;
4987 cur
->bc_private
.b
.flist
= flist
;
4988 cur
->bc_private
.b
.flags
= 0;
4992 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
4993 (nexts
== 0 || extno
< nexts
)) {
4995 * Is the found extent after a hole in which bno lives?
4996 * Just back up to the previous extent, if so.
4998 if (got
.br_startoff
> bno
) {
5001 ep
= xfs_iext_get_ext(ifp
, lastx
);
5002 xfs_bmbt_get_all(ep
, &got
);
5005 * Is the last block of this extent before the range
5006 * we're supposed to delete? If so, we're done.
5008 bno
= XFS_FILEOFF_MIN(bno
,
5009 got
.br_startoff
+ got
.br_blockcount
- 1);
5013 * Then deal with the (possibly delayed) allocated space
5018 wasdel
= isnullstartblock(del
.br_startblock
);
5019 if (got
.br_startoff
< start
) {
5020 del
.br_startoff
= start
;
5021 del
.br_blockcount
-= start
- got
.br_startoff
;
5023 del
.br_startblock
+= start
- got
.br_startoff
;
5025 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5026 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5027 sum
= del
.br_startblock
+ del
.br_blockcount
;
5029 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5031 * Realtime extent not lined up at the end.
5032 * The extent could have been split into written
5033 * and unwritten pieces, or we could just be
5034 * unmapping part of it. But we can't really
5035 * get rid of part of a realtime extent.
5037 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5038 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5040 * This piece is unwritten, or we're not
5041 * using unwritten extents. Skip over it.
5044 bno
-= mod
> del
.br_blockcount
?
5045 del
.br_blockcount
: mod
;
5046 if (bno
< got
.br_startoff
) {
5048 xfs_bmbt_get_all(xfs_iext_get_ext(
5054 * It's written, turn it unwritten.
5055 * This is better than zeroing it.
5057 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5058 ASSERT(xfs_trans_get_block_res(tp
) > 0);
5060 * If this spans a realtime extent boundary,
5061 * chop it back to the start of the one we end at.
5063 if (del
.br_blockcount
> mod
) {
5064 del
.br_startoff
+= del
.br_blockcount
- mod
;
5065 del
.br_startblock
+= del
.br_blockcount
- mod
;
5066 del
.br_blockcount
= mod
;
5068 del
.br_state
= XFS_EXT_UNWRITTEN
;
5069 error
= xfs_bmap_add_extent(tp
, ip
, &lastx
, &cur
, &del
,
5070 firstblock
, flist
, &logflags
,
5076 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5078 * Realtime extent is lined up at the end but not
5079 * at the front. We'll get rid of full extents if
5082 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5083 if (del
.br_blockcount
> mod
) {
5084 del
.br_blockcount
-= mod
;
5085 del
.br_startoff
+= mod
;
5086 del
.br_startblock
+= mod
;
5087 } else if ((del
.br_startoff
== start
&&
5088 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5089 xfs_trans_get_block_res(tp
) == 0)) ||
5090 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5092 * Can't make it unwritten. There isn't
5093 * a full extent here so just skip it.
5095 ASSERT(bno
>= del
.br_blockcount
);
5096 bno
-= del
.br_blockcount
;
5097 if (got
.br_startoff
> bno
) {
5099 ep
= xfs_iext_get_ext(ifp
,
5101 xfs_bmbt_get_all(ep
, &got
);
5105 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5107 * This one is already unwritten.
5108 * It must have a written left neighbor.
5109 * Unwrite the killed part of that one and
5113 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
,
5115 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5116 ASSERT(!isnullstartblock(prev
.br_startblock
));
5117 ASSERT(del
.br_startblock
==
5118 prev
.br_startblock
+ prev
.br_blockcount
);
5119 if (prev
.br_startoff
< start
) {
5120 mod
= start
- prev
.br_startoff
;
5121 prev
.br_blockcount
-= mod
;
5122 prev
.br_startblock
+= mod
;
5123 prev
.br_startoff
= start
;
5125 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5127 error
= xfs_bmap_add_extent(tp
, ip
, &lastx
,
5128 &cur
, &prev
, firstblock
, flist
,
5129 &logflags
, XFS_DATA_FORK
);
5134 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5135 del
.br_state
= XFS_EXT_UNWRITTEN
;
5136 error
= xfs_bmap_add_extent(tp
, ip
, &lastx
,
5137 &cur
, &del
, firstblock
, flist
,
5138 &logflags
, XFS_DATA_FORK
);
5145 ASSERT(startblockval(del
.br_startblock
) > 0);
5146 /* Update realtime/data freespace, unreserve quota */
5148 xfs_filblks_t rtexts
;
5150 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5151 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5152 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
5153 (int64_t)rtexts
, 0);
5154 (void)xfs_trans_reserve_quota_nblks(NULL
,
5155 ip
, -((long)del
.br_blockcount
), 0,
5156 XFS_QMOPT_RES_RTBLKS
);
5158 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
5159 (int64_t)del
.br_blockcount
, 0);
5160 (void)xfs_trans_reserve_quota_nblks(NULL
,
5161 ip
, -((long)del
.br_blockcount
), 0,
5162 XFS_QMOPT_RES_REGBLKS
);
5164 ip
->i_delayed_blks
-= del
.br_blockcount
;
5166 cur
->bc_private
.b
.flags
|=
5167 XFS_BTCUR_BPRV_WASDEL
;
5169 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5171 * If it's the case where the directory code is running
5172 * with no block reservation, and the deleted block is in
5173 * the middle of its extent, and the resulting insert
5174 * of an extent would cause transformation to btree format,
5175 * then reject it. The calling code will then swap
5176 * blocks around instead.
5177 * We have to do this now, rather than waiting for the
5178 * conversion to btree format, since the transaction
5181 if (!wasdel
&& xfs_trans_get_block_res(tp
) == 0 &&
5182 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5183 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= ifp
->if_ext_max
&&
5184 del
.br_startoff
> got
.br_startoff
&&
5185 del
.br_startoff
+ del
.br_blockcount
<
5186 got
.br_startoff
+ got
.br_blockcount
) {
5187 error
= XFS_ERROR(ENOSPC
);
5190 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, flist
, cur
, &del
,
5191 &tmp_logflags
, whichfork
);
5192 logflags
|= tmp_logflags
;
5195 bno
= del
.br_startoff
- 1;
5198 * If not done go on to the next (previous) record.
5200 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5202 ep
= xfs_iext_get_ext(ifp
, lastx
);
5203 if (xfs_bmbt_get_startoff(ep
) > bno
) {
5205 ep
= xfs_iext_get_ext(ifp
,
5208 xfs_bmbt_get_all(ep
, &got
);
5213 *done
= bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0;
5214 ASSERT(ifp
->if_ext_max
==
5215 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
5217 * Convert to a btree if necessary.
5219 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5220 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ifp
->if_ext_max
) {
5221 ASSERT(cur
== NULL
);
5222 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
,
5223 &cur
, 0, &tmp_logflags
, whichfork
);
5224 logflags
|= tmp_logflags
;
5229 * transform from btree to extents, give it cur
5231 else if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
5232 XFS_IFORK_NEXTENTS(ip
, whichfork
) <= ifp
->if_ext_max
) {
5233 ASSERT(cur
!= NULL
);
5234 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5236 logflags
|= tmp_logflags
;
5241 * transform from extents to local?
5243 ASSERT(ifp
->if_ext_max
==
5244 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
5248 * Log everything. Do this after conversion, there's no point in
5249 * logging the extent records if we've converted to btree format.
5251 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5252 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5253 logflags
&= ~xfs_ilog_fext(whichfork
);
5254 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5255 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5256 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5258 * Log inode even in the error case, if the transaction
5259 * is dirty we'll need to shut down the filesystem.
5262 xfs_trans_log_inode(tp
, ip
, logflags
);
5265 *firstblock
= cur
->bc_private
.b
.firstblock
;
5266 cur
->bc_private
.b
.allocated
= 0;
5268 xfs_btree_del_cursor(cur
,
5269 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5275 * returns 1 for success, 0 if we failed to map the extent.
5278 xfs_getbmapx_fix_eof_hole(
5279 xfs_inode_t
*ip
, /* xfs incore inode pointer */
5280 struct getbmapx
*out
, /* output structure */
5281 int prealloced
, /* this is a file with
5282 * preallocated data space */
5283 __int64_t end
, /* last block requested */
5284 xfs_fsblock_t startblock
)
5287 xfs_mount_t
*mp
; /* file system mount point */
5288 xfs_ifork_t
*ifp
; /* inode fork pointer */
5289 xfs_extnum_t lastx
; /* last extent pointer */
5290 xfs_fileoff_t fileblock
;
5292 if (startblock
== HOLESTARTBLOCK
) {
5294 out
->bmv_block
= -1;
5295 fixlen
= XFS_FSB_TO_BB(mp
, XFS_B_TO_FSB(mp
, ip
->i_size
));
5296 fixlen
-= out
->bmv_offset
;
5297 if (prealloced
&& out
->bmv_offset
+ out
->bmv_length
== end
) {
5298 /* Came to hole at EOF. Trim it. */
5301 out
->bmv_length
= fixlen
;
5304 if (startblock
== DELAYSTARTBLOCK
)
5305 out
->bmv_block
= -2;
5307 out
->bmv_block
= xfs_fsb_to_db(ip
, startblock
);
5308 fileblock
= XFS_BB_TO_FSB(ip
->i_mount
, out
->bmv_offset
);
5309 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
5310 if (xfs_iext_bno_to_ext(ifp
, fileblock
, &lastx
) &&
5311 (lastx
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))-1))
5312 out
->bmv_oflags
|= BMV_OF_LAST
;
5319 * Get inode's extents as described in bmv, and format for output.
5320 * Calls formatter to fill the user's buffer until all extents
5321 * are mapped, until the passed-in bmv->bmv_count slots have
5322 * been filled, or until the formatter short-circuits the loop,
5323 * if it is tracking filled-in extents on its own.
5325 int /* error code */
5328 struct getbmapx
*bmv
, /* user bmap structure */
5329 xfs_bmap_format_t formatter
, /* format to user */
5330 void *arg
) /* formatter arg */
5332 __int64_t bmvend
; /* last block requested */
5333 int error
= 0; /* return value */
5334 __int64_t fixlen
; /* length for -1 case */
5335 int i
; /* extent number */
5336 int lock
; /* lock state */
5337 xfs_bmbt_irec_t
*map
; /* buffer for user's data */
5338 xfs_mount_t
*mp
; /* file system mount point */
5339 int nex
; /* # of user extents can do */
5340 int nexleft
; /* # of user extents left */
5341 int subnex
; /* # of bmapi's can do */
5342 int nmap
; /* number of map entries */
5343 struct getbmapx
*out
; /* output structure */
5344 int whichfork
; /* data or attr fork */
5345 int prealloced
; /* this is a file with
5346 * preallocated data space */
5347 int iflags
; /* interface flags */
5348 int bmapi_flags
; /* flags for xfs_bmapi */
5352 iflags
= bmv
->bmv_iflags
;
5353 whichfork
= iflags
& BMV_IF_ATTRFORK
? XFS_ATTR_FORK
: XFS_DATA_FORK
;
5355 if (whichfork
== XFS_ATTR_FORK
) {
5356 if (XFS_IFORK_Q(ip
)) {
5357 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
&&
5358 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_BTREE
&&
5359 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_LOCAL
)
5360 return XFS_ERROR(EINVAL
);
5361 } else if (unlikely(
5362 ip
->i_d
.di_aformat
!= 0 &&
5363 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
)) {
5364 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW
,
5366 return XFS_ERROR(EFSCORRUPTED
);
5372 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
&&
5373 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
&&
5374 ip
->i_d
.di_format
!= XFS_DINODE_FMT_LOCAL
)
5375 return XFS_ERROR(EINVAL
);
5377 if (xfs_get_extsz_hint(ip
) ||
5378 ip
->i_d
.di_flags
& (XFS_DIFLAG_PREALLOC
|XFS_DIFLAG_APPEND
)){
5380 fixlen
= XFS_MAXIOFFSET(mp
);
5383 fixlen
= ip
->i_size
;
5387 if (bmv
->bmv_length
== -1) {
5388 fixlen
= XFS_FSB_TO_BB(mp
, XFS_B_TO_FSB(mp
, fixlen
));
5390 max_t(__int64_t
, fixlen
- bmv
->bmv_offset
, 0);
5391 } else if (bmv
->bmv_length
== 0) {
5392 bmv
->bmv_entries
= 0;
5394 } else if (bmv
->bmv_length
< 0) {
5395 return XFS_ERROR(EINVAL
);
5398 nex
= bmv
->bmv_count
- 1;
5400 return XFS_ERROR(EINVAL
);
5401 bmvend
= bmv
->bmv_offset
+ bmv
->bmv_length
;
5404 if (bmv
->bmv_count
> ULONG_MAX
/ sizeof(struct getbmapx
))
5405 return XFS_ERROR(ENOMEM
);
5406 out
= kmem_zalloc(bmv
->bmv_count
* sizeof(struct getbmapx
), KM_MAYFAIL
);
5408 return XFS_ERROR(ENOMEM
);
5410 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
5411 if (whichfork
== XFS_DATA_FORK
&& !(iflags
& BMV_IF_DELALLOC
)) {
5412 if (ip
->i_delayed_blks
|| ip
->i_size
> ip
->i_d
.di_size
) {
5413 error
= xfs_flush_pages(ip
, 0, -1, 0, FI_REMAPF
);
5415 goto out_unlock_iolock
;
5418 * even after flushing the inode, there can still be delalloc
5419 * blocks on the inode beyond EOF due to speculative
5420 * preallocation. These are not removed until the release
5421 * function is called or the inode is inactivated. Hence we
5422 * cannot assert here that ip->i_delayed_blks == 0.
5426 lock
= xfs_ilock_map_shared(ip
);
5429 * Don't let nex be bigger than the number of extents
5430 * we can have assuming alternating holes and real extents.
5432 if (nex
> XFS_IFORK_NEXTENTS(ip
, whichfork
) * 2 + 1)
5433 nex
= XFS_IFORK_NEXTENTS(ip
, whichfork
) * 2 + 1;
5435 bmapi_flags
= xfs_bmapi_aflag(whichfork
);
5436 if (!(iflags
& BMV_IF_PREALLOC
))
5437 bmapi_flags
|= XFS_BMAPI_IGSTATE
;
5440 * Allocate enough space to handle "subnex" maps at a time.
5444 map
= kmem_alloc(subnex
* sizeof(*map
), KM_MAYFAIL
| KM_NOFS
);
5446 goto out_unlock_ilock
;
5448 bmv
->bmv_entries
= 0;
5450 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0 &&
5451 (whichfork
== XFS_ATTR_FORK
|| !(iflags
& BMV_IF_DELALLOC
))) {
5459 nmap
= (nexleft
> subnex
) ? subnex
: nexleft
;
5460 error
= xfs_bmapi(NULL
, ip
, XFS_BB_TO_FSBT(mp
, bmv
->bmv_offset
),
5461 XFS_BB_TO_FSB(mp
, bmv
->bmv_length
),
5462 bmapi_flags
, NULL
, 0, map
, &nmap
,
5466 ASSERT(nmap
<= subnex
);
5468 for (i
= 0; i
< nmap
&& nexleft
&& bmv
->bmv_length
; i
++) {
5469 out
[cur_ext
].bmv_oflags
= 0;
5470 if (map
[i
].br_state
== XFS_EXT_UNWRITTEN
)
5471 out
[cur_ext
].bmv_oflags
|= BMV_OF_PREALLOC
;
5472 else if (map
[i
].br_startblock
== DELAYSTARTBLOCK
)
5473 out
[cur_ext
].bmv_oflags
|= BMV_OF_DELALLOC
;
5474 out
[cur_ext
].bmv_offset
=
5475 XFS_FSB_TO_BB(mp
, map
[i
].br_startoff
);
5476 out
[cur_ext
].bmv_length
=
5477 XFS_FSB_TO_BB(mp
, map
[i
].br_blockcount
);
5478 out
[cur_ext
].bmv_unused1
= 0;
5479 out
[cur_ext
].bmv_unused2
= 0;
5480 ASSERT(((iflags
& BMV_IF_DELALLOC
) != 0) ||
5481 (map
[i
].br_startblock
!= DELAYSTARTBLOCK
));
5482 if (map
[i
].br_startblock
== HOLESTARTBLOCK
&&
5483 whichfork
== XFS_ATTR_FORK
) {
5484 /* came to the end of attribute fork */
5485 out
[cur_ext
].bmv_oflags
|= BMV_OF_LAST
;
5489 if (!xfs_getbmapx_fix_eof_hole(ip
, &out
[cur_ext
],
5491 map
[i
].br_startblock
))
5495 out
[cur_ext
].bmv_offset
+
5496 out
[cur_ext
].bmv_length
;
5498 max_t(__int64_t
, 0, bmvend
- bmv
->bmv_offset
);
5501 * In case we don't want to return the hole,
5502 * don't increase cur_ext so that we can reuse
5503 * it in the next loop.
5505 if ((iflags
& BMV_IF_NO_HOLES
) &&
5506 map
[i
].br_startblock
== HOLESTARTBLOCK
) {
5507 memset(&out
[cur_ext
], 0, sizeof(out
[cur_ext
]));
5515 } while (nmap
&& nexleft
&& bmv
->bmv_length
);
5520 xfs_iunlock_map_shared(ip
, lock
);
5522 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
5524 for (i
= 0; i
< cur_ext
; i
++) {
5525 int full
= 0; /* user array is full */
5527 /* format results & advance arg */
5528 error
= formatter(&arg
, &out
[i
], &full
);
5538 * Check the last inode extent to determine whether this allocation will result
5539 * in blocks being allocated at the end of the file. When we allocate new data
5540 * blocks at the end of the file which do not start at the previous data block,
5541 * we will try to align the new blocks at stripe unit boundaries.
5543 STATIC
int /* error */
5545 xfs_inode_t
*ip
, /* incore inode pointer */
5546 xfs_fileoff_t off
, /* file offset in fsblocks */
5547 int whichfork
, /* data or attribute fork */
5548 char *aeof
) /* return value */
5550 int error
; /* error return value */
5551 xfs_ifork_t
*ifp
; /* inode fork pointer */
5552 xfs_bmbt_rec_host_t
*lastrec
; /* extent record pointer */
5553 xfs_extnum_t nextents
; /* number of file extents */
5554 xfs_bmbt_irec_t s
; /* expanded extent record */
5556 ASSERT(whichfork
== XFS_DATA_FORK
);
5557 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5558 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5559 (error
= xfs_iread_extents(NULL
, ip
, whichfork
)))
5561 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5562 if (nextents
== 0) {
5567 * Go to the last extent
5569 lastrec
= xfs_iext_get_ext(ifp
, nextents
- 1);
5570 xfs_bmbt_get_all(lastrec
, &s
);
5572 * Check we are allocating in the last extent (for delayed allocations)
5573 * or past the last extent for non-delayed allocations.
5575 *aeof
= (off
>= s
.br_startoff
&&
5576 off
< s
.br_startoff
+ s
.br_blockcount
&&
5577 isnullstartblock(s
.br_startblock
)) ||
5578 off
>= s
.br_startoff
+ s
.br_blockcount
;
5583 * Check if the endoff is outside the last extent. If so the caller will grow
5584 * the allocation to a stripe unit boundary.
5588 xfs_inode_t
*ip
, /* incore inode pointer */
5589 xfs_fileoff_t endoff
, /* file offset in fsblocks */
5590 int whichfork
, /* data or attribute fork */
5591 int *eof
) /* result value */
5593 xfs_fsblock_t blockcount
; /* extent block count */
5594 int error
; /* error return value */
5595 xfs_ifork_t
*ifp
; /* inode fork pointer */
5596 xfs_bmbt_rec_host_t
*lastrec
; /* extent record pointer */
5597 xfs_extnum_t nextents
; /* number of file extents */
5598 xfs_fileoff_t startoff
; /* extent starting file offset */
5600 ASSERT(whichfork
== XFS_DATA_FORK
);
5601 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5602 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5603 (error
= xfs_iread_extents(NULL
, ip
, whichfork
)))
5605 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5606 if (nextents
== 0) {
5611 * Go to the last extent
5613 lastrec
= xfs_iext_get_ext(ifp
, nextents
- 1);
5614 startoff
= xfs_bmbt_get_startoff(lastrec
);
5615 blockcount
= xfs_bmbt_get_blockcount(lastrec
);
5616 *eof
= endoff
>= startoff
+ blockcount
;
5621 STATIC
struct xfs_buf
*
5623 struct xfs_btree_cur
*cur
,
5626 struct xfs_log_item_desc
*lidp
;
5632 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
5633 if (!cur
->bc_bufs
[i
])
5635 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
5636 return cur
->bc_bufs
[i
];
5639 /* Chase down all the log items to see if the bp is there */
5640 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
5641 struct xfs_buf_log_item
*bip
;
5642 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
5643 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
5644 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
5645 return bip
->bli_buf
;
5653 struct xfs_btree_block
*block
,
5659 __be64
*pp
, *thispa
; /* pointer to block address */
5660 xfs_bmbt_key_t
*prevp
, *keyp
;
5662 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
5665 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
5666 dmxr
= mp
->m_bmap_dmxr
[0];
5667 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
5670 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
5671 be64_to_cpu(keyp
->br_startoff
));
5676 * Compare the block numbers to see if there are dups.
5679 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
5681 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
5683 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
5685 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
5687 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
5688 if (*thispa
== *pp
) {
5689 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
5691 (unsigned long long)be64_to_cpu(*thispa
));
5692 panic("%s: ptrs are equal in node\n",
5700 * Check that the extents for the inode ip are in the right order in all
5705 xfs_bmap_check_leaf_extents(
5706 xfs_btree_cur_t
*cur
, /* btree cursor or null */
5707 xfs_inode_t
*ip
, /* incore inode pointer */
5708 int whichfork
) /* data or attr fork */
5710 struct xfs_btree_block
*block
; /* current btree block */
5711 xfs_fsblock_t bno
; /* block # of "block" */
5712 xfs_buf_t
*bp
; /* buffer for "block" */
5713 int error
; /* error return value */
5714 xfs_extnum_t i
=0, j
; /* index into the extents list */
5715 xfs_ifork_t
*ifp
; /* fork structure */
5716 int level
; /* btree level, for checking */
5717 xfs_mount_t
*mp
; /* file system mount structure */
5718 __be64
*pp
; /* pointer to block address */
5719 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
5720 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
5721 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
5724 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
5730 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5731 block
= ifp
->if_broot
;
5733 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5735 level
= be16_to_cpu(block
->bb_level
);
5737 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
5738 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
5739 bno
= be64_to_cpu(*pp
);
5741 ASSERT(bno
!= NULLDFSBNO
);
5742 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
5743 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
5746 * Go down the tree until leaf level is reached, following the first
5747 * pointer (leftmost) at each level.
5749 while (level
-- > 0) {
5750 /* See if buf is in cur first */
5751 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
5757 if (!bp
&& (error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
5758 XFS_BMAP_BTREE_REF
)))
5760 block
= XFS_BUF_TO_BLOCK(bp
);
5761 XFS_WANT_CORRUPTED_GOTO(
5762 xfs_bmap_sanity_check(mp
, bp
, level
),
5768 * Check this block for basic sanity (increasing keys and
5769 * no duplicate blocks).
5772 xfs_check_block(block
, mp
, 0, 0);
5773 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
5774 bno
= be64_to_cpu(*pp
);
5775 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
5778 xfs_trans_brelse(NULL
, bp
);
5783 * Here with bp and block set to the leftmost leaf node in the tree.
5788 * Loop over all leaf nodes checking that all extents are in the right order.
5791 xfs_fsblock_t nextbno
;
5792 xfs_extnum_t num_recs
;
5795 num_recs
= xfs_btree_get_numrecs(block
);
5798 * Read-ahead the next leaf block, if any.
5801 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
5804 * Check all the extents to make sure they are OK.
5805 * If we had a previous block, the last entry should
5806 * conform with the first entry in this one.
5809 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
5811 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
5812 xfs_bmbt_disk_get_blockcount(&last
) <=
5813 xfs_bmbt_disk_get_startoff(ep
));
5815 for (j
= 1; j
< num_recs
; j
++) {
5816 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
5817 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
5818 xfs_bmbt_disk_get_blockcount(ep
) <=
5819 xfs_bmbt_disk_get_startoff(nextp
));
5827 xfs_trans_brelse(NULL
, bp
);
5831 * If we've reached the end, stop.
5833 if (bno
== NULLFSBLOCK
)
5836 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
5842 if (!bp
&& (error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
5843 XFS_BMAP_BTREE_REF
)))
5845 block
= XFS_BUF_TO_BLOCK(bp
);
5849 xfs_trans_brelse(NULL
, bp
);
5854 xfs_warn(mp
, "%s: at error0", __func__
);
5856 xfs_trans_brelse(NULL
, bp
);
5858 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
5860 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
5866 * Count fsblocks of the given fork.
5869 xfs_bmap_count_blocks(
5870 xfs_trans_t
*tp
, /* transaction pointer */
5871 xfs_inode_t
*ip
, /* incore inode */
5872 int whichfork
, /* data or attr fork */
5873 int *count
) /* out: count of blocks */
5875 struct xfs_btree_block
*block
; /* current btree block */
5876 xfs_fsblock_t bno
; /* block # of "block" */
5877 xfs_ifork_t
*ifp
; /* fork structure */
5878 int level
; /* btree level, for checking */
5879 xfs_mount_t
*mp
; /* file system mount structure */
5880 __be64
*pp
; /* pointer to block address */
5884 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5885 if ( XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
) {
5886 xfs_bmap_count_leaves(ifp
, 0,
5887 ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
),
5893 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5895 block
= ifp
->if_broot
;
5896 level
= be16_to_cpu(block
->bb_level
);
5898 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
5899 bno
= be64_to_cpu(*pp
);
5900 ASSERT(bno
!= NULLDFSBNO
);
5901 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
5902 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
5904 if (unlikely(xfs_bmap_count_tree(mp
, tp
, ifp
, bno
, level
, count
) < 0)) {
5905 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW
,
5907 return XFS_ERROR(EFSCORRUPTED
);
5914 * Recursively walks each level of a btree
5915 * to count total fsblocks is use.
5917 STATIC
int /* error */
5918 xfs_bmap_count_tree(
5919 xfs_mount_t
*mp
, /* file system mount point */
5920 xfs_trans_t
*tp
, /* transaction pointer */
5921 xfs_ifork_t
*ifp
, /* inode fork pointer */
5922 xfs_fsblock_t blockno
, /* file system block number */
5923 int levelin
, /* level in btree */
5924 int *count
) /* Count of blocks */
5927 xfs_buf_t
*bp
, *nbp
;
5928 int level
= levelin
;
5930 xfs_fsblock_t bno
= blockno
;
5931 xfs_fsblock_t nextbno
;
5932 struct xfs_btree_block
*block
, *nextblock
;
5935 if ((error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
, XFS_BMAP_BTREE_REF
)))
5938 block
= XFS_BUF_TO_BLOCK(bp
);
5941 /* Not at node above leaves, count this level of nodes */
5942 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
5943 while (nextbno
!= NULLFSBLOCK
) {
5944 if ((error
= xfs_btree_read_bufl(mp
, tp
, nextbno
,
5945 0, &nbp
, XFS_BMAP_BTREE_REF
)))
5948 nextblock
= XFS_BUF_TO_BLOCK(nbp
);
5949 nextbno
= be64_to_cpu(nextblock
->bb_u
.l
.bb_rightsib
);
5950 xfs_trans_brelse(tp
, nbp
);
5953 /* Dive to the next level */
5954 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
5955 bno
= be64_to_cpu(*pp
);
5956 if (unlikely((error
=
5957 xfs_bmap_count_tree(mp
, tp
, ifp
, bno
, level
, count
)) < 0)) {
5958 xfs_trans_brelse(tp
, bp
);
5959 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
5960 XFS_ERRLEVEL_LOW
, mp
);
5961 return XFS_ERROR(EFSCORRUPTED
);
5963 xfs_trans_brelse(tp
, bp
);
5965 /* count all level 1 nodes and their leaves */
5967 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
5968 numrecs
= be16_to_cpu(block
->bb_numrecs
);
5969 xfs_bmap_disk_count_leaves(mp
, block
, numrecs
, count
);
5970 xfs_trans_brelse(tp
, bp
);
5971 if (nextbno
== NULLFSBLOCK
)
5974 if ((error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
5975 XFS_BMAP_BTREE_REF
)))
5978 block
= XFS_BUF_TO_BLOCK(bp
);
5985 * Count leaf blocks given a range of extent records.
5988 xfs_bmap_count_leaves(
5996 for (b
= 0; b
< numrecs
; b
++) {
5997 xfs_bmbt_rec_host_t
*frp
= xfs_iext_get_ext(ifp
, idx
+ b
);
5998 *count
+= xfs_bmbt_get_blockcount(frp
);
6003 * Count leaf blocks given a range of extent records originally
6007 xfs_bmap_disk_count_leaves(
6008 struct xfs_mount
*mp
,
6009 struct xfs_btree_block
*block
,
6014 xfs_bmbt_rec_t
*frp
;
6016 for (b
= 1; b
<= numrecs
; b
++) {
6017 frp
= XFS_BMBT_REC_ADDR(mp
, block
, b
);
6018 *count
+= xfs_bmbt_disk_get_blockcount(frp
);
6023 * dead simple method of punching delalyed allocation blocks from a range in
6024 * the inode. Walks a block at a time so will be slow, but is only executed in
6025 * rare error cases so the overhead is not critical. This will alays punch out
6026 * both the start and end blocks, even if the ranges only partially overlap
6027 * them, so it is up to the caller to ensure that partial blocks are not
6031 xfs_bmap_punch_delalloc_range(
6032 struct xfs_inode
*ip
,
6033 xfs_fileoff_t start_fsb
,
6034 xfs_fileoff_t length
)
6036 xfs_fileoff_t remaining
= length
;
6039 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
6043 xfs_bmbt_irec_t imap
;
6045 xfs_fsblock_t firstblock
;
6046 xfs_bmap_free_t flist
;
6049 * Map the range first and check that it is a delalloc extent
6050 * before trying to unmap the range. Otherwise we will be
6051 * trying to remove a real extent (which requires a
6052 * transaction) or a hole, which is probably a bad idea...
6054 error
= xfs_bmapi(NULL
, ip
, start_fsb
, 1,
6055 XFS_BMAPI_ENTIRE
, NULL
, 0, &imap
,
6059 /* something screwed, just bail */
6060 if (!XFS_FORCED_SHUTDOWN(ip
->i_mount
)) {
6061 xfs_alert(ip
->i_mount
,
6062 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6063 ip
->i_ino
, start_fsb
);
6071 if (imap
.br_startblock
!= DELAYSTARTBLOCK
) {
6072 /* been converted, ignore */
6075 WARN_ON(imap
.br_blockcount
== 0);
6078 * Note: while we initialise the firstblock/flist pair, they
6079 * should never be used because blocks should never be
6080 * allocated or freed for a delalloc extent and hence we need
6081 * don't cancel or finish them after the xfs_bunmapi() call.
6083 xfs_bmap_init(&flist
, &firstblock
);
6084 error
= xfs_bunmapi(NULL
, ip
, start_fsb
, 1, 0, 1, &firstblock
,
6089 ASSERT(!flist
.xbf_count
&& !flist
.xbf_first
);
6093 } while(remaining
> 0);