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"
53 kmem_zone_t
*xfs_bmap_free_item_zone
;
56 * Prototypes for internal bmap routines.
61 xfs_bmap_check_leaf_extents(
62 struct xfs_btree_cur
*cur
,
66 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
71 * Called from xfs_bmap_add_attrfork to handle extents format files.
73 STATIC
int /* error */
74 xfs_bmap_add_attrfork_extents(
75 xfs_trans_t
*tp
, /* transaction pointer */
76 xfs_inode_t
*ip
, /* incore inode pointer */
77 xfs_fsblock_t
*firstblock
, /* first block allocated */
78 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
79 int *flags
); /* inode logging flags */
82 * Called from xfs_bmap_add_attrfork to handle local format files.
84 STATIC
int /* error */
85 xfs_bmap_add_attrfork_local(
86 xfs_trans_t
*tp
, /* transaction pointer */
87 xfs_inode_t
*ip
, /* incore inode pointer */
88 xfs_fsblock_t
*firstblock
, /* first block allocated */
89 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
90 int *flags
); /* inode logging flags */
93 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
94 * It figures out where to ask the underlying allocator to put the new extent.
96 STATIC
int /* error */
98 xfs_bmalloca_t
*ap
); /* bmap alloc argument struct */
101 * Transform a btree format file with only one leaf node, where the
102 * extents list will fit in the inode, into an extents format file.
103 * Since the file extents are already in-core, all we have to do is
104 * give up the space for the btree root and pitch the leaf block.
106 STATIC
int /* error */
107 xfs_bmap_btree_to_extents(
108 xfs_trans_t
*tp
, /* transaction pointer */
109 xfs_inode_t
*ip
, /* incore inode pointer */
110 xfs_btree_cur_t
*cur
, /* btree cursor */
111 int *logflagsp
, /* inode logging flags */
112 int whichfork
); /* data or attr fork */
115 * Remove the entry "free" from the free item list. Prev points to the
116 * previous entry, unless "free" is the head of the list.
120 xfs_bmap_free_t
*flist
, /* free item list header */
121 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
122 xfs_bmap_free_item_t
*free
); /* list item to be freed */
125 * Convert an extents-format file into a btree-format file.
126 * The new file will have a root block (in the inode) and a single child block.
128 STATIC
int /* error */
129 xfs_bmap_extents_to_btree(
130 xfs_trans_t
*tp
, /* transaction pointer */
131 xfs_inode_t
*ip
, /* incore inode pointer */
132 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
133 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
134 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
135 int wasdel
, /* converting a delayed alloc */
136 int *logflagsp
, /* inode logging flags */
137 int whichfork
); /* data or attr fork */
140 * Convert a local file to an extents file.
141 * This code is sort of bogus, since the file data needs to get
142 * logged so it won't be lost. The bmap-level manipulations are ok, though.
144 STATIC
int /* error */
145 xfs_bmap_local_to_extents(
146 xfs_trans_t
*tp
, /* transaction pointer */
147 xfs_inode_t
*ip
, /* incore inode pointer */
148 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
149 xfs_extlen_t total
, /* total blocks needed by transaction */
150 int *logflagsp
, /* inode logging flags */
151 int whichfork
); /* data or attr fork */
154 * Search the extents list for the inode, for the extent containing bno.
155 * If bno lies in a hole, point to the next entry. If bno lies past eof,
156 * *eofp will be set, and *prevp will contain the last entry (null if none).
157 * Else, *lastxp will be set to the index of the found
158 * entry; *gotp will contain the entry.
160 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
161 xfs_bmap_search_extents(
162 xfs_inode_t
*ip
, /* incore inode pointer */
163 xfs_fileoff_t bno
, /* block number searched for */
164 int whichfork
, /* data or attr fork */
165 int *eofp
, /* out: end of file found */
166 xfs_extnum_t
*lastxp
, /* out: last extent index */
167 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
168 xfs_bmbt_irec_t
*prevp
); /* out: previous extent entry found */
171 * Compute the worst-case number of indirect blocks that will be used
172 * for ip's delayed extent of length "len".
175 xfs_bmap_worst_indlen(
176 xfs_inode_t
*ip
, /* incore inode pointer */
177 xfs_filblks_t len
); /* delayed extent length */
181 * Perform various validation checks on the values being returned
185 xfs_bmap_validate_ret(
189 xfs_bmbt_irec_t
*mval
,
193 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
201 xfs_fsblock_t blockno
,
206 xfs_bmap_count_leaves(
213 xfs_bmap_disk_count_leaves(
214 struct xfs_mount
*mp
,
215 struct xfs_btree_block
*block
,
220 * Bmap internal routines.
223 STATIC
int /* error */
225 struct xfs_btree_cur
*cur
,
229 int *stat
) /* success/failure */
231 cur
->bc_rec
.b
.br_startoff
= off
;
232 cur
->bc_rec
.b
.br_startblock
= bno
;
233 cur
->bc_rec
.b
.br_blockcount
= len
;
234 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
237 STATIC
int /* error */
239 struct xfs_btree_cur
*cur
,
243 int *stat
) /* success/failure */
245 cur
->bc_rec
.b
.br_startoff
= off
;
246 cur
->bc_rec
.b
.br_startblock
= bno
;
247 cur
->bc_rec
.b
.br_blockcount
= len
;
248 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
252 * Update the record referred to by cur to the value given
253 * by [off, bno, len, state].
254 * This either works (return 0) or gets an EFSCORRUPTED error.
258 struct xfs_btree_cur
*cur
,
264 union xfs_btree_rec rec
;
266 xfs_bmbt_disk_set_allf(&rec
.bmbt
, off
, bno
, len
, state
);
267 return xfs_btree_update(cur
, &rec
);
271 * Called from xfs_bmap_add_attrfork to handle btree format files.
273 STATIC
int /* error */
274 xfs_bmap_add_attrfork_btree(
275 xfs_trans_t
*tp
, /* transaction pointer */
276 xfs_inode_t
*ip
, /* incore inode pointer */
277 xfs_fsblock_t
*firstblock
, /* first block allocated */
278 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
279 int *flags
) /* inode logging flags */
281 xfs_btree_cur_t
*cur
; /* btree cursor */
282 int error
; /* error return value */
283 xfs_mount_t
*mp
; /* file system mount struct */
284 int stat
; /* newroot status */
287 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
288 *flags
|= XFS_ILOG_DBROOT
;
290 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, XFS_DATA_FORK
);
291 cur
->bc_private
.b
.flist
= flist
;
292 cur
->bc_private
.b
.firstblock
= *firstblock
;
293 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
295 /* must be at least one entry */
296 XFS_WANT_CORRUPTED_GOTO(stat
== 1, error0
);
297 if ((error
= xfs_btree_new_iroot(cur
, flags
, &stat
)))
300 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
301 return XFS_ERROR(ENOSPC
);
303 *firstblock
= cur
->bc_private
.b
.firstblock
;
304 cur
->bc_private
.b
.allocated
= 0;
305 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
309 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
314 * Called from xfs_bmap_add_attrfork to handle extents format files.
316 STATIC
int /* error */
317 xfs_bmap_add_attrfork_extents(
318 xfs_trans_t
*tp
, /* transaction pointer */
319 xfs_inode_t
*ip
, /* incore inode pointer */
320 xfs_fsblock_t
*firstblock
, /* first block allocated */
321 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
322 int *flags
) /* inode logging flags */
324 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
325 int error
; /* error return value */
327 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
330 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
, &cur
, 0,
331 flags
, XFS_DATA_FORK
);
333 cur
->bc_private
.b
.allocated
= 0;
334 xfs_btree_del_cursor(cur
,
335 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
341 * Called from xfs_bmap_add_attrfork to handle local format files.
343 STATIC
int /* error */
344 xfs_bmap_add_attrfork_local(
345 xfs_trans_t
*tp
, /* transaction pointer */
346 xfs_inode_t
*ip
, /* incore inode pointer */
347 xfs_fsblock_t
*firstblock
, /* first block allocated */
348 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
349 int *flags
) /* inode logging flags */
351 xfs_da_args_t dargs
; /* args for dir/attr code */
352 int error
; /* error return value */
353 xfs_mount_t
*mp
; /* mount structure pointer */
355 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
357 if (S_ISDIR(ip
->i_d
.di_mode
)) {
359 memset(&dargs
, 0, sizeof(dargs
));
361 dargs
.firstblock
= firstblock
;
363 dargs
.total
= mp
->m_dirblkfsbs
;
364 dargs
.whichfork
= XFS_DATA_FORK
;
366 error
= xfs_dir2_sf_to_block(&dargs
);
368 error
= xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1, flags
,
374 * Convert a delayed allocation to a real allocation.
376 STATIC
int /* error */
377 xfs_bmap_add_extent_delay_real(
378 struct xfs_trans
*tp
, /* transaction pointer */
379 xfs_inode_t
*ip
, /* incore inode pointer */
380 xfs_extnum_t
*idx
, /* extent number to update/insert */
381 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
382 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
383 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
384 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
385 int *logflagsp
) /* inode logging flags */
387 xfs_btree_cur_t
*cur
; /* btree cursor */
388 int diff
; /* temp value */
389 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
390 int error
; /* error return value */
391 int i
; /* temp state */
392 xfs_ifork_t
*ifp
; /* inode fork pointer */
393 xfs_fileoff_t new_endoff
; /* end offset of new entry */
394 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
395 /* left is 0, right is 1, prev is 2 */
396 int rval
=0; /* return value (logging flags) */
397 int state
= 0;/* state bits, accessed thru macros */
398 xfs_filblks_t da_new
; /* new count del alloc blocks used */
399 xfs_filblks_t da_old
; /* old count del alloc blocks used */
400 xfs_filblks_t temp
=0; /* value for da_new calculations */
401 xfs_filblks_t temp2
=0;/* value for da_new calculations */
402 int tmp_rval
; /* partial logging flags */
404 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
408 ASSERT(*idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
409 ASSERT(!isnullstartblock(new->br_startblock
));
410 ASSERT(!cur
|| (cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
412 XFS_STATS_INC(xs_add_exlist
);
421 * Set up a bunch of variables to make the tests simpler.
423 ep
= xfs_iext_get_ext(ifp
, *idx
);
424 xfs_bmbt_get_all(ep
, &PREV
);
425 new_endoff
= new->br_startoff
+ new->br_blockcount
;
426 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
427 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
429 da_old
= startblockval(PREV
.br_startblock
);
433 * Set flags determining what part of the previous delayed allocation
434 * extent is being replaced by a real allocation.
436 if (PREV
.br_startoff
== new->br_startoff
)
437 state
|= BMAP_LEFT_FILLING
;
438 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
439 state
|= BMAP_RIGHT_FILLING
;
442 * Check and set flags if this segment has a left neighbor.
443 * Don't set contiguous if the combined extent would be too large.
446 state
|= BMAP_LEFT_VALID
;
447 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
449 if (isnullstartblock(LEFT
.br_startblock
))
450 state
|= BMAP_LEFT_DELAY
;
453 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
454 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
455 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
456 LEFT
.br_state
== new->br_state
&&
457 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
458 state
|= BMAP_LEFT_CONTIG
;
461 * Check and set flags if this segment has a right neighbor.
462 * Don't set contiguous if the combined extent would be too large.
463 * Also check for all-three-contiguous being too large.
465 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
466 state
|= BMAP_RIGHT_VALID
;
467 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
469 if (isnullstartblock(RIGHT
.br_startblock
))
470 state
|= BMAP_RIGHT_DELAY
;
473 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
474 new_endoff
== RIGHT
.br_startoff
&&
475 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
476 new->br_state
== RIGHT
.br_state
&&
477 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
478 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
479 BMAP_RIGHT_FILLING
)) !=
480 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
481 BMAP_RIGHT_FILLING
) ||
482 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
484 state
|= BMAP_RIGHT_CONTIG
;
488 * Switch out based on the FILLING and CONTIG state bits.
490 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
491 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
492 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
493 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
495 * Filling in all of a previously delayed allocation extent.
496 * The left and right neighbors are both contiguous with new.
499 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
500 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
501 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
502 RIGHT
.br_blockcount
);
503 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
505 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
506 ip
->i_d
.di_nextents
--;
508 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
510 rval
= XFS_ILOG_CORE
;
511 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
513 RIGHT
.br_blockcount
, &i
)))
515 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
516 if ((error
= xfs_btree_delete(cur
, &i
)))
518 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
519 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
521 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
522 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
526 RIGHT
.br_blockcount
, LEFT
.br_state
)))
531 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
533 * Filling in all of a previously delayed allocation extent.
534 * The left neighbor is contiguous, the right is not.
538 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
539 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
540 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
541 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
543 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
545 rval
= XFS_ILOG_DEXT
;
548 if ((error
= xfs_bmbt_lookup_eq(cur
, LEFT
.br_startoff
,
549 LEFT
.br_startblock
, LEFT
.br_blockcount
,
552 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
553 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
556 PREV
.br_blockcount
, LEFT
.br_state
)))
561 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
563 * Filling in all of a previously delayed allocation extent.
564 * The right neighbor is contiguous, the left is not.
566 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
567 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
568 xfs_bmbt_set_blockcount(ep
,
569 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
570 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
572 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
574 rval
= XFS_ILOG_DEXT
;
577 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
579 RIGHT
.br_blockcount
, &i
)))
581 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
582 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
585 RIGHT
.br_blockcount
, PREV
.br_state
)))
590 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
592 * Filling in all of a previously delayed allocation extent.
593 * Neither the left nor right neighbors are contiguous with
596 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
597 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
598 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
600 ip
->i_d
.di_nextents
++;
602 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
604 rval
= XFS_ILOG_CORE
;
605 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
606 new->br_startblock
, new->br_blockcount
,
609 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
610 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
611 if ((error
= xfs_btree_insert(cur
, &i
)))
613 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
617 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
619 * Filling in the first part of a previous delayed allocation.
620 * The left neighbor is contiguous.
622 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
623 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
624 LEFT
.br_blockcount
+ new->br_blockcount
);
625 xfs_bmbt_set_startoff(ep
,
626 PREV
.br_startoff
+ new->br_blockcount
);
627 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
629 temp
= PREV
.br_blockcount
- new->br_blockcount
;
630 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
631 xfs_bmbt_set_blockcount(ep
, temp
);
633 rval
= XFS_ILOG_DEXT
;
636 if ((error
= xfs_bmbt_lookup_eq(cur
, LEFT
.br_startoff
,
637 LEFT
.br_startblock
, LEFT
.br_blockcount
,
640 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
641 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
648 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
649 startblockval(PREV
.br_startblock
));
650 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
651 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
656 case BMAP_LEFT_FILLING
:
658 * Filling in the first part of a previous delayed allocation.
659 * The left neighbor is not contiguous.
661 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
662 xfs_bmbt_set_startoff(ep
, new_endoff
);
663 temp
= PREV
.br_blockcount
- new->br_blockcount
;
664 xfs_bmbt_set_blockcount(ep
, temp
);
665 xfs_iext_insert(ip
, *idx
, 1, new, state
);
666 ip
->i_d
.di_nextents
++;
668 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
670 rval
= XFS_ILOG_CORE
;
671 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
672 new->br_startblock
, new->br_blockcount
,
675 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
676 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
677 if ((error
= xfs_btree_insert(cur
, &i
)))
679 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
681 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
682 ip
->i_d
.di_nextents
> ip
->i_df
.if_ext_max
) {
683 error
= xfs_bmap_extents_to_btree(tp
, ip
,
684 first
, flist
, &cur
, 1, &tmp_rval
,
690 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
691 startblockval(PREV
.br_startblock
) -
692 (cur
? cur
->bc_private
.b
.allocated
: 0));
693 ep
= xfs_iext_get_ext(ifp
, *idx
+ 1);
694 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
695 trace_xfs_bmap_post_update(ip
, *idx
+ 1, state
, _THIS_IP_
);
698 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
700 * Filling in the last part of a previous delayed allocation.
701 * The right neighbor is contiguous with the new allocation.
703 temp
= PREV
.br_blockcount
- new->br_blockcount
;
704 trace_xfs_bmap_pre_update(ip
, *idx
+ 1, state
, _THIS_IP_
);
705 xfs_bmbt_set_blockcount(ep
, temp
);
706 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
+ 1),
707 new->br_startoff
, new->br_startblock
,
708 new->br_blockcount
+ RIGHT
.br_blockcount
,
710 trace_xfs_bmap_post_update(ip
, *idx
+ 1, state
, _THIS_IP_
);
712 rval
= XFS_ILOG_DEXT
;
715 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
717 RIGHT
.br_blockcount
, &i
)))
719 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
720 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
728 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
729 startblockval(PREV
.br_startblock
));
730 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
731 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
732 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
737 case BMAP_RIGHT_FILLING
:
739 * Filling in the last part of a previous delayed allocation.
740 * The right neighbor is not contiguous.
742 temp
= PREV
.br_blockcount
- new->br_blockcount
;
743 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
744 xfs_bmbt_set_blockcount(ep
, temp
);
745 xfs_iext_insert(ip
, *idx
+ 1, 1, new, state
);
746 ip
->i_d
.di_nextents
++;
748 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
750 rval
= XFS_ILOG_CORE
;
751 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
752 new->br_startblock
, new->br_blockcount
,
755 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
756 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
757 if ((error
= xfs_btree_insert(cur
, &i
)))
759 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
761 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
762 ip
->i_d
.di_nextents
> ip
->i_df
.if_ext_max
) {
763 error
= xfs_bmap_extents_to_btree(tp
, ip
,
764 first
, flist
, &cur
, 1, &tmp_rval
,
770 da_new
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
771 startblockval(PREV
.br_startblock
) -
772 (cur
? cur
->bc_private
.b
.allocated
: 0));
773 ep
= xfs_iext_get_ext(ifp
, *idx
);
774 xfs_bmbt_set_startblock(ep
, nullstartblock(da_new
));
775 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
782 * Filling in the middle part of a previous delayed allocation.
783 * Contiguity is impossible here.
784 * This case is avoided almost all the time.
786 * We start with a delayed allocation:
788 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
791 * and we are allocating:
792 * +rrrrrrrrrrrrrrrrr+
795 * and we set it up for insertion as:
796 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
798 * PREV @ idx LEFT RIGHT
799 * inserted at idx + 1
801 temp
= new->br_startoff
- PREV
.br_startoff
;
802 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
803 trace_xfs_bmap_pre_update(ip
, *idx
, 0, _THIS_IP_
);
804 xfs_bmbt_set_blockcount(ep
, temp
); /* truncate PREV */
806 RIGHT
.br_state
= PREV
.br_state
;
807 RIGHT
.br_startblock
= nullstartblock(
808 (int)xfs_bmap_worst_indlen(ip
, temp2
));
809 RIGHT
.br_startoff
= new_endoff
;
810 RIGHT
.br_blockcount
= temp2
;
811 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
812 xfs_iext_insert(ip
, *idx
+ 1, 2, &LEFT
, state
);
813 ip
->i_d
.di_nextents
++;
815 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
817 rval
= XFS_ILOG_CORE
;
818 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
819 new->br_startblock
, new->br_blockcount
,
822 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
823 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
824 if ((error
= xfs_btree_insert(cur
, &i
)))
826 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
828 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
829 ip
->i_d
.di_nextents
> ip
->i_df
.if_ext_max
) {
830 error
= xfs_bmap_extents_to_btree(tp
, ip
,
831 first
, flist
, &cur
, 1, &tmp_rval
,
837 temp
= xfs_bmap_worst_indlen(ip
, temp
);
838 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
839 diff
= (int)(temp
+ temp2
- startblockval(PREV
.br_startblock
) -
840 (cur
? cur
->bc_private
.b
.allocated
: 0));
842 error
= xfs_icsb_modify_counters(ip
->i_mount
,
844 -((int64_t)diff
), 0);
850 ep
= xfs_iext_get_ext(ifp
, *idx
);
851 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
852 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
853 trace_xfs_bmap_pre_update(ip
, *idx
+ 2, state
, _THIS_IP_
);
854 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
+ 2),
855 nullstartblock((int)temp2
));
856 trace_xfs_bmap_post_update(ip
, *idx
+ 2, state
, _THIS_IP_
);
859 da_new
= temp
+ temp2
;
862 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
863 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
864 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
865 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
866 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
867 case BMAP_LEFT_CONTIG
:
868 case BMAP_RIGHT_CONTIG
:
870 * These cases are all impossible.
875 /* convert to a btree if necessary */
876 if (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) == XFS_DINODE_FMT_EXTENTS
&&
877 XFS_IFORK_NEXTENTS(ip
, XFS_DATA_FORK
) > ifp
->if_ext_max
) {
878 int tmp_logflags
; /* partial log flag return val */
881 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, flist
, &cur
,
882 da_old
> 0, &tmp_logflags
, XFS_DATA_FORK
);
883 *logflagsp
|= tmp_logflags
;
888 /* adjust for changes in reserved delayed indirect blocks */
889 if (da_old
|| da_new
) {
892 temp
+= cur
->bc_private
.b
.allocated
;
893 ASSERT(temp
<= da_old
);
895 xfs_icsb_modify_counters(ip
->i_mount
, XFS_SBS_FDBLOCKS
,
896 (int64_t)(da_old
- temp
), 0);
899 /* clear out the allocated field, done with it now in any case. */
901 cur
->bc_private
.b
.allocated
= 0;
904 xfs_bmap_check_leaf_extents(cur
, ip
, XFS_DATA_FORK
);
914 * Convert an unwritten allocation to a real allocation or vice versa.
916 STATIC
int /* error */
917 xfs_bmap_add_extent_unwritten_real(
918 struct xfs_trans
*tp
,
919 xfs_inode_t
*ip
, /* incore inode pointer */
920 xfs_extnum_t
*idx
, /* extent number to update/insert */
921 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
922 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
923 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
924 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
925 int *logflagsp
) /* inode logging flags */
927 xfs_btree_cur_t
*cur
; /* btree cursor */
928 xfs_bmbt_rec_host_t
*ep
; /* extent entry for idx */
929 int error
; /* error return value */
930 int i
; /* temp state */
931 xfs_ifork_t
*ifp
; /* inode fork pointer */
932 xfs_fileoff_t new_endoff
; /* end offset of new entry */
933 xfs_exntst_t newext
; /* new extent state */
934 xfs_exntst_t oldext
; /* old extent state */
935 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
936 /* left is 0, right is 1, prev is 2 */
937 int rval
=0; /* return value (logging flags) */
938 int state
= 0;/* state bits, accessed thru macros */
943 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
946 ASSERT(*idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
947 ASSERT(!isnullstartblock(new->br_startblock
));
949 XFS_STATS_INC(xs_add_exlist
);
956 * Set up a bunch of variables to make the tests simpler.
959 ep
= xfs_iext_get_ext(ifp
, *idx
);
960 xfs_bmbt_get_all(ep
, &PREV
);
961 newext
= new->br_state
;
962 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
963 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
964 ASSERT(PREV
.br_state
== oldext
);
965 new_endoff
= new->br_startoff
+ new->br_blockcount
;
966 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
967 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
970 * Set flags determining what part of the previous oldext allocation
971 * extent is being replaced by a newext allocation.
973 if (PREV
.br_startoff
== new->br_startoff
)
974 state
|= BMAP_LEFT_FILLING
;
975 if (PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
)
976 state
|= BMAP_RIGHT_FILLING
;
979 * Check and set flags if this segment has a left neighbor.
980 * Don't set contiguous if the combined extent would be too large.
983 state
|= BMAP_LEFT_VALID
;
984 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &LEFT
);
986 if (isnullstartblock(LEFT
.br_startblock
))
987 state
|= BMAP_LEFT_DELAY
;
990 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
991 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
992 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
993 LEFT
.br_state
== newext
&&
994 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
995 state
|= BMAP_LEFT_CONTIG
;
998 * Check and set flags if this segment has a right neighbor.
999 * Don't set contiguous if the combined extent would be too large.
1000 * Also check for all-three-contiguous being too large.
1002 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1) {
1003 state
|= BMAP_RIGHT_VALID
;
1004 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
+ 1), &RIGHT
);
1005 if (isnullstartblock(RIGHT
.br_startblock
))
1006 state
|= BMAP_RIGHT_DELAY
;
1009 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1010 new_endoff
== RIGHT
.br_startoff
&&
1011 new->br_startblock
+ new->br_blockcount
== RIGHT
.br_startblock
&&
1012 newext
== RIGHT
.br_state
&&
1013 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1014 ((state
& (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1015 BMAP_RIGHT_FILLING
)) !=
1016 (BMAP_LEFT_CONTIG
| BMAP_LEFT_FILLING
|
1017 BMAP_RIGHT_FILLING
) ||
1018 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1020 state
|= BMAP_RIGHT_CONTIG
;
1023 * Switch out based on the FILLING and CONTIG state bits.
1025 switch (state
& (BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1026 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
)) {
1027 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
|
1028 BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1030 * Setting all of a previous oldext extent to newext.
1031 * The left and right neighbors are both contiguous with new.
1035 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1036 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
1037 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1038 RIGHT
.br_blockcount
);
1039 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1041 xfs_iext_remove(ip
, *idx
+ 1, 2, state
);
1042 ip
->i_d
.di_nextents
-= 2;
1044 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1046 rval
= XFS_ILOG_CORE
;
1047 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
1048 RIGHT
.br_startblock
,
1049 RIGHT
.br_blockcount
, &i
)))
1051 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1052 if ((error
= xfs_btree_delete(cur
, &i
)))
1054 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1055 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
1057 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1058 if ((error
= xfs_btree_delete(cur
, &i
)))
1060 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1061 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
1063 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1064 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
1066 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1067 RIGHT
.br_blockcount
, LEFT
.br_state
)))
1072 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1074 * Setting all of a previous oldext extent to newext.
1075 * The left neighbor is contiguous, the right is not.
1079 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1080 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
1081 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
1082 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1084 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
1085 ip
->i_d
.di_nextents
--;
1087 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1089 rval
= XFS_ILOG_CORE
;
1090 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1091 PREV
.br_startblock
, PREV
.br_blockcount
,
1094 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1095 if ((error
= xfs_btree_delete(cur
, &i
)))
1097 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1098 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
1100 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1101 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
1103 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
1109 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1111 * Setting all of a previous oldext extent to newext.
1112 * The right neighbor is contiguous, the left is not.
1114 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1115 xfs_bmbt_set_blockcount(ep
,
1116 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
1117 xfs_bmbt_set_state(ep
, newext
);
1118 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1119 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
1120 ip
->i_d
.di_nextents
--;
1122 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1124 rval
= XFS_ILOG_CORE
;
1125 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
1126 RIGHT
.br_startblock
,
1127 RIGHT
.br_blockcount
, &i
)))
1129 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1130 if ((error
= xfs_btree_delete(cur
, &i
)))
1132 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1133 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
1135 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1136 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
1138 new->br_blockcount
+ RIGHT
.br_blockcount
,
1144 case BMAP_LEFT_FILLING
| BMAP_RIGHT_FILLING
:
1146 * Setting all of a previous oldext extent to newext.
1147 * Neither the left nor right neighbors are contiguous with
1150 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1151 xfs_bmbt_set_state(ep
, newext
);
1152 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1155 rval
= XFS_ILOG_DEXT
;
1158 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1159 new->br_startblock
, new->br_blockcount
,
1162 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1163 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
1164 new->br_startblock
, new->br_blockcount
,
1170 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
:
1172 * Setting the first part of a previous oldext extent to newext.
1173 * The left neighbor is contiguous.
1175 trace_xfs_bmap_pre_update(ip
, *idx
- 1, state
, _THIS_IP_
);
1176 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
- 1),
1177 LEFT
.br_blockcount
+ new->br_blockcount
);
1178 xfs_bmbt_set_startoff(ep
,
1179 PREV
.br_startoff
+ new->br_blockcount
);
1180 trace_xfs_bmap_post_update(ip
, *idx
- 1, state
, _THIS_IP_
);
1182 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1183 xfs_bmbt_set_startblock(ep
,
1184 new->br_startblock
+ new->br_blockcount
);
1185 xfs_bmbt_set_blockcount(ep
,
1186 PREV
.br_blockcount
- new->br_blockcount
);
1187 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1192 rval
= XFS_ILOG_DEXT
;
1195 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1196 PREV
.br_startblock
, PREV
.br_blockcount
,
1199 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1200 if ((error
= xfs_bmbt_update(cur
,
1201 PREV
.br_startoff
+ new->br_blockcount
,
1202 PREV
.br_startblock
+ new->br_blockcount
,
1203 PREV
.br_blockcount
- new->br_blockcount
,
1206 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
1208 if (xfs_bmbt_update(cur
, LEFT
.br_startoff
,
1210 LEFT
.br_blockcount
+ new->br_blockcount
,
1216 case BMAP_LEFT_FILLING
:
1218 * Setting the first part of a previous oldext extent to newext.
1219 * The left neighbor is not contiguous.
1221 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1222 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
1223 xfs_bmbt_set_startoff(ep
, new_endoff
);
1224 xfs_bmbt_set_blockcount(ep
,
1225 PREV
.br_blockcount
- new->br_blockcount
);
1226 xfs_bmbt_set_startblock(ep
,
1227 new->br_startblock
+ new->br_blockcount
);
1228 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1230 xfs_iext_insert(ip
, *idx
, 1, new, state
);
1231 ip
->i_d
.di_nextents
++;
1233 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1235 rval
= XFS_ILOG_CORE
;
1236 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1237 PREV
.br_startblock
, PREV
.br_blockcount
,
1240 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1241 if ((error
= xfs_bmbt_update(cur
,
1242 PREV
.br_startoff
+ new->br_blockcount
,
1243 PREV
.br_startblock
+ new->br_blockcount
,
1244 PREV
.br_blockcount
- new->br_blockcount
,
1247 cur
->bc_rec
.b
= *new;
1248 if ((error
= xfs_btree_insert(cur
, &i
)))
1250 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1254 case BMAP_RIGHT_FILLING
| BMAP_RIGHT_CONTIG
:
1256 * Setting the last part of a previous oldext extent to newext.
1257 * The right neighbor is contiguous with the new allocation.
1259 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1260 xfs_bmbt_set_blockcount(ep
,
1261 PREV
.br_blockcount
- new->br_blockcount
);
1262 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1266 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1267 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
1268 new->br_startoff
, new->br_startblock
,
1269 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
1270 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1273 rval
= XFS_ILOG_DEXT
;
1276 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1278 PREV
.br_blockcount
, &i
)))
1280 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1281 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
1283 PREV
.br_blockcount
- new->br_blockcount
,
1286 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
1288 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
1290 new->br_blockcount
+ RIGHT
.br_blockcount
,
1296 case BMAP_RIGHT_FILLING
:
1298 * Setting the last part of a previous oldext extent to newext.
1299 * The right neighbor is not contiguous.
1301 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1302 xfs_bmbt_set_blockcount(ep
,
1303 PREV
.br_blockcount
- new->br_blockcount
);
1304 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1307 xfs_iext_insert(ip
, *idx
, 1, new, state
);
1309 ip
->i_d
.di_nextents
++;
1311 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1313 rval
= XFS_ILOG_CORE
;
1314 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1315 PREV
.br_startblock
, PREV
.br_blockcount
,
1318 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1319 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
1321 PREV
.br_blockcount
- new->br_blockcount
,
1324 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1325 new->br_startblock
, new->br_blockcount
,
1328 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
1329 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1330 if ((error
= xfs_btree_insert(cur
, &i
)))
1332 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1338 * Setting the middle part of a previous oldext extent to
1339 * newext. Contiguity is impossible here.
1340 * One extent becomes three extents.
1342 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1343 xfs_bmbt_set_blockcount(ep
,
1344 new->br_startoff
- PREV
.br_startoff
);
1345 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1348 r
[1].br_startoff
= new_endoff
;
1349 r
[1].br_blockcount
=
1350 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1351 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
1352 r
[1].br_state
= oldext
;
1355 xfs_iext_insert(ip
, *idx
, 2, &r
[0], state
);
1357 ip
->i_d
.di_nextents
+= 2;
1359 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1361 rval
= XFS_ILOG_CORE
;
1362 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1363 PREV
.br_startblock
, PREV
.br_blockcount
,
1366 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1367 /* new right extent - oldext */
1368 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
1369 r
[1].br_startblock
, r
[1].br_blockcount
,
1372 /* new left extent - oldext */
1373 cur
->bc_rec
.b
= PREV
;
1374 cur
->bc_rec
.b
.br_blockcount
=
1375 new->br_startoff
- PREV
.br_startoff
;
1376 if ((error
= xfs_btree_insert(cur
, &i
)))
1378 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1380 * Reset the cursor to the position of the new extent
1381 * we are about to insert as we can't trust it after
1382 * the previous insert.
1384 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1385 new->br_startblock
, new->br_blockcount
,
1388 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
1389 /* new middle extent - newext */
1390 cur
->bc_rec
.b
.br_state
= new->br_state
;
1391 if ((error
= xfs_btree_insert(cur
, &i
)))
1393 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1397 case BMAP_LEFT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1398 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1399 case BMAP_LEFT_FILLING
| BMAP_RIGHT_CONTIG
:
1400 case BMAP_RIGHT_FILLING
| BMAP_LEFT_CONTIG
:
1401 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1402 case BMAP_LEFT_CONTIG
:
1403 case BMAP_RIGHT_CONTIG
:
1405 * These cases are all impossible.
1410 /* convert to a btree if necessary */
1411 if (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) == XFS_DINODE_FMT_EXTENTS
&&
1412 XFS_IFORK_NEXTENTS(ip
, XFS_DATA_FORK
) > ifp
->if_ext_max
) {
1413 int tmp_logflags
; /* partial log flag return val */
1415 ASSERT(cur
== NULL
);
1416 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
, flist
, &cur
,
1417 0, &tmp_logflags
, XFS_DATA_FORK
);
1418 *logflagsp
|= tmp_logflags
;
1423 /* clear out the allocated field, done with it now in any case. */
1425 cur
->bc_private
.b
.allocated
= 0;
1429 xfs_bmap_check_leaf_extents(*curp
, ip
, XFS_DATA_FORK
);
1439 * Convert a hole to a delayed allocation.
1442 xfs_bmap_add_extent_hole_delay(
1443 xfs_inode_t
*ip
, /* incore inode pointer */
1444 xfs_extnum_t
*idx
, /* extent number to update/insert */
1445 xfs_bmbt_irec_t
*new) /* new data to add to file extents */
1447 xfs_ifork_t
*ifp
; /* inode fork pointer */
1448 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
1449 xfs_filblks_t newlen
=0; /* new indirect size */
1450 xfs_filblks_t oldlen
=0; /* old indirect size */
1451 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
1452 int state
; /* state bits, accessed thru macros */
1453 xfs_filblks_t temp
=0; /* temp for indirect calculations */
1455 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
1457 ASSERT(isnullstartblock(new->br_startblock
));
1460 * Check and set flags if this segment has a left neighbor
1463 state
|= BMAP_LEFT_VALID
;
1464 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
1466 if (isnullstartblock(left
.br_startblock
))
1467 state
|= BMAP_LEFT_DELAY
;
1471 * Check and set flags if the current (right) segment exists.
1472 * If it doesn't exist, we're converting the hole at end-of-file.
1474 if (*idx
< ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
1475 state
|= BMAP_RIGHT_VALID
;
1476 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
1478 if (isnullstartblock(right
.br_startblock
))
1479 state
|= BMAP_RIGHT_DELAY
;
1483 * Set contiguity flags on the left and right neighbors.
1484 * Don't let extents get too large, even if the pieces are contiguous.
1486 if ((state
& BMAP_LEFT_VALID
) && (state
& BMAP_LEFT_DELAY
) &&
1487 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
1488 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1489 state
|= BMAP_LEFT_CONTIG
;
1491 if ((state
& BMAP_RIGHT_VALID
) && (state
& BMAP_RIGHT_DELAY
) &&
1492 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
1493 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
1494 (!(state
& BMAP_LEFT_CONTIG
) ||
1495 (left
.br_blockcount
+ new->br_blockcount
+
1496 right
.br_blockcount
<= MAXEXTLEN
)))
1497 state
|= BMAP_RIGHT_CONTIG
;
1500 * Switch out based on the contiguity flags.
1502 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
1503 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1505 * New allocation is contiguous with delayed allocations
1506 * on the left and on the right.
1507 * Merge all three into a single extent record.
1510 temp
= left
.br_blockcount
+ new->br_blockcount
+
1511 right
.br_blockcount
;
1513 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1514 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
1515 oldlen
= startblockval(left
.br_startblock
) +
1516 startblockval(new->br_startblock
) +
1517 startblockval(right
.br_startblock
);
1518 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
1519 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
1520 nullstartblock((int)newlen
));
1521 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1523 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
1526 case BMAP_LEFT_CONTIG
:
1528 * New allocation is contiguous with a delayed allocation
1530 * Merge the new allocation with the left neighbor.
1533 temp
= left
.br_blockcount
+ new->br_blockcount
;
1535 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1536 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
), temp
);
1537 oldlen
= startblockval(left
.br_startblock
) +
1538 startblockval(new->br_startblock
);
1539 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
1540 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, *idx
),
1541 nullstartblock((int)newlen
));
1542 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1545 case BMAP_RIGHT_CONTIG
:
1547 * New allocation is contiguous with a delayed allocation
1549 * Merge the new allocation with the right neighbor.
1551 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1552 temp
= new->br_blockcount
+ right
.br_blockcount
;
1553 oldlen
= startblockval(new->br_startblock
) +
1554 startblockval(right
.br_startblock
);
1555 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
1556 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
1558 nullstartblock((int)newlen
), temp
, right
.br_state
);
1559 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1564 * New allocation is not contiguous with another
1565 * delayed allocation.
1566 * Insert a new entry.
1568 oldlen
= newlen
= 0;
1569 xfs_iext_insert(ip
, *idx
, 1, new, state
);
1572 if (oldlen
!= newlen
) {
1573 ASSERT(oldlen
> newlen
);
1574 xfs_icsb_modify_counters(ip
->i_mount
, XFS_SBS_FDBLOCKS
,
1575 (int64_t)(oldlen
- newlen
), 0);
1577 * Nothing to do for disk quota accounting here.
1583 * Convert a hole to a real allocation.
1585 STATIC
int /* error */
1586 xfs_bmap_add_extent_hole_real(
1587 struct xfs_trans
*tp
,
1588 xfs_inode_t
*ip
, /* incore inode pointer */
1589 xfs_extnum_t
*idx
, /* extent number to update/insert */
1590 xfs_btree_cur_t
**curp
, /* if null, not a btree */
1591 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
1592 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
1593 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
1594 int *logflagsp
, /* inode logging flags */
1595 int whichfork
) /* data or attr fork */
1597 int error
; /* error return value */
1598 int i
; /* temp state */
1599 xfs_btree_cur_t
*cur
; /* if null, not a btree */
1600 xfs_ifork_t
*ifp
; /* inode fork pointer */
1601 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
1602 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
1603 int rval
=0; /* return value (logging flags) */
1604 int state
; /* state bits, accessed thru macros */
1608 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1612 ASSERT(*idx
<= ifp
->if_bytes
/ sizeof(struct xfs_bmbt_rec
));
1613 ASSERT(!isnullstartblock(new->br_startblock
));
1614 ASSERT(!cur
|| !(cur
->bc_private
.b
.flags
& XFS_BTCUR_BPRV_WASDEL
));
1616 XFS_STATS_INC(xs_add_exlist
);
1619 if (whichfork
== XFS_ATTR_FORK
)
1620 state
|= BMAP_ATTRFORK
;
1623 * Check and set flags if this segment has a left neighbor.
1626 state
|= BMAP_LEFT_VALID
;
1627 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
- 1), &left
);
1628 if (isnullstartblock(left
.br_startblock
))
1629 state
|= BMAP_LEFT_DELAY
;
1633 * Check and set flags if this segment has a current value.
1634 * Not true if we're inserting into the "hole" at eof.
1636 if (*idx
< ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)) {
1637 state
|= BMAP_RIGHT_VALID
;
1638 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *idx
), &right
);
1639 if (isnullstartblock(right
.br_startblock
))
1640 state
|= BMAP_RIGHT_DELAY
;
1644 * We're inserting a real allocation between "left" and "right".
1645 * Set the contiguity flags. Don't let extents get too large.
1647 if ((state
& BMAP_LEFT_VALID
) && !(state
& BMAP_LEFT_DELAY
) &&
1648 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
1649 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
1650 left
.br_state
== new->br_state
&&
1651 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
)
1652 state
|= BMAP_LEFT_CONTIG
;
1654 if ((state
& BMAP_RIGHT_VALID
) && !(state
& BMAP_RIGHT_DELAY
) &&
1655 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
1656 new->br_startblock
+ new->br_blockcount
== right
.br_startblock
&&
1657 new->br_state
== right
.br_state
&&
1658 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
1659 (!(state
& BMAP_LEFT_CONTIG
) ||
1660 left
.br_blockcount
+ new->br_blockcount
+
1661 right
.br_blockcount
<= MAXEXTLEN
))
1662 state
|= BMAP_RIGHT_CONTIG
;
1666 * Select which case we're in here, and implement it.
1668 switch (state
& (BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
)) {
1669 case BMAP_LEFT_CONTIG
| BMAP_RIGHT_CONTIG
:
1671 * New allocation is contiguous with real allocations on the
1672 * left and on the right.
1673 * Merge all three into a single extent record.
1676 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1677 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
1678 left
.br_blockcount
+ new->br_blockcount
+
1679 right
.br_blockcount
);
1680 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1682 xfs_iext_remove(ip
, *idx
+ 1, 1, state
);
1684 XFS_IFORK_NEXT_SET(ip
, whichfork
,
1685 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
1687 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
1689 rval
= XFS_ILOG_CORE
;
1690 if ((error
= xfs_bmbt_lookup_eq(cur
,
1692 right
.br_startblock
,
1693 right
.br_blockcount
, &i
)))
1695 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1696 if ((error
= xfs_btree_delete(cur
, &i
)))
1698 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1699 if ((error
= xfs_btree_decrement(cur
, 0, &i
)))
1701 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1702 if ((error
= xfs_bmbt_update(cur
, left
.br_startoff
,
1704 left
.br_blockcount
+
1705 new->br_blockcount
+
1706 right
.br_blockcount
,
1712 case BMAP_LEFT_CONTIG
:
1714 * New allocation is contiguous with a real allocation
1716 * Merge the new allocation with the left neighbor.
1719 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1720 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, *idx
),
1721 left
.br_blockcount
+ new->br_blockcount
);
1722 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1725 rval
= xfs_ilog_fext(whichfork
);
1728 if ((error
= xfs_bmbt_lookup_eq(cur
,
1731 left
.br_blockcount
, &i
)))
1733 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1734 if ((error
= xfs_bmbt_update(cur
, left
.br_startoff
,
1736 left
.br_blockcount
+
1743 case BMAP_RIGHT_CONTIG
:
1745 * New allocation is contiguous with a real allocation
1747 * Merge the new allocation with the right neighbor.
1749 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
1750 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, *idx
),
1751 new->br_startoff
, new->br_startblock
,
1752 new->br_blockcount
+ right
.br_blockcount
,
1754 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
1757 rval
= xfs_ilog_fext(whichfork
);
1760 if ((error
= xfs_bmbt_lookup_eq(cur
,
1762 right
.br_startblock
,
1763 right
.br_blockcount
, &i
)))
1765 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1766 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
1768 new->br_blockcount
+
1769 right
.br_blockcount
,
1777 * New allocation is not contiguous with another
1779 * Insert a new entry.
1781 xfs_iext_insert(ip
, *idx
, 1, new, state
);
1782 XFS_IFORK_NEXT_SET(ip
, whichfork
,
1783 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
1785 rval
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
1787 rval
= XFS_ILOG_CORE
;
1788 if ((error
= xfs_bmbt_lookup_eq(cur
,
1791 new->br_blockcount
, &i
)))
1793 XFS_WANT_CORRUPTED_GOTO(i
== 0, done
);
1794 cur
->bc_rec
.b
.br_state
= new->br_state
;
1795 if ((error
= xfs_btree_insert(cur
, &i
)))
1797 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
1802 /* convert to a btree if necessary */
1803 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
1804 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ifp
->if_ext_max
) {
1805 int tmp_logflags
; /* partial log flag return val */
1807 ASSERT(cur
== NULL
);
1808 error
= xfs_bmap_extents_to_btree(tp
, ip
, first
,
1809 flist
, &cur
, 0, &tmp_logflags
, whichfork
);
1810 *logflagsp
|= tmp_logflags
;
1815 /* clear out the allocated field, done with it now in any case. */
1817 cur
->bc_private
.b
.allocated
= 0;
1820 xfs_bmap_check_leaf_extents(cur
, ip
, whichfork
);
1827 * Adjust the size of the new extent based on di_extsize and rt extsize.
1830 xfs_bmap_extsize_align(
1832 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
1833 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
1834 xfs_extlen_t extsz
, /* align to this extent size */
1835 int rt
, /* is this a realtime inode? */
1836 int eof
, /* is extent at end-of-file? */
1837 int delay
, /* creating delalloc extent? */
1838 int convert
, /* overwriting unwritten extent? */
1839 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
1840 xfs_extlen_t
*lenp
) /* in/out: aligned length */
1842 xfs_fileoff_t orig_off
; /* original offset */
1843 xfs_extlen_t orig_alen
; /* original length */
1844 xfs_fileoff_t orig_end
; /* original off+len */
1845 xfs_fileoff_t nexto
; /* next file offset */
1846 xfs_fileoff_t prevo
; /* previous file offset */
1847 xfs_fileoff_t align_off
; /* temp for offset */
1848 xfs_extlen_t align_alen
; /* temp for length */
1849 xfs_extlen_t temp
; /* temp for calculations */
1854 orig_off
= align_off
= *offp
;
1855 orig_alen
= align_alen
= *lenp
;
1856 orig_end
= orig_off
+ orig_alen
;
1859 * If this request overlaps an existing extent, then don't
1860 * attempt to perform any additional alignment.
1862 if (!delay
&& !eof
&&
1863 (orig_off
>= gotp
->br_startoff
) &&
1864 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
1869 * If the file offset is unaligned vs. the extent size
1870 * we need to align it. This will be possible unless
1871 * the file was previously written with a kernel that didn't
1872 * perform this alignment, or if a truncate shot us in the
1875 temp
= do_mod(orig_off
, extsz
);
1881 * Same adjustment for the end of the requested area.
1883 if ((temp
= (align_alen
% extsz
))) {
1884 align_alen
+= extsz
- temp
;
1887 * If the previous block overlaps with this proposed allocation
1888 * then move the start forward without adjusting the length.
1890 if (prevp
->br_startoff
!= NULLFILEOFF
) {
1891 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
1892 prevo
= prevp
->br_startoff
;
1894 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
1897 if (align_off
!= orig_off
&& align_off
< prevo
)
1900 * If the next block overlaps with this proposed allocation
1901 * then move the start back without adjusting the length,
1902 * but not before offset 0.
1903 * This may of course make the start overlap previous block,
1904 * and if we hit the offset 0 limit then the next block
1905 * can still overlap too.
1907 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
1908 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
1909 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
1910 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
1912 nexto
= gotp
->br_startoff
;
1914 nexto
= NULLFILEOFF
;
1916 align_off
+ align_alen
!= orig_end
&&
1917 align_off
+ align_alen
> nexto
)
1918 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
1920 * If we're now overlapping the next or previous extent that
1921 * means we can't fit an extsz piece in this hole. Just move
1922 * the start forward to the first valid spot and set
1923 * the length so we hit the end.
1925 if (align_off
!= orig_off
&& align_off
< prevo
)
1927 if (align_off
+ align_alen
!= orig_end
&&
1928 align_off
+ align_alen
> nexto
&&
1929 nexto
!= NULLFILEOFF
) {
1930 ASSERT(nexto
> prevo
);
1931 align_alen
= nexto
- align_off
;
1935 * If realtime, and the result isn't a multiple of the realtime
1936 * extent size we need to remove blocks until it is.
1938 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
1940 * We're not covering the original request, or
1941 * we won't be able to once we fix the length.
1943 if (orig_off
< align_off
||
1944 orig_end
> align_off
+ align_alen
||
1945 align_alen
- temp
< orig_alen
)
1946 return XFS_ERROR(EINVAL
);
1948 * Try to fix it by moving the start up.
1950 if (align_off
+ temp
<= orig_off
) {
1955 * Try to fix it by moving the end in.
1957 else if (align_off
+ align_alen
- temp
>= orig_end
)
1960 * Set the start to the minimum then trim the length.
1963 align_alen
-= orig_off
- align_off
;
1964 align_off
= orig_off
;
1965 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
1968 * Result doesn't cover the request, fail it.
1970 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
1971 return XFS_ERROR(EINVAL
);
1973 ASSERT(orig_off
>= align_off
);
1974 ASSERT(orig_end
<= align_off
+ align_alen
);
1978 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
1979 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
1980 if (prevp
->br_startoff
!= NULLFILEOFF
)
1981 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
1989 #define XFS_ALLOC_GAP_UNITS 4
1993 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
1995 xfs_fsblock_t adjust
; /* adjustment to block numbers */
1996 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
1997 xfs_mount_t
*mp
; /* mount point structure */
1998 int nullfb
; /* true if ap->firstblock isn't set */
1999 int rt
; /* true if inode is realtime */
2001 #define ISVALID(x,y) \
2003 (x) < mp->m_sb.sb_rblocks : \
2004 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2005 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2006 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2008 mp
= ap
->ip
->i_mount
;
2009 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
2010 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
;
2011 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
2013 * If allocating at eof, and there's a previous real block,
2014 * try to use its last block as our starting point.
2016 if (ap
->eof
&& ap
->prev
.br_startoff
!= NULLFILEOFF
&&
2017 !isnullstartblock(ap
->prev
.br_startblock
) &&
2018 ISVALID(ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
,
2019 ap
->prev
.br_startblock
)) {
2020 ap
->rval
= ap
->prev
.br_startblock
+ ap
->prev
.br_blockcount
;
2022 * Adjust for the gap between prevp and us.
2025 (ap
->prev
.br_startoff
+ ap
->prev
.br_blockcount
);
2027 ISVALID(ap
->rval
+ adjust
, ap
->prev
.br_startblock
))
2031 * If not at eof, then compare the two neighbor blocks.
2032 * Figure out whether either one gives us a good starting point,
2033 * and pick the better one.
2035 else if (!ap
->eof
) {
2036 xfs_fsblock_t gotbno
; /* right side block number */
2037 xfs_fsblock_t gotdiff
=0; /* right side difference */
2038 xfs_fsblock_t prevbno
; /* left side block number */
2039 xfs_fsblock_t prevdiff
=0; /* left side difference */
2042 * If there's a previous (left) block, select a requested
2043 * start block based on it.
2045 if (ap
->prev
.br_startoff
!= NULLFILEOFF
&&
2046 !isnullstartblock(ap
->prev
.br_startblock
) &&
2047 (prevbno
= ap
->prev
.br_startblock
+
2048 ap
->prev
.br_blockcount
) &&
2049 ISVALID(prevbno
, ap
->prev
.br_startblock
)) {
2051 * Calculate gap to end of previous block.
2053 adjust
= prevdiff
= ap
->off
-
2054 (ap
->prev
.br_startoff
+
2055 ap
->prev
.br_blockcount
);
2057 * Figure the startblock based on the previous block's
2058 * end and the gap size.
2060 * If the gap is large relative to the piece we're
2061 * allocating, or using it gives us an invalid block
2062 * number, then just use the end of the previous block.
2064 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->alen
&&
2065 ISVALID(prevbno
+ prevdiff
,
2066 ap
->prev
.br_startblock
))
2071 * If the firstblock forbids it, can't use it,
2074 if (!rt
&& !nullfb
&&
2075 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
2076 prevbno
= NULLFSBLOCK
;
2079 * No previous block or can't follow it, just default.
2082 prevbno
= NULLFSBLOCK
;
2084 * If there's a following (right) block, select a requested
2085 * start block based on it.
2087 if (!isnullstartblock(ap
->got
.br_startblock
)) {
2089 * Calculate gap to start of next block.
2091 adjust
= gotdiff
= ap
->got
.br_startoff
- ap
->off
;
2093 * Figure the startblock based on the next block's
2094 * start and the gap size.
2096 gotbno
= ap
->got
.br_startblock
;
2099 * If the gap is large relative to the piece we're
2100 * allocating, or using it gives us an invalid block
2101 * number, then just use the start of the next block
2102 * offset by our length.
2104 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->alen
&&
2105 ISVALID(gotbno
- gotdiff
, gotbno
))
2107 else if (ISVALID(gotbno
- ap
->alen
, gotbno
)) {
2109 gotdiff
+= adjust
- ap
->alen
;
2113 * If the firstblock forbids it, can't use it,
2116 if (!rt
&& !nullfb
&&
2117 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
2118 gotbno
= NULLFSBLOCK
;
2121 * No next block, just default.
2124 gotbno
= NULLFSBLOCK
;
2126 * If both valid, pick the better one, else the only good
2127 * one, else ap->rval is already set (to 0 or the inode block).
2129 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
2130 ap
->rval
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
2131 else if (prevbno
!= NULLFSBLOCK
)
2133 else if (gotbno
!= NULLFSBLOCK
)
2141 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
2143 xfs_alloctype_t atype
= 0; /* type for allocation routines */
2144 int error
; /* error return value */
2145 xfs_mount_t
*mp
; /* mount point structure */
2146 xfs_extlen_t prod
= 0; /* product factor for allocators */
2147 xfs_extlen_t ralen
= 0; /* realtime allocation length */
2148 xfs_extlen_t align
; /* minimum allocation alignment */
2151 mp
= ap
->ip
->i_mount
;
2152 align
= xfs_get_extsz_hint(ap
->ip
);
2153 prod
= align
/ mp
->m_sb
.sb_rextsize
;
2154 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
2155 align
, 1, ap
->eof
, 0,
2156 ap
->conv
, &ap
->off
, &ap
->alen
);
2160 ASSERT(ap
->alen
% mp
->m_sb
.sb_rextsize
== 0);
2163 * If the offset & length are not perfectly aligned
2164 * then kill prod, it will just get us in trouble.
2166 if (do_mod(ap
->off
, align
) || ap
->alen
% align
)
2169 * Set ralen to be the actual requested length in rtextents.
2171 ralen
= ap
->alen
/ mp
->m_sb
.sb_rextsize
;
2173 * If the old value was close enough to MAXEXTLEN that
2174 * we rounded up to it, cut it back so it's valid again.
2175 * Note that if it's a really large request (bigger than
2176 * MAXEXTLEN), we don't hear about that number, and can't
2177 * adjust the starting point to match it.
2179 if (ralen
* mp
->m_sb
.sb_rextsize
>= MAXEXTLEN
)
2180 ralen
= MAXEXTLEN
/ mp
->m_sb
.sb_rextsize
;
2183 * Lock out other modifications to the RT bitmap inode.
2185 xfs_ilock(mp
->m_rbmip
, XFS_ILOCK_EXCL
);
2186 xfs_trans_ijoin_ref(ap
->tp
, mp
->m_rbmip
, XFS_ILOCK_EXCL
);
2189 * If it's an allocation to an empty file at offset 0,
2190 * pick an extent that will space things out in the rt area.
2192 if (ap
->eof
&& ap
->off
== 0) {
2193 xfs_rtblock_t
uninitialized_var(rtx
); /* realtime extent no */
2195 error
= xfs_rtpick_extent(mp
, ap
->tp
, ralen
, &rtx
);
2198 ap
->rval
= rtx
* mp
->m_sb
.sb_rextsize
;
2203 xfs_bmap_adjacent(ap
);
2206 * Realtime allocation, done through xfs_rtallocate_extent.
2208 atype
= ap
->rval
== 0 ? XFS_ALLOCTYPE_ANY_AG
: XFS_ALLOCTYPE_NEAR_BNO
;
2209 do_div(ap
->rval
, mp
->m_sb
.sb_rextsize
);
2212 if ((error
= xfs_rtallocate_extent(ap
->tp
, ap
->rval
, 1, ap
->alen
,
2213 &ralen
, atype
, ap
->wasdel
, prod
, &rtb
)))
2215 if (rtb
== NULLFSBLOCK
&& prod
> 1 &&
2216 (error
= xfs_rtallocate_extent(ap
->tp
, ap
->rval
, 1,
2217 ap
->alen
, &ralen
, atype
,
2218 ap
->wasdel
, 1, &rtb
)))
2221 if (ap
->rval
!= NULLFSBLOCK
) {
2222 ap
->rval
*= mp
->m_sb
.sb_rextsize
;
2223 ralen
*= mp
->m_sb
.sb_rextsize
;
2225 ap
->ip
->i_d
.di_nblocks
+= ralen
;
2226 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
2228 ap
->ip
->i_delayed_blks
-= ralen
;
2230 * Adjust the disk quota also. This was reserved
2233 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
2234 ap
->wasdel
? XFS_TRANS_DQ_DELRTBCOUNT
:
2235 XFS_TRANS_DQ_RTBCOUNT
, (long) ralen
);
2243 xfs_bmap_btalloc_nullfb(
2244 struct xfs_bmalloca
*ap
,
2245 struct xfs_alloc_arg
*args
,
2248 struct xfs_mount
*mp
= ap
->ip
->i_mount
;
2249 struct xfs_perag
*pag
;
2250 xfs_agnumber_t ag
, startag
;
2254 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
))
2255 args
->type
= XFS_ALLOCTYPE_NEAR_BNO
;
2257 args
->type
= XFS_ALLOCTYPE_START_BNO
;
2258 args
->total
= ap
->total
;
2261 * Search for an allocation group with a single extent large enough
2262 * for the request. If one isn't found, then adjust the minimum
2263 * allocation size to the largest space found.
2265 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
->fsbno
);
2266 if (startag
== NULLAGNUMBER
)
2269 pag
= xfs_perag_get(mp
, ag
);
2270 while (*blen
< args
->maxlen
) {
2271 if (!pag
->pagf_init
) {
2272 error
= xfs_alloc_pagf_init(mp
, args
->tp
, ag
,
2273 XFS_ALLOC_FLAG_TRYLOCK
);
2281 * See xfs_alloc_fix_freelist...
2283 if (pag
->pagf_init
) {
2284 xfs_extlen_t longest
;
2285 longest
= xfs_alloc_longest_free_extent(mp
, pag
);
2286 if (*blen
< longest
)
2291 if (xfs_inode_is_filestream(ap
->ip
)) {
2292 if (*blen
>= args
->maxlen
)
2297 * If startag is an invalid AG, we've
2298 * come here once before and
2299 * xfs_filestream_new_ag picked the
2300 * best currently available.
2302 * Don't continue looping, since we
2303 * could loop forever.
2305 if (startag
== NULLAGNUMBER
)
2308 error
= xfs_filestream_new_ag(ap
, &ag
);
2313 /* loop again to set 'blen'*/
2314 startag
= NULLAGNUMBER
;
2315 pag
= xfs_perag_get(mp
, ag
);
2319 if (++ag
== mp
->m_sb
.sb_agcount
)
2324 pag
= xfs_perag_get(mp
, ag
);
2329 * Since the above loop did a BUF_TRYLOCK, it is
2330 * possible that there is space for this request.
2332 if (notinit
|| *blen
< ap
->minlen
)
2333 args
->minlen
= ap
->minlen
;
2335 * If the best seen length is less than the request
2336 * length, use the best as the minimum.
2338 else if (*blen
< args
->maxlen
)
2339 args
->minlen
= *blen
;
2341 * Otherwise we've seen an extent as big as maxlen,
2342 * use that as the minimum.
2345 args
->minlen
= args
->maxlen
;
2348 * set the failure fallback case to look in the selected
2349 * AG as the stream may have moved.
2351 if (xfs_inode_is_filestream(ap
->ip
))
2352 ap
->rval
= args
->fsbno
= XFS_AGB_TO_FSB(mp
, ag
, 0);
2359 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
2361 xfs_mount_t
*mp
; /* mount point structure */
2362 xfs_alloctype_t atype
= 0; /* type for allocation routines */
2363 xfs_extlen_t align
; /* minimum allocation alignment */
2364 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
2366 xfs_alloc_arg_t args
;
2368 xfs_extlen_t nextminlen
= 0;
2369 int nullfb
; /* true if ap->firstblock isn't set */
2374 mp
= ap
->ip
->i_mount
;
2375 align
= ap
->userdata
? xfs_get_extsz_hint(ap
->ip
) : 0;
2376 if (unlikely(align
)) {
2377 error
= xfs_bmap_extsize_align(mp
, &ap
->got
, &ap
->prev
,
2378 align
, 0, ap
->eof
, 0, ap
->conv
,
2379 &ap
->off
, &ap
->alen
);
2383 nullfb
= *ap
->firstblock
== NULLFSBLOCK
;
2384 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
);
2386 if (ap
->userdata
&& xfs_inode_is_filestream(ap
->ip
)) {
2387 ag
= xfs_filestream_lookup_ag(ap
->ip
);
2388 ag
= (ag
!= NULLAGNUMBER
) ? ag
: 0;
2389 ap
->rval
= XFS_AGB_TO_FSB(mp
, ag
, 0);
2391 ap
->rval
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
2394 ap
->rval
= *ap
->firstblock
;
2396 xfs_bmap_adjacent(ap
);
2399 * If allowed, use ap->rval; otherwise must use firstblock since
2400 * it's in the right allocation group.
2402 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->rval
) == fb_agno
)
2405 ap
->rval
= *ap
->firstblock
;
2407 * Normal allocation, done through xfs_alloc_vextent.
2409 tryagain
= isaligned
= 0;
2412 args
.fsbno
= ap
->rval
;
2414 /* Trim the allocation back to the maximum an AG can fit. */
2415 args
.maxlen
= MIN(ap
->alen
, XFS_ALLOC_AG_MAX_USABLE(mp
));
2416 args
.firstblock
= *ap
->firstblock
;
2419 error
= xfs_bmap_btalloc_nullfb(ap
, &args
, &blen
);
2422 } else if (ap
->flist
->xbf_low
) {
2423 if (xfs_inode_is_filestream(ap
->ip
))
2424 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
2426 args
.type
= XFS_ALLOCTYPE_START_BNO
;
2427 args
.total
= args
.minlen
= ap
->minlen
;
2429 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
2430 args
.total
= ap
->total
;
2431 args
.minlen
= ap
->minlen
;
2433 /* apply extent size hints if obtained earlier */
2434 if (unlikely(align
)) {
2436 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->off
, args
.prod
)))
2437 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
2438 } else if (mp
->m_sb
.sb_blocksize
>= PAGE_CACHE_SIZE
) {
2442 args
.prod
= PAGE_CACHE_SIZE
>> mp
->m_sb
.sb_blocklog
;
2443 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->off
, args
.prod
))))
2444 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
2447 * If we are not low on available data blocks, and the
2448 * underlying logical volume manager is a stripe, and
2449 * the file offset is zero then try to allocate data
2450 * blocks on stripe unit boundary.
2451 * NOTE: ap->aeof is only set if the allocation length
2452 * is >= the stripe unit and the allocation offset is
2453 * at the end of file.
2455 if (!ap
->flist
->xbf_low
&& ap
->aeof
) {
2457 args
.alignment
= mp
->m_dalign
;
2461 * Adjust for alignment
2463 if (blen
> args
.alignment
&& blen
<= args
.maxlen
)
2464 args
.minlen
= blen
- args
.alignment
;
2465 args
.minalignslop
= 0;
2468 * First try an exact bno allocation.
2469 * If it fails then do a near or start bno
2470 * allocation with alignment turned on.
2474 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
2477 * Compute the minlen+alignment for the
2478 * next case. Set slop so that the value
2479 * of minlen+alignment+slop doesn't go up
2480 * between the calls.
2482 if (blen
> mp
->m_dalign
&& blen
<= args
.maxlen
)
2483 nextminlen
= blen
- mp
->m_dalign
;
2485 nextminlen
= args
.minlen
;
2486 if (nextminlen
+ mp
->m_dalign
> args
.minlen
+ 1)
2488 nextminlen
+ mp
->m_dalign
-
2491 args
.minalignslop
= 0;
2495 args
.minalignslop
= 0;
2497 args
.minleft
= ap
->minleft
;
2498 args
.wasdel
= ap
->wasdel
;
2500 args
.userdata
= ap
->userdata
;
2501 if ((error
= xfs_alloc_vextent(&args
)))
2503 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
2505 * Exact allocation failed. Now try with alignment
2509 args
.fsbno
= ap
->rval
;
2510 args
.alignment
= mp
->m_dalign
;
2511 args
.minlen
= nextminlen
;
2512 args
.minalignslop
= 0;
2514 if ((error
= xfs_alloc_vextent(&args
)))
2517 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
2519 * allocation failed, so turn off alignment and
2523 args
.fsbno
= ap
->rval
;
2525 if ((error
= xfs_alloc_vextent(&args
)))
2528 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
2529 args
.minlen
> ap
->minlen
) {
2530 args
.minlen
= ap
->minlen
;
2531 args
.type
= XFS_ALLOCTYPE_START_BNO
;
2532 args
.fsbno
= ap
->rval
;
2533 if ((error
= xfs_alloc_vextent(&args
)))
2536 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
2538 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
2539 args
.total
= ap
->minlen
;
2541 if ((error
= xfs_alloc_vextent(&args
)))
2543 ap
->flist
->xbf_low
= 1;
2545 if (args
.fsbno
!= NULLFSBLOCK
) {
2547 * check the allocation happened at the same or higher AG than
2548 * the first block that was allocated.
2550 ASSERT(*ap
->firstblock
== NULLFSBLOCK
||
2551 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) ==
2552 XFS_FSB_TO_AGNO(mp
, args
.fsbno
) ||
2553 (ap
->flist
->xbf_low
&&
2554 XFS_FSB_TO_AGNO(mp
, *ap
->firstblock
) <
2555 XFS_FSB_TO_AGNO(mp
, args
.fsbno
)));
2557 ap
->rval
= args
.fsbno
;
2558 if (*ap
->firstblock
== NULLFSBLOCK
)
2559 *ap
->firstblock
= args
.fsbno
;
2560 ASSERT(nullfb
|| fb_agno
== args
.agno
||
2561 (ap
->flist
->xbf_low
&& fb_agno
< args
.agno
));
2562 ap
->alen
= args
.len
;
2563 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
2564 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
2566 ap
->ip
->i_delayed_blks
-= args
.len
;
2568 * Adjust the disk quota also. This was reserved
2571 xfs_trans_mod_dquot_byino(ap
->tp
, ap
->ip
,
2572 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
2573 XFS_TRANS_DQ_BCOUNT
,
2576 ap
->rval
= NULLFSBLOCK
;
2583 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2584 * It figures out where to ask the underlying allocator to put the new extent.
2588 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
2590 if (XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
)
2591 return xfs_bmap_rtalloc(ap
);
2592 return xfs_bmap_btalloc(ap
);
2596 * Transform a btree format file with only one leaf node, where the
2597 * extents list will fit in the inode, into an extents format file.
2598 * Since the file extents are already in-core, all we have to do is
2599 * give up the space for the btree root and pitch the leaf block.
2601 STATIC
int /* error */
2602 xfs_bmap_btree_to_extents(
2603 xfs_trans_t
*tp
, /* transaction pointer */
2604 xfs_inode_t
*ip
, /* incore inode pointer */
2605 xfs_btree_cur_t
*cur
, /* btree cursor */
2606 int *logflagsp
, /* inode logging flags */
2607 int whichfork
) /* data or attr fork */
2610 struct xfs_btree_block
*cblock
;/* child btree block */
2611 xfs_fsblock_t cbno
; /* child block number */
2612 xfs_buf_t
*cbp
; /* child block's buffer */
2613 int error
; /* error return value */
2614 xfs_ifork_t
*ifp
; /* inode fork data */
2615 xfs_mount_t
*mp
; /* mount point structure */
2616 __be64
*pp
; /* ptr to block address */
2617 struct xfs_btree_block
*rblock
;/* root btree block */
2620 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2621 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
2622 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
2623 rblock
= ifp
->if_broot
;
2624 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
2625 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
2626 ASSERT(xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0) == 1);
2627 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, rblock
, 1, ifp
->if_broot_bytes
);
2628 cbno
= be64_to_cpu(*pp
);
2631 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
2634 if ((error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
,
2635 XFS_BMAP_BTREE_REF
)))
2637 cblock
= XFS_BUF_TO_BLOCK(cbp
);
2638 if ((error
= xfs_btree_check_block(cur
, cblock
, 0, cbp
)))
2640 xfs_bmap_add_free(cbno
, 1, cur
->bc_private
.b
.flist
, mp
);
2641 ip
->i_d
.di_nblocks
--;
2642 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
2643 xfs_trans_binval(tp
, cbp
);
2644 if (cur
->bc_bufs
[0] == cbp
)
2645 cur
->bc_bufs
[0] = NULL
;
2646 xfs_iroot_realloc(ip
, -1, whichfork
);
2647 ASSERT(ifp
->if_broot
== NULL
);
2648 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
2649 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
2650 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fext(whichfork
);
2655 * Called by xfs_bmapi to update file extent records and the btree
2656 * after removing space (or undoing a delayed allocation).
2658 STATIC
int /* error */
2659 xfs_bmap_del_extent(
2660 xfs_inode_t
*ip
, /* incore inode pointer */
2661 xfs_trans_t
*tp
, /* current transaction pointer */
2662 xfs_extnum_t
*idx
, /* extent number to update/delete */
2663 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
2664 xfs_btree_cur_t
*cur
, /* if null, not a btree */
2665 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
2666 int *logflagsp
, /* inode logging flags */
2667 int whichfork
) /* data or attr fork */
2669 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
2670 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
2671 xfs_fsblock_t del_endblock
=0; /* first block past del */
2672 xfs_fileoff_t del_endoff
; /* first offset past del */
2673 int delay
; /* current block is delayed allocated */
2674 int do_fx
; /* free extent at end of routine */
2675 xfs_bmbt_rec_host_t
*ep
; /* current extent entry pointer */
2676 int error
; /* error return value */
2677 int flags
; /* inode logging flags */
2678 xfs_bmbt_irec_t got
; /* current extent entry */
2679 xfs_fileoff_t got_endoff
; /* first offset past got */
2680 int i
; /* temp state */
2681 xfs_ifork_t
*ifp
; /* inode fork pointer */
2682 xfs_mount_t
*mp
; /* mount structure */
2683 xfs_filblks_t nblks
; /* quota/sb block count */
2684 xfs_bmbt_irec_t
new; /* new record to be inserted */
2686 uint qfield
; /* quota field to update */
2687 xfs_filblks_t temp
; /* for indirect length calculations */
2688 xfs_filblks_t temp2
; /* for indirect length calculations */
2691 XFS_STATS_INC(xs_del_exlist
);
2693 if (whichfork
== XFS_ATTR_FORK
)
2694 state
|= BMAP_ATTRFORK
;
2697 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2698 ASSERT((*idx
>= 0) && (*idx
< ifp
->if_bytes
/
2699 (uint
)sizeof(xfs_bmbt_rec_t
)));
2700 ASSERT(del
->br_blockcount
> 0);
2701 ep
= xfs_iext_get_ext(ifp
, *idx
);
2702 xfs_bmbt_get_all(ep
, &got
);
2703 ASSERT(got
.br_startoff
<= del
->br_startoff
);
2704 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
2705 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
2706 ASSERT(got_endoff
>= del_endoff
);
2707 delay
= isnullstartblock(got
.br_startblock
);
2708 ASSERT(isnullstartblock(del
->br_startblock
) == delay
);
2713 * If deleting a real allocation, must free up the disk space.
2716 flags
= XFS_ILOG_CORE
;
2718 * Realtime allocation. Free it and record di_nblocks update.
2720 if (whichfork
== XFS_DATA_FORK
&& XFS_IS_REALTIME_INODE(ip
)) {
2724 ASSERT(do_mod(del
->br_blockcount
,
2725 mp
->m_sb
.sb_rextsize
) == 0);
2726 ASSERT(do_mod(del
->br_startblock
,
2727 mp
->m_sb
.sb_rextsize
) == 0);
2728 bno
= del
->br_startblock
;
2729 len
= del
->br_blockcount
;
2730 do_div(bno
, mp
->m_sb
.sb_rextsize
);
2731 do_div(len
, mp
->m_sb
.sb_rextsize
);
2732 error
= xfs_rtfree_extent(tp
, bno
, (xfs_extlen_t
)len
);
2736 nblks
= len
* mp
->m_sb
.sb_rextsize
;
2737 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
2740 * Ordinary allocation.
2744 nblks
= del
->br_blockcount
;
2745 qfield
= XFS_TRANS_DQ_BCOUNT
;
2748 * Set up del_endblock and cur for later.
2750 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
2752 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
2753 got
.br_startblock
, got
.br_blockcount
,
2756 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2758 da_old
= da_new
= 0;
2760 da_old
= startblockval(got
.br_startblock
);
2766 * Set flag value to use in switch statement.
2767 * Left-contig is 2, right-contig is 1.
2769 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
2770 (got_endoff
== del_endoff
)) {
2773 * Matches the whole extent. Delete the entry.
2775 xfs_iext_remove(ip
, *idx
, 1,
2776 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0);
2781 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2782 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2783 flags
|= XFS_ILOG_CORE
;
2785 flags
|= xfs_ilog_fext(whichfork
);
2788 if ((error
= xfs_btree_delete(cur
, &i
)))
2790 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2795 * Deleting the first part of the extent.
2797 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2798 xfs_bmbt_set_startoff(ep
, del_endoff
);
2799 temp
= got
.br_blockcount
- del
->br_blockcount
;
2800 xfs_bmbt_set_blockcount(ep
, temp
);
2802 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2804 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2805 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2809 xfs_bmbt_set_startblock(ep
, del_endblock
);
2810 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2812 flags
|= xfs_ilog_fext(whichfork
);
2815 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
2816 got
.br_blockcount
- del
->br_blockcount
,
2823 * Deleting the last part of the extent.
2825 temp
= got
.br_blockcount
- del
->br_blockcount
;
2826 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2827 xfs_bmbt_set_blockcount(ep
, temp
);
2829 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2831 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2832 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2836 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2838 flags
|= xfs_ilog_fext(whichfork
);
2841 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
2843 got
.br_blockcount
- del
->br_blockcount
,
2850 * Deleting the middle of the extent.
2852 temp
= del
->br_startoff
- got
.br_startoff
;
2853 trace_xfs_bmap_pre_update(ip
, *idx
, state
, _THIS_IP_
);
2854 xfs_bmbt_set_blockcount(ep
, temp
);
2855 new.br_startoff
= del_endoff
;
2856 temp2
= got_endoff
- del_endoff
;
2857 new.br_blockcount
= temp2
;
2858 new.br_state
= got
.br_state
;
2860 new.br_startblock
= del_endblock
;
2861 flags
|= XFS_ILOG_CORE
;
2863 if ((error
= xfs_bmbt_update(cur
,
2865 got
.br_startblock
, temp
,
2868 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
2870 cur
->bc_rec
.b
= new;
2871 error
= xfs_btree_insert(cur
, &i
);
2872 if (error
&& error
!= ENOSPC
)
2875 * If get no-space back from btree insert,
2876 * it tried a split, and we have a zero
2877 * block reservation.
2878 * Fix up our state and return the error.
2880 if (error
== ENOSPC
) {
2882 * Reset the cursor, don't trust
2883 * it after any insert operation.
2885 if ((error
= xfs_bmbt_lookup_eq(cur
,
2890 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2892 * Update the btree record back
2893 * to the original value.
2895 if ((error
= xfs_bmbt_update(cur
,
2902 * Reset the extent record back
2903 * to the original value.
2905 xfs_bmbt_set_blockcount(ep
,
2908 error
= XFS_ERROR(ENOSPC
);
2911 XFS_WANT_CORRUPTED_GOTO(i
== 1, done
);
2913 flags
|= xfs_ilog_fext(whichfork
);
2914 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2915 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2917 ASSERT(whichfork
== XFS_DATA_FORK
);
2918 temp
= xfs_bmap_worst_indlen(ip
, temp
);
2919 xfs_bmbt_set_startblock(ep
, nullstartblock((int)temp
));
2920 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
2921 new.br_startblock
= nullstartblock((int)temp2
);
2922 da_new
= temp
+ temp2
;
2923 while (da_new
> da_old
) {
2927 xfs_bmbt_set_startblock(ep
,
2928 nullstartblock((int)temp
));
2930 if (da_new
== da_old
)
2936 nullstartblock((int)temp2
);
2940 trace_xfs_bmap_post_update(ip
, *idx
, state
, _THIS_IP_
);
2941 xfs_iext_insert(ip
, *idx
+ 1, 1, &new, state
);
2946 * If we need to, add to list of extents to delete.
2949 xfs_bmap_add_free(del
->br_startblock
, del
->br_blockcount
, flist
,
2952 * Adjust inode # blocks in the file.
2955 ip
->i_d
.di_nblocks
-= nblks
;
2957 * Adjust quota data.
2960 xfs_trans_mod_dquot_byino(tp
, ip
, qfield
, (long)-nblks
);
2963 * Account for change in delayed indirect blocks.
2964 * Nothing to do for disk quota accounting here.
2966 ASSERT(da_old
>= da_new
);
2967 if (da_old
> da_new
) {
2968 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
2969 (int64_t)(da_old
- da_new
), 0);
2977 * Remove the entry "free" from the free item list. Prev points to the
2978 * previous entry, unless "free" is the head of the list.
2982 xfs_bmap_free_t
*flist
, /* free item list header */
2983 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
2984 xfs_bmap_free_item_t
*free
) /* list item to be freed */
2987 prev
->xbfi_next
= free
->xbfi_next
;
2989 flist
->xbf_first
= free
->xbfi_next
;
2991 kmem_zone_free(xfs_bmap_free_item_zone
, free
);
2995 * Convert an extents-format file into a btree-format file.
2996 * The new file will have a root block (in the inode) and a single child block.
2998 STATIC
int /* error */
2999 xfs_bmap_extents_to_btree(
3000 xfs_trans_t
*tp
, /* transaction pointer */
3001 xfs_inode_t
*ip
, /* incore inode pointer */
3002 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
3003 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
3004 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
3005 int wasdel
, /* converting a delayed alloc */
3006 int *logflagsp
, /* inode logging flags */
3007 int whichfork
) /* data or attr fork */
3009 struct xfs_btree_block
*ablock
; /* allocated (child) bt block */
3010 xfs_buf_t
*abp
; /* buffer for ablock */
3011 xfs_alloc_arg_t args
; /* allocation arguments */
3012 xfs_bmbt_rec_t
*arp
; /* child record pointer */
3013 struct xfs_btree_block
*block
; /* btree root block */
3014 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
3015 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
3016 int error
; /* error return value */
3017 xfs_extnum_t i
, cnt
; /* extent record index */
3018 xfs_ifork_t
*ifp
; /* inode fork pointer */
3019 xfs_bmbt_key_t
*kp
; /* root block key pointer */
3020 xfs_mount_t
*mp
; /* mount structure */
3021 xfs_extnum_t nextents
; /* number of file extents */
3022 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
3024 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3025 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
3026 ASSERT(ifp
->if_ext_max
==
3027 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
3029 * Make space in the inode incore.
3031 xfs_iroot_realloc(ip
, 1, whichfork
);
3032 ifp
->if_flags
|= XFS_IFBROOT
;
3037 block
= ifp
->if_broot
;
3038 block
->bb_magic
= cpu_to_be32(XFS_BMAP_MAGIC
);
3039 block
->bb_level
= cpu_to_be16(1);
3040 block
->bb_numrecs
= cpu_to_be16(1);
3041 block
->bb_u
.l
.bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
3042 block
->bb_u
.l
.bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
3045 * Need a cursor. Can't allocate until bb_level is filled in.
3048 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
3049 cur
->bc_private
.b
.firstblock
= *firstblock
;
3050 cur
->bc_private
.b
.flist
= flist
;
3051 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
3053 * Convert to a btree with two levels, one record in root.
3055 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
3058 args
.firstblock
= *firstblock
;
3059 if (*firstblock
== NULLFSBLOCK
) {
3060 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3061 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
3062 } else if (flist
->xbf_low
) {
3063 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3064 args
.fsbno
= *firstblock
;
3066 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3067 args
.fsbno
= *firstblock
;
3069 args
.minlen
= args
.maxlen
= args
.prod
= 1;
3070 args
.total
= args
.minleft
= args
.alignment
= args
.mod
= args
.isfl
=
3071 args
.minalignslop
= 0;
3072 args
.wasdel
= wasdel
;
3074 if ((error
= xfs_alloc_vextent(&args
))) {
3075 xfs_iroot_realloc(ip
, -1, whichfork
);
3076 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
3080 * Allocation can't fail, the space was reserved.
3082 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
3083 ASSERT(*firstblock
== NULLFSBLOCK
||
3084 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
3086 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
3087 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
3088 cur
->bc_private
.b
.allocated
++;
3089 ip
->i_d
.di_nblocks
++;
3090 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
3091 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
3093 * Fill in the child block.
3095 ablock
= XFS_BUF_TO_BLOCK(abp
);
3096 ablock
->bb_magic
= cpu_to_be32(XFS_BMAP_MAGIC
);
3097 ablock
->bb_level
= 0;
3098 ablock
->bb_u
.l
.bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
3099 ablock
->bb_u
.l
.bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
3100 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
3101 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
3102 for (cnt
= i
= 0; i
< nextents
; i
++) {
3103 ep
= xfs_iext_get_ext(ifp
, i
);
3104 if (!isnullstartblock(xfs_bmbt_get_startblock(ep
))) {
3105 arp
->l0
= cpu_to_be64(ep
->l0
);
3106 arp
->l1
= cpu_to_be64(ep
->l1
);
3110 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
3111 xfs_btree_set_numrecs(ablock
, cnt
);
3114 * Fill in the root key and pointer.
3116 kp
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
3117 arp
= XFS_BMBT_REC_ADDR(mp
, ablock
, 1);
3118 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
3119 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, xfs_bmbt_get_maxrecs(cur
,
3120 be16_to_cpu(block
->bb_level
)));
3121 *pp
= cpu_to_be64(args
.fsbno
);
3124 * Do all this logging at the end so that
3125 * the root is at the right level.
3127 xfs_btree_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
3128 xfs_btree_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
3129 ASSERT(*curp
== NULL
);
3131 *logflagsp
= XFS_ILOG_CORE
| xfs_ilog_fbroot(whichfork
);
3136 * Calculate the default attribute fork offset for newly created inodes.
3139 xfs_default_attroffset(
3140 struct xfs_inode
*ip
)
3142 struct xfs_mount
*mp
= ip
->i_mount
;
3145 if (mp
->m_sb
.sb_inodesize
== 256) {
3146 offset
= XFS_LITINO(mp
) -
3147 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
3149 offset
= XFS_BMDR_SPACE_CALC(6 * MINABTPTRS
);
3152 ASSERT(offset
< XFS_LITINO(mp
));
3157 * Helper routine to reset inode di_forkoff field when switching
3158 * attribute fork from local to extent format - we reset it where
3159 * possible to make space available for inline data fork extents.
3162 xfs_bmap_forkoff_reset(
3167 if (whichfork
== XFS_ATTR_FORK
&&
3168 ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
&&
3169 ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
&&
3170 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) {
3171 uint dfl_forkoff
= xfs_default_attroffset(ip
) >> 3;
3173 if (dfl_forkoff
> ip
->i_d
.di_forkoff
) {
3174 ip
->i_d
.di_forkoff
= dfl_forkoff
;
3175 ip
->i_df
.if_ext_max
=
3176 XFS_IFORK_DSIZE(ip
) / sizeof(xfs_bmbt_rec_t
);
3177 ip
->i_afp
->if_ext_max
=
3178 XFS_IFORK_ASIZE(ip
) / sizeof(xfs_bmbt_rec_t
);
3184 * Convert a local file to an extents file.
3185 * This code is out of bounds for data forks of regular files,
3186 * since the file data needs to get logged so things will stay consistent.
3187 * (The bmap-level manipulations are ok, though).
3189 STATIC
int /* error */
3190 xfs_bmap_local_to_extents(
3191 xfs_trans_t
*tp
, /* transaction pointer */
3192 xfs_inode_t
*ip
, /* incore inode pointer */
3193 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
3194 xfs_extlen_t total
, /* total blocks needed by transaction */
3195 int *logflagsp
, /* inode logging flags */
3196 int whichfork
) /* data or attr fork */
3198 int error
; /* error return value */
3199 int flags
; /* logging flags returned */
3200 xfs_ifork_t
*ifp
; /* inode fork pointer */
3203 * We don't want to deal with the case of keeping inode data inline yet.
3204 * So sending the data fork of a regular inode is invalid.
3206 ASSERT(!(S_ISREG(ip
->i_d
.di_mode
) && whichfork
== XFS_DATA_FORK
));
3207 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3208 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
3211 if (ifp
->if_bytes
) {
3212 xfs_alloc_arg_t args
; /* allocation arguments */
3213 xfs_buf_t
*bp
; /* buffer for extent block */
3214 xfs_bmbt_rec_host_t
*ep
;/* extent record pointer */
3217 args
.mp
= ip
->i_mount
;
3218 args
.firstblock
= *firstblock
;
3219 ASSERT((ifp
->if_flags
&
3220 (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) == XFS_IFINLINE
);
3222 * Allocate a block. We know we need only one, since the
3223 * file currently fits in an inode.
3225 if (*firstblock
== NULLFSBLOCK
) {
3226 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
3227 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3229 args
.fsbno
= *firstblock
;
3230 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3233 args
.mod
= args
.minleft
= args
.alignment
= args
.wasdel
=
3234 args
.isfl
= args
.minalignslop
= 0;
3235 args
.minlen
= args
.maxlen
= args
.prod
= 1;
3236 if ((error
= xfs_alloc_vextent(&args
)))
3239 * Can't fail, the space was reserved.
3241 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
3242 ASSERT(args
.len
== 1);
3243 *firstblock
= args
.fsbno
;
3244 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
3245 memcpy(bp
->b_addr
, ifp
->if_u1
.if_data
, ifp
->if_bytes
);
3246 xfs_trans_log_buf(tp
, bp
, 0, ifp
->if_bytes
- 1);
3247 xfs_bmap_forkoff_reset(args
.mp
, ip
, whichfork
);
3248 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
3249 xfs_iext_add(ifp
, 0, 1);
3250 ep
= xfs_iext_get_ext(ifp
, 0);
3251 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
3252 trace_xfs_bmap_post_update(ip
, 0,
3253 whichfork
== XFS_ATTR_FORK
? BMAP_ATTRFORK
: 0,
3255 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
3256 ip
->i_d
.di_nblocks
= 1;
3257 xfs_trans_mod_dquot_byino(tp
, ip
,
3258 XFS_TRANS_DQ_BCOUNT
, 1L);
3259 flags
|= xfs_ilog_fext(whichfork
);
3261 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
3262 xfs_bmap_forkoff_reset(ip
->i_mount
, ip
, whichfork
);
3264 ifp
->if_flags
&= ~XFS_IFINLINE
;
3265 ifp
->if_flags
|= XFS_IFEXTENTS
;
3266 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
3267 flags
|= XFS_ILOG_CORE
;
3274 * Search the extent records for the entry containing block bno.
3275 * If bno lies in a hole, point to the next entry. If bno lies
3276 * past eof, *eofp will be set, and *prevp will contain the last
3277 * entry (null if none). Else, *lastxp will be set to the index
3278 * of the found entry; *gotp will contain the entry.
3280 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
3281 xfs_bmap_search_multi_extents(
3282 xfs_ifork_t
*ifp
, /* inode fork pointer */
3283 xfs_fileoff_t bno
, /* block number searched for */
3284 int *eofp
, /* out: end of file found */
3285 xfs_extnum_t
*lastxp
, /* out: last extent index */
3286 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
3287 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
3289 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
3290 xfs_extnum_t lastx
; /* last extent index */
3293 * Initialize the extent entry structure to catch access to
3294 * uninitialized br_startblock field.
3296 gotp
->br_startoff
= 0xffa5a5a5a5a5a5a5LL
;
3297 gotp
->br_blockcount
= 0xa55a5a5a5a5a5a5aLL
;
3298 gotp
->br_state
= XFS_EXT_INVALID
;
3300 gotp
->br_startblock
= 0xffffa5a5a5a5a5a5LL
;
3302 gotp
->br_startblock
= 0xffffa5a5;
3304 prevp
->br_startoff
= NULLFILEOFF
;
3306 ep
= xfs_iext_bno_to_ext(ifp
, bno
, &lastx
);
3308 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
- 1), prevp
);
3310 if (lastx
< (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
3311 xfs_bmbt_get_all(ep
, gotp
);
3325 * Search the extents list for the inode, for the extent containing bno.
3326 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3327 * *eofp will be set, and *prevp will contain the last entry (null if none).
3328 * Else, *lastxp will be set to the index of the found
3329 * entry; *gotp will contain the entry.
3331 STATIC xfs_bmbt_rec_host_t
* /* pointer to found extent entry */
3332 xfs_bmap_search_extents(
3333 xfs_inode_t
*ip
, /* incore inode pointer */
3334 xfs_fileoff_t bno
, /* block number searched for */
3335 int fork
, /* data or attr fork */
3336 int *eofp
, /* out: end of file found */
3337 xfs_extnum_t
*lastxp
, /* out: last extent index */
3338 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
3339 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
3341 xfs_ifork_t
*ifp
; /* inode fork pointer */
3342 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
3344 XFS_STATS_INC(xs_look_exlist
);
3345 ifp
= XFS_IFORK_PTR(ip
, fork
);
3347 ep
= xfs_bmap_search_multi_extents(ifp
, bno
, eofp
, lastxp
, gotp
, prevp
);
3349 if (unlikely(!(gotp
->br_startblock
) && (*lastxp
!= NULLEXTNUM
) &&
3350 !(XFS_IS_REALTIME_INODE(ip
) && fork
== XFS_DATA_FORK
))) {
3351 xfs_alert_tag(ip
->i_mount
, XFS_PTAG_FSBLOCK_ZERO
,
3352 "Access to block zero in inode %llu "
3353 "start_block: %llx start_off: %llx "
3354 "blkcnt: %llx extent-state: %x lastx: %x\n",
3355 (unsigned long long)ip
->i_ino
,
3356 (unsigned long long)gotp
->br_startblock
,
3357 (unsigned long long)gotp
->br_startoff
,
3358 (unsigned long long)gotp
->br_blockcount
,
3359 gotp
->br_state
, *lastxp
);
3360 *lastxp
= NULLEXTNUM
;
3368 * Compute the worst-case number of indirect blocks that will be used
3369 * for ip's delayed extent of length "len".
3371 STATIC xfs_filblks_t
3372 xfs_bmap_worst_indlen(
3373 xfs_inode_t
*ip
, /* incore inode pointer */
3374 xfs_filblks_t len
) /* delayed extent length */
3376 int level
; /* btree level number */
3377 int maxrecs
; /* maximum record count at this level */
3378 xfs_mount_t
*mp
; /* mount structure */
3379 xfs_filblks_t rval
; /* return value */
3382 maxrecs
= mp
->m_bmap_dmxr
[0];
3383 for (level
= 0, rval
= 0;
3384 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
3387 do_div(len
, maxrecs
);
3390 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
3393 maxrecs
= mp
->m_bmap_dmxr
[1];
3399 * Convert inode from non-attributed to attributed.
3400 * Must not be in a transaction, ip must not be locked.
3402 int /* error code */
3403 xfs_bmap_add_attrfork(
3404 xfs_inode_t
*ip
, /* incore inode pointer */
3405 int size
, /* space new attribute needs */
3406 int rsvd
) /* xact may use reserved blks */
3408 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
3409 xfs_bmap_free_t flist
; /* freed extent records */
3410 xfs_mount_t
*mp
; /* mount structure */
3411 xfs_trans_t
*tp
; /* transaction pointer */
3412 int blks
; /* space reservation */
3413 int version
= 1; /* superblock attr version */
3414 int committed
; /* xaction was committed */
3415 int logflags
; /* logging flags */
3416 int error
; /* error return value */
3418 ASSERT(XFS_IFORK_Q(ip
) == 0);
3419 ASSERT(ip
->i_df
.if_ext_max
==
3420 XFS_IFORK_DSIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
));
3423 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
3424 tp
= xfs_trans_alloc(mp
, XFS_TRANS_ADDAFORK
);
3425 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
3427 tp
->t_flags
|= XFS_TRANS_RESERVE
;
3428 if ((error
= xfs_trans_reserve(tp
, blks
, XFS_ADDAFORK_LOG_RES(mp
), 0,
3429 XFS_TRANS_PERM_LOG_RES
, XFS_ADDAFORK_LOG_COUNT
)))
3431 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3432 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, blks
, 0, rsvd
?
3433 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
3434 XFS_QMOPT_RES_REGBLKS
);
3436 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3437 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
);
3440 if (XFS_IFORK_Q(ip
))
3442 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
3444 * For inodes coming from pre-6.2 filesystems.
3446 ASSERT(ip
->i_d
.di_aformat
== 0);
3447 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
3449 ASSERT(ip
->i_d
.di_anextents
== 0);
3451 xfs_trans_ijoin_ref(tp
, ip
, XFS_ILOCK_EXCL
);
3452 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
3454 switch (ip
->i_d
.di_format
) {
3455 case XFS_DINODE_FMT_DEV
:
3456 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
3458 case XFS_DINODE_FMT_UUID
:
3459 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
3461 case XFS_DINODE_FMT_LOCAL
:
3462 case XFS_DINODE_FMT_EXTENTS
:
3463 case XFS_DINODE_FMT_BTREE
:
3464 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
3465 if (!ip
->i_d
.di_forkoff
)
3466 ip
->i_d
.di_forkoff
= xfs_default_attroffset(ip
) >> 3;
3467 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
3472 error
= XFS_ERROR(EINVAL
);
3475 ip
->i_df
.if_ext_max
=
3476 XFS_IFORK_DSIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
);
3477 ASSERT(ip
->i_afp
== NULL
);
3478 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
3479 ip
->i_afp
->if_ext_max
=
3480 XFS_IFORK_ASIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
);
3481 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
3483 xfs_bmap_init(&flist
, &firstblock
);
3484 switch (ip
->i_d
.di_format
) {
3485 case XFS_DINODE_FMT_LOCAL
:
3486 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &flist
,
3489 case XFS_DINODE_FMT_EXTENTS
:
3490 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
3493 case XFS_DINODE_FMT_BTREE
:
3494 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &flist
,
3502 xfs_trans_log_inode(tp
, ip
, logflags
);
3505 if (!xfs_sb_version_hasattr(&mp
->m_sb
) ||
3506 (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2)) {
3507 __int64_t sbfields
= 0;
3509 spin_lock(&mp
->m_sb_lock
);
3510 if (!xfs_sb_version_hasattr(&mp
->m_sb
)) {
3511 xfs_sb_version_addattr(&mp
->m_sb
);
3512 sbfields
|= XFS_SB_VERSIONNUM
;
3514 if (!xfs_sb_version_hasattr2(&mp
->m_sb
) && version
== 2) {
3515 xfs_sb_version_addattr2(&mp
->m_sb
);
3516 sbfields
|= (XFS_SB_VERSIONNUM
| XFS_SB_FEATURES2
);
3519 spin_unlock(&mp
->m_sb_lock
);
3520 xfs_mod_sb(tp
, sbfields
);
3522 spin_unlock(&mp
->m_sb_lock
);
3524 if ((error
= xfs_bmap_finish(&tp
, &flist
, &committed
)))
3526 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
3527 ASSERT(ip
->i_df
.if_ext_max
==
3528 XFS_IFORK_DSIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
));
3531 xfs_bmap_cancel(&flist
);
3533 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3535 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
3536 ASSERT(ip
->i_df
.if_ext_max
==
3537 XFS_IFORK_DSIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
));
3542 * Add the extent to the list of extents to be free at transaction end.
3543 * The list is maintained sorted (by block number).
3548 xfs_fsblock_t bno
, /* fs block number of extent */
3549 xfs_filblks_t len
, /* length of extent */
3550 xfs_bmap_free_t
*flist
, /* list of extents */
3551 xfs_mount_t
*mp
) /* mount point structure */
3553 xfs_bmap_free_item_t
*cur
; /* current (next) element */
3554 xfs_bmap_free_item_t
*new; /* new element */
3555 xfs_bmap_free_item_t
*prev
; /* previous element */
3557 xfs_agnumber_t agno
;
3558 xfs_agblock_t agbno
;
3560 ASSERT(bno
!= NULLFSBLOCK
);
3562 ASSERT(len
<= MAXEXTLEN
);
3563 ASSERT(!isnullstartblock(bno
));
3564 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
3565 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
3566 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
3567 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
3568 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
3569 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
3571 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
3572 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
3573 new->xbfi_startblock
= bno
;
3574 new->xbfi_blockcount
= (xfs_extlen_t
)len
;
3575 for (prev
= NULL
, cur
= flist
->xbf_first
;
3577 prev
= cur
, cur
= cur
->xbfi_next
) {
3578 if (cur
->xbfi_startblock
>= bno
)
3582 prev
->xbfi_next
= new;
3584 flist
->xbf_first
= new;
3585 new->xbfi_next
= cur
;
3590 * Compute and fill in the value of the maximum depth of a bmap btree
3591 * in this filesystem. Done once, during mount.
3594 xfs_bmap_compute_maxlevels(
3595 xfs_mount_t
*mp
, /* file system mount structure */
3596 int whichfork
) /* data or attr fork */
3598 int level
; /* btree level */
3599 uint maxblocks
; /* max blocks at this level */
3600 uint maxleafents
; /* max leaf entries possible */
3601 int maxrootrecs
; /* max records in root block */
3602 int minleafrecs
; /* min records in leaf block */
3603 int minnoderecs
; /* min records in node block */
3604 int sz
; /* root block size */
3607 * The maximum number of extents in a file, hence the maximum
3608 * number of leaf entries, is controlled by the type of di_nextents
3609 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3610 * (a signed 16-bit number, xfs_aextnum_t).
3612 * Note that we can no longer assume that if we are in ATTR1 that
3613 * the fork offset of all the inodes will be
3614 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3615 * with ATTR2 and then mounted back with ATTR1, keeping the
3616 * di_forkoff's fixed but probably at various positions. Therefore,
3617 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3618 * of a minimum size available.
3620 if (whichfork
== XFS_DATA_FORK
) {
3621 maxleafents
= MAXEXTNUM
;
3622 sz
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
3624 maxleafents
= MAXAEXTNUM
;
3625 sz
= XFS_BMDR_SPACE_CALC(MINABTPTRS
);
3627 maxrootrecs
= xfs_bmdr_maxrecs(mp
, sz
, 0);
3628 minleafrecs
= mp
->m_bmap_dmnr
[0];
3629 minnoderecs
= mp
->m_bmap_dmnr
[1];
3630 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
3631 for (level
= 1; maxblocks
> 1; level
++) {
3632 if (maxblocks
<= maxrootrecs
)
3635 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
3637 mp
->m_bm_maxlevels
[whichfork
] = level
;
3641 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3642 * caller. Frees all the extents that need freeing, which must be done
3643 * last due to locking considerations. We never free any extents in
3644 * the first transaction.
3646 * Return 1 if the given transaction was committed and a new one
3647 * started, and 0 otherwise in the committed parameter.
3651 xfs_trans_t
**tp
, /* transaction pointer addr */
3652 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
3653 int *committed
) /* xact committed or not */
3655 xfs_efd_log_item_t
*efd
; /* extent free data */
3656 xfs_efi_log_item_t
*efi
; /* extent free intention */
3657 int error
; /* error return value */
3658 xfs_bmap_free_item_t
*free
; /* free extent item */
3659 unsigned int logres
; /* new log reservation */
3660 unsigned int logcount
; /* new log count */
3661 xfs_mount_t
*mp
; /* filesystem mount structure */
3662 xfs_bmap_free_item_t
*next
; /* next item on free list */
3663 xfs_trans_t
*ntp
; /* new transaction pointer */
3665 ASSERT((*tp
)->t_flags
& XFS_TRANS_PERM_LOG_RES
);
3666 if (flist
->xbf_count
== 0) {
3671 efi
= xfs_trans_get_efi(ntp
, flist
->xbf_count
);
3672 for (free
= flist
->xbf_first
; free
; free
= free
->xbfi_next
)
3673 xfs_trans_log_efi_extent(ntp
, efi
, free
->xbfi_startblock
,
3674 free
->xbfi_blockcount
);
3675 logres
= ntp
->t_log_res
;
3676 logcount
= ntp
->t_log_count
;
3677 ntp
= xfs_trans_dup(*tp
);
3678 error
= xfs_trans_commit(*tp
, 0);
3682 * We have a new transaction, so we should return committed=1,
3683 * even though we're returning an error.
3689 * transaction commit worked ok so we can drop the extra ticket
3690 * reference that we gained in xfs_trans_dup()
3692 xfs_log_ticket_put(ntp
->t_ticket
);
3694 if ((error
= xfs_trans_reserve(ntp
, 0, logres
, 0, XFS_TRANS_PERM_LOG_RES
,
3697 efd
= xfs_trans_get_efd(ntp
, efi
, flist
->xbf_count
);
3698 for (free
= flist
->xbf_first
; free
!= NULL
; free
= next
) {
3699 next
= free
->xbfi_next
;
3700 if ((error
= xfs_free_extent(ntp
, free
->xbfi_startblock
,
3701 free
->xbfi_blockcount
))) {
3703 * The bmap free list will be cleaned up at a
3704 * higher level. The EFI will be canceled when
3705 * this transaction is aborted.
3706 * Need to force shutdown here to make sure it
3707 * happens, since this transaction may not be
3711 if (!XFS_FORCED_SHUTDOWN(mp
))
3712 xfs_force_shutdown(mp
,
3713 (error
== EFSCORRUPTED
) ?
3714 SHUTDOWN_CORRUPT_INCORE
:
3715 SHUTDOWN_META_IO_ERROR
);
3718 xfs_trans_log_efd_extent(ntp
, efd
, free
->xbfi_startblock
,
3719 free
->xbfi_blockcount
);
3720 xfs_bmap_del_free(flist
, NULL
, free
);
3726 * Free up any items left in the list.
3730 xfs_bmap_free_t
*flist
) /* list of bmap_free_items */
3732 xfs_bmap_free_item_t
*free
; /* free list item */
3733 xfs_bmap_free_item_t
*next
;
3735 if (flist
->xbf_count
== 0)
3737 ASSERT(flist
->xbf_first
!= NULL
);
3738 for (free
= flist
->xbf_first
; free
; free
= next
) {
3739 next
= free
->xbfi_next
;
3740 xfs_bmap_del_free(flist
, NULL
, free
);
3742 ASSERT(flist
->xbf_count
== 0);
3746 * Returns the file-relative block number of the first unused block(s)
3747 * in the file with at least "len" logically contiguous blocks free.
3748 * This is the lowest-address hole if the file has holes, else the first block
3749 * past the end of file.
3750 * Return 0 if the file is currently local (in-inode).
3753 xfs_bmap_first_unused(
3754 xfs_trans_t
*tp
, /* transaction pointer */
3755 xfs_inode_t
*ip
, /* incore inode */
3756 xfs_extlen_t len
, /* size of hole to find */
3757 xfs_fileoff_t
*first_unused
, /* unused block */
3758 int whichfork
) /* data or attr fork */
3760 int error
; /* error return value */
3761 int idx
; /* extent record index */
3762 xfs_ifork_t
*ifp
; /* inode fork pointer */
3763 xfs_fileoff_t lastaddr
; /* last block number seen */
3764 xfs_fileoff_t lowest
; /* lowest useful block */
3765 xfs_fileoff_t max
; /* starting useful block */
3766 xfs_fileoff_t off
; /* offset for this block */
3767 xfs_extnum_t nextents
; /* number of extent entries */
3769 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
3770 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
3771 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
3772 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
3776 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3777 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
3778 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
3780 lowest
= *first_unused
;
3781 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
3782 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
3783 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, idx
);
3784 off
= xfs_bmbt_get_startoff(ep
);
3786 * See if the hole before this extent will work.
3788 if (off
>= lowest
+ len
&& off
- max
>= len
) {
3789 *first_unused
= max
;
3792 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
3793 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
3795 *first_unused
= max
;
3800 * Returns the file-relative block number of the last block + 1 before
3801 * last_block (input value) in the file.
3802 * This is not based on i_size, it is based on the extent records.
3803 * Returns 0 for local files, as they do not have extent records.
3806 xfs_bmap_last_before(
3807 xfs_trans_t
*tp
, /* transaction pointer */
3808 xfs_inode_t
*ip
, /* incore inode */
3809 xfs_fileoff_t
*last_block
, /* last block */
3810 int whichfork
) /* data or attr fork */
3812 xfs_fileoff_t bno
; /* input file offset */
3813 int eof
; /* hit end of file */
3814 xfs_bmbt_rec_host_t
*ep
; /* pointer to last extent */
3815 int error
; /* error return value */
3816 xfs_bmbt_irec_t got
; /* current extent value */
3817 xfs_ifork_t
*ifp
; /* inode fork pointer */
3818 xfs_extnum_t lastx
; /* last extent used */
3819 xfs_bmbt_irec_t prev
; /* previous extent value */
3821 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
3822 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3823 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
3824 return XFS_ERROR(EIO
);
3825 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
3829 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3830 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
3831 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
3833 bno
= *last_block
- 1;
3834 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
3836 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
3837 if (prev
.br_startoff
== NULLFILEOFF
)
3840 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
3843 * Otherwise *last_block is already the right answer.
3849 xfs_bmap_last_extent(
3850 struct xfs_trans
*tp
,
3851 struct xfs_inode
*ip
,
3853 struct xfs_bmbt_irec
*rec
,
3856 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3860 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
3861 error
= xfs_iread_extents(tp
, ip
, whichfork
);
3866 nextents
= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
);
3867 if (nextents
== 0) {
3872 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, nextents
- 1), rec
);
3878 * Check the last inode extent to determine whether this allocation will result
3879 * in blocks being allocated at the end of the file. When we allocate new data
3880 * blocks at the end of the file which do not start at the previous data block,
3881 * we will try to align the new blocks at stripe unit boundaries.
3883 * Returns 0 in bma->aeof if the file (fork) is empty as any new write will be
3884 * at, or past the EOF.
3888 struct xfs_bmalloca
*bma
,
3891 struct xfs_bmbt_irec rec
;
3896 error
= xfs_bmap_last_extent(NULL
, bma
->ip
, whichfork
, &rec
,
3898 if (error
|| is_empty
)
3902 * Check if we are allocation or past the last extent, or at least into
3903 * the last delayed allocated extent.
3905 bma
->aeof
= bma
->off
>= rec
.br_startoff
+ rec
.br_blockcount
||
3906 (bma
->off
>= rec
.br_startoff
&&
3907 isnullstartblock(rec
.br_startblock
));
3912 * Check if the endoff is outside the last extent. If so the caller will grow
3913 * the allocation to a stripe unit boundary. All offsets are considered outside
3914 * the end of file for an empty fork, so 1 is returned in *eof in that case.
3918 struct xfs_inode
*ip
,
3919 xfs_fileoff_t endoff
,
3923 struct xfs_bmbt_irec rec
;
3926 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, eof
);
3930 *eof
= endoff
>= rec
.br_startoff
+ rec
.br_blockcount
;
3935 * Returns the file-relative block number of the first block past eof in
3936 * the file. This is not based on i_size, it is based on the extent records.
3937 * Returns 0 for local files, as they do not have extent records.
3940 xfs_bmap_last_offset(
3941 struct xfs_trans
*tp
,
3942 struct xfs_inode
*ip
,
3943 xfs_fileoff_t
*last_block
,
3946 struct xfs_bmbt_irec rec
;
3952 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
)
3955 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
3956 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
3957 return XFS_ERROR(EIO
);
3959 error
= xfs_bmap_last_extent(NULL
, ip
, whichfork
, &rec
, &is_empty
);
3960 if (error
|| is_empty
)
3963 *last_block
= rec
.br_startoff
+ rec
.br_blockcount
;
3968 * Returns whether the selected fork of the inode has exactly one
3969 * block or not. For the data fork we check this matches di_size,
3970 * implying the file's range is 0..bsize-1.
3972 int /* 1=>1 block, 0=>otherwise */
3974 xfs_inode_t
*ip
, /* incore inode */
3975 int whichfork
) /* data or attr fork */
3977 xfs_bmbt_rec_host_t
*ep
; /* ptr to fork's extent */
3978 xfs_ifork_t
*ifp
; /* inode fork pointer */
3979 int rval
; /* return value */
3980 xfs_bmbt_irec_t s
; /* internal version of extent */
3983 if (whichfork
== XFS_DATA_FORK
) {
3984 return S_ISREG(ip
->i_d
.di_mode
) ?
3985 (ip
->i_size
== ip
->i_mount
->m_sb
.sb_blocksize
) :
3986 (ip
->i_d
.di_size
== ip
->i_mount
->m_sb
.sb_blocksize
);
3989 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
3991 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
3993 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3994 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
3995 ep
= xfs_iext_get_ext(ifp
, 0);
3996 xfs_bmbt_get_all(ep
, &s
);
3997 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
3998 if (rval
&& whichfork
== XFS_DATA_FORK
)
3999 ASSERT(ip
->i_size
== ip
->i_mount
->m_sb
.sb_blocksize
);
4004 xfs_bmap_sanity_check(
4005 struct xfs_mount
*mp
,
4009 struct xfs_btree_block
*block
= XFS_BUF_TO_BLOCK(bp
);
4011 if (block
->bb_magic
!= cpu_to_be32(XFS_BMAP_MAGIC
) ||
4012 be16_to_cpu(block
->bb_level
) != level
||
4013 be16_to_cpu(block
->bb_numrecs
) == 0 ||
4014 be16_to_cpu(block
->bb_numrecs
) > mp
->m_bmap_dmxr
[level
!= 0])
4020 * Read in the extents to if_extents.
4021 * All inode fields are set up by caller, we just traverse the btree
4022 * and copy the records in. If the file system cannot contain unwritten
4023 * extents, the records are checked for no "state" flags.
4026 xfs_bmap_read_extents(
4027 xfs_trans_t
*tp
, /* transaction pointer */
4028 xfs_inode_t
*ip
, /* incore inode */
4029 int whichfork
) /* data or attr fork */
4031 struct xfs_btree_block
*block
; /* current btree block */
4032 xfs_fsblock_t bno
; /* block # of "block" */
4033 xfs_buf_t
*bp
; /* buffer for "block" */
4034 int error
; /* error return value */
4035 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
4036 xfs_extnum_t i
, j
; /* index into the extents list */
4037 xfs_ifork_t
*ifp
; /* fork structure */
4038 int level
; /* btree level, for checking */
4039 xfs_mount_t
*mp
; /* file system mount structure */
4040 __be64
*pp
; /* pointer to block address */
4042 xfs_extnum_t room
; /* number of entries there's room for */
4046 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4047 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
4048 XFS_EXTFMT_INODE(ip
);
4049 block
= ifp
->if_broot
;
4051 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4053 level
= be16_to_cpu(block
->bb_level
);
4055 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
4056 bno
= be64_to_cpu(*pp
);
4057 ASSERT(bno
!= NULLDFSBNO
);
4058 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
4059 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
4061 * Go down the tree until leaf level is reached, following the first
4062 * pointer (leftmost) at each level.
4064 while (level
-- > 0) {
4065 if ((error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
4066 XFS_BMAP_BTREE_REF
)))
4068 block
= XFS_BUF_TO_BLOCK(bp
);
4069 XFS_WANT_CORRUPTED_GOTO(
4070 xfs_bmap_sanity_check(mp
, bp
, level
),
4074 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
4075 bno
= be64_to_cpu(*pp
);
4076 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
4077 xfs_trans_brelse(tp
, bp
);
4080 * Here with bp and block set to the leftmost leaf node in the tree.
4082 room
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
4085 * Loop over all leaf nodes. Copy information to the extent records.
4088 xfs_bmbt_rec_t
*frp
;
4089 xfs_fsblock_t nextbno
;
4090 xfs_extnum_t num_recs
;
4093 num_recs
= xfs_btree_get_numrecs(block
);
4094 if (unlikely(i
+ num_recs
> room
)) {
4095 ASSERT(i
+ num_recs
<= room
);
4096 xfs_warn(ip
->i_mount
,
4097 "corrupt dinode %Lu, (btree extents).",
4098 (unsigned long long) ip
->i_ino
);
4099 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
4100 XFS_ERRLEVEL_LOW
, ip
->i_mount
, block
);
4103 XFS_WANT_CORRUPTED_GOTO(
4104 xfs_bmap_sanity_check(mp
, bp
, 0),
4107 * Read-ahead the next leaf block, if any.
4109 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
4110 if (nextbno
!= NULLFSBLOCK
)
4111 xfs_btree_reada_bufl(mp
, nextbno
, 1);
4113 * Copy records into the extent records.
4115 frp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
4117 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
4118 xfs_bmbt_rec_host_t
*trp
= xfs_iext_get_ext(ifp
, i
);
4119 trp
->l0
= be64_to_cpu(frp
->l0
);
4120 trp
->l1
= be64_to_cpu(frp
->l1
);
4122 if (exntf
== XFS_EXTFMT_NOSTATE
) {
4124 * Check all attribute bmap btree records and
4125 * any "older" data bmap btree records for a
4126 * set bit in the "extent flag" position.
4128 if (unlikely(xfs_check_nostate_extents(ifp
,
4129 start
, num_recs
))) {
4130 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4136 xfs_trans_brelse(tp
, bp
);
4139 * If we've reached the end, stop.
4141 if (bno
== NULLFSBLOCK
)
4143 if ((error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
4144 XFS_BMAP_BTREE_REF
)))
4146 block
= XFS_BUF_TO_BLOCK(bp
);
4148 ASSERT(i
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
4149 ASSERT(i
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
4150 XFS_BMAP_TRACE_EXLIST(ip
, i
, whichfork
);
4153 xfs_trans_brelse(tp
, bp
);
4154 return XFS_ERROR(EFSCORRUPTED
);
4159 * Add bmap trace insert entries for all the contents of the extent records.
4162 xfs_bmap_trace_exlist(
4163 xfs_inode_t
*ip
, /* incore inode pointer */
4164 xfs_extnum_t cnt
, /* count of entries in the list */
4165 int whichfork
, /* data or attr fork */
4166 unsigned long caller_ip
)
4168 xfs_extnum_t idx
; /* extent record index */
4169 xfs_ifork_t
*ifp
; /* inode fork pointer */
4172 if (whichfork
== XFS_ATTR_FORK
)
4173 state
|= BMAP_ATTRFORK
;
4175 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4176 ASSERT(cnt
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
4177 for (idx
= 0; idx
< cnt
; idx
++)
4178 trace_xfs_extlist(ip
, idx
, whichfork
, caller_ip
);
4182 * Validate that the bmbt_irecs being returned from bmapi are valid
4183 * given the callers original parameters. Specifically check the
4184 * ranges of the returned irecs to ensure that they only extent beyond
4185 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4188 xfs_bmap_validate_ret(
4192 xfs_bmbt_irec_t
*mval
,
4196 int i
; /* index to map values */
4198 ASSERT(ret_nmap
<= nmap
);
4200 for (i
= 0; i
< ret_nmap
; i
++) {
4201 ASSERT(mval
[i
].br_blockcount
> 0);
4202 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
4203 ASSERT(mval
[i
].br_startoff
>= bno
);
4204 ASSERT(mval
[i
].br_blockcount
<= len
);
4205 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
4208 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
4209 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
4213 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
4214 mval
[i
].br_startoff
);
4215 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
4216 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
4217 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
4218 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
4225 * Trim the returned map to the required bounds
4229 struct xfs_bmbt_irec
*mval
,
4230 struct xfs_bmbt_irec
*got
,
4238 if ((flags
& XFS_BMAPI_ENTIRE
) ||
4239 got
->br_startoff
+ got
->br_blockcount
<= obno
) {
4241 if (isnullstartblock(got
->br_startblock
))
4242 mval
->br_startblock
= DELAYSTARTBLOCK
;
4248 ASSERT((*bno
>= obno
) || (n
== 0));
4250 mval
->br_startoff
= *bno
;
4251 if (isnullstartblock(got
->br_startblock
))
4252 mval
->br_startblock
= DELAYSTARTBLOCK
;
4254 mval
->br_startblock
= got
->br_startblock
+
4255 (*bno
- got
->br_startoff
);
4257 * Return the minimum of what we got and what we asked for for
4258 * the length. We can use the len variable here because it is
4259 * modified below and we could have been there before coming
4260 * here if the first part of the allocation didn't overlap what
4263 mval
->br_blockcount
= XFS_FILBLKS_MIN(end
- *bno
,
4264 got
->br_blockcount
- (*bno
- got
->br_startoff
));
4265 mval
->br_state
= got
->br_state
;
4266 ASSERT(mval
->br_blockcount
<= len
);
4271 * Update and validate the extent map to return
4274 xfs_bmapi_update_map(
4275 struct xfs_bmbt_irec
**map
,
4283 xfs_bmbt_irec_t
*mval
= *map
;
4285 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
4286 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
4287 ASSERT((flags
& XFS_BMAPI_ENTIRE
) || (mval
->br_blockcount
<= *len
) ||
4288 (mval
->br_startoff
< obno
));
4290 *bno
= mval
->br_startoff
+ mval
->br_blockcount
;
4292 if (*n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
4293 /* update previous map with new information */
4294 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
4295 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
4296 ASSERT(mval
->br_state
== mval
[-1].br_state
);
4297 mval
[-1].br_blockcount
= mval
->br_blockcount
;
4298 mval
[-1].br_state
= mval
->br_state
;
4299 } else if (*n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
4300 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
4301 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
4302 mval
->br_startblock
== mval
[-1].br_startblock
+
4303 mval
[-1].br_blockcount
&&
4304 ((flags
& XFS_BMAPI_IGSTATE
) ||
4305 mval
[-1].br_state
== mval
->br_state
)) {
4306 ASSERT(mval
->br_startoff
==
4307 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
4308 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4309 } else if (*n
> 0 &&
4310 mval
->br_startblock
== DELAYSTARTBLOCK
&&
4311 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
4312 mval
->br_startoff
==
4313 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
4314 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4315 mval
[-1].br_state
= mval
->br_state
;
4316 } else if (!((*n
== 0) &&
4317 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
4326 * Map file blocks to filesystem blocks without allocation.
4330 struct xfs_inode
*ip
,
4333 struct xfs_bmbt_irec
*mval
,
4337 struct xfs_mount
*mp
= ip
->i_mount
;
4338 struct xfs_ifork
*ifp
;
4339 struct xfs_bmbt_irec got
;
4340 struct xfs_bmbt_irec prev
;
4347 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4348 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4351 ASSERT(!(flags
& ~(XFS_BMAPI_ATTRFORK
|XFS_BMAPI_ENTIRE
|
4352 XFS_BMAPI_IGSTATE
)));
4354 if (unlikely(XFS_TEST_ERROR(
4355 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4356 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
),
4357 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4358 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW
, mp
);
4359 return XFS_ERROR(EFSCORRUPTED
);
4362 if (XFS_FORCED_SHUTDOWN(mp
))
4363 return XFS_ERROR(EIO
);
4365 XFS_STATS_INC(xs_blk_mapr
);
4367 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4368 ASSERT(ifp
->if_ext_max
==
4369 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
4371 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4372 error
= xfs_iread_extents(NULL
, ip
, whichfork
);
4377 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
, &prev
);
4381 while (bno
< end
&& n
< *nmap
) {
4382 /* Reading past eof, act as though there's a hole up to end. */
4384 got
.br_startoff
= end
;
4385 if (got
.br_startoff
> bno
) {
4386 /* Reading in a hole. */
4387 mval
->br_startoff
= bno
;
4388 mval
->br_startblock
= HOLESTARTBLOCK
;
4389 mval
->br_blockcount
=
4390 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4391 mval
->br_state
= XFS_EXT_NORM
;
4392 bno
+= mval
->br_blockcount
;
4393 len
-= mval
->br_blockcount
;
4399 /* set up the extent map to return. */
4400 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4401 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4403 /* If we're done, stop now. */
4404 if (bno
>= end
|| n
>= *nmap
)
4407 /* Else go on to the next record. */
4408 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4409 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4418 xfs_bmapi_reserve_delalloc(
4419 struct xfs_inode
*ip
,
4422 struct xfs_bmbt_irec
*got
,
4423 struct xfs_bmbt_irec
*prev
,
4424 xfs_extnum_t
*lastx
,
4427 struct xfs_mount
*mp
= ip
->i_mount
;
4428 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4430 xfs_extlen_t indlen
;
4431 char rt
= XFS_IS_REALTIME_INODE(ip
);
4435 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
4437 alen
= XFS_FILBLKS_MIN(alen
, got
->br_startoff
- aoff
);
4439 /* Figure out the extent size, adjust alen */
4440 extsz
= xfs_get_extsz_hint(ip
);
4443 * Make sure we don't exceed a single extent length when we
4444 * align the extent by reducing length we are going to
4445 * allocate by the maximum amount extent size aligment may
4448 alen
= XFS_FILBLKS_MIN(len
, MAXEXTLEN
- (2 * extsz
- 1));
4449 error
= xfs_bmap_extsize_align(mp
, got
, prev
, extsz
, rt
, eof
,
4450 1, 0, &aoff
, &alen
);
4455 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4458 * Make a transaction-less quota reservation for delayed allocation
4459 * blocks. This number gets adjusted later. We return if we haven't
4460 * allocated blocks already inside this loop.
4462 error
= xfs_trans_reserve_quota_nblks(NULL
, ip
, (long)alen
, 0,
4463 rt
? XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4468 * Split changing sb for alen and indlen since they could be coming
4469 * from different places.
4471 indlen
= (xfs_extlen_t
)xfs_bmap_worst_indlen(ip
, alen
);
4475 error
= xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
4476 -((int64_t)extsz
), 0);
4478 error
= xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
4479 -((int64_t)alen
), 0);
4483 goto out_unreserve_quota
;
4485 error
= xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
4486 -((int64_t)indlen
), 0);
4488 goto out_unreserve_blocks
;
4491 ip
->i_delayed_blks
+= alen
;
4493 got
->br_startoff
= aoff
;
4494 got
->br_startblock
= nullstartblock(indlen
);
4495 got
->br_blockcount
= alen
;
4496 got
->br_state
= XFS_EXT_NORM
;
4497 xfs_bmap_add_extent_hole_delay(ip
, lastx
, got
);
4500 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4501 * might have merged it into one of the neighbouring ones.
4503 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *lastx
), got
);
4505 ASSERT(got
->br_startoff
<= aoff
);
4506 ASSERT(got
->br_startoff
+ got
->br_blockcount
>= aoff
+ alen
);
4507 ASSERT(isnullstartblock(got
->br_startblock
));
4508 ASSERT(got
->br_state
== XFS_EXT_NORM
);
4511 out_unreserve_blocks
:
4513 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
, extsz
, 0);
4515 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
, alen
, 0);
4516 out_unreserve_quota
:
4517 if (XFS_IS_QUOTA_ON(mp
))
4518 xfs_trans_unreserve_quota_nblks(NULL
, ip
, alen
, 0, rt
?
4519 XFS_QMOPT_RES_RTBLKS
: XFS_QMOPT_RES_REGBLKS
);
4524 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4528 struct xfs_inode
*ip
, /* incore inode */
4529 xfs_fileoff_t bno
, /* starting file offs. mapped */
4530 xfs_filblks_t len
, /* length to map in file */
4531 struct xfs_bmbt_irec
*mval
, /* output: map values */
4532 int *nmap
, /* i/o: mval size/count */
4533 int flags
) /* XFS_BMAPI_... */
4535 struct xfs_mount
*mp
= ip
->i_mount
;
4536 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
4537 struct xfs_bmbt_irec got
; /* current file extent record */
4538 struct xfs_bmbt_irec prev
; /* previous file extent record */
4539 xfs_fileoff_t obno
; /* old block number (offset) */
4540 xfs_fileoff_t end
; /* end of mapped file region */
4541 xfs_extnum_t lastx
; /* last useful extent number */
4542 int eof
; /* we've hit the end of extents */
4543 int n
= 0; /* current extent index */
4547 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4548 ASSERT(!(flags
& ~XFS_BMAPI_ENTIRE
));
4550 if (unlikely(XFS_TEST_ERROR(
4551 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
4552 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
4553 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4554 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW
, mp
);
4555 return XFS_ERROR(EFSCORRUPTED
);
4558 if (XFS_FORCED_SHUTDOWN(mp
))
4559 return XFS_ERROR(EIO
);
4561 XFS_STATS_INC(xs_blk_mapw
);
4563 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4564 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
4569 xfs_bmap_search_extents(ip
, bno
, XFS_DATA_FORK
, &eof
, &lastx
, &got
, &prev
);
4573 while (bno
< end
&& n
< *nmap
) {
4574 if (eof
|| got
.br_startoff
> bno
) {
4575 error
= xfs_bmapi_reserve_delalloc(ip
, bno
, len
, &got
,
4576 &prev
, &lastx
, eof
);
4586 /* set up the extent map to return. */
4587 xfs_bmapi_trim_map(mval
, &got
, &bno
, len
, obno
, end
, n
, flags
);
4588 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4590 /* If we're done, stop now. */
4591 if (bno
>= end
|| n
>= *nmap
)
4594 /* Else go on to the next record. */
4596 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4597 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &got
);
4609 struct xfs_bmalloca
*bma
,
4610 xfs_extnum_t
*lastx
,
4611 struct xfs_btree_cur
**cur
,
4616 struct xfs_mount
*mp
= bma
->ip
->i_mount
;
4617 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4618 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4619 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4626 rt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(bma
->ip
);
4629 * For the wasdelay case, we could also just allocate the stuff asked
4630 * for in this bmap call but that wouldn't be as good.
4633 alen
= (xfs_extlen_t
)bma
->got
.br_blockcount
;
4634 aoff
= bma
->got
.br_startoff
;
4635 if (*lastx
!= NULLEXTNUM
&& *lastx
) {
4636 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *lastx
- 1),
4640 alen
= (xfs_extlen_t
)XFS_FILBLKS_MIN(bma
->alen
, MAXEXTLEN
);
4642 alen
= (xfs_extlen_t
)XFS_FILBLKS_MIN(alen
,
4643 bma
->got
.br_startoff
- bma
->off
);
4648 * Indicate if this is the first user data in the file, or just any
4651 if (!(flags
& XFS_BMAPI_METADATA
)) {
4652 bma
->userdata
= (aoff
== 0) ?
4653 XFS_ALLOC_INITIAL_USER_DATA
: XFS_ALLOC_USERDATA
;
4657 * Fill in changeable bma fields.
4661 bma
->minlen
= (flags
& XFS_BMAPI_CONTIG
) ? alen
: 1;
4665 * Only want to do the alignment at the eof if it is userdata and
4666 * allocation length is larger than a stripe unit.
4668 if (mp
->m_dalign
&& alen
>= mp
->m_dalign
&&
4669 !(flags
& XFS_BMAPI_METADATA
) && whichfork
== XFS_DATA_FORK
) {
4670 error
= xfs_bmap_isaeof(bma
, whichfork
);
4675 error
= xfs_bmap_alloc(bma
);
4680 * Copy out result fields.
4685 if (bma
->flist
->xbf_low
)
4688 (*cur
)->bc_private
.b
.firstblock
= *bma
->firstblock
;
4689 if (abno
== NULLFSBLOCK
)
4691 if ((ifp
->if_flags
& XFS_IFBROOT
) && !*cur
) {
4692 (*cur
) = xfs_bmbt_init_cursor(mp
, bma
->tp
, bma
->ip
, whichfork
);
4693 (*cur
)->bc_private
.b
.firstblock
= *bma
->firstblock
;
4694 (*cur
)->bc_private
.b
.flist
= bma
->flist
;
4697 * Bump the number of extents we've allocated
4703 (*cur
)->bc_private
.b
.flags
=
4704 bma
->wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
4706 bma
->got
.br_startoff
= aoff
;
4707 bma
->got
.br_startblock
= abno
;
4708 bma
->got
.br_blockcount
= alen
;
4709 bma
->got
.br_state
= XFS_EXT_NORM
;
4712 * A wasdelay extent has been initialized, so shouldn't be flagged
4715 if (!bma
->wasdel
&& (flags
& XFS_BMAPI_PREALLOC
) &&
4716 xfs_sb_version_hasextflgbit(&mp
->m_sb
))
4717 bma
->got
.br_state
= XFS_EXT_UNWRITTEN
;
4720 error
= xfs_bmap_add_extent_delay_real(bma
->tp
, bma
->ip
, lastx
,
4721 cur
, &bma
->got
, bma
->firstblock
, bma
->flist
,
4724 error
= xfs_bmap_add_extent_hole_real(bma
->tp
, bma
->ip
, lastx
,
4725 cur
, &bma
->got
, bma
->firstblock
, bma
->flist
,
4726 logflags
, whichfork
);
4733 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4734 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4735 * the neighbouring ones.
4737 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *lastx
), &bma
->got
);
4739 ASSERT(bma
->got
.br_startoff
<= aoff
);
4740 ASSERT(bma
->got
.br_startoff
+ bma
->got
.br_blockcount
>= aoff
+ alen
);
4741 ASSERT(bma
->got
.br_state
== XFS_EXT_NORM
||
4742 bma
->got
.br_state
== XFS_EXT_UNWRITTEN
);
4747 xfs_bmapi_convert_unwritten(
4748 struct xfs_bmalloca
*bma
,
4749 struct xfs_bmbt_irec
*mval
,
4751 xfs_extnum_t
*lastx
,
4752 struct xfs_btree_cur
**cur
,
4756 int whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4757 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4758 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(bma
->ip
, whichfork
);
4763 /* check if we need to do unwritten->real conversion */
4764 if (mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4765 (flags
& XFS_BMAPI_PREALLOC
))
4768 /* check if we need to do real->unwritten conversion */
4769 if (mval
->br_state
== XFS_EXT_NORM
&&
4770 (flags
& (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
)) !=
4771 (XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
))
4775 * Modify (by adding) the state flag, if writing.
4777 ASSERT(mval
->br_blockcount
<= len
);
4778 if ((ifp
->if_flags
& XFS_IFBROOT
) && !*cur
) {
4779 *cur
= xfs_bmbt_init_cursor(bma
->ip
->i_mount
, bma
->tp
,
4780 bma
->ip
, whichfork
);
4781 (*cur
)->bc_private
.b
.firstblock
= *bma
->firstblock
;
4782 (*cur
)->bc_private
.b
.flist
= bma
->flist
;
4784 mval
->br_state
= (mval
->br_state
== XFS_EXT_UNWRITTEN
)
4785 ? XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
4787 error
= xfs_bmap_add_extent_unwritten_real(bma
->tp
, bma
->ip
, lastx
,
4788 cur
, mval
, bma
->firstblock
, bma
->flist
, logflags
);
4793 * Update our extent pointer, given that
4794 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4795 * of the neighbouring ones.
4797 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, *lastx
), &bma
->got
);
4800 * We may have combined previously unwritten space with written space,
4801 * so generate another request.
4803 if (mval
->br_blockcount
< len
)
4809 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4810 * extent state if necessary. Details behaviour is controlled by the flags
4811 * parameter. Only allocates blocks from a single allocation group, to avoid
4814 * The returned value in "firstblock" from the first call in a transaction
4815 * must be remembered and presented to subsequent calls in "firstblock".
4816 * An upper bound for the number of blocks to be allocated is supplied to
4817 * the first call in "total"; if no allocation group has that many free
4818 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4822 struct xfs_trans
*tp
, /* transaction pointer */
4823 struct xfs_inode
*ip
, /* incore inode */
4824 xfs_fileoff_t bno
, /* starting file offs. mapped */
4825 xfs_filblks_t len
, /* length to map in file */
4826 int flags
, /* XFS_BMAPI_... */
4827 xfs_fsblock_t
*firstblock
, /* first allocated block
4828 controls a.g. for allocs */
4829 xfs_extlen_t total
, /* total blocks needed */
4830 struct xfs_bmbt_irec
*mval
, /* output: map values */
4831 int *nmap
, /* i/o: mval size/count */
4832 struct xfs_bmap_free
*flist
) /* i/o: list extents to free */
4834 struct xfs_mount
*mp
= ip
->i_mount
;
4835 struct xfs_ifork
*ifp
;
4836 struct xfs_bmalloca bma
= { 0 }; /* args for xfs_bmap_alloc */
4837 struct xfs_btree_cur
*cur
; /* bmap btree cursor */
4838 xfs_fileoff_t end
; /* end of mapped file region */
4839 int eof
; /* after the end of extents */
4840 int error
; /* error return */
4841 xfs_extnum_t lastx
; /* last useful extent number */
4842 int logflags
; /* flags for transaction logging */
4843 int n
; /* current extent index */
4844 int nallocs
; /* number of extents alloc'd */
4845 xfs_fileoff_t obno
; /* old block number (offset) */
4846 int tmp_logflags
; /* temp flags holder */
4847 int whichfork
; /* data or attr fork */
4848 char inhole
; /* current location is hole in file */
4849 char wasdelay
; /* old extent was delayed */
4852 xfs_fileoff_t orig_bno
; /* original block number value */
4853 int orig_flags
; /* original flags arg value */
4854 xfs_filblks_t orig_len
; /* original value of len arg */
4855 struct xfs_bmbt_irec
*orig_mval
; /* original value of mval */
4856 int orig_nmap
; /* original value of *nmap */
4866 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
);
4867 ASSERT(!(flags
& XFS_BMAPI_IGSTATE
));
4870 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4871 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4873 if (unlikely(XFS_TEST_ERROR(
4874 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4875 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
4876 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
),
4877 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4878 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW
, mp
);
4879 return XFS_ERROR(EFSCORRUPTED
);
4882 if (XFS_FORCED_SHUTDOWN(mp
))
4883 return XFS_ERROR(EIO
);
4885 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4886 ASSERT(ifp
->if_ext_max
==
4887 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
4889 XFS_STATS_INC(xs_blk_mapw
);
4895 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
4896 error
= xfs_bmap_local_to_extents(tp
, ip
, firstblock
, total
,
4897 &logflags
, whichfork
);
4902 if (*firstblock
== NULLFSBLOCK
) {
4903 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4904 bma
.minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4911 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
4912 error
= xfs_iread_extents(tp
, ip
, whichfork
);
4917 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &bma
.got
,
4928 bma
.firstblock
= firstblock
;
4930 while (bno
< end
&& n
< *nmap
) {
4931 inhole
= eof
|| bma
.got
.br_startoff
> bno
;
4932 wasdelay
= !inhole
&& isnullstartblock(bma
.got
.br_startblock
);
4935 * First, deal with the hole before the allocated space
4936 * that we found, if any.
4938 if (inhole
|| wasdelay
) {
4940 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4941 bma
.wasdel
= wasdelay
;
4945 error
= xfs_bmapi_allocate(&bma
, &lastx
, &cur
, flags
,
4946 &nallocs
, &tmp_logflags
);
4947 logflags
|= tmp_logflags
;
4950 if (bma
.rval
== NULLFSBLOCK
)
4954 /* Deal with the allocated space we found. */
4955 xfs_bmapi_trim_map(mval
, &bma
.got
, &bno
, len
, obno
,
4958 /* Execute unwritten extent conversion if necessary */
4959 error
= xfs_bmapi_convert_unwritten(&bma
, mval
, len
, &lastx
,
4960 &cur
, flags
, &tmp_logflags
);
4961 logflags
|= tmp_logflags
;
4962 if (error
== EAGAIN
)
4967 /* update the extent map to return */
4968 xfs_bmapi_update_map(&mval
, &bno
, &len
, obno
, end
, &n
, flags
);
4971 * If we're done, stop now. Stop when we've allocated
4972 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4973 * the transaction may get too big.
4975 if (bno
>= end
|| n
>= *nmap
|| nallocs
>= *nmap
)
4978 /* Else go on to the next record. */
4980 if (++lastx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
4981 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
), &bma
.got
);
4988 * Transform from btree to extents, give it cur.
4990 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
4991 XFS_IFORK_NEXTENTS(ip
, whichfork
) <= ifp
->if_ext_max
) {
4993 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
,
4994 &tmp_logflags
, whichfork
);
4995 logflags
|= tmp_logflags
;
4999 ASSERT(ifp
->if_ext_max
==
5000 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
5001 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
5002 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ifp
->if_ext_max
);
5006 * Log everything. Do this after conversion, there's no point in
5007 * logging the extent records if we've converted to btree format.
5009 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5010 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5011 logflags
&= ~xfs_ilog_fext(whichfork
);
5012 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5013 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5014 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5016 * Log whatever the flags say, even if error. Otherwise we might miss
5017 * detecting a case where the data is changed, there's an error,
5018 * and it's not logged so we don't shutdown when we should.
5021 xfs_trans_log_inode(tp
, ip
, logflags
);
5025 ASSERT(*firstblock
== NULLFSBLOCK
||
5026 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
5028 cur
->bc_private
.b
.firstblock
) ||
5030 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
5032 cur
->bc_private
.b
.firstblock
)));
5033 *firstblock
= cur
->bc_private
.b
.firstblock
;
5035 xfs_btree_del_cursor(cur
,
5036 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5039 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
5045 * Unmap (remove) blocks from a file.
5046 * If nexts is nonzero then the number of extents to remove is limited to
5047 * that value. If not all extents in the block range can be removed then
5052 xfs_trans_t
*tp
, /* transaction pointer */
5053 struct xfs_inode
*ip
, /* incore inode */
5054 xfs_fileoff_t bno
, /* starting offset to unmap */
5055 xfs_filblks_t len
, /* length to unmap in file */
5056 int flags
, /* misc flags */
5057 xfs_extnum_t nexts
, /* number of extents max */
5058 xfs_fsblock_t
*firstblock
, /* first allocated block
5059 controls a.g. for allocs */
5060 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
5061 int *done
) /* set if not done yet */
5063 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5064 xfs_bmbt_irec_t del
; /* extent being deleted */
5065 int eof
; /* is deleting at eof */
5066 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
5067 int error
; /* error return value */
5068 xfs_extnum_t extno
; /* extent number in list */
5069 xfs_bmbt_irec_t got
; /* current extent record */
5070 xfs_ifork_t
*ifp
; /* inode fork pointer */
5071 int isrt
; /* freeing in rt area */
5072 xfs_extnum_t lastx
; /* last extent index used */
5073 int logflags
; /* transaction logging flags */
5074 xfs_extlen_t mod
; /* rt extent offset */
5075 xfs_mount_t
*mp
; /* mount structure */
5076 xfs_extnum_t nextents
; /* number of file extents */
5077 xfs_bmbt_irec_t prev
; /* previous extent record */
5078 xfs_fileoff_t start
; /* first file offset deleted */
5079 int tmp_logflags
; /* partial logging flags */
5080 int wasdel
; /* was a delayed alloc extent */
5081 int whichfork
; /* data or attribute fork */
5084 trace_xfs_bunmap(ip
, bno
, len
, flags
, _RET_IP_
);
5086 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
5087 XFS_ATTR_FORK
: XFS_DATA_FORK
;
5088 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5090 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5091 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5092 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5094 return XFS_ERROR(EFSCORRUPTED
);
5097 if (XFS_FORCED_SHUTDOWN(mp
))
5098 return XFS_ERROR(EIO
);
5102 ASSERT(ifp
->if_ext_max
==
5103 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
5104 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5105 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5107 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5108 if (nextents
== 0) {
5112 XFS_STATS_INC(xs_blk_unmap
);
5113 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5115 bno
= start
+ len
- 1;
5116 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5120 * Check to see if the given block number is past the end of the
5121 * file, back up to the last block if so...
5124 ep
= xfs_iext_get_ext(ifp
, --lastx
);
5125 xfs_bmbt_get_all(ep
, &got
);
5126 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5129 if (ifp
->if_flags
& XFS_IFBROOT
) {
5130 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5131 cur
= xfs_bmbt_init_cursor(mp
, tp
, ip
, whichfork
);
5132 cur
->bc_private
.b
.firstblock
= *firstblock
;
5133 cur
->bc_private
.b
.flist
= flist
;
5134 cur
->bc_private
.b
.flags
= 0;
5138 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5139 (nexts
== 0 || extno
< nexts
)) {
5141 * Is the found extent after a hole in which bno lives?
5142 * Just back up to the previous extent, if so.
5144 if (got
.br_startoff
> bno
) {
5147 ep
= xfs_iext_get_ext(ifp
, lastx
);
5148 xfs_bmbt_get_all(ep
, &got
);
5151 * Is the last block of this extent before the range
5152 * we're supposed to delete? If so, we're done.
5154 bno
= XFS_FILEOFF_MIN(bno
,
5155 got
.br_startoff
+ got
.br_blockcount
- 1);
5159 * Then deal with the (possibly delayed) allocated space
5164 wasdel
= isnullstartblock(del
.br_startblock
);
5165 if (got
.br_startoff
< start
) {
5166 del
.br_startoff
= start
;
5167 del
.br_blockcount
-= start
- got
.br_startoff
;
5169 del
.br_startblock
+= start
- got
.br_startoff
;
5171 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5172 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5173 sum
= del
.br_startblock
+ del
.br_blockcount
;
5175 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5177 * Realtime extent not lined up at the end.
5178 * The extent could have been split into written
5179 * and unwritten pieces, or we could just be
5180 * unmapping part of it. But we can't really
5181 * get rid of part of a realtime extent.
5183 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5184 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5186 * This piece is unwritten, or we're not
5187 * using unwritten extents. Skip over it.
5190 bno
-= mod
> del
.br_blockcount
?
5191 del
.br_blockcount
: mod
;
5192 if (bno
< got
.br_startoff
) {
5194 xfs_bmbt_get_all(xfs_iext_get_ext(
5200 * It's written, turn it unwritten.
5201 * This is better than zeroing it.
5203 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5204 ASSERT(xfs_trans_get_block_res(tp
) > 0);
5206 * If this spans a realtime extent boundary,
5207 * chop it back to the start of the one we end at.
5209 if (del
.br_blockcount
> mod
) {
5210 del
.br_startoff
+= del
.br_blockcount
- mod
;
5211 del
.br_startblock
+= del
.br_blockcount
- mod
;
5212 del
.br_blockcount
= mod
;
5214 del
.br_state
= XFS_EXT_UNWRITTEN
;
5215 error
= xfs_bmap_add_extent_unwritten_real(tp
, ip
,
5216 &lastx
, &cur
, &del
, firstblock
, flist
,
5222 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5224 * Realtime extent is lined up at the end but not
5225 * at the front. We'll get rid of full extents if
5228 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5229 if (del
.br_blockcount
> mod
) {
5230 del
.br_blockcount
-= mod
;
5231 del
.br_startoff
+= mod
;
5232 del
.br_startblock
+= mod
;
5233 } else if ((del
.br_startoff
== start
&&
5234 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5235 xfs_trans_get_block_res(tp
) == 0)) ||
5236 !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
5238 * Can't make it unwritten. There isn't
5239 * a full extent here so just skip it.
5241 ASSERT(bno
>= del
.br_blockcount
);
5242 bno
-= del
.br_blockcount
;
5243 if (got
.br_startoff
> bno
) {
5245 ep
= xfs_iext_get_ext(ifp
,
5247 xfs_bmbt_get_all(ep
, &got
);
5251 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5253 * This one is already unwritten.
5254 * It must have a written left neighbor.
5255 * Unwrite the killed part of that one and
5259 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
,
5261 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5262 ASSERT(!isnullstartblock(prev
.br_startblock
));
5263 ASSERT(del
.br_startblock
==
5264 prev
.br_startblock
+ prev
.br_blockcount
);
5265 if (prev
.br_startoff
< start
) {
5266 mod
= start
- prev
.br_startoff
;
5267 prev
.br_blockcount
-= mod
;
5268 prev
.br_startblock
+= mod
;
5269 prev
.br_startoff
= start
;
5271 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5273 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5274 ip
, &lastx
, &cur
, &prev
,
5275 firstblock
, flist
, &logflags
);
5280 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5281 del
.br_state
= XFS_EXT_UNWRITTEN
;
5282 error
= xfs_bmap_add_extent_unwritten_real(tp
,
5283 ip
, &lastx
, &cur
, &del
,
5284 firstblock
, flist
, &logflags
);
5291 ASSERT(startblockval(del
.br_startblock
) > 0);
5292 /* Update realtime/data freespace, unreserve quota */
5294 xfs_filblks_t rtexts
;
5296 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5297 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5298 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
5299 (int64_t)rtexts
, 0);
5300 (void)xfs_trans_reserve_quota_nblks(NULL
,
5301 ip
, -((long)del
.br_blockcount
), 0,
5302 XFS_QMOPT_RES_RTBLKS
);
5304 xfs_icsb_modify_counters(mp
, XFS_SBS_FDBLOCKS
,
5305 (int64_t)del
.br_blockcount
, 0);
5306 (void)xfs_trans_reserve_quota_nblks(NULL
,
5307 ip
, -((long)del
.br_blockcount
), 0,
5308 XFS_QMOPT_RES_REGBLKS
);
5310 ip
->i_delayed_blks
-= del
.br_blockcount
;
5312 cur
->bc_private
.b
.flags
|=
5313 XFS_BTCUR_BPRV_WASDEL
;
5315 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5317 * If it's the case where the directory code is running
5318 * with no block reservation, and the deleted block is in
5319 * the middle of its extent, and the resulting insert
5320 * of an extent would cause transformation to btree format,
5321 * then reject it. The calling code will then swap
5322 * blocks around instead.
5323 * We have to do this now, rather than waiting for the
5324 * conversion to btree format, since the transaction
5327 if (!wasdel
&& xfs_trans_get_block_res(tp
) == 0 &&
5328 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5329 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= ifp
->if_ext_max
&&
5330 del
.br_startoff
> got
.br_startoff
&&
5331 del
.br_startoff
+ del
.br_blockcount
<
5332 got
.br_startoff
+ got
.br_blockcount
) {
5333 error
= XFS_ERROR(ENOSPC
);
5336 error
= xfs_bmap_del_extent(ip
, tp
, &lastx
, flist
, cur
, &del
,
5337 &tmp_logflags
, whichfork
);
5338 logflags
|= tmp_logflags
;
5341 bno
= del
.br_startoff
- 1;
5344 * If not done go on to the next (previous) record.
5346 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5348 ep
= xfs_iext_get_ext(ifp
, lastx
);
5349 if (xfs_bmbt_get_startoff(ep
) > bno
) {
5351 ep
= xfs_iext_get_ext(ifp
,
5354 xfs_bmbt_get_all(ep
, &got
);
5359 *done
= bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0;
5360 ASSERT(ifp
->if_ext_max
==
5361 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
5363 * Convert to a btree if necessary.
5365 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5366 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ifp
->if_ext_max
) {
5367 ASSERT(cur
== NULL
);
5368 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
,
5369 &cur
, 0, &tmp_logflags
, whichfork
);
5370 logflags
|= tmp_logflags
;
5375 * transform from btree to extents, give it cur
5377 else if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
5378 XFS_IFORK_NEXTENTS(ip
, whichfork
) <= ifp
->if_ext_max
) {
5379 ASSERT(cur
!= NULL
);
5380 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5382 logflags
|= tmp_logflags
;
5387 * transform from extents to local?
5389 ASSERT(ifp
->if_ext_max
==
5390 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
5394 * Log everything. Do this after conversion, there's no point in
5395 * logging the extent records if we've converted to btree format.
5397 if ((logflags
& xfs_ilog_fext(whichfork
)) &&
5398 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5399 logflags
&= ~xfs_ilog_fext(whichfork
);
5400 else if ((logflags
& xfs_ilog_fbroot(whichfork
)) &&
5401 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5402 logflags
&= ~xfs_ilog_fbroot(whichfork
);
5404 * Log inode even in the error case, if the transaction
5405 * is dirty we'll need to shut down the filesystem.
5408 xfs_trans_log_inode(tp
, ip
, logflags
);
5411 *firstblock
= cur
->bc_private
.b
.firstblock
;
5412 cur
->bc_private
.b
.allocated
= 0;
5414 xfs_btree_del_cursor(cur
,
5415 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5421 * returns 1 for success, 0 if we failed to map the extent.
5424 xfs_getbmapx_fix_eof_hole(
5425 xfs_inode_t
*ip
, /* xfs incore inode pointer */
5426 struct getbmapx
*out
, /* output structure */
5427 int prealloced
, /* this is a file with
5428 * preallocated data space */
5429 __int64_t end
, /* last block requested */
5430 xfs_fsblock_t startblock
)
5433 xfs_mount_t
*mp
; /* file system mount point */
5434 xfs_ifork_t
*ifp
; /* inode fork pointer */
5435 xfs_extnum_t lastx
; /* last extent pointer */
5436 xfs_fileoff_t fileblock
;
5438 if (startblock
== HOLESTARTBLOCK
) {
5440 out
->bmv_block
= -1;
5441 fixlen
= XFS_FSB_TO_BB(mp
, XFS_B_TO_FSB(mp
, ip
->i_size
));
5442 fixlen
-= out
->bmv_offset
;
5443 if (prealloced
&& out
->bmv_offset
+ out
->bmv_length
== end
) {
5444 /* Came to hole at EOF. Trim it. */
5447 out
->bmv_length
= fixlen
;
5450 if (startblock
== DELAYSTARTBLOCK
)
5451 out
->bmv_block
= -2;
5453 out
->bmv_block
= xfs_fsb_to_db(ip
, startblock
);
5454 fileblock
= XFS_BB_TO_FSB(ip
->i_mount
, out
->bmv_offset
);
5455 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
5456 if (xfs_iext_bno_to_ext(ifp
, fileblock
, &lastx
) &&
5457 (lastx
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))-1))
5458 out
->bmv_oflags
|= BMV_OF_LAST
;
5465 * Get inode's extents as described in bmv, and format for output.
5466 * Calls formatter to fill the user's buffer until all extents
5467 * are mapped, until the passed-in bmv->bmv_count slots have
5468 * been filled, or until the formatter short-circuits the loop,
5469 * if it is tracking filled-in extents on its own.
5471 int /* error code */
5474 struct getbmapx
*bmv
, /* user bmap structure */
5475 xfs_bmap_format_t formatter
, /* format to user */
5476 void *arg
) /* formatter arg */
5478 __int64_t bmvend
; /* last block requested */
5479 int error
= 0; /* return value */
5480 __int64_t fixlen
; /* length for -1 case */
5481 int i
; /* extent number */
5482 int lock
; /* lock state */
5483 xfs_bmbt_irec_t
*map
; /* buffer for user's data */
5484 xfs_mount_t
*mp
; /* file system mount point */
5485 int nex
; /* # of user extents can do */
5486 int nexleft
; /* # of user extents left */
5487 int subnex
; /* # of bmapi's can do */
5488 int nmap
; /* number of map entries */
5489 struct getbmapx
*out
; /* output structure */
5490 int whichfork
; /* data or attr fork */
5491 int prealloced
; /* this is a file with
5492 * preallocated data space */
5493 int iflags
; /* interface flags */
5494 int bmapi_flags
; /* flags for xfs_bmapi */
5498 iflags
= bmv
->bmv_iflags
;
5499 whichfork
= iflags
& BMV_IF_ATTRFORK
? XFS_ATTR_FORK
: XFS_DATA_FORK
;
5501 if (whichfork
== XFS_ATTR_FORK
) {
5502 if (XFS_IFORK_Q(ip
)) {
5503 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
&&
5504 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_BTREE
&&
5505 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_LOCAL
)
5506 return XFS_ERROR(EINVAL
);
5507 } else if (unlikely(
5508 ip
->i_d
.di_aformat
!= 0 &&
5509 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
)) {
5510 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW
,
5512 return XFS_ERROR(EFSCORRUPTED
);
5518 if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
&&
5519 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
&&
5520 ip
->i_d
.di_format
!= XFS_DINODE_FMT_LOCAL
)
5521 return XFS_ERROR(EINVAL
);
5523 if (xfs_get_extsz_hint(ip
) ||
5524 ip
->i_d
.di_flags
& (XFS_DIFLAG_PREALLOC
|XFS_DIFLAG_APPEND
)){
5526 fixlen
= XFS_MAXIOFFSET(mp
);
5529 fixlen
= ip
->i_size
;
5533 if (bmv
->bmv_length
== -1) {
5534 fixlen
= XFS_FSB_TO_BB(mp
, XFS_B_TO_FSB(mp
, fixlen
));
5536 max_t(__int64_t
, fixlen
- bmv
->bmv_offset
, 0);
5537 } else if (bmv
->bmv_length
== 0) {
5538 bmv
->bmv_entries
= 0;
5540 } else if (bmv
->bmv_length
< 0) {
5541 return XFS_ERROR(EINVAL
);
5544 nex
= bmv
->bmv_count
- 1;
5546 return XFS_ERROR(EINVAL
);
5547 bmvend
= bmv
->bmv_offset
+ bmv
->bmv_length
;
5550 if (bmv
->bmv_count
> ULONG_MAX
/ sizeof(struct getbmapx
))
5551 return XFS_ERROR(ENOMEM
);
5552 out
= kmem_zalloc(bmv
->bmv_count
* sizeof(struct getbmapx
), KM_MAYFAIL
);
5554 return XFS_ERROR(ENOMEM
);
5556 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
5557 if (whichfork
== XFS_DATA_FORK
&& !(iflags
& BMV_IF_DELALLOC
)) {
5558 if (ip
->i_delayed_blks
|| ip
->i_size
> ip
->i_d
.di_size
) {
5559 error
= xfs_flush_pages(ip
, 0, -1, 0, FI_REMAPF
);
5561 goto out_unlock_iolock
;
5564 * even after flushing the inode, there can still be delalloc
5565 * blocks on the inode beyond EOF due to speculative
5566 * preallocation. These are not removed until the release
5567 * function is called or the inode is inactivated. Hence we
5568 * cannot assert here that ip->i_delayed_blks == 0.
5572 lock
= xfs_ilock_map_shared(ip
);
5575 * Don't let nex be bigger than the number of extents
5576 * we can have assuming alternating holes and real extents.
5578 if (nex
> XFS_IFORK_NEXTENTS(ip
, whichfork
) * 2 + 1)
5579 nex
= XFS_IFORK_NEXTENTS(ip
, whichfork
) * 2 + 1;
5581 bmapi_flags
= xfs_bmapi_aflag(whichfork
);
5582 if (!(iflags
& BMV_IF_PREALLOC
))
5583 bmapi_flags
|= XFS_BMAPI_IGSTATE
;
5586 * Allocate enough space to handle "subnex" maps at a time.
5590 map
= kmem_alloc(subnex
* sizeof(*map
), KM_MAYFAIL
| KM_NOFS
);
5592 goto out_unlock_ilock
;
5594 bmv
->bmv_entries
= 0;
5596 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0 &&
5597 (whichfork
== XFS_ATTR_FORK
|| !(iflags
& BMV_IF_DELALLOC
))) {
5605 nmap
= (nexleft
> subnex
) ? subnex
: nexleft
;
5606 error
= xfs_bmapi_read(ip
, XFS_BB_TO_FSBT(mp
, bmv
->bmv_offset
),
5607 XFS_BB_TO_FSB(mp
, bmv
->bmv_length
),
5608 map
, &nmap
, bmapi_flags
);
5611 ASSERT(nmap
<= subnex
);
5613 for (i
= 0; i
< nmap
&& nexleft
&& bmv
->bmv_length
; i
++) {
5614 out
[cur_ext
].bmv_oflags
= 0;
5615 if (map
[i
].br_state
== XFS_EXT_UNWRITTEN
)
5616 out
[cur_ext
].bmv_oflags
|= BMV_OF_PREALLOC
;
5617 else if (map
[i
].br_startblock
== DELAYSTARTBLOCK
)
5618 out
[cur_ext
].bmv_oflags
|= BMV_OF_DELALLOC
;
5619 out
[cur_ext
].bmv_offset
=
5620 XFS_FSB_TO_BB(mp
, map
[i
].br_startoff
);
5621 out
[cur_ext
].bmv_length
=
5622 XFS_FSB_TO_BB(mp
, map
[i
].br_blockcount
);
5623 out
[cur_ext
].bmv_unused1
= 0;
5624 out
[cur_ext
].bmv_unused2
= 0;
5625 ASSERT(((iflags
& BMV_IF_DELALLOC
) != 0) ||
5626 (map
[i
].br_startblock
!= DELAYSTARTBLOCK
));
5627 if (map
[i
].br_startblock
== HOLESTARTBLOCK
&&
5628 whichfork
== XFS_ATTR_FORK
) {
5629 /* came to the end of attribute fork */
5630 out
[cur_ext
].bmv_oflags
|= BMV_OF_LAST
;
5634 if (!xfs_getbmapx_fix_eof_hole(ip
, &out
[cur_ext
],
5636 map
[i
].br_startblock
))
5640 out
[cur_ext
].bmv_offset
+
5641 out
[cur_ext
].bmv_length
;
5643 max_t(__int64_t
, 0, bmvend
- bmv
->bmv_offset
);
5646 * In case we don't want to return the hole,
5647 * don't increase cur_ext so that we can reuse
5648 * it in the next loop.
5650 if ((iflags
& BMV_IF_NO_HOLES
) &&
5651 map
[i
].br_startblock
== HOLESTARTBLOCK
) {
5652 memset(&out
[cur_ext
], 0, sizeof(out
[cur_ext
]));
5660 } while (nmap
&& nexleft
&& bmv
->bmv_length
);
5665 xfs_iunlock_map_shared(ip
, lock
);
5667 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
5669 for (i
= 0; i
< cur_ext
; i
++) {
5670 int full
= 0; /* user array is full */
5672 /* format results & advance arg */
5673 error
= formatter(&arg
, &out
[i
], &full
);
5683 STATIC
struct xfs_buf
*
5685 struct xfs_btree_cur
*cur
,
5688 struct xfs_log_item_desc
*lidp
;
5694 for (i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
5695 if (!cur
->bc_bufs
[i
])
5697 if (XFS_BUF_ADDR(cur
->bc_bufs
[i
]) == bno
)
5698 return cur
->bc_bufs
[i
];
5701 /* Chase down all the log items to see if the bp is there */
5702 list_for_each_entry(lidp
, &cur
->bc_tp
->t_items
, lid_trans
) {
5703 struct xfs_buf_log_item
*bip
;
5704 bip
= (struct xfs_buf_log_item
*)lidp
->lid_item
;
5705 if (bip
->bli_item
.li_type
== XFS_LI_BUF
&&
5706 XFS_BUF_ADDR(bip
->bli_buf
) == bno
)
5707 return bip
->bli_buf
;
5715 struct xfs_btree_block
*block
,
5721 __be64
*pp
, *thispa
; /* pointer to block address */
5722 xfs_bmbt_key_t
*prevp
, *keyp
;
5724 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
5727 for( i
= 1; i
<= xfs_btree_get_numrecs(block
); i
++) {
5728 dmxr
= mp
->m_bmap_dmxr
[0];
5729 keyp
= XFS_BMBT_KEY_ADDR(mp
, block
, i
);
5732 ASSERT(be64_to_cpu(prevp
->br_startoff
) <
5733 be64_to_cpu(keyp
->br_startoff
));
5738 * Compare the block numbers to see if there are dups.
5741 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, i
, sz
);
5743 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, i
, dmxr
);
5745 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
5747 thispa
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, j
, sz
);
5749 thispa
= XFS_BMBT_PTR_ADDR(mp
, block
, j
, dmxr
);
5750 if (*thispa
== *pp
) {
5751 xfs_warn(mp
, "%s: thispa(%d) == pp(%d) %Ld",
5753 (unsigned long long)be64_to_cpu(*thispa
));
5754 panic("%s: ptrs are equal in node\n",
5762 * Check that the extents for the inode ip are in the right order in all
5767 xfs_bmap_check_leaf_extents(
5768 xfs_btree_cur_t
*cur
, /* btree cursor or null */
5769 xfs_inode_t
*ip
, /* incore inode pointer */
5770 int whichfork
) /* data or attr fork */
5772 struct xfs_btree_block
*block
; /* current btree block */
5773 xfs_fsblock_t bno
; /* block # of "block" */
5774 xfs_buf_t
*bp
; /* buffer for "block" */
5775 int error
; /* error return value */
5776 xfs_extnum_t i
=0, j
; /* index into the extents list */
5777 xfs_ifork_t
*ifp
; /* fork structure */
5778 int level
; /* btree level, for checking */
5779 xfs_mount_t
*mp
; /* file system mount structure */
5780 __be64
*pp
; /* pointer to block address */
5781 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
5782 xfs_bmbt_rec_t last
= {0, 0}; /* last extent in prev block */
5783 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
5786 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
5792 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5793 block
= ifp
->if_broot
;
5795 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5797 level
= be16_to_cpu(block
->bb_level
);
5799 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
5800 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
5801 bno
= be64_to_cpu(*pp
);
5803 ASSERT(bno
!= NULLDFSBNO
);
5804 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
5805 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
5808 * Go down the tree until leaf level is reached, following the first
5809 * pointer (leftmost) at each level.
5811 while (level
-- > 0) {
5812 /* See if buf is in cur first */
5813 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
5819 if (!bp
&& (error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
5820 XFS_BMAP_BTREE_REF
)))
5822 block
= XFS_BUF_TO_BLOCK(bp
);
5823 XFS_WANT_CORRUPTED_GOTO(
5824 xfs_bmap_sanity_check(mp
, bp
, level
),
5830 * Check this block for basic sanity (increasing keys and
5831 * no duplicate blocks).
5834 xfs_check_block(block
, mp
, 0, 0);
5835 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
5836 bno
= be64_to_cpu(*pp
);
5837 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
5840 xfs_trans_brelse(NULL
, bp
);
5845 * Here with bp and block set to the leftmost leaf node in the tree.
5850 * Loop over all leaf nodes checking that all extents are in the right order.
5853 xfs_fsblock_t nextbno
;
5854 xfs_extnum_t num_recs
;
5857 num_recs
= xfs_btree_get_numrecs(block
);
5860 * Read-ahead the next leaf block, if any.
5863 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
5866 * Check all the extents to make sure they are OK.
5867 * If we had a previous block, the last entry should
5868 * conform with the first entry in this one.
5871 ep
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
5873 ASSERT(xfs_bmbt_disk_get_startoff(&last
) +
5874 xfs_bmbt_disk_get_blockcount(&last
) <=
5875 xfs_bmbt_disk_get_startoff(ep
));
5877 for (j
= 1; j
< num_recs
; j
++) {
5878 nextp
= XFS_BMBT_REC_ADDR(mp
, block
, j
+ 1);
5879 ASSERT(xfs_bmbt_disk_get_startoff(ep
) +
5880 xfs_bmbt_disk_get_blockcount(ep
) <=
5881 xfs_bmbt_disk_get_startoff(nextp
));
5889 xfs_trans_brelse(NULL
, bp
);
5893 * If we've reached the end, stop.
5895 if (bno
== NULLFSBLOCK
)
5898 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
5904 if (!bp
&& (error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
5905 XFS_BMAP_BTREE_REF
)))
5907 block
= XFS_BUF_TO_BLOCK(bp
);
5911 xfs_trans_brelse(NULL
, bp
);
5916 xfs_warn(mp
, "%s: at error0", __func__
);
5918 xfs_trans_brelse(NULL
, bp
);
5920 xfs_warn(mp
, "%s: BAD after btree leaves for %d extents",
5922 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__
);
5928 * Count fsblocks of the given fork.
5931 xfs_bmap_count_blocks(
5932 xfs_trans_t
*tp
, /* transaction pointer */
5933 xfs_inode_t
*ip
, /* incore inode */
5934 int whichfork
, /* data or attr fork */
5935 int *count
) /* out: count of blocks */
5937 struct xfs_btree_block
*block
; /* current btree block */
5938 xfs_fsblock_t bno
; /* block # of "block" */
5939 xfs_ifork_t
*ifp
; /* fork structure */
5940 int level
; /* btree level, for checking */
5941 xfs_mount_t
*mp
; /* file system mount structure */
5942 __be64
*pp
; /* pointer to block address */
5946 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5947 if ( XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
) {
5948 xfs_bmap_count_leaves(ifp
, 0,
5949 ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
),
5955 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5957 block
= ifp
->if_broot
;
5958 level
= be16_to_cpu(block
->bb_level
);
5960 pp
= XFS_BMAP_BROOT_PTR_ADDR(mp
, block
, 1, ifp
->if_broot_bytes
);
5961 bno
= be64_to_cpu(*pp
);
5962 ASSERT(bno
!= NULLDFSBNO
);
5963 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
5964 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
5966 if (unlikely(xfs_bmap_count_tree(mp
, tp
, ifp
, bno
, level
, count
) < 0)) {
5967 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW
,
5969 return XFS_ERROR(EFSCORRUPTED
);
5976 * Recursively walks each level of a btree
5977 * to count total fsblocks is use.
5979 STATIC
int /* error */
5980 xfs_bmap_count_tree(
5981 xfs_mount_t
*mp
, /* file system mount point */
5982 xfs_trans_t
*tp
, /* transaction pointer */
5983 xfs_ifork_t
*ifp
, /* inode fork pointer */
5984 xfs_fsblock_t blockno
, /* file system block number */
5985 int levelin
, /* level in btree */
5986 int *count
) /* Count of blocks */
5989 xfs_buf_t
*bp
, *nbp
;
5990 int level
= levelin
;
5992 xfs_fsblock_t bno
= blockno
;
5993 xfs_fsblock_t nextbno
;
5994 struct xfs_btree_block
*block
, *nextblock
;
5997 if ((error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
, XFS_BMAP_BTREE_REF
)))
6000 block
= XFS_BUF_TO_BLOCK(bp
);
6003 /* Not at node above leaves, count this level of nodes */
6004 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
6005 while (nextbno
!= NULLFSBLOCK
) {
6006 if ((error
= xfs_btree_read_bufl(mp
, tp
, nextbno
,
6007 0, &nbp
, XFS_BMAP_BTREE_REF
)))
6010 nextblock
= XFS_BUF_TO_BLOCK(nbp
);
6011 nextbno
= be64_to_cpu(nextblock
->bb_u
.l
.bb_rightsib
);
6012 xfs_trans_brelse(tp
, nbp
);
6015 /* Dive to the next level */
6016 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
6017 bno
= be64_to_cpu(*pp
);
6018 if (unlikely((error
=
6019 xfs_bmap_count_tree(mp
, tp
, ifp
, bno
, level
, count
)) < 0)) {
6020 xfs_trans_brelse(tp
, bp
);
6021 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6022 XFS_ERRLEVEL_LOW
, mp
);
6023 return XFS_ERROR(EFSCORRUPTED
);
6025 xfs_trans_brelse(tp
, bp
);
6027 /* count all level 1 nodes and their leaves */
6029 nextbno
= be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
6030 numrecs
= be16_to_cpu(block
->bb_numrecs
);
6031 xfs_bmap_disk_count_leaves(mp
, block
, numrecs
, count
);
6032 xfs_trans_brelse(tp
, bp
);
6033 if (nextbno
== NULLFSBLOCK
)
6036 if ((error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
6037 XFS_BMAP_BTREE_REF
)))
6040 block
= XFS_BUF_TO_BLOCK(bp
);
6047 * Count leaf blocks given a range of extent records.
6050 xfs_bmap_count_leaves(
6058 for (b
= 0; b
< numrecs
; b
++) {
6059 xfs_bmbt_rec_host_t
*frp
= xfs_iext_get_ext(ifp
, idx
+ b
);
6060 *count
+= xfs_bmbt_get_blockcount(frp
);
6065 * Count leaf blocks given a range of extent records originally
6069 xfs_bmap_disk_count_leaves(
6070 struct xfs_mount
*mp
,
6071 struct xfs_btree_block
*block
,
6076 xfs_bmbt_rec_t
*frp
;
6078 for (b
= 1; b
<= numrecs
; b
++) {
6079 frp
= XFS_BMBT_REC_ADDR(mp
, block
, b
);
6080 *count
+= xfs_bmbt_disk_get_blockcount(frp
);
6085 * dead simple method of punching delalyed allocation blocks from a range in
6086 * the inode. Walks a block at a time so will be slow, but is only executed in
6087 * rare error cases so the overhead is not critical. This will alays punch out
6088 * both the start and end blocks, even if the ranges only partially overlap
6089 * them, so it is up to the caller to ensure that partial blocks are not
6093 xfs_bmap_punch_delalloc_range(
6094 struct xfs_inode
*ip
,
6095 xfs_fileoff_t start_fsb
,
6096 xfs_fileoff_t length
)
6098 xfs_fileoff_t remaining
= length
;
6101 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
6105 xfs_bmbt_irec_t imap
;
6107 xfs_fsblock_t firstblock
;
6108 xfs_bmap_free_t flist
;
6111 * Map the range first and check that it is a delalloc extent
6112 * before trying to unmap the range. Otherwise we will be
6113 * trying to remove a real extent (which requires a
6114 * transaction) or a hole, which is probably a bad idea...
6116 error
= xfs_bmapi_read(ip
, start_fsb
, 1, &imap
, &nimaps
,
6120 /* something screwed, just bail */
6121 if (!XFS_FORCED_SHUTDOWN(ip
->i_mount
)) {
6122 xfs_alert(ip
->i_mount
,
6123 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6124 ip
->i_ino
, start_fsb
);
6132 if (imap
.br_startblock
!= DELAYSTARTBLOCK
) {
6133 /* been converted, ignore */
6136 WARN_ON(imap
.br_blockcount
== 0);
6139 * Note: while we initialise the firstblock/flist pair, they
6140 * should never be used because blocks should never be
6141 * allocated or freed for a delalloc extent and hence we need
6142 * don't cancel or finish them after the xfs_bunmapi() call.
6144 xfs_bmap_init(&flist
, &firstblock
);
6145 error
= xfs_bunmapi(NULL
, ip
, start_fsb
, 1, 0, 1, &firstblock
,
6150 ASSERT(!flist
.xbf_count
&& !flist
.xbf_first
);
6154 } while(remaining
> 0);