2 * Copyright (c) 2000-2005 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
21 extern xfs_zone_t
*xfs_ifork_zone
;
22 xfs_zone_t
*xfs_bmap_free_item_zone
;
24 STATIC
int /* error */
25 xfs_bmap_add_attrfork_btree(
26 xfs_trans_t
*tp
, /* transaction pointer */
27 xfs_inode_t
*ip
, /* incore inode pointer */
28 xfs_fsblock_t
*firstblock
, /* first block allocated */
29 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
30 int *flags
) /* inode logging flags */
32 xfs_btree_cur_t
*cur
; /* btree cursor */
33 int error
; /* error return value */
34 xfs_mount_t
*mp
; /* file system mount struct */
35 int stat
; /* newroot status */
38 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
39 *flags
|= XFS_ILOG_DBROOT
;
41 cur
= xfs_btree_init_cursor(mp
, tp
, NULL
, 0, XFS_BTNUM_BMAP
, ip
,
43 cur
->bc_private
.b
.flist
= flist
;
44 cur
->bc_private
.b
.firstblock
= *firstblock
;
45 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
47 ASSERT(stat
== 1); /* must be at least one entry */
48 if ((error
= xfs_bmbt_newroot(cur
, flags
, &stat
)))
51 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
52 return XFS_ERROR(ENOSPC
);
54 *firstblock
= cur
->bc_private
.b
.firstblock
;
55 cur
->bc_private
.b
.allocated
= 0;
56 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
60 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
65 * Called from xfs_bmap_add_attrfork to handle extents format files.
67 STATIC
int /* error */
68 xfs_bmap_add_attrfork_extents(
69 xfs_trans_t
*tp
, /* transaction pointer */
70 xfs_inode_t
*ip
, /* incore inode pointer */
71 xfs_fsblock_t
*firstblock
, /* first block allocated */
72 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
73 int *flags
) /* inode logging flags */
75 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
76 int error
; /* error return value */
78 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
81 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
, &cur
, 0,
82 flags
, XFS_DATA_FORK
);
84 cur
->bc_private
.b
.allocated
= 0;
85 xfs_btree_del_cursor(cur
,
86 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
92 * Called from xfs_bmap_add_attrfork to handle local format files.
94 STATIC
int /* error */
95 xfs_bmap_add_attrfork_local(
96 xfs_trans_t
*tp
, /* transaction pointer */
97 xfs_inode_t
*ip
, /* incore inode pointer */
98 xfs_fsblock_t
*firstblock
, /* first block allocated */
99 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
100 int *flags
) /* inode logging flags */
102 xfs_da_args_t dargs
; /* args for dir/attr code */
103 int error
; /* error return value */
104 xfs_mount_t
*mp
; /* mount structure pointer */
106 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
108 if ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
) {
110 memset(&dargs
, 0, sizeof(dargs
));
112 dargs
.firstblock
= firstblock
;
114 dargs
.total
= mp
->m_dirblkfsbs
;
115 dargs
.whichfork
= XFS_DATA_FORK
;
117 error
= XFS_DIR_SHORTFORM_TO_SINGLE(mp
, &dargs
);
119 error
= xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1, flags
,
125 * Called by xfs_bmapi to update extent list structure and the btree
126 * after allocating space (or doing a delayed allocation).
128 STATIC
int /* error */
130 xfs_inode_t
*ip
, /* incore inode pointer */
131 xfs_extnum_t idx
, /* extent number to update/insert */
132 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
133 xfs_bmbt_irec_t
*new, /* new data to put in extent list */
134 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
135 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
136 int *logflagsp
, /* inode logging flags */
137 int whichfork
, /* data or attr fork */
138 int rsvd
) /* OK to use reserved data blocks */
140 xfs_btree_cur_t
*cur
; /* btree cursor or null */
141 xfs_filblks_t da_new
; /* new count del alloc blocks used */
142 xfs_filblks_t da_old
; /* old count del alloc blocks used */
143 int error
; /* error return value */
144 #ifdef XFS_BMAP_TRACE
145 static char fname
[] = "xfs_bmap_add_extent";
147 xfs_ifork_t
*ifp
; /* inode fork ptr */
148 int logflags
; /* returned value */
149 xfs_extnum_t nextents
; /* number of extents in file now */
151 XFS_STATS_INC(xs_add_exlist
);
153 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
154 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
155 ASSERT(idx
<= nextents
);
159 * This is the first extent added to a new/empty file.
160 * Special case this one, so other routines get to assume there are
161 * already extents in the list.
164 xfs_bmap_trace_insert(fname
, "insert empty", ip
, 0, 1, new,
166 xfs_bmap_insert_exlist(ip
, 0, 1, new, whichfork
);
169 if (!ISNULLSTARTBLOCK(new->br_startblock
)) {
170 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
171 logflags
= XFS_ILOG_CORE
| XFS_ILOG_FEXT(whichfork
);
176 * Any kind of new delayed allocation goes here.
178 else if (ISNULLSTARTBLOCK(new->br_startblock
)) {
180 ASSERT((cur
->bc_private
.b
.flags
&
181 XFS_BTCUR_BPRV_WASDEL
) == 0);
182 if ((error
= xfs_bmap_add_extent_hole_delay(ip
, idx
, cur
, new,
187 * Real allocation off the end of the file.
189 else if (idx
== nextents
) {
191 ASSERT((cur
->bc_private
.b
.flags
&
192 XFS_BTCUR_BPRV_WASDEL
) == 0);
193 if ((error
= xfs_bmap_add_extent_hole_real(ip
, idx
, cur
, new,
194 &logflags
, whichfork
)))
197 xfs_bmbt_irec_t prev
; /* old extent at offset idx */
200 * Get the record referred to by idx.
202 xfs_bmbt_get_all(&ifp
->if_u1
.if_extents
[idx
], &prev
);
204 * If it's a real allocation record, and the new allocation ends
205 * after the start of the referred to record, then we're filling
206 * in a delayed or unwritten allocation with a real one, or
207 * converting real back to unwritten.
209 if (!ISNULLSTARTBLOCK(new->br_startblock
) &&
210 new->br_startoff
+ new->br_blockcount
> prev
.br_startoff
) {
211 if (prev
.br_state
!= XFS_EXT_UNWRITTEN
&&
212 ISNULLSTARTBLOCK(prev
.br_startblock
)) {
213 da_old
= STARTBLOCKVAL(prev
.br_startblock
);
215 ASSERT(cur
->bc_private
.b
.flags
&
216 XFS_BTCUR_BPRV_WASDEL
);
217 if ((error
= xfs_bmap_add_extent_delay_real(ip
,
218 idx
, &cur
, new, &da_new
, first
, flist
,
221 } else if (new->br_state
== XFS_EXT_NORM
) {
222 ASSERT(new->br_state
== XFS_EXT_NORM
);
223 if ((error
= xfs_bmap_add_extent_unwritten_real(
224 ip
, idx
, &cur
, new, &logflags
)))
227 ASSERT(new->br_state
== XFS_EXT_UNWRITTEN
);
228 if ((error
= xfs_bmap_add_extent_unwritten_real(
229 ip
, idx
, &cur
, new, &logflags
)))
232 ASSERT(*curp
== cur
|| *curp
== NULL
);
235 * Otherwise we're filling in a hole with an allocation.
239 ASSERT((cur
->bc_private
.b
.flags
&
240 XFS_BTCUR_BPRV_WASDEL
) == 0);
241 if ((error
= xfs_bmap_add_extent_hole_real(ip
, idx
, cur
,
242 new, &logflags
, whichfork
)))
247 ASSERT(*curp
== cur
|| *curp
== NULL
);
249 * Convert to a btree if necessary.
251 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
252 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ifp
->if_ext_max
) {
253 int tmp_logflags
; /* partial log flag return val */
256 error
= xfs_bmap_extents_to_btree(ip
->i_transp
, ip
, first
,
257 flist
, &cur
, da_old
> 0, &tmp_logflags
, whichfork
);
258 logflags
|= tmp_logflags
;
263 * Adjust for changes in reserved delayed indirect blocks.
264 * Nothing to do for disk quotas here.
266 if (da_old
|| da_new
) {
271 nblks
+= cur
->bc_private
.b
.allocated
;
272 ASSERT(nblks
<= da_old
);
274 xfs_mod_incore_sb(ip
->i_mount
, XFS_SBS_FDBLOCKS
,
275 (int)(da_old
- nblks
), rsvd
);
278 * Clear out the allocated field, done with it now in any case.
281 cur
->bc_private
.b
.allocated
= 0;
287 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
289 *logflagsp
= logflags
;
294 * Called by xfs_bmap_add_extent to handle cases converting a delayed
295 * allocation to a real allocation.
297 STATIC
int /* error */
298 xfs_bmap_add_extent_delay_real(
299 xfs_inode_t
*ip
, /* incore inode pointer */
300 xfs_extnum_t idx
, /* extent number to update/insert */
301 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
302 xfs_bmbt_irec_t
*new, /* new data to put in extent list */
303 xfs_filblks_t
*dnew
, /* new delayed-alloc indirect blocks */
304 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
305 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
306 int *logflagsp
, /* inode logging flags */
307 int rsvd
) /* OK to use reserved data block allocation */
309 xfs_bmbt_rec_t
*base
; /* base of extent entry list */
310 xfs_btree_cur_t
*cur
; /* btree cursor */
311 int diff
; /* temp value */
312 xfs_bmbt_rec_t
*ep
; /* extent entry for idx */
313 int error
; /* error return value */
314 #ifdef XFS_BMAP_TRACE
315 static char fname
[] = "xfs_bmap_add_extent_delay_real";
317 int i
; /* temp state */
318 xfs_fileoff_t new_endoff
; /* end offset of new entry */
319 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
320 /* left is 0, right is 1, prev is 2 */
321 int rval
=0; /* return value (logging flags) */
322 int state
= 0;/* state bits, accessed thru macros */
323 xfs_filblks_t temp
; /* value for dnew calculations */
324 xfs_filblks_t temp2
; /* value for dnew calculations */
325 int tmp_rval
; /* partial logging flags */
326 enum { /* bit number definitions for state */
327 LEFT_CONTIG
, RIGHT_CONTIG
,
328 LEFT_FILLING
, RIGHT_FILLING
,
329 LEFT_DELAY
, RIGHT_DELAY
,
330 LEFT_VALID
, RIGHT_VALID
336 #define MASK(b) (1 << (b))
337 #define MASK2(a,b) (MASK(a) | MASK(b))
338 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
339 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
340 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
341 #define STATE_TEST(b) (state & MASK(b))
342 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
343 ((state &= ~MASK(b)), 0))
344 #define SWITCH_STATE \
345 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
348 * Set up a bunch of variables to make the tests simpler.
351 base
= ip
->i_df
.if_u1
.if_extents
;
353 xfs_bmbt_get_all(ep
, &PREV
);
354 new_endoff
= new->br_startoff
+ new->br_blockcount
;
355 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
356 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
358 * Set flags determining what part of the previous delayed allocation
359 * extent is being replaced by a real allocation.
361 STATE_SET(LEFT_FILLING
, PREV
.br_startoff
== new->br_startoff
);
362 STATE_SET(RIGHT_FILLING
,
363 PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
);
365 * Check and set flags if this segment has a left neighbor.
366 * Don't set contiguous if the combined extent would be too large.
368 if (STATE_SET_TEST(LEFT_VALID
, idx
> 0)) {
369 xfs_bmbt_get_all(ep
- 1, &LEFT
);
370 STATE_SET(LEFT_DELAY
, ISNULLSTARTBLOCK(LEFT
.br_startblock
));
372 STATE_SET(LEFT_CONTIG
,
373 STATE_TEST(LEFT_VALID
) && !STATE_TEST(LEFT_DELAY
) &&
374 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
375 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
376 LEFT
.br_state
== new->br_state
&&
377 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
);
379 * Check and set flags if this segment has a right neighbor.
380 * Don't set contiguous if the combined extent would be too large.
381 * Also check for all-three-contiguous being too large.
383 if (STATE_SET_TEST(RIGHT_VALID
,
385 ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1)) {
386 xfs_bmbt_get_all(ep
+ 1, &RIGHT
);
387 STATE_SET(RIGHT_DELAY
, ISNULLSTARTBLOCK(RIGHT
.br_startblock
));
389 STATE_SET(RIGHT_CONTIG
,
390 STATE_TEST(RIGHT_VALID
) && !STATE_TEST(RIGHT_DELAY
) &&
391 new_endoff
== RIGHT
.br_startoff
&&
392 new->br_startblock
+ new->br_blockcount
==
393 RIGHT
.br_startblock
&&
394 new->br_state
== RIGHT
.br_state
&&
395 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
396 ((state
& MASK3(LEFT_CONTIG
, LEFT_FILLING
, RIGHT_FILLING
)) !=
397 MASK3(LEFT_CONTIG
, LEFT_FILLING
, RIGHT_FILLING
) ||
398 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
402 * Switch out based on the FILLING and CONTIG state bits.
404 switch (SWITCH_STATE
) {
406 case MASK4(LEFT_FILLING
, RIGHT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
408 * Filling in all of a previously delayed allocation extent.
409 * The left and right neighbors are both contiguous with new.
411 xfs_bmap_trace_pre_update(fname
, "LF|RF|LC|RC", ip
, idx
- 1,
413 xfs_bmbt_set_blockcount(ep
- 1,
414 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
415 RIGHT
.br_blockcount
);
416 xfs_bmap_trace_post_update(fname
, "LF|RF|LC|RC", ip
, idx
- 1,
418 xfs_bmap_trace_delete(fname
, "LF|RF|LC|RC", ip
, idx
, 2,
420 xfs_bmap_delete_exlist(ip
, idx
, 2, XFS_DATA_FORK
);
421 ip
->i_df
.if_lastex
= idx
- 1;
422 ip
->i_d
.di_nextents
--;
424 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
426 rval
= XFS_ILOG_CORE
;
427 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
429 RIGHT
.br_blockcount
, &i
)))
432 if ((error
= xfs_bmbt_delete(cur
, &i
)))
435 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
438 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
442 RIGHT
.br_blockcount
, LEFT
.br_state
)))
448 case MASK3(LEFT_FILLING
, RIGHT_FILLING
, LEFT_CONTIG
):
450 * Filling in all of a previously delayed allocation extent.
451 * The left neighbor is contiguous, the right is not.
453 xfs_bmap_trace_pre_update(fname
, "LF|RF|LC", ip
, idx
- 1,
455 xfs_bmbt_set_blockcount(ep
- 1,
456 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
457 xfs_bmap_trace_post_update(fname
, "LF|RF|LC", ip
, idx
- 1,
459 ip
->i_df
.if_lastex
= idx
- 1;
460 xfs_bmap_trace_delete(fname
, "LF|RF|LC", ip
, idx
, 1,
462 xfs_bmap_delete_exlist(ip
, idx
, 1, XFS_DATA_FORK
);
464 rval
= XFS_ILOG_DEXT
;
467 if ((error
= xfs_bmbt_lookup_eq(cur
, LEFT
.br_startoff
,
468 LEFT
.br_startblock
, LEFT
.br_blockcount
,
472 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
475 PREV
.br_blockcount
, LEFT
.br_state
)))
481 case MASK3(LEFT_FILLING
, RIGHT_FILLING
, RIGHT_CONTIG
):
483 * Filling in all of a previously delayed allocation extent.
484 * The right neighbor is contiguous, the left is not.
486 xfs_bmap_trace_pre_update(fname
, "LF|RF|RC", ip
, idx
,
488 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
489 xfs_bmbt_set_blockcount(ep
,
490 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
491 xfs_bmap_trace_post_update(fname
, "LF|RF|RC", ip
, idx
,
493 ip
->i_df
.if_lastex
= idx
;
494 xfs_bmap_trace_delete(fname
, "LF|RF|RC", ip
, idx
+ 1, 1,
496 xfs_bmap_delete_exlist(ip
, idx
+ 1, 1, XFS_DATA_FORK
);
498 rval
= XFS_ILOG_DEXT
;
501 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
503 RIGHT
.br_blockcount
, &i
)))
506 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
509 RIGHT
.br_blockcount
, PREV
.br_state
)))
515 case MASK2(LEFT_FILLING
, RIGHT_FILLING
):
517 * Filling in all of a previously delayed allocation extent.
518 * Neither the left nor right neighbors are contiguous with
521 xfs_bmap_trace_pre_update(fname
, "LF|RF", ip
, idx
,
523 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
524 xfs_bmap_trace_post_update(fname
, "LF|RF", ip
, idx
,
526 ip
->i_df
.if_lastex
= idx
;
527 ip
->i_d
.di_nextents
++;
529 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
531 rval
= XFS_ILOG_CORE
;
532 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
533 new->br_startblock
, new->br_blockcount
,
537 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
538 if ((error
= xfs_bmbt_insert(cur
, &i
)))
545 case MASK2(LEFT_FILLING
, LEFT_CONTIG
):
547 * Filling in the first part of a previous delayed allocation.
548 * The left neighbor is contiguous.
550 xfs_bmap_trace_pre_update(fname
, "LF|LC", ip
, idx
- 1,
552 xfs_bmbt_set_blockcount(ep
- 1,
553 LEFT
.br_blockcount
+ new->br_blockcount
);
554 xfs_bmbt_set_startoff(ep
,
555 PREV
.br_startoff
+ new->br_blockcount
);
556 xfs_bmap_trace_post_update(fname
, "LF|LC", ip
, idx
- 1,
558 temp
= PREV
.br_blockcount
- new->br_blockcount
;
559 xfs_bmap_trace_pre_update(fname
, "LF|LC", ip
, idx
,
561 xfs_bmbt_set_blockcount(ep
, temp
);
562 ip
->i_df
.if_lastex
= idx
- 1;
564 rval
= XFS_ILOG_DEXT
;
567 if ((error
= xfs_bmbt_lookup_eq(cur
, LEFT
.br_startoff
,
568 LEFT
.br_startblock
, LEFT
.br_blockcount
,
572 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
579 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
580 STARTBLOCKVAL(PREV
.br_startblock
));
581 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
582 xfs_bmap_trace_post_update(fname
, "LF|LC", ip
, idx
,
587 case MASK(LEFT_FILLING
):
589 * Filling in the first part of a previous delayed allocation.
590 * The left neighbor is not contiguous.
592 xfs_bmap_trace_pre_update(fname
, "LF", ip
, idx
, XFS_DATA_FORK
);
593 xfs_bmbt_set_startoff(ep
, new_endoff
);
594 temp
= PREV
.br_blockcount
- new->br_blockcount
;
595 xfs_bmbt_set_blockcount(ep
, temp
);
596 xfs_bmap_trace_insert(fname
, "LF", ip
, idx
, 1, new, NULL
,
598 xfs_bmap_insert_exlist(ip
, idx
, 1, new, XFS_DATA_FORK
);
599 ip
->i_df
.if_lastex
= idx
;
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
,
610 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
611 if ((error
= xfs_bmbt_insert(cur
, &i
)))
615 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
616 ip
->i_d
.di_nextents
> ip
->i_df
.if_ext_max
) {
617 error
= xfs_bmap_extents_to_btree(ip
->i_transp
, ip
,
618 first
, flist
, &cur
, 1, &tmp_rval
,
624 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
625 STARTBLOCKVAL(PREV
.br_startblock
) -
626 (cur
? cur
->bc_private
.b
.allocated
: 0));
627 base
= ip
->i_df
.if_u1
.if_extents
;
629 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
630 xfs_bmap_trace_post_update(fname
, "LF", ip
, idx
+ 1,
635 case MASK2(RIGHT_FILLING
, RIGHT_CONTIG
):
637 * Filling in the last part of a previous delayed allocation.
638 * The right neighbor is contiguous with the new allocation.
640 temp
= PREV
.br_blockcount
- new->br_blockcount
;
641 xfs_bmap_trace_pre_update(fname
, "RF|RC", ip
, idx
,
643 xfs_bmap_trace_pre_update(fname
, "RF|RC", ip
, idx
+ 1,
645 xfs_bmbt_set_blockcount(ep
, temp
);
646 xfs_bmbt_set_allf(ep
+ 1, new->br_startoff
, new->br_startblock
,
647 new->br_blockcount
+ RIGHT
.br_blockcount
,
649 xfs_bmap_trace_post_update(fname
, "RF|RC", ip
, idx
+ 1,
651 ip
->i_df
.if_lastex
= idx
+ 1;
653 rval
= XFS_ILOG_DEXT
;
656 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
658 RIGHT
.br_blockcount
, &i
)))
661 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
668 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
669 STARTBLOCKVAL(PREV
.br_startblock
));
670 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
671 xfs_bmap_trace_post_update(fname
, "RF|RC", ip
, idx
,
676 case MASK(RIGHT_FILLING
):
678 * Filling in the last part of a previous delayed allocation.
679 * The right neighbor is not contiguous.
681 temp
= PREV
.br_blockcount
- new->br_blockcount
;
682 xfs_bmap_trace_pre_update(fname
, "RF", ip
, idx
, XFS_DATA_FORK
);
683 xfs_bmbt_set_blockcount(ep
, temp
);
684 xfs_bmap_trace_insert(fname
, "RF", ip
, idx
+ 1, 1,
685 new, NULL
, XFS_DATA_FORK
);
686 xfs_bmap_insert_exlist(ip
, idx
+ 1, 1, new, XFS_DATA_FORK
);
687 ip
->i_df
.if_lastex
= idx
+ 1;
688 ip
->i_d
.di_nextents
++;
690 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
692 rval
= XFS_ILOG_CORE
;
693 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
694 new->br_startblock
, new->br_blockcount
,
698 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
699 if ((error
= xfs_bmbt_insert(cur
, &i
)))
703 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
704 ip
->i_d
.di_nextents
> ip
->i_df
.if_ext_max
) {
705 error
= xfs_bmap_extents_to_btree(ip
->i_transp
, ip
,
706 first
, flist
, &cur
, 1, &tmp_rval
,
712 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
713 STARTBLOCKVAL(PREV
.br_startblock
) -
714 (cur
? cur
->bc_private
.b
.allocated
: 0));
715 base
= ip
->i_df
.if_u1
.if_extents
;
717 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
718 xfs_bmap_trace_post_update(fname
, "RF", ip
, idx
, XFS_DATA_FORK
);
724 * Filling in the middle part of a previous delayed allocation.
725 * Contiguity is impossible here.
726 * This case is avoided almost all the time.
728 temp
= new->br_startoff
- PREV
.br_startoff
;
729 xfs_bmap_trace_pre_update(fname
, "0", ip
, idx
, XFS_DATA_FORK
);
730 xfs_bmbt_set_blockcount(ep
, temp
);
732 r
[1].br_startoff
= new_endoff
;
733 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
734 r
[1].br_blockcount
= temp2
;
735 xfs_bmap_trace_insert(fname
, "0", ip
, idx
+ 1, 2, &r
[0], &r
[1],
737 xfs_bmap_insert_exlist(ip
, idx
+ 1, 2, &r
[0], XFS_DATA_FORK
);
738 ip
->i_df
.if_lastex
= idx
+ 1;
739 ip
->i_d
.di_nextents
++;
741 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
743 rval
= XFS_ILOG_CORE
;
744 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
745 new->br_startblock
, new->br_blockcount
,
749 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
750 if ((error
= xfs_bmbt_insert(cur
, &i
)))
754 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
755 ip
->i_d
.di_nextents
> ip
->i_df
.if_ext_max
) {
756 error
= xfs_bmap_extents_to_btree(ip
->i_transp
, ip
,
757 first
, flist
, &cur
, 1, &tmp_rval
,
763 temp
= xfs_bmap_worst_indlen(ip
, temp
);
764 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
765 diff
= (int)(temp
+ temp2
- STARTBLOCKVAL(PREV
.br_startblock
) -
766 (cur
? cur
->bc_private
.b
.allocated
: 0));
768 xfs_mod_incore_sb(ip
->i_mount
, XFS_SBS_FDBLOCKS
, -diff
, rsvd
)) {
770 * Ick gross gag me with a spoon.
772 ASSERT(0); /* want to see if this ever happens! */
778 !xfs_mod_incore_sb(ip
->i_mount
,
779 XFS_SBS_FDBLOCKS
, -diff
, rsvd
))
786 !xfs_mod_incore_sb(ip
->i_mount
,
787 XFS_SBS_FDBLOCKS
, -diff
, rsvd
))
792 base
= ip
->i_df
.if_u1
.if_extents
;
794 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
795 xfs_bmap_trace_post_update(fname
, "0", ip
, idx
, XFS_DATA_FORK
);
796 xfs_bmap_trace_pre_update(fname
, "0", ip
, idx
+ 2,
798 xfs_bmbt_set_startblock(ep
+ 2, NULLSTARTBLOCK((int)temp2
));
799 xfs_bmap_trace_post_update(fname
, "0", ip
, idx
+ 2,
801 *dnew
= temp
+ temp2
;
804 case MASK3(LEFT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
805 case MASK3(RIGHT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
806 case MASK2(LEFT_FILLING
, RIGHT_CONTIG
):
807 case MASK2(RIGHT_FILLING
, LEFT_CONTIG
):
808 case MASK2(LEFT_CONTIG
, RIGHT_CONTIG
):
809 case MASK(LEFT_CONTIG
):
810 case MASK(RIGHT_CONTIG
):
812 * These cases are all impossible.
829 #undef STATE_SET_TEST
834 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
835 * allocation to a real allocation or vice versa.
837 STATIC
int /* error */
838 xfs_bmap_add_extent_unwritten_real(
839 xfs_inode_t
*ip
, /* incore inode pointer */
840 xfs_extnum_t idx
, /* extent number to update/insert */
841 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
842 xfs_bmbt_irec_t
*new, /* new data to put in extent list */
843 int *logflagsp
) /* inode logging flags */
845 xfs_bmbt_rec_t
*base
; /* base of extent entry list */
846 xfs_btree_cur_t
*cur
; /* btree cursor */
847 xfs_bmbt_rec_t
*ep
; /* extent entry for idx */
848 int error
; /* error return value */
849 #ifdef XFS_BMAP_TRACE
850 static char fname
[] = "xfs_bmap_add_extent_unwritten_real";
852 int i
; /* temp state */
853 xfs_fileoff_t new_endoff
; /* end offset of new entry */
854 xfs_exntst_t newext
; /* new extent state */
855 xfs_exntst_t oldext
; /* old extent state */
856 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
857 /* left is 0, right is 1, prev is 2 */
858 int rval
=0; /* return value (logging flags) */
859 int state
= 0;/* state bits, accessed thru macros */
860 enum { /* bit number definitions for state */
861 LEFT_CONTIG
, RIGHT_CONTIG
,
862 LEFT_FILLING
, RIGHT_FILLING
,
863 LEFT_DELAY
, RIGHT_DELAY
,
864 LEFT_VALID
, RIGHT_VALID
870 #define MASK(b) (1 << (b))
871 #define MASK2(a,b) (MASK(a) | MASK(b))
872 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
873 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
874 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
875 #define STATE_TEST(b) (state & MASK(b))
876 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
877 ((state &= ~MASK(b)), 0))
878 #define SWITCH_STATE \
879 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
882 * Set up a bunch of variables to make the tests simpler.
886 base
= ip
->i_df
.if_u1
.if_extents
;
888 xfs_bmbt_get_all(ep
, &PREV
);
889 newext
= new->br_state
;
890 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
891 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
892 ASSERT(PREV
.br_state
== oldext
);
893 new_endoff
= new->br_startoff
+ new->br_blockcount
;
894 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
895 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
897 * Set flags determining what part of the previous oldext allocation
898 * extent is being replaced by a newext allocation.
900 STATE_SET(LEFT_FILLING
, PREV
.br_startoff
== new->br_startoff
);
901 STATE_SET(RIGHT_FILLING
,
902 PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
);
904 * Check and set flags if this segment has a left neighbor.
905 * Don't set contiguous if the combined extent would be too large.
907 if (STATE_SET_TEST(LEFT_VALID
, idx
> 0)) {
908 xfs_bmbt_get_all(ep
- 1, &LEFT
);
909 STATE_SET(LEFT_DELAY
, ISNULLSTARTBLOCK(LEFT
.br_startblock
));
911 STATE_SET(LEFT_CONTIG
,
912 STATE_TEST(LEFT_VALID
) && !STATE_TEST(LEFT_DELAY
) &&
913 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
914 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
915 LEFT
.br_state
== newext
&&
916 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
);
918 * Check and set flags if this segment has a right neighbor.
919 * Don't set contiguous if the combined extent would be too large.
920 * Also check for all-three-contiguous being too large.
922 if (STATE_SET_TEST(RIGHT_VALID
,
924 ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1)) {
925 xfs_bmbt_get_all(ep
+ 1, &RIGHT
);
926 STATE_SET(RIGHT_DELAY
, ISNULLSTARTBLOCK(RIGHT
.br_startblock
));
928 STATE_SET(RIGHT_CONTIG
,
929 STATE_TEST(RIGHT_VALID
) && !STATE_TEST(RIGHT_DELAY
) &&
930 new_endoff
== RIGHT
.br_startoff
&&
931 new->br_startblock
+ new->br_blockcount
==
932 RIGHT
.br_startblock
&&
933 newext
== RIGHT
.br_state
&&
934 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
935 ((state
& MASK3(LEFT_CONTIG
, LEFT_FILLING
, RIGHT_FILLING
)) !=
936 MASK3(LEFT_CONTIG
, LEFT_FILLING
, RIGHT_FILLING
) ||
937 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
940 * Switch out based on the FILLING and CONTIG state bits.
942 switch (SWITCH_STATE
) {
944 case MASK4(LEFT_FILLING
, RIGHT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
946 * Setting all of a previous oldext extent to newext.
947 * The left and right neighbors are both contiguous with new.
949 xfs_bmap_trace_pre_update(fname
, "LF|RF|LC|RC", ip
, idx
- 1,
951 xfs_bmbt_set_blockcount(ep
- 1,
952 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
953 RIGHT
.br_blockcount
);
954 xfs_bmap_trace_post_update(fname
, "LF|RF|LC|RC", ip
, idx
- 1,
956 xfs_bmap_trace_delete(fname
, "LF|RF|LC|RC", ip
, idx
, 2,
958 xfs_bmap_delete_exlist(ip
, idx
, 2, XFS_DATA_FORK
);
959 ip
->i_df
.if_lastex
= idx
- 1;
960 ip
->i_d
.di_nextents
-= 2;
962 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
964 rval
= XFS_ILOG_CORE
;
965 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
967 RIGHT
.br_blockcount
, &i
)))
970 if ((error
= xfs_bmbt_delete(cur
, &i
)))
973 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
976 if ((error
= xfs_bmbt_delete(cur
, &i
)))
979 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
982 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
984 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
985 RIGHT
.br_blockcount
, LEFT
.br_state
)))
990 case MASK3(LEFT_FILLING
, RIGHT_FILLING
, LEFT_CONTIG
):
992 * Setting all of a previous oldext extent to newext.
993 * The left neighbor is contiguous, the right is not.
995 xfs_bmap_trace_pre_update(fname
, "LF|RF|LC", ip
, idx
- 1,
997 xfs_bmbt_set_blockcount(ep
- 1,
998 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
999 xfs_bmap_trace_post_update(fname
, "LF|RF|LC", ip
, idx
- 1,
1001 ip
->i_df
.if_lastex
= idx
- 1;
1002 xfs_bmap_trace_delete(fname
, "LF|RF|LC", ip
, idx
, 1,
1004 xfs_bmap_delete_exlist(ip
, idx
, 1, XFS_DATA_FORK
);
1005 ip
->i_d
.di_nextents
--;
1007 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1009 rval
= XFS_ILOG_CORE
;
1010 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1011 PREV
.br_startblock
, PREV
.br_blockcount
,
1015 if ((error
= xfs_bmbt_delete(cur
, &i
)))
1018 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
1021 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
1023 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
1029 case MASK3(LEFT_FILLING
, RIGHT_FILLING
, RIGHT_CONTIG
):
1031 * Setting all of a previous oldext extent to newext.
1032 * The right neighbor is contiguous, the left is not.
1034 xfs_bmap_trace_pre_update(fname
, "LF|RF|RC", ip
, idx
,
1036 xfs_bmbt_set_blockcount(ep
,
1037 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
1038 xfs_bmbt_set_state(ep
, newext
);
1039 xfs_bmap_trace_post_update(fname
, "LF|RF|RC", ip
, idx
,
1041 ip
->i_df
.if_lastex
= idx
;
1042 xfs_bmap_trace_delete(fname
, "LF|RF|RC", ip
, idx
+ 1, 1,
1044 xfs_bmap_delete_exlist(ip
, idx
+ 1, 1, XFS_DATA_FORK
);
1045 ip
->i_d
.di_nextents
--;
1047 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1049 rval
= XFS_ILOG_CORE
;
1050 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
1051 RIGHT
.br_startblock
,
1052 RIGHT
.br_blockcount
, &i
)))
1055 if ((error
= xfs_bmbt_delete(cur
, &i
)))
1058 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
1061 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
1063 new->br_blockcount
+ RIGHT
.br_blockcount
,
1069 case MASK2(LEFT_FILLING
, RIGHT_FILLING
):
1071 * Setting all of a previous oldext extent to newext.
1072 * Neither the left nor right neighbors are contiguous with
1075 xfs_bmap_trace_pre_update(fname
, "LF|RF", ip
, idx
,
1077 xfs_bmbt_set_state(ep
, newext
);
1078 xfs_bmap_trace_post_update(fname
, "LF|RF", ip
, idx
,
1080 ip
->i_df
.if_lastex
= idx
;
1082 rval
= XFS_ILOG_DEXT
;
1085 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1086 new->br_startblock
, new->br_blockcount
,
1090 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
1091 new->br_startblock
, new->br_blockcount
,
1097 case MASK2(LEFT_FILLING
, LEFT_CONTIG
):
1099 * Setting the first part of a previous oldext extent to newext.
1100 * The left neighbor is contiguous.
1102 xfs_bmap_trace_pre_update(fname
, "LF|LC", ip
, idx
- 1,
1104 xfs_bmbt_set_blockcount(ep
- 1,
1105 LEFT
.br_blockcount
+ new->br_blockcount
);
1106 xfs_bmbt_set_startoff(ep
,
1107 PREV
.br_startoff
+ new->br_blockcount
);
1108 xfs_bmap_trace_post_update(fname
, "LF|LC", ip
, idx
- 1,
1110 xfs_bmap_trace_pre_update(fname
, "LF|LC", ip
, idx
,
1112 xfs_bmbt_set_startblock(ep
,
1113 new->br_startblock
+ new->br_blockcount
);
1114 xfs_bmbt_set_blockcount(ep
,
1115 PREV
.br_blockcount
- new->br_blockcount
);
1116 xfs_bmap_trace_post_update(fname
, "LF|LC", ip
, idx
,
1118 ip
->i_df
.if_lastex
= idx
- 1;
1120 rval
= XFS_ILOG_DEXT
;
1123 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1124 PREV
.br_startblock
, PREV
.br_blockcount
,
1128 if ((error
= xfs_bmbt_update(cur
,
1129 PREV
.br_startoff
+ new->br_blockcount
,
1130 PREV
.br_startblock
+ new->br_blockcount
,
1131 PREV
.br_blockcount
- new->br_blockcount
,
1134 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
1136 if (xfs_bmbt_update(cur
, LEFT
.br_startoff
,
1138 LEFT
.br_blockcount
+ new->br_blockcount
,
1144 case MASK(LEFT_FILLING
):
1146 * Setting the first part of a previous oldext extent to newext.
1147 * The left neighbor is not contiguous.
1149 xfs_bmap_trace_pre_update(fname
, "LF", ip
, idx
, XFS_DATA_FORK
);
1150 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
1151 xfs_bmbt_set_startoff(ep
, new_endoff
);
1152 xfs_bmbt_set_blockcount(ep
,
1153 PREV
.br_blockcount
- new->br_blockcount
);
1154 xfs_bmbt_set_startblock(ep
,
1155 new->br_startblock
+ new->br_blockcount
);
1156 xfs_bmap_trace_post_update(fname
, "LF", ip
, idx
, XFS_DATA_FORK
);
1157 xfs_bmap_trace_insert(fname
, "LF", ip
, idx
, 1, new, NULL
,
1159 xfs_bmap_insert_exlist(ip
, idx
, 1, new, XFS_DATA_FORK
);
1160 ip
->i_df
.if_lastex
= idx
;
1161 ip
->i_d
.di_nextents
++;
1163 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1165 rval
= XFS_ILOG_CORE
;
1166 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1167 PREV
.br_startblock
, PREV
.br_blockcount
,
1171 if ((error
= xfs_bmbt_update(cur
,
1172 PREV
.br_startoff
+ new->br_blockcount
,
1173 PREV
.br_startblock
+ new->br_blockcount
,
1174 PREV
.br_blockcount
- new->br_blockcount
,
1177 cur
->bc_rec
.b
= *new;
1178 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1184 case MASK2(RIGHT_FILLING
, RIGHT_CONTIG
):
1186 * Setting the last part of a previous oldext extent to newext.
1187 * The right neighbor is contiguous with the new allocation.
1189 xfs_bmap_trace_pre_update(fname
, "RF|RC", ip
, idx
,
1191 xfs_bmap_trace_pre_update(fname
, "RF|RC", ip
, idx
+ 1,
1193 xfs_bmbt_set_blockcount(ep
,
1194 PREV
.br_blockcount
- new->br_blockcount
);
1195 xfs_bmap_trace_post_update(fname
, "RF|RC", ip
, idx
,
1197 xfs_bmbt_set_allf(ep
+ 1, new->br_startoff
, new->br_startblock
,
1198 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
1199 xfs_bmap_trace_post_update(fname
, "RF|RC", ip
, idx
+ 1,
1201 ip
->i_df
.if_lastex
= idx
+ 1;
1203 rval
= XFS_ILOG_DEXT
;
1206 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1208 PREV
.br_blockcount
, &i
)))
1211 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
1213 PREV
.br_blockcount
- new->br_blockcount
,
1216 if ((error
= xfs_bmbt_increment(cur
, 0, &i
)))
1218 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
1220 new->br_blockcount
+ RIGHT
.br_blockcount
,
1226 case MASK(RIGHT_FILLING
):
1228 * Setting the last part of a previous oldext extent to newext.
1229 * The right neighbor is not contiguous.
1231 xfs_bmap_trace_pre_update(fname
, "RF", ip
, idx
, XFS_DATA_FORK
);
1232 xfs_bmbt_set_blockcount(ep
,
1233 PREV
.br_blockcount
- new->br_blockcount
);
1234 xfs_bmap_trace_post_update(fname
, "RF", ip
, idx
, XFS_DATA_FORK
);
1235 xfs_bmap_trace_insert(fname
, "RF", ip
, idx
+ 1, 1,
1236 new, NULL
, XFS_DATA_FORK
);
1237 xfs_bmap_insert_exlist(ip
, idx
+ 1, 1, new, XFS_DATA_FORK
);
1238 ip
->i_df
.if_lastex
= idx
+ 1;
1239 ip
->i_d
.di_nextents
++;
1241 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1243 rval
= XFS_ILOG_CORE
;
1244 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1245 PREV
.br_startblock
, PREV
.br_blockcount
,
1249 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
1251 PREV
.br_blockcount
- new->br_blockcount
,
1254 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1255 new->br_startblock
, new->br_blockcount
,
1259 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1260 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1268 * Setting the middle part of a previous oldext extent to
1269 * newext. Contiguity is impossible here.
1270 * One extent becomes three extents.
1272 xfs_bmap_trace_pre_update(fname
, "0", ip
, idx
, XFS_DATA_FORK
);
1273 xfs_bmbt_set_blockcount(ep
,
1274 new->br_startoff
- PREV
.br_startoff
);
1275 xfs_bmap_trace_post_update(fname
, "0", ip
, idx
, XFS_DATA_FORK
);
1277 r
[1].br_startoff
= new_endoff
;
1278 r
[1].br_blockcount
=
1279 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1280 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
1281 r
[1].br_state
= oldext
;
1282 xfs_bmap_trace_insert(fname
, "0", ip
, idx
+ 1, 2, &r
[0], &r
[1],
1284 xfs_bmap_insert_exlist(ip
, idx
+ 1, 2, &r
[0], XFS_DATA_FORK
);
1285 ip
->i_df
.if_lastex
= idx
+ 1;
1286 ip
->i_d
.di_nextents
+= 2;
1288 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1290 rval
= XFS_ILOG_CORE
;
1291 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1292 PREV
.br_startblock
, PREV
.br_blockcount
,
1296 /* new right extent - oldext */
1297 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
1298 r
[1].br_startblock
, r
[1].br_blockcount
,
1301 /* new left extent - oldext */
1302 PREV
.br_blockcount
=
1303 new->br_startoff
- PREV
.br_startoff
;
1304 cur
->bc_rec
.b
= PREV
;
1305 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1308 if ((error
= xfs_bmbt_increment(cur
, 0, &i
)))
1311 /* new middle extent - newext */
1312 cur
->bc_rec
.b
= *new;
1313 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1319 case MASK3(LEFT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
1320 case MASK3(RIGHT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
1321 case MASK2(LEFT_FILLING
, RIGHT_CONTIG
):
1322 case MASK2(RIGHT_FILLING
, LEFT_CONTIG
):
1323 case MASK2(LEFT_CONTIG
, RIGHT_CONTIG
):
1324 case MASK(LEFT_CONTIG
):
1325 case MASK(RIGHT_CONTIG
):
1327 * These cases are all impossible.
1344 #undef STATE_SET_TEST
1349 * Called by xfs_bmap_add_extent to handle cases converting a hole
1350 * to a delayed allocation.
1353 STATIC
int /* error */
1354 xfs_bmap_add_extent_hole_delay(
1355 xfs_inode_t
*ip
, /* incore inode pointer */
1356 xfs_extnum_t idx
, /* extent number to update/insert */
1357 xfs_btree_cur_t
*cur
, /* if null, not a btree */
1358 xfs_bmbt_irec_t
*new, /* new data to put in extent list */
1359 int *logflagsp
, /* inode logging flags */
1360 int rsvd
) /* OK to allocate reserved blocks */
1362 xfs_bmbt_rec_t
*base
; /* base of extent entry list */
1363 xfs_bmbt_rec_t
*ep
; /* extent list entry for idx */
1364 #ifdef XFS_BMAP_TRACE
1365 static char fname
[] = "xfs_bmap_add_extent_hole_delay";
1367 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
1368 xfs_filblks_t newlen
=0; /* new indirect size */
1369 xfs_filblks_t oldlen
=0; /* old indirect size */
1370 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
1371 int state
; /* state bits, accessed thru macros */
1372 xfs_filblks_t temp
; /* temp for indirect calculations */
1373 enum { /* bit number definitions for state */
1374 LEFT_CONTIG
, RIGHT_CONTIG
,
1375 LEFT_DELAY
, RIGHT_DELAY
,
1376 LEFT_VALID
, RIGHT_VALID
1379 #define MASK(b) (1 << (b))
1380 #define MASK2(a,b) (MASK(a) | MASK(b))
1381 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1382 #define STATE_TEST(b) (state & MASK(b))
1383 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1384 ((state &= ~MASK(b)), 0))
1385 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1387 base
= ip
->i_df
.if_u1
.if_extents
;
1390 ASSERT(ISNULLSTARTBLOCK(new->br_startblock
));
1392 * Check and set flags if this segment has a left neighbor
1394 if (STATE_SET_TEST(LEFT_VALID
, idx
> 0)) {
1395 xfs_bmbt_get_all(ep
- 1, &left
);
1396 STATE_SET(LEFT_DELAY
, ISNULLSTARTBLOCK(left
.br_startblock
));
1399 * Check and set flags if the current (right) segment exists.
1400 * If it doesn't exist, we're converting the hole at end-of-file.
1402 if (STATE_SET_TEST(RIGHT_VALID
,
1404 ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1405 xfs_bmbt_get_all(ep
, &right
);
1406 STATE_SET(RIGHT_DELAY
, ISNULLSTARTBLOCK(right
.br_startblock
));
1409 * Set contiguity flags on the left and right neighbors.
1410 * Don't let extents get too large, even if the pieces are contiguous.
1412 STATE_SET(LEFT_CONTIG
,
1413 STATE_TEST(LEFT_VALID
) && STATE_TEST(LEFT_DELAY
) &&
1414 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
1415 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
);
1416 STATE_SET(RIGHT_CONTIG
,
1417 STATE_TEST(RIGHT_VALID
) && STATE_TEST(RIGHT_DELAY
) &&
1418 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
1419 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
1420 (!STATE_TEST(LEFT_CONTIG
) ||
1421 (left
.br_blockcount
+ new->br_blockcount
+
1422 right
.br_blockcount
<= MAXEXTLEN
)));
1424 * Switch out based on the contiguity flags.
1426 switch (SWITCH_STATE
) {
1428 case MASK2(LEFT_CONTIG
, RIGHT_CONTIG
):
1430 * New allocation is contiguous with delayed allocations
1431 * on the left and on the right.
1432 * Merge all three into a single extent list entry.
1434 temp
= left
.br_blockcount
+ new->br_blockcount
+
1435 right
.br_blockcount
;
1436 xfs_bmap_trace_pre_update(fname
, "LC|RC", ip
, idx
- 1,
1438 xfs_bmbt_set_blockcount(ep
- 1, temp
);
1439 oldlen
= STARTBLOCKVAL(left
.br_startblock
) +
1440 STARTBLOCKVAL(new->br_startblock
) +
1441 STARTBLOCKVAL(right
.br_startblock
);
1442 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
1443 xfs_bmbt_set_startblock(ep
- 1, NULLSTARTBLOCK((int)newlen
));
1444 xfs_bmap_trace_post_update(fname
, "LC|RC", ip
, idx
- 1,
1446 xfs_bmap_trace_delete(fname
, "LC|RC", ip
, idx
, 1,
1448 xfs_bmap_delete_exlist(ip
, idx
, 1, XFS_DATA_FORK
);
1449 ip
->i_df
.if_lastex
= idx
- 1;
1452 case MASK(LEFT_CONTIG
):
1454 * New allocation is contiguous with a delayed allocation
1456 * Merge the new allocation with the left neighbor.
1458 temp
= left
.br_blockcount
+ new->br_blockcount
;
1459 xfs_bmap_trace_pre_update(fname
, "LC", ip
, idx
- 1,
1461 xfs_bmbt_set_blockcount(ep
- 1, temp
);
1462 oldlen
= STARTBLOCKVAL(left
.br_startblock
) +
1463 STARTBLOCKVAL(new->br_startblock
);
1464 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
1465 xfs_bmbt_set_startblock(ep
- 1, NULLSTARTBLOCK((int)newlen
));
1466 xfs_bmap_trace_post_update(fname
, "LC", ip
, idx
- 1,
1468 ip
->i_df
.if_lastex
= idx
- 1;
1471 case MASK(RIGHT_CONTIG
):
1473 * New allocation is contiguous with a delayed allocation
1475 * Merge the new allocation with the right neighbor.
1477 xfs_bmap_trace_pre_update(fname
, "RC", ip
, idx
, XFS_DATA_FORK
);
1478 temp
= new->br_blockcount
+ right
.br_blockcount
;
1479 oldlen
= STARTBLOCKVAL(new->br_startblock
) +
1480 STARTBLOCKVAL(right
.br_startblock
);
1481 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
1482 xfs_bmbt_set_allf(ep
, new->br_startoff
,
1483 NULLSTARTBLOCK((int)newlen
), temp
, right
.br_state
);
1484 xfs_bmap_trace_post_update(fname
, "RC", ip
, idx
, XFS_DATA_FORK
);
1485 ip
->i_df
.if_lastex
= idx
;
1490 * New allocation is not contiguous with another
1491 * delayed allocation.
1492 * Insert a new entry.
1494 oldlen
= newlen
= 0;
1495 xfs_bmap_trace_insert(fname
, "0", ip
, idx
, 1, new, NULL
,
1497 xfs_bmap_insert_exlist(ip
, idx
, 1, new, XFS_DATA_FORK
);
1498 ip
->i_df
.if_lastex
= idx
;
1501 if (oldlen
!= newlen
) {
1502 ASSERT(oldlen
> newlen
);
1503 xfs_mod_incore_sb(ip
->i_mount
, XFS_SBS_FDBLOCKS
,
1504 (int)(oldlen
- newlen
), rsvd
);
1506 * Nothing to do for disk quota accounting here.
1515 #undef STATE_SET_TEST
1520 * Called by xfs_bmap_add_extent to handle cases converting a hole
1521 * to a real allocation.
1523 STATIC
int /* error */
1524 xfs_bmap_add_extent_hole_real(
1525 xfs_inode_t
*ip
, /* incore inode pointer */
1526 xfs_extnum_t idx
, /* extent number to update/insert */
1527 xfs_btree_cur_t
*cur
, /* if null, not a btree */
1528 xfs_bmbt_irec_t
*new, /* new data to put in extent list */
1529 int *logflagsp
, /* inode logging flags */
1530 int whichfork
) /* data or attr fork */
1532 xfs_bmbt_rec_t
*ep
; /* pointer to extent entry ins. point */
1533 int error
; /* error return value */
1534 #ifdef XFS_BMAP_TRACE
1535 static char fname
[] = "xfs_bmap_add_extent_hole_real";
1537 int i
; /* temp state */
1538 xfs_ifork_t
*ifp
; /* inode fork pointer */
1539 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
1540 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
1541 int state
; /* state bits, accessed thru macros */
1542 enum { /* bit number definitions for state */
1543 LEFT_CONTIG
, RIGHT_CONTIG
,
1544 LEFT_DELAY
, RIGHT_DELAY
,
1545 LEFT_VALID
, RIGHT_VALID
1548 #define MASK(b) (1 << (b))
1549 #define MASK2(a,b) (MASK(a) | MASK(b))
1550 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1551 #define STATE_TEST(b) (state & MASK(b))
1552 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1553 ((state &= ~MASK(b)), 0))
1554 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1556 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1557 ASSERT(idx
<= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
));
1558 ep
= &ifp
->if_u1
.if_extents
[idx
];
1561 * Check and set flags if this segment has a left neighbor.
1563 if (STATE_SET_TEST(LEFT_VALID
, idx
> 0)) {
1564 xfs_bmbt_get_all(ep
- 1, &left
);
1565 STATE_SET(LEFT_DELAY
, ISNULLSTARTBLOCK(left
.br_startblock
));
1568 * Check and set flags if this segment has a current value.
1569 * Not true if we're inserting into the "hole" at eof.
1571 if (STATE_SET_TEST(RIGHT_VALID
,
1573 ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1574 xfs_bmbt_get_all(ep
, &right
);
1575 STATE_SET(RIGHT_DELAY
, ISNULLSTARTBLOCK(right
.br_startblock
));
1578 * We're inserting a real allocation between "left" and "right".
1579 * Set the contiguity flags. Don't let extents get too large.
1581 STATE_SET(LEFT_CONTIG
,
1582 STATE_TEST(LEFT_VALID
) && !STATE_TEST(LEFT_DELAY
) &&
1583 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
1584 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
1585 left
.br_state
== new->br_state
&&
1586 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
);
1587 STATE_SET(RIGHT_CONTIG
,
1588 STATE_TEST(RIGHT_VALID
) && !STATE_TEST(RIGHT_DELAY
) &&
1589 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
1590 new->br_startblock
+ new->br_blockcount
==
1591 right
.br_startblock
&&
1592 new->br_state
== right
.br_state
&&
1593 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
1594 (!STATE_TEST(LEFT_CONTIG
) ||
1595 left
.br_blockcount
+ new->br_blockcount
+
1596 right
.br_blockcount
<= MAXEXTLEN
));
1599 * Select which case we're in here, and implement it.
1601 switch (SWITCH_STATE
) {
1603 case MASK2(LEFT_CONTIG
, RIGHT_CONTIG
):
1605 * New allocation is contiguous with real allocations on the
1606 * left and on the right.
1607 * Merge all three into a single extent list entry.
1609 xfs_bmap_trace_pre_update(fname
, "LC|RC", ip
, idx
- 1,
1611 xfs_bmbt_set_blockcount(ep
- 1,
1612 left
.br_blockcount
+ new->br_blockcount
+
1613 right
.br_blockcount
);
1614 xfs_bmap_trace_post_update(fname
, "LC|RC", ip
, idx
- 1,
1616 xfs_bmap_trace_delete(fname
, "LC|RC", ip
,
1618 xfs_bmap_delete_exlist(ip
, idx
, 1, whichfork
);
1619 ifp
->if_lastex
= idx
- 1;
1620 XFS_IFORK_NEXT_SET(ip
, whichfork
,
1621 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
1623 *logflagsp
= XFS_ILOG_CORE
| XFS_ILOG_FEXT(whichfork
);
1626 *logflagsp
= XFS_ILOG_CORE
;
1627 if ((error
= xfs_bmbt_lookup_eq(cur
, right
.br_startoff
,
1628 right
.br_startblock
, right
.br_blockcount
, &i
)))
1631 if ((error
= xfs_bmbt_delete(cur
, &i
)))
1634 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
1637 error
= xfs_bmbt_update(cur
, left
.br_startoff
,
1639 left
.br_blockcount
+ new->br_blockcount
+
1640 right
.br_blockcount
, left
.br_state
);
1643 case MASK(LEFT_CONTIG
):
1645 * New allocation is contiguous with a real allocation
1647 * Merge the new allocation with the left neighbor.
1649 xfs_bmap_trace_pre_update(fname
, "LC", ip
, idx
- 1, whichfork
);
1650 xfs_bmbt_set_blockcount(ep
- 1,
1651 left
.br_blockcount
+ new->br_blockcount
);
1652 xfs_bmap_trace_post_update(fname
, "LC", ip
, idx
- 1, whichfork
);
1653 ifp
->if_lastex
= idx
- 1;
1655 *logflagsp
= XFS_ILOG_FEXT(whichfork
);
1659 if ((error
= xfs_bmbt_lookup_eq(cur
, left
.br_startoff
,
1660 left
.br_startblock
, left
.br_blockcount
, &i
)))
1663 error
= xfs_bmbt_update(cur
, left
.br_startoff
,
1665 left
.br_blockcount
+ new->br_blockcount
,
1669 case MASK(RIGHT_CONTIG
):
1671 * New allocation is contiguous with a real allocation
1673 * Merge the new allocation with the right neighbor.
1675 xfs_bmap_trace_pre_update(fname
, "RC", ip
, idx
, whichfork
);
1676 xfs_bmbt_set_allf(ep
, new->br_startoff
, new->br_startblock
,
1677 new->br_blockcount
+ right
.br_blockcount
,
1679 xfs_bmap_trace_post_update(fname
, "RC", ip
, idx
, whichfork
);
1680 ifp
->if_lastex
= idx
;
1682 *logflagsp
= XFS_ILOG_FEXT(whichfork
);
1686 if ((error
= xfs_bmbt_lookup_eq(cur
, right
.br_startoff
,
1687 right
.br_startblock
, right
.br_blockcount
, &i
)))
1690 error
= xfs_bmbt_update(cur
, new->br_startoff
,
1692 new->br_blockcount
+ right
.br_blockcount
,
1698 * New allocation is not contiguous with another
1700 * Insert a new entry.
1702 xfs_bmap_trace_insert(fname
, "0", ip
, idx
, 1, new, NULL
,
1704 xfs_bmap_insert_exlist(ip
, idx
, 1, new, whichfork
);
1705 ifp
->if_lastex
= idx
;
1706 XFS_IFORK_NEXT_SET(ip
, whichfork
,
1707 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
1709 *logflagsp
= XFS_ILOG_CORE
| XFS_ILOG_FEXT(whichfork
);
1712 *logflagsp
= XFS_ILOG_CORE
;
1713 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1714 new->br_startblock
, new->br_blockcount
, &i
)))
1717 cur
->bc_rec
.b
.br_state
= new->br_state
;
1718 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1727 #undef STATE_SET_TEST
1731 return 0; /* keep gcc quite */
1735 * xfs_bmap_alloc_extsize_align is called by xfs_bmapi_alloc to adjust
1736 * size of the extent-to-be-allocated based on inode and rt extsize.
1739 xfs_bmap_extsize_align(
1741 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
1742 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
1743 xfs_extlen_t extsz
, /* align to this extent size */
1744 int rt
, /* is this a realtime inode? */
1745 int eof
, /* is extent at end-of-file? */
1746 int delay
, /* creating delalloc extent? */
1747 int convert
, /* overwriting unwritten extent? */
1748 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
1749 xfs_extlen_t
*lenp
) /* in/out: aligned length */
1751 xfs_fileoff_t orig_off
; /* original offset */
1752 xfs_extlen_t orig_alen
; /* original length */
1753 xfs_fileoff_t orig_end
; /* original off+len */
1754 xfs_fileoff_t nexto
; /* next file offset */
1755 xfs_fileoff_t prevo
; /* previous file offset */
1756 xfs_fileoff_t align_off
; /* temp for offset */
1757 xfs_extlen_t align_alen
; /* temp for length */
1758 xfs_extlen_t temp
; /* temp for calculations */
1763 orig_off
= align_off
= *offp
;
1764 orig_alen
= align_alen
= *lenp
;
1765 orig_end
= orig_off
+ orig_alen
;
1768 * If this request overlaps an existing extent, then don't
1769 * attempt to perform any additional alignment.
1771 if (!delay
&& !eof
&&
1772 (orig_off
>= gotp
->br_startoff
) &&
1773 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
1778 * If the file offset is unaligned vs. the extent size
1779 * we need to align it. This will be possible unless
1780 * the file was previously written with a kernel that didn't
1781 * perform this alignment, or if a truncate shot us in the
1784 temp
= do_mod(orig_off
, extsz
);
1790 * Same adjustment for the end of the requested area.
1792 if ((temp
= (align_alen
% extsz
))) {
1793 align_alen
+= extsz
- temp
;
1796 * If the previous block overlaps with this proposed allocation
1797 * then move the start forward without adjusting the length.
1799 if (prevp
->br_startoff
!= NULLFILEOFF
) {
1800 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
1801 prevo
= prevp
->br_startoff
;
1803 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
1806 if (align_off
!= orig_off
&& align_off
< prevo
)
1809 * If the next block overlaps with this proposed allocation
1810 * then move the start back without adjusting the length,
1811 * but not before offset 0.
1812 * This may of course make the start overlap previous block,
1813 * and if we hit the offset 0 limit then the next block
1814 * can still overlap too.
1816 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
1817 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
1818 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
1819 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
1821 nexto
= gotp
->br_startoff
;
1823 nexto
= NULLFILEOFF
;
1825 align_off
+ align_alen
!= orig_end
&&
1826 align_off
+ align_alen
> nexto
)
1827 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
1829 * If we're now overlapping the next or previous extent that
1830 * means we can't fit an extsz piece in this hole. Just move
1831 * the start forward to the first valid spot and set
1832 * the length so we hit the end.
1834 if (align_off
!= orig_off
&& align_off
< prevo
)
1836 if (align_off
+ align_alen
!= orig_end
&&
1837 align_off
+ align_alen
> nexto
&&
1838 nexto
!= NULLFILEOFF
) {
1839 ASSERT(nexto
> prevo
);
1840 align_alen
= nexto
- align_off
;
1844 * If realtime, and the result isn't a multiple of the realtime
1845 * extent size we need to remove blocks until it is.
1847 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
1849 * We're not covering the original request, or
1850 * we won't be able to once we fix the length.
1852 if (orig_off
< align_off
||
1853 orig_end
> align_off
+ align_alen
||
1854 align_alen
- temp
< orig_alen
)
1855 return XFS_ERROR(EINVAL
);
1857 * Try to fix it by moving the start up.
1859 if (align_off
+ temp
<= orig_off
) {
1864 * Try to fix it by moving the end in.
1866 else if (align_off
+ align_alen
- temp
>= orig_end
)
1869 * Set the start to the minimum then trim the length.
1872 align_alen
-= orig_off
- align_off
;
1873 align_off
= orig_off
;
1874 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
1877 * Result doesn't cover the request, fail it.
1879 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
1880 return XFS_ERROR(EINVAL
);
1882 ASSERT(orig_off
>= align_off
);
1883 ASSERT(orig_end
<= align_off
+ align_alen
);
1887 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
1888 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
1889 if (prevp
->br_startoff
!= NULLFILEOFF
)
1890 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
1898 #define XFS_ALLOC_GAP_UNITS 4
1901 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
1902 * It figures out where to ask the underlying allocator to put the new extent.
1906 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
1908 xfs_fsblock_t adjust
; /* adjustment to block numbers */
1909 xfs_alloctype_t atype
=0; /* type for allocation routines */
1910 int error
; /* error return value */
1911 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
1912 xfs_mount_t
*mp
; /* mount point structure */
1913 int nullfb
; /* true if ap->firstblock isn't set */
1914 int rt
; /* true if inode is realtime */
1915 xfs_extlen_t prod
= 0; /* product factor for allocators */
1916 xfs_extlen_t ralen
= 0; /* realtime allocation length */
1917 xfs_extlen_t align
; /* minimum allocation alignment */
1920 #define ISVALID(x,y) \
1922 (x) < mp->m_sb.sb_rblocks : \
1923 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
1924 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
1925 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
1930 mp
= ap
->ip
->i_mount
;
1931 nullfb
= ap
->firstblock
== NULLFSBLOCK
;
1932 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
;
1933 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, ap
->firstblock
);
1935 align
= ap
->ip
->i_d
.di_extsize
?
1936 ap
->ip
->i_d
.di_extsize
: mp
->m_sb
.sb_rextsize
;
1937 /* Set prod to match the extent size */
1938 prod
= align
/ mp
->m_sb
.sb_rextsize
;
1940 error
= xfs_bmap_extsize_align(mp
, ap
->gotp
, ap
->prevp
,
1941 align
, rt
, ap
->eof
, 0,
1942 ap
->conv
, &ap
->off
, &ap
->alen
);
1946 ASSERT(ap
->alen
% mp
->m_sb
.sb_rextsize
== 0);
1949 * If the offset & length are not perfectly aligned
1950 * then kill prod, it will just get us in trouble.
1952 if (do_mod(ap
->off
, align
) || ap
->alen
% align
)
1955 * Set ralen to be the actual requested length in rtextents.
1957 ralen
= ap
->alen
/ mp
->m_sb
.sb_rextsize
;
1959 * If the old value was close enough to MAXEXTLEN that
1960 * we rounded up to it, cut it back so it's valid again.
1961 * Note that if it's a really large request (bigger than
1962 * MAXEXTLEN), we don't hear about that number, and can't
1963 * adjust the starting point to match it.
1965 if (ralen
* mp
->m_sb
.sb_rextsize
>= MAXEXTLEN
)
1966 ralen
= MAXEXTLEN
/ mp
->m_sb
.sb_rextsize
;
1968 * If it's an allocation to an empty file at offset 0,
1969 * pick an extent that will space things out in the rt area.
1971 if (ap
->eof
&& ap
->off
== 0) {
1972 error
= xfs_rtpick_extent(mp
, ap
->tp
, ralen
, &rtx
);
1975 ap
->rval
= rtx
* mp
->m_sb
.sb_rextsize
;
1979 align
= (ap
->userdata
&& ap
->ip
->i_d
.di_extsize
&&
1980 (ap
->ip
->i_d
.di_flags
& XFS_DIFLAG_EXTSIZE
)) ?
1981 ap
->ip
->i_d
.di_extsize
: 0;
1982 if (unlikely(align
)) {
1983 error
= xfs_bmap_extsize_align(mp
, ap
->gotp
, ap
->prevp
,
1985 ap
->eof
, 0, ap
->conv
,
1986 &ap
->off
, &ap
->alen
);
1991 ap
->rval
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
1993 ap
->rval
= ap
->firstblock
;
1997 * If allocating at eof, and there's a previous real block,
1998 * try to use it's last block as our starting point.
2000 if (ap
->eof
&& ap
->prevp
->br_startoff
!= NULLFILEOFF
&&
2001 !ISNULLSTARTBLOCK(ap
->prevp
->br_startblock
) &&
2002 ISVALID(ap
->prevp
->br_startblock
+ ap
->prevp
->br_blockcount
,
2003 ap
->prevp
->br_startblock
)) {
2004 ap
->rval
= ap
->prevp
->br_startblock
+ ap
->prevp
->br_blockcount
;
2006 * Adjust for the gap between prevp and us.
2009 (ap
->prevp
->br_startoff
+ ap
->prevp
->br_blockcount
);
2011 ISVALID(ap
->rval
+ adjust
, ap
->prevp
->br_startblock
))
2015 * If not at eof, then compare the two neighbor blocks.
2016 * Figure out whether either one gives us a good starting point,
2017 * and pick the better one.
2019 else if (!ap
->eof
) {
2020 xfs_fsblock_t gotbno
; /* right side block number */
2021 xfs_fsblock_t gotdiff
=0; /* right side difference */
2022 xfs_fsblock_t prevbno
; /* left side block number */
2023 xfs_fsblock_t prevdiff
=0; /* left side difference */
2026 * If there's a previous (left) block, select a requested
2027 * start block based on it.
2029 if (ap
->prevp
->br_startoff
!= NULLFILEOFF
&&
2030 !ISNULLSTARTBLOCK(ap
->prevp
->br_startblock
) &&
2031 (prevbno
= ap
->prevp
->br_startblock
+
2032 ap
->prevp
->br_blockcount
) &&
2033 ISVALID(prevbno
, ap
->prevp
->br_startblock
)) {
2035 * Calculate gap to end of previous block.
2037 adjust
= prevdiff
= ap
->off
-
2038 (ap
->prevp
->br_startoff
+
2039 ap
->prevp
->br_blockcount
);
2041 * Figure the startblock based on the previous block's
2042 * end and the gap size.
2044 * If the gap is large relative to the piece we're
2045 * allocating, or using it gives us an invalid block
2046 * number, then just use the end of the previous block.
2048 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->alen
&&
2049 ISVALID(prevbno
+ prevdiff
,
2050 ap
->prevp
->br_startblock
))
2055 * If the firstblock forbids it, can't use it,
2058 if (!rt
&& !nullfb
&&
2059 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
2060 prevbno
= NULLFSBLOCK
;
2063 * No previous block or can't follow it, just default.
2066 prevbno
= NULLFSBLOCK
;
2068 * If there's a following (right) block, select a requested
2069 * start block based on it.
2071 if (!ISNULLSTARTBLOCK(ap
->gotp
->br_startblock
)) {
2073 * Calculate gap to start of next block.
2075 adjust
= gotdiff
= ap
->gotp
->br_startoff
- ap
->off
;
2077 * Figure the startblock based on the next block's
2078 * start and the gap size.
2080 gotbno
= ap
->gotp
->br_startblock
;
2083 * If the gap is large relative to the piece we're
2084 * allocating, or using it gives us an invalid block
2085 * number, then just use the start of the next block
2086 * offset by our length.
2088 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->alen
&&
2089 ISVALID(gotbno
- gotdiff
, gotbno
))
2091 else if (ISVALID(gotbno
- ap
->alen
, gotbno
)) {
2093 gotdiff
+= adjust
- ap
->alen
;
2097 * If the firstblock forbids it, can't use it,
2100 if (!rt
&& !nullfb
&&
2101 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
2102 gotbno
= NULLFSBLOCK
;
2105 * No next block, just default.
2108 gotbno
= NULLFSBLOCK
;
2110 * If both valid, pick the better one, else the only good
2111 * one, else ap->rval is already set (to 0 or the inode block).
2113 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
2114 ap
->rval
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
2115 else if (prevbno
!= NULLFSBLOCK
)
2117 else if (gotbno
!= NULLFSBLOCK
)
2121 * If allowed, use ap->rval; otherwise must use firstblock since
2122 * it's in the right allocation group.
2124 if (nullfb
|| rt
|| XFS_FSB_TO_AGNO(mp
, ap
->rval
) == fb_agno
)
2127 ap
->rval
= ap
->firstblock
;
2129 * Realtime allocation, done through xfs_rtallocate_extent.
2137 atype
= ap
->rval
== 0 ?
2138 XFS_ALLOCTYPE_ANY_AG
: XFS_ALLOCTYPE_NEAR_BNO
;
2139 do_div(ap
->rval
, mp
->m_sb
.sb_rextsize
);
2142 if ((error
= xfs_rtallocate_extent(ap
->tp
, ap
->rval
, 1, ap
->alen
,
2143 &ralen
, atype
, ap
->wasdel
, prod
, &rtb
)))
2145 if (rtb
== NULLFSBLOCK
&& prod
> 1 &&
2146 (error
= xfs_rtallocate_extent(ap
->tp
, ap
->rval
, 1,
2147 ap
->alen
, &ralen
, atype
,
2148 ap
->wasdel
, 1, &rtb
)))
2151 if (ap
->rval
!= NULLFSBLOCK
) {
2152 ap
->rval
*= mp
->m_sb
.sb_rextsize
;
2153 ralen
*= mp
->m_sb
.sb_rextsize
;
2155 ap
->ip
->i_d
.di_nblocks
+= ralen
;
2156 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
2158 ap
->ip
->i_delayed_blks
-= ralen
;
2160 * Adjust the disk quota also. This was reserved
2163 XFS_TRANS_MOD_DQUOT_BYINO(mp
, ap
->tp
, ap
->ip
,
2164 ap
->wasdel
? XFS_TRANS_DQ_DELRTBCOUNT
:
2165 XFS_TRANS_DQ_RTBCOUNT
,
2169 #endif /* __KERNEL__ */
2172 * Normal allocation, done through xfs_alloc_vextent.
2176 xfs_alloc_arg_t args
;
2180 xfs_extlen_t longest
;
2182 xfs_extlen_t nextminlen
=0;
2185 xfs_agnumber_t startag
;
2188 tryagain
= isaligned
= 0;
2191 args
.fsbno
= ap
->rval
;
2192 args
.maxlen
= MIN(ap
->alen
, mp
->m_sb
.sb_agblocks
);
2195 args
.type
= XFS_ALLOCTYPE_START_BNO
;
2196 args
.total
= ap
->total
;
2198 * Find the longest available space.
2199 * We're going to try for the whole allocation at once.
2201 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
.fsbno
);
2203 down_read(&mp
->m_peraglock
);
2204 while (blen
< ap
->alen
) {
2205 pag
= &mp
->m_perag
[ag
];
2206 if (!pag
->pagf_init
&&
2207 (error
= xfs_alloc_pagf_init(mp
, args
.tp
,
2208 ag
, XFS_ALLOC_FLAG_TRYLOCK
))) {
2209 up_read(&mp
->m_peraglock
);
2213 * See xfs_alloc_fix_freelist...
2215 if (pag
->pagf_init
) {
2216 need
= XFS_MIN_FREELIST_PAG(pag
, mp
);
2217 delta
= need
> pag
->pagf_flcount
?
2218 need
- pag
->pagf_flcount
: 0;
2219 longest
= (pag
->pagf_longest
> delta
) ?
2220 (pag
->pagf_longest
- delta
) :
2221 (pag
->pagf_flcount
> 0 ||
2222 pag
->pagf_longest
> 0);
2227 if (++ag
== mp
->m_sb
.sb_agcount
)
2232 up_read(&mp
->m_peraglock
);
2234 * Since the above loop did a BUF_TRYLOCK, it is
2235 * possible that there is space for this request.
2237 if (notinit
|| blen
< ap
->minlen
)
2238 args
.minlen
= ap
->minlen
;
2240 * If the best seen length is less than the request
2241 * length, use the best as the minimum.
2243 else if (blen
< ap
->alen
)
2246 * Otherwise we've seen an extent as big as alen,
2247 * use that as the minimum.
2250 args
.minlen
= ap
->alen
;
2251 } else if (ap
->low
) {
2252 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
2253 args
.total
= args
.minlen
= ap
->minlen
;
2255 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
2256 args
.total
= ap
->total
;
2257 args
.minlen
= ap
->minlen
;
2259 if (unlikely(ap
->userdata
&& ap
->ip
->i_d
.di_extsize
&&
2260 (ap
->ip
->i_d
.di_flags
& XFS_DIFLAG_EXTSIZE
))) {
2261 args
.prod
= ap
->ip
->i_d
.di_extsize
;
2262 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->off
, args
.prod
)))
2263 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
2264 } else if (unlikely(mp
->m_sb
.sb_blocksize
>= NBPP
)) {
2268 args
.prod
= NBPP
>> mp
->m_sb
.sb_blocklog
;
2269 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->off
, args
.prod
))))
2270 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
2273 * If we are not low on available data blocks, and the
2274 * underlying logical volume manager is a stripe, and
2275 * the file offset is zero then try to allocate data
2276 * blocks on stripe unit boundary.
2277 * NOTE: ap->aeof is only set if the allocation length
2278 * is >= the stripe unit and the allocation offset is
2279 * at the end of file.
2281 if (!ap
->low
&& ap
->aeof
) {
2283 args
.alignment
= mp
->m_dalign
;
2287 * Adjust for alignment
2289 if (blen
> args
.alignment
&& blen
<= ap
->alen
)
2290 args
.minlen
= blen
- args
.alignment
;
2291 args
.minalignslop
= 0;
2294 * First try an exact bno allocation.
2295 * If it fails then do a near or start bno
2296 * allocation with alignment turned on.
2300 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
2303 * Compute the minlen+alignment for the
2304 * next case. Set slop so that the value
2305 * of minlen+alignment+slop doesn't go up
2306 * between the calls.
2308 if (blen
> mp
->m_dalign
&& blen
<= ap
->alen
)
2309 nextminlen
= blen
- mp
->m_dalign
;
2311 nextminlen
= args
.minlen
;
2312 if (nextminlen
+ mp
->m_dalign
> args
.minlen
+ 1)
2314 nextminlen
+ mp
->m_dalign
-
2317 args
.minalignslop
= 0;
2321 args
.minalignslop
= 0;
2323 args
.minleft
= ap
->minleft
;
2324 args
.wasdel
= ap
->wasdel
;
2326 args
.userdata
= ap
->userdata
;
2327 if ((error
= xfs_alloc_vextent(&args
)))
2329 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
2331 * Exact allocation failed. Now try with alignment
2335 args
.fsbno
= ap
->rval
;
2336 args
.alignment
= mp
->m_dalign
;
2337 args
.minlen
= nextminlen
;
2338 args
.minalignslop
= 0;
2340 if ((error
= xfs_alloc_vextent(&args
)))
2343 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
2345 * allocation failed, so turn off alignment and
2349 args
.fsbno
= ap
->rval
;
2351 if ((error
= xfs_alloc_vextent(&args
)))
2354 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
2355 args
.minlen
> ap
->minlen
) {
2356 args
.minlen
= ap
->minlen
;
2357 args
.type
= XFS_ALLOCTYPE_START_BNO
;
2358 args
.fsbno
= ap
->rval
;
2359 if ((error
= xfs_alloc_vextent(&args
)))
2362 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
2364 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
2365 args
.total
= ap
->minlen
;
2367 if ((error
= xfs_alloc_vextent(&args
)))
2371 if (args
.fsbno
!= NULLFSBLOCK
) {
2372 ap
->firstblock
= ap
->rval
= args
.fsbno
;
2373 ASSERT(nullfb
|| fb_agno
== args
.agno
||
2374 (ap
->low
&& fb_agno
< args
.agno
));
2375 ap
->alen
= args
.len
;
2376 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
2377 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
2379 ap
->ip
->i_delayed_blks
-= args
.len
;
2381 * Adjust the disk quota also. This was reserved
2384 XFS_TRANS_MOD_DQUOT_BYINO(mp
, ap
->tp
, ap
->ip
,
2385 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
2386 XFS_TRANS_DQ_BCOUNT
,
2389 ap
->rval
= NULLFSBLOCK
;
2398 * Transform a btree format file with only one leaf node, where the
2399 * extents list will fit in the inode, into an extents format file.
2400 * Since the extent list is already in-core, all we have to do is
2401 * give up the space for the btree root and pitch the leaf block.
2403 STATIC
int /* error */
2404 xfs_bmap_btree_to_extents(
2405 xfs_trans_t
*tp
, /* transaction pointer */
2406 xfs_inode_t
*ip
, /* incore inode pointer */
2407 xfs_btree_cur_t
*cur
, /* btree cursor */
2408 int *logflagsp
, /* inode logging flags */
2409 int whichfork
) /* data or attr fork */
2412 xfs_bmbt_block_t
*cblock
;/* child btree block */
2413 xfs_fsblock_t cbno
; /* child block number */
2414 xfs_buf_t
*cbp
; /* child block's buffer */
2415 int error
; /* error return value */
2416 xfs_ifork_t
*ifp
; /* inode fork data */
2417 xfs_mount_t
*mp
; /* mount point structure */
2418 xfs_bmbt_ptr_t
*pp
; /* ptr to block address */
2419 xfs_bmbt_block_t
*rblock
;/* root btree block */
2421 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2422 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
2423 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
2424 rblock
= ifp
->if_broot
;
2425 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
2426 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
2427 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp
->if_broot_bytes
) == 1);
2429 pp
= XFS_BMAP_BROOT_PTR_ADDR(rblock
, 1, ifp
->if_broot_bytes
);
2432 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(*pp
, ARCH_CONVERT
), 1)))
2435 cbno
= INT_GET(*pp
, ARCH_CONVERT
);
2436 if ((error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
,
2437 XFS_BMAP_BTREE_REF
)))
2439 cblock
= XFS_BUF_TO_BMBT_BLOCK(cbp
);
2440 if ((error
= xfs_btree_check_lblock(cur
, cblock
, 0, cbp
)))
2442 xfs_bmap_add_free(cbno
, 1, cur
->bc_private
.b
.flist
, mp
);
2443 ip
->i_d
.di_nblocks
--;
2444 XFS_TRANS_MOD_DQUOT_BYINO(mp
, tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
2445 xfs_trans_binval(tp
, cbp
);
2446 if (cur
->bc_bufs
[0] == cbp
)
2447 cur
->bc_bufs
[0] = NULL
;
2448 xfs_iroot_realloc(ip
, -1, whichfork
);
2449 ASSERT(ifp
->if_broot
== NULL
);
2450 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
2451 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
2452 *logflagsp
= XFS_ILOG_CORE
| XFS_ILOG_FEXT(whichfork
);
2457 * Called by xfs_bmapi to update extent list structure and the btree
2458 * after removing space (or undoing a delayed allocation).
2460 STATIC
int /* error */
2461 xfs_bmap_del_extent(
2462 xfs_inode_t
*ip
, /* incore inode pointer */
2463 xfs_trans_t
*tp
, /* current transaction pointer */
2464 xfs_extnum_t idx
, /* extent number to update/delete */
2465 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
2466 xfs_btree_cur_t
*cur
, /* if null, not a btree */
2467 xfs_bmbt_irec_t
*del
, /* data to remove from extent list */
2468 int *logflagsp
, /* inode logging flags */
2469 int whichfork
, /* data or attr fork */
2470 int rsvd
) /* OK to allocate reserved blocks */
2472 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
2473 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
2474 xfs_fsblock_t del_endblock
=0; /* first block past del */
2475 xfs_fileoff_t del_endoff
; /* first offset past del */
2476 int delay
; /* current block is delayed allocated */
2477 int do_fx
; /* free extent at end of routine */
2478 xfs_bmbt_rec_t
*ep
; /* current extent entry pointer */
2479 int error
; /* error return value */
2480 int flags
; /* inode logging flags */
2481 #ifdef XFS_BMAP_TRACE
2482 static char fname
[] = "xfs_bmap_del_extent";
2484 xfs_bmbt_irec_t got
; /* current extent entry */
2485 xfs_fileoff_t got_endoff
; /* first offset past got */
2486 int i
; /* temp state */
2487 xfs_ifork_t
*ifp
; /* inode fork pointer */
2488 xfs_mount_t
*mp
; /* mount structure */
2489 xfs_filblks_t nblks
; /* quota/sb block count */
2490 xfs_bmbt_irec_t
new; /* new record to be inserted */
2492 xfs_extnum_t nextents
; /* number of extents in list */
2493 uint qfield
; /* quota field to update */
2494 xfs_filblks_t temp
; /* for indirect length calculations */
2495 xfs_filblks_t temp2
; /* for indirect length calculations */
2497 XFS_STATS_INC(xs_del_exlist
);
2499 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2500 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
2501 ASSERT(idx
>= 0 && idx
< nextents
);
2502 ASSERT(del
->br_blockcount
> 0);
2503 ep
= &ifp
->if_u1
.if_extents
[idx
];
2504 xfs_bmbt_get_all(ep
, &got
);
2505 ASSERT(got
.br_startoff
<= del
->br_startoff
);
2506 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
2507 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
2508 ASSERT(got_endoff
>= del_endoff
);
2509 delay
= ISNULLSTARTBLOCK(got
.br_startblock
);
2510 ASSERT(ISNULLSTARTBLOCK(del
->br_startblock
) == delay
);
2515 * If deleting a real allocation, must free up the disk space.
2518 flags
= XFS_ILOG_CORE
;
2520 * Realtime allocation. Free it and record di_nblocks update.
2522 if (whichfork
== XFS_DATA_FORK
&&
2523 (ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
)) {
2527 ASSERT(do_mod(del
->br_blockcount
,
2528 mp
->m_sb
.sb_rextsize
) == 0);
2529 ASSERT(do_mod(del
->br_startblock
,
2530 mp
->m_sb
.sb_rextsize
) == 0);
2531 bno
= del
->br_startblock
;
2532 len
= del
->br_blockcount
;
2533 do_div(bno
, mp
->m_sb
.sb_rextsize
);
2534 do_div(len
, mp
->m_sb
.sb_rextsize
);
2535 if ((error
= xfs_rtfree_extent(ip
->i_transp
, bno
,
2536 (xfs_extlen_t
)len
)))
2539 nblks
= len
* mp
->m_sb
.sb_rextsize
;
2540 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
2543 * Ordinary allocation.
2547 nblks
= del
->br_blockcount
;
2548 qfield
= XFS_TRANS_DQ_BCOUNT
;
2551 * Set up del_endblock and cur for later.
2553 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
2555 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
2556 got
.br_startblock
, got
.br_blockcount
,
2561 da_old
= da_new
= 0;
2563 da_old
= STARTBLOCKVAL(got
.br_startblock
);
2569 * Set flag value to use in switch statement.
2570 * Left-contig is 2, right-contig is 1.
2572 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
2573 (got_endoff
== del_endoff
)) {
2576 * Matches the whole extent. Delete the entry.
2578 xfs_bmap_trace_delete(fname
, "3", ip
, idx
, 1, whichfork
);
2579 xfs_bmap_delete_exlist(ip
, idx
, 1, whichfork
);
2580 ifp
->if_lastex
= idx
;
2583 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2584 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2585 flags
|= XFS_ILOG_CORE
;
2587 flags
|= XFS_ILOG_FEXT(whichfork
);
2590 if ((error
= xfs_bmbt_delete(cur
, &i
)))
2597 * Deleting the first part of the extent.
2599 xfs_bmap_trace_pre_update(fname
, "2", ip
, idx
, whichfork
);
2600 xfs_bmbt_set_startoff(ep
, del_endoff
);
2601 temp
= got
.br_blockcount
- del
->br_blockcount
;
2602 xfs_bmbt_set_blockcount(ep
, temp
);
2603 ifp
->if_lastex
= idx
;
2605 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2607 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
2608 xfs_bmap_trace_post_update(fname
, "2", ip
, idx
,
2613 xfs_bmbt_set_startblock(ep
, del_endblock
);
2614 xfs_bmap_trace_post_update(fname
, "2", ip
, idx
, whichfork
);
2616 flags
|= XFS_ILOG_FEXT(whichfork
);
2619 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
2620 got
.br_blockcount
- del
->br_blockcount
,
2627 * Deleting the last part of the extent.
2629 temp
= got
.br_blockcount
- del
->br_blockcount
;
2630 xfs_bmap_trace_pre_update(fname
, "1", ip
, idx
, whichfork
);
2631 xfs_bmbt_set_blockcount(ep
, temp
);
2632 ifp
->if_lastex
= idx
;
2634 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2636 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
2637 xfs_bmap_trace_post_update(fname
, "1", ip
, idx
,
2642 xfs_bmap_trace_post_update(fname
, "1", ip
, idx
, whichfork
);
2644 flags
|= XFS_ILOG_FEXT(whichfork
);
2647 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
2649 got
.br_blockcount
- del
->br_blockcount
,
2656 * Deleting the middle of the extent.
2658 temp
= del
->br_startoff
- got
.br_startoff
;
2659 xfs_bmap_trace_pre_update(fname
, "0", ip
, idx
, whichfork
);
2660 xfs_bmbt_set_blockcount(ep
, temp
);
2661 new.br_startoff
= del_endoff
;
2662 temp2
= got_endoff
- del_endoff
;
2663 new.br_blockcount
= temp2
;
2664 new.br_state
= got
.br_state
;
2666 new.br_startblock
= del_endblock
;
2667 flags
|= XFS_ILOG_CORE
;
2669 if ((error
= xfs_bmbt_update(cur
,
2671 got
.br_startblock
, temp
,
2674 if ((error
= xfs_bmbt_increment(cur
, 0, &i
)))
2676 cur
->bc_rec
.b
= new;
2677 error
= xfs_bmbt_insert(cur
, &i
);
2678 if (error
&& error
!= ENOSPC
)
2681 * If get no-space back from btree insert,
2682 * it tried a split, and we have a zero
2683 * block reservation.
2684 * Fix up our state and return the error.
2686 if (error
== ENOSPC
) {
2688 * Reset the cursor, don't trust
2689 * it after any insert operation.
2691 if ((error
= xfs_bmbt_lookup_eq(cur
,
2698 * Update the btree record back
2699 * to the original value.
2701 if ((error
= xfs_bmbt_update(cur
,
2708 * Reset the extent record back
2709 * to the original value.
2711 xfs_bmbt_set_blockcount(ep
,
2714 error
= XFS_ERROR(ENOSPC
);
2719 flags
|= XFS_ILOG_FEXT(whichfork
);
2720 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2721 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2723 ASSERT(whichfork
== XFS_DATA_FORK
);
2724 temp
= xfs_bmap_worst_indlen(ip
, temp
);
2725 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
2726 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
2727 new.br_startblock
= NULLSTARTBLOCK((int)temp2
);
2728 da_new
= temp
+ temp2
;
2729 while (da_new
> da_old
) {
2733 xfs_bmbt_set_startblock(ep
,
2734 NULLSTARTBLOCK((int)temp
));
2736 if (da_new
== da_old
)
2742 NULLSTARTBLOCK((int)temp2
);
2746 xfs_bmap_trace_post_update(fname
, "0", ip
, idx
, whichfork
);
2747 xfs_bmap_trace_insert(fname
, "0", ip
, idx
+ 1, 1, &new, NULL
,
2749 xfs_bmap_insert_exlist(ip
, idx
+ 1, 1, &new, whichfork
);
2750 ifp
->if_lastex
= idx
+ 1;
2754 * If we need to, add to list of extents to delete.
2757 xfs_bmap_add_free(del
->br_startblock
, del
->br_blockcount
, flist
,
2760 * Adjust inode # blocks in the file.
2763 ip
->i_d
.di_nblocks
-= nblks
;
2765 * Adjust quota data.
2768 XFS_TRANS_MOD_DQUOT_BYINO(mp
, tp
, ip
, qfield
, (long)-nblks
);
2771 * Account for change in delayed indirect blocks.
2772 * Nothing to do for disk quota accounting here.
2774 ASSERT(da_old
>= da_new
);
2775 if (da_old
> da_new
)
2776 xfs_mod_incore_sb(mp
, XFS_SBS_FDBLOCKS
, (int)(da_old
- da_new
),
2784 * Remove the entry "free" from the free item list. Prev points to the
2785 * previous entry, unless "free" is the head of the list.
2789 xfs_bmap_free_t
*flist
, /* free item list header */
2790 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
2791 xfs_bmap_free_item_t
*free
) /* list item to be freed */
2794 prev
->xbfi_next
= free
->xbfi_next
;
2796 flist
->xbf_first
= free
->xbfi_next
;
2798 kmem_zone_free(xfs_bmap_free_item_zone
, free
);
2802 * Remove count entries from the extents array for inode "ip", starting
2803 * at index "idx". Copies the remaining items down over the deleted ones,
2804 * and gives back the excess memory.
2807 xfs_bmap_delete_exlist(
2808 xfs_inode_t
*ip
, /* incore inode pointer */
2809 xfs_extnum_t idx
, /* starting delete index */
2810 xfs_extnum_t count
, /* count of items to delete */
2811 int whichfork
) /* data or attr fork */
2813 xfs_bmbt_rec_t
*base
; /* base of extent list */
2814 xfs_ifork_t
*ifp
; /* inode fork pointer */
2815 xfs_extnum_t nextents
; /* number of extents in list after */
2817 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2818 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
2819 base
= ifp
->if_u1
.if_extents
;
2820 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - count
;
2821 memmove(&base
[idx
], &base
[idx
+ count
],
2822 (nextents
- idx
) * sizeof(*base
));
2823 xfs_iext_realloc(ip
, -count
, whichfork
);
2827 * Convert an extents-format file into a btree-format file.
2828 * The new file will have a root block (in the inode) and a single child block.
2830 STATIC
int /* error */
2831 xfs_bmap_extents_to_btree(
2832 xfs_trans_t
*tp
, /* transaction pointer */
2833 xfs_inode_t
*ip
, /* incore inode pointer */
2834 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
2835 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
2836 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
2837 int wasdel
, /* converting a delayed alloc */
2838 int *logflagsp
, /* inode logging flags */
2839 int whichfork
) /* data or attr fork */
2841 xfs_bmbt_block_t
*ablock
; /* allocated (child) bt block */
2842 xfs_buf_t
*abp
; /* buffer for ablock */
2843 xfs_alloc_arg_t args
; /* allocation arguments */
2844 xfs_bmbt_rec_t
*arp
; /* child record pointer */
2845 xfs_bmbt_block_t
*block
; /* btree root block */
2846 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
2847 xfs_bmbt_rec_t
*ep
; /* extent list pointer */
2848 int error
; /* error return value */
2849 xfs_extnum_t i
, cnt
; /* extent list index */
2850 xfs_ifork_t
*ifp
; /* inode fork pointer */
2851 xfs_bmbt_key_t
*kp
; /* root block key pointer */
2852 xfs_mount_t
*mp
; /* mount structure */
2853 xfs_extnum_t nextents
; /* extent list size */
2854 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
2856 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2857 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
2858 ASSERT(ifp
->if_ext_max
==
2859 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
2861 * Make space in the inode incore.
2863 xfs_iroot_realloc(ip
, 1, whichfork
);
2864 ifp
->if_flags
|= XFS_IFBROOT
;
2868 block
= ifp
->if_broot
;
2869 block
->bb_magic
= cpu_to_be32(XFS_BMAP_MAGIC
);
2870 block
->bb_level
= cpu_to_be16(1);
2871 block
->bb_numrecs
= cpu_to_be16(1);
2872 block
->bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
2873 block
->bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
2875 * Need a cursor. Can't allocate until bb_level is filled in.
2878 cur
= xfs_btree_init_cursor(mp
, tp
, NULL
, 0, XFS_BTNUM_BMAP
, ip
,
2880 cur
->bc_private
.b
.firstblock
= *firstblock
;
2881 cur
->bc_private
.b
.flist
= flist
;
2882 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
2884 * Convert to a btree with two levels, one record in root.
2886 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
2889 if (*firstblock
== NULLFSBLOCK
) {
2890 args
.type
= XFS_ALLOCTYPE_START_BNO
;
2891 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
2892 } else if (flist
->xbf_low
) {
2893 args
.type
= XFS_ALLOCTYPE_START_BNO
;
2894 args
.fsbno
= *firstblock
;
2896 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
2897 args
.fsbno
= *firstblock
;
2899 args
.minlen
= args
.maxlen
= args
.prod
= 1;
2900 args
.total
= args
.minleft
= args
.alignment
= args
.mod
= args
.isfl
=
2901 args
.minalignslop
= 0;
2902 args
.wasdel
= wasdel
;
2904 if ((error
= xfs_alloc_vextent(&args
))) {
2905 xfs_iroot_realloc(ip
, -1, whichfork
);
2906 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
2910 * Allocation can't fail, the space was reserved.
2912 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
2913 ASSERT(*firstblock
== NULLFSBLOCK
||
2914 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
2916 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
2917 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
2918 cur
->bc_private
.b
.allocated
++;
2919 ip
->i_d
.di_nblocks
++;
2920 XFS_TRANS_MOD_DQUOT_BYINO(mp
, tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
2921 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
2923 * Fill in the child block.
2925 ablock
= XFS_BUF_TO_BMBT_BLOCK(abp
);
2926 ablock
->bb_magic
= cpu_to_be32(XFS_BMAP_MAGIC
);
2927 ablock
->bb_level
= 0;
2928 ablock
->bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
2929 ablock
->bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
2930 arp
= XFS_BMAP_REC_IADDR(ablock
, 1, cur
);
2931 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
2932 for (ep
= ifp
->if_u1
.if_extents
, cnt
= i
= 0; i
< nextents
; i
++, ep
++) {
2933 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep
))) {
2934 arp
->l0
= INT_GET(ep
->l0
, ARCH_CONVERT
);
2935 arp
->l1
= INT_GET(ep
->l1
, ARCH_CONVERT
);
2939 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
2940 ablock
->bb_numrecs
= cpu_to_be16(cnt
);
2942 * Fill in the root key and pointer.
2944 kp
= XFS_BMAP_KEY_IADDR(block
, 1, cur
);
2945 arp
= XFS_BMAP_REC_IADDR(ablock
, 1, cur
);
2946 INT_SET(kp
->br_startoff
, ARCH_CONVERT
, xfs_bmbt_disk_get_startoff(arp
));
2947 pp
= XFS_BMAP_PTR_IADDR(block
, 1, cur
);
2948 INT_SET(*pp
, ARCH_CONVERT
, args
.fsbno
);
2950 * Do all this logging at the end so that
2951 * the root is at the right level.
2953 xfs_bmbt_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
2954 xfs_bmbt_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
2955 ASSERT(*curp
== NULL
);
2957 *logflagsp
= XFS_ILOG_CORE
| XFS_ILOG_FBROOT(whichfork
);
2962 * Insert new item(s) in the extent list for inode "ip".
2963 * Count new items are inserted at offset idx.
2966 xfs_bmap_insert_exlist(
2967 xfs_inode_t
*ip
, /* incore inode pointer */
2968 xfs_extnum_t idx
, /* starting index of new items */
2969 xfs_extnum_t count
, /* number of inserted items */
2970 xfs_bmbt_irec_t
*new, /* items to insert */
2971 int whichfork
) /* data or attr fork */
2973 xfs_bmbt_rec_t
*base
; /* extent list base */
2974 xfs_ifork_t
*ifp
; /* inode fork pointer */
2975 xfs_extnum_t nextents
; /* extent list size */
2976 xfs_extnum_t to
; /* extent list index */
2978 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2979 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
2980 xfs_iext_realloc(ip
, count
, whichfork
);
2981 base
= ifp
->if_u1
.if_extents
;
2982 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
2983 memmove(&base
[idx
+ count
], &base
[idx
],
2984 (nextents
- (idx
+ count
)) * sizeof(*base
));
2985 for (to
= idx
; to
< idx
+ count
; to
++, new++)
2986 xfs_bmbt_set_all(&base
[to
], new);
2990 * Helper routine to reset inode di_forkoff field when switching
2991 * attributes from local to extents. This can increase the space
2992 * we have available for inline extents.
2995 xfs_bmap_forkoff_reset(
3000 if (whichfork
== XFS_ATTR_FORK
&&
3001 (ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
) &&
3002 (ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
) &&
3003 ((mp
->m_attroffset
>> 3) > ip
->i_d
.di_forkoff
)) {
3004 ip
->i_d
.di_forkoff
= mp
->m_attroffset
>> 3;
3005 ip
->i_df
.if_ext_max
= XFS_IFORK_DSIZE(ip
) /
3006 (uint
)sizeof(xfs_bmbt_rec_t
);
3007 ip
->i_afp
->if_ext_max
= XFS_IFORK_ASIZE(ip
) /
3008 (uint
)sizeof(xfs_bmbt_rec_t
);
3013 * Convert a local file to an extents file.
3014 * This code is out of bounds for data forks of regular files,
3015 * since the file data needs to get logged so things will stay consistent.
3016 * (The bmap-level manipulations are ok, though).
3018 STATIC
int /* error */
3019 xfs_bmap_local_to_extents(
3020 xfs_trans_t
*tp
, /* transaction pointer */
3021 xfs_inode_t
*ip
, /* incore inode pointer */
3022 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
3023 xfs_extlen_t total
, /* total blocks needed by transaction */
3024 int *logflagsp
, /* inode logging flags */
3025 int whichfork
) /* data or attr fork */
3027 int error
; /* error return value */
3028 int flags
; /* logging flags returned */
3029 #ifdef XFS_BMAP_TRACE
3030 static char fname
[] = "xfs_bmap_local_to_extents";
3032 xfs_ifork_t
*ifp
; /* inode fork pointer */
3035 * We don't want to deal with the case of keeping inode data inline yet.
3036 * So sending the data fork of a regular inode is invalid.
3038 ASSERT(!((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
&&
3039 whichfork
== XFS_DATA_FORK
));
3040 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3041 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
3044 if (ifp
->if_bytes
) {
3045 xfs_alloc_arg_t args
; /* allocation arguments */
3046 xfs_buf_t
*bp
; /* buffer for extent list block */
3047 xfs_bmbt_rec_t
*ep
; /* extent list pointer */
3050 args
.mp
= ip
->i_mount
;
3051 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
3053 * Allocate a block. We know we need only one, since the
3054 * file currently fits in an inode.
3056 if (*firstblock
== NULLFSBLOCK
) {
3057 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
3058 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3060 args
.fsbno
= *firstblock
;
3061 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3064 args
.mod
= args
.minleft
= args
.alignment
= args
.wasdel
=
3065 args
.isfl
= args
.minalignslop
= 0;
3066 args
.minlen
= args
.maxlen
= args
.prod
= 1;
3067 if ((error
= xfs_alloc_vextent(&args
)))
3070 * Can't fail, the space was reserved.
3072 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
3073 ASSERT(args
.len
== 1);
3074 *firstblock
= args
.fsbno
;
3075 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
3076 memcpy((char *)XFS_BUF_PTR(bp
), ifp
->if_u1
.if_data
,
3078 xfs_trans_log_buf(tp
, bp
, 0, ifp
->if_bytes
- 1);
3079 xfs_bmap_forkoff_reset(args
.mp
, ip
, whichfork
);
3080 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
3081 xfs_iext_realloc(ip
, 1, whichfork
);
3082 ep
= ifp
->if_u1
.if_extents
;
3083 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
3084 xfs_bmap_trace_post_update(fname
, "new", ip
, 0, whichfork
);
3085 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
3086 ip
->i_d
.di_nblocks
= 1;
3087 XFS_TRANS_MOD_DQUOT_BYINO(args
.mp
, tp
, ip
,
3088 XFS_TRANS_DQ_BCOUNT
, 1L);
3089 flags
|= XFS_ILOG_FEXT(whichfork
);
3091 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
3092 xfs_bmap_forkoff_reset(ip
->i_mount
, ip
, whichfork
);
3094 ifp
->if_flags
&= ~XFS_IFINLINE
;
3095 ifp
->if_flags
|= XFS_IFEXTENTS
;
3096 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
3097 flags
|= XFS_ILOG_CORE
;
3103 xfs_bmbt_rec_t
* /* pointer to found extent entry */
3104 xfs_bmap_do_search_extents(
3105 xfs_bmbt_rec_t
*base
, /* base of extent list */
3106 xfs_extnum_t lastx
, /* last extent index used */
3107 xfs_extnum_t nextents
, /* extent list size */
3108 xfs_fileoff_t bno
, /* block number searched for */
3109 int *eofp
, /* out: end of file found */
3110 xfs_extnum_t
*lastxp
, /* out: last extent index */
3111 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
3112 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
3114 xfs_bmbt_rec_t
*ep
; /* extent list entry pointer */
3115 xfs_bmbt_irec_t got
; /* extent list entry, decoded */
3116 int high
; /* high index of binary search */
3117 int low
; /* low index of binary search */
3120 * Initialize the extent entry structure to catch access to
3121 * uninitialized br_startblock field.
3123 got
.br_startoff
= 0xffa5a5a5a5a5a5a5LL
;
3124 got
.br_blockcount
= 0xa55a5a5a5a5a5a5aLL
;
3125 got
.br_state
= XFS_EXT_INVALID
;
3128 got
.br_startblock
= 0xffffa5a5a5a5a5a5LL
;
3130 got
.br_startblock
= 0xffffa5a5;
3133 if (lastx
!= NULLEXTNUM
&& lastx
< nextents
)
3137 prevp
->br_startoff
= NULLFILEOFF
;
3138 if (ep
&& bno
>= (got
.br_startoff
= xfs_bmbt_get_startoff(ep
)) &&
3139 bno
< got
.br_startoff
+
3140 (got
.br_blockcount
= xfs_bmbt_get_blockcount(ep
)))
3142 else if (ep
&& lastx
< nextents
- 1 &&
3143 bno
>= (got
.br_startoff
= xfs_bmbt_get_startoff(ep
+ 1)) &&
3144 bno
< got
.br_startoff
+
3145 (got
.br_blockcount
= xfs_bmbt_get_blockcount(ep
+ 1))) {
3149 } else if (nextents
== 0)
3151 else if (bno
== 0 &&
3152 (got
.br_startoff
= xfs_bmbt_get_startoff(base
)) == 0) {
3155 got
.br_blockcount
= xfs_bmbt_get_blockcount(ep
);
3158 /* binary search the extents array */
3160 high
= nextents
- 1;
3161 while (low
<= high
) {
3162 XFS_STATS_INC(xs_cmp_exlist
);
3163 lastx
= (low
+ high
) >> 1;
3165 got
.br_startoff
= xfs_bmbt_get_startoff(ep
);
3166 got
.br_blockcount
= xfs_bmbt_get_blockcount(ep
);
3167 if (bno
< got
.br_startoff
)
3169 else if (bno
>= got
.br_startoff
+ got
.br_blockcount
)
3172 got
.br_startblock
= xfs_bmbt_get_startblock(ep
);
3173 got
.br_state
= xfs_bmbt_get_state(ep
);
3180 if (bno
>= got
.br_startoff
+ got
.br_blockcount
) {
3182 if (lastx
== nextents
) {
3184 got
.br_startblock
= xfs_bmbt_get_startblock(ep
);
3185 got
.br_state
= xfs_bmbt_get_state(ep
);
3190 xfs_bmbt_get_all(ep
, prevp
);
3192 got
.br_startoff
= xfs_bmbt_get_startoff(ep
);
3193 got
.br_blockcount
= xfs_bmbt_get_blockcount(ep
);
3198 xfs_bmbt_get_all(ep
- 1, prevp
);
3202 got
.br_startblock
= xfs_bmbt_get_startblock(ep
);
3203 got
.br_state
= xfs_bmbt_get_state(ep
);
3211 * Search the extents list for the inode, for the extent containing bno.
3212 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3213 * *eofp will be set, and *prevp will contain the last entry (null if none).
3214 * Else, *lastxp will be set to the index of the found
3215 * entry; *gotp will contain the entry.
3217 STATIC xfs_bmbt_rec_t
* /* pointer to found extent entry */
3218 xfs_bmap_search_extents(
3219 xfs_inode_t
*ip
, /* incore inode pointer */
3220 xfs_fileoff_t bno
, /* block number searched for */
3221 int whichfork
, /* data or attr fork */
3222 int *eofp
, /* out: end of file found */
3223 xfs_extnum_t
*lastxp
, /* out: last extent index */
3224 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
3225 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
3227 xfs_ifork_t
*ifp
; /* inode fork pointer */
3228 xfs_bmbt_rec_t
*base
; /* base of extent list */
3229 xfs_extnum_t lastx
; /* last extent index used */
3230 xfs_extnum_t nextents
; /* extent list size */
3231 xfs_bmbt_rec_t
*ep
; /* extent list entry pointer */
3232 int rt
; /* realtime flag */
3234 XFS_STATS_INC(xs_look_exlist
);
3235 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3236 lastx
= ifp
->if_lastex
;
3237 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
3238 base
= &ifp
->if_u1
.if_extents
[0];
3240 ep
= xfs_bmap_do_search_extents(base
, lastx
, nextents
, bno
, eofp
,
3241 lastxp
, gotp
, prevp
);
3242 rt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
3243 if (unlikely(!rt
&& !gotp
->br_startblock
&& (*lastxp
!= NULLEXTNUM
))) {
3244 cmn_err(CE_PANIC
,"Access to block zero: fs: <%s> inode: %lld "
3245 "start_block : %llx start_off : %llx blkcnt : %llx "
3246 "extent-state : %x \n",
3247 (ip
->i_mount
)->m_fsname
, (long long)ip
->i_ino
,
3248 (unsigned long long)gotp
->br_startblock
,
3249 (unsigned long long)gotp
->br_startoff
,
3250 (unsigned long long)gotp
->br_blockcount
,
3257 * Compute the worst-case number of indirect blocks that will be used
3258 * for ip's delayed extent of length "len".
3260 STATIC xfs_filblks_t
3261 xfs_bmap_worst_indlen(
3262 xfs_inode_t
*ip
, /* incore inode pointer */
3263 xfs_filblks_t len
) /* delayed extent length */
3265 int level
; /* btree level number */
3266 int maxrecs
; /* maximum record count at this level */
3267 xfs_mount_t
*mp
; /* mount structure */
3268 xfs_filblks_t rval
; /* return value */
3271 maxrecs
= mp
->m_bmap_dmxr
[0];
3272 for (level
= 0, rval
= 0;
3273 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
3276 do_div(len
, maxrecs
);
3279 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
3282 maxrecs
= mp
->m_bmap_dmxr
[1];
3288 * Convert inode from non-attributed to attributed.
3289 * Must not be in a transaction, ip must not be locked.
3291 int /* error code */
3292 xfs_bmap_add_attrfork(
3293 xfs_inode_t
*ip
, /* incore inode pointer */
3294 int size
, /* space new attribute needs */
3295 int rsvd
) /* xact may use reserved blks */
3297 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
3298 xfs_bmap_free_t flist
; /* freed extent list */
3299 xfs_mount_t
*mp
; /* mount structure */
3300 xfs_trans_t
*tp
; /* transaction pointer */
3301 unsigned long s
; /* spinlock spl value */
3302 int blks
; /* space reservation */
3303 int version
= 1; /* superblock attr version */
3304 int committed
; /* xaction was committed */
3305 int logflags
; /* logging flags */
3306 int error
; /* error return value */
3308 ASSERT(XFS_IFORK_Q(ip
) == 0);
3309 ASSERT(ip
->i_df
.if_ext_max
==
3310 XFS_IFORK_DSIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
));
3313 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
3314 tp
= xfs_trans_alloc(mp
, XFS_TRANS_ADDAFORK
);
3315 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
3317 tp
->t_flags
|= XFS_TRANS_RESERVE
;
3318 if ((error
= xfs_trans_reserve(tp
, blks
, XFS_ADDAFORK_LOG_RES(mp
), 0,
3319 XFS_TRANS_PERM_LOG_RES
, XFS_ADDAFORK_LOG_COUNT
)))
3321 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3322 error
= XFS_TRANS_RESERVE_QUOTA_NBLKS(mp
, tp
, ip
, blks
, 0, rsvd
?
3323 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
3324 XFS_QMOPT_RES_REGBLKS
);
3326 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3327 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
);
3330 if (XFS_IFORK_Q(ip
))
3332 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
3334 * For inodes coming from pre-6.2 filesystems.
3336 ASSERT(ip
->i_d
.di_aformat
== 0);
3337 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
3339 ASSERT(ip
->i_d
.di_anextents
== 0);
3340 VN_HOLD(XFS_ITOV(ip
));
3341 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
3342 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
3343 switch (ip
->i_d
.di_format
) {
3344 case XFS_DINODE_FMT_DEV
:
3345 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
3347 case XFS_DINODE_FMT_UUID
:
3348 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
3350 case XFS_DINODE_FMT_LOCAL
:
3351 case XFS_DINODE_FMT_EXTENTS
:
3352 case XFS_DINODE_FMT_BTREE
:
3353 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
3354 if (!ip
->i_d
.di_forkoff
)
3355 ip
->i_d
.di_forkoff
= mp
->m_attroffset
>> 3;
3356 else if (!(mp
->m_flags
& XFS_MOUNT_COMPAT_ATTR
))
3361 error
= XFS_ERROR(EINVAL
);
3364 ip
->i_df
.if_ext_max
=
3365 XFS_IFORK_DSIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
);
3366 ASSERT(ip
->i_afp
== NULL
);
3367 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
3368 ip
->i_afp
->if_ext_max
=
3369 XFS_IFORK_ASIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
);
3370 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
3372 XFS_BMAP_INIT(&flist
, &firstblock
);
3373 switch (ip
->i_d
.di_format
) {
3374 case XFS_DINODE_FMT_LOCAL
:
3375 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &flist
,
3378 case XFS_DINODE_FMT_EXTENTS
:
3379 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
3382 case XFS_DINODE_FMT_BTREE
:
3383 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &flist
,
3391 xfs_trans_log_inode(tp
, ip
, logflags
);
3394 if (!XFS_SB_VERSION_HASATTR(&mp
->m_sb
) ||
3395 (!XFS_SB_VERSION_HASATTR2(&mp
->m_sb
) && version
== 2)) {
3396 __int64_t sbfields
= 0;
3398 s
= XFS_SB_LOCK(mp
);
3399 if (!XFS_SB_VERSION_HASATTR(&mp
->m_sb
)) {
3400 XFS_SB_VERSION_ADDATTR(&mp
->m_sb
);
3401 sbfields
|= XFS_SB_VERSIONNUM
;
3403 if (!XFS_SB_VERSION_HASATTR2(&mp
->m_sb
) && version
== 2) {
3404 XFS_SB_VERSION_ADDATTR2(&mp
->m_sb
);
3405 sbfields
|= (XFS_SB_VERSIONNUM
| XFS_SB_FEATURES2
);
3408 XFS_SB_UNLOCK(mp
, s
);
3409 xfs_mod_sb(tp
, sbfields
);
3411 XFS_SB_UNLOCK(mp
, s
);
3413 if ((error
= xfs_bmap_finish(&tp
, &flist
, firstblock
, &committed
)))
3415 error
= xfs_trans_commit(tp
, XFS_TRANS_PERM_LOG_RES
, NULL
);
3416 ASSERT(ip
->i_df
.if_ext_max
==
3417 XFS_IFORK_DSIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
));
3420 xfs_bmap_cancel(&flist
);
3422 ASSERT(ismrlocked(&ip
->i_lock
,MR_UPDATE
));
3423 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3425 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
3426 ASSERT(ip
->i_df
.if_ext_max
==
3427 XFS_IFORK_DSIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
));
3432 * Add the extent to the list of extents to be free at transaction end.
3433 * The list is maintained sorted (by block number).
3438 xfs_fsblock_t bno
, /* fs block number of extent */
3439 xfs_filblks_t len
, /* length of extent */
3440 xfs_bmap_free_t
*flist
, /* list of extents */
3441 xfs_mount_t
*mp
) /* mount point structure */
3443 xfs_bmap_free_item_t
*cur
; /* current (next) element */
3444 xfs_bmap_free_item_t
*new; /* new element */
3445 xfs_bmap_free_item_t
*prev
; /* previous element */
3447 xfs_agnumber_t agno
;
3448 xfs_agblock_t agbno
;
3450 ASSERT(bno
!= NULLFSBLOCK
);
3452 ASSERT(len
<= MAXEXTLEN
);
3453 ASSERT(!ISNULLSTARTBLOCK(bno
));
3454 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
3455 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
3456 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
3457 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
3458 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
3459 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
3461 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
3462 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
3463 new->xbfi_startblock
= bno
;
3464 new->xbfi_blockcount
= (xfs_extlen_t
)len
;
3465 for (prev
= NULL
, cur
= flist
->xbf_first
;
3467 prev
= cur
, cur
= cur
->xbfi_next
) {
3468 if (cur
->xbfi_startblock
>= bno
)
3472 prev
->xbfi_next
= new;
3474 flist
->xbf_first
= new;
3475 new->xbfi_next
= cur
;
3480 * Compute and fill in the value of the maximum depth of a bmap btree
3481 * in this filesystem. Done once, during mount.
3484 xfs_bmap_compute_maxlevels(
3485 xfs_mount_t
*mp
, /* file system mount structure */
3486 int whichfork
) /* data or attr fork */
3488 int level
; /* btree level */
3489 uint maxblocks
; /* max blocks at this level */
3490 uint maxleafents
; /* max leaf entries possible */
3491 int maxrootrecs
; /* max records in root block */
3492 int minleafrecs
; /* min records in leaf block */
3493 int minnoderecs
; /* min records in node block */
3494 int sz
; /* root block size */
3497 * The maximum number of extents in a file, hence the maximum
3498 * number of leaf entries, is controlled by the type of di_nextents
3499 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3500 * (a signed 16-bit number, xfs_aextnum_t).
3502 if (whichfork
== XFS_DATA_FORK
) {
3503 maxleafents
= MAXEXTNUM
;
3504 sz
= (mp
->m_flags
& XFS_MOUNT_COMPAT_ATTR
) ?
3505 mp
->m_attroffset
: XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
3507 maxleafents
= MAXAEXTNUM
;
3508 sz
= (mp
->m_flags
& XFS_MOUNT_COMPAT_ATTR
) ?
3509 mp
->m_sb
.sb_inodesize
- mp
->m_attroffset
:
3510 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
3512 maxrootrecs
= (int)XFS_BTREE_BLOCK_MAXRECS(sz
, xfs_bmdr
, 0);
3513 minleafrecs
= mp
->m_bmap_dmnr
[0];
3514 minnoderecs
= mp
->m_bmap_dmnr
[1];
3515 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
3516 for (level
= 1; maxblocks
> 1; level
++) {
3517 if (maxblocks
<= maxrootrecs
)
3520 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
3522 mp
->m_bm_maxlevels
[whichfork
] = level
;
3526 * Free up any items left in the list.
3530 xfs_bmap_free_t
*flist
) /* list of bmap_free_items */
3532 xfs_bmap_free_item_t
*free
; /* free list item */
3533 xfs_bmap_free_item_t
*next
;
3535 if (flist
->xbf_count
== 0)
3537 ASSERT(flist
->xbf_first
!= NULL
);
3538 for (free
= flist
->xbf_first
; free
; free
= next
) {
3539 next
= free
->xbfi_next
;
3540 xfs_bmap_del_free(flist
, NULL
, free
);
3542 ASSERT(flist
->xbf_count
== 0);
3546 * Returns the file-relative block number of the first unused block(s)
3547 * in the file with at least "len" logically contiguous blocks free.
3548 * This is the lowest-address hole if the file has holes, else the first block
3549 * past the end of file.
3550 * Return 0 if the file is currently local (in-inode).
3553 xfs_bmap_first_unused(
3554 xfs_trans_t
*tp
, /* transaction pointer */
3555 xfs_inode_t
*ip
, /* incore inode */
3556 xfs_extlen_t len
, /* size of hole to find */
3557 xfs_fileoff_t
*first_unused
, /* unused block */
3558 int whichfork
) /* data or attr fork */
3560 xfs_bmbt_rec_t
*base
; /* base of extent array */
3561 xfs_bmbt_rec_t
*ep
; /* pointer to an extent entry */
3562 int error
; /* error return value */
3563 xfs_ifork_t
*ifp
; /* inode fork pointer */
3564 xfs_fileoff_t lastaddr
; /* last block number seen */
3565 xfs_fileoff_t lowest
; /* lowest useful block */
3566 xfs_fileoff_t max
; /* starting useful block */
3567 xfs_fileoff_t off
; /* offset for this block */
3568 xfs_extnum_t nextents
; /* number of extent entries */
3570 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
3571 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
3572 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
3573 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
3577 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3578 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
3579 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
3581 lowest
= *first_unused
;
3582 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
3583 base
= &ifp
->if_u1
.if_extents
[0];
3584 for (lastaddr
= 0, max
= lowest
, ep
= base
;
3585 ep
< &base
[nextents
];
3587 off
= xfs_bmbt_get_startoff(ep
);
3589 * See if the hole before this extent will work.
3591 if (off
>= lowest
+ len
&& off
- max
>= len
) {
3592 *first_unused
= max
;
3595 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
3596 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
3598 *first_unused
= max
;
3603 * Returns the file-relative block number of the last block + 1 before
3604 * last_block (input value) in the file.
3605 * This is not based on i_size, it is based on the extent list.
3606 * Returns 0 for local files, as they do not have an extent list.
3609 xfs_bmap_last_before(
3610 xfs_trans_t
*tp
, /* transaction pointer */
3611 xfs_inode_t
*ip
, /* incore inode */
3612 xfs_fileoff_t
*last_block
, /* last block */
3613 int whichfork
) /* data or attr fork */
3615 xfs_fileoff_t bno
; /* input file offset */
3616 int eof
; /* hit end of file */
3617 xfs_bmbt_rec_t
*ep
; /* pointer to last extent */
3618 int error
; /* error return value */
3619 xfs_bmbt_irec_t got
; /* current extent value */
3620 xfs_ifork_t
*ifp
; /* inode fork pointer */
3621 xfs_extnum_t lastx
; /* last extent used */
3622 xfs_bmbt_irec_t prev
; /* previous extent value */
3624 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
3625 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3626 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
3627 return XFS_ERROR(EIO
);
3628 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
3632 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3633 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
3634 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
3636 bno
= *last_block
- 1;
3637 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
3639 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
3640 if (prev
.br_startoff
== NULLFILEOFF
)
3643 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
3646 * Otherwise *last_block is already the right answer.
3652 * Returns the file-relative block number of the first block past eof in
3653 * the file. This is not based on i_size, it is based on the extent list.
3654 * Returns 0 for local files, as they do not have an extent list.
3657 xfs_bmap_last_offset(
3658 xfs_trans_t
*tp
, /* transaction pointer */
3659 xfs_inode_t
*ip
, /* incore inode */
3660 xfs_fileoff_t
*last_block
, /* last block */
3661 int whichfork
) /* data or attr fork */
3663 xfs_bmbt_rec_t
*base
; /* base of extent array */
3664 xfs_bmbt_rec_t
*ep
; /* pointer to last extent */
3665 int error
; /* error return value */
3666 xfs_ifork_t
*ifp
; /* inode fork pointer */
3667 xfs_extnum_t nextents
; /* number of extent entries */
3669 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
3670 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3671 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
3672 return XFS_ERROR(EIO
);
3673 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
3677 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3678 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
3679 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
3681 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
3686 base
= &ifp
->if_u1
.if_extents
[0];
3687 ASSERT(base
!= NULL
);
3688 ep
= &base
[nextents
- 1];
3689 *last_block
= xfs_bmbt_get_startoff(ep
) + xfs_bmbt_get_blockcount(ep
);
3694 * Returns whether the selected fork of the inode has exactly one
3695 * block or not. For the data fork we check this matches di_size,
3696 * implying the file's range is 0..bsize-1.
3698 int /* 1=>1 block, 0=>otherwise */
3700 xfs_inode_t
*ip
, /* incore inode */
3701 int whichfork
) /* data or attr fork */
3703 xfs_bmbt_rec_t
*ep
; /* ptr to fork's extent */
3704 xfs_ifork_t
*ifp
; /* inode fork pointer */
3705 int rval
; /* return value */
3706 xfs_bmbt_irec_t s
; /* internal version of extent */
3709 if (whichfork
== XFS_DATA_FORK
)
3710 return ip
->i_d
.di_size
== ip
->i_mount
->m_sb
.sb_blocksize
;
3712 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
3714 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
3716 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3717 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
3718 ep
= ifp
->if_u1
.if_extents
;
3719 xfs_bmbt_get_all(ep
, &s
);
3720 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
3721 if (rval
&& whichfork
== XFS_DATA_FORK
)
3722 ASSERT(ip
->i_d
.di_size
== ip
->i_mount
->m_sb
.sb_blocksize
);
3727 * Read in the extents to if_extents.
3728 * All inode fields are set up by caller, we just traverse the btree
3729 * and copy the records in. If the file system cannot contain unwritten
3730 * extents, the records are checked for no "state" flags.
3733 xfs_bmap_read_extents(
3734 xfs_trans_t
*tp
, /* transaction pointer */
3735 xfs_inode_t
*ip
, /* incore inode */
3736 int whichfork
) /* data or attr fork */
3738 xfs_bmbt_block_t
*block
; /* current btree block */
3739 xfs_fsblock_t bno
; /* block # of "block" */
3740 xfs_buf_t
*bp
; /* buffer for "block" */
3741 int error
; /* error return value */
3742 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
3743 #ifdef XFS_BMAP_TRACE
3744 static char fname
[] = "xfs_bmap_read_extents";
3746 xfs_extnum_t i
, j
; /* index into the extents list */
3747 xfs_ifork_t
*ifp
; /* fork structure */
3748 int level
; /* btree level, for checking */
3749 xfs_mount_t
*mp
; /* file system mount structure */
3750 xfs_bmbt_ptr_t
*pp
; /* pointer to block address */
3752 xfs_extnum_t room
; /* number of entries there's room for */
3753 xfs_bmbt_rec_t
*trp
; /* target record pointer */
3757 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3758 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
3759 XFS_EXTFMT_INODE(ip
);
3760 block
= ifp
->if_broot
;
3762 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
3764 level
= be16_to_cpu(block
->bb_level
);
3766 pp
= XFS_BMAP_BROOT_PTR_ADDR(block
, 1, ifp
->if_broot_bytes
);
3767 ASSERT(INT_GET(*pp
, ARCH_CONVERT
) != NULLDFSBNO
);
3768 ASSERT(XFS_FSB_TO_AGNO(mp
, INT_GET(*pp
, ARCH_CONVERT
)) < mp
->m_sb
.sb_agcount
);
3769 ASSERT(XFS_FSB_TO_AGBNO(mp
, INT_GET(*pp
, ARCH_CONVERT
)) < mp
->m_sb
.sb_agblocks
);
3770 bno
= INT_GET(*pp
, ARCH_CONVERT
);
3772 * Go down the tree until leaf level is reached, following the first
3773 * pointer (leftmost) at each level.
3775 while (level
-- > 0) {
3776 if ((error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
3777 XFS_BMAP_BTREE_REF
)))
3779 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
3780 XFS_WANT_CORRUPTED_GOTO(
3781 XFS_BMAP_SANITY_CHECK(mp
, block
, level
),
3785 pp
= XFS_BTREE_PTR_ADDR(mp
->m_sb
.sb_blocksize
, xfs_bmbt
, block
,
3786 1, mp
->m_bmap_dmxr
[1]);
3787 XFS_WANT_CORRUPTED_GOTO(
3788 XFS_FSB_SANITY_CHECK(mp
, INT_GET(*pp
, ARCH_CONVERT
)),
3790 bno
= INT_GET(*pp
, ARCH_CONVERT
);
3791 xfs_trans_brelse(tp
, bp
);
3794 * Here with bp and block set to the leftmost leaf node in the tree.
3796 room
= ifp
->if_bytes
/ (uint
)sizeof(*trp
);
3797 trp
= ifp
->if_u1
.if_extents
;
3800 * Loop over all leaf nodes. Copy information to the extent list.
3803 xfs_bmbt_rec_t
*frp
, *temp
;
3804 xfs_fsblock_t nextbno
;
3805 xfs_extnum_t num_recs
;
3808 num_recs
= be16_to_cpu(block
->bb_numrecs
);
3809 if (unlikely(i
+ num_recs
> room
)) {
3810 ASSERT(i
+ num_recs
<= room
);
3811 xfs_fs_cmn_err(CE_WARN
, ip
->i_mount
,
3812 "corrupt dinode %Lu, (btree extents). Unmount and run xfs_repair.",
3813 (unsigned long long) ip
->i_ino
);
3814 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
3819 XFS_WANT_CORRUPTED_GOTO(
3820 XFS_BMAP_SANITY_CHECK(mp
, block
, 0),
3823 * Read-ahead the next leaf block, if any.
3825 nextbno
= be64_to_cpu(block
->bb_rightsib
);
3826 if (nextbno
!= NULLFSBLOCK
)
3827 xfs_btree_reada_bufl(mp
, nextbno
, 1);
3829 * Copy records into the extent list.
3831 frp
= XFS_BTREE_REC_ADDR(mp
->m_sb
.sb_blocksize
, xfs_bmbt
,
3832 block
, 1, mp
->m_bmap_dmxr
[0]);
3834 for (j
= 0; j
< num_recs
; j
++, frp
++, trp
++) {
3835 trp
->l0
= INT_GET(frp
->l0
, ARCH_CONVERT
);
3836 trp
->l1
= INT_GET(frp
->l1
, ARCH_CONVERT
);
3838 if (exntf
== XFS_EXTFMT_NOSTATE
) {
3840 * Check all attribute bmap btree records and
3841 * any "older" data bmap btree records for a
3842 * set bit in the "extent flag" position.
3844 if (unlikely(xfs_check_nostate_extents(temp
, num_recs
))) {
3845 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
3852 xfs_trans_brelse(tp
, bp
);
3855 * If we've reached the end, stop.
3857 if (bno
== NULLFSBLOCK
)
3859 if ((error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
3860 XFS_BMAP_BTREE_REF
)))
3862 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
3864 ASSERT(i
== ifp
->if_bytes
/ (uint
)sizeof(*trp
));
3865 ASSERT(i
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
3866 xfs_bmap_trace_exlist(fname
, ip
, i
, whichfork
);
3869 xfs_trans_brelse(tp
, bp
);
3870 return XFS_ERROR(EFSCORRUPTED
);
3874 * Map file blocks to filesystem blocks.
3875 * File range is given by the bno/len pair.
3876 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
3877 * into a hole or past eof.
3878 * Only allocates blocks from a single allocation group,
3879 * to avoid locking problems.
3880 * The returned value in "firstblock" from the first call in a transaction
3881 * must be remembered and presented to subsequent calls in "firstblock".
3882 * An upper bound for the number of blocks to be allocated is supplied to
3883 * the first call in "total"; if no allocation group has that many free
3884 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
3888 xfs_trans_t
*tp
, /* transaction pointer */
3889 xfs_inode_t
*ip
, /* incore inode */
3890 xfs_fileoff_t bno
, /* starting file offs. mapped */
3891 xfs_filblks_t len
, /* length to map in file */
3892 int flags
, /* XFS_BMAPI_... */
3893 xfs_fsblock_t
*firstblock
, /* first allocated block
3894 controls a.g. for allocs */
3895 xfs_extlen_t total
, /* total blocks needed */
3896 xfs_bmbt_irec_t
*mval
, /* output: map values */
3897 int *nmap
, /* i/o: mval size/count */
3898 xfs_bmap_free_t
*flist
) /* i/o: list extents to free */
3900 xfs_fsblock_t abno
; /* allocated block number */
3901 xfs_extlen_t alen
; /* allocated extent length */
3902 xfs_fileoff_t aoff
; /* allocated file offset */
3903 xfs_bmalloca_t bma
; /* args for xfs_bmap_alloc */
3904 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
3905 xfs_fileoff_t end
; /* end of mapped file region */
3906 int eof
; /* we've hit the end of extent list */
3907 char contig
; /* allocation must be one extent */
3908 char delay
; /* this request is for delayed alloc */
3909 char exact
; /* don't do all of wasdelayed extent */
3910 char convert
; /* unwritten extent I/O completion */
3911 xfs_bmbt_rec_t
*ep
; /* extent list entry pointer */
3912 int error
; /* error return */
3913 xfs_bmbt_irec_t got
; /* current extent list record */
3914 xfs_ifork_t
*ifp
; /* inode fork pointer */
3915 xfs_extlen_t indlen
; /* indirect blocks length */
3916 xfs_extnum_t lastx
; /* last useful extent number */
3917 int logflags
; /* flags for transaction logging */
3918 xfs_extlen_t minleft
; /* min blocks left after allocation */
3919 xfs_extlen_t minlen
; /* min allocation size */
3920 xfs_mount_t
*mp
; /* xfs mount structure */
3921 int n
; /* current extent index */
3922 int nallocs
; /* number of extents alloc\'d */
3923 xfs_extnum_t nextents
; /* number of extents in file */
3924 xfs_fileoff_t obno
; /* old block number (offset) */
3925 xfs_bmbt_irec_t prev
; /* previous extent list record */
3926 int tmp_logflags
; /* temp flags holder */
3927 int whichfork
; /* data or attr fork */
3928 char inhole
; /* current location is hole in file */
3929 char stateless
; /* ignore state flag set */
3930 char trim
; /* output trimmed to match range */
3931 char userdata
; /* allocating non-metadata */
3932 char wasdelay
; /* old extent was delayed */
3933 char wr
; /* this is a write request */
3934 char rt
; /* this is a realtime file */
3935 char rsvd
; /* OK to allocate reserved blocks */
3937 xfs_fileoff_t orig_bno
; /* original block number value */
3938 int orig_flags
; /* original flags arg value */
3939 xfs_filblks_t orig_len
; /* original value of len arg */
3940 xfs_bmbt_irec_t
*orig_mval
; /* original value of mval */
3941 int orig_nmap
; /* original value of *nmap */
3950 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
|| !(flags
& XFS_BMAPI_WRITE
));
3951 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
3952 XFS_ATTR_FORK
: XFS_DATA_FORK
;
3954 if (unlikely(XFS_TEST_ERROR(
3955 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3956 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
3957 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
),
3958 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
3959 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW
, mp
);
3960 return XFS_ERROR(EFSCORRUPTED
);
3962 if (XFS_FORCED_SHUTDOWN(mp
))
3963 return XFS_ERROR(EIO
);
3964 rt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
3965 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3966 ASSERT(ifp
->if_ext_max
==
3967 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
3968 if ((wr
= (flags
& XFS_BMAPI_WRITE
)) != 0)
3969 XFS_STATS_INC(xs_blk_mapw
);
3971 XFS_STATS_INC(xs_blk_mapr
);
3972 delay
= (flags
& XFS_BMAPI_DELAY
) != 0;
3973 trim
= (flags
& XFS_BMAPI_ENTIRE
) == 0;
3974 userdata
= (flags
& XFS_BMAPI_METADATA
) == 0;
3975 convert
= (flags
& XFS_BMAPI_CONVERT
) != 0;
3976 exact
= (flags
& XFS_BMAPI_EXACT
) != 0;
3977 rsvd
= (flags
& XFS_BMAPI_RSVBLOCKS
) != 0;
3978 contig
= (flags
& XFS_BMAPI_CONTIG
) != 0;
3980 * stateless is used to combine extents which
3981 * differ only due to the state of the extents.
3982 * This technique is used from xfs_getbmap()
3983 * when the caller does not wish to see the
3984 * separation (which is the default).
3986 * This technique is also used when writing a
3987 * buffer which has been partially written,
3988 * (usually by being flushed during a chunkread),
3989 * to ensure one write takes place. This also
3990 * prevents a change in the xfs inode extents at
3991 * this time, intentionally. This change occurs
3992 * on completion of the write operation, in
3993 * xfs_strat_comp(), where the xfs_bmapi() call
3994 * is transactioned, and the extents combined.
3996 stateless
= (flags
& XFS_BMAPI_IGSTATE
) != 0;
3997 if (stateless
&& wr
) /* if writing unwritten space, no */
3998 wr
= 0; /* allocations are allowed */
3999 ASSERT(wr
|| !delay
);
4003 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
4005 if ((error
= xfs_bmap_local_to_extents(tp
, ip
,
4006 firstblock
, total
, &logflags
, whichfork
)))
4009 if (wr
&& *firstblock
== NULLFSBLOCK
) {
4010 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4011 minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4016 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
4017 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
4019 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
4021 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
4026 while (bno
< end
&& n
< *nmap
) {
4028 * Reading past eof, act as though there's a hole
4032 got
.br_startoff
= end
;
4033 inhole
= eof
|| got
.br_startoff
> bno
;
4034 wasdelay
= wr
&& !inhole
&& !delay
&&
4035 ISNULLSTARTBLOCK(got
.br_startblock
);
4037 * First, deal with the hole before the allocated space
4038 * that we found, if any.
4040 if (wr
&& (inhole
|| wasdelay
)) {
4042 * For the wasdelay case, we could also just
4043 * allocate the stuff asked for in this bmap call
4044 * but that wouldn't be as good.
4046 if (wasdelay
&& !exact
) {
4047 alen
= (xfs_extlen_t
)got
.br_blockcount
;
4048 aoff
= got
.br_startoff
;
4049 if (lastx
!= NULLEXTNUM
&& lastx
) {
4050 ep
= &ifp
->if_u1
.if_extents
[lastx
- 1];
4051 xfs_bmbt_get_all(ep
, &prev
);
4053 } else if (wasdelay
) {
4054 alen
= (xfs_extlen_t
)
4055 XFS_FILBLKS_MIN(len
,
4057 got
.br_blockcount
) - bno
);
4060 alen
= (xfs_extlen_t
)
4061 XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
4063 alen
= (xfs_extlen_t
)
4064 XFS_FILBLKS_MIN(alen
,
4065 got
.br_startoff
- bno
);
4068 minlen
= contig
? alen
: 1;
4072 /* Figure out the extent size, adjust alen */
4074 if (!(extsz
= ip
->i_d
.di_extsize
))
4075 extsz
= mp
->m_sb
.sb_rextsize
;
4077 extsz
= ip
->i_d
.di_extsize
;
4080 error
= xfs_bmap_extsize_align(mp
,
4082 rt
, eof
, delay
, convert
,
4088 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4091 * Make a transaction-less quota reservation for
4092 * delayed allocation blocks. This number gets
4094 * We return EDQUOT if we haven't allocated
4095 * blks already inside this loop;
4097 if (XFS_TRANS_RESERVE_QUOTA_NBLKS(
4098 mp
, NULL
, ip
, (long)alen
, 0,
4099 rt
? XFS_QMOPT_RES_RTBLKS
:
4100 XFS_QMOPT_RES_REGBLKS
)) {
4103 ASSERT(cur
== NULL
);
4104 return XFS_ERROR(EDQUOT
);
4110 * Split changing sb for alen and indlen since
4111 * they could be coming from different places.
4113 indlen
= (xfs_extlen_t
)
4114 xfs_bmap_worst_indlen(ip
, alen
);
4118 error
= xfs_mod_incore_sb(mp
,
4122 error
= xfs_mod_incore_sb(mp
,
4127 error
= xfs_mod_incore_sb(mp
,
4131 xfs_mod_incore_sb(mp
,
4135 xfs_mod_incore_sb(mp
,
4141 if (XFS_IS_QUOTA_ON(mp
))
4142 /* unreserve the blocks now */
4144 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
4147 XFS_QMOPT_RES_RTBLKS
:
4148 XFS_QMOPT_RES_REGBLKS
);
4152 ip
->i_delayed_blks
+= alen
;
4153 abno
= NULLSTARTBLOCK(indlen
);
4156 * If first time, allocate and fill in
4157 * once-only bma fields.
4159 if (bma
.ip
== NULL
) {
4167 /* Indicate if this is the first user data
4168 * in the file, or just any user data.
4171 bma
.userdata
= (aoff
== 0) ?
4172 XFS_ALLOC_INITIAL_USER_DATA
:
4176 * Fill in changeable bma fields.
4179 bma
.firstblock
= *firstblock
;
4183 bma
.wasdel
= wasdelay
;
4184 bma
.minlen
= minlen
;
4185 bma
.low
= flist
->xbf_low
;
4186 bma
.minleft
= minleft
;
4188 * Only want to do the alignment at the
4189 * eof if it is userdata and allocation length
4190 * is larger than a stripe unit.
4192 if (mp
->m_dalign
&& alen
>= mp
->m_dalign
&&
4193 userdata
&& whichfork
== XFS_DATA_FORK
) {
4194 if ((error
= xfs_bmap_isaeof(ip
, aoff
,
4195 whichfork
, &bma
.aeof
)))
4202 if ((error
= xfs_bmap_alloc(&bma
)))
4205 * Copy out result fields.
4208 if ((flist
->xbf_low
= bma
.low
))
4212 ASSERT(*firstblock
== NULLFSBLOCK
||
4213 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
4214 XFS_FSB_TO_AGNO(mp
, bma
.firstblock
) ||
4216 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
4217 XFS_FSB_TO_AGNO(mp
, bma
.firstblock
)));
4218 *firstblock
= bma
.firstblock
;
4220 cur
->bc_private
.b
.firstblock
=
4222 if (abno
== NULLFSBLOCK
)
4224 if ((ifp
->if_flags
& XFS_IFBROOT
) && !cur
) {
4225 cur
= xfs_btree_init_cursor(mp
,
4226 tp
, NULL
, 0, XFS_BTNUM_BMAP
,
4228 cur
->bc_private
.b
.firstblock
=
4230 cur
->bc_private
.b
.flist
= flist
;
4233 * Bump the number of extents we've allocated
4239 cur
->bc_private
.b
.flags
=
4240 wasdelay
? XFS_BTCUR_BPRV_WASDEL
: 0;
4241 got
.br_startoff
= aoff
;
4242 got
.br_startblock
= abno
;
4243 got
.br_blockcount
= alen
;
4244 got
.br_state
= XFS_EXT_NORM
; /* assume normal */
4246 * Determine state of extent, and the filesystem.
4247 * A wasdelay extent has been initialized, so
4248 * shouldn't be flagged as unwritten.
4250 if (wr
&& XFS_SB_VERSION_HASEXTFLGBIT(&mp
->m_sb
)) {
4251 if (!wasdelay
&& (flags
& XFS_BMAPI_PREALLOC
))
4252 got
.br_state
= XFS_EXT_UNWRITTEN
;
4254 error
= xfs_bmap_add_extent(ip
, lastx
, &cur
, &got
,
4255 firstblock
, flist
, &tmp_logflags
, whichfork
,
4257 logflags
|= tmp_logflags
;
4260 lastx
= ifp
->if_lastex
;
4261 ep
= &ifp
->if_u1
.if_extents
[lastx
];
4262 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
4263 xfs_bmbt_get_all(ep
, &got
);
4264 ASSERT(got
.br_startoff
<= aoff
);
4265 ASSERT(got
.br_startoff
+ got
.br_blockcount
>=
4269 ASSERT(ISNULLSTARTBLOCK(got
.br_startblock
));
4270 ASSERT(STARTBLOCKVAL(got
.br_startblock
) > 0);
4272 ASSERT(got
.br_state
== XFS_EXT_NORM
||
4273 got
.br_state
== XFS_EXT_UNWRITTEN
);
4276 * Fall down into the found allocated space case.
4278 } else if (inhole
) {
4280 * Reading in a hole.
4282 mval
->br_startoff
= bno
;
4283 mval
->br_startblock
= HOLESTARTBLOCK
;
4284 mval
->br_blockcount
=
4285 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4286 mval
->br_state
= XFS_EXT_NORM
;
4287 bno
+= mval
->br_blockcount
;
4288 len
-= mval
->br_blockcount
;
4294 * Then deal with the allocated space we found.
4297 if (trim
&& (got
.br_startoff
+ got
.br_blockcount
> obno
)) {
4300 ASSERT((bno
>= obno
) || (n
== 0));
4302 mval
->br_startoff
= bno
;
4303 if (ISNULLSTARTBLOCK(got
.br_startblock
)) {
4304 ASSERT(!wr
|| delay
);
4305 mval
->br_startblock
= DELAYSTARTBLOCK
;
4307 mval
->br_startblock
=
4309 (bno
- got
.br_startoff
);
4311 * Return the minimum of what we got and what we
4312 * asked for for the length. We can use the len
4313 * variable here because it is modified below
4314 * and we could have been there before coming
4315 * here if the first part of the allocation
4316 * didn't overlap what was asked for.
4318 mval
->br_blockcount
=
4319 XFS_FILBLKS_MIN(end
- bno
, got
.br_blockcount
-
4320 (bno
- got
.br_startoff
));
4321 mval
->br_state
= got
.br_state
;
4322 ASSERT(mval
->br_blockcount
<= len
);
4325 if (ISNULLSTARTBLOCK(mval
->br_startblock
)) {
4326 ASSERT(!wr
|| delay
);
4327 mval
->br_startblock
= DELAYSTARTBLOCK
;
4332 * Check if writing previously allocated but
4333 * unwritten extents.
4335 if (wr
&& mval
->br_state
== XFS_EXT_UNWRITTEN
&&
4336 ((flags
& (XFS_BMAPI_PREALLOC
|XFS_BMAPI_DELAY
)) == 0)) {
4338 * Modify (by adding) the state flag, if writing.
4340 ASSERT(mval
->br_blockcount
<= len
);
4341 if ((ifp
->if_flags
& XFS_IFBROOT
) && !cur
) {
4342 cur
= xfs_btree_init_cursor(mp
,
4343 tp
, NULL
, 0, XFS_BTNUM_BMAP
,
4345 cur
->bc_private
.b
.firstblock
=
4347 cur
->bc_private
.b
.flist
= flist
;
4349 mval
->br_state
= XFS_EXT_NORM
;
4350 error
= xfs_bmap_add_extent(ip
, lastx
, &cur
, mval
,
4351 firstblock
, flist
, &tmp_logflags
, whichfork
,
4353 logflags
|= tmp_logflags
;
4356 lastx
= ifp
->if_lastex
;
4357 ep
= &ifp
->if_u1
.if_extents
[lastx
];
4358 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
4359 xfs_bmbt_get_all(ep
, &got
);
4361 * We may have combined previously unwritten
4362 * space with written space, so generate
4365 if (mval
->br_blockcount
< len
)
4370 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
4371 ASSERT(!trim
|| (mval
->br_blockcount
<= len
) ||
4372 (mval
->br_startoff
< obno
));
4373 bno
= mval
->br_startoff
+ mval
->br_blockcount
;
4375 if (n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
4376 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
4377 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
4378 ASSERT(mval
->br_state
== mval
[-1].br_state
);
4379 mval
[-1].br_blockcount
= mval
->br_blockcount
;
4380 mval
[-1].br_state
= mval
->br_state
;
4381 } else if (n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
4382 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
4383 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
4384 mval
->br_startblock
==
4385 mval
[-1].br_startblock
+ mval
[-1].br_blockcount
&&
4386 (stateless
|| mval
[-1].br_state
== mval
->br_state
)) {
4387 ASSERT(mval
->br_startoff
==
4388 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
4389 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4391 mval
->br_startblock
== DELAYSTARTBLOCK
&&
4392 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
4393 mval
->br_startoff
==
4394 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
4395 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
4396 mval
[-1].br_state
= mval
->br_state
;
4397 } else if (!((n
== 0) &&
4398 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
4404 * If we're done, stop now. Stop when we've allocated
4405 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4406 * the transaction may get too big.
4408 if (bno
>= end
|| n
>= *nmap
|| nallocs
>= *nmap
)
4411 * Else go on to the next record.
4415 if (lastx
>= nextents
) {
4419 xfs_bmbt_get_all(ep
, &got
);
4421 ifp
->if_lastex
= lastx
;
4424 * Transform from btree to extents, give it cur.
4426 if (tp
&& XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
4427 XFS_IFORK_NEXTENTS(ip
, whichfork
) <= ifp
->if_ext_max
) {
4429 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
,
4430 &tmp_logflags
, whichfork
);
4431 logflags
|= tmp_logflags
;
4435 ASSERT(ifp
->if_ext_max
==
4436 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
4437 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
4438 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ifp
->if_ext_max
);
4443 * Log everything. Do this after conversion, there's no point in
4444 * logging the extent list if we've converted to btree format.
4446 if ((logflags
& XFS_ILOG_FEXT(whichfork
)) &&
4447 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4448 logflags
&= ~XFS_ILOG_FEXT(whichfork
);
4449 else if ((logflags
& XFS_ILOG_FBROOT(whichfork
)) &&
4450 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4451 logflags
&= ~XFS_ILOG_FBROOT(whichfork
);
4453 * Log whatever the flags say, even if error. Otherwise we might miss
4454 * detecting a case where the data is changed, there's an error,
4455 * and it's not logged so we don't shutdown when we should.
4459 xfs_trans_log_inode(tp
, ip
, logflags
);
4463 ASSERT(*firstblock
== NULLFSBLOCK
||
4464 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
4466 cur
->bc_private
.b
.firstblock
) ||
4468 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
4470 cur
->bc_private
.b
.firstblock
)));
4471 *firstblock
= cur
->bc_private
.b
.firstblock
;
4473 xfs_btree_del_cursor(cur
,
4474 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4477 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4483 * Map file blocks to filesystem blocks, simple version.
4484 * One block (extent) only, read-only.
4485 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
4486 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
4487 * was set and all the others were clear.
4491 xfs_trans_t
*tp
, /* transaction pointer */
4492 xfs_inode_t
*ip
, /* incore inode */
4493 int whichfork
, /* data or attr fork */
4494 xfs_fsblock_t
*fsb
, /* output: mapped block */
4495 xfs_fileoff_t bno
) /* starting file offs. mapped */
4497 int eof
; /* we've hit the end of extent list */
4498 int error
; /* error return */
4499 xfs_bmbt_irec_t got
; /* current extent list record */
4500 xfs_ifork_t
*ifp
; /* inode fork pointer */
4501 xfs_extnum_t lastx
; /* last useful extent number */
4502 xfs_bmbt_irec_t prev
; /* previous extent list record */
4504 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4506 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
4507 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)) {
4508 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW
,
4510 return XFS_ERROR(EFSCORRUPTED
);
4512 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
4513 return XFS_ERROR(EIO
);
4514 XFS_STATS_INC(xs_blk_mapr
);
4515 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
4516 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
4518 (void)xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
4521 * Reading past eof, act as though there's a hole
4524 if (eof
|| got
.br_startoff
> bno
) {
4528 ASSERT(!ISNULLSTARTBLOCK(got
.br_startblock
));
4529 ASSERT(bno
< got
.br_startoff
+ got
.br_blockcount
);
4530 *fsb
= got
.br_startblock
+ (bno
- got
.br_startoff
);
4531 ifp
->if_lastex
= lastx
;
4536 * Unmap (remove) blocks from a file.
4537 * If nexts is nonzero then the number of extents to remove is limited to
4538 * that value. If not all extents in the block range can be removed then
4543 xfs_trans_t
*tp
, /* transaction pointer */
4544 struct xfs_inode
*ip
, /* incore inode */
4545 xfs_fileoff_t bno
, /* starting offset to unmap */
4546 xfs_filblks_t len
, /* length to unmap in file */
4547 int flags
, /* misc flags */
4548 xfs_extnum_t nexts
, /* number of extents max */
4549 xfs_fsblock_t
*firstblock
, /* first allocated block
4550 controls a.g. for allocs */
4551 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
4552 int *done
) /* set if not done yet */
4554 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
4555 xfs_bmbt_irec_t del
; /* extent being deleted */
4556 int eof
; /* is deleting at eof */
4557 xfs_bmbt_rec_t
*ep
; /* extent list entry pointer */
4558 int error
; /* error return value */
4559 xfs_extnum_t extno
; /* extent number in list */
4560 xfs_bmbt_irec_t got
; /* current extent list entry */
4561 xfs_ifork_t
*ifp
; /* inode fork pointer */
4562 int isrt
; /* freeing in rt area */
4563 xfs_extnum_t lastx
; /* last extent index used */
4564 int logflags
; /* transaction logging flags */
4565 xfs_extlen_t mod
; /* rt extent offset */
4566 xfs_mount_t
*mp
; /* mount structure */
4567 xfs_extnum_t nextents
; /* size of extent list */
4568 xfs_bmbt_irec_t prev
; /* previous extent list entry */
4569 xfs_fileoff_t start
; /* first file offset deleted */
4570 int tmp_logflags
; /* partial logging flags */
4571 int wasdel
; /* was a delayed alloc extent */
4572 int whichfork
; /* data or attribute fork */
4573 int rsvd
; /* OK to allocate reserved blocks */
4576 xfs_bunmap_trace(ip
, bno
, len
, flags
, (inst_t
*)__return_address
);
4577 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4578 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4579 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4581 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4582 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
4583 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
4585 return XFS_ERROR(EFSCORRUPTED
);
4588 if (XFS_FORCED_SHUTDOWN(mp
))
4589 return XFS_ERROR(EIO
);
4590 rsvd
= (flags
& XFS_BMAPI_RSVBLOCKS
) != 0;
4593 ASSERT(ifp
->if_ext_max
==
4594 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
4595 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
4596 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
4598 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
4599 if (nextents
== 0) {
4603 XFS_STATS_INC(xs_blk_unmap
);
4604 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4606 bno
= start
+ len
- 1;
4607 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
4610 * Check to see if the given block number is past the end of the
4611 * file, back up to the last block if so...
4614 ep
= &ifp
->if_u1
.if_extents
[--lastx
];
4615 xfs_bmbt_get_all(ep
, &got
);
4616 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
4619 if (ifp
->if_flags
& XFS_IFBROOT
) {
4620 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
4621 cur
= xfs_btree_init_cursor(mp
, tp
, NULL
, 0, XFS_BTNUM_BMAP
, ip
,
4623 cur
->bc_private
.b
.firstblock
= *firstblock
;
4624 cur
->bc_private
.b
.flist
= flist
;
4625 cur
->bc_private
.b
.flags
= 0;
4629 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
4630 (nexts
== 0 || extno
< nexts
)) {
4632 * Is the found extent after a hole in which bno lives?
4633 * Just back up to the previous extent, if so.
4635 if (got
.br_startoff
> bno
) {
4639 xfs_bmbt_get_all(ep
, &got
);
4642 * Is the last block of this extent before the range
4643 * we're supposed to delete? If so, we're done.
4645 bno
= XFS_FILEOFF_MIN(bno
,
4646 got
.br_startoff
+ got
.br_blockcount
- 1);
4650 * Then deal with the (possibly delayed) allocated space
4655 wasdel
= ISNULLSTARTBLOCK(del
.br_startblock
);
4656 if (got
.br_startoff
< start
) {
4657 del
.br_startoff
= start
;
4658 del
.br_blockcount
-= start
- got
.br_startoff
;
4660 del
.br_startblock
+= start
- got
.br_startoff
;
4662 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
4663 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
4664 sum
= del
.br_startblock
+ del
.br_blockcount
;
4666 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
4668 * Realtime extent not lined up at the end.
4669 * The extent could have been split into written
4670 * and unwritten pieces, or we could just be
4671 * unmapping part of it. But we can't really
4672 * get rid of part of a realtime extent.
4674 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
4675 !XFS_SB_VERSION_HASEXTFLGBIT(&mp
->m_sb
)) {
4677 * This piece is unwritten, or we're not
4678 * using unwritten extents. Skip over it.
4681 bno
-= mod
> del
.br_blockcount
?
4682 del
.br_blockcount
: mod
;
4683 if (bno
< got
.br_startoff
) {
4685 xfs_bmbt_get_all(--ep
, &got
);
4690 * It's written, turn it unwritten.
4691 * This is better than zeroing it.
4693 ASSERT(del
.br_state
== XFS_EXT_NORM
);
4694 ASSERT(xfs_trans_get_block_res(tp
) > 0);
4696 * If this spans a realtime extent boundary,
4697 * chop it back to the start of the one we end at.
4699 if (del
.br_blockcount
> mod
) {
4700 del
.br_startoff
+= del
.br_blockcount
- mod
;
4701 del
.br_startblock
+= del
.br_blockcount
- mod
;
4702 del
.br_blockcount
= mod
;
4704 del
.br_state
= XFS_EXT_UNWRITTEN
;
4705 error
= xfs_bmap_add_extent(ip
, lastx
, &cur
, &del
,
4706 firstblock
, flist
, &logflags
, XFS_DATA_FORK
, 0);
4711 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
4713 * Realtime extent is lined up at the end but not
4714 * at the front. We'll get rid of full extents if
4717 mod
= mp
->m_sb
.sb_rextsize
- mod
;
4718 if (del
.br_blockcount
> mod
) {
4719 del
.br_blockcount
-= mod
;
4720 del
.br_startoff
+= mod
;
4721 del
.br_startblock
+= mod
;
4722 } else if ((del
.br_startoff
== start
&&
4723 (del
.br_state
== XFS_EXT_UNWRITTEN
||
4724 xfs_trans_get_block_res(tp
) == 0)) ||
4725 !XFS_SB_VERSION_HASEXTFLGBIT(&mp
->m_sb
)) {
4727 * Can't make it unwritten. There isn't
4728 * a full extent here so just skip it.
4730 ASSERT(bno
>= del
.br_blockcount
);
4731 bno
-= del
.br_blockcount
;
4732 if (bno
< got
.br_startoff
) {
4734 xfs_bmbt_get_all(--ep
, &got
);
4737 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
4739 * This one is already unwritten.
4740 * It must have a written left neighbor.
4741 * Unwrite the killed part of that one and
4745 xfs_bmbt_get_all(ep
- 1, &prev
);
4746 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
4747 ASSERT(!ISNULLSTARTBLOCK(prev
.br_startblock
));
4748 ASSERT(del
.br_startblock
==
4749 prev
.br_startblock
+ prev
.br_blockcount
);
4750 if (prev
.br_startoff
< start
) {
4751 mod
= start
- prev
.br_startoff
;
4752 prev
.br_blockcount
-= mod
;
4753 prev
.br_startblock
+= mod
;
4754 prev
.br_startoff
= start
;
4756 prev
.br_state
= XFS_EXT_UNWRITTEN
;
4757 error
= xfs_bmap_add_extent(ip
, lastx
- 1, &cur
,
4758 &prev
, firstblock
, flist
, &logflags
,
4764 ASSERT(del
.br_state
== XFS_EXT_NORM
);
4765 del
.br_state
= XFS_EXT_UNWRITTEN
;
4766 error
= xfs_bmap_add_extent(ip
, lastx
, &cur
,
4767 &del
, firstblock
, flist
, &logflags
,
4775 ASSERT(STARTBLOCKVAL(del
.br_startblock
) > 0);
4776 /* Update realtime/data freespace, unreserve quota */
4778 xfs_filblks_t rtexts
;
4780 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
4781 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
4782 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
4784 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp
,
4785 NULL
, ip
, -((long)del
.br_blockcount
), 0,
4786 XFS_QMOPT_RES_RTBLKS
);
4788 xfs_mod_incore_sb(mp
, XFS_SBS_FDBLOCKS
,
4789 (int)del
.br_blockcount
, rsvd
);
4790 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp
,
4791 NULL
, ip
, -((long)del
.br_blockcount
), 0,
4792 XFS_QMOPT_RES_REGBLKS
);
4794 ip
->i_delayed_blks
-= del
.br_blockcount
;
4796 cur
->bc_private
.b
.flags
|=
4797 XFS_BTCUR_BPRV_WASDEL
;
4799 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
4801 * If it's the case where the directory code is running
4802 * with no block reservation, and the deleted block is in
4803 * the middle of its extent, and the resulting insert
4804 * of an extent would cause transformation to btree format,
4805 * then reject it. The calling code will then swap
4806 * blocks around instead.
4807 * We have to do this now, rather than waiting for the
4808 * conversion to btree format, since the transaction
4811 if (!wasdel
&& xfs_trans_get_block_res(tp
) == 0 &&
4812 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
4813 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= ifp
->if_ext_max
&&
4814 del
.br_startoff
> got
.br_startoff
&&
4815 del
.br_startoff
+ del
.br_blockcount
<
4816 got
.br_startoff
+ got
.br_blockcount
) {
4817 error
= XFS_ERROR(ENOSPC
);
4820 error
= xfs_bmap_del_extent(ip
, tp
, lastx
, flist
, cur
, &del
,
4821 &tmp_logflags
, whichfork
, rsvd
);
4822 logflags
|= tmp_logflags
;
4825 bno
= del
.br_startoff
- 1;
4827 lastx
= ifp
->if_lastex
;
4829 * If not done go on to the next (previous) record.
4830 * Reset ep in case the extents array was re-alloced.
4832 ep
= &ifp
->if_u1
.if_extents
[lastx
];
4833 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
4834 if (lastx
>= XFS_IFORK_NEXTENTS(ip
, whichfork
) ||
4835 xfs_bmbt_get_startoff(ep
) > bno
) {
4840 xfs_bmbt_get_all(ep
, &got
);
4844 ifp
->if_lastex
= lastx
;
4845 *done
= bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0;
4846 ASSERT(ifp
->if_ext_max
==
4847 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
4849 * Convert to a btree if necessary.
4851 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
4852 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ifp
->if_ext_max
) {
4853 ASSERT(cur
== NULL
);
4854 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
,
4855 &cur
, 0, &tmp_logflags
, whichfork
);
4856 logflags
|= tmp_logflags
;
4861 * transform from btree to extents, give it cur
4863 else if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
4864 XFS_IFORK_NEXTENTS(ip
, whichfork
) <= ifp
->if_ext_max
) {
4865 ASSERT(cur
!= NULL
);
4866 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
4868 logflags
|= tmp_logflags
;
4873 * transform from extents to local?
4875 ASSERT(ifp
->if_ext_max
==
4876 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
4880 * Log everything. Do this after conversion, there's no point in
4881 * logging the extent list if we've converted to btree format.
4883 if ((logflags
& XFS_ILOG_FEXT(whichfork
)) &&
4884 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4885 logflags
&= ~XFS_ILOG_FEXT(whichfork
);
4886 else if ((logflags
& XFS_ILOG_FBROOT(whichfork
)) &&
4887 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4888 logflags
&= ~XFS_ILOG_FBROOT(whichfork
);
4890 * Log inode even in the error case, if the transaction
4891 * is dirty we'll need to shut down the filesystem.
4894 xfs_trans_log_inode(tp
, ip
, logflags
);
4897 *firstblock
= cur
->bc_private
.b
.firstblock
;
4898 cur
->bc_private
.b
.allocated
= 0;
4900 xfs_btree_del_cursor(cur
,
4901 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4907 * Check the last inode extent to determine whether this allocation will result
4908 * in blocks being allocated at the end of the file. When we allocate new data
4909 * blocks at the end of the file which do not start at the previous data block,
4910 * we will try to align the new blocks at stripe unit boundaries.
4914 xfs_inode_t
*ip
, /* incore inode pointer */
4915 xfs_fileoff_t off
, /* file offset in fsblocks */
4916 int whichfork
, /* data or attribute fork */
4917 char *aeof
) /* return value */
4919 int error
; /* error return value */
4920 xfs_ifork_t
*ifp
; /* inode fork pointer */
4921 xfs_bmbt_rec_t
*lastrec
; /* extent list entry pointer */
4922 xfs_extnum_t nextents
; /* size of extent list */
4923 xfs_bmbt_irec_t s
; /* expanded extent list entry */
4925 ASSERT(whichfork
== XFS_DATA_FORK
);
4926 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4927 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
4928 (error
= xfs_iread_extents(NULL
, ip
, whichfork
)))
4930 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
4931 if (nextents
== 0) {
4936 * Go to the last extent
4938 lastrec
= &ifp
->if_u1
.if_extents
[nextents
- 1];
4939 xfs_bmbt_get_all(lastrec
, &s
);
4941 * Check we are allocating in the last extent (for delayed allocations)
4942 * or past the last extent for non-delayed allocations.
4944 *aeof
= (off
>= s
.br_startoff
&&
4945 off
< s
.br_startoff
+ s
.br_blockcount
&&
4946 ISNULLSTARTBLOCK(s
.br_startblock
)) ||
4947 off
>= s
.br_startoff
+ s
.br_blockcount
;