2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
35 xfs_zone_t
*xfs_bmap_free_item_zone
;
38 * Called by xfs_bmapi to update extent list structure and the btree
39 * after allocating space (or doing a delayed allocation).
41 STATIC
int /* error */
43 xfs_inode_t
*ip
, /* incore inode pointer */
44 xfs_extnum_t idx
, /* extent number to update/insert */
45 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
46 xfs_bmbt_irec_t
*new, /* new data to put in extent list */
47 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
48 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
49 int *logflagsp
, /* inode logging flags */
50 int whichfork
, /* data or attr fork */
51 int rsvd
) /* OK to use reserved data blocks */
53 xfs_btree_cur_t
*cur
; /* btree cursor or null */
54 xfs_filblks_t da_new
; /* new count del alloc blocks used */
55 xfs_filblks_t da_old
; /* old count del alloc blocks used */
56 int error
; /* error return value */
58 static char fname
[] = "xfs_bmap_add_extent";
60 xfs_ifork_t
*ifp
; /* inode fork ptr */
61 int logflags
; /* returned value */
62 xfs_extnum_t nextents
; /* number of extents in file now */
64 XFS_STATS_INC(xfsstats
.xs_add_exlist
);
66 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
67 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
68 ASSERT(idx
<= nextents
);
72 * This is the first extent added to a new/empty file.
73 * Special case this one, so other routines get to assume there are
74 * already extents in the list.
77 xfs_bmap_trace_insert(fname
, "insert empty", ip
, 0, 1, new,
79 xfs_bmap_insert_exlist(ip
, 0, 1, new, whichfork
);
82 if (!ISNULLSTARTBLOCK(new->br_startblock
)) {
83 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
84 logflags
= XFS_ILOG_CORE
| XFS_ILOG_FEXT(whichfork
);
89 * Any kind of new delayed allocation goes here.
91 else if (ISNULLSTARTBLOCK(new->br_startblock
)) {
93 ASSERT((cur
->bc_private
.b
.flags
&
94 XFS_BTCUR_BPRV_WASDEL
) == 0);
95 if ((error
= xfs_bmap_add_extent_hole_delay(ip
, idx
, cur
, new,
100 * Real allocation off the end of the file.
102 else if (idx
== nextents
) {
104 ASSERT((cur
->bc_private
.b
.flags
&
105 XFS_BTCUR_BPRV_WASDEL
) == 0);
106 if ((error
= xfs_bmap_add_extent_hole_real(ip
, idx
, cur
, new,
107 &logflags
, whichfork
)))
110 xfs_bmbt_irec_t prev
; /* old extent at offset idx */
113 * Get the record referred to by idx.
115 xfs_bmbt_get_all(&ifp
->if_u1
.if_extents
[idx
], &prev
);
117 * If it's a real allocation record, and the new allocation ends
118 * after the start of the referred to record, then we're filling
119 * in a delayed or unwritten allocation with a real one, or
120 * converting real back to unwritten.
122 if (!ISNULLSTARTBLOCK(new->br_startblock
) &&
123 new->br_startoff
+ new->br_blockcount
> prev
.br_startoff
) {
124 if (prev
.br_state
!= XFS_EXT_UNWRITTEN
&&
125 ISNULLSTARTBLOCK(prev
.br_startblock
)) {
126 da_old
= STARTBLOCKVAL(prev
.br_startblock
);
128 ASSERT(cur
->bc_private
.b
.flags
&
129 XFS_BTCUR_BPRV_WASDEL
);
130 if ((error
= xfs_bmap_add_extent_delay_real(ip
,
131 idx
, &cur
, new, &da_new
, first
, flist
,
134 } else if (new->br_state
== XFS_EXT_NORM
) {
135 ASSERT(new->br_state
== XFS_EXT_NORM
);
136 if ((error
= xfs_bmap_add_extent_unwritten_real(
137 ip
, idx
, &cur
, new, &logflags
)))
140 ASSERT(new->br_state
== XFS_EXT_UNWRITTEN
);
141 if ((error
= xfs_bmap_add_extent_unwritten_real(
142 ip
, idx
, &cur
, new, &logflags
)))
145 ASSERT(*curp
== cur
|| *curp
== NULL
);
148 * Otherwise we're filling in a hole with an allocation.
152 ASSERT((cur
->bc_private
.b
.flags
&
153 XFS_BTCUR_BPRV_WASDEL
) == 0);
154 if ((error
= xfs_bmap_add_extent_hole_real(ip
, idx
, cur
,
155 new, &logflags
, whichfork
)))
160 ASSERT(*curp
== cur
|| *curp
== NULL
);
162 * Convert to a btree if necessary.
164 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
165 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ifp
->if_ext_max
) {
166 int tmp_logflags
; /* partial log flag return val */
169 error
= xfs_bmap_extents_to_btree(ip
->i_transp
, ip
, first
,
170 flist
, &cur
, da_old
> 0, &tmp_logflags
, whichfork
);
171 logflags
|= tmp_logflags
;
176 * Adjust for changes in reserved delayed indirect blocks.
177 * Nothing to do for disk quotas here.
179 if (da_old
|| da_new
) {
184 nblks
+= cur
->bc_private
.b
.allocated
;
185 ASSERT(nblks
<= da_old
);
187 xfs_mod_incore_sb(ip
->i_mount
, XFS_SBS_FDBLOCKS
,
188 (int)(da_old
- nblks
), rsvd
);
191 * Clear out the allocated field, done with it now in any case.
194 cur
->bc_private
.b
.allocated
= 0;
200 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
202 *logflagsp
= logflags
;
207 * Called by xfs_bmap_add_extent to handle cases converting a delayed
208 * allocation to a real allocation.
210 STATIC
int /* error */
211 xfs_bmap_add_extent_delay_real(
212 xfs_inode_t
*ip
, /* incore inode pointer */
213 xfs_extnum_t idx
, /* extent number to update/insert */
214 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
215 xfs_bmbt_irec_t
*new, /* new data to put in extent list */
216 xfs_filblks_t
*dnew
, /* new delayed-alloc indirect blocks */
217 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
218 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
219 int *logflagsp
, /* inode logging flags */
220 int rsvd
) /* OK to use reserved data block allocation */
222 xfs_bmbt_rec_t
*base
; /* base of extent entry list */
223 xfs_btree_cur_t
*cur
; /* btree cursor */
224 int diff
; /* temp value */
225 xfs_bmbt_rec_t
*ep
; /* extent entry for idx */
226 int error
; /* error return value */
227 #ifdef XFS_BMAP_TRACE
228 static char fname
[] = "xfs_bmap_add_extent_delay_real";
230 int i
; /* temp state */
231 xfs_fileoff_t new_endoff
; /* end offset of new entry */
232 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
233 /* left is 0, right is 1, prev is 2 */
234 int rval
=0; /* return value (logging flags) */
235 int state
= 0;/* state bits, accessed thru macros */
236 xfs_filblks_t temp
; /* value for dnew calculations */
237 xfs_filblks_t temp2
; /* value for dnew calculations */
238 int tmp_rval
; /* partial logging flags */
239 enum { /* bit number definitions for state */
240 LEFT_CONTIG
, RIGHT_CONTIG
,
241 LEFT_FILLING
, RIGHT_FILLING
,
242 LEFT_DELAY
, RIGHT_DELAY
,
243 LEFT_VALID
, RIGHT_VALID
249 #define MASK(b) (1 << (b))
250 #define MASK2(a,b) (MASK(a) | MASK(b))
251 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
252 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
253 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
254 #define STATE_TEST(b) (state & MASK(b))
255 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
256 ((state &= ~MASK(b)), 0))
257 #define SWITCH_STATE \
258 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
261 * Set up a bunch of variables to make the tests simpler.
264 base
= ip
->i_df
.if_u1
.if_extents
;
266 xfs_bmbt_get_all(ep
, &PREV
);
267 new_endoff
= new->br_startoff
+ new->br_blockcount
;
268 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
269 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
271 * Set flags determining what part of the previous delayed allocation
272 * extent is being replaced by a real allocation.
274 STATE_SET(LEFT_FILLING
, PREV
.br_startoff
== new->br_startoff
);
275 STATE_SET(RIGHT_FILLING
,
276 PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
);
278 * Check and set flags if this segment has a left neighbor.
279 * Don't set contiguous if the combined extent would be too large.
281 if (STATE_SET_TEST(LEFT_VALID
, idx
> 0)) {
282 xfs_bmbt_get_all(ep
- 1, &LEFT
);
283 STATE_SET(LEFT_DELAY
, ISNULLSTARTBLOCK(LEFT
.br_startblock
));
285 STATE_SET(LEFT_CONTIG
,
286 STATE_TEST(LEFT_VALID
) && !STATE_TEST(LEFT_DELAY
) &&
287 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
288 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
289 LEFT
.br_state
== new->br_state
&&
290 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
);
292 * Check and set flags if this segment has a right neighbor.
293 * Don't set contiguous if the combined extent would be too large.
294 * Also check for all-three-contiguous being too large.
296 if (STATE_SET_TEST(RIGHT_VALID
,
298 ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1)) {
299 xfs_bmbt_get_all(ep
+ 1, &RIGHT
);
300 STATE_SET(RIGHT_DELAY
, ISNULLSTARTBLOCK(RIGHT
.br_startblock
));
302 STATE_SET(RIGHT_CONTIG
,
303 STATE_TEST(RIGHT_VALID
) && !STATE_TEST(RIGHT_DELAY
) &&
304 new_endoff
== RIGHT
.br_startoff
&&
305 new->br_startblock
+ new->br_blockcount
==
306 RIGHT
.br_startblock
&&
307 new->br_state
== RIGHT
.br_state
&&
308 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
309 ((state
& MASK3(LEFT_CONTIG
, LEFT_FILLING
, RIGHT_FILLING
)) !=
310 MASK3(LEFT_CONTIG
, LEFT_FILLING
, RIGHT_FILLING
) ||
311 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
315 * Switch out based on the FILLING and CONTIG state bits.
317 switch (SWITCH_STATE
) {
319 case MASK4(LEFT_FILLING
, RIGHT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
321 * Filling in all of a previously delayed allocation extent.
322 * The left and right neighbors are both contiguous with new.
324 xfs_bmap_trace_pre_update(fname
, "LF|RF|LC|RC", ip
, idx
- 1,
326 xfs_bmbt_set_blockcount(ep
- 1,
327 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
328 RIGHT
.br_blockcount
);
329 xfs_bmap_trace_post_update(fname
, "LF|RF|LC|RC", ip
, idx
- 1,
331 xfs_bmap_trace_delete(fname
, "LF|RF|LC|RC", ip
, idx
, 2,
333 xfs_bmap_delete_exlist(ip
, idx
, 2, XFS_DATA_FORK
);
334 ip
->i_df
.if_lastex
= idx
- 1;
335 ip
->i_d
.di_nextents
--;
337 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
339 rval
= XFS_ILOG_CORE
;
340 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
342 RIGHT
.br_blockcount
, &i
)))
345 if ((error
= xfs_bmbt_delete(cur
, &i
)))
348 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
351 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
355 RIGHT
.br_blockcount
, LEFT
.br_state
)))
361 case MASK3(LEFT_FILLING
, RIGHT_FILLING
, LEFT_CONTIG
):
363 * Filling in all of a previously delayed allocation extent.
364 * The left neighbor is contiguous, the right is not.
366 xfs_bmap_trace_pre_update(fname
, "LF|RF|LC", ip
, idx
- 1,
368 xfs_bmbt_set_blockcount(ep
- 1,
369 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
370 xfs_bmap_trace_post_update(fname
, "LF|RF|LC", ip
, idx
- 1,
372 ip
->i_df
.if_lastex
= idx
- 1;
373 xfs_bmap_trace_delete(fname
, "LF|RF|LC", ip
, idx
, 1,
375 xfs_bmap_delete_exlist(ip
, idx
, 1, XFS_DATA_FORK
);
377 rval
= XFS_ILOG_DEXT
;
380 if ((error
= xfs_bmbt_lookup_eq(cur
, LEFT
.br_startoff
,
381 LEFT
.br_startblock
, LEFT
.br_blockcount
,
385 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
388 PREV
.br_blockcount
, LEFT
.br_state
)))
394 case MASK3(LEFT_FILLING
, RIGHT_FILLING
, RIGHT_CONTIG
):
396 * Filling in all of a previously delayed allocation extent.
397 * The right neighbor is contiguous, the left is not.
399 xfs_bmap_trace_pre_update(fname
, "LF|RF|RC", ip
, idx
,
401 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
402 xfs_bmbt_set_blockcount(ep
,
403 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
404 xfs_bmap_trace_post_update(fname
, "LF|RF|RC", ip
, idx
,
406 ip
->i_df
.if_lastex
= idx
;
407 xfs_bmap_trace_delete(fname
, "LF|RF|RC", ip
, idx
+ 1, 1,
409 xfs_bmap_delete_exlist(ip
, idx
+ 1, 1, XFS_DATA_FORK
);
411 rval
= XFS_ILOG_DEXT
;
414 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
416 RIGHT
.br_blockcount
, &i
)))
419 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
422 RIGHT
.br_blockcount
, PREV
.br_state
)))
428 case MASK2(LEFT_FILLING
, RIGHT_FILLING
):
430 * Filling in all of a previously delayed allocation extent.
431 * Neither the left nor right neighbors are contiguous with
434 xfs_bmap_trace_pre_update(fname
, "LF|RF", ip
, idx
,
436 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
437 xfs_bmap_trace_post_update(fname
, "LF|RF", ip
, idx
,
439 ip
->i_df
.if_lastex
= idx
;
440 ip
->i_d
.di_nextents
++;
442 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
444 rval
= XFS_ILOG_CORE
;
445 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
446 new->br_startblock
, new->br_blockcount
,
450 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
451 if ((error
= xfs_bmbt_insert(cur
, &i
)))
458 case MASK2(LEFT_FILLING
, LEFT_CONTIG
):
460 * Filling in the first part of a previous delayed allocation.
461 * The left neighbor is contiguous.
463 xfs_bmap_trace_pre_update(fname
, "LF|LC", ip
, idx
- 1,
465 xfs_bmbt_set_blockcount(ep
- 1,
466 LEFT
.br_blockcount
+ new->br_blockcount
);
467 xfs_bmbt_set_startoff(ep
,
468 PREV
.br_startoff
+ new->br_blockcount
);
469 xfs_bmap_trace_post_update(fname
, "LF|LC", ip
, idx
- 1,
471 temp
= PREV
.br_blockcount
- new->br_blockcount
;
472 xfs_bmap_trace_pre_update(fname
, "LF|LC", ip
, idx
,
474 xfs_bmbt_set_blockcount(ep
, temp
);
475 ip
->i_df
.if_lastex
= idx
- 1;
477 rval
= XFS_ILOG_DEXT
;
480 if ((error
= xfs_bmbt_lookup_eq(cur
, LEFT
.br_startoff
,
481 LEFT
.br_startblock
, LEFT
.br_blockcount
,
485 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
492 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
493 STARTBLOCKVAL(PREV
.br_startblock
));
494 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
495 xfs_bmap_trace_post_update(fname
, "LF|LC", ip
, idx
,
500 case MASK(LEFT_FILLING
):
502 * Filling in the first part of a previous delayed allocation.
503 * The left neighbor is not contiguous.
505 xfs_bmap_trace_pre_update(fname
, "LF", ip
, idx
, XFS_DATA_FORK
);
506 xfs_bmbt_set_startoff(ep
, new_endoff
);
507 temp
= PREV
.br_blockcount
- new->br_blockcount
;
508 xfs_bmbt_set_blockcount(ep
, temp
);
509 xfs_bmap_trace_insert(fname
, "LF", ip
, idx
, 1, new, NULL
,
511 xfs_bmap_insert_exlist(ip
, idx
, 1, new, XFS_DATA_FORK
);
512 ip
->i_df
.if_lastex
= idx
;
513 ip
->i_d
.di_nextents
++;
515 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
517 rval
= XFS_ILOG_CORE
;
518 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
519 new->br_startblock
, new->br_blockcount
,
523 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
524 if ((error
= xfs_bmbt_insert(cur
, &i
)))
528 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
529 ip
->i_d
.di_nextents
> ip
->i_df
.if_ext_max
) {
530 error
= xfs_bmap_extents_to_btree(ip
->i_transp
, ip
,
531 first
, flist
, &cur
, 1, &tmp_rval
,
537 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
538 STARTBLOCKVAL(PREV
.br_startblock
) -
539 (cur
? cur
->bc_private
.b
.allocated
: 0));
540 base
= ip
->i_df
.if_u1
.if_extents
;
542 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
543 xfs_bmap_trace_post_update(fname
, "LF", ip
, idx
+ 1,
548 case MASK2(RIGHT_FILLING
, RIGHT_CONTIG
):
550 * Filling in the last part of a previous delayed allocation.
551 * The right neighbor is contiguous with the new allocation.
553 temp
= PREV
.br_blockcount
- new->br_blockcount
;
554 xfs_bmap_trace_pre_update(fname
, "RF|RC", ip
, idx
,
556 xfs_bmap_trace_pre_update(fname
, "RF|RC", ip
, idx
+ 1,
558 xfs_bmbt_set_blockcount(ep
, temp
);
559 xfs_bmbt_set_allf(ep
+ 1, new->br_startoff
, new->br_startblock
,
560 new->br_blockcount
+ RIGHT
.br_blockcount
,
562 xfs_bmap_trace_post_update(fname
, "RF|RC", ip
, idx
+ 1,
564 ip
->i_df
.if_lastex
= idx
+ 1;
566 rval
= XFS_ILOG_DEXT
;
569 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
571 RIGHT
.br_blockcount
, &i
)))
574 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
581 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
582 STARTBLOCKVAL(PREV
.br_startblock
));
583 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
584 xfs_bmap_trace_post_update(fname
, "RF|RC", ip
, idx
,
589 case MASK(RIGHT_FILLING
):
591 * Filling in the last part of a previous delayed allocation.
592 * The right neighbor is not contiguous.
594 temp
= PREV
.br_blockcount
- new->br_blockcount
;
595 xfs_bmap_trace_pre_update(fname
, "RF", ip
, idx
, XFS_DATA_FORK
);
596 xfs_bmbt_set_blockcount(ep
, temp
);
597 xfs_bmap_trace_insert(fname
, "RF", ip
, idx
+ 1, 1,
598 new, NULL
, XFS_DATA_FORK
);
599 xfs_bmap_insert_exlist(ip
, idx
+ 1, 1, new, XFS_DATA_FORK
);
600 ip
->i_df
.if_lastex
= idx
+ 1;
601 ip
->i_d
.di_nextents
++;
603 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
605 rval
= XFS_ILOG_CORE
;
606 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
607 new->br_startblock
, new->br_blockcount
,
611 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
612 if ((error
= xfs_bmbt_insert(cur
, &i
)))
616 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
617 ip
->i_d
.di_nextents
> ip
->i_df
.if_ext_max
) {
618 error
= xfs_bmap_extents_to_btree(ip
->i_transp
, ip
,
619 first
, flist
, &cur
, 1, &tmp_rval
,
625 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
626 STARTBLOCKVAL(PREV
.br_startblock
) -
627 (cur
? cur
->bc_private
.b
.allocated
: 0));
628 base
= ip
->i_df
.if_u1
.if_extents
;
630 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
631 xfs_bmap_trace_post_update(fname
, "RF", ip
, idx
, XFS_DATA_FORK
);
637 * Filling in the middle part of a previous delayed allocation.
638 * Contiguity is impossible here.
639 * This case is avoided almost all the time.
641 temp
= new->br_startoff
- PREV
.br_startoff
;
642 xfs_bmap_trace_pre_update(fname
, "0", ip
, idx
, XFS_DATA_FORK
);
643 xfs_bmbt_set_blockcount(ep
, temp
);
645 r
[1].br_startoff
= new_endoff
;
646 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
647 r
[1].br_blockcount
= temp2
;
648 xfs_bmap_trace_insert(fname
, "0", ip
, idx
+ 1, 2, &r
[0], &r
[1],
650 xfs_bmap_insert_exlist(ip
, idx
+ 1, 2, &r
[0], XFS_DATA_FORK
);
651 ip
->i_df
.if_lastex
= idx
+ 1;
652 ip
->i_d
.di_nextents
++;
654 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
656 rval
= XFS_ILOG_CORE
;
657 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
658 new->br_startblock
, new->br_blockcount
,
662 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
663 if ((error
= xfs_bmbt_insert(cur
, &i
)))
667 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
668 ip
->i_d
.di_nextents
> ip
->i_df
.if_ext_max
) {
669 error
= xfs_bmap_extents_to_btree(ip
->i_transp
, ip
,
670 first
, flist
, &cur
, 1, &tmp_rval
,
676 temp
= xfs_bmap_worst_indlen(ip
, temp
);
677 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
678 diff
= (int)(temp
+ temp2
- STARTBLOCKVAL(PREV
.br_startblock
) -
679 (cur
? cur
->bc_private
.b
.allocated
: 0));
681 xfs_mod_incore_sb(ip
->i_mount
, XFS_SBS_FDBLOCKS
, -diff
, rsvd
)) {
683 * Ick gross gag me with a spoon.
685 ASSERT(0); /* want to see if this ever happens! */
691 !xfs_mod_incore_sb(ip
->i_mount
,
692 XFS_SBS_FDBLOCKS
, -diff
, rsvd
))
699 !xfs_mod_incore_sb(ip
->i_mount
,
700 XFS_SBS_FDBLOCKS
, -diff
, rsvd
))
705 base
= ip
->i_df
.if_u1
.if_extents
;
707 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
708 xfs_bmap_trace_post_update(fname
, "0", ip
, idx
, XFS_DATA_FORK
);
709 xfs_bmap_trace_pre_update(fname
, "0", ip
, idx
+ 2,
711 xfs_bmbt_set_startblock(ep
+ 2, NULLSTARTBLOCK((int)temp2
));
712 xfs_bmap_trace_post_update(fname
, "0", ip
, idx
+ 2,
714 *dnew
= temp
+ temp2
;
717 case MASK3(LEFT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
718 case MASK3(RIGHT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
719 case MASK2(LEFT_FILLING
, RIGHT_CONTIG
):
720 case MASK2(RIGHT_FILLING
, LEFT_CONTIG
):
721 case MASK2(LEFT_CONTIG
, RIGHT_CONTIG
):
722 case MASK(LEFT_CONTIG
):
723 case MASK(RIGHT_CONTIG
):
725 * These cases are all impossible.
742 #undef STATE_SET_TEST
747 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
748 * allocation to a real allocation or vice versa.
750 STATIC
int /* error */
751 xfs_bmap_add_extent_unwritten_real(
752 xfs_inode_t
*ip
, /* incore inode pointer */
753 xfs_extnum_t idx
, /* extent number to update/insert */
754 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
755 xfs_bmbt_irec_t
*new, /* new data to put in extent list */
756 int *logflagsp
) /* inode logging flags */
758 xfs_bmbt_rec_t
*base
; /* base of extent entry list */
759 xfs_btree_cur_t
*cur
; /* btree cursor */
760 xfs_bmbt_rec_t
*ep
; /* extent entry for idx */
761 int error
; /* error return value */
762 #ifdef XFS_BMAP_TRACE
763 static char fname
[] = "xfs_bmap_add_extent_unwritten_real";
765 int i
; /* temp state */
766 xfs_fileoff_t new_endoff
; /* end offset of new entry */
767 xfs_exntst_t newext
; /* new extent state */
768 xfs_exntst_t oldext
; /* old extent state */
769 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
770 /* left is 0, right is 1, prev is 2 */
771 int rval
=0; /* return value (logging flags) */
772 int state
= 0;/* state bits, accessed thru macros */
773 enum { /* bit number definitions for state */
774 LEFT_CONTIG
, RIGHT_CONTIG
,
775 LEFT_FILLING
, RIGHT_FILLING
,
776 LEFT_DELAY
, RIGHT_DELAY
,
777 LEFT_VALID
, RIGHT_VALID
783 #define MASK(b) (1 << (b))
784 #define MASK2(a,b) (MASK(a) | MASK(b))
785 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
786 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
787 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
788 #define STATE_TEST(b) (state & MASK(b))
789 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
790 ((state &= ~MASK(b)), 0))
791 #define SWITCH_STATE \
792 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
795 * Set up a bunch of variables to make the tests simpler.
799 base
= ip
->i_df
.if_u1
.if_extents
;
801 xfs_bmbt_get_all(ep
, &PREV
);
802 newext
= new->br_state
;
803 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
804 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
805 ASSERT(PREV
.br_state
== oldext
);
806 new_endoff
= new->br_startoff
+ new->br_blockcount
;
807 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
808 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
810 * Set flags determining what part of the previous oldext allocation
811 * extent is being replaced by a newext allocation.
813 STATE_SET(LEFT_FILLING
, PREV
.br_startoff
== new->br_startoff
);
814 STATE_SET(RIGHT_FILLING
,
815 PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
);
817 * Check and set flags if this segment has a left neighbor.
818 * Don't set contiguous if the combined extent would be too large.
820 if (STATE_SET_TEST(LEFT_VALID
, idx
> 0)) {
821 xfs_bmbt_get_all(ep
- 1, &LEFT
);
822 STATE_SET(LEFT_DELAY
, ISNULLSTARTBLOCK(LEFT
.br_startblock
));
824 STATE_SET(LEFT_CONTIG
,
825 STATE_TEST(LEFT_VALID
) && !STATE_TEST(LEFT_DELAY
) &&
826 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
827 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
828 LEFT
.br_state
== newext
&&
829 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
);
831 * Check and set flags if this segment has a right neighbor.
832 * Don't set contiguous if the combined extent would be too large.
833 * Also check for all-three-contiguous being too large.
835 if (STATE_SET_TEST(RIGHT_VALID
,
837 ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1)) {
838 xfs_bmbt_get_all(ep
+ 1, &RIGHT
);
839 STATE_SET(RIGHT_DELAY
, ISNULLSTARTBLOCK(RIGHT
.br_startblock
));
841 STATE_SET(RIGHT_CONTIG
,
842 STATE_TEST(RIGHT_VALID
) && !STATE_TEST(RIGHT_DELAY
) &&
843 new_endoff
== RIGHT
.br_startoff
&&
844 new->br_startblock
+ new->br_blockcount
==
845 RIGHT
.br_startblock
&&
846 newext
== RIGHT
.br_state
&&
847 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
848 ((state
& MASK3(LEFT_CONTIG
, LEFT_FILLING
, RIGHT_FILLING
)) !=
849 MASK3(LEFT_CONTIG
, LEFT_FILLING
, RIGHT_FILLING
) ||
850 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
853 * Switch out based on the FILLING and CONTIG state bits.
855 switch (SWITCH_STATE
) {
857 case MASK4(LEFT_FILLING
, RIGHT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
859 * Setting all of a previous oldext extent to newext.
860 * The left and right neighbors are both contiguous with new.
862 xfs_bmap_trace_pre_update(fname
, "LF|RF|LC|RC", ip
, idx
- 1,
864 xfs_bmbt_set_blockcount(ep
- 1,
865 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
866 RIGHT
.br_blockcount
);
867 xfs_bmap_trace_post_update(fname
, "LF|RF|LC|RC", ip
, idx
- 1,
869 xfs_bmap_trace_delete(fname
, "LF|RF|LC|RC", ip
, idx
, 2,
871 xfs_bmap_delete_exlist(ip
, idx
, 2, XFS_DATA_FORK
);
872 ip
->i_df
.if_lastex
= idx
- 1;
873 ip
->i_d
.di_nextents
-= 2;
875 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
877 rval
= XFS_ILOG_CORE
;
878 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
880 RIGHT
.br_blockcount
, &i
)))
883 if ((error
= xfs_bmbt_delete(cur
, &i
)))
886 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
889 if ((error
= xfs_bmbt_delete(cur
, &i
)))
892 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
895 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
897 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
898 RIGHT
.br_blockcount
, LEFT
.br_state
)))
903 case MASK3(LEFT_FILLING
, RIGHT_FILLING
, LEFT_CONTIG
):
905 * Setting all of a previous oldext extent to newext.
906 * The left neighbor is contiguous, the right is not.
908 xfs_bmap_trace_pre_update(fname
, "LF|RF|LC", ip
, idx
- 1,
910 xfs_bmbt_set_blockcount(ep
- 1,
911 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
912 xfs_bmap_trace_post_update(fname
, "LF|RF|LC", ip
, idx
- 1,
914 ip
->i_df
.if_lastex
= idx
- 1;
915 xfs_bmap_trace_delete(fname
, "LF|RF|LC", ip
, idx
, 1,
917 xfs_bmap_delete_exlist(ip
, idx
, 1, XFS_DATA_FORK
);
918 ip
->i_d
.di_nextents
--;
920 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
922 rval
= XFS_ILOG_CORE
;
923 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
924 PREV
.br_startblock
, PREV
.br_blockcount
,
928 if ((error
= xfs_bmbt_delete(cur
, &i
)))
931 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
934 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
936 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
942 case MASK3(LEFT_FILLING
, RIGHT_FILLING
, RIGHT_CONTIG
):
944 * Setting all of a previous oldext extent to newext.
945 * The right neighbor is contiguous, the left is not.
947 xfs_bmap_trace_pre_update(fname
, "LF|RF|RC", ip
, idx
,
949 xfs_bmbt_set_blockcount(ep
,
950 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
951 xfs_bmbt_set_state(ep
, newext
);
952 xfs_bmap_trace_post_update(fname
, "LF|RF|RC", ip
, idx
,
954 ip
->i_df
.if_lastex
= idx
;
955 xfs_bmap_trace_delete(fname
, "LF|RF|RC", ip
, idx
+ 1, 1,
957 xfs_bmap_delete_exlist(ip
, idx
+ 1, 1, XFS_DATA_FORK
);
958 ip
->i_d
.di_nextents
--;
960 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
962 rval
= XFS_ILOG_CORE
;
963 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
965 RIGHT
.br_blockcount
, &i
)))
968 if ((error
= xfs_bmbt_delete(cur
, &i
)))
971 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
974 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
976 new->br_blockcount
+ RIGHT
.br_blockcount
,
982 case MASK2(LEFT_FILLING
, RIGHT_FILLING
):
984 * Setting all of a previous oldext extent to newext.
985 * Neither the left nor right neighbors are contiguous with
988 xfs_bmap_trace_pre_update(fname
, "LF|RF", ip
, idx
,
990 xfs_bmbt_set_state(ep
, newext
);
991 xfs_bmap_trace_post_update(fname
, "LF|RF", ip
, idx
,
993 ip
->i_df
.if_lastex
= idx
;
995 rval
= XFS_ILOG_DEXT
;
998 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
999 new->br_startblock
, new->br_blockcount
,
1003 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
1004 new->br_startblock
, new->br_blockcount
,
1010 case MASK2(LEFT_FILLING
, LEFT_CONTIG
):
1012 * Setting the first part of a previous oldext extent to newext.
1013 * The left neighbor is contiguous.
1015 xfs_bmap_trace_pre_update(fname
, "LF|LC", ip
, idx
- 1,
1017 xfs_bmbt_set_blockcount(ep
- 1,
1018 LEFT
.br_blockcount
+ new->br_blockcount
);
1019 xfs_bmbt_set_startoff(ep
,
1020 PREV
.br_startoff
+ new->br_blockcount
);
1021 xfs_bmap_trace_post_update(fname
, "LF|LC", ip
, idx
- 1,
1023 xfs_bmap_trace_pre_update(fname
, "LF|LC", ip
, idx
,
1025 xfs_bmbt_set_startblock(ep
,
1026 new->br_startblock
+ new->br_blockcount
);
1027 xfs_bmbt_set_blockcount(ep
,
1028 PREV
.br_blockcount
- new->br_blockcount
);
1029 xfs_bmap_trace_post_update(fname
, "LF|LC", ip
, idx
,
1031 ip
->i_df
.if_lastex
= idx
- 1;
1033 rval
= XFS_ILOG_DEXT
;
1036 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1037 PREV
.br_startblock
, PREV
.br_blockcount
,
1041 if ((error
= xfs_bmbt_update(cur
,
1042 PREV
.br_startoff
+ new->br_blockcount
,
1043 PREV
.br_startblock
+ new->br_blockcount
,
1044 PREV
.br_blockcount
- new->br_blockcount
,
1047 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
1049 if (xfs_bmbt_update(cur
, LEFT
.br_startoff
,
1051 LEFT
.br_blockcount
+ new->br_blockcount
,
1057 case MASK(LEFT_FILLING
):
1059 * Setting the first part of a previous oldext extent to newext.
1060 * The left neighbor is not contiguous.
1062 xfs_bmap_trace_pre_update(fname
, "LF", ip
, idx
, XFS_DATA_FORK
);
1063 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
1064 xfs_bmbt_set_startoff(ep
, new_endoff
);
1065 xfs_bmbt_set_blockcount(ep
,
1066 PREV
.br_blockcount
- new->br_blockcount
);
1067 xfs_bmbt_set_startblock(ep
,
1068 new->br_startblock
+ new->br_blockcount
);
1069 xfs_bmap_trace_post_update(fname
, "LF", ip
, idx
, XFS_DATA_FORK
);
1070 xfs_bmap_trace_insert(fname
, "LF", ip
, idx
, 1, new, NULL
,
1072 xfs_bmap_insert_exlist(ip
, idx
, 1, new, XFS_DATA_FORK
);
1073 ip
->i_df
.if_lastex
= idx
;
1074 ip
->i_d
.di_nextents
++;
1076 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1078 rval
= XFS_ILOG_CORE
;
1079 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1080 PREV
.br_startblock
, PREV
.br_blockcount
,
1084 if ((error
= xfs_bmbt_update(cur
,
1085 PREV
.br_startoff
+ new->br_blockcount
,
1086 PREV
.br_startblock
+ new->br_blockcount
,
1087 PREV
.br_blockcount
- new->br_blockcount
,
1090 cur
->bc_rec
.b
= *new;
1091 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1097 case MASK2(RIGHT_FILLING
, RIGHT_CONTIG
):
1099 * Setting the last part of a previous oldext extent to newext.
1100 * The right neighbor is contiguous with the new allocation.
1102 xfs_bmap_trace_pre_update(fname
, "RF|RC", ip
, idx
,
1104 xfs_bmap_trace_pre_update(fname
, "RF|RC", ip
, idx
+ 1,
1106 xfs_bmbt_set_blockcount(ep
,
1107 PREV
.br_blockcount
- new->br_blockcount
);
1108 xfs_bmap_trace_post_update(fname
, "RF|RC", ip
, idx
,
1110 xfs_bmbt_set_allf(ep
+ 1, new->br_startoff
, new->br_startblock
,
1111 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
1112 xfs_bmap_trace_post_update(fname
, "RF|RC", ip
, idx
+ 1,
1114 ip
->i_df
.if_lastex
= idx
+ 1;
1116 rval
= XFS_ILOG_DEXT
;
1119 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1121 PREV
.br_blockcount
, &i
)))
1124 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
1126 PREV
.br_blockcount
- new->br_blockcount
,
1129 if ((error
= xfs_bmbt_increment(cur
, 0, &i
)))
1131 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
1133 new->br_blockcount
+ RIGHT
.br_blockcount
,
1139 case MASK(RIGHT_FILLING
):
1141 * Setting the last part of a previous oldext extent to newext.
1142 * The right neighbor is not contiguous.
1144 xfs_bmap_trace_pre_update(fname
, "RF", ip
, idx
, XFS_DATA_FORK
);
1145 xfs_bmbt_set_blockcount(ep
,
1146 PREV
.br_blockcount
- new->br_blockcount
);
1147 xfs_bmap_trace_post_update(fname
, "RF", ip
, idx
, XFS_DATA_FORK
);
1148 xfs_bmap_trace_insert(fname
, "RF", ip
, idx
+ 1, 1,
1149 new, NULL
, XFS_DATA_FORK
);
1150 xfs_bmap_insert_exlist(ip
, idx
+ 1, 1, new, XFS_DATA_FORK
);
1151 ip
->i_df
.if_lastex
= idx
+ 1;
1152 ip
->i_d
.di_nextents
++;
1154 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1156 rval
= XFS_ILOG_CORE
;
1157 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1158 PREV
.br_startblock
, PREV
.br_blockcount
,
1162 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
1164 PREV
.br_blockcount
- new->br_blockcount
,
1167 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1168 new->br_startblock
, new->br_blockcount
,
1172 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1173 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1181 * Setting the middle part of a previous oldext extent to
1182 * newext. Contiguity is impossible here.
1183 * One extent becomes three extents.
1185 xfs_bmap_trace_pre_update(fname
, "0", ip
, idx
, XFS_DATA_FORK
);
1186 xfs_bmbt_set_blockcount(ep
,
1187 new->br_startoff
- PREV
.br_startoff
);
1188 xfs_bmap_trace_post_update(fname
, "0", ip
, idx
, XFS_DATA_FORK
);
1190 r
[1].br_startoff
= new_endoff
;
1191 r
[1].br_blockcount
=
1192 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1193 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
1194 r
[1].br_state
= oldext
;
1195 xfs_bmap_trace_insert(fname
, "0", ip
, idx
+ 1, 2, &r
[0], &r
[1],
1197 xfs_bmap_insert_exlist(ip
, idx
+ 1, 2, &r
[0], XFS_DATA_FORK
);
1198 ip
->i_df
.if_lastex
= idx
+ 1;
1199 ip
->i_d
.di_nextents
+= 2;
1201 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1203 rval
= XFS_ILOG_CORE
;
1204 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1205 PREV
.br_startblock
, PREV
.br_blockcount
,
1209 /* new right extent - oldext */
1210 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
1211 r
[1].br_startblock
, r
[1].br_blockcount
,
1214 /* new left extent - oldext */
1215 PREV
.br_blockcount
=
1216 new->br_startoff
- PREV
.br_startoff
;
1217 cur
->bc_rec
.b
= PREV
;
1218 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1221 if ((error
= xfs_bmbt_increment(cur
, 0, &i
)))
1224 /* new middle extent - newext */
1225 cur
->bc_rec
.b
= *new;
1226 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1232 case MASK3(LEFT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
1233 case MASK3(RIGHT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
1234 case MASK2(LEFT_FILLING
, RIGHT_CONTIG
):
1235 case MASK2(RIGHT_FILLING
, LEFT_CONTIG
):
1236 case MASK2(LEFT_CONTIG
, RIGHT_CONTIG
):
1237 case MASK(LEFT_CONTIG
):
1238 case MASK(RIGHT_CONTIG
):
1240 * These cases are all impossible.
1257 #undef STATE_SET_TEST
1262 * Called by xfs_bmap_add_extent to handle cases converting a hole
1263 * to a delayed allocation.
1266 STATIC
int /* error */
1267 xfs_bmap_add_extent_hole_delay(
1268 xfs_inode_t
*ip
, /* incore inode pointer */
1269 xfs_extnum_t idx
, /* extent number to update/insert */
1270 xfs_btree_cur_t
*cur
, /* if null, not a btree */
1271 xfs_bmbt_irec_t
*new, /* new data to put in extent list */
1272 int *logflagsp
, /* inode logging flags */
1273 int rsvd
) /* OK to allocate reserved blocks */
1275 xfs_bmbt_rec_t
*base
; /* base of extent entry list */
1276 xfs_bmbt_rec_t
*ep
; /* extent list entry for idx */
1277 #ifdef XFS_BMAP_TRACE
1278 static char fname
[] = "xfs_bmap_add_extent_hole_delay";
1280 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
1281 xfs_filblks_t newlen
=0; /* new indirect size */
1282 xfs_filblks_t oldlen
=0; /* old indirect size */
1283 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
1284 int state
; /* state bits, accessed thru macros */
1285 xfs_filblks_t temp
; /* temp for indirect calculations */
1286 enum { /* bit number definitions for state */
1287 LEFT_CONTIG
, RIGHT_CONTIG
,
1288 LEFT_DELAY
, RIGHT_DELAY
,
1289 LEFT_VALID
, RIGHT_VALID
1292 #define MASK(b) (1 << (b))
1293 #define MASK2(a,b) (MASK(a) | MASK(b))
1294 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1295 #define STATE_TEST(b) (state & MASK(b))
1296 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1297 ((state &= ~MASK(b)), 0))
1298 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1300 base
= ip
->i_df
.if_u1
.if_extents
;
1303 ASSERT(ISNULLSTARTBLOCK(new->br_startblock
));
1305 * Check and set flags if this segment has a left neighbor
1307 if (STATE_SET_TEST(LEFT_VALID
, idx
> 0)) {
1308 xfs_bmbt_get_all(ep
- 1, &left
);
1309 STATE_SET(LEFT_DELAY
, ISNULLSTARTBLOCK(left
.br_startblock
));
1312 * Check and set flags if the current (right) segment exists.
1313 * If it doesn't exist, we're converting the hole at end-of-file.
1315 if (STATE_SET_TEST(RIGHT_VALID
,
1317 ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1318 xfs_bmbt_get_all(ep
, &right
);
1319 STATE_SET(RIGHT_DELAY
, ISNULLSTARTBLOCK(right
.br_startblock
));
1322 * Set contiguity flags on the left and right neighbors.
1323 * Don't let extents get too large, even if the pieces are contiguous.
1325 STATE_SET(LEFT_CONTIG
,
1326 STATE_TEST(LEFT_VALID
) && STATE_TEST(LEFT_DELAY
) &&
1327 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
1328 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
);
1329 STATE_SET(RIGHT_CONTIG
,
1330 STATE_TEST(RIGHT_VALID
) && STATE_TEST(RIGHT_DELAY
) &&
1331 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
1332 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
1333 (!STATE_TEST(LEFT_CONTIG
) ||
1334 (left
.br_blockcount
+ new->br_blockcount
+
1335 right
.br_blockcount
<= MAXEXTLEN
)));
1337 * Switch out based on the contiguity flags.
1339 switch (SWITCH_STATE
) {
1341 case MASK2(LEFT_CONTIG
, RIGHT_CONTIG
):
1343 * New allocation is contiguous with delayed allocations
1344 * on the left and on the right.
1345 * Merge all three into a single extent list entry.
1347 temp
= left
.br_blockcount
+ new->br_blockcount
+
1348 right
.br_blockcount
;
1349 xfs_bmap_trace_pre_update(fname
, "LC|RC", ip
, idx
- 1,
1351 xfs_bmbt_set_blockcount(ep
- 1, temp
);
1352 oldlen
= STARTBLOCKVAL(left
.br_startblock
) +
1353 STARTBLOCKVAL(new->br_startblock
) +
1354 STARTBLOCKVAL(right
.br_startblock
);
1355 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
1356 xfs_bmbt_set_startblock(ep
- 1, NULLSTARTBLOCK((int)newlen
));
1357 xfs_bmap_trace_post_update(fname
, "LC|RC", ip
, idx
- 1,
1359 xfs_bmap_trace_delete(fname
, "LC|RC", ip
, idx
, 1,
1361 xfs_bmap_delete_exlist(ip
, idx
, 1, XFS_DATA_FORK
);
1362 ip
->i_df
.if_lastex
= idx
- 1;
1365 case MASK(LEFT_CONTIG
):
1367 * New allocation is contiguous with a delayed allocation
1369 * Merge the new allocation with the left neighbor.
1371 temp
= left
.br_blockcount
+ new->br_blockcount
;
1372 xfs_bmap_trace_pre_update(fname
, "LC", ip
, idx
- 1,
1374 xfs_bmbt_set_blockcount(ep
- 1, temp
);
1375 oldlen
= STARTBLOCKVAL(left
.br_startblock
) +
1376 STARTBLOCKVAL(new->br_startblock
);
1377 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
1378 xfs_bmbt_set_startblock(ep
- 1, NULLSTARTBLOCK((int)newlen
));
1379 xfs_bmap_trace_post_update(fname
, "LC", ip
, idx
- 1,
1381 ip
->i_df
.if_lastex
= idx
- 1;
1384 case MASK(RIGHT_CONTIG
):
1386 * New allocation is contiguous with a delayed allocation
1388 * Merge the new allocation with the right neighbor.
1390 xfs_bmap_trace_pre_update(fname
, "RC", ip
, idx
, XFS_DATA_FORK
);
1391 temp
= new->br_blockcount
+ right
.br_blockcount
;
1392 oldlen
= STARTBLOCKVAL(new->br_startblock
) +
1393 STARTBLOCKVAL(right
.br_startblock
);
1394 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
1395 xfs_bmbt_set_allf(ep
, new->br_startoff
,
1396 NULLSTARTBLOCK((int)newlen
), temp
, right
.br_state
);
1397 xfs_bmap_trace_post_update(fname
, "RC", ip
, idx
, XFS_DATA_FORK
);
1398 ip
->i_df
.if_lastex
= idx
;
1403 * New allocation is not contiguous with another
1404 * delayed allocation.
1405 * Insert a new entry.
1407 oldlen
= newlen
= 0;
1408 xfs_bmap_trace_insert(fname
, "0", ip
, idx
, 1, new, NULL
,
1410 xfs_bmap_insert_exlist(ip
, idx
, 1, new, XFS_DATA_FORK
);
1411 ip
->i_df
.if_lastex
= idx
;
1414 if (oldlen
!= newlen
) {
1415 ASSERT(oldlen
> newlen
);
1416 xfs_mod_incore_sb(ip
->i_mount
, XFS_SBS_FDBLOCKS
,
1417 (int)(oldlen
- newlen
), rsvd
);
1419 * Nothing to do for disk quota accounting here.
1428 #undef STATE_SET_TEST
1433 * Called by xfs_bmap_add_extent to handle cases converting a hole
1434 * to a real allocation.
1436 STATIC
int /* error */
1437 xfs_bmap_add_extent_hole_real(
1438 xfs_inode_t
*ip
, /* incore inode pointer */
1439 xfs_extnum_t idx
, /* extent number to update/insert */
1440 xfs_btree_cur_t
*cur
, /* if null, not a btree */
1441 xfs_bmbt_irec_t
*new, /* new data to put in extent list */
1442 int *logflagsp
, /* inode logging flags */
1443 int whichfork
) /* data or attr fork */
1445 xfs_bmbt_rec_t
*ep
; /* pointer to extent entry ins. point */
1446 int error
; /* error return value */
1447 #ifdef XFS_BMAP_TRACE
1448 static char fname
[] = "xfs_bmap_add_extent_hole_real";
1450 int i
; /* temp state */
1451 xfs_ifork_t
*ifp
; /* inode fork pointer */
1452 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
1453 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
1454 int state
; /* state bits, accessed thru macros */
1455 enum { /* bit number definitions for state */
1456 LEFT_CONTIG
, RIGHT_CONTIG
,
1457 LEFT_DELAY
, RIGHT_DELAY
,
1458 LEFT_VALID
, RIGHT_VALID
1461 #define MASK(b) (1 << (b))
1462 #define MASK2(a,b) (MASK(a) | MASK(b))
1463 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1464 #define STATE_TEST(b) (state & MASK(b))
1465 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1466 ((state &= ~MASK(b)), 0))
1467 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1469 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1470 ASSERT(idx
<= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
));
1471 ep
= &ifp
->if_u1
.if_extents
[idx
];
1474 * Check and set flags if this segment has a left neighbor.
1476 if (STATE_SET_TEST(LEFT_VALID
, idx
> 0)) {
1477 xfs_bmbt_get_all(ep
- 1, &left
);
1478 STATE_SET(LEFT_DELAY
, ISNULLSTARTBLOCK(left
.br_startblock
));
1481 * Check and set flags if this segment has a current value.
1482 * Not true if we're inserting into the "hole" at eof.
1484 if (STATE_SET_TEST(RIGHT_VALID
,
1486 ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1487 xfs_bmbt_get_all(ep
, &right
);
1488 STATE_SET(RIGHT_DELAY
, ISNULLSTARTBLOCK(right
.br_startblock
));
1491 * We're inserting a real allocation between "left" and "right".
1492 * Set the contiguity flags. Don't let extents get too large.
1494 STATE_SET(LEFT_CONTIG
,
1495 STATE_TEST(LEFT_VALID
) && !STATE_TEST(LEFT_DELAY
) &&
1496 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
1497 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
1498 left
.br_state
== new->br_state
&&
1499 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
);
1500 STATE_SET(RIGHT_CONTIG
,
1501 STATE_TEST(RIGHT_VALID
) && !STATE_TEST(RIGHT_DELAY
) &&
1502 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
1503 new->br_startblock
+ new->br_blockcount
==
1504 right
.br_startblock
&&
1505 new->br_state
== right
.br_state
&&
1506 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
1507 (!STATE_TEST(LEFT_CONTIG
) ||
1508 left
.br_blockcount
+ new->br_blockcount
+
1509 right
.br_blockcount
<= MAXEXTLEN
));
1512 * Select which case we're in here, and implement it.
1514 switch (SWITCH_STATE
) {
1516 case MASK2(LEFT_CONTIG
, RIGHT_CONTIG
):
1518 * New allocation is contiguous with real allocations on the
1519 * left and on the right.
1520 * Merge all three into a single extent list entry.
1522 xfs_bmap_trace_pre_update(fname
, "LC|RC", ip
, idx
- 1,
1524 xfs_bmbt_set_blockcount(ep
- 1,
1525 left
.br_blockcount
+ new->br_blockcount
+
1526 right
.br_blockcount
);
1527 xfs_bmap_trace_post_update(fname
, "LC|RC", ip
, idx
- 1,
1529 xfs_bmap_trace_delete(fname
, "LC|RC", ip
,
1531 xfs_bmap_delete_exlist(ip
, idx
, 1, whichfork
);
1532 ifp
->if_lastex
= idx
- 1;
1533 XFS_IFORK_NEXT_SET(ip
, whichfork
,
1534 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
1536 *logflagsp
= XFS_ILOG_CORE
| XFS_ILOG_FEXT(whichfork
);
1539 *logflagsp
= XFS_ILOG_CORE
;
1540 if ((error
= xfs_bmbt_lookup_eq(cur
, right
.br_startoff
,
1541 right
.br_startblock
, right
.br_blockcount
, &i
)))
1544 if ((error
= xfs_bmbt_delete(cur
, &i
)))
1547 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
1550 error
= xfs_bmbt_update(cur
, left
.br_startoff
,
1552 left
.br_blockcount
+ new->br_blockcount
+
1553 right
.br_blockcount
, left
.br_state
);
1556 case MASK(LEFT_CONTIG
):
1558 * New allocation is contiguous with a real allocation
1560 * Merge the new allocation with the left neighbor.
1562 xfs_bmap_trace_pre_update(fname
, "LC", ip
, idx
- 1, whichfork
);
1563 xfs_bmbt_set_blockcount(ep
- 1,
1564 left
.br_blockcount
+ new->br_blockcount
);
1565 xfs_bmap_trace_post_update(fname
, "LC", ip
, idx
- 1, whichfork
);
1566 ifp
->if_lastex
= idx
- 1;
1568 *logflagsp
= XFS_ILOG_FEXT(whichfork
);
1572 if ((error
= xfs_bmbt_lookup_eq(cur
, left
.br_startoff
,
1573 left
.br_startblock
, left
.br_blockcount
, &i
)))
1576 error
= xfs_bmbt_update(cur
, left
.br_startoff
,
1578 left
.br_blockcount
+ new->br_blockcount
,
1582 case MASK(RIGHT_CONTIG
):
1584 * New allocation is contiguous with a real allocation
1586 * Merge the new allocation with the right neighbor.
1588 xfs_bmap_trace_pre_update(fname
, "RC", ip
, idx
, whichfork
);
1589 xfs_bmbt_set_allf(ep
, new->br_startoff
, new->br_startblock
,
1590 new->br_blockcount
+ right
.br_blockcount
,
1592 xfs_bmap_trace_post_update(fname
, "RC", ip
, idx
, whichfork
);
1593 ifp
->if_lastex
= idx
;
1595 *logflagsp
= XFS_ILOG_FEXT(whichfork
);
1599 if ((error
= xfs_bmbt_lookup_eq(cur
, right
.br_startoff
,
1600 right
.br_startblock
, right
.br_blockcount
, &i
)))
1603 error
= xfs_bmbt_update(cur
, new->br_startoff
,
1605 new->br_blockcount
+ right
.br_blockcount
,
1611 * New allocation is not contiguous with another
1613 * Insert a new entry.
1615 xfs_bmap_trace_insert(fname
, "0", ip
, idx
, 1, new, NULL
,
1617 xfs_bmap_insert_exlist(ip
, idx
, 1, new, whichfork
);
1618 ifp
->if_lastex
= idx
;
1619 XFS_IFORK_NEXT_SET(ip
, whichfork
,
1620 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
1622 *logflagsp
= XFS_ILOG_CORE
| XFS_ILOG_FEXT(whichfork
);
1625 *logflagsp
= XFS_ILOG_CORE
;
1626 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1627 new->br_startblock
, new->br_blockcount
, &i
)))
1630 cur
->bc_rec
.b
.br_state
= new->br_state
;
1631 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1640 #undef STATE_SET_TEST
1644 return 0; /* keep gcc quite */
1647 #define XFS_ALLOC_GAP_UNITS 4
1650 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
1651 * It figures out where to ask the underlying allocator to put the new extent.
1653 STATIC
int /* error */
1655 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
1657 xfs_fsblock_t adjust
; /* adjustment to block numbers */
1658 xfs_alloctype_t atype
=0; /* type for allocation routines */
1659 int error
; /* error return value */
1660 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
1661 xfs_mount_t
*mp
; /* mount point structure */
1662 int nullfb
; /* true if ap->firstblock isn't set */
1663 int rt
; /* true if inode is realtime */
1665 xfs_extlen_t prod
=0; /* product factor for allocators */
1666 xfs_extlen_t ralen
=0; /* realtime allocation length */
1669 #define ISLEGAL(x,y) \
1671 (x) < mp->m_sb.sb_rblocks : \
1672 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
1673 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
1674 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
1679 mp
= ap
->ip
->i_mount
;
1680 nullfb
= ap
->firstblock
== NULLFSBLOCK
;
1681 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
;
1682 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, ap
->firstblock
);
1685 xfs_extlen_t extsz
; /* file extent size for rt */
1686 xfs_fileoff_t nexto
; /* next file offset */
1687 xfs_extlen_t orig_alen
; /* original ap->alen */
1688 xfs_fileoff_t orig_end
; /* original off+len */
1689 xfs_fileoff_t orig_off
; /* original ap->off */
1690 xfs_extlen_t mod_off
; /* modulus calculations */
1691 xfs_fileoff_t prevo
; /* previous file offset */
1692 xfs_rtblock_t rtx
; /* realtime extent number */
1693 xfs_extlen_t temp
; /* temp for rt calculations */
1696 * Set prod to match the realtime extent size.
1698 if (!(extsz
= ap
->ip
->i_d
.di_extsize
))
1699 extsz
= mp
->m_sb
.sb_rextsize
;
1700 prod
= extsz
/ mp
->m_sb
.sb_rextsize
;
1702 orig_alen
= ap
->alen
;
1703 orig_end
= orig_off
+ orig_alen
;
1705 * If the file offset is unaligned vs. the extent size
1706 * we need to align it. This will be possible unless
1707 * the file was previously written with a kernel that didn't
1708 * perform this alignment.
1710 mod_off
= do_mod(orig_off
, extsz
);
1712 ap
->alen
+= mod_off
;
1716 * Same adjustment for the end of the requested area.
1718 if ((temp
= (ap
->alen
% extsz
)))
1719 ap
->alen
+= extsz
- temp
;
1721 * If the previous block overlaps with this proposed allocation
1722 * then move the start forward without adjusting the length.
1725 ap
->prevp
->br_startoff
== NULLFILEOFF
?
1727 (ap
->prevp
->br_startoff
+
1728 ap
->prevp
->br_blockcount
);
1729 if (ap
->off
!= orig_off
&& ap
->off
< prevo
)
1732 * If the next block overlaps with this proposed allocation
1733 * then move the start back without adjusting the length,
1734 * but not before offset 0.
1735 * This may of course make the start overlap previous block,
1736 * and if we hit the offset 0 limit then the next block
1737 * can still overlap too.
1739 nexto
= (ap
->eof
|| ap
->gotp
->br_startoff
== NULLFILEOFF
) ?
1740 NULLFILEOFF
: ap
->gotp
->br_startoff
;
1742 ap
->off
+ ap
->alen
!= orig_end
&&
1743 ap
->off
+ ap
->alen
> nexto
)
1744 ap
->off
= nexto
> ap
->alen
? nexto
- ap
->alen
: 0;
1746 * If we're now overlapping the next or previous extent that
1747 * means we can't fit an extsz piece in this hole. Just move
1748 * the start forward to the first legal spot and set
1749 * the length so we hit the end.
1751 if ((ap
->off
!= orig_off
&& ap
->off
< prevo
) ||
1752 (ap
->off
+ ap
->alen
!= orig_end
&&
1753 ap
->off
+ ap
->alen
> nexto
)) {
1755 ap
->alen
= nexto
- prevo
;
1758 * If the result isn't a multiple of rtextents we need to
1759 * remove blocks until it is.
1761 if ((temp
= (ap
->alen
% mp
->m_sb
.sb_rextsize
))) {
1763 * We're not covering the original request, or
1764 * we won't be able to once we fix the length.
1766 if (orig_off
< ap
->off
||
1767 orig_end
> ap
->off
+ ap
->alen
||
1768 ap
->alen
- temp
< orig_alen
)
1769 return XFS_ERROR(EINVAL
);
1771 * Try to fix it by moving the start up.
1773 if (ap
->off
+ temp
<= orig_off
) {
1778 * Try to fix it by moving the end in.
1780 else if (ap
->off
+ ap
->alen
- temp
>= orig_end
)
1783 * Set the start to the minimum then trim the length.
1786 ap
->alen
-= orig_off
- ap
->off
;
1788 ap
->alen
-= ap
->alen
% mp
->m_sb
.sb_rextsize
;
1791 * Result doesn't cover the request, fail it.
1793 if (orig_off
< ap
->off
|| orig_end
> ap
->off
+ ap
->alen
)
1794 return XFS_ERROR(EINVAL
);
1796 ASSERT(ap
->alen
% mp
->m_sb
.sb_rextsize
== 0);
1798 * If the offset & length are not perfectly aligned
1799 * then kill prod, it will just get us in trouble.
1801 if (do_mod(ap
->off
, extsz
) || ap
->alen
% extsz
)
1804 * Set ralen to be the actual requested length in rtextents.
1806 ralen
= ap
->alen
/ mp
->m_sb
.sb_rextsize
;
1808 * If the old value was close enough to MAXEXTLEN that
1809 * we rounded up to it, cut it back so it's legal again.
1810 * Note that if it's a really large request (bigger than
1811 * MAXEXTLEN), we don't hear about that number, and can't
1812 * adjust the starting point to match it.
1814 if (ralen
* mp
->m_sb
.sb_rextsize
>= MAXEXTLEN
)
1815 ralen
= MAXEXTLEN
/ mp
->m_sb
.sb_rextsize
;
1817 * If it's an allocation to an empty file at offset 0,
1818 * pick an extent that will space things out in the rt area.
1820 if (ap
->eof
&& ap
->off
== 0) {
1821 error
= xfs_rtpick_extent(mp
, ap
->tp
, ralen
, &rtx
);
1824 ap
->rval
= rtx
* mp
->m_sb
.sb_rextsize
;
1831 #endif /* __KERNEL__ */
1833 ap
->rval
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
1835 ap
->rval
= ap
->firstblock
;
1837 * If allocating at eof, and there's a previous real block,
1838 * try to use it's last block as our starting point.
1840 if (ap
->eof
&& ap
->prevp
->br_startoff
!= NULLFILEOFF
&&
1841 !ISNULLSTARTBLOCK(ap
->prevp
->br_startblock
) &&
1842 ISLEGAL(ap
->prevp
->br_startblock
+ ap
->prevp
->br_blockcount
,
1843 ap
->prevp
->br_startblock
)) {
1844 ap
->rval
= ap
->prevp
->br_startblock
+ ap
->prevp
->br_blockcount
;
1846 * Adjust for the gap between prevp and us.
1849 (ap
->prevp
->br_startoff
+ ap
->prevp
->br_blockcount
);
1851 ISLEGAL(ap
->rval
+ adjust
, ap
->prevp
->br_startblock
))
1855 * If not at eof, then compare the two neighbor blocks.
1856 * Figure out whether either one gives us a good starting point,
1857 * and pick the better one.
1859 else if (!ap
->eof
) {
1860 xfs_fsblock_t gotbno
; /* right side block number */
1861 xfs_fsblock_t gotdiff
=0; /* right side difference */
1862 xfs_fsblock_t prevbno
; /* left side block number */
1863 xfs_fsblock_t prevdiff
=0; /* left side difference */
1866 * If there's a previous (left) block, select a requested
1867 * start block based on it.
1869 if (ap
->prevp
->br_startoff
!= NULLFILEOFF
&&
1870 !ISNULLSTARTBLOCK(ap
->prevp
->br_startblock
) &&
1871 (prevbno
= ap
->prevp
->br_startblock
+
1872 ap
->prevp
->br_blockcount
) &&
1873 ISLEGAL(prevbno
, ap
->prevp
->br_startblock
)) {
1875 * Calculate gap to end of previous block.
1877 adjust
= prevdiff
= ap
->off
-
1878 (ap
->prevp
->br_startoff
+
1879 ap
->prevp
->br_blockcount
);
1881 * Figure the startblock based on the previous block's
1882 * end and the gap size.
1884 * If the gap is large relative to the piece we're
1885 * allocating, or using it gives us an illegal block
1886 * number, then just use the end of the previous block.
1888 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->alen
&&
1889 ISLEGAL(prevbno
+ prevdiff
,
1890 ap
->prevp
->br_startblock
))
1895 * If the firstblock forbids it, can't use it,
1898 if (!rt
&& !nullfb
&&
1899 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
1900 prevbno
= NULLFSBLOCK
;
1903 * No previous block or can't follow it, just default.
1906 prevbno
= NULLFSBLOCK
;
1908 * If there's a following (right) block, select a requested
1909 * start block based on it.
1911 if (!ISNULLSTARTBLOCK(ap
->gotp
->br_startblock
)) {
1913 * Calculate gap to start of next block.
1915 adjust
= gotdiff
= ap
->gotp
->br_startoff
- ap
->off
;
1917 * Figure the startblock based on the next block's
1918 * start and the gap size.
1920 gotbno
= ap
->gotp
->br_startblock
;
1923 * If the gap is large relative to the piece we're
1924 * allocating, or using it gives us an illegal block
1925 * number, then just use the start of the next block
1926 * offset by our length.
1928 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->alen
&&
1929 ISLEGAL(gotbno
- gotdiff
, gotbno
))
1931 else if (ISLEGAL(gotbno
- ap
->alen
, gotbno
)) {
1933 gotdiff
+= adjust
- ap
->alen
;
1937 * If the firstblock forbids it, can't use it,
1940 if (!rt
&& !nullfb
&&
1941 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
1942 gotbno
= NULLFSBLOCK
;
1945 * No next block, just default.
1948 gotbno
= NULLFSBLOCK
;
1950 * If both valid, pick the better one, else the only good
1951 * one, else ap->rval is already set (to 0 or the inode block).
1953 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
1954 ap
->rval
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
1955 else if (prevbno
!= NULLFSBLOCK
)
1957 else if (gotbno
!= NULLFSBLOCK
)
1961 * If allowed, use ap->rval; otherwise must use firstblock since
1962 * it's in the right allocation group.
1964 if (nullfb
|| rt
|| XFS_FSB_TO_AGNO(mp
, ap
->rval
) == fb_agno
)
1967 ap
->rval
= ap
->firstblock
;
1969 * Realtime allocation, done through xfs_rtallocate_extent.
1977 atype
= ap
->rval
== 0 ?
1978 XFS_ALLOCTYPE_ANY_AG
: XFS_ALLOCTYPE_NEAR_BNO
;
1979 do_div(ap
->rval
, mp
->m_sb
.sb_rextsize
);
1982 if ((error
= xfs_rtallocate_extent(ap
->tp
, ap
->rval
, 1, ap
->alen
,
1983 &ralen
, atype
, ap
->wasdel
, prod
, &rtb
)))
1985 if (rtb
== NULLFSBLOCK
&& prod
> 1 &&
1986 (error
= xfs_rtallocate_extent(ap
->tp
, ap
->rval
, 1,
1987 ap
->alen
, &ralen
, atype
,
1988 ap
->wasdel
, 1, &rtb
)))
1991 if (ap
->rval
!= NULLFSBLOCK
) {
1992 ap
->rval
*= mp
->m_sb
.sb_rextsize
;
1993 ralen
*= mp
->m_sb
.sb_rextsize
;
1995 ap
->ip
->i_d
.di_nblocks
+= ralen
;
1996 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
1998 ap
->ip
->i_delayed_blks
-= ralen
;
2000 * Adjust the disk quota also. This was reserved
2003 XFS_TRANS_MOD_DQUOT_BYINO(mp
, ap
->tp
, ap
->ip
,
2004 ap
->wasdel
? XFS_TRANS_DQ_DELRTBCOUNT
:
2005 XFS_TRANS_DQ_RTBCOUNT
,
2009 #endif /* __KERNEL__ */
2012 * Normal allocation, done through xfs_alloc_vextent.
2016 xfs_alloc_arg_t args
;
2020 xfs_extlen_t longest
;
2022 xfs_extlen_t nextminlen
=0;
2025 xfs_agnumber_t startag
;
2028 tryagain
= isaligned
= 0;
2031 args
.fsbno
= ap
->rval
;
2032 args
.maxlen
= MIN(ap
->alen
, mp
->m_sb
.sb_agblocks
);
2035 args
.type
= XFS_ALLOCTYPE_START_BNO
;
2036 args
.total
= ap
->total
;
2038 * Find the longest available space.
2039 * We're going to try for the whole allocation at once.
2041 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
.fsbno
);
2043 down_read(&mp
->m_peraglock
);
2044 while (blen
< ap
->alen
) {
2045 pag
= &mp
->m_perag
[ag
];
2046 if (!pag
->pagf_init
&&
2047 (error
= xfs_alloc_pagf_init(mp
, args
.tp
,
2048 ag
, XFS_ALLOC_FLAG_TRYLOCK
))) {
2049 up_read(&mp
->m_peraglock
);
2053 * See xfs_alloc_fix_freelist...
2055 if (pag
->pagf_init
) {
2056 need
= XFS_MIN_FREELIST_PAG(pag
, mp
);
2057 delta
= need
> pag
->pagf_flcount
?
2058 need
- pag
->pagf_flcount
: 0;
2059 longest
= (pag
->pagf_longest
> delta
) ?
2060 (pag
->pagf_longest
- delta
) :
2061 (pag
->pagf_flcount
> 0 ||
2062 pag
->pagf_longest
> 0);
2067 if (++ag
== mp
->m_sb
.sb_agcount
)
2072 up_read(&mp
->m_peraglock
);
2074 * Since the above loop did a BUF_TRYLOCK, it is
2075 * possible that there is space for this request.
2077 if (notinit
|| blen
< ap
->minlen
)
2078 args
.minlen
= ap
->minlen
;
2080 * If the best seen length is less than the request
2081 * length, use the best as the minimum.
2083 else if (blen
< ap
->alen
)
2086 * Otherwise we've seen an extent as big as alen,
2087 * use that as the minimum.
2090 args
.minlen
= ap
->alen
;
2091 } else if (ap
->low
) {
2092 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
2093 args
.total
= args
.minlen
= ap
->minlen
;
2095 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
2096 args
.total
= ap
->total
;
2097 args
.minlen
= ap
->minlen
;
2099 if (ap
->ip
->i_d
.di_extsize
) {
2100 args
.prod
= ap
->ip
->i_d
.di_extsize
;
2101 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->off
, args
.prod
)))
2102 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
2103 } else if (mp
->m_sb
.sb_blocksize
>= NBPP
) {
2107 args
.prod
= NBPP
>> mp
->m_sb
.sb_blocklog
;
2108 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->off
, args
.prod
))))
2109 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
2112 * If we are not low on available data blocks, and the
2113 * underlying logical volume manager is a stripe, and
2114 * the file offset is zero then try to allocate data
2115 * blocks on stripe unit boundary.
2116 * NOTE: ap->aeof is only set if the allocation length
2117 * is >= the stripe unit and the allocation offset is
2118 * at the end of file.
2120 if (!ap
->low
&& ap
->aeof
) {
2122 args
.alignment
= mp
->m_dalign
;
2126 * Adjust for alignment
2128 if (blen
> args
.alignment
&& blen
<= ap
->alen
)
2129 args
.minlen
= blen
- args
.alignment
;
2130 args
.minalignslop
= 0;
2133 * First try an exact bno allocation.
2134 * If it fails then do a near or start bno
2135 * allocation with alignment turned on.
2139 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
2142 * Compute the minlen+alignment for the
2143 * next case. Set slop so that the value
2144 * of minlen+alignment+slop doesn't go up
2145 * between the calls.
2147 if (blen
> mp
->m_dalign
&& blen
<= ap
->alen
)
2148 nextminlen
= blen
- mp
->m_dalign
;
2150 nextminlen
= args
.minlen
;
2151 if (nextminlen
+ mp
->m_dalign
> args
.minlen
+ 1)
2153 nextminlen
+ mp
->m_dalign
-
2156 args
.minalignslop
= 0;
2160 args
.minalignslop
= 0;
2162 args
.minleft
= ap
->minleft
;
2163 args
.wasdel
= ap
->wasdel
;
2165 args
.userdata
= ap
->userdata
;
2166 if ((error
= xfs_alloc_vextent(&args
)))
2168 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
2170 * Exact allocation failed. Now try with alignment
2174 args
.fsbno
= ap
->rval
;
2175 args
.alignment
= mp
->m_dalign
;
2176 args
.minlen
= nextminlen
;
2177 args
.minalignslop
= 0;
2179 if ((error
= xfs_alloc_vextent(&args
)))
2182 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
2184 * allocation failed, so turn off alignment and
2188 args
.fsbno
= ap
->rval
;
2190 if ((error
= xfs_alloc_vextent(&args
)))
2193 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
2194 args
.minlen
> ap
->minlen
) {
2195 args
.minlen
= ap
->minlen
;
2196 args
.type
= XFS_ALLOCTYPE_START_BNO
;
2197 args
.fsbno
= ap
->rval
;
2198 if ((error
= xfs_alloc_vextent(&args
)))
2201 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
2203 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
2204 args
.total
= ap
->minlen
;
2206 if ((error
= xfs_alloc_vextent(&args
)))
2210 if (args
.fsbno
!= NULLFSBLOCK
) {
2211 ap
->firstblock
= ap
->rval
= args
.fsbno
;
2212 ASSERT(nullfb
|| fb_agno
== args
.agno
||
2213 (ap
->low
&& fb_agno
< args
.agno
));
2214 ap
->alen
= args
.len
;
2215 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
2216 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
2218 ap
->ip
->i_delayed_blks
-= args
.len
;
2220 * Adjust the disk quota also. This was reserved
2223 XFS_TRANS_MOD_DQUOT_BYINO(mp
, ap
->tp
, ap
->ip
,
2224 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
2225 XFS_TRANS_DQ_BCOUNT
,
2228 ap
->rval
= NULLFSBLOCK
;
2237 * Transform a btree format file with only one leaf node, where the
2238 * extents list will fit in the inode, into an extents format file.
2239 * Since the extent list is already in-core, all we have to do is
2240 * give up the space for the btree root and pitch the leaf block.
2242 STATIC
int /* error */
2243 xfs_bmap_btree_to_extents(
2244 xfs_trans_t
*tp
, /* transaction pointer */
2245 xfs_inode_t
*ip
, /* incore inode pointer */
2246 xfs_btree_cur_t
*cur
, /* btree cursor */
2247 int *logflagsp
, /* inode logging flags */
2248 int whichfork
) /* data or attr fork */
2251 xfs_bmbt_block_t
*cblock
;/* child btree block */
2252 xfs_fsblock_t cbno
; /* child block number */
2253 xfs_buf_t
*cbp
; /* child block's buffer */
2254 int error
; /* error return value */
2255 xfs_ifork_t
*ifp
; /* inode fork data */
2256 xfs_mount_t
*mp
; /* mount point structure */
2257 xfs_bmbt_ptr_t
*pp
; /* ptr to block address */
2258 xfs_bmbt_block_t
*rblock
;/* root btree block */
2260 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2261 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
2262 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
2263 rblock
= ifp
->if_broot
;
2264 ASSERT(INT_GET(rblock
->bb_level
, ARCH_CONVERT
) == 1);
2265 ASSERT(INT_GET(rblock
->bb_numrecs
, ARCH_CONVERT
) == 1);
2266 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp
->if_broot_bytes
) == 1);
2268 pp
= XFS_BMAP_BROOT_PTR_ADDR(rblock
, 1, ifp
->if_broot_bytes
);
2271 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(*pp
, ARCH_CONVERT
), 1)))
2274 cbno
= INT_GET(*pp
, ARCH_CONVERT
);
2275 if ((error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
,
2276 XFS_BMAP_BTREE_REF
)))
2278 cblock
= XFS_BUF_TO_BMBT_BLOCK(cbp
);
2279 if ((error
= xfs_btree_check_lblock(cur
, cblock
, 0, cbp
)))
2281 xfs_bmap_add_free(cbno
, 1, cur
->bc_private
.b
.flist
, mp
);
2282 ip
->i_d
.di_nblocks
--;
2283 XFS_TRANS_MOD_DQUOT_BYINO(mp
, tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
2284 xfs_trans_binval(tp
, cbp
);
2285 if (cur
->bc_bufs
[0] == cbp
)
2286 cur
->bc_bufs
[0] = NULL
;
2287 xfs_iroot_realloc(ip
, -1, whichfork
);
2288 ASSERT(ifp
->if_broot
== NULL
);
2289 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
2290 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
2291 *logflagsp
= XFS_ILOG_CORE
| XFS_ILOG_FEXT(whichfork
);
2296 * Called by xfs_bmapi to update extent list structure and the btree
2297 * after removing space (or undoing a delayed allocation).
2299 STATIC
int /* error */
2300 xfs_bmap_del_extent(
2301 xfs_inode_t
*ip
, /* incore inode pointer */
2302 xfs_trans_t
*tp
, /* current transaction pointer */
2303 xfs_extnum_t idx
, /* extent number to update/delete */
2304 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
2305 xfs_btree_cur_t
*cur
, /* if null, not a btree */
2306 xfs_bmbt_irec_t
*del
, /* data to remove from extent list */
2307 int *logflagsp
, /* inode logging flags */
2308 int whichfork
, /* data or attr fork */
2309 int rsvd
) /* OK to allocate reserved blocks */
2311 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
2312 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
2313 xfs_fsblock_t del_endblock
=0; /* first block past del */
2314 xfs_fileoff_t del_endoff
; /* first offset past del */
2315 int delay
; /* current block is delayed allocated */
2316 int do_fx
; /* free extent at end of routine */
2317 xfs_bmbt_rec_t
*ep
; /* current extent entry pointer */
2318 int error
; /* error return value */
2319 int flags
; /* inode logging flags */
2320 #ifdef XFS_BMAP_TRACE
2321 static char fname
[] = "xfs_bmap_del_extent";
2323 xfs_bmbt_irec_t got
; /* current extent entry */
2324 xfs_fileoff_t got_endoff
; /* first offset past got */
2325 int i
; /* temp state */
2326 xfs_ifork_t
*ifp
; /* inode fork pointer */
2327 xfs_mount_t
*mp
; /* mount structure */
2328 xfs_filblks_t nblks
; /* quota/sb block count */
2329 xfs_bmbt_irec_t
new; /* new record to be inserted */
2331 xfs_extnum_t nextents
; /* number of extents in list */
2332 uint qfield
; /* quota field to update */
2333 xfs_filblks_t temp
; /* for indirect length calculations */
2334 xfs_filblks_t temp2
; /* for indirect length calculations */
2336 XFS_STATS_INC(xfsstats
.xs_del_exlist
);
2338 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2339 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
2340 ASSERT(idx
>= 0 && idx
< nextents
);
2341 ASSERT(del
->br_blockcount
> 0);
2342 ep
= &ifp
->if_u1
.if_extents
[idx
];
2343 xfs_bmbt_get_all(ep
, &got
);
2344 ASSERT(got
.br_startoff
<= del
->br_startoff
);
2345 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
2346 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
2347 ASSERT(got_endoff
>= del_endoff
);
2348 delay
= ISNULLSTARTBLOCK(got
.br_startblock
);
2349 ASSERT(ISNULLSTARTBLOCK(del
->br_startblock
) == delay
);
2354 * If deleting a real allocation, must free up the disk space.
2357 flags
= XFS_ILOG_CORE
;
2359 * Realtime allocation. Free it and record di_nblocks update.
2361 if (whichfork
== XFS_DATA_FORK
&&
2362 (ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
)) {
2366 ASSERT(do_mod(del
->br_blockcount
,
2367 mp
->m_sb
.sb_rextsize
) == 0);
2368 ASSERT(do_mod(del
->br_startblock
,
2369 mp
->m_sb
.sb_rextsize
) == 0);
2370 bno
= del
->br_startblock
;
2371 len
= del
->br_blockcount
;
2372 do_div(bno
, mp
->m_sb
.sb_rextsize
);
2373 do_div(len
, mp
->m_sb
.sb_rextsize
);
2374 if ((error
= xfs_rtfree_extent(ip
->i_transp
, bno
,
2375 (xfs_extlen_t
)len
)))
2378 nblks
= len
* mp
->m_sb
.sb_rextsize
;
2379 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
2382 * Ordinary allocation.
2386 nblks
= del
->br_blockcount
;
2387 qfield
= XFS_TRANS_DQ_BCOUNT
;
2390 * Set up del_endblock and cur for later.
2392 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
2394 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
2395 got
.br_startblock
, got
.br_blockcount
,
2400 da_old
= da_new
= 0;
2402 da_old
= STARTBLOCKVAL(got
.br_startblock
);
2408 * Set flag value to use in switch statement.
2409 * Left-contig is 2, right-contig is 1.
2411 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
2412 (got_endoff
== del_endoff
)) {
2415 * Matches the whole extent. Delete the entry.
2417 xfs_bmap_trace_delete(fname
, "3", ip
, idx
, 1, whichfork
);
2418 xfs_bmap_delete_exlist(ip
, idx
, 1, whichfork
);
2419 ifp
->if_lastex
= idx
;
2422 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2423 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2424 flags
|= XFS_ILOG_CORE
;
2426 flags
|= XFS_ILOG_FEXT(whichfork
);
2429 if ((error
= xfs_bmbt_delete(cur
, &i
)))
2436 * Deleting the first part of the extent.
2438 xfs_bmap_trace_pre_update(fname
, "2", ip
, idx
, whichfork
);
2439 xfs_bmbt_set_startoff(ep
, del_endoff
);
2440 temp
= got
.br_blockcount
- del
->br_blockcount
;
2441 xfs_bmbt_set_blockcount(ep
, temp
);
2442 ifp
->if_lastex
= idx
;
2444 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2446 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
2447 xfs_bmap_trace_post_update(fname
, "2", ip
, idx
,
2452 xfs_bmbt_set_startblock(ep
, del_endblock
);
2453 xfs_bmap_trace_post_update(fname
, "2", ip
, idx
, whichfork
);
2455 flags
|= XFS_ILOG_FEXT(whichfork
);
2458 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
2459 got
.br_blockcount
- del
->br_blockcount
,
2466 * Deleting the last part of the extent.
2468 temp
= got
.br_blockcount
- del
->br_blockcount
;
2469 xfs_bmap_trace_pre_update(fname
, "1", ip
, idx
, whichfork
);
2470 xfs_bmbt_set_blockcount(ep
, temp
);
2471 ifp
->if_lastex
= idx
;
2473 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2475 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
2476 xfs_bmap_trace_post_update(fname
, "1", ip
, idx
,
2481 xfs_bmap_trace_post_update(fname
, "1", ip
, idx
, whichfork
);
2483 flags
|= XFS_ILOG_FEXT(whichfork
);
2486 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
2488 got
.br_blockcount
- del
->br_blockcount
,
2495 * Deleting the middle of the extent.
2497 temp
= del
->br_startoff
- got
.br_startoff
;
2498 xfs_bmap_trace_pre_update(fname
, "0", ip
, idx
, whichfork
);
2499 xfs_bmbt_set_blockcount(ep
, temp
);
2500 new.br_startoff
= del_endoff
;
2501 temp2
= got_endoff
- del_endoff
;
2502 new.br_blockcount
= temp2
;
2503 new.br_state
= got
.br_state
;
2505 new.br_startblock
= del_endblock
;
2506 flags
|= XFS_ILOG_CORE
;
2508 if ((error
= xfs_bmbt_update(cur
,
2510 got
.br_startblock
, temp
,
2513 if ((error
= xfs_bmbt_increment(cur
, 0, &i
)))
2515 cur
->bc_rec
.b
= new;
2516 error
= xfs_bmbt_insert(cur
, &i
);
2517 if (error
&& error
!= ENOSPC
)
2520 * If get no-space back from btree insert,
2521 * it tried a split, and we have a zero
2522 * block reservation.
2523 * Fix up our state and return the error.
2525 if (error
== ENOSPC
) {
2527 * Reset the cursor, don't trust
2528 * it after any insert operation.
2530 if ((error
= xfs_bmbt_lookup_eq(cur
,
2537 * Update the btree record back
2538 * to the original value.
2540 if ((error
= xfs_bmbt_update(cur
,
2547 * Reset the extent record back
2548 * to the original value.
2550 xfs_bmbt_set_blockcount(ep
,
2553 error
= XFS_ERROR(ENOSPC
);
2558 flags
|= XFS_ILOG_FEXT(whichfork
);
2559 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2560 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2562 ASSERT(whichfork
== XFS_DATA_FORK
);
2563 temp
= xfs_bmap_worst_indlen(ip
, temp
);
2564 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
2565 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
2566 new.br_startblock
= NULLSTARTBLOCK((int)temp2
);
2567 da_new
= temp
+ temp2
;
2568 while (da_new
> da_old
) {
2572 xfs_bmbt_set_startblock(ep
,
2573 NULLSTARTBLOCK((int)temp
));
2575 if (da_new
== da_old
)
2581 NULLSTARTBLOCK((int)temp2
);
2585 xfs_bmap_trace_post_update(fname
, "0", ip
, idx
, whichfork
);
2586 xfs_bmap_trace_insert(fname
, "0", ip
, idx
+ 1, 1, &new, NULL
,
2588 xfs_bmap_insert_exlist(ip
, idx
+ 1, 1, &new, whichfork
);
2589 ifp
->if_lastex
= idx
+ 1;
2593 * If we need to, add to list of extents to delete.
2596 xfs_bmap_add_free(del
->br_startblock
, del
->br_blockcount
, flist
,
2599 * Adjust inode # blocks in the file.
2602 ip
->i_d
.di_nblocks
-= nblks
;
2604 * Adjust quota data.
2607 XFS_TRANS_MOD_DQUOT_BYINO(mp
, tp
, ip
, qfield
, (long)-nblks
);
2610 * Account for change in delayed indirect blocks.
2611 * Nothing to do for disk quota accounting here.
2613 ASSERT(da_old
>= da_new
);
2614 if (da_old
> da_new
)
2615 xfs_mod_incore_sb(mp
, XFS_SBS_FDBLOCKS
, (int)(da_old
- da_new
),
2623 * Remove the entry "free" from the free item list. Prev points to the
2624 * previous entry, unless "free" is the head of the list.
2628 xfs_bmap_free_t
*flist
, /* free item list header */
2629 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
2630 xfs_bmap_free_item_t
*free
) /* list item to be freed */
2633 prev
->xbfi_next
= free
->xbfi_next
;
2635 flist
->xbf_first
= free
->xbfi_next
;
2637 kmem_zone_free(xfs_bmap_free_item_zone
, free
);
2641 * Remove count entries from the extents array for inode "ip", starting
2642 * at index "idx". Copies the remaining items down over the deleted ones,
2643 * and gives back the excess memory.
2646 xfs_bmap_delete_exlist(
2647 xfs_inode_t
*ip
, /* incore inode pointer */
2648 xfs_extnum_t idx
, /* starting delete index */
2649 xfs_extnum_t count
, /* count of items to delete */
2650 int whichfork
) /* data or attr fork */
2652 xfs_bmbt_rec_t
*base
; /* base of extent list */
2653 xfs_ifork_t
*ifp
; /* inode fork pointer */
2654 xfs_extnum_t nextents
; /* number of extents in list after */
2656 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2657 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
2658 base
= ifp
->if_u1
.if_extents
;
2659 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - count
;
2660 memmove(&base
[idx
], &base
[idx
+ count
],
2661 (nextents
- idx
) * sizeof(*base
));
2662 xfs_iext_realloc(ip
, -count
, whichfork
);
2666 * Convert an extents-format file into a btree-format file.
2667 * The new file will have a root block (in the inode) and a single child block.
2669 STATIC
int /* error */
2670 xfs_bmap_extents_to_btree(
2671 xfs_trans_t
*tp
, /* transaction pointer */
2672 xfs_inode_t
*ip
, /* incore inode pointer */
2673 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
2674 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
2675 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
2676 int wasdel
, /* converting a delayed alloc */
2677 int *logflagsp
, /* inode logging flags */
2678 int whichfork
) /* data or attr fork */
2680 xfs_bmbt_block_t
*ablock
; /* allocated (child) bt block */
2681 xfs_buf_t
*abp
; /* buffer for ablock */
2682 xfs_alloc_arg_t args
; /* allocation arguments */
2683 xfs_bmbt_rec_t
*arp
; /* child record pointer */
2684 xfs_bmbt_block_t
*block
; /* btree root block */
2685 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
2686 xfs_bmbt_rec_t
*ep
; /* extent list pointer */
2687 int error
; /* error return value */
2688 xfs_extnum_t i
, cnt
; /* extent list index */
2689 xfs_ifork_t
*ifp
; /* inode fork pointer */
2690 xfs_bmbt_key_t
*kp
; /* root block key pointer */
2691 xfs_mount_t
*mp
; /* mount structure */
2692 xfs_extnum_t nextents
; /* extent list size */
2693 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
2695 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2696 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
2697 ASSERT(ifp
->if_ext_max
==
2698 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
2700 * Make space in the inode incore.
2702 xfs_iroot_realloc(ip
, 1, whichfork
);
2703 ifp
->if_flags
|= XFS_IFBROOT
;
2707 block
= ifp
->if_broot
;
2708 INT_SET(block
->bb_magic
, ARCH_CONVERT
, XFS_BMAP_MAGIC
);
2709 INT_SET(block
->bb_level
, ARCH_CONVERT
, 1);
2710 INT_SET(block
->bb_numrecs
, ARCH_CONVERT
, 1);
2711 INT_SET(block
->bb_leftsib
, ARCH_CONVERT
, NULLDFSBNO
);
2712 INT_SET(block
->bb_rightsib
, ARCH_CONVERT
, NULLDFSBNO
);
2714 * Need a cursor. Can't allocate until bb_level is filled in.
2717 cur
= xfs_btree_init_cursor(mp
, tp
, NULL
, 0, XFS_BTNUM_BMAP
, ip
,
2719 cur
->bc_private
.b
.firstblock
= *firstblock
;
2720 cur
->bc_private
.b
.flist
= flist
;
2721 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
2723 * Convert to a btree with two levels, one record in root.
2725 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
2728 if (*firstblock
== NULLFSBLOCK
) {
2729 args
.type
= XFS_ALLOCTYPE_START_BNO
;
2730 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
2731 } else if (flist
->xbf_low
) {
2732 args
.type
= XFS_ALLOCTYPE_START_BNO
;
2733 args
.fsbno
= *firstblock
;
2735 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
2736 args
.fsbno
= *firstblock
;
2738 args
.minlen
= args
.maxlen
= args
.prod
= 1;
2739 args
.total
= args
.minleft
= args
.alignment
= args
.mod
= args
.isfl
=
2740 args
.minalignslop
= 0;
2741 args
.wasdel
= wasdel
;
2743 if ((error
= xfs_alloc_vextent(&args
))) {
2744 xfs_iroot_realloc(ip
, -1, whichfork
);
2745 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
2749 * Allocation can't fail, the space was reserved.
2751 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
2752 ASSERT(*firstblock
== NULLFSBLOCK
||
2753 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
2755 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
2756 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
2757 cur
->bc_private
.b
.allocated
++;
2758 ip
->i_d
.di_nblocks
++;
2759 XFS_TRANS_MOD_DQUOT_BYINO(mp
, tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
2760 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
2762 * Fill in the child block.
2764 ablock
= XFS_BUF_TO_BMBT_BLOCK(abp
);
2765 INT_SET(ablock
->bb_magic
, ARCH_CONVERT
, XFS_BMAP_MAGIC
);
2766 INT_ZERO(ablock
->bb_level
, ARCH_CONVERT
);
2767 INT_SET(ablock
->bb_leftsib
, ARCH_CONVERT
, NULLDFSBNO
);
2768 INT_SET(ablock
->bb_rightsib
, ARCH_CONVERT
, NULLDFSBNO
);
2769 arp
= XFS_BMAP_REC_IADDR(ablock
, 1, cur
);
2770 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
2771 for (ep
= ifp
->if_u1
.if_extents
, cnt
= i
= 0; i
< nextents
; i
++, ep
++) {
2772 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep
))) {
2773 arp
->l0
= INT_GET(ep
->l0
, ARCH_CONVERT
);
2774 arp
->l1
= INT_GET(ep
->l1
, ARCH_CONVERT
);
2778 INT_SET(ablock
->bb_numrecs
, ARCH_CONVERT
, cnt
);
2779 ASSERT(INT_GET(ablock
->bb_numrecs
, ARCH_CONVERT
) == XFS_IFORK_NEXTENTS(ip
, whichfork
));
2781 * Fill in the root key and pointer.
2783 kp
= XFS_BMAP_KEY_IADDR(block
, 1, cur
);
2784 arp
= XFS_BMAP_REC_IADDR(ablock
, 1, cur
);
2785 INT_SET(kp
->br_startoff
, ARCH_CONVERT
, xfs_bmbt_disk_get_startoff(arp
));
2786 pp
= XFS_BMAP_PTR_IADDR(block
, 1, cur
);
2787 INT_SET(*pp
, ARCH_CONVERT
, args
.fsbno
);
2789 * Do all this logging at the end so that
2790 * the root is at the right level.
2792 xfs_bmbt_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
2793 xfs_bmbt_log_recs(cur
, abp
, 1, INT_GET(ablock
->bb_numrecs
, ARCH_CONVERT
));
2794 ASSERT(*curp
== NULL
);
2796 *logflagsp
= XFS_ILOG_CORE
| XFS_ILOG_FBROOT(whichfork
);
2801 * Insert new item(s) in the extent list for inode "ip".
2802 * Count new items are inserted at offset idx.
2805 xfs_bmap_insert_exlist(
2806 xfs_inode_t
*ip
, /* incore inode pointer */
2807 xfs_extnum_t idx
, /* starting index of new items */
2808 xfs_extnum_t count
, /* number of inserted items */
2809 xfs_bmbt_irec_t
*new, /* items to insert */
2810 int whichfork
) /* data or attr fork */
2812 xfs_bmbt_rec_t
*base
; /* extent list base */
2813 xfs_ifork_t
*ifp
; /* inode fork pointer */
2814 xfs_extnum_t nextents
; /* extent list size */
2815 xfs_extnum_t to
; /* extent list index */
2817 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2818 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
2819 xfs_iext_realloc(ip
, count
, whichfork
);
2820 base
= ifp
->if_u1
.if_extents
;
2821 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
2822 memmove(&base
[idx
+ count
], &base
[idx
],
2823 (nextents
- (idx
+ count
)) * sizeof(*base
));
2824 for (to
= idx
; to
< idx
+ count
; to
++, new++)
2825 xfs_bmbt_set_all(&base
[to
], new);
2829 * Convert a local file to an extents file.
2830 * This code is out of bounds for data forks of regular files,
2831 * since the file data needs to get logged so things will stay consistent.
2832 * (The bmap-level manipulations are ok, though).
2834 STATIC
int /* error */
2835 xfs_bmap_local_to_extents(
2836 xfs_trans_t
*tp
, /* transaction pointer */
2837 xfs_inode_t
*ip
, /* incore inode pointer */
2838 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
2839 xfs_extlen_t total
, /* total blocks needed by transaction */
2840 int *logflagsp
, /* inode logging flags */
2841 int whichfork
) /* data or attr fork */
2843 int error
; /* error return value */
2844 int flags
; /* logging flags returned */
2845 #ifdef XFS_BMAP_TRACE
2846 static char fname
[] = "xfs_bmap_local_to_extents";
2848 xfs_ifork_t
*ifp
; /* inode fork pointer */
2851 * We don't want to deal with the case of keeping inode data inline yet.
2852 * So sending the data fork of a regular inode is illegal.
2854 ASSERT(!((ip
->i_d
.di_mode
& IFMT
) == IFREG
&&
2855 whichfork
== XFS_DATA_FORK
));
2856 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2857 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
2860 if (ifp
->if_bytes
) {
2861 xfs_alloc_arg_t args
; /* allocation arguments */
2862 xfs_buf_t
*bp
; /* buffer for extent list block */
2863 xfs_bmbt_rec_t
*ep
; /* extent list pointer */
2866 args
.mp
= ip
->i_mount
;
2867 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
2869 * Allocate a block. We know we need only one, since the
2870 * file currently fits in an inode.
2872 if (*firstblock
== NULLFSBLOCK
) {
2873 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
2874 args
.type
= XFS_ALLOCTYPE_START_BNO
;
2876 args
.fsbno
= *firstblock
;
2877 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
2880 args
.mod
= args
.minleft
= args
.alignment
= args
.wasdel
=
2881 args
.isfl
= args
.minalignslop
= 0;
2882 args
.minlen
= args
.maxlen
= args
.prod
= 1;
2883 if ((error
= xfs_alloc_vextent(&args
)))
2886 * Can't fail, the space was reserved.
2888 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
2889 ASSERT(args
.len
== 1);
2890 *firstblock
= args
.fsbno
;
2891 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
2892 memcpy((char *)XFS_BUF_PTR(bp
), ifp
->if_u1
.if_data
,
2894 xfs_trans_log_buf(tp
, bp
, 0, ifp
->if_bytes
- 1);
2895 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
2896 xfs_iext_realloc(ip
, 1, whichfork
);
2897 ep
= ifp
->if_u1
.if_extents
;
2898 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
2899 xfs_bmap_trace_post_update(fname
, "new", ip
, 0, whichfork
);
2900 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
2901 ip
->i_d
.di_nblocks
= 1;
2902 XFS_TRANS_MOD_DQUOT_BYINO(args
.mp
, tp
, ip
,
2903 XFS_TRANS_DQ_BCOUNT
, 1L);
2904 flags
|= XFS_ILOG_FEXT(whichfork
);
2906 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
2907 ifp
->if_flags
&= ~XFS_IFINLINE
;
2908 ifp
->if_flags
|= XFS_IFEXTENTS
;
2909 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
2910 flags
|= XFS_ILOG_CORE
;
2916 xfs_bmbt_rec_t
* /* pointer to found extent entry */
2917 xfs_bmap_do_search_extents(
2918 xfs_bmbt_rec_t
*base
, /* base of extent list */
2919 xfs_extnum_t lastx
, /* last extent index used */
2920 xfs_extnum_t nextents
, /* extent list size */
2921 xfs_fileoff_t bno
, /* block number searched for */
2922 int *eofp
, /* out: end of file found */
2923 xfs_extnum_t
*lastxp
, /* out: last extent index */
2924 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
2925 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
2927 xfs_bmbt_rec_t
*ep
; /* extent list entry pointer */
2928 xfs_bmbt_irec_t got
; /* extent list entry, decoded */
2929 int high
; /* high index of binary search */
2930 int low
; /* low index of binary search */
2932 if (lastx
!= NULLEXTNUM
&& lastx
< nextents
)
2936 prevp
->br_startoff
= NULLFILEOFF
;
2937 if (ep
&& bno
>= (got
.br_startoff
= xfs_bmbt_get_startoff(ep
)) &&
2938 bno
< got
.br_startoff
+
2939 (got
.br_blockcount
= xfs_bmbt_get_blockcount(ep
)))
2941 else if (ep
&& lastx
< nextents
- 1 &&
2942 bno
>= (got
.br_startoff
= xfs_bmbt_get_startoff(ep
+ 1)) &&
2943 bno
< got
.br_startoff
+
2944 (got
.br_blockcount
= xfs_bmbt_get_blockcount(ep
+ 1))) {
2948 } else if (nextents
== 0)
2950 else if (bno
== 0 &&
2951 (got
.br_startoff
= xfs_bmbt_get_startoff(base
)) == 0) {
2954 got
.br_blockcount
= xfs_bmbt_get_blockcount(ep
);
2957 /* binary search the extents array */
2959 high
= nextents
- 1;
2960 while (low
<= high
) {
2961 XFS_STATS_INC(xfsstats
.xs_cmp_exlist
);
2962 lastx
= (low
+ high
) >> 1;
2964 got
.br_startoff
= xfs_bmbt_get_startoff(ep
);
2965 got
.br_blockcount
= xfs_bmbt_get_blockcount(ep
);
2966 if (bno
< got
.br_startoff
)
2968 else if (bno
>= got
.br_startoff
+ got
.br_blockcount
)
2971 got
.br_startblock
= xfs_bmbt_get_startblock(ep
);
2972 got
.br_state
= xfs_bmbt_get_state(ep
);
2979 if (bno
>= got
.br_startoff
+ got
.br_blockcount
) {
2981 if (lastx
== nextents
) {
2983 got
.br_startblock
= xfs_bmbt_get_startblock(ep
);
2984 got
.br_state
= xfs_bmbt_get_state(ep
);
2989 xfs_bmbt_get_all(ep
, prevp
);
2991 got
.br_startoff
= xfs_bmbt_get_startoff(ep
);
2992 got
.br_blockcount
= xfs_bmbt_get_blockcount(ep
);
2997 xfs_bmbt_get_all(ep
- 1, prevp
);
3001 got
.br_startblock
= xfs_bmbt_get_startblock(ep
);
3002 got
.br_state
= xfs_bmbt_get_state(ep
);
3010 * Search the extents list for the inode, for the extent containing bno.
3011 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3012 * *eofp will be set, and *prevp will contain the last entry (null if none).
3013 * Else, *lastxp will be set to the index of the found
3014 * entry; *gotp will contain the entry.
3016 STATIC xfs_bmbt_rec_t
* /* pointer to found extent entry */
3017 xfs_bmap_search_extents(
3018 xfs_inode_t
*ip
, /* incore inode pointer */
3019 xfs_fileoff_t bno
, /* block number searched for */
3020 int whichfork
, /* data or attr fork */
3021 int *eofp
, /* out: end of file found */
3022 xfs_extnum_t
*lastxp
, /* out: last extent index */
3023 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
3024 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
3026 xfs_ifork_t
*ifp
; /* inode fork pointer */
3027 xfs_bmbt_rec_t
*base
; /* base of extent list */
3028 xfs_extnum_t lastx
; /* last extent index used */
3029 xfs_extnum_t nextents
; /* extent list size */
3031 XFS_STATS_INC(xfsstats
.xs_look_exlist
);
3032 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3033 lastx
= ifp
->if_lastex
;
3034 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
3035 base
= &ifp
->if_u1
.if_extents
[0];
3037 return xfs_bmap_do_search_extents(base
, lastx
, nextents
, bno
, eofp
,
3038 lastxp
, gotp
, prevp
);
3042 * Compute the worst-case number of indirect blocks that will be used
3043 * for ip's delayed extent of length "len".
3045 STATIC xfs_filblks_t
3046 xfs_bmap_worst_indlen(
3047 xfs_inode_t
*ip
, /* incore inode pointer */
3048 xfs_filblks_t len
) /* delayed extent length */
3050 int level
; /* btree level number */
3051 int maxrecs
; /* maximum record count at this level */
3052 xfs_mount_t
*mp
; /* mount structure */
3053 xfs_filblks_t rval
; /* return value */
3056 maxrecs
= mp
->m_bmap_dmxr
[0];
3057 for (level
= 0, rval
= 0;
3058 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
3061 do_div(len
, maxrecs
);
3064 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
3067 maxrecs
= mp
->m_bmap_dmxr
[1];
3073 * Add the extent to the list of extents to be free at transaction end.
3074 * The list is maintained sorted (by block number).
3079 xfs_fsblock_t bno
, /* fs block number of extent */
3080 xfs_filblks_t len
, /* length of extent */
3081 xfs_bmap_free_t
*flist
, /* list of extents */
3082 xfs_mount_t
*mp
) /* mount point structure */
3084 xfs_bmap_free_item_t
*cur
; /* current (next) element */
3085 xfs_bmap_free_item_t
*new; /* new element */
3086 xfs_bmap_free_item_t
*prev
; /* previous element */
3088 xfs_agnumber_t agno
;
3089 xfs_agblock_t agbno
;
3091 ASSERT(bno
!= NULLFSBLOCK
);
3093 ASSERT(len
<= MAXEXTLEN
);
3094 ASSERT(!ISNULLSTARTBLOCK(bno
));
3095 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
3096 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
3097 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
3098 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
3099 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
3100 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
3102 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
3103 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
3104 new->xbfi_startblock
= bno
;
3105 new->xbfi_blockcount
= (xfs_extlen_t
)len
;
3106 for (prev
= NULL
, cur
= flist
->xbf_first
;
3108 prev
= cur
, cur
= cur
->xbfi_next
) {
3109 if (cur
->xbfi_startblock
>= bno
)
3113 prev
->xbfi_next
= new;
3115 flist
->xbf_first
= new;
3116 new->xbfi_next
= cur
;
3121 * Compute and fill in the value of the maximum depth of a bmap btree
3122 * in this filesystem. Done once, during mount.
3125 xfs_bmap_compute_maxlevels(
3126 xfs_mount_t
*mp
, /* file system mount structure */
3127 int whichfork
) /* data or attr fork */
3129 int level
; /* btree level */
3130 uint maxblocks
; /* max blocks at this level */
3131 uint maxleafents
; /* max leaf entries possible */
3132 int maxrootrecs
; /* max records in root block */
3133 int minleafrecs
; /* min records in leaf block */
3134 int minnoderecs
; /* min records in node block */
3135 int sz
; /* root block size */
3138 * The maximum number of extents in a file, hence the maximum
3139 * number of leaf entries, is controlled by the type of di_nextents
3140 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3141 * (a signed 16-bit number, xfs_aextnum_t).
3143 maxleafents
= (whichfork
== XFS_DATA_FORK
) ? MAXEXTNUM
: MAXAEXTNUM
;
3144 minleafrecs
= mp
->m_bmap_dmnr
[0];
3145 minnoderecs
= mp
->m_bmap_dmnr
[1];
3146 sz
= (whichfork
== XFS_DATA_FORK
) ?
3148 mp
->m_sb
.sb_inodesize
- mp
->m_attroffset
;
3149 maxrootrecs
= (int)XFS_BTREE_BLOCK_MAXRECS(sz
, xfs_bmdr
, 0);
3150 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
3151 for (level
= 1; maxblocks
> 1; level
++) {
3152 if (maxblocks
<= maxrootrecs
)
3155 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
3157 mp
->m_bm_maxlevels
[whichfork
] = level
;
3161 * Returns the file-relative block number of the first unused block(s)
3162 * in the file with at least "len" logically contiguous blocks free.
3163 * This is the lowest-address hole if the file has holes, else the first block
3164 * past the end of file.
3165 * Return 0 if the file is currently local (in-inode).
3168 xfs_bmap_first_unused(
3169 xfs_trans_t
*tp
, /* transaction pointer */
3170 xfs_inode_t
*ip
, /* incore inode */
3171 xfs_extlen_t len
, /* size of hole to find */
3172 xfs_fileoff_t
*first_unused
, /* unused block */
3173 int whichfork
) /* data or attr fork */
3175 xfs_bmbt_rec_t
*base
; /* base of extent array */
3176 xfs_bmbt_rec_t
*ep
; /* pointer to an extent entry */
3177 int error
; /* error return value */
3178 xfs_ifork_t
*ifp
; /* inode fork pointer */
3179 xfs_fileoff_t lastaddr
; /* last block number seen */
3180 xfs_fileoff_t lowest
; /* lowest useful block */
3181 xfs_fileoff_t max
; /* starting useful block */
3182 xfs_fileoff_t off
; /* offset for this block */
3183 xfs_extnum_t nextents
; /* number of extent entries */
3185 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
3186 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
3187 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
3188 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
3192 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3193 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
3194 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
3196 lowest
= *first_unused
;
3197 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
3198 base
= &ifp
->if_u1
.if_extents
[0];
3199 for (lastaddr
= 0, max
= lowest
, ep
= base
;
3200 ep
< &base
[nextents
];
3202 off
= xfs_bmbt_get_startoff(ep
);
3204 * See if the hole before this extent will work.
3206 if (off
>= lowest
+ len
&& off
- max
>= len
) {
3207 *first_unused
= max
;
3210 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
3211 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
3213 *first_unused
= max
;
3218 * Returns the file-relative block number of the last block + 1 before
3219 * last_block (input value) in the file.
3220 * This is not based on i_size, it is based on the extent list.
3221 * Returns 0 for local files, as they do not have an extent list.
3224 xfs_bmap_last_before(
3225 xfs_trans_t
*tp
, /* transaction pointer */
3226 xfs_inode_t
*ip
, /* incore inode */
3227 xfs_fileoff_t
*last_block
, /* last block */
3228 int whichfork
) /* data or attr fork */
3230 xfs_fileoff_t bno
; /* input file offset */
3231 int eof
; /* hit end of file */
3232 xfs_bmbt_rec_t
*ep
; /* pointer to last extent */
3233 int error
; /* error return value */
3234 xfs_bmbt_irec_t got
; /* current extent value */
3235 xfs_ifork_t
*ifp
; /* inode fork pointer */
3236 xfs_extnum_t lastx
; /* last extent used */
3237 xfs_bmbt_irec_t prev
; /* previous extent value */
3239 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
3240 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3241 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
3242 return XFS_ERROR(EIO
);
3243 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
3247 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3248 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
3249 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
3251 bno
= *last_block
- 1;
3252 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
3254 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
3255 if (prev
.br_startoff
== NULLFILEOFF
)
3258 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
3261 * Otherwise *last_block is already the right answer.
3267 * Returns the file-relative block number of the first block past eof in
3268 * the file. This is not based on i_size, it is based on the extent list.
3269 * Returns 0 for local files, as they do not have an extent list.
3272 xfs_bmap_last_offset(
3273 xfs_trans_t
*tp
, /* transaction pointer */
3274 xfs_inode_t
*ip
, /* incore inode */
3275 xfs_fileoff_t
*last_block
, /* last block */
3276 int whichfork
) /* data or attr fork */
3278 xfs_bmbt_rec_t
*base
; /* base of extent array */
3279 xfs_bmbt_rec_t
*ep
; /* pointer to last extent */
3280 int error
; /* error return value */
3281 xfs_ifork_t
*ifp
; /* inode fork pointer */
3282 xfs_extnum_t nextents
; /* number of extent entries */
3284 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
3285 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3286 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
3287 return XFS_ERROR(EIO
);
3288 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
3292 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3293 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
3294 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
3296 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
3301 base
= &ifp
->if_u1
.if_extents
[0];
3302 ASSERT(base
!= NULL
);
3303 ep
= &base
[nextents
- 1];
3304 *last_block
= xfs_bmbt_get_startoff(ep
) + xfs_bmbt_get_blockcount(ep
);
3309 * Returns whether the selected fork of the inode has exactly one
3310 * block or not. For the data fork we check this matches di_size,
3311 * implying the file's range is 0..bsize-1.
3313 int /* 1=>1 block, 0=>otherwise */
3315 xfs_inode_t
*ip
, /* incore inode */
3316 int whichfork
) /* data or attr fork */
3318 xfs_bmbt_rec_t
*ep
; /* ptr to fork's extent */
3319 xfs_ifork_t
*ifp
; /* inode fork pointer */
3320 int rval
; /* return value */
3321 xfs_bmbt_irec_t s
; /* internal version of extent */
3324 if (whichfork
== XFS_DATA_FORK
)
3325 return ip
->i_d
.di_size
== ip
->i_mount
->m_sb
.sb_blocksize
;
3327 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
3329 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
3331 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3332 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
3333 ep
= ifp
->if_u1
.if_extents
;
3334 xfs_bmbt_get_all(ep
, &s
);
3335 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
3336 if (rval
&& whichfork
== XFS_DATA_FORK
)
3337 ASSERT(ip
->i_d
.di_size
== ip
->i_mount
->m_sb
.sb_blocksize
);
3342 * Read in the extents to if_extents.
3343 * All inode fields are set up by caller, we just traverse the btree
3344 * and copy the records in. If the file system cannot contain unwritten
3345 * extents, the records are checked for no "state" flags.
3348 xfs_bmap_read_extents(
3349 xfs_trans_t
*tp
, /* transaction pointer */
3350 xfs_inode_t
*ip
, /* incore inode */
3351 int whichfork
) /* data or attr fork */
3353 xfs_bmbt_block_t
*block
; /* current btree block */
3354 xfs_fsblock_t bno
; /* block # of "block" */
3355 xfs_buf_t
*bp
; /* buffer for "block" */
3356 int error
; /* error return value */
3357 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
3358 #ifdef XFS_BMAP_TRACE
3359 static char fname
[] = "xfs_bmap_read_extents";
3361 xfs_extnum_t i
, j
; /* index into the extents list */
3362 xfs_ifork_t
*ifp
; /* fork structure */
3363 int level
; /* btree level, for checking */
3364 xfs_mount_t
*mp
; /* file system mount structure */
3365 xfs_bmbt_ptr_t
*pp
; /* pointer to block address */
3367 xfs_extnum_t room
; /* number of entries there's room for */
3368 xfs_bmbt_rec_t
*trp
; /* target record pointer */
3372 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3373 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
3374 XFS_EXTFMT_INODE(ip
);
3375 block
= ifp
->if_broot
;
3377 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
3379 ASSERT(INT_GET(block
->bb_level
, ARCH_CONVERT
) > 0);
3380 level
= INT_GET(block
->bb_level
, ARCH_CONVERT
);
3381 pp
= XFS_BMAP_BROOT_PTR_ADDR(block
, 1, ifp
->if_broot_bytes
);
3382 ASSERT(INT_GET(*pp
, ARCH_CONVERT
) != NULLDFSBNO
);
3383 ASSERT(XFS_FSB_TO_AGNO(mp
, INT_GET(*pp
, ARCH_CONVERT
)) < mp
->m_sb
.sb_agcount
);
3384 ASSERT(XFS_FSB_TO_AGBNO(mp
, INT_GET(*pp
, ARCH_CONVERT
)) < mp
->m_sb
.sb_agblocks
);
3385 bno
= INT_GET(*pp
, ARCH_CONVERT
);
3387 * Go down the tree until leaf level is reached, following the first
3388 * pointer (leftmost) at each level.
3390 while (level
-- > 0) {
3391 if ((error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
3392 XFS_BMAP_BTREE_REF
)))
3394 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
3395 XFS_WANT_CORRUPTED_GOTO(
3396 XFS_BMAP_SANITY_CHECK(mp
, block
, level
),
3400 pp
= XFS_BTREE_PTR_ADDR(mp
->m_sb
.sb_blocksize
, xfs_bmbt
, block
,
3401 1, mp
->m_bmap_dmxr
[1]);
3402 XFS_WANT_CORRUPTED_GOTO(
3403 XFS_FSB_SANITY_CHECK(mp
, INT_GET(*pp
, ARCH_CONVERT
)),
3405 bno
= INT_GET(*pp
, ARCH_CONVERT
);
3406 xfs_trans_brelse(tp
, bp
);
3409 * Here with bp and block set to the leftmost leaf node in the tree.
3411 room
= ifp
->if_bytes
/ (uint
)sizeof(*trp
);
3412 trp
= ifp
->if_u1
.if_extents
;
3415 * Loop over all leaf nodes. Copy information to the extent list.
3418 xfs_bmbt_rec_t
*frp
, *temp
;
3419 xfs_fsblock_t nextbno
;
3420 xfs_extnum_t num_recs
;
3423 num_recs
= INT_GET(block
->bb_numrecs
, ARCH_CONVERT
);
3424 if (unlikely(i
+ num_recs
> room
)) {
3425 ASSERT(i
+ num_recs
<= room
);
3426 xfs_fs_cmn_err(CE_WARN
, ip
->i_mount
,
3427 "corrupt dinode %Lu, (btree extents). Unmount and run xfs_repair.",
3428 (unsigned long long) ip
->i_ino
);
3429 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
3434 XFS_WANT_CORRUPTED_GOTO(
3435 XFS_BMAP_SANITY_CHECK(mp
, block
, 0),
3438 * Read-ahead the next leaf block, if any.
3440 nextbno
= INT_GET(block
->bb_rightsib
, ARCH_CONVERT
);
3441 if (nextbno
!= NULLFSBLOCK
)
3442 xfs_btree_reada_bufl(mp
, nextbno
, 1);
3444 * Copy records into the extent list.
3446 frp
= XFS_BTREE_REC_ADDR(mp
->m_sb
.sb_blocksize
, xfs_bmbt
,
3447 block
, 1, mp
->m_bmap_dmxr
[0]);
3449 for (j
= 0; j
< num_recs
; j
++, frp
++, trp
++) {
3450 trp
->l0
= INT_GET(frp
->l0
, ARCH_CONVERT
);
3451 trp
->l1
= INT_GET(frp
->l1
, ARCH_CONVERT
);
3453 if (exntf
== XFS_EXTFMT_NOSTATE
) {
3455 * Check all attribute bmap btree records and
3456 * any "older" data bmap btree records for a
3457 * set bit in the "extent flag" position.
3459 if (unlikely(xfs_check_nostate_extents(temp
, num_recs
))) {
3460 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
3467 xfs_trans_brelse(tp
, bp
);
3470 * If we've reached the end, stop.
3472 if (bno
== NULLFSBLOCK
)
3474 if ((error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
3475 XFS_BMAP_BTREE_REF
)))
3477 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
3479 ASSERT(i
== ifp
->if_bytes
/ (uint
)sizeof(*trp
));
3480 ASSERT(i
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
3481 xfs_bmap_trace_exlist(fname
, ip
, i
, whichfork
);
3484 xfs_trans_brelse(tp
, bp
);
3485 return XFS_ERROR(EFSCORRUPTED
);
3489 * Map file blocks to filesystem blocks.
3490 * File range is given by the bno/len pair.
3491 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
3492 * into a hole or past eof.
3493 * Only allocates blocks from a single allocation group,
3494 * to avoid locking problems.
3495 * The returned value in "firstblock" from the first call in a transaction
3496 * must be remembered and presented to subsequent calls in "firstblock".
3497 * An upper bound for the number of blocks to be allocated is supplied to
3498 * the first call in "total"; if no allocation group has that many free
3499 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
3503 xfs_trans_t
*tp
, /* transaction pointer */
3504 xfs_inode_t
*ip
, /* incore inode */
3505 xfs_fileoff_t bno
, /* starting file offs. mapped */
3506 xfs_filblks_t len
, /* length to map in file */
3507 int flags
, /* XFS_BMAPI_... */
3508 xfs_fsblock_t
*firstblock
, /* first allocated block
3509 controls a.g. for allocs */
3510 xfs_extlen_t total
, /* total blocks needed */
3511 xfs_bmbt_irec_t
*mval
, /* output: map values */
3512 int *nmap
, /* i/o: mval size/count */
3513 xfs_bmap_free_t
*flist
) /* i/o: list extents to free */
3515 xfs_fsblock_t abno
; /* allocated block number */
3516 xfs_extlen_t alen
; /* allocated extent length */
3517 xfs_fileoff_t aoff
; /* allocated file offset */
3518 xfs_bmalloca_t bma
; /* args for xfs_bmap_alloc */
3519 char contig
; /* allocation must be one extent */
3520 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
3521 char delay
; /* this request is for delayed alloc */
3522 xfs_fileoff_t end
; /* end of mapped file region */
3523 int eof
; /* we've hit the end of extent list */
3524 xfs_bmbt_rec_t
*ep
; /* extent list entry pointer */
3525 int error
; /* error return */
3526 char exact
; /* don't do all of wasdelayed extent */
3527 xfs_bmbt_irec_t got
; /* current extent list record */
3528 xfs_ifork_t
*ifp
; /* inode fork pointer */
3529 xfs_extlen_t indlen
; /* indirect blocks length */
3530 char inhole
; /* current location is hole in file */
3531 xfs_extnum_t lastx
; /* last useful extent number */
3532 int logflags
; /* flags for transaction logging */
3533 xfs_extlen_t minleft
; /* min blocks left after allocation */
3534 xfs_extlen_t minlen
; /* min allocation size */
3535 xfs_mount_t
*mp
; /* xfs mount structure */
3536 int n
; /* current extent index */
3537 int nallocs
; /* number of extents alloc\'d */
3538 xfs_extnum_t nextents
; /* number of extents in file */
3539 xfs_fileoff_t obno
; /* old block number (offset) */
3540 xfs_bmbt_irec_t prev
; /* previous extent list record */
3541 char stateless
; /* ignore state flag set */
3542 int tmp_logflags
; /* temp flags holder */
3543 char trim
; /* output trimmed to match range */
3544 char userdata
; /* allocating non-metadata */
3545 char wasdelay
; /* old extent was delayed */
3546 int whichfork
; /* data or attr fork */
3547 char wr
; /* this is a write request */
3548 char rsvd
; /* OK to allocate reserved blocks */
3550 xfs_fileoff_t orig_bno
; /* original block number value */
3551 int orig_flags
; /* original flags arg value */
3552 xfs_filblks_t orig_len
; /* original value of len arg */
3553 xfs_bmbt_irec_t
*orig_mval
; /* original value of mval */
3554 int orig_nmap
; /* original value of *nmap */
3563 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
|| !(flags
& XFS_BMAPI_WRITE
));
3564 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
3565 XFS_ATTR_FORK
: XFS_DATA_FORK
;
3567 if (unlikely(XFS_TEST_ERROR(
3568 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
3569 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
3570 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
),
3571 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
3572 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW
, mp
);
3573 return XFS_ERROR(EFSCORRUPTED
);
3575 if (XFS_FORCED_SHUTDOWN(mp
))
3576 return XFS_ERROR(EIO
);
3577 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3578 ASSERT(ifp
->if_ext_max
==
3579 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
3580 if ((wr
= (flags
& XFS_BMAPI_WRITE
)) != 0)
3581 XFS_STATS_INC(xfsstats
.xs_blk_mapw
);
3583 XFS_STATS_INC(xfsstats
.xs_blk_mapr
);
3584 delay
= (flags
& XFS_BMAPI_DELAY
) != 0;
3585 trim
= (flags
& XFS_BMAPI_ENTIRE
) == 0;
3586 userdata
= (flags
& XFS_BMAPI_METADATA
) == 0;
3587 exact
= (flags
& XFS_BMAPI_EXACT
) != 0;
3588 rsvd
= (flags
& XFS_BMAPI_RSVBLOCKS
) != 0;
3589 contig
= (flags
& XFS_BMAPI_CONTIG
) != 0;
3591 * stateless is used to combine extents which
3592 * differ only due to the state of the extents.
3593 * This technique is used from xfs_getbmap()
3594 * when the caller does not wish to see the
3595 * separation (which is the default).
3597 * This technique is also used when writing a
3598 * buffer which has been partially written,
3599 * (usually by being flushed during a chunkread),
3600 * to ensure one write takes place. This also
3601 * prevents a change in the xfs inode extents at
3602 * this time, intentionally. This change occurs
3603 * on completion of the write operation, in
3604 * xfs_strat_comp(), where the xfs_bmapi() call
3605 * is transactioned, and the extents combined.
3607 stateless
= (flags
& XFS_BMAPI_IGSTATE
) != 0;
3608 if (stateless
&& wr
) /* if writing unwritten space, no */
3609 wr
= 0; /* allocations are allowed */
3610 ASSERT(wr
|| !delay
);
3614 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
3616 if ((error
= xfs_bmap_local_to_extents(tp
, ip
,
3617 firstblock
, total
, &logflags
, whichfork
)))
3620 if (wr
&& *firstblock
== NULLFSBLOCK
) {
3621 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
3622 minleft
= INT_GET(ifp
->if_broot
->bb_level
, ARCH_CONVERT
) + 1;
3627 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
3628 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
3630 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
3632 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
3637 while (bno
< end
&& n
< *nmap
) {
3639 * Reading past eof, act as though there's a hole
3643 got
.br_startoff
= end
;
3644 inhole
= eof
|| got
.br_startoff
> bno
;
3645 wasdelay
= wr
&& !inhole
&& !delay
&&
3646 ISNULLSTARTBLOCK(got
.br_startblock
);
3648 * First, deal with the hole before the allocated space
3649 * that we found, if any.
3651 if (wr
&& (inhole
|| wasdelay
)) {
3653 * For the wasdelay case, we could also just
3654 * allocate the stuff asked for in this bmap call
3655 * but that wouldn't be as good.
3657 if (wasdelay
&& !exact
) {
3658 alen
= (xfs_extlen_t
)got
.br_blockcount
;
3659 aoff
= got
.br_startoff
;
3660 if (lastx
!= NULLEXTNUM
&& lastx
) {
3661 ep
= &ifp
->if_u1
.if_extents
[lastx
- 1];
3662 xfs_bmbt_get_all(ep
, &prev
);
3664 } else if (wasdelay
) {
3665 alen
= (xfs_extlen_t
)
3666 XFS_FILBLKS_MIN(len
,
3668 got
.br_blockcount
) - bno
);
3671 alen
= (xfs_extlen_t
)
3672 XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
3674 alen
= (xfs_extlen_t
)
3675 XFS_FILBLKS_MIN(alen
,
3676 got
.br_startoff
- bno
);
3679 minlen
= contig
? alen
: 1;
3681 indlen
= (xfs_extlen_t
)
3682 xfs_bmap_worst_indlen(ip
, alen
);
3685 * Make a transaction-less quota reservation for
3686 * delayed allocation blocks. This number gets
3688 * We return EDQUOT if we haven't allocated
3689 * blks already inside this loop;
3691 if (XFS_TRANS_RESERVE_BLKQUOTA(
3692 mp
, NULL
, ip
, (long)alen
)) {
3695 ASSERT(cur
== NULL
);
3696 return XFS_ERROR(EDQUOT
);
3700 if (xfs_mod_incore_sb(mp
, XFS_SBS_FDBLOCKS
,
3701 -(alen
+ indlen
), rsvd
)) {
3702 XFS_TRANS_UNRESERVE_BLKQUOTA(
3703 mp
, NULL
, ip
, (long)alen
);
3706 ip
->i_delayed_blks
+= alen
;
3707 abno
= NULLSTARTBLOCK(indlen
);
3710 * If first time, allocate and fill in
3711 * once-only bma fields.
3713 if (bma
.ip
== NULL
) {
3721 /* Indicate if this is the first user data
3722 * in the file, or just any user data.
3725 bma
.userdata
= (aoff
== 0) ?
3726 XFS_ALLOC_INITIAL_USER_DATA
:
3730 * Fill in changeable bma fields.
3733 bma
.firstblock
= *firstblock
;
3736 bma
.wasdel
= wasdelay
;
3737 bma
.minlen
= minlen
;
3738 bma
.low
= flist
->xbf_low
;
3739 bma
.minleft
= minleft
;
3741 * Only want to do the alignment at the
3742 * eof if it is userdata and allocation length
3743 * is larger than a stripe unit.
3745 if (mp
->m_dalign
&& alen
>= mp
->m_dalign
&&
3746 userdata
&& whichfork
== XFS_DATA_FORK
) {
3747 if ((error
= xfs_bmap_isaeof(ip
, aoff
,
3748 whichfork
, &bma
.aeof
)))
3755 if ((error
= xfs_bmap_alloc(&bma
)))
3758 * Copy out result fields.
3761 if ((flist
->xbf_low
= bma
.low
))
3765 ASSERT(*firstblock
== NULLFSBLOCK
||
3766 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
3767 XFS_FSB_TO_AGNO(mp
, bma
.firstblock
) ||
3769 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
3770 XFS_FSB_TO_AGNO(mp
, bma
.firstblock
)));
3771 *firstblock
= bma
.firstblock
;
3773 cur
->bc_private
.b
.firstblock
=
3775 if (abno
== NULLFSBLOCK
)
3777 if ((ifp
->if_flags
& XFS_IFBROOT
) && !cur
) {
3778 cur
= xfs_btree_init_cursor(mp
,
3779 tp
, NULL
, 0, XFS_BTNUM_BMAP
,
3781 cur
->bc_private
.b
.firstblock
=
3783 cur
->bc_private
.b
.flist
= flist
;
3786 * Bump the number of extents we've allocated
3792 cur
->bc_private
.b
.flags
=
3793 wasdelay
? XFS_BTCUR_BPRV_WASDEL
: 0;
3794 got
.br_startoff
= aoff
;
3795 got
.br_startblock
= abno
;
3796 got
.br_blockcount
= alen
;
3797 got
.br_state
= XFS_EXT_NORM
; /* assume normal */
3799 * Determine state of extent, and the filesystem.
3800 * A wasdelay extent has been initialized, so
3801 * shouldn't be flagged as unwritten.
3803 if (wr
&& XFS_SB_VERSION_HASEXTFLGBIT(&mp
->m_sb
)) {
3804 if (!wasdelay
&& (flags
& XFS_BMAPI_PREALLOC
))
3805 got
.br_state
= XFS_EXT_UNWRITTEN
;
3807 error
= xfs_bmap_add_extent(ip
, lastx
, &cur
, &got
,
3808 firstblock
, flist
, &tmp_logflags
, whichfork
,
3810 logflags
|= tmp_logflags
;
3813 lastx
= ifp
->if_lastex
;
3814 ep
= &ifp
->if_u1
.if_extents
[lastx
];
3815 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
3816 xfs_bmbt_get_all(ep
, &got
);
3817 ASSERT(got
.br_startoff
<= aoff
);
3818 ASSERT(got
.br_startoff
+ got
.br_blockcount
>=
3822 ASSERT(ISNULLSTARTBLOCK(got
.br_startblock
));
3823 ASSERT(STARTBLOCKVAL(got
.br_startblock
) > 0);
3825 ASSERT(got
.br_state
== XFS_EXT_NORM
||
3826 got
.br_state
== XFS_EXT_UNWRITTEN
);
3829 * Fall down into the found allocated space case.
3831 } else if (inhole
) {
3833 * Reading in a hole.
3835 mval
->br_startoff
= bno
;
3836 mval
->br_startblock
= HOLESTARTBLOCK
;
3837 mval
->br_blockcount
=
3838 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
3839 mval
->br_state
= XFS_EXT_NORM
;
3840 bno
+= mval
->br_blockcount
;
3841 len
-= mval
->br_blockcount
;
3847 * Then deal with the allocated space we found.
3850 if (trim
&& (got
.br_startoff
+ got
.br_blockcount
> obno
)) {
3853 ASSERT((bno
>= obno
) || (n
== 0));
3855 mval
->br_startoff
= bno
;
3856 if (ISNULLSTARTBLOCK(got
.br_startblock
)) {
3857 ASSERT(!wr
|| delay
);
3858 mval
->br_startblock
= DELAYSTARTBLOCK
;
3860 mval
->br_startblock
=
3862 (bno
- got
.br_startoff
);
3864 * Return the minimum of what we got and what we
3865 * asked for for the length. We can use the len
3866 * variable here because it is modified below
3867 * and we could have been there before coming
3868 * here if the first part of the allocation
3869 * didn't overlap what was asked for.
3871 mval
->br_blockcount
=
3872 XFS_FILBLKS_MIN(end
- bno
, got
.br_blockcount
-
3873 (bno
- got
.br_startoff
));
3874 mval
->br_state
= got
.br_state
;
3875 ASSERT(mval
->br_blockcount
<= len
);
3878 if (ISNULLSTARTBLOCK(mval
->br_startblock
)) {
3879 ASSERT(!wr
|| delay
);
3880 mval
->br_startblock
= DELAYSTARTBLOCK
;
3885 * Check if writing previously allocated but
3886 * unwritten extents.
3888 if (wr
&& mval
->br_state
== XFS_EXT_UNWRITTEN
&&
3889 ((flags
& (XFS_BMAPI_PREALLOC
|XFS_BMAPI_DELAY
)) == 0)) {
3891 * Modify (by adding) the state flag, if writing.
3893 ASSERT(mval
->br_blockcount
<= len
);
3894 if ((ifp
->if_flags
& XFS_IFBROOT
) && !cur
) {
3895 cur
= xfs_btree_init_cursor(mp
,
3896 tp
, NULL
, 0, XFS_BTNUM_BMAP
,
3898 cur
->bc_private
.b
.firstblock
=
3900 cur
->bc_private
.b
.flist
= flist
;
3902 mval
->br_state
= XFS_EXT_NORM
;
3903 error
= xfs_bmap_add_extent(ip
, lastx
, &cur
, mval
,
3904 firstblock
, flist
, &tmp_logflags
, whichfork
,
3906 logflags
|= tmp_logflags
;
3909 lastx
= ifp
->if_lastex
;
3910 ep
= &ifp
->if_u1
.if_extents
[lastx
];
3911 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
3912 xfs_bmbt_get_all(ep
, &got
);
3914 * We may have combined previously unwritten
3915 * space with written space, so generate
3918 if (mval
->br_blockcount
< len
)
3923 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
3924 ASSERT(!trim
|| (mval
->br_blockcount
<= len
) ||
3925 (mval
->br_startoff
< obno
));
3926 bno
= mval
->br_startoff
+ mval
->br_blockcount
;
3928 if (n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
3929 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
3930 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
3931 ASSERT(mval
->br_state
== mval
[-1].br_state
);
3932 mval
[-1].br_blockcount
= mval
->br_blockcount
;
3933 mval
[-1].br_state
= mval
->br_state
;
3934 } else if (n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
3935 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
3936 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
3937 mval
->br_startblock
==
3938 mval
[-1].br_startblock
+ mval
[-1].br_blockcount
&&
3939 (stateless
|| mval
[-1].br_state
== mval
->br_state
)) {
3940 ASSERT(mval
->br_startoff
==
3941 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
3942 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3944 mval
->br_startblock
== DELAYSTARTBLOCK
&&
3945 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
3946 mval
->br_startoff
==
3947 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
3948 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
3949 mval
[-1].br_state
= mval
->br_state
;
3950 } else if (!((n
== 0) &&
3951 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
3957 * If we're done, stop now. Stop when we've allocated
3958 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
3959 * the transaction may get too big.
3961 if (bno
>= end
|| n
>= *nmap
|| nallocs
>= *nmap
)
3964 * Else go on to the next record.
3968 if (lastx
>= nextents
) {
3972 xfs_bmbt_get_all(ep
, &got
);
3974 ifp
->if_lastex
= lastx
;
3977 * Transform from btree to extents, give it cur.
3979 if (tp
&& XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
3980 XFS_IFORK_NEXTENTS(ip
, whichfork
) <= ifp
->if_ext_max
) {
3982 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
,
3983 &tmp_logflags
, whichfork
);
3984 logflags
|= tmp_logflags
;
3988 ASSERT(ifp
->if_ext_max
==
3989 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
3990 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
3991 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ifp
->if_ext_max
);
3996 * Log everything. Do this after conversion, there's no point in
3997 * logging the extent list if we've converted to btree format.
3999 if ((logflags
& XFS_ILOG_FEXT(whichfork
)) &&
4000 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4001 logflags
&= ~XFS_ILOG_FEXT(whichfork
);
4002 else if ((logflags
& XFS_ILOG_FBROOT(whichfork
)) &&
4003 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4004 logflags
&= ~XFS_ILOG_FBROOT(whichfork
);
4006 * Log whatever the flags say, even if error. Otherwise we might miss
4007 * detecting a case where the data is changed, there's an error,
4008 * and it's not logged so we don't shutdown when we should.
4012 xfs_trans_log_inode(tp
, ip
, logflags
);
4016 ASSERT(*firstblock
== NULLFSBLOCK
||
4017 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
4019 cur
->bc_private
.b
.firstblock
) ||
4021 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
4023 cur
->bc_private
.b
.firstblock
)));
4024 *firstblock
= cur
->bc_private
.b
.firstblock
;
4026 xfs_btree_del_cursor(cur
,
4027 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4030 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
4036 * Map file blocks to filesystem blocks, simple version.
4037 * One block (extent) only, read-only.
4038 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
4039 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
4040 * was set and all the others were clear.
4044 xfs_trans_t
*tp
, /* transaction pointer */
4045 xfs_inode_t
*ip
, /* incore inode */
4046 int whichfork
, /* data or attr fork */
4047 xfs_fsblock_t
*fsb
, /* output: mapped block */
4048 xfs_fileoff_t bno
) /* starting file offs. mapped */
4050 int eof
; /* we've hit the end of extent list */
4051 int error
; /* error return */
4052 xfs_bmbt_irec_t got
; /* current extent list record */
4053 xfs_ifork_t
*ifp
; /* inode fork pointer */
4054 xfs_extnum_t lastx
; /* last useful extent number */
4055 xfs_bmbt_irec_t prev
; /* previous extent list record */
4057 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4059 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
4060 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)) {
4061 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW
,
4063 return XFS_ERROR(EFSCORRUPTED
);
4065 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
4066 return XFS_ERROR(EIO
);
4067 XFS_STATS_INC(xfsstats
.xs_blk_mapr
);
4068 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
4069 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
4071 (void)xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
4074 * Reading past eof, act as though there's a hole
4077 if (eof
|| got
.br_startoff
> bno
) {
4081 ASSERT(!ISNULLSTARTBLOCK(got
.br_startblock
));
4082 ASSERT(bno
< got
.br_startoff
+ got
.br_blockcount
);
4083 *fsb
= got
.br_startblock
+ (bno
- got
.br_startoff
);
4084 ifp
->if_lastex
= lastx
;
4089 * Unmap (remove) blocks from a file.
4090 * If nexts is nonzero then the number of extents to remove is limited to
4091 * that value. If not all extents in the block range can be removed then
4096 xfs_trans_t
*tp
, /* transaction pointer */
4097 struct xfs_inode
*ip
, /* incore inode */
4098 xfs_fileoff_t bno
, /* starting offset to unmap */
4099 xfs_filblks_t len
, /* length to unmap in file */
4100 int flags
, /* misc flags */
4101 xfs_extnum_t nexts
, /* number of extents max */
4102 xfs_fsblock_t
*firstblock
, /* first allocated block
4103 controls a.g. for allocs */
4104 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
4105 int *done
) /* set if not done yet */
4107 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
4108 xfs_bmbt_irec_t del
; /* extent being deleted */
4109 int eof
; /* is deleting at eof */
4110 xfs_bmbt_rec_t
*ep
; /* extent list entry pointer */
4111 int error
; /* error return value */
4112 xfs_extnum_t extno
; /* extent number in list */
4113 xfs_bmbt_irec_t got
; /* current extent list entry */
4114 xfs_ifork_t
*ifp
; /* inode fork pointer */
4115 int isrt
; /* freeing in rt area */
4116 xfs_extnum_t lastx
; /* last extent index used */
4117 int logflags
; /* transaction logging flags */
4118 xfs_extlen_t mod
; /* rt extent offset */
4119 xfs_mount_t
*mp
; /* mount structure */
4120 xfs_extnum_t nextents
; /* size of extent list */
4121 xfs_bmbt_irec_t prev
; /* previous extent list entry */
4122 xfs_fileoff_t start
; /* first file offset deleted */
4123 int tmp_logflags
; /* partial logging flags */
4124 int wasdel
; /* was a delayed alloc extent */
4125 int whichfork
; /* data or attribute fork */
4126 int rsvd
; /* OK to allocate reserved blocks */
4129 xfs_bunmap_trace(ip
, bno
, len
, flags
, (inst_t
*)__return_address
);
4130 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4131 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4132 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4134 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4135 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
4136 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
4138 return XFS_ERROR(EFSCORRUPTED
);
4141 if (XFS_FORCED_SHUTDOWN(mp
))
4142 return XFS_ERROR(EIO
);
4143 rsvd
= (flags
& XFS_BMAPI_RSVBLOCKS
) != 0;
4146 ASSERT(ifp
->if_ext_max
==
4147 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
4148 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
4149 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
4151 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
4152 if (nextents
== 0) {
4156 XFS_STATS_INC(xfsstats
.xs_blk_unmap
);
4157 isrt
= (whichfork
== XFS_DATA_FORK
) &&
4158 (ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
);
4160 bno
= start
+ len
- 1;
4161 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
4164 * Check to see if the given block number is past the end of the
4165 * file, back up to the last block if so...
4168 ep
= &ifp
->if_u1
.if_extents
[--lastx
];
4169 xfs_bmbt_get_all(ep
, &got
);
4170 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
4173 if (ifp
->if_flags
& XFS_IFBROOT
) {
4174 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
4175 cur
= xfs_btree_init_cursor(mp
, tp
, NULL
, 0, XFS_BTNUM_BMAP
, ip
,
4177 cur
->bc_private
.b
.firstblock
= *firstblock
;
4178 cur
->bc_private
.b
.flist
= flist
;
4179 cur
->bc_private
.b
.flags
= 0;
4183 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
4184 (nexts
== 0 || extno
< nexts
)) {
4186 * Is the found extent after a hole in which bno lives?
4187 * Just back up to the previous extent, if so.
4189 if (got
.br_startoff
> bno
) {
4193 xfs_bmbt_get_all(ep
, &got
);
4196 * Is the last block of this extent before the range
4197 * we're supposed to delete? If so, we're done.
4199 bno
= XFS_FILEOFF_MIN(bno
,
4200 got
.br_startoff
+ got
.br_blockcount
- 1);
4204 * Then deal with the (possibly delayed) allocated space
4209 wasdel
= ISNULLSTARTBLOCK(del
.br_startblock
);
4210 if (got
.br_startoff
< start
) {
4211 del
.br_startoff
= start
;
4212 del
.br_blockcount
-= start
- got
.br_startoff
;
4214 del
.br_startblock
+= start
- got
.br_startoff
;
4216 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
4217 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
4218 sum
= del
.br_startblock
+ del
.br_blockcount
;
4220 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
4222 * Realtime extent not lined up at the end.
4223 * The extent could have been split into written
4224 * and unwritten pieces, or we could just be
4225 * unmapping part of it. But we can't really
4226 * get rid of part of a realtime extent.
4228 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
4229 !XFS_SB_VERSION_HASEXTFLGBIT(&mp
->m_sb
)) {
4231 * This piece is unwritten, or we're not
4232 * using unwritten extents. Skip over it.
4235 bno
-= mod
> del
.br_blockcount
?
4236 del
.br_blockcount
: mod
;
4237 if (bno
< got
.br_startoff
) {
4239 xfs_bmbt_get_all(--ep
, &got
);
4244 * It's written, turn it unwritten.
4245 * This is better than zeroing it.
4247 ASSERT(del
.br_state
== XFS_EXT_NORM
);
4248 ASSERT(xfs_trans_get_block_res(tp
) > 0);
4250 * If this spans a realtime extent boundary,
4251 * chop it back to the start of the one we end at.
4253 if (del
.br_blockcount
> mod
) {
4254 del
.br_startoff
+= del
.br_blockcount
- mod
;
4255 del
.br_startblock
+= del
.br_blockcount
- mod
;
4256 del
.br_blockcount
= mod
;
4258 del
.br_state
= XFS_EXT_UNWRITTEN
;
4259 error
= xfs_bmap_add_extent(ip
, lastx
, &cur
, &del
,
4260 firstblock
, flist
, &logflags
, XFS_DATA_FORK
, 0);
4265 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
4267 * Realtime extent is lined up at the end but not
4268 * at the front. We'll get rid of full extents if
4271 mod
= mp
->m_sb
.sb_rextsize
- mod
;
4272 if (del
.br_blockcount
> mod
) {
4273 del
.br_blockcount
-= mod
;
4274 del
.br_startoff
+= mod
;
4275 del
.br_startblock
+= mod
;
4276 } else if ((del
.br_startoff
== start
&&
4277 (del
.br_state
== XFS_EXT_UNWRITTEN
||
4278 xfs_trans_get_block_res(tp
) == 0)) ||
4279 !XFS_SB_VERSION_HASEXTFLGBIT(&mp
->m_sb
)) {
4281 * Can't make it unwritten. There isn't
4282 * a full extent here so just skip it.
4284 ASSERT(bno
>= del
.br_blockcount
);
4285 bno
-= del
.br_blockcount
;
4286 if (bno
< got
.br_startoff
) {
4288 xfs_bmbt_get_all(--ep
, &got
);
4291 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
4293 * This one is already unwritten.
4294 * It must have a written left neighbor.
4295 * Unwrite the killed part of that one and
4299 xfs_bmbt_get_all(ep
- 1, &prev
);
4300 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
4301 ASSERT(!ISNULLSTARTBLOCK(prev
.br_startblock
));
4302 ASSERT(del
.br_startblock
==
4303 prev
.br_startblock
+ prev
.br_blockcount
);
4304 if (prev
.br_startoff
< start
) {
4305 mod
= start
- prev
.br_startoff
;
4306 prev
.br_blockcount
-= mod
;
4307 prev
.br_startblock
+= mod
;
4308 prev
.br_startoff
= start
;
4310 prev
.br_state
= XFS_EXT_UNWRITTEN
;
4311 error
= xfs_bmap_add_extent(ip
, lastx
- 1, &cur
,
4312 &prev
, firstblock
, flist
, &logflags
,
4318 ASSERT(del
.br_state
== XFS_EXT_NORM
);
4319 del
.br_state
= XFS_EXT_UNWRITTEN
;
4320 error
= xfs_bmap_add_extent(ip
, lastx
, &cur
,
4321 &del
, firstblock
, flist
, &logflags
,
4329 ASSERT(STARTBLOCKVAL(del
.br_startblock
) > 0);
4330 xfs_mod_incore_sb(mp
, XFS_SBS_FDBLOCKS
,
4331 (int)del
.br_blockcount
, rsvd
);
4332 /* Unreserve our quota space */
4333 XFS_TRANS_RESERVE_QUOTA_NBLKS(
4334 mp
, NULL
, ip
, -((long)del
.br_blockcount
), 0,
4335 isrt
? XFS_QMOPT_RES_RTBLKS
:
4336 XFS_QMOPT_RES_REGBLKS
);
4337 ip
->i_delayed_blks
-= del
.br_blockcount
;
4339 cur
->bc_private
.b
.flags
|=
4340 XFS_BTCUR_BPRV_WASDEL
;
4342 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
4344 * If it's the case where the directory code is running
4345 * with no block reservation, and the deleted block is in
4346 * the middle of its extent, and the resulting insert
4347 * of an extent would cause transformation to btree format,
4348 * then reject it. The calling code will then swap
4349 * blocks around instead.
4350 * We have to do this now, rather than waiting for the
4351 * conversion to btree format, since the transaction
4354 if (!wasdel
&& xfs_trans_get_block_res(tp
) == 0 &&
4355 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
4356 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= ifp
->if_ext_max
&&
4357 del
.br_startoff
> got
.br_startoff
&&
4358 del
.br_startoff
+ del
.br_blockcount
<
4359 got
.br_startoff
+ got
.br_blockcount
) {
4360 error
= XFS_ERROR(ENOSPC
);
4363 error
= xfs_bmap_del_extent(ip
, tp
, lastx
, flist
, cur
, &del
,
4364 &tmp_logflags
, whichfork
, rsvd
);
4365 logflags
|= tmp_logflags
;
4368 bno
= del
.br_startoff
- 1;
4370 lastx
= ifp
->if_lastex
;
4372 * If not done go on to the next (previous) record.
4373 * Reset ep in case the extents array was re-alloced.
4375 ep
= &ifp
->if_u1
.if_extents
[lastx
];
4376 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
4377 if (lastx
>= XFS_IFORK_NEXTENTS(ip
, whichfork
) ||
4378 xfs_bmbt_get_startoff(ep
) > bno
) {
4383 xfs_bmbt_get_all(ep
, &got
);
4387 ifp
->if_lastex
= lastx
;
4388 *done
= bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0;
4389 ASSERT(ifp
->if_ext_max
==
4390 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
4392 * Convert to a btree if necessary.
4394 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
4395 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ifp
->if_ext_max
) {
4396 ASSERT(cur
== NULL
);
4397 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
,
4398 &cur
, 0, &tmp_logflags
, whichfork
);
4399 logflags
|= tmp_logflags
;
4404 * transform from btree to extents, give it cur
4406 else if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
4407 XFS_IFORK_NEXTENTS(ip
, whichfork
) <= ifp
->if_ext_max
) {
4408 ASSERT(cur
!= NULL
);
4409 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
4411 logflags
|= tmp_logflags
;
4416 * transform from extents to local?
4418 ASSERT(ifp
->if_ext_max
==
4419 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
4423 * Log everything. Do this after conversion, there's no point in
4424 * logging the extent list if we've converted to btree format.
4426 if ((logflags
& XFS_ILOG_FEXT(whichfork
)) &&
4427 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4428 logflags
&= ~XFS_ILOG_FEXT(whichfork
);
4429 else if ((logflags
& XFS_ILOG_FBROOT(whichfork
)) &&
4430 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
4431 logflags
&= ~XFS_ILOG_FBROOT(whichfork
);
4433 * Log inode even in the error case, if the transaction
4434 * is dirty we'll need to shut down the filesystem.
4437 xfs_trans_log_inode(tp
, ip
, logflags
);
4440 *firstblock
= cur
->bc_private
.b
.firstblock
;
4441 cur
->bc_private
.b
.allocated
= 0;
4443 xfs_btree_del_cursor(cur
,
4444 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
4450 * Check the last inode extent to determine whether this allocation will result
4451 * in blocks being allocated at the end of the file. When we allocate new data
4452 * blocks at the end of the file which do not start at the previous data block,
4453 * we will try to align the new blocks at stripe unit boundaries.
4457 xfs_inode_t
*ip
, /* incore inode pointer */
4458 xfs_fileoff_t off
, /* file offset in fsblocks */
4459 int whichfork
, /* data or attribute fork */
4460 char *aeof
) /* return value */
4462 int error
; /* error return value */
4463 xfs_ifork_t
*ifp
; /* inode fork pointer */
4464 xfs_bmbt_rec_t
*lastrec
; /* extent list entry pointer */
4465 xfs_extnum_t nextents
; /* size of extent list */
4466 xfs_bmbt_irec_t s
; /* expanded extent list entry */
4468 ASSERT(whichfork
== XFS_DATA_FORK
);
4469 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4470 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
4471 (error
= xfs_iread_extents(NULL
, ip
, whichfork
)))
4473 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
4474 if (nextents
== 0) {
4479 * Go to the last extent
4481 lastrec
= &ifp
->if_u1
.if_extents
[nextents
- 1];
4482 xfs_bmbt_get_all(lastrec
, &s
);
4484 * Check we are allocating in the last extent (for delayed allocations)
4485 * or past the last extent for non-delayed allocations.
4487 *aeof
= (off
>= s
.br_startoff
&&
4488 off
< s
.br_startoff
+ s
.br_blockcount
&&
4489 ISNULLSTARTBLOCK(s
.br_startblock
)) ||
4490 off
>= s
.br_startoff
+ s
.br_blockcount
;