2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_alloc_btree.h"
31 #include "xfs_ialloc_btree.h"
32 #include "xfs_dir2_sf.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_dinode.h"
35 #include "xfs_inode.h"
36 #include "xfs_btree.h"
37 #include "xfs_dmapi.h"
38 #include "xfs_mount.h"
39 #include "xfs_ialloc.h"
40 #include "xfs_itable.h"
41 #include "xfs_dir2_data.h"
42 #include "xfs_dir2_leaf.h"
43 #include "xfs_dir2_block.h"
44 #include "xfs_inode_item.h"
45 #include "xfs_extfree_item.h"
46 #include "xfs_alloc.h"
48 #include "xfs_rtalloc.h"
49 #include "xfs_error.h"
50 #include "xfs_attr_leaf.h"
52 #include "xfs_quota.h"
53 #include "xfs_trans_space.h"
54 #include "xfs_buf_item.h"
59 xfs_bmap_check_leaf_extents(xfs_btree_cur_t
*cur
, xfs_inode_t
*ip
, int whichfork
);
62 kmem_zone_t
*xfs_bmap_free_item_zone
;
65 * Prototypes for internal bmap routines.
70 * Called from xfs_bmap_add_attrfork to handle extents format files.
72 STATIC
int /* error */
73 xfs_bmap_add_attrfork_extents(
74 xfs_trans_t
*tp
, /* transaction pointer */
75 xfs_inode_t
*ip
, /* incore inode pointer */
76 xfs_fsblock_t
*firstblock
, /* first block allocated */
77 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
78 int *flags
); /* inode logging flags */
81 * Called from xfs_bmap_add_attrfork to handle local format files.
83 STATIC
int /* error */
84 xfs_bmap_add_attrfork_local(
85 xfs_trans_t
*tp
, /* transaction pointer */
86 xfs_inode_t
*ip
, /* incore inode pointer */
87 xfs_fsblock_t
*firstblock
, /* first block allocated */
88 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
89 int *flags
); /* inode logging flags */
92 * Called by xfs_bmapi to update file extent records and the btree
93 * after allocating space (or doing a delayed allocation).
95 STATIC
int /* error */
97 xfs_inode_t
*ip
, /* incore inode pointer */
98 xfs_extnum_t idx
, /* extent number to update/insert */
99 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
100 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
101 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
102 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
103 int *logflagsp
, /* inode logging flags */
104 xfs_extdelta_t
*delta
, /* Change made to incore extents */
105 int whichfork
, /* data or attr fork */
106 int rsvd
); /* OK to allocate reserved blocks */
109 * Called by xfs_bmap_add_extent to handle cases converting a delayed
110 * allocation to a real allocation.
112 STATIC
int /* error */
113 xfs_bmap_add_extent_delay_real(
114 xfs_inode_t
*ip
, /* incore inode pointer */
115 xfs_extnum_t idx
, /* extent number to update/insert */
116 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
117 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
118 xfs_filblks_t
*dnew
, /* new delayed-alloc indirect blocks */
119 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
120 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
121 int *logflagsp
, /* inode logging flags */
122 xfs_extdelta_t
*delta
, /* Change made to incore extents */
123 int rsvd
); /* OK to allocate reserved blocks */
126 * Called by xfs_bmap_add_extent to handle cases converting a hole
127 * to a delayed allocation.
129 STATIC
int /* error */
130 xfs_bmap_add_extent_hole_delay(
131 xfs_inode_t
*ip
, /* incore inode pointer */
132 xfs_extnum_t idx
, /* extent number to update/insert */
133 xfs_btree_cur_t
*cur
, /* if null, not a btree */
134 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
135 int *logflagsp
,/* inode logging flags */
136 xfs_extdelta_t
*delta
, /* Change made to incore extents */
137 int rsvd
); /* OK to allocate reserved blocks */
140 * Called by xfs_bmap_add_extent to handle cases converting a hole
141 * to a real allocation.
143 STATIC
int /* error */
144 xfs_bmap_add_extent_hole_real(
145 xfs_inode_t
*ip
, /* incore inode pointer */
146 xfs_extnum_t idx
, /* extent number to update/insert */
147 xfs_btree_cur_t
*cur
, /* if null, not a btree */
148 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
149 int *logflagsp
, /* inode logging flags */
150 xfs_extdelta_t
*delta
, /* Change made to incore extents */
151 int whichfork
); /* data or attr fork */
154 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
155 * allocation to a real allocation or vice versa.
157 STATIC
int /* error */
158 xfs_bmap_add_extent_unwritten_real(
159 xfs_inode_t
*ip
, /* incore inode pointer */
160 xfs_extnum_t idx
, /* extent number to update/insert */
161 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
162 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
163 int *logflagsp
, /* inode logging flags */
164 xfs_extdelta_t
*delta
); /* Change made to incore extents */
167 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
168 * It figures out where to ask the underlying allocator to put the new extent.
170 STATIC
int /* error */
172 xfs_bmalloca_t
*ap
); /* bmap alloc argument struct */
175 * Transform a btree format file with only one leaf node, where the
176 * extents list will fit in the inode, into an extents format file.
177 * Since the file extents are already in-core, all we have to do is
178 * give up the space for the btree root and pitch the leaf block.
180 STATIC
int /* error */
181 xfs_bmap_btree_to_extents(
182 xfs_trans_t
*tp
, /* transaction pointer */
183 xfs_inode_t
*ip
, /* incore inode pointer */
184 xfs_btree_cur_t
*cur
, /* btree cursor */
185 int *logflagsp
, /* inode logging flags */
186 int whichfork
); /* data or attr fork */
189 * Called by xfs_bmapi to update file extent records and the btree
190 * after removing space (or undoing a delayed allocation).
192 STATIC
int /* error */
194 xfs_inode_t
*ip
, /* incore inode pointer */
195 xfs_trans_t
*tp
, /* current trans pointer */
196 xfs_extnum_t idx
, /* extent number to update/insert */
197 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
198 xfs_btree_cur_t
*cur
, /* if null, not a btree */
199 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
200 int *logflagsp
,/* inode logging flags */
201 xfs_extdelta_t
*delta
, /* Change made to incore extents */
202 int whichfork
, /* data or attr fork */
203 int rsvd
); /* OK to allocate reserved blocks */
206 * Remove the entry "free" from the free item list. Prev points to the
207 * previous entry, unless "free" is the head of the list.
211 xfs_bmap_free_t
*flist
, /* free item list header */
212 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
213 xfs_bmap_free_item_t
*free
); /* list item to be freed */
216 * Convert an extents-format file into a btree-format file.
217 * The new file will have a root block (in the inode) and a single child block.
219 STATIC
int /* error */
220 xfs_bmap_extents_to_btree(
221 xfs_trans_t
*tp
, /* transaction pointer */
222 xfs_inode_t
*ip
, /* incore inode pointer */
223 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
224 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
225 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
226 int wasdel
, /* converting a delayed alloc */
227 int *logflagsp
, /* inode logging flags */
228 int whichfork
); /* data or attr fork */
231 * Convert a local file to an extents file.
232 * This code is sort of bogus, since the file data needs to get
233 * logged so it won't be lost. The bmap-level manipulations are ok, though.
235 STATIC
int /* error */
236 xfs_bmap_local_to_extents(
237 xfs_trans_t
*tp
, /* transaction pointer */
238 xfs_inode_t
*ip
, /* incore inode pointer */
239 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
240 xfs_extlen_t total
, /* total blocks needed by transaction */
241 int *logflagsp
, /* inode logging flags */
242 int whichfork
); /* data or attr fork */
245 * Search the extents list for the inode, for the extent containing bno.
246 * If bno lies in a hole, point to the next entry. If bno lies past eof,
247 * *eofp will be set, and *prevp will contain the last entry (null if none).
248 * Else, *lastxp will be set to the index of the found
249 * entry; *gotp will contain the entry.
251 STATIC xfs_bmbt_rec_t
* /* pointer to found extent entry */
252 xfs_bmap_search_extents(
253 xfs_inode_t
*ip
, /* incore inode pointer */
254 xfs_fileoff_t bno
, /* block number searched for */
255 int whichfork
, /* data or attr fork */
256 int *eofp
, /* out: end of file found */
257 xfs_extnum_t
*lastxp
, /* out: last extent index */
258 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
259 xfs_bmbt_irec_t
*prevp
); /* out: previous extent entry found */
262 * Check the last inode extent to determine whether this allocation will result
263 * in blocks being allocated at the end of the file. When we allocate new data
264 * blocks at the end of the file which do not start at the previous data block,
265 * we will try to align the new blocks at stripe unit boundaries.
267 STATIC
int /* error */
269 xfs_inode_t
*ip
, /* incore inode pointer */
270 xfs_fileoff_t off
, /* file offset in fsblocks */
271 int whichfork
, /* data or attribute fork */
272 char *aeof
); /* return value */
274 #ifdef XFS_BMAP_TRACE
276 * Add a bmap trace buffer entry. Base routine for the others.
279 xfs_bmap_trace_addentry(
280 int opcode
, /* operation */
281 char *fname
, /* function name */
282 char *desc
, /* operation description */
283 xfs_inode_t
*ip
, /* incore inode pointer */
284 xfs_extnum_t idx
, /* index of entry(ies) */
285 xfs_extnum_t cnt
, /* count of entries, 1 or 2 */
286 xfs_bmbt_rec_t
*r1
, /* first record */
287 xfs_bmbt_rec_t
*r2
, /* second record or null */
288 int whichfork
); /* data or attr fork */
291 * Add bmap trace entry prior to a call to xfs_iext_remove.
294 xfs_bmap_trace_delete(
295 char *fname
, /* function name */
296 char *desc
, /* operation description */
297 xfs_inode_t
*ip
, /* incore inode pointer */
298 xfs_extnum_t idx
, /* index of entry(entries) deleted */
299 xfs_extnum_t cnt
, /* count of entries deleted, 1 or 2 */
300 int whichfork
); /* data or attr fork */
303 * Add bmap trace entry prior to a call to xfs_iext_insert, or
304 * reading in the extents list from the disk (in the btree).
307 xfs_bmap_trace_insert(
308 char *fname
, /* function name */
309 char *desc
, /* operation description */
310 xfs_inode_t
*ip
, /* incore inode pointer */
311 xfs_extnum_t idx
, /* index of entry(entries) inserted */
312 xfs_extnum_t cnt
, /* count of entries inserted, 1 or 2 */
313 xfs_bmbt_irec_t
*r1
, /* inserted record 1 */
314 xfs_bmbt_irec_t
*r2
, /* inserted record 2 or null */
315 int whichfork
); /* data or attr fork */
318 * Add bmap trace entry after updating an extent record in place.
321 xfs_bmap_trace_post_update(
322 char *fname
, /* function name */
323 char *desc
, /* operation description */
324 xfs_inode_t
*ip
, /* incore inode pointer */
325 xfs_extnum_t idx
, /* index of entry updated */
326 int whichfork
); /* data or attr fork */
329 * Add bmap trace entry prior to updating an extent record in place.
332 xfs_bmap_trace_pre_update(
333 char *fname
, /* function name */
334 char *desc
, /* operation description */
335 xfs_inode_t
*ip
, /* incore inode pointer */
336 xfs_extnum_t idx
, /* index of entry to be updated */
337 int whichfork
); /* data or attr fork */
340 #define xfs_bmap_trace_delete(f,d,ip,i,c,w)
341 #define xfs_bmap_trace_insert(f,d,ip,i,c,r1,r2,w)
342 #define xfs_bmap_trace_post_update(f,d,ip,i,w)
343 #define xfs_bmap_trace_pre_update(f,d,ip,i,w)
344 #endif /* XFS_BMAP_TRACE */
347 * Compute the worst-case number of indirect blocks that will be used
348 * for ip's delayed extent of length "len".
351 xfs_bmap_worst_indlen(
352 xfs_inode_t
*ip
, /* incore inode pointer */
353 xfs_filblks_t len
); /* delayed extent length */
357 * Perform various validation checks on the values being returned
361 xfs_bmap_validate_ret(
365 xfs_bmbt_irec_t
*mval
,
369 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
372 #if defined(XFS_RW_TRACE)
381 #define xfs_bunmap_trace(ip, bno, len, flags, ra)
382 #endif /* XFS_RW_TRACE */
389 xfs_fsblock_t blockno
,
394 xfs_bmap_count_leaves(
401 xfs_bmap_disk_count_leaves(
404 xfs_bmbt_block_t
*block
,
409 * Bmap internal routines.
413 * Called from xfs_bmap_add_attrfork to handle btree format files.
415 STATIC
int /* error */
416 xfs_bmap_add_attrfork_btree(
417 xfs_trans_t
*tp
, /* transaction pointer */
418 xfs_inode_t
*ip
, /* incore inode pointer */
419 xfs_fsblock_t
*firstblock
, /* first block allocated */
420 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
421 int *flags
) /* inode logging flags */
423 xfs_btree_cur_t
*cur
; /* btree cursor */
424 int error
; /* error return value */
425 xfs_mount_t
*mp
; /* file system mount struct */
426 int stat
; /* newroot status */
429 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
430 *flags
|= XFS_ILOG_DBROOT
;
432 cur
= xfs_btree_init_cursor(mp
, tp
, NULL
, 0, XFS_BTNUM_BMAP
, ip
,
434 cur
->bc_private
.b
.flist
= flist
;
435 cur
->bc_private
.b
.firstblock
= *firstblock
;
436 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
438 ASSERT(stat
== 1); /* must be at least one entry */
439 if ((error
= xfs_bmbt_newroot(cur
, flags
, &stat
)))
442 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
443 return XFS_ERROR(ENOSPC
);
445 *firstblock
= cur
->bc_private
.b
.firstblock
;
446 cur
->bc_private
.b
.allocated
= 0;
447 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
451 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
456 * Called from xfs_bmap_add_attrfork to handle extents format files.
458 STATIC
int /* error */
459 xfs_bmap_add_attrfork_extents(
460 xfs_trans_t
*tp
, /* transaction pointer */
461 xfs_inode_t
*ip
, /* incore inode pointer */
462 xfs_fsblock_t
*firstblock
, /* first block allocated */
463 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
464 int *flags
) /* inode logging flags */
466 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
467 int error
; /* error return value */
469 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
472 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
, &cur
, 0,
473 flags
, XFS_DATA_FORK
);
475 cur
->bc_private
.b
.allocated
= 0;
476 xfs_btree_del_cursor(cur
,
477 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
483 * Called from xfs_bmap_add_attrfork to handle local format files.
485 STATIC
int /* error */
486 xfs_bmap_add_attrfork_local(
487 xfs_trans_t
*tp
, /* transaction pointer */
488 xfs_inode_t
*ip
, /* incore inode pointer */
489 xfs_fsblock_t
*firstblock
, /* first block allocated */
490 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
491 int *flags
) /* inode logging flags */
493 xfs_da_args_t dargs
; /* args for dir/attr code */
494 int error
; /* error return value */
495 xfs_mount_t
*mp
; /* mount structure pointer */
497 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
499 if ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
) {
501 memset(&dargs
, 0, sizeof(dargs
));
503 dargs
.firstblock
= firstblock
;
505 dargs
.total
= mp
->m_dirblkfsbs
;
506 dargs
.whichfork
= XFS_DATA_FORK
;
508 error
= xfs_dir2_sf_to_block(&dargs
);
510 error
= xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1, flags
,
516 * Called by xfs_bmapi to update file extent records and the btree
517 * after allocating space (or doing a delayed allocation).
519 STATIC
int /* error */
521 xfs_inode_t
*ip
, /* incore inode pointer */
522 xfs_extnum_t idx
, /* extent number to update/insert */
523 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
524 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
525 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
526 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
527 int *logflagsp
, /* inode logging flags */
528 xfs_extdelta_t
*delta
, /* Change made to incore extents */
529 int whichfork
, /* data or attr fork */
530 int rsvd
) /* OK to use reserved data blocks */
532 xfs_btree_cur_t
*cur
; /* btree cursor or null */
533 xfs_filblks_t da_new
; /* new count del alloc blocks used */
534 xfs_filblks_t da_old
; /* old count del alloc blocks used */
535 int error
; /* error return value */
536 #ifdef XFS_BMAP_TRACE
537 static char fname
[] = "xfs_bmap_add_extent";
539 xfs_ifork_t
*ifp
; /* inode fork ptr */
540 int logflags
; /* returned value */
541 xfs_extnum_t nextents
; /* number of extents in file now */
543 XFS_STATS_INC(xs_add_exlist
);
545 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
546 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
547 ASSERT(idx
<= nextents
);
551 * This is the first extent added to a new/empty file.
552 * Special case this one, so other routines get to assume there are
553 * already extents in the list.
556 xfs_bmap_trace_insert(fname
, "insert empty", ip
, 0, 1, new,
558 xfs_iext_insert(ifp
, 0, 1, new);
561 if (!ISNULLSTARTBLOCK(new->br_startblock
)) {
562 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
563 logflags
= XFS_ILOG_CORE
| XFS_ILOG_FEXT(whichfork
);
566 /* DELTA: single new extent */
568 if (delta
->xed_startoff
> new->br_startoff
)
569 delta
->xed_startoff
= new->br_startoff
;
570 if (delta
->xed_blockcount
<
571 new->br_startoff
+ new->br_blockcount
)
572 delta
->xed_blockcount
= new->br_startoff
+
577 * Any kind of new delayed allocation goes here.
579 else if (ISNULLSTARTBLOCK(new->br_startblock
)) {
581 ASSERT((cur
->bc_private
.b
.flags
&
582 XFS_BTCUR_BPRV_WASDEL
) == 0);
583 if ((error
= xfs_bmap_add_extent_hole_delay(ip
, idx
, cur
, new,
584 &logflags
, delta
, rsvd
)))
588 * Real allocation off the end of the file.
590 else if (idx
== nextents
) {
592 ASSERT((cur
->bc_private
.b
.flags
&
593 XFS_BTCUR_BPRV_WASDEL
) == 0);
594 if ((error
= xfs_bmap_add_extent_hole_real(ip
, idx
, cur
, new,
595 &logflags
, delta
, whichfork
)))
598 xfs_bmbt_irec_t prev
; /* old extent at offset idx */
601 * Get the record referred to by idx.
603 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, idx
), &prev
);
605 * If it's a real allocation record, and the new allocation ends
606 * after the start of the referred to record, then we're filling
607 * in a delayed or unwritten allocation with a real one, or
608 * converting real back to unwritten.
610 if (!ISNULLSTARTBLOCK(new->br_startblock
) &&
611 new->br_startoff
+ new->br_blockcount
> prev
.br_startoff
) {
612 if (prev
.br_state
!= XFS_EXT_UNWRITTEN
&&
613 ISNULLSTARTBLOCK(prev
.br_startblock
)) {
614 da_old
= STARTBLOCKVAL(prev
.br_startblock
);
616 ASSERT(cur
->bc_private
.b
.flags
&
617 XFS_BTCUR_BPRV_WASDEL
);
618 if ((error
= xfs_bmap_add_extent_delay_real(ip
,
619 idx
, &cur
, new, &da_new
, first
, flist
,
620 &logflags
, delta
, rsvd
)))
622 } else if (new->br_state
== XFS_EXT_NORM
) {
623 ASSERT(new->br_state
== XFS_EXT_NORM
);
624 if ((error
= xfs_bmap_add_extent_unwritten_real(
625 ip
, idx
, &cur
, new, &logflags
, delta
)))
628 ASSERT(new->br_state
== XFS_EXT_UNWRITTEN
);
629 if ((error
= xfs_bmap_add_extent_unwritten_real(
630 ip
, idx
, &cur
, new, &logflags
, delta
)))
633 ASSERT(*curp
== cur
|| *curp
== NULL
);
636 * Otherwise we're filling in a hole with an allocation.
640 ASSERT((cur
->bc_private
.b
.flags
&
641 XFS_BTCUR_BPRV_WASDEL
) == 0);
642 if ((error
= xfs_bmap_add_extent_hole_real(ip
, idx
, cur
,
643 new, &logflags
, delta
, whichfork
)))
648 ASSERT(*curp
== cur
|| *curp
== NULL
);
650 * Convert to a btree if necessary.
652 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
653 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ifp
->if_ext_max
) {
654 int tmp_logflags
; /* partial log flag return val */
657 error
= xfs_bmap_extents_to_btree(ip
->i_transp
, ip
, first
,
658 flist
, &cur
, da_old
> 0, &tmp_logflags
, whichfork
);
659 logflags
|= tmp_logflags
;
664 * Adjust for changes in reserved delayed indirect blocks.
665 * Nothing to do for disk quotas here.
667 if (da_old
|| da_new
) {
672 nblks
+= cur
->bc_private
.b
.allocated
;
673 ASSERT(nblks
<= da_old
);
675 xfs_mod_incore_sb(ip
->i_mount
, XFS_SBS_FDBLOCKS
,
676 (int64_t)(da_old
- nblks
), rsvd
);
679 * Clear out the allocated field, done with it now in any case.
682 cur
->bc_private
.b
.allocated
= 0;
688 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
690 *logflagsp
= logflags
;
695 * Called by xfs_bmap_add_extent to handle cases converting a delayed
696 * allocation to a real allocation.
698 STATIC
int /* error */
699 xfs_bmap_add_extent_delay_real(
700 xfs_inode_t
*ip
, /* incore inode pointer */
701 xfs_extnum_t idx
, /* extent number to update/insert */
702 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
703 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
704 xfs_filblks_t
*dnew
, /* new delayed-alloc indirect blocks */
705 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
706 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
707 int *logflagsp
, /* inode logging flags */
708 xfs_extdelta_t
*delta
, /* Change made to incore extents */
709 int rsvd
) /* OK to use reserved data block allocation */
711 xfs_btree_cur_t
*cur
; /* btree cursor */
712 int diff
; /* temp value */
713 xfs_bmbt_rec_t
*ep
; /* extent entry for idx */
714 int error
; /* error return value */
715 #ifdef XFS_BMAP_TRACE
716 static char fname
[] = "xfs_bmap_add_extent_delay_real";
718 int i
; /* temp state */
719 xfs_ifork_t
*ifp
; /* inode fork pointer */
720 xfs_fileoff_t new_endoff
; /* end offset of new entry */
721 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
722 /* left is 0, right is 1, prev is 2 */
723 int rval
=0; /* return value (logging flags) */
724 int state
= 0;/* state bits, accessed thru macros */
725 xfs_filblks_t temp
=0; /* value for dnew calculations */
726 xfs_filblks_t temp2
=0;/* value for dnew calculations */
727 int tmp_rval
; /* partial logging flags */
728 enum { /* bit number definitions for state */
729 LEFT_CONTIG
, RIGHT_CONTIG
,
730 LEFT_FILLING
, RIGHT_FILLING
,
731 LEFT_DELAY
, RIGHT_DELAY
,
732 LEFT_VALID
, RIGHT_VALID
738 #define MASK(b) (1 << (b))
739 #define MASK2(a,b) (MASK(a) | MASK(b))
740 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
741 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
742 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
743 #define STATE_TEST(b) (state & MASK(b))
744 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
745 ((state &= ~MASK(b)), 0))
746 #define SWITCH_STATE \
747 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
750 * Set up a bunch of variables to make the tests simpler.
753 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
754 ep
= xfs_iext_get_ext(ifp
, idx
);
755 xfs_bmbt_get_all(ep
, &PREV
);
756 new_endoff
= new->br_startoff
+ new->br_blockcount
;
757 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
758 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
760 * Set flags determining what part of the previous delayed allocation
761 * extent is being replaced by a real allocation.
763 STATE_SET(LEFT_FILLING
, PREV
.br_startoff
== new->br_startoff
);
764 STATE_SET(RIGHT_FILLING
,
765 PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
);
767 * Check and set flags if this segment has a left neighbor.
768 * Don't set contiguous if the combined extent would be too large.
770 if (STATE_SET_TEST(LEFT_VALID
, idx
> 0)) {
771 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, idx
- 1), &LEFT
);
772 STATE_SET(LEFT_DELAY
, ISNULLSTARTBLOCK(LEFT
.br_startblock
));
774 STATE_SET(LEFT_CONTIG
,
775 STATE_TEST(LEFT_VALID
) && !STATE_TEST(LEFT_DELAY
) &&
776 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
777 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
778 LEFT
.br_state
== new->br_state
&&
779 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
);
781 * Check and set flags if this segment has a right neighbor.
782 * Don't set contiguous if the combined extent would be too large.
783 * Also check for all-three-contiguous being too large.
785 if (STATE_SET_TEST(RIGHT_VALID
,
787 ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1)) {
788 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, idx
+ 1), &RIGHT
);
789 STATE_SET(RIGHT_DELAY
, ISNULLSTARTBLOCK(RIGHT
.br_startblock
));
791 STATE_SET(RIGHT_CONTIG
,
792 STATE_TEST(RIGHT_VALID
) && !STATE_TEST(RIGHT_DELAY
) &&
793 new_endoff
== RIGHT
.br_startoff
&&
794 new->br_startblock
+ new->br_blockcount
==
795 RIGHT
.br_startblock
&&
796 new->br_state
== RIGHT
.br_state
&&
797 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
798 ((state
& MASK3(LEFT_CONTIG
, LEFT_FILLING
, RIGHT_FILLING
)) !=
799 MASK3(LEFT_CONTIG
, LEFT_FILLING
, RIGHT_FILLING
) ||
800 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
804 * Switch out based on the FILLING and CONTIG state bits.
806 switch (SWITCH_STATE
) {
808 case MASK4(LEFT_FILLING
, RIGHT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
810 * Filling in all of a previously delayed allocation extent.
811 * The left and right neighbors are both contiguous with new.
813 xfs_bmap_trace_pre_update(fname
, "LF|RF|LC|RC", ip
, idx
- 1,
815 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, idx
- 1),
816 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
817 RIGHT
.br_blockcount
);
818 xfs_bmap_trace_post_update(fname
, "LF|RF|LC|RC", ip
, idx
- 1,
820 xfs_bmap_trace_delete(fname
, "LF|RF|LC|RC", ip
, idx
, 2,
822 xfs_iext_remove(ifp
, idx
, 2);
823 ip
->i_df
.if_lastex
= idx
- 1;
824 ip
->i_d
.di_nextents
--;
826 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
828 rval
= XFS_ILOG_CORE
;
829 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
831 RIGHT
.br_blockcount
, &i
)))
834 if ((error
= xfs_bmbt_delete(cur
, &i
)))
837 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
840 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
844 RIGHT
.br_blockcount
, LEFT
.br_state
)))
848 /* DELTA: Three in-core extents are replaced by one. */
849 temp
= LEFT
.br_startoff
;
850 temp2
= LEFT
.br_blockcount
+
855 case MASK3(LEFT_FILLING
, RIGHT_FILLING
, LEFT_CONTIG
):
857 * Filling in all of a previously delayed allocation extent.
858 * The left neighbor is contiguous, the right is not.
860 xfs_bmap_trace_pre_update(fname
, "LF|RF|LC", ip
, idx
- 1,
862 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, idx
- 1),
863 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
864 xfs_bmap_trace_post_update(fname
, "LF|RF|LC", ip
, idx
- 1,
866 ip
->i_df
.if_lastex
= idx
- 1;
867 xfs_bmap_trace_delete(fname
, "LF|RF|LC", ip
, idx
, 1,
869 xfs_iext_remove(ifp
, idx
, 1);
871 rval
= XFS_ILOG_DEXT
;
874 if ((error
= xfs_bmbt_lookup_eq(cur
, LEFT
.br_startoff
,
875 LEFT
.br_startblock
, LEFT
.br_blockcount
,
879 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
882 PREV
.br_blockcount
, LEFT
.br_state
)))
886 /* DELTA: Two in-core extents are replaced by one. */
887 temp
= LEFT
.br_startoff
;
888 temp2
= LEFT
.br_blockcount
+
892 case MASK3(LEFT_FILLING
, RIGHT_FILLING
, RIGHT_CONTIG
):
894 * Filling in all of a previously delayed allocation extent.
895 * The right neighbor is contiguous, the left is not.
897 xfs_bmap_trace_pre_update(fname
, "LF|RF|RC", ip
, idx
,
899 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
900 xfs_bmbt_set_blockcount(ep
,
901 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
902 xfs_bmap_trace_post_update(fname
, "LF|RF|RC", ip
, idx
,
904 ip
->i_df
.if_lastex
= idx
;
905 xfs_bmap_trace_delete(fname
, "LF|RF|RC", ip
, idx
+ 1, 1,
907 xfs_iext_remove(ifp
, idx
+ 1, 1);
909 rval
= XFS_ILOG_DEXT
;
912 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
914 RIGHT
.br_blockcount
, &i
)))
917 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
920 RIGHT
.br_blockcount
, PREV
.br_state
)))
924 /* DELTA: Two in-core extents are replaced by one. */
925 temp
= PREV
.br_startoff
;
926 temp2
= PREV
.br_blockcount
+
930 case MASK2(LEFT_FILLING
, RIGHT_FILLING
):
932 * Filling in all of a previously delayed allocation extent.
933 * Neither the left nor right neighbors are contiguous with
936 xfs_bmap_trace_pre_update(fname
, "LF|RF", ip
, idx
,
938 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
939 xfs_bmap_trace_post_update(fname
, "LF|RF", ip
, idx
,
941 ip
->i_df
.if_lastex
= idx
;
942 ip
->i_d
.di_nextents
++;
944 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
946 rval
= XFS_ILOG_CORE
;
947 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
948 new->br_startblock
, new->br_blockcount
,
952 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
953 if ((error
= xfs_bmbt_insert(cur
, &i
)))
958 /* DELTA: The in-core extent described by new changed type. */
959 temp
= new->br_startoff
;
960 temp2
= new->br_blockcount
;
963 case MASK2(LEFT_FILLING
, LEFT_CONTIG
):
965 * Filling in the first part of a previous delayed allocation.
966 * The left neighbor is contiguous.
968 xfs_bmap_trace_pre_update(fname
, "LF|LC", ip
, idx
- 1,
970 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, idx
- 1),
971 LEFT
.br_blockcount
+ new->br_blockcount
);
972 xfs_bmbt_set_startoff(ep
,
973 PREV
.br_startoff
+ new->br_blockcount
);
974 xfs_bmap_trace_post_update(fname
, "LF|LC", ip
, idx
- 1,
976 temp
= PREV
.br_blockcount
- new->br_blockcount
;
977 xfs_bmap_trace_pre_update(fname
, "LF|LC", ip
, idx
,
979 xfs_bmbt_set_blockcount(ep
, temp
);
980 ip
->i_df
.if_lastex
= idx
- 1;
982 rval
= XFS_ILOG_DEXT
;
985 if ((error
= xfs_bmbt_lookup_eq(cur
, LEFT
.br_startoff
,
986 LEFT
.br_startblock
, LEFT
.br_blockcount
,
990 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
997 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
998 STARTBLOCKVAL(PREV
.br_startblock
));
999 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
1000 xfs_bmap_trace_post_update(fname
, "LF|LC", ip
, idx
,
1003 /* DELTA: The boundary between two in-core extents moved. */
1004 temp
= LEFT
.br_startoff
;
1005 temp2
= LEFT
.br_blockcount
+
1009 case MASK(LEFT_FILLING
):
1011 * Filling in the first part of a previous delayed allocation.
1012 * The left neighbor is not contiguous.
1014 xfs_bmap_trace_pre_update(fname
, "LF", ip
, idx
, XFS_DATA_FORK
);
1015 xfs_bmbt_set_startoff(ep
, new_endoff
);
1016 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1017 xfs_bmbt_set_blockcount(ep
, temp
);
1018 xfs_bmap_trace_insert(fname
, "LF", ip
, idx
, 1, new, NULL
,
1020 xfs_iext_insert(ifp
, idx
, 1, new);
1021 ip
->i_df
.if_lastex
= idx
;
1022 ip
->i_d
.di_nextents
++;
1024 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1026 rval
= XFS_ILOG_CORE
;
1027 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1028 new->br_startblock
, new->br_blockcount
,
1032 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1033 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1037 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
1038 ip
->i_d
.di_nextents
> ip
->i_df
.if_ext_max
) {
1039 error
= xfs_bmap_extents_to_btree(ip
->i_transp
, ip
,
1040 first
, flist
, &cur
, 1, &tmp_rval
,
1046 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
1047 STARTBLOCKVAL(PREV
.br_startblock
) -
1048 (cur
? cur
->bc_private
.b
.allocated
: 0));
1049 ep
= xfs_iext_get_ext(ifp
, idx
+ 1);
1050 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
1051 xfs_bmap_trace_post_update(fname
, "LF", ip
, idx
+ 1,
1054 /* DELTA: One in-core extent is split in two. */
1055 temp
= PREV
.br_startoff
;
1056 temp2
= PREV
.br_blockcount
;
1059 case MASK2(RIGHT_FILLING
, RIGHT_CONTIG
):
1061 * Filling in the last part of a previous delayed allocation.
1062 * The right neighbor is contiguous with the new allocation.
1064 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1065 xfs_bmap_trace_pre_update(fname
, "RF|RC", ip
, idx
,
1067 xfs_bmap_trace_pre_update(fname
, "RF|RC", ip
, idx
+ 1,
1069 xfs_bmbt_set_blockcount(ep
, temp
);
1070 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, idx
+ 1),
1071 new->br_startoff
, new->br_startblock
,
1072 new->br_blockcount
+ RIGHT
.br_blockcount
,
1074 xfs_bmap_trace_post_update(fname
, "RF|RC", ip
, idx
+ 1,
1076 ip
->i_df
.if_lastex
= idx
+ 1;
1078 rval
= XFS_ILOG_DEXT
;
1081 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
1082 RIGHT
.br_startblock
,
1083 RIGHT
.br_blockcount
, &i
)))
1086 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
1088 new->br_blockcount
+
1089 RIGHT
.br_blockcount
,
1093 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
1094 STARTBLOCKVAL(PREV
.br_startblock
));
1095 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
1096 xfs_bmap_trace_post_update(fname
, "RF|RC", ip
, idx
,
1099 /* DELTA: The boundary between two in-core extents moved. */
1100 temp
= PREV
.br_startoff
;
1101 temp2
= PREV
.br_blockcount
+
1102 RIGHT
.br_blockcount
;
1105 case MASK(RIGHT_FILLING
):
1107 * Filling in the last part of a previous delayed allocation.
1108 * The right neighbor is not contiguous.
1110 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1111 xfs_bmap_trace_pre_update(fname
, "RF", ip
, idx
, XFS_DATA_FORK
);
1112 xfs_bmbt_set_blockcount(ep
, temp
);
1113 xfs_bmap_trace_insert(fname
, "RF", ip
, idx
+ 1, 1,
1114 new, NULL
, XFS_DATA_FORK
);
1115 xfs_iext_insert(ifp
, idx
+ 1, 1, new);
1116 ip
->i_df
.if_lastex
= idx
+ 1;
1117 ip
->i_d
.di_nextents
++;
1119 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1121 rval
= XFS_ILOG_CORE
;
1122 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1123 new->br_startblock
, new->br_blockcount
,
1127 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1128 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1132 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
1133 ip
->i_d
.di_nextents
> ip
->i_df
.if_ext_max
) {
1134 error
= xfs_bmap_extents_to_btree(ip
->i_transp
, ip
,
1135 first
, flist
, &cur
, 1, &tmp_rval
,
1141 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
1142 STARTBLOCKVAL(PREV
.br_startblock
) -
1143 (cur
? cur
->bc_private
.b
.allocated
: 0));
1144 ep
= xfs_iext_get_ext(ifp
, idx
);
1145 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
1146 xfs_bmap_trace_post_update(fname
, "RF", ip
, idx
, XFS_DATA_FORK
);
1148 /* DELTA: One in-core extent is split in two. */
1149 temp
= PREV
.br_startoff
;
1150 temp2
= PREV
.br_blockcount
;
1155 * Filling in the middle part of a previous delayed allocation.
1156 * Contiguity is impossible here.
1157 * This case is avoided almost all the time.
1159 temp
= new->br_startoff
- PREV
.br_startoff
;
1160 xfs_bmap_trace_pre_update(fname
, "0", ip
, idx
, XFS_DATA_FORK
);
1161 xfs_bmbt_set_blockcount(ep
, temp
);
1163 r
[1].br_state
= PREV
.br_state
;
1164 r
[1].br_startblock
= 0;
1165 r
[1].br_startoff
= new_endoff
;
1166 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1167 r
[1].br_blockcount
= temp2
;
1168 xfs_bmap_trace_insert(fname
, "0", ip
, idx
+ 1, 2, &r
[0], &r
[1],
1170 xfs_iext_insert(ifp
, idx
+ 1, 2, &r
[0]);
1171 ip
->i_df
.if_lastex
= idx
+ 1;
1172 ip
->i_d
.di_nextents
++;
1174 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1176 rval
= XFS_ILOG_CORE
;
1177 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1178 new->br_startblock
, new->br_blockcount
,
1182 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1183 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1187 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
1188 ip
->i_d
.di_nextents
> ip
->i_df
.if_ext_max
) {
1189 error
= xfs_bmap_extents_to_btree(ip
->i_transp
, ip
,
1190 first
, flist
, &cur
, 1, &tmp_rval
,
1196 temp
= xfs_bmap_worst_indlen(ip
, temp
);
1197 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
1198 diff
= (int)(temp
+ temp2
- STARTBLOCKVAL(PREV
.br_startblock
) -
1199 (cur
? cur
->bc_private
.b
.allocated
: 0));
1201 xfs_mod_incore_sb(ip
->i_mount
, XFS_SBS_FDBLOCKS
, -((int64_t)diff
), rsvd
)) {
1203 * Ick gross gag me with a spoon.
1205 ASSERT(0); /* want to see if this ever happens! */
1211 !xfs_mod_incore_sb(ip
->i_mount
,
1212 XFS_SBS_FDBLOCKS
, -((int64_t)diff
), rsvd
))
1219 !xfs_mod_incore_sb(ip
->i_mount
,
1220 XFS_SBS_FDBLOCKS
, -((int64_t)diff
), rsvd
))
1225 ep
= xfs_iext_get_ext(ifp
, idx
);
1226 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
1227 xfs_bmap_trace_post_update(fname
, "0", ip
, idx
, XFS_DATA_FORK
);
1228 xfs_bmap_trace_pre_update(fname
, "0", ip
, idx
+ 2,
1230 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, idx
+ 2),
1231 NULLSTARTBLOCK((int)temp2
));
1232 xfs_bmap_trace_post_update(fname
, "0", ip
, idx
+ 2,
1234 *dnew
= temp
+ temp2
;
1235 /* DELTA: One in-core extent is split in three. */
1236 temp
= PREV
.br_startoff
;
1237 temp2
= PREV
.br_blockcount
;
1240 case MASK3(LEFT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
1241 case MASK3(RIGHT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
1242 case MASK2(LEFT_FILLING
, RIGHT_CONTIG
):
1243 case MASK2(RIGHT_FILLING
, LEFT_CONTIG
):
1244 case MASK2(LEFT_CONTIG
, RIGHT_CONTIG
):
1245 case MASK(LEFT_CONTIG
):
1246 case MASK(RIGHT_CONTIG
):
1248 * These cases are all impossible.
1255 if (delta
->xed_startoff
> temp
)
1256 delta
->xed_startoff
= temp
;
1257 if (delta
->xed_blockcount
< temp2
)
1258 delta
->xed_blockcount
= temp2
;
1272 #undef STATE_SET_TEST
1277 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1278 * allocation to a real allocation or vice versa.
1280 STATIC
int /* error */
1281 xfs_bmap_add_extent_unwritten_real(
1282 xfs_inode_t
*ip
, /* incore inode pointer */
1283 xfs_extnum_t idx
, /* extent number to update/insert */
1284 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
1285 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
1286 int *logflagsp
, /* inode logging flags */
1287 xfs_extdelta_t
*delta
) /* Change made to incore extents */
1289 xfs_btree_cur_t
*cur
; /* btree cursor */
1290 xfs_bmbt_rec_t
*ep
; /* extent entry for idx */
1291 int error
; /* error return value */
1292 #ifdef XFS_BMAP_TRACE
1293 static char fname
[] = "xfs_bmap_add_extent_unwritten_real";
1295 int i
; /* temp state */
1296 xfs_ifork_t
*ifp
; /* inode fork pointer */
1297 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1298 xfs_exntst_t newext
; /* new extent state */
1299 xfs_exntst_t oldext
; /* old extent state */
1300 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1301 /* left is 0, right is 1, prev is 2 */
1302 int rval
=0; /* return value (logging flags) */
1303 int state
= 0;/* state bits, accessed thru macros */
1304 xfs_filblks_t temp
=0;
1305 xfs_filblks_t temp2
=0;
1306 enum { /* bit number definitions for state */
1307 LEFT_CONTIG
, RIGHT_CONTIG
,
1308 LEFT_FILLING
, RIGHT_FILLING
,
1309 LEFT_DELAY
, RIGHT_DELAY
,
1310 LEFT_VALID
, RIGHT_VALID
1316 #define MASK(b) (1 << (b))
1317 #define MASK2(a,b) (MASK(a) | MASK(b))
1318 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
1319 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
1320 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1321 #define STATE_TEST(b) (state & MASK(b))
1322 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1323 ((state &= ~MASK(b)), 0))
1324 #define SWITCH_STATE \
1325 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
1328 * Set up a bunch of variables to make the tests simpler.
1332 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
1333 ep
= xfs_iext_get_ext(ifp
, idx
);
1334 xfs_bmbt_get_all(ep
, &PREV
);
1335 newext
= new->br_state
;
1336 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
1337 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
1338 ASSERT(PREV
.br_state
== oldext
);
1339 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1340 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1341 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1343 * Set flags determining what part of the previous oldext allocation
1344 * extent is being replaced by a newext allocation.
1346 STATE_SET(LEFT_FILLING
, PREV
.br_startoff
== new->br_startoff
);
1347 STATE_SET(RIGHT_FILLING
,
1348 PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
);
1350 * Check and set flags if this segment has a left neighbor.
1351 * Don't set contiguous if the combined extent would be too large.
1353 if (STATE_SET_TEST(LEFT_VALID
, idx
> 0)) {
1354 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, idx
- 1), &LEFT
);
1355 STATE_SET(LEFT_DELAY
, ISNULLSTARTBLOCK(LEFT
.br_startblock
));
1357 STATE_SET(LEFT_CONTIG
,
1358 STATE_TEST(LEFT_VALID
) && !STATE_TEST(LEFT_DELAY
) &&
1359 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1360 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1361 LEFT
.br_state
== newext
&&
1362 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
);
1364 * Check and set flags if this segment has a right neighbor.
1365 * Don't set contiguous if the combined extent would be too large.
1366 * Also check for all-three-contiguous being too large.
1368 if (STATE_SET_TEST(RIGHT_VALID
,
1370 ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1)) {
1371 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, idx
+ 1), &RIGHT
);
1372 STATE_SET(RIGHT_DELAY
, ISNULLSTARTBLOCK(RIGHT
.br_startblock
));
1374 STATE_SET(RIGHT_CONTIG
,
1375 STATE_TEST(RIGHT_VALID
) && !STATE_TEST(RIGHT_DELAY
) &&
1376 new_endoff
== RIGHT
.br_startoff
&&
1377 new->br_startblock
+ new->br_blockcount
==
1378 RIGHT
.br_startblock
&&
1379 newext
== RIGHT
.br_state
&&
1380 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1381 ((state
& MASK3(LEFT_CONTIG
, LEFT_FILLING
, RIGHT_FILLING
)) !=
1382 MASK3(LEFT_CONTIG
, LEFT_FILLING
, RIGHT_FILLING
) ||
1383 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1386 * Switch out based on the FILLING and CONTIG state bits.
1388 switch (SWITCH_STATE
) {
1390 case MASK4(LEFT_FILLING
, RIGHT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
1392 * Setting all of a previous oldext extent to newext.
1393 * The left and right neighbors are both contiguous with new.
1395 xfs_bmap_trace_pre_update(fname
, "LF|RF|LC|RC", ip
, idx
- 1,
1397 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, idx
- 1),
1398 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1399 RIGHT
.br_blockcount
);
1400 xfs_bmap_trace_post_update(fname
, "LF|RF|LC|RC", ip
, idx
- 1,
1402 xfs_bmap_trace_delete(fname
, "LF|RF|LC|RC", ip
, idx
, 2,
1404 xfs_iext_remove(ifp
, idx
, 2);
1405 ip
->i_df
.if_lastex
= idx
- 1;
1406 ip
->i_d
.di_nextents
-= 2;
1408 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1410 rval
= XFS_ILOG_CORE
;
1411 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
1412 RIGHT
.br_startblock
,
1413 RIGHT
.br_blockcount
, &i
)))
1416 if ((error
= xfs_bmbt_delete(cur
, &i
)))
1419 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
1422 if ((error
= xfs_bmbt_delete(cur
, &i
)))
1425 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
1428 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
1430 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1431 RIGHT
.br_blockcount
, LEFT
.br_state
)))
1434 /* DELTA: Three in-core extents are replaced by one. */
1435 temp
= LEFT
.br_startoff
;
1436 temp2
= LEFT
.br_blockcount
+
1437 PREV
.br_blockcount
+
1438 RIGHT
.br_blockcount
;
1441 case MASK3(LEFT_FILLING
, RIGHT_FILLING
, LEFT_CONTIG
):
1443 * Setting all of a previous oldext extent to newext.
1444 * The left neighbor is contiguous, the right is not.
1446 xfs_bmap_trace_pre_update(fname
, "LF|RF|LC", ip
, idx
- 1,
1448 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, idx
- 1),
1449 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
1450 xfs_bmap_trace_post_update(fname
, "LF|RF|LC", ip
, idx
- 1,
1452 ip
->i_df
.if_lastex
= idx
- 1;
1453 xfs_bmap_trace_delete(fname
, "LF|RF|LC", ip
, idx
, 1,
1455 xfs_iext_remove(ifp
, idx
, 1);
1456 ip
->i_d
.di_nextents
--;
1458 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1460 rval
= XFS_ILOG_CORE
;
1461 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1462 PREV
.br_startblock
, PREV
.br_blockcount
,
1466 if ((error
= xfs_bmbt_delete(cur
, &i
)))
1469 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
1472 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
1474 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
1478 /* DELTA: Two in-core extents are replaced by one. */
1479 temp
= LEFT
.br_startoff
;
1480 temp2
= LEFT
.br_blockcount
+
1484 case MASK3(LEFT_FILLING
, RIGHT_FILLING
, RIGHT_CONTIG
):
1486 * Setting all of a previous oldext extent to newext.
1487 * The right neighbor is contiguous, the left is not.
1489 xfs_bmap_trace_pre_update(fname
, "LF|RF|RC", ip
, idx
,
1491 xfs_bmbt_set_blockcount(ep
,
1492 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
1493 xfs_bmbt_set_state(ep
, newext
);
1494 xfs_bmap_trace_post_update(fname
, "LF|RF|RC", ip
, idx
,
1496 ip
->i_df
.if_lastex
= idx
;
1497 xfs_bmap_trace_delete(fname
, "LF|RF|RC", ip
, idx
+ 1, 1,
1499 xfs_iext_remove(ifp
, idx
+ 1, 1);
1500 ip
->i_d
.di_nextents
--;
1502 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1504 rval
= XFS_ILOG_CORE
;
1505 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
1506 RIGHT
.br_startblock
,
1507 RIGHT
.br_blockcount
, &i
)))
1510 if ((error
= xfs_bmbt_delete(cur
, &i
)))
1513 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
1516 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
1518 new->br_blockcount
+ RIGHT
.br_blockcount
,
1522 /* DELTA: Two in-core extents are replaced by one. */
1523 temp
= PREV
.br_startoff
;
1524 temp2
= PREV
.br_blockcount
+
1525 RIGHT
.br_blockcount
;
1528 case MASK2(LEFT_FILLING
, RIGHT_FILLING
):
1530 * Setting all of a previous oldext extent to newext.
1531 * Neither the left nor right neighbors are contiguous with
1534 xfs_bmap_trace_pre_update(fname
, "LF|RF", ip
, idx
,
1536 xfs_bmbt_set_state(ep
, newext
);
1537 xfs_bmap_trace_post_update(fname
, "LF|RF", ip
, idx
,
1539 ip
->i_df
.if_lastex
= idx
;
1541 rval
= XFS_ILOG_DEXT
;
1544 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1545 new->br_startblock
, new->br_blockcount
,
1549 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
1550 new->br_startblock
, new->br_blockcount
,
1554 /* DELTA: The in-core extent described by new changed type. */
1555 temp
= new->br_startoff
;
1556 temp2
= new->br_blockcount
;
1559 case MASK2(LEFT_FILLING
, LEFT_CONTIG
):
1561 * Setting the first part of a previous oldext extent to newext.
1562 * The left neighbor is contiguous.
1564 xfs_bmap_trace_pre_update(fname
, "LF|LC", ip
, idx
- 1,
1566 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, idx
- 1),
1567 LEFT
.br_blockcount
+ new->br_blockcount
);
1568 xfs_bmbt_set_startoff(ep
,
1569 PREV
.br_startoff
+ new->br_blockcount
);
1570 xfs_bmap_trace_post_update(fname
, "LF|LC", ip
, idx
- 1,
1572 xfs_bmap_trace_pre_update(fname
, "LF|LC", ip
, idx
,
1574 xfs_bmbt_set_startblock(ep
,
1575 new->br_startblock
+ new->br_blockcount
);
1576 xfs_bmbt_set_blockcount(ep
,
1577 PREV
.br_blockcount
- new->br_blockcount
);
1578 xfs_bmap_trace_post_update(fname
, "LF|LC", ip
, idx
,
1580 ip
->i_df
.if_lastex
= idx
- 1;
1582 rval
= XFS_ILOG_DEXT
;
1585 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1586 PREV
.br_startblock
, PREV
.br_blockcount
,
1590 if ((error
= xfs_bmbt_update(cur
,
1591 PREV
.br_startoff
+ new->br_blockcount
,
1592 PREV
.br_startblock
+ new->br_blockcount
,
1593 PREV
.br_blockcount
- new->br_blockcount
,
1596 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
1598 if (xfs_bmbt_update(cur
, LEFT
.br_startoff
,
1600 LEFT
.br_blockcount
+ new->br_blockcount
,
1604 /* DELTA: The boundary between two in-core extents moved. */
1605 temp
= LEFT
.br_startoff
;
1606 temp2
= LEFT
.br_blockcount
+
1610 case MASK(LEFT_FILLING
):
1612 * Setting the first part of a previous oldext extent to newext.
1613 * The left neighbor is not contiguous.
1615 xfs_bmap_trace_pre_update(fname
, "LF", ip
, idx
, XFS_DATA_FORK
);
1616 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
1617 xfs_bmbt_set_startoff(ep
, new_endoff
);
1618 xfs_bmbt_set_blockcount(ep
,
1619 PREV
.br_blockcount
- new->br_blockcount
);
1620 xfs_bmbt_set_startblock(ep
,
1621 new->br_startblock
+ new->br_blockcount
);
1622 xfs_bmap_trace_post_update(fname
, "LF", ip
, idx
, XFS_DATA_FORK
);
1623 xfs_bmap_trace_insert(fname
, "LF", ip
, idx
, 1, new, NULL
,
1625 xfs_iext_insert(ifp
, idx
, 1, new);
1626 ip
->i_df
.if_lastex
= idx
;
1627 ip
->i_d
.di_nextents
++;
1629 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1631 rval
= XFS_ILOG_CORE
;
1632 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1633 PREV
.br_startblock
, PREV
.br_blockcount
,
1637 if ((error
= xfs_bmbt_update(cur
,
1638 PREV
.br_startoff
+ new->br_blockcount
,
1639 PREV
.br_startblock
+ new->br_blockcount
,
1640 PREV
.br_blockcount
- new->br_blockcount
,
1643 cur
->bc_rec
.b
= *new;
1644 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1648 /* DELTA: One in-core extent is split in two. */
1649 temp
= PREV
.br_startoff
;
1650 temp2
= PREV
.br_blockcount
;
1653 case MASK2(RIGHT_FILLING
, RIGHT_CONTIG
):
1655 * Setting the last part of a previous oldext extent to newext.
1656 * The right neighbor is contiguous with the new allocation.
1658 xfs_bmap_trace_pre_update(fname
, "RF|RC", ip
, idx
,
1660 xfs_bmap_trace_pre_update(fname
, "RF|RC", ip
, idx
+ 1,
1662 xfs_bmbt_set_blockcount(ep
,
1663 PREV
.br_blockcount
- new->br_blockcount
);
1664 xfs_bmap_trace_post_update(fname
, "RF|RC", ip
, idx
,
1666 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp
, idx
+ 1),
1667 new->br_startoff
, new->br_startblock
,
1668 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
1669 xfs_bmap_trace_post_update(fname
, "RF|RC", ip
, idx
+ 1,
1671 ip
->i_df
.if_lastex
= idx
+ 1;
1673 rval
= XFS_ILOG_DEXT
;
1676 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1678 PREV
.br_blockcount
, &i
)))
1681 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
1683 PREV
.br_blockcount
- new->br_blockcount
,
1686 if ((error
= xfs_bmbt_increment(cur
, 0, &i
)))
1688 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
1690 new->br_blockcount
+ RIGHT
.br_blockcount
,
1694 /* DELTA: The boundary between two in-core extents moved. */
1695 temp
= PREV
.br_startoff
;
1696 temp2
= PREV
.br_blockcount
+
1697 RIGHT
.br_blockcount
;
1700 case MASK(RIGHT_FILLING
):
1702 * Setting the last part of a previous oldext extent to newext.
1703 * The right neighbor is not contiguous.
1705 xfs_bmap_trace_pre_update(fname
, "RF", ip
, idx
, XFS_DATA_FORK
);
1706 xfs_bmbt_set_blockcount(ep
,
1707 PREV
.br_blockcount
- new->br_blockcount
);
1708 xfs_bmap_trace_post_update(fname
, "RF", ip
, idx
, XFS_DATA_FORK
);
1709 xfs_bmap_trace_insert(fname
, "RF", ip
, idx
+ 1, 1,
1710 new, NULL
, XFS_DATA_FORK
);
1711 xfs_iext_insert(ifp
, idx
+ 1, 1, new);
1712 ip
->i_df
.if_lastex
= idx
+ 1;
1713 ip
->i_d
.di_nextents
++;
1715 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1717 rval
= XFS_ILOG_CORE
;
1718 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1719 PREV
.br_startblock
, PREV
.br_blockcount
,
1723 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
1725 PREV
.br_blockcount
- new->br_blockcount
,
1728 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1729 new->br_startblock
, new->br_blockcount
,
1733 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1734 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1738 /* DELTA: One in-core extent is split in two. */
1739 temp
= PREV
.br_startoff
;
1740 temp2
= PREV
.br_blockcount
;
1745 * Setting the middle part of a previous oldext extent to
1746 * newext. Contiguity is impossible here.
1747 * One extent becomes three extents.
1749 xfs_bmap_trace_pre_update(fname
, "0", ip
, idx
, XFS_DATA_FORK
);
1750 xfs_bmbt_set_blockcount(ep
,
1751 new->br_startoff
- PREV
.br_startoff
);
1752 xfs_bmap_trace_post_update(fname
, "0", ip
, idx
, XFS_DATA_FORK
);
1754 r
[1].br_startoff
= new_endoff
;
1755 r
[1].br_blockcount
=
1756 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1757 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
1758 r
[1].br_state
= oldext
;
1759 xfs_bmap_trace_insert(fname
, "0", ip
, idx
+ 1, 2, &r
[0], &r
[1],
1761 xfs_iext_insert(ifp
, idx
+ 1, 2, &r
[0]);
1762 ip
->i_df
.if_lastex
= idx
+ 1;
1763 ip
->i_d
.di_nextents
+= 2;
1765 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1767 rval
= XFS_ILOG_CORE
;
1768 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1769 PREV
.br_startblock
, PREV
.br_blockcount
,
1773 /* new right extent - oldext */
1774 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
1775 r
[1].br_startblock
, r
[1].br_blockcount
,
1778 /* new left extent - oldext */
1779 PREV
.br_blockcount
=
1780 new->br_startoff
- PREV
.br_startoff
;
1781 cur
->bc_rec
.b
= PREV
;
1782 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1785 if ((error
= xfs_bmbt_increment(cur
, 0, &i
)))
1788 /* new middle extent - newext */
1789 cur
->bc_rec
.b
= *new;
1790 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1794 /* DELTA: One in-core extent is split in three. */
1795 temp
= PREV
.br_startoff
;
1796 temp2
= PREV
.br_blockcount
;
1799 case MASK3(LEFT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
1800 case MASK3(RIGHT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
1801 case MASK2(LEFT_FILLING
, RIGHT_CONTIG
):
1802 case MASK2(RIGHT_FILLING
, LEFT_CONTIG
):
1803 case MASK2(LEFT_CONTIG
, RIGHT_CONTIG
):
1804 case MASK(LEFT_CONTIG
):
1805 case MASK(RIGHT_CONTIG
):
1807 * These cases are all impossible.
1814 if (delta
->xed_startoff
> temp
)
1815 delta
->xed_startoff
= temp
;
1816 if (delta
->xed_blockcount
< temp2
)
1817 delta
->xed_blockcount
= temp2
;
1831 #undef STATE_SET_TEST
1836 * Called by xfs_bmap_add_extent to handle cases converting a hole
1837 * to a delayed allocation.
1840 STATIC
int /* error */
1841 xfs_bmap_add_extent_hole_delay(
1842 xfs_inode_t
*ip
, /* incore inode pointer */
1843 xfs_extnum_t idx
, /* extent number to update/insert */
1844 xfs_btree_cur_t
*cur
, /* if null, not a btree */
1845 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
1846 int *logflagsp
, /* inode logging flags */
1847 xfs_extdelta_t
*delta
, /* Change made to incore extents */
1848 int rsvd
) /* OK to allocate reserved blocks */
1850 xfs_bmbt_rec_t
*ep
; /* extent record for idx */
1851 #ifdef XFS_BMAP_TRACE
1852 static char fname
[] = "xfs_bmap_add_extent_hole_delay";
1854 xfs_ifork_t
*ifp
; /* inode fork pointer */
1855 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
1856 xfs_filblks_t newlen
=0; /* new indirect size */
1857 xfs_filblks_t oldlen
=0; /* old indirect size */
1858 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
1859 int state
; /* state bits, accessed thru macros */
1860 xfs_filblks_t temp
=0; /* temp for indirect calculations */
1861 xfs_filblks_t temp2
=0;
1862 enum { /* bit number definitions for state */
1863 LEFT_CONTIG
, RIGHT_CONTIG
,
1864 LEFT_DELAY
, RIGHT_DELAY
,
1865 LEFT_VALID
, RIGHT_VALID
1868 #define MASK(b) (1 << (b))
1869 #define MASK2(a,b) (MASK(a) | MASK(b))
1870 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1871 #define STATE_TEST(b) (state & MASK(b))
1872 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1873 ((state &= ~MASK(b)), 0))
1874 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1876 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
1877 ep
= xfs_iext_get_ext(ifp
, idx
);
1879 ASSERT(ISNULLSTARTBLOCK(new->br_startblock
));
1881 * Check and set flags if this segment has a left neighbor
1883 if (STATE_SET_TEST(LEFT_VALID
, idx
> 0)) {
1884 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, idx
- 1), &left
);
1885 STATE_SET(LEFT_DELAY
, ISNULLSTARTBLOCK(left
.br_startblock
));
1888 * Check and set flags if the current (right) segment exists.
1889 * If it doesn't exist, we're converting the hole at end-of-file.
1891 if (STATE_SET_TEST(RIGHT_VALID
,
1893 ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1894 xfs_bmbt_get_all(ep
, &right
);
1895 STATE_SET(RIGHT_DELAY
, ISNULLSTARTBLOCK(right
.br_startblock
));
1898 * Set contiguity flags on the left and right neighbors.
1899 * Don't let extents get too large, even if the pieces are contiguous.
1901 STATE_SET(LEFT_CONTIG
,
1902 STATE_TEST(LEFT_VALID
) && STATE_TEST(LEFT_DELAY
) &&
1903 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
1904 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
);
1905 STATE_SET(RIGHT_CONTIG
,
1906 STATE_TEST(RIGHT_VALID
) && STATE_TEST(RIGHT_DELAY
) &&
1907 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
1908 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
1909 (!STATE_TEST(LEFT_CONTIG
) ||
1910 (left
.br_blockcount
+ new->br_blockcount
+
1911 right
.br_blockcount
<= MAXEXTLEN
)));
1913 * Switch out based on the contiguity flags.
1915 switch (SWITCH_STATE
) {
1917 case MASK2(LEFT_CONTIG
, RIGHT_CONTIG
):
1919 * New allocation is contiguous with delayed allocations
1920 * on the left and on the right.
1921 * Merge all three into a single extent record.
1923 temp
= left
.br_blockcount
+ new->br_blockcount
+
1924 right
.br_blockcount
;
1925 xfs_bmap_trace_pre_update(fname
, "LC|RC", ip
, idx
- 1,
1927 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, idx
- 1), temp
);
1928 oldlen
= STARTBLOCKVAL(left
.br_startblock
) +
1929 STARTBLOCKVAL(new->br_startblock
) +
1930 STARTBLOCKVAL(right
.br_startblock
);
1931 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
1932 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, idx
- 1),
1933 NULLSTARTBLOCK((int)newlen
));
1934 xfs_bmap_trace_post_update(fname
, "LC|RC", ip
, idx
- 1,
1936 xfs_bmap_trace_delete(fname
, "LC|RC", ip
, idx
, 1,
1938 xfs_iext_remove(ifp
, idx
, 1);
1939 ip
->i_df
.if_lastex
= idx
- 1;
1940 /* DELTA: Two in-core extents were replaced by one. */
1942 temp
= left
.br_startoff
;
1945 case MASK(LEFT_CONTIG
):
1947 * New allocation is contiguous with a delayed allocation
1949 * Merge the new allocation with the left neighbor.
1951 temp
= left
.br_blockcount
+ new->br_blockcount
;
1952 xfs_bmap_trace_pre_update(fname
, "LC", ip
, idx
- 1,
1954 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, idx
- 1), temp
);
1955 oldlen
= STARTBLOCKVAL(left
.br_startblock
) +
1956 STARTBLOCKVAL(new->br_startblock
);
1957 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
1958 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp
, idx
- 1),
1959 NULLSTARTBLOCK((int)newlen
));
1960 xfs_bmap_trace_post_update(fname
, "LC", ip
, idx
- 1,
1962 ip
->i_df
.if_lastex
= idx
- 1;
1963 /* DELTA: One in-core extent grew into a hole. */
1965 temp
= left
.br_startoff
;
1968 case MASK(RIGHT_CONTIG
):
1970 * New allocation is contiguous with a delayed allocation
1972 * Merge the new allocation with the right neighbor.
1974 xfs_bmap_trace_pre_update(fname
, "RC", ip
, idx
, XFS_DATA_FORK
);
1975 temp
= new->br_blockcount
+ right
.br_blockcount
;
1976 oldlen
= STARTBLOCKVAL(new->br_startblock
) +
1977 STARTBLOCKVAL(right
.br_startblock
);
1978 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
1979 xfs_bmbt_set_allf(ep
, new->br_startoff
,
1980 NULLSTARTBLOCK((int)newlen
), temp
, right
.br_state
);
1981 xfs_bmap_trace_post_update(fname
, "RC", ip
, idx
, XFS_DATA_FORK
);
1982 ip
->i_df
.if_lastex
= idx
;
1983 /* DELTA: One in-core extent grew into a hole. */
1985 temp
= new->br_startoff
;
1990 * New allocation is not contiguous with another
1991 * delayed allocation.
1992 * Insert a new entry.
1994 oldlen
= newlen
= 0;
1995 xfs_bmap_trace_insert(fname
, "0", ip
, idx
, 1, new, NULL
,
1997 xfs_iext_insert(ifp
, idx
, 1, new);
1998 ip
->i_df
.if_lastex
= idx
;
1999 /* DELTA: A new in-core extent was added in a hole. */
2000 temp2
= new->br_blockcount
;
2001 temp
= new->br_startoff
;
2004 if (oldlen
!= newlen
) {
2005 ASSERT(oldlen
> newlen
);
2006 xfs_mod_incore_sb(ip
->i_mount
, XFS_SBS_FDBLOCKS
,
2007 (int64_t)(oldlen
- newlen
), rsvd
);
2009 * Nothing to do for disk quota accounting here.
2014 if (delta
->xed_startoff
> temp
)
2015 delta
->xed_startoff
= temp
;
2016 if (delta
->xed_blockcount
< temp2
)
2017 delta
->xed_blockcount
= temp2
;
2025 #undef STATE_SET_TEST
2030 * Called by xfs_bmap_add_extent to handle cases converting a hole
2031 * to a real allocation.
2033 STATIC
int /* error */
2034 xfs_bmap_add_extent_hole_real(
2035 xfs_inode_t
*ip
, /* incore inode pointer */
2036 xfs_extnum_t idx
, /* extent number to update/insert */
2037 xfs_btree_cur_t
*cur
, /* if null, not a btree */
2038 xfs_bmbt_irec_t
*new, /* new data to add to file extents */
2039 int *logflagsp
, /* inode logging flags */
2040 xfs_extdelta_t
*delta
, /* Change made to incore extents */
2041 int whichfork
) /* data or attr fork */
2043 xfs_bmbt_rec_t
*ep
; /* pointer to extent entry ins. point */
2044 int error
; /* error return value */
2045 #ifdef XFS_BMAP_TRACE
2046 static char fname
[] = "xfs_bmap_add_extent_hole_real";
2048 int i
; /* temp state */
2049 xfs_ifork_t
*ifp
; /* inode fork pointer */
2050 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
2051 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
2052 int rval
=0; /* return value (logging flags) */
2053 int state
; /* state bits, accessed thru macros */
2054 xfs_filblks_t temp
=0;
2055 xfs_filblks_t temp2
=0;
2056 enum { /* bit number definitions for state */
2057 LEFT_CONTIG
, RIGHT_CONTIG
,
2058 LEFT_DELAY
, RIGHT_DELAY
,
2059 LEFT_VALID
, RIGHT_VALID
2062 #define MASK(b) (1 << (b))
2063 #define MASK2(a,b) (MASK(a) | MASK(b))
2064 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
2065 #define STATE_TEST(b) (state & MASK(b))
2066 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
2067 ((state &= ~MASK(b)), 0))
2068 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
2070 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2071 ASSERT(idx
<= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
));
2072 ep
= xfs_iext_get_ext(ifp
, idx
);
2075 * Check and set flags if this segment has a left neighbor.
2077 if (STATE_SET_TEST(LEFT_VALID
, idx
> 0)) {
2078 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, idx
- 1), &left
);
2079 STATE_SET(LEFT_DELAY
, ISNULLSTARTBLOCK(left
.br_startblock
));
2082 * Check and set flags if this segment has a current value.
2083 * Not true if we're inserting into the "hole" at eof.
2085 if (STATE_SET_TEST(RIGHT_VALID
,
2087 ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
2088 xfs_bmbt_get_all(ep
, &right
);
2089 STATE_SET(RIGHT_DELAY
, ISNULLSTARTBLOCK(right
.br_startblock
));
2092 * We're inserting a real allocation between "left" and "right".
2093 * Set the contiguity flags. Don't let extents get too large.
2095 STATE_SET(LEFT_CONTIG
,
2096 STATE_TEST(LEFT_VALID
) && !STATE_TEST(LEFT_DELAY
) &&
2097 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2098 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2099 left
.br_state
== new->br_state
&&
2100 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
);
2101 STATE_SET(RIGHT_CONTIG
,
2102 STATE_TEST(RIGHT_VALID
) && !STATE_TEST(RIGHT_DELAY
) &&
2103 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2104 new->br_startblock
+ new->br_blockcount
==
2105 right
.br_startblock
&&
2106 new->br_state
== right
.br_state
&&
2107 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2108 (!STATE_TEST(LEFT_CONTIG
) ||
2109 left
.br_blockcount
+ new->br_blockcount
+
2110 right
.br_blockcount
<= MAXEXTLEN
));
2114 * Select which case we're in here, and implement it.
2116 switch (SWITCH_STATE
) {
2118 case MASK2(LEFT_CONTIG
, RIGHT_CONTIG
):
2120 * New allocation is contiguous with real allocations on the
2121 * left and on the right.
2122 * Merge all three into a single extent record.
2124 xfs_bmap_trace_pre_update(fname
, "LC|RC", ip
, idx
- 1,
2126 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, idx
- 1),
2127 left
.br_blockcount
+ new->br_blockcount
+
2128 right
.br_blockcount
);
2129 xfs_bmap_trace_post_update(fname
, "LC|RC", ip
, idx
- 1,
2131 xfs_bmap_trace_delete(fname
, "LC|RC", ip
,
2133 xfs_iext_remove(ifp
, idx
, 1);
2134 ifp
->if_lastex
= idx
- 1;
2135 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2136 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2138 rval
= XFS_ILOG_CORE
| XFS_ILOG_FEXT(whichfork
);
2140 rval
= XFS_ILOG_CORE
;
2141 if ((error
= xfs_bmbt_lookup_eq(cur
,
2143 right
.br_startblock
,
2144 right
.br_blockcount
, &i
)))
2147 if ((error
= xfs_bmbt_delete(cur
, &i
)))
2150 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
2153 if ((error
= xfs_bmbt_update(cur
, left
.br_startoff
,
2155 left
.br_blockcount
+
2156 new->br_blockcount
+
2157 right
.br_blockcount
,
2161 /* DELTA: Two in-core extents were replaced by one. */
2162 temp
= left
.br_startoff
;
2163 temp2
= left
.br_blockcount
+
2164 new->br_blockcount
+
2165 right
.br_blockcount
;
2168 case MASK(LEFT_CONTIG
):
2170 * New allocation is contiguous with a real allocation
2172 * Merge the new allocation with the left neighbor.
2174 xfs_bmap_trace_pre_update(fname
, "LC", ip
, idx
- 1, whichfork
);
2175 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp
, idx
- 1),
2176 left
.br_blockcount
+ new->br_blockcount
);
2177 xfs_bmap_trace_post_update(fname
, "LC", ip
, idx
- 1, whichfork
);
2178 ifp
->if_lastex
= idx
- 1;
2180 rval
= XFS_ILOG_FEXT(whichfork
);
2183 if ((error
= xfs_bmbt_lookup_eq(cur
,
2186 left
.br_blockcount
, &i
)))
2189 if ((error
= xfs_bmbt_update(cur
, left
.br_startoff
,
2191 left
.br_blockcount
+
2196 /* DELTA: One in-core extent grew. */
2197 temp
= left
.br_startoff
;
2198 temp2
= left
.br_blockcount
+
2202 case MASK(RIGHT_CONTIG
):
2204 * New allocation is contiguous with a real allocation
2206 * Merge the new allocation with the right neighbor.
2208 xfs_bmap_trace_pre_update(fname
, "RC", ip
, idx
, whichfork
);
2209 xfs_bmbt_set_allf(ep
, new->br_startoff
, new->br_startblock
,
2210 new->br_blockcount
+ right
.br_blockcount
,
2212 xfs_bmap_trace_post_update(fname
, "RC", ip
, idx
, whichfork
);
2213 ifp
->if_lastex
= idx
;
2215 rval
= XFS_ILOG_FEXT(whichfork
);
2218 if ((error
= xfs_bmbt_lookup_eq(cur
,
2220 right
.br_startblock
,
2221 right
.br_blockcount
, &i
)))
2224 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
2226 new->br_blockcount
+
2227 right
.br_blockcount
,
2231 /* DELTA: One in-core extent grew. */
2232 temp
= new->br_startoff
;
2233 temp2
= new->br_blockcount
+
2234 right
.br_blockcount
;
2239 * New allocation is not contiguous with another
2241 * Insert a new entry.
2243 xfs_bmap_trace_insert(fname
, "0", ip
, idx
, 1, new, NULL
,
2245 xfs_iext_insert(ifp
, idx
, 1, new);
2246 ifp
->if_lastex
= idx
;
2247 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2248 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2250 rval
= XFS_ILOG_CORE
| XFS_ILOG_FEXT(whichfork
);
2252 rval
= XFS_ILOG_CORE
;
2253 if ((error
= xfs_bmbt_lookup_eq(cur
,
2256 new->br_blockcount
, &i
)))
2259 cur
->bc_rec
.b
.br_state
= new->br_state
;
2260 if ((error
= xfs_bmbt_insert(cur
, &i
)))
2264 /* DELTA: A new extent was added in a hole. */
2265 temp
= new->br_startoff
;
2266 temp2
= new->br_blockcount
;
2271 if (delta
->xed_startoff
> temp
)
2272 delta
->xed_startoff
= temp
;
2273 if (delta
->xed_blockcount
< temp2
)
2274 delta
->xed_blockcount
= temp2
;
2283 #undef STATE_SET_TEST
2288 * Adjust the size of the new extent based on di_extsize and rt extsize.
2291 xfs_bmap_extsize_align(
2293 xfs_bmbt_irec_t
*gotp
, /* next extent pointer */
2294 xfs_bmbt_irec_t
*prevp
, /* previous extent pointer */
2295 xfs_extlen_t extsz
, /* align to this extent size */
2296 int rt
, /* is this a realtime inode? */
2297 int eof
, /* is extent at end-of-file? */
2298 int delay
, /* creating delalloc extent? */
2299 int convert
, /* overwriting unwritten extent? */
2300 xfs_fileoff_t
*offp
, /* in/out: aligned offset */
2301 xfs_extlen_t
*lenp
) /* in/out: aligned length */
2303 xfs_fileoff_t orig_off
; /* original offset */
2304 xfs_extlen_t orig_alen
; /* original length */
2305 xfs_fileoff_t orig_end
; /* original off+len */
2306 xfs_fileoff_t nexto
; /* next file offset */
2307 xfs_fileoff_t prevo
; /* previous file offset */
2308 xfs_fileoff_t align_off
; /* temp for offset */
2309 xfs_extlen_t align_alen
; /* temp for length */
2310 xfs_extlen_t temp
; /* temp for calculations */
2315 orig_off
= align_off
= *offp
;
2316 orig_alen
= align_alen
= *lenp
;
2317 orig_end
= orig_off
+ orig_alen
;
2320 * If this request overlaps an existing extent, then don't
2321 * attempt to perform any additional alignment.
2323 if (!delay
&& !eof
&&
2324 (orig_off
>= gotp
->br_startoff
) &&
2325 (orig_end
<= gotp
->br_startoff
+ gotp
->br_blockcount
)) {
2330 * If the file offset is unaligned vs. the extent size
2331 * we need to align it. This will be possible unless
2332 * the file was previously written with a kernel that didn't
2333 * perform this alignment, or if a truncate shot us in the
2336 temp
= do_mod(orig_off
, extsz
);
2342 * Same adjustment for the end of the requested area.
2344 if ((temp
= (align_alen
% extsz
))) {
2345 align_alen
+= extsz
- temp
;
2348 * If the previous block overlaps with this proposed allocation
2349 * then move the start forward without adjusting the length.
2351 if (prevp
->br_startoff
!= NULLFILEOFF
) {
2352 if (prevp
->br_startblock
== HOLESTARTBLOCK
)
2353 prevo
= prevp
->br_startoff
;
2355 prevo
= prevp
->br_startoff
+ prevp
->br_blockcount
;
2358 if (align_off
!= orig_off
&& align_off
< prevo
)
2361 * If the next block overlaps with this proposed allocation
2362 * then move the start back without adjusting the length,
2363 * but not before offset 0.
2364 * This may of course make the start overlap previous block,
2365 * and if we hit the offset 0 limit then the next block
2366 * can still overlap too.
2368 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
) {
2369 if ((delay
&& gotp
->br_startblock
== HOLESTARTBLOCK
) ||
2370 (!delay
&& gotp
->br_startblock
== DELAYSTARTBLOCK
))
2371 nexto
= gotp
->br_startoff
+ gotp
->br_blockcount
;
2373 nexto
= gotp
->br_startoff
;
2375 nexto
= NULLFILEOFF
;
2377 align_off
+ align_alen
!= orig_end
&&
2378 align_off
+ align_alen
> nexto
)
2379 align_off
= nexto
> align_alen
? nexto
- align_alen
: 0;
2381 * If we're now overlapping the next or previous extent that
2382 * means we can't fit an extsz piece in this hole. Just move
2383 * the start forward to the first valid spot and set
2384 * the length so we hit the end.
2386 if (align_off
!= orig_off
&& align_off
< prevo
)
2388 if (align_off
+ align_alen
!= orig_end
&&
2389 align_off
+ align_alen
> nexto
&&
2390 nexto
!= NULLFILEOFF
) {
2391 ASSERT(nexto
> prevo
);
2392 align_alen
= nexto
- align_off
;
2396 * If realtime, and the result isn't a multiple of the realtime
2397 * extent size we need to remove blocks until it is.
2399 if (rt
&& (temp
= (align_alen
% mp
->m_sb
.sb_rextsize
))) {
2401 * We're not covering the original request, or
2402 * we won't be able to once we fix the length.
2404 if (orig_off
< align_off
||
2405 orig_end
> align_off
+ align_alen
||
2406 align_alen
- temp
< orig_alen
)
2407 return XFS_ERROR(EINVAL
);
2409 * Try to fix it by moving the start up.
2411 if (align_off
+ temp
<= orig_off
) {
2416 * Try to fix it by moving the end in.
2418 else if (align_off
+ align_alen
- temp
>= orig_end
)
2421 * Set the start to the minimum then trim the length.
2424 align_alen
-= orig_off
- align_off
;
2425 align_off
= orig_off
;
2426 align_alen
-= align_alen
% mp
->m_sb
.sb_rextsize
;
2429 * Result doesn't cover the request, fail it.
2431 if (orig_off
< align_off
|| orig_end
> align_off
+ align_alen
)
2432 return XFS_ERROR(EINVAL
);
2434 ASSERT(orig_off
>= align_off
);
2435 ASSERT(orig_end
<= align_off
+ align_alen
);
2439 if (!eof
&& gotp
->br_startoff
!= NULLFILEOFF
)
2440 ASSERT(align_off
+ align_alen
<= gotp
->br_startoff
);
2441 if (prevp
->br_startoff
!= NULLFILEOFF
)
2442 ASSERT(align_off
>= prevp
->br_startoff
+ prevp
->br_blockcount
);
2450 #define XFS_ALLOC_GAP_UNITS 4
2454 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
2456 xfs_fsblock_t adjust
; /* adjustment to block numbers */
2457 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
2458 xfs_mount_t
*mp
; /* mount point structure */
2459 int nullfb
; /* true if ap->firstblock isn't set */
2460 int rt
; /* true if inode is realtime */
2462 #define ISVALID(x,y) \
2464 (x) < mp->m_sb.sb_rblocks : \
2465 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2466 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2467 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2469 mp
= ap
->ip
->i_mount
;
2470 nullfb
= ap
->firstblock
== NULLFSBLOCK
;
2471 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
;
2472 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, ap
->firstblock
);
2474 * If allocating at eof, and there's a previous real block,
2475 * try to use it's last block as our starting point.
2477 if (ap
->eof
&& ap
->prevp
->br_startoff
!= NULLFILEOFF
&&
2478 !ISNULLSTARTBLOCK(ap
->prevp
->br_startblock
) &&
2479 ISVALID(ap
->prevp
->br_startblock
+ ap
->prevp
->br_blockcount
,
2480 ap
->prevp
->br_startblock
)) {
2481 ap
->rval
= ap
->prevp
->br_startblock
+ ap
->prevp
->br_blockcount
;
2483 * Adjust for the gap between prevp and us.
2486 (ap
->prevp
->br_startoff
+ ap
->prevp
->br_blockcount
);
2488 ISVALID(ap
->rval
+ adjust
, ap
->prevp
->br_startblock
))
2492 * If not at eof, then compare the two neighbor blocks.
2493 * Figure out whether either one gives us a good starting point,
2494 * and pick the better one.
2496 else if (!ap
->eof
) {
2497 xfs_fsblock_t gotbno
; /* right side block number */
2498 xfs_fsblock_t gotdiff
=0; /* right side difference */
2499 xfs_fsblock_t prevbno
; /* left side block number */
2500 xfs_fsblock_t prevdiff
=0; /* left side difference */
2503 * If there's a previous (left) block, select a requested
2504 * start block based on it.
2506 if (ap
->prevp
->br_startoff
!= NULLFILEOFF
&&
2507 !ISNULLSTARTBLOCK(ap
->prevp
->br_startblock
) &&
2508 (prevbno
= ap
->prevp
->br_startblock
+
2509 ap
->prevp
->br_blockcount
) &&
2510 ISVALID(prevbno
, ap
->prevp
->br_startblock
)) {
2512 * Calculate gap to end of previous block.
2514 adjust
= prevdiff
= ap
->off
-
2515 (ap
->prevp
->br_startoff
+
2516 ap
->prevp
->br_blockcount
);
2518 * Figure the startblock based on the previous block's
2519 * end and the gap size.
2521 * If the gap is large relative to the piece we're
2522 * allocating, or using it gives us an invalid block
2523 * number, then just use the end of the previous block.
2525 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->alen
&&
2526 ISVALID(prevbno
+ prevdiff
,
2527 ap
->prevp
->br_startblock
))
2532 * If the firstblock forbids it, can't use it,
2535 if (!rt
&& !nullfb
&&
2536 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
2537 prevbno
= NULLFSBLOCK
;
2540 * No previous block or can't follow it, just default.
2543 prevbno
= NULLFSBLOCK
;
2545 * If there's a following (right) block, select a requested
2546 * start block based on it.
2548 if (!ISNULLSTARTBLOCK(ap
->gotp
->br_startblock
)) {
2550 * Calculate gap to start of next block.
2552 adjust
= gotdiff
= ap
->gotp
->br_startoff
- ap
->off
;
2554 * Figure the startblock based on the next block's
2555 * start and the gap size.
2557 gotbno
= ap
->gotp
->br_startblock
;
2560 * If the gap is large relative to the piece we're
2561 * allocating, or using it gives us an invalid block
2562 * number, then just use the start of the next block
2563 * offset by our length.
2565 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->alen
&&
2566 ISVALID(gotbno
- gotdiff
, gotbno
))
2568 else if (ISVALID(gotbno
- ap
->alen
, gotbno
)) {
2570 gotdiff
+= adjust
- ap
->alen
;
2574 * If the firstblock forbids it, can't use it,
2577 if (!rt
&& !nullfb
&&
2578 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
2579 gotbno
= NULLFSBLOCK
;
2582 * No next block, just default.
2585 gotbno
= NULLFSBLOCK
;
2587 * If both valid, pick the better one, else the only good
2588 * one, else ap->rval is already set (to 0 or the inode block).
2590 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
2591 ap
->rval
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
2592 else if (prevbno
!= NULLFSBLOCK
)
2594 else if (gotbno
!= NULLFSBLOCK
)
2603 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
2605 xfs_alloctype_t atype
= 0; /* type for allocation routines */
2606 int error
; /* error return value */
2607 xfs_mount_t
*mp
; /* mount point structure */
2608 xfs_extlen_t prod
= 0; /* product factor for allocators */
2609 xfs_extlen_t ralen
= 0; /* realtime allocation length */
2610 xfs_extlen_t align
; /* minimum allocation alignment */
2611 xfs_rtblock_t rtx
; /* realtime extent number */
2614 mp
= ap
->ip
->i_mount
;
2615 align
= ap
->ip
->i_d
.di_extsize
?
2616 ap
->ip
->i_d
.di_extsize
: mp
->m_sb
.sb_rextsize
;
2617 prod
= align
/ mp
->m_sb
.sb_rextsize
;
2618 error
= xfs_bmap_extsize_align(mp
, ap
->gotp
, ap
->prevp
,
2619 align
, 1, ap
->eof
, 0,
2620 ap
->conv
, &ap
->off
, &ap
->alen
);
2624 ASSERT(ap
->alen
% mp
->m_sb
.sb_rextsize
== 0);
2627 * If the offset & length are not perfectly aligned
2628 * then kill prod, it will just get us in trouble.
2630 if (do_mod(ap
->off
, align
) || ap
->alen
% align
)
2633 * Set ralen to be the actual requested length in rtextents.
2635 ralen
= ap
->alen
/ mp
->m_sb
.sb_rextsize
;
2637 * If the old value was close enough to MAXEXTLEN that
2638 * we rounded up to it, cut it back so it's valid again.
2639 * Note that if it's a really large request (bigger than
2640 * MAXEXTLEN), we don't hear about that number, and can't
2641 * adjust the starting point to match it.
2643 if (ralen
* mp
->m_sb
.sb_rextsize
>= MAXEXTLEN
)
2644 ralen
= MAXEXTLEN
/ mp
->m_sb
.sb_rextsize
;
2646 * If it's an allocation to an empty file at offset 0,
2647 * pick an extent that will space things out in the rt area.
2649 if (ap
->eof
&& ap
->off
== 0) {
2650 error
= xfs_rtpick_extent(mp
, ap
->tp
, ralen
, &rtx
);
2653 ap
->rval
= rtx
* mp
->m_sb
.sb_rextsize
;
2658 xfs_bmap_adjacent(ap
);
2661 * Realtime allocation, done through xfs_rtallocate_extent.
2663 atype
= ap
->rval
== 0 ? XFS_ALLOCTYPE_ANY_AG
: XFS_ALLOCTYPE_NEAR_BNO
;
2664 do_div(ap
->rval
, mp
->m_sb
.sb_rextsize
);
2667 if ((error
= xfs_rtallocate_extent(ap
->tp
, ap
->rval
, 1, ap
->alen
,
2668 &ralen
, atype
, ap
->wasdel
, prod
, &rtb
)))
2670 if (rtb
== NULLFSBLOCK
&& prod
> 1 &&
2671 (error
= xfs_rtallocate_extent(ap
->tp
, ap
->rval
, 1,
2672 ap
->alen
, &ralen
, atype
,
2673 ap
->wasdel
, 1, &rtb
)))
2676 if (ap
->rval
!= NULLFSBLOCK
) {
2677 ap
->rval
*= mp
->m_sb
.sb_rextsize
;
2678 ralen
*= mp
->m_sb
.sb_rextsize
;
2680 ap
->ip
->i_d
.di_nblocks
+= ralen
;
2681 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
2683 ap
->ip
->i_delayed_blks
-= ralen
;
2685 * Adjust the disk quota also. This was reserved
2688 XFS_TRANS_MOD_DQUOT_BYINO(mp
, ap
->tp
, ap
->ip
,
2689 ap
->wasdel
? XFS_TRANS_DQ_DELRTBCOUNT
:
2690 XFS_TRANS_DQ_RTBCOUNT
, (long) ralen
);
2699 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
2701 xfs_mount_t
*mp
; /* mount point structure */
2702 xfs_alloctype_t atype
= 0; /* type for allocation routines */
2703 xfs_extlen_t align
; /* minimum allocation alignment */
2705 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
2706 xfs_agnumber_t startag
;
2707 xfs_alloc_arg_t args
;
2710 xfs_extlen_t longest
;
2712 xfs_extlen_t nextminlen
= 0;
2714 int nullfb
; /* true if ap->firstblock isn't set */
2720 mp
= ap
->ip
->i_mount
;
2721 align
= (ap
->userdata
&& ap
->ip
->i_d
.di_extsize
&&
2722 (ap
->ip
->i_d
.di_flags
& XFS_DIFLAG_EXTSIZE
)) ?
2723 ap
->ip
->i_d
.di_extsize
: 0;
2724 if (unlikely(align
)) {
2725 error
= xfs_bmap_extsize_align(mp
, ap
->gotp
, ap
->prevp
,
2726 align
, 0, ap
->eof
, 0, ap
->conv
,
2727 &ap
->off
, &ap
->alen
);
2731 nullfb
= ap
->firstblock
== NULLFSBLOCK
;
2732 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, ap
->firstblock
);
2734 ap
->rval
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
2736 ap
->rval
= ap
->firstblock
;
2738 xfs_bmap_adjacent(ap
);
2741 * If allowed, use ap->rval; otherwise must use firstblock since
2742 * it's in the right allocation group.
2744 if (nullfb
|| XFS_FSB_TO_AGNO(mp
, ap
->rval
) == fb_agno
)
2747 ap
->rval
= ap
->firstblock
;
2749 * Normal allocation, done through xfs_alloc_vextent.
2751 tryagain
= isaligned
= 0;
2754 args
.fsbno
= ap
->rval
;
2755 args
.maxlen
= MIN(ap
->alen
, mp
->m_sb
.sb_agblocks
);
2756 args
.firstblock
= ap
->firstblock
;
2759 args
.type
= XFS_ALLOCTYPE_START_BNO
;
2760 args
.total
= ap
->total
;
2762 * Find the longest available space.
2763 * We're going to try for the whole allocation at once.
2765 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
.fsbno
);
2767 down_read(&mp
->m_peraglock
);
2768 while (blen
< ap
->alen
) {
2769 pag
= &mp
->m_perag
[ag
];
2770 if (!pag
->pagf_init
&&
2771 (error
= xfs_alloc_pagf_init(mp
, args
.tp
,
2772 ag
, XFS_ALLOC_FLAG_TRYLOCK
))) {
2773 up_read(&mp
->m_peraglock
);
2777 * See xfs_alloc_fix_freelist...
2779 if (pag
->pagf_init
) {
2780 need
= XFS_MIN_FREELIST_PAG(pag
, mp
);
2781 delta
= need
> pag
->pagf_flcount
?
2782 need
- pag
->pagf_flcount
: 0;
2783 longest
= (pag
->pagf_longest
> delta
) ?
2784 (pag
->pagf_longest
- delta
) :
2785 (pag
->pagf_flcount
> 0 ||
2786 pag
->pagf_longest
> 0);
2791 if (++ag
== mp
->m_sb
.sb_agcount
)
2796 up_read(&mp
->m_peraglock
);
2798 * Since the above loop did a BUF_TRYLOCK, it is
2799 * possible that there is space for this request.
2801 if (notinit
|| blen
< ap
->minlen
)
2802 args
.minlen
= ap
->minlen
;
2804 * If the best seen length is less than the request
2805 * length, use the best as the minimum.
2807 else if (blen
< ap
->alen
)
2810 * Otherwise we've seen an extent as big as alen,
2811 * use that as the minimum.
2814 args
.minlen
= ap
->alen
;
2815 } else if (ap
->low
) {
2816 args
.type
= XFS_ALLOCTYPE_START_BNO
;
2817 args
.total
= args
.minlen
= ap
->minlen
;
2819 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
2820 args
.total
= ap
->total
;
2821 args
.minlen
= ap
->minlen
;
2823 if (unlikely(ap
->userdata
&& ap
->ip
->i_d
.di_extsize
&&
2824 (ap
->ip
->i_d
.di_flags
& XFS_DIFLAG_EXTSIZE
))) {
2825 args
.prod
= ap
->ip
->i_d
.di_extsize
;
2826 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->off
, args
.prod
)))
2827 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
2828 } else if (mp
->m_sb
.sb_blocksize
>= NBPP
) {
2832 args
.prod
= NBPP
>> mp
->m_sb
.sb_blocklog
;
2833 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->off
, args
.prod
))))
2834 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
2837 * If we are not low on available data blocks, and the
2838 * underlying logical volume manager is a stripe, and
2839 * the file offset is zero then try to allocate data
2840 * blocks on stripe unit boundary.
2841 * NOTE: ap->aeof is only set if the allocation length
2842 * is >= the stripe unit and the allocation offset is
2843 * at the end of file.
2845 if (!ap
->low
&& ap
->aeof
) {
2847 args
.alignment
= mp
->m_dalign
;
2851 * Adjust for alignment
2853 if (blen
> args
.alignment
&& blen
<= ap
->alen
)
2854 args
.minlen
= blen
- args
.alignment
;
2855 args
.minalignslop
= 0;
2858 * First try an exact bno allocation.
2859 * If it fails then do a near or start bno
2860 * allocation with alignment turned on.
2864 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
2867 * Compute the minlen+alignment for the
2868 * next case. Set slop so that the value
2869 * of minlen+alignment+slop doesn't go up
2870 * between the calls.
2872 if (blen
> mp
->m_dalign
&& blen
<= ap
->alen
)
2873 nextminlen
= blen
- mp
->m_dalign
;
2875 nextminlen
= args
.minlen
;
2876 if (nextminlen
+ mp
->m_dalign
> args
.minlen
+ 1)
2878 nextminlen
+ mp
->m_dalign
-
2881 args
.minalignslop
= 0;
2885 args
.minalignslop
= 0;
2887 args
.minleft
= ap
->minleft
;
2888 args
.wasdel
= ap
->wasdel
;
2890 args
.userdata
= ap
->userdata
;
2891 if ((error
= xfs_alloc_vextent(&args
)))
2893 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
2895 * Exact allocation failed. Now try with alignment
2899 args
.fsbno
= ap
->rval
;
2900 args
.alignment
= mp
->m_dalign
;
2901 args
.minlen
= nextminlen
;
2902 args
.minalignslop
= 0;
2904 if ((error
= xfs_alloc_vextent(&args
)))
2907 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
2909 * allocation failed, so turn off alignment and
2913 args
.fsbno
= ap
->rval
;
2915 if ((error
= xfs_alloc_vextent(&args
)))
2918 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
2919 args
.minlen
> ap
->minlen
) {
2920 args
.minlen
= ap
->minlen
;
2921 args
.type
= XFS_ALLOCTYPE_START_BNO
;
2922 args
.fsbno
= ap
->rval
;
2923 if ((error
= xfs_alloc_vextent(&args
)))
2926 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
2928 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
2929 args
.total
= ap
->minlen
;
2931 if ((error
= xfs_alloc_vextent(&args
)))
2935 if (args
.fsbno
!= NULLFSBLOCK
) {
2936 ap
->firstblock
= ap
->rval
= args
.fsbno
;
2937 ASSERT(nullfb
|| fb_agno
== args
.agno
||
2938 (ap
->low
&& fb_agno
< args
.agno
));
2939 ap
->alen
= args
.len
;
2940 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
2941 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
2943 ap
->ip
->i_delayed_blks
-= args
.len
;
2945 * Adjust the disk quota also. This was reserved
2948 XFS_TRANS_MOD_DQUOT_BYINO(mp
, ap
->tp
, ap
->ip
,
2949 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
2950 XFS_TRANS_DQ_BCOUNT
,
2953 ap
->rval
= NULLFSBLOCK
;
2960 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2961 * It figures out where to ask the underlying allocator to put the new extent.
2965 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
2967 if ((ap
->ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
) && ap
->userdata
)
2968 return xfs_bmap_rtalloc(ap
);
2969 return xfs_bmap_btalloc(ap
);
2973 * Transform a btree format file with only one leaf node, where the
2974 * extents list will fit in the inode, into an extents format file.
2975 * Since the file extents are already in-core, all we have to do is
2976 * give up the space for the btree root and pitch the leaf block.
2978 STATIC
int /* error */
2979 xfs_bmap_btree_to_extents(
2980 xfs_trans_t
*tp
, /* transaction pointer */
2981 xfs_inode_t
*ip
, /* incore inode pointer */
2982 xfs_btree_cur_t
*cur
, /* btree cursor */
2983 int *logflagsp
, /* inode logging flags */
2984 int whichfork
) /* data or attr fork */
2987 xfs_bmbt_block_t
*cblock
;/* child btree block */
2988 xfs_fsblock_t cbno
; /* child block number */
2989 xfs_buf_t
*cbp
; /* child block's buffer */
2990 int error
; /* error return value */
2991 xfs_ifork_t
*ifp
; /* inode fork data */
2992 xfs_mount_t
*mp
; /* mount point structure */
2993 __be64
*pp
; /* ptr to block address */
2994 xfs_bmbt_block_t
*rblock
;/* root btree block */
2996 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2997 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
2998 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
2999 rblock
= ifp
->if_broot
;
3000 ASSERT(be16_to_cpu(rblock
->bb_level
) == 1);
3001 ASSERT(be16_to_cpu(rblock
->bb_numrecs
) == 1);
3002 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp
->if_broot_bytes
) == 1);
3004 pp
= XFS_BMAP_BROOT_PTR_ADDR(rblock
, 1, ifp
->if_broot_bytes
);
3005 cbno
= be64_to_cpu(*pp
);
3008 if ((error
= xfs_btree_check_lptr(cur
, cbno
, 1)))
3011 if ((error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
,
3012 XFS_BMAP_BTREE_REF
)))
3014 cblock
= XFS_BUF_TO_BMBT_BLOCK(cbp
);
3015 if ((error
= xfs_btree_check_lblock(cur
, cblock
, 0, cbp
)))
3017 xfs_bmap_add_free(cbno
, 1, cur
->bc_private
.b
.flist
, mp
);
3018 ip
->i_d
.di_nblocks
--;
3019 XFS_TRANS_MOD_DQUOT_BYINO(mp
, tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
3020 xfs_trans_binval(tp
, cbp
);
3021 if (cur
->bc_bufs
[0] == cbp
)
3022 cur
->bc_bufs
[0] = NULL
;
3023 xfs_iroot_realloc(ip
, -1, whichfork
);
3024 ASSERT(ifp
->if_broot
== NULL
);
3025 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
3026 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
3027 *logflagsp
= XFS_ILOG_CORE
| XFS_ILOG_FEXT(whichfork
);
3032 * Called by xfs_bmapi to update file extent records and the btree
3033 * after removing space (or undoing a delayed allocation).
3035 STATIC
int /* error */
3036 xfs_bmap_del_extent(
3037 xfs_inode_t
*ip
, /* incore inode pointer */
3038 xfs_trans_t
*tp
, /* current transaction pointer */
3039 xfs_extnum_t idx
, /* extent number to update/delete */
3040 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
3041 xfs_btree_cur_t
*cur
, /* if null, not a btree */
3042 xfs_bmbt_irec_t
*del
, /* data to remove from extents */
3043 int *logflagsp
, /* inode logging flags */
3044 xfs_extdelta_t
*delta
, /* Change made to incore extents */
3045 int whichfork
, /* data or attr fork */
3046 int rsvd
) /* OK to allocate reserved blocks */
3048 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
3049 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
3050 xfs_fsblock_t del_endblock
=0; /* first block past del */
3051 xfs_fileoff_t del_endoff
; /* first offset past del */
3052 int delay
; /* current block is delayed allocated */
3053 int do_fx
; /* free extent at end of routine */
3054 xfs_bmbt_rec_t
*ep
; /* current extent entry pointer */
3055 int error
; /* error return value */
3056 int flags
; /* inode logging flags */
3057 #ifdef XFS_BMAP_TRACE
3058 static char fname
[] = "xfs_bmap_del_extent";
3060 xfs_bmbt_irec_t got
; /* current extent entry */
3061 xfs_fileoff_t got_endoff
; /* first offset past got */
3062 int i
; /* temp state */
3063 xfs_ifork_t
*ifp
; /* inode fork pointer */
3064 xfs_mount_t
*mp
; /* mount structure */
3065 xfs_filblks_t nblks
; /* quota/sb block count */
3066 xfs_bmbt_irec_t
new; /* new record to be inserted */
3068 uint qfield
; /* quota field to update */
3069 xfs_filblks_t temp
; /* for indirect length calculations */
3070 xfs_filblks_t temp2
; /* for indirect length calculations */
3072 XFS_STATS_INC(xs_del_exlist
);
3074 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3075 ASSERT((idx
>= 0) && (idx
< ifp
->if_bytes
/
3076 (uint
)sizeof(xfs_bmbt_rec_t
)));
3077 ASSERT(del
->br_blockcount
> 0);
3078 ep
= xfs_iext_get_ext(ifp
, idx
);
3079 xfs_bmbt_get_all(ep
, &got
);
3080 ASSERT(got
.br_startoff
<= del
->br_startoff
);
3081 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
3082 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
3083 ASSERT(got_endoff
>= del_endoff
);
3084 delay
= ISNULLSTARTBLOCK(got
.br_startblock
);
3085 ASSERT(ISNULLSTARTBLOCK(del
->br_startblock
) == delay
);
3090 * If deleting a real allocation, must free up the disk space.
3093 flags
= XFS_ILOG_CORE
;
3095 * Realtime allocation. Free it and record di_nblocks update.
3097 if (whichfork
== XFS_DATA_FORK
&&
3098 (ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
)) {
3102 ASSERT(do_mod(del
->br_blockcount
,
3103 mp
->m_sb
.sb_rextsize
) == 0);
3104 ASSERT(do_mod(del
->br_startblock
,
3105 mp
->m_sb
.sb_rextsize
) == 0);
3106 bno
= del
->br_startblock
;
3107 len
= del
->br_blockcount
;
3108 do_div(bno
, mp
->m_sb
.sb_rextsize
);
3109 do_div(len
, mp
->m_sb
.sb_rextsize
);
3110 if ((error
= xfs_rtfree_extent(ip
->i_transp
, bno
,
3111 (xfs_extlen_t
)len
)))
3114 nblks
= len
* mp
->m_sb
.sb_rextsize
;
3115 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
3118 * Ordinary allocation.
3122 nblks
= del
->br_blockcount
;
3123 qfield
= XFS_TRANS_DQ_BCOUNT
;
3126 * Set up del_endblock and cur for later.
3128 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
3130 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
3131 got
.br_startblock
, got
.br_blockcount
,
3136 da_old
= da_new
= 0;
3138 da_old
= STARTBLOCKVAL(got
.br_startblock
);
3144 * Set flag value to use in switch statement.
3145 * Left-contig is 2, right-contig is 1.
3147 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
3148 (got_endoff
== del_endoff
)) {
3151 * Matches the whole extent. Delete the entry.
3153 xfs_bmap_trace_delete(fname
, "3", ip
, idx
, 1, whichfork
);
3154 xfs_iext_remove(ifp
, idx
, 1);
3155 ifp
->if_lastex
= idx
;
3158 XFS_IFORK_NEXT_SET(ip
, whichfork
,
3159 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
3160 flags
|= XFS_ILOG_CORE
;
3162 flags
|= XFS_ILOG_FEXT(whichfork
);
3165 if ((error
= xfs_bmbt_delete(cur
, &i
)))
3172 * Deleting the first part of the extent.
3174 xfs_bmap_trace_pre_update(fname
, "2", ip
, idx
, whichfork
);
3175 xfs_bmbt_set_startoff(ep
, del_endoff
);
3176 temp
= got
.br_blockcount
- del
->br_blockcount
;
3177 xfs_bmbt_set_blockcount(ep
, temp
);
3178 ifp
->if_lastex
= idx
;
3180 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
3182 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
3183 xfs_bmap_trace_post_update(fname
, "2", ip
, idx
,
3188 xfs_bmbt_set_startblock(ep
, del_endblock
);
3189 xfs_bmap_trace_post_update(fname
, "2", ip
, idx
, whichfork
);
3191 flags
|= XFS_ILOG_FEXT(whichfork
);
3194 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
3195 got
.br_blockcount
- del
->br_blockcount
,
3202 * Deleting the last part of the extent.
3204 temp
= got
.br_blockcount
- del
->br_blockcount
;
3205 xfs_bmap_trace_pre_update(fname
, "1", ip
, idx
, whichfork
);
3206 xfs_bmbt_set_blockcount(ep
, temp
);
3207 ifp
->if_lastex
= idx
;
3209 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
3211 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
3212 xfs_bmap_trace_post_update(fname
, "1", ip
, idx
,
3217 xfs_bmap_trace_post_update(fname
, "1", ip
, idx
, whichfork
);
3219 flags
|= XFS_ILOG_FEXT(whichfork
);
3222 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
3224 got
.br_blockcount
- del
->br_blockcount
,
3231 * Deleting the middle of the extent.
3233 temp
= del
->br_startoff
- got
.br_startoff
;
3234 xfs_bmap_trace_pre_update(fname
, "0", ip
, idx
, whichfork
);
3235 xfs_bmbt_set_blockcount(ep
, temp
);
3236 new.br_startoff
= del_endoff
;
3237 temp2
= got_endoff
- del_endoff
;
3238 new.br_blockcount
= temp2
;
3239 new.br_state
= got
.br_state
;
3241 new.br_startblock
= del_endblock
;
3242 flags
|= XFS_ILOG_CORE
;
3244 if ((error
= xfs_bmbt_update(cur
,
3246 got
.br_startblock
, temp
,
3249 if ((error
= xfs_bmbt_increment(cur
, 0, &i
)))
3251 cur
->bc_rec
.b
= new;
3252 error
= xfs_bmbt_insert(cur
, &i
);
3253 if (error
&& error
!= ENOSPC
)
3256 * If get no-space back from btree insert,
3257 * it tried a split, and we have a zero
3258 * block reservation.
3259 * Fix up our state and return the error.
3261 if (error
== ENOSPC
) {
3263 * Reset the cursor, don't trust
3264 * it after any insert operation.
3266 if ((error
= xfs_bmbt_lookup_eq(cur
,
3273 * Update the btree record back
3274 * to the original value.
3276 if ((error
= xfs_bmbt_update(cur
,
3283 * Reset the extent record back
3284 * to the original value.
3286 xfs_bmbt_set_blockcount(ep
,
3289 error
= XFS_ERROR(ENOSPC
);
3294 flags
|= XFS_ILOG_FEXT(whichfork
);
3295 XFS_IFORK_NEXT_SET(ip
, whichfork
,
3296 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
3298 ASSERT(whichfork
== XFS_DATA_FORK
);
3299 temp
= xfs_bmap_worst_indlen(ip
, temp
);
3300 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
3301 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
3302 new.br_startblock
= NULLSTARTBLOCK((int)temp2
);
3303 da_new
= temp
+ temp2
;
3304 while (da_new
> da_old
) {
3308 xfs_bmbt_set_startblock(ep
,
3309 NULLSTARTBLOCK((int)temp
));
3311 if (da_new
== da_old
)
3317 NULLSTARTBLOCK((int)temp2
);
3321 xfs_bmap_trace_post_update(fname
, "0", ip
, idx
, whichfork
);
3322 xfs_bmap_trace_insert(fname
, "0", ip
, idx
+ 1, 1, &new, NULL
,
3324 xfs_iext_insert(ifp
, idx
+ 1, 1, &new);
3325 ifp
->if_lastex
= idx
+ 1;
3329 * If we need to, add to list of extents to delete.
3332 xfs_bmap_add_free(del
->br_startblock
, del
->br_blockcount
, flist
,
3335 * Adjust inode # blocks in the file.
3338 ip
->i_d
.di_nblocks
-= nblks
;
3340 * Adjust quota data.
3343 XFS_TRANS_MOD_DQUOT_BYINO(mp
, tp
, ip
, qfield
, (long)-nblks
);
3346 * Account for change in delayed indirect blocks.
3347 * Nothing to do for disk quota accounting here.
3349 ASSERT(da_old
>= da_new
);
3350 if (da_old
> da_new
)
3351 xfs_mod_incore_sb(mp
, XFS_SBS_FDBLOCKS
, (int64_t)(da_old
- da_new
),
3354 /* DELTA: report the original extent. */
3355 if (delta
->xed_startoff
> got
.br_startoff
)
3356 delta
->xed_startoff
= got
.br_startoff
;
3357 if (delta
->xed_blockcount
< got
.br_startoff
+got
.br_blockcount
)
3358 delta
->xed_blockcount
= got
.br_startoff
+
3367 * Remove the entry "free" from the free item list. Prev points to the
3368 * previous entry, unless "free" is the head of the list.
3372 xfs_bmap_free_t
*flist
, /* free item list header */
3373 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
3374 xfs_bmap_free_item_t
*free
) /* list item to be freed */
3377 prev
->xbfi_next
= free
->xbfi_next
;
3379 flist
->xbf_first
= free
->xbfi_next
;
3381 kmem_zone_free(xfs_bmap_free_item_zone
, free
);
3385 * Convert an extents-format file into a btree-format file.
3386 * The new file will have a root block (in the inode) and a single child block.
3388 STATIC
int /* error */
3389 xfs_bmap_extents_to_btree(
3390 xfs_trans_t
*tp
, /* transaction pointer */
3391 xfs_inode_t
*ip
, /* incore inode pointer */
3392 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
3393 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
3394 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
3395 int wasdel
, /* converting a delayed alloc */
3396 int *logflagsp
, /* inode logging flags */
3397 int whichfork
) /* data or attr fork */
3399 xfs_bmbt_block_t
*ablock
; /* allocated (child) bt block */
3400 xfs_buf_t
*abp
; /* buffer for ablock */
3401 xfs_alloc_arg_t args
; /* allocation arguments */
3402 xfs_bmbt_rec_t
*arp
; /* child record pointer */
3403 xfs_bmbt_block_t
*block
; /* btree root block */
3404 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
3405 xfs_bmbt_rec_t
*ep
; /* extent record pointer */
3406 int error
; /* error return value */
3407 xfs_extnum_t i
, cnt
; /* extent record index */
3408 xfs_ifork_t
*ifp
; /* inode fork pointer */
3409 xfs_bmbt_key_t
*kp
; /* root block key pointer */
3410 xfs_mount_t
*mp
; /* mount structure */
3411 xfs_extnum_t nextents
; /* number of file extents */
3412 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
3414 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3415 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
3416 ASSERT(ifp
->if_ext_max
==
3417 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
3419 * Make space in the inode incore.
3421 xfs_iroot_realloc(ip
, 1, whichfork
);
3422 ifp
->if_flags
|= XFS_IFBROOT
;
3426 block
= ifp
->if_broot
;
3427 block
->bb_magic
= cpu_to_be32(XFS_BMAP_MAGIC
);
3428 block
->bb_level
= cpu_to_be16(1);
3429 block
->bb_numrecs
= cpu_to_be16(1);
3430 block
->bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
3431 block
->bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
3433 * Need a cursor. Can't allocate until bb_level is filled in.
3436 cur
= xfs_btree_init_cursor(mp
, tp
, NULL
, 0, XFS_BTNUM_BMAP
, ip
,
3438 cur
->bc_private
.b
.firstblock
= *firstblock
;
3439 cur
->bc_private
.b
.flist
= flist
;
3440 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
3442 * Convert to a btree with two levels, one record in root.
3444 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
3447 args
.firstblock
= *firstblock
;
3448 if (*firstblock
== NULLFSBLOCK
) {
3449 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3450 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
3451 } else if (flist
->xbf_low
) {
3452 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3453 args
.fsbno
= *firstblock
;
3455 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3456 args
.fsbno
= *firstblock
;
3458 args
.minlen
= args
.maxlen
= args
.prod
= 1;
3459 args
.total
= args
.minleft
= args
.alignment
= args
.mod
= args
.isfl
=
3460 args
.minalignslop
= 0;
3461 args
.wasdel
= wasdel
;
3463 if ((error
= xfs_alloc_vextent(&args
))) {
3464 xfs_iroot_realloc(ip
, -1, whichfork
);
3465 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
3469 * Allocation can't fail, the space was reserved.
3471 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
3472 ASSERT(*firstblock
== NULLFSBLOCK
||
3473 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
3475 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
3476 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
3477 cur
->bc_private
.b
.allocated
++;
3478 ip
->i_d
.di_nblocks
++;
3479 XFS_TRANS_MOD_DQUOT_BYINO(mp
, tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
3480 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
3482 * Fill in the child block.
3484 ablock
= XFS_BUF_TO_BMBT_BLOCK(abp
);
3485 ablock
->bb_magic
= cpu_to_be32(XFS_BMAP_MAGIC
);
3486 ablock
->bb_level
= 0;
3487 ablock
->bb_leftsib
= cpu_to_be64(NULLDFSBNO
);
3488 ablock
->bb_rightsib
= cpu_to_be64(NULLDFSBNO
);
3489 arp
= XFS_BMAP_REC_IADDR(ablock
, 1, cur
);
3490 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
3491 for (cnt
= i
= 0; i
< nextents
; i
++) {
3492 ep
= xfs_iext_get_ext(ifp
, i
);
3493 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep
))) {
3494 arp
->l0
= INT_GET(ep
->l0
, ARCH_CONVERT
);
3495 arp
->l1
= INT_GET(ep
->l1
, ARCH_CONVERT
);
3499 ASSERT(cnt
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
3500 ablock
->bb_numrecs
= cpu_to_be16(cnt
);
3502 * Fill in the root key and pointer.
3504 kp
= XFS_BMAP_KEY_IADDR(block
, 1, cur
);
3505 arp
= XFS_BMAP_REC_IADDR(ablock
, 1, cur
);
3506 kp
->br_startoff
= cpu_to_be64(xfs_bmbt_disk_get_startoff(arp
));
3507 pp
= XFS_BMAP_PTR_IADDR(block
, 1, cur
);
3508 *pp
= cpu_to_be64(args
.fsbno
);
3510 * Do all this logging at the end so that
3511 * the root is at the right level.
3513 xfs_bmbt_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
3514 xfs_bmbt_log_recs(cur
, abp
, 1, be16_to_cpu(ablock
->bb_numrecs
));
3515 ASSERT(*curp
== NULL
);
3517 *logflagsp
= XFS_ILOG_CORE
| XFS_ILOG_FBROOT(whichfork
);
3522 * Helper routine to reset inode di_forkoff field when switching
3523 * attribute fork from local to extent format - we reset it where
3524 * possible to make space available for inline data fork extents.
3527 xfs_bmap_forkoff_reset(
3532 if (whichfork
== XFS_ATTR_FORK
&&
3533 (ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
) &&
3534 (ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
) &&
3535 (ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) &&
3536 ((mp
->m_attroffset
>> 3) > ip
->i_d
.di_forkoff
)) {
3537 ip
->i_d
.di_forkoff
= mp
->m_attroffset
>> 3;
3538 ip
->i_df
.if_ext_max
= XFS_IFORK_DSIZE(ip
) /
3539 (uint
)sizeof(xfs_bmbt_rec_t
);
3540 ip
->i_afp
->if_ext_max
= XFS_IFORK_ASIZE(ip
) /
3541 (uint
)sizeof(xfs_bmbt_rec_t
);
3546 * Convert a local file to an extents file.
3547 * This code is out of bounds for data forks of regular files,
3548 * since the file data needs to get logged so things will stay consistent.
3549 * (The bmap-level manipulations are ok, though).
3551 STATIC
int /* error */
3552 xfs_bmap_local_to_extents(
3553 xfs_trans_t
*tp
, /* transaction pointer */
3554 xfs_inode_t
*ip
, /* incore inode pointer */
3555 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
3556 xfs_extlen_t total
, /* total blocks needed by transaction */
3557 int *logflagsp
, /* inode logging flags */
3558 int whichfork
) /* data or attr fork */
3560 int error
; /* error return value */
3561 int flags
; /* logging flags returned */
3562 #ifdef XFS_BMAP_TRACE
3563 static char fname
[] = "xfs_bmap_local_to_extents";
3565 xfs_ifork_t
*ifp
; /* inode fork pointer */
3568 * We don't want to deal with the case of keeping inode data inline yet.
3569 * So sending the data fork of a regular inode is invalid.
3571 ASSERT(!((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
&&
3572 whichfork
== XFS_DATA_FORK
));
3573 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3574 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
3577 if (ifp
->if_bytes
) {
3578 xfs_alloc_arg_t args
; /* allocation arguments */
3579 xfs_buf_t
*bp
; /* buffer for extent block */
3580 xfs_bmbt_rec_t
*ep
; /* extent record pointer */
3583 args
.mp
= ip
->i_mount
;
3584 args
.firstblock
= *firstblock
;
3585 ASSERT((ifp
->if_flags
&
3586 (XFS_IFINLINE
|XFS_IFEXTENTS
|XFS_IFEXTIREC
)) == XFS_IFINLINE
);
3588 * Allocate a block. We know we need only one, since the
3589 * file currently fits in an inode.
3591 if (*firstblock
== NULLFSBLOCK
) {
3592 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
3593 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3595 args
.fsbno
= *firstblock
;
3596 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3599 args
.mod
= args
.minleft
= args
.alignment
= args
.wasdel
=
3600 args
.isfl
= args
.minalignslop
= 0;
3601 args
.minlen
= args
.maxlen
= args
.prod
= 1;
3602 if ((error
= xfs_alloc_vextent(&args
)))
3605 * Can't fail, the space was reserved.
3607 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
3608 ASSERT(args
.len
== 1);
3609 *firstblock
= args
.fsbno
;
3610 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
3611 memcpy((char *)XFS_BUF_PTR(bp
), ifp
->if_u1
.if_data
,
3613 xfs_trans_log_buf(tp
, bp
, 0, ifp
->if_bytes
- 1);
3614 xfs_bmap_forkoff_reset(args
.mp
, ip
, whichfork
);
3615 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
3616 xfs_iext_add(ifp
, 0, 1);
3617 ep
= xfs_iext_get_ext(ifp
, 0);
3618 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
3619 xfs_bmap_trace_post_update(fname
, "new", ip
, 0, whichfork
);
3620 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
3621 ip
->i_d
.di_nblocks
= 1;
3622 XFS_TRANS_MOD_DQUOT_BYINO(args
.mp
, tp
, ip
,
3623 XFS_TRANS_DQ_BCOUNT
, 1L);
3624 flags
|= XFS_ILOG_FEXT(whichfork
);
3626 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
3627 xfs_bmap_forkoff_reset(ip
->i_mount
, ip
, whichfork
);
3629 ifp
->if_flags
&= ~XFS_IFINLINE
;
3630 ifp
->if_flags
|= XFS_IFEXTENTS
;
3631 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
3632 flags
|= XFS_ILOG_CORE
;
3639 * Search the extent records for the entry containing block bno.
3640 * If bno lies in a hole, point to the next entry. If bno lies
3641 * past eof, *eofp will be set, and *prevp will contain the last
3642 * entry (null if none). Else, *lastxp will be set to the index
3643 * of the found entry; *gotp will contain the entry.
3645 xfs_bmbt_rec_t
* /* pointer to found extent entry */
3646 xfs_bmap_search_multi_extents(
3647 xfs_ifork_t
*ifp
, /* inode fork pointer */
3648 xfs_fileoff_t bno
, /* block number searched for */
3649 int *eofp
, /* out: end of file found */
3650 xfs_extnum_t
*lastxp
, /* out: last extent index */
3651 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
3652 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
3654 xfs_bmbt_rec_t
*ep
; /* extent record pointer */
3655 xfs_extnum_t lastx
; /* last extent index */
3658 * Initialize the extent entry structure to catch access to
3659 * uninitialized br_startblock field.
3661 gotp
->br_startoff
= 0xffa5a5a5a5a5a5a5LL
;
3662 gotp
->br_blockcount
= 0xa55a5a5a5a5a5a5aLL
;
3663 gotp
->br_state
= XFS_EXT_INVALID
;
3665 gotp
->br_startblock
= 0xffffa5a5a5a5a5a5LL
;
3667 gotp
->br_startblock
= 0xffffa5a5;
3669 prevp
->br_startoff
= NULLFILEOFF
;
3671 ep
= xfs_iext_bno_to_ext(ifp
, bno
, &lastx
);
3673 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, lastx
- 1), prevp
);
3675 if (lastx
< (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
3676 xfs_bmbt_get_all(ep
, gotp
);
3690 * Search the extents list for the inode, for the extent containing bno.
3691 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3692 * *eofp will be set, and *prevp will contain the last entry (null if none).
3693 * Else, *lastxp will be set to the index of the found
3694 * entry; *gotp will contain the entry.
3696 STATIC xfs_bmbt_rec_t
* /* pointer to found extent entry */
3697 xfs_bmap_search_extents(
3698 xfs_inode_t
*ip
, /* incore inode pointer */
3699 xfs_fileoff_t bno
, /* block number searched for */
3700 int fork
, /* data or attr fork */
3701 int *eofp
, /* out: end of file found */
3702 xfs_extnum_t
*lastxp
, /* out: last extent index */
3703 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
3704 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
3706 xfs_ifork_t
*ifp
; /* inode fork pointer */
3707 xfs_bmbt_rec_t
*ep
; /* extent record pointer */
3709 XFS_STATS_INC(xs_look_exlist
);
3710 ifp
= XFS_IFORK_PTR(ip
, fork
);
3712 ep
= xfs_bmap_search_multi_extents(ifp
, bno
, eofp
, lastxp
, gotp
, prevp
);
3714 if (unlikely(!(gotp
->br_startblock
) && (*lastxp
!= NULLEXTNUM
) &&
3715 !(XFS_IS_REALTIME_INODE(ip
) && fork
== XFS_DATA_FORK
))) {
3716 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO
, CE_ALERT
, ip
->i_mount
,
3717 "Access to block zero in inode %llu "
3718 "start_block: %llx start_off: %llx "
3719 "blkcnt: %llx extent-state: %x lastx: %x\n",
3720 (unsigned long long)ip
->i_ino
,
3721 (unsigned long long)gotp
->br_startblock
,
3722 (unsigned long long)gotp
->br_startoff
,
3723 (unsigned long long)gotp
->br_blockcount
,
3724 gotp
->br_state
, *lastxp
);
3725 *lastxp
= NULLEXTNUM
;
3733 #ifdef XFS_BMAP_TRACE
3734 ktrace_t
*xfs_bmap_trace_buf
;
3737 * Add a bmap trace buffer entry. Base routine for the others.
3740 xfs_bmap_trace_addentry(
3741 int opcode
, /* operation */
3742 char *fname
, /* function name */
3743 char *desc
, /* operation description */
3744 xfs_inode_t
*ip
, /* incore inode pointer */
3745 xfs_extnum_t idx
, /* index of entry(ies) */
3746 xfs_extnum_t cnt
, /* count of entries, 1 or 2 */
3747 xfs_bmbt_rec_t
*r1
, /* first record */
3748 xfs_bmbt_rec_t
*r2
, /* second record or null */
3749 int whichfork
) /* data or attr fork */
3753 ASSERT(cnt
== 1 || cnt
== 2);
3758 memset(&tr2
, 0, sizeof(tr2
));
3761 ktrace_enter(xfs_bmap_trace_buf
,
3762 (void *)(__psint_t
)(opcode
| (whichfork
<< 16)),
3763 (void *)fname
, (void *)desc
, (void *)ip
,
3764 (void *)(__psint_t
)idx
,
3765 (void *)(__psint_t
)cnt
,
3766 (void *)(__psunsigned_t
)(ip
->i_ino
>> 32),
3767 (void *)(__psunsigned_t
)(unsigned)ip
->i_ino
,
3768 (void *)(__psunsigned_t
)(r1
->l0
>> 32),
3769 (void *)(__psunsigned_t
)(unsigned)(r1
->l0
),
3770 (void *)(__psunsigned_t
)(r1
->l1
>> 32),
3771 (void *)(__psunsigned_t
)(unsigned)(r1
->l1
),
3772 (void *)(__psunsigned_t
)(r2
->l0
>> 32),
3773 (void *)(__psunsigned_t
)(unsigned)(r2
->l0
),
3774 (void *)(__psunsigned_t
)(r2
->l1
>> 32),
3775 (void *)(__psunsigned_t
)(unsigned)(r2
->l1
)
3777 ASSERT(ip
->i_xtrace
);
3778 ktrace_enter(ip
->i_xtrace
,
3779 (void *)(__psint_t
)(opcode
| (whichfork
<< 16)),
3780 (void *)fname
, (void *)desc
, (void *)ip
,
3781 (void *)(__psint_t
)idx
,
3782 (void *)(__psint_t
)cnt
,
3783 (void *)(__psunsigned_t
)(ip
->i_ino
>> 32),
3784 (void *)(__psunsigned_t
)(unsigned)ip
->i_ino
,
3785 (void *)(__psunsigned_t
)(r1
->l0
>> 32),
3786 (void *)(__psunsigned_t
)(unsigned)(r1
->l0
),
3787 (void *)(__psunsigned_t
)(r1
->l1
>> 32),
3788 (void *)(__psunsigned_t
)(unsigned)(r1
->l1
),
3789 (void *)(__psunsigned_t
)(r2
->l0
>> 32),
3790 (void *)(__psunsigned_t
)(unsigned)(r2
->l0
),
3791 (void *)(__psunsigned_t
)(r2
->l1
>> 32),
3792 (void *)(__psunsigned_t
)(unsigned)(r2
->l1
)
3797 * Add bmap trace entry prior to a call to xfs_iext_remove.
3800 xfs_bmap_trace_delete(
3801 char *fname
, /* function name */
3802 char *desc
, /* operation description */
3803 xfs_inode_t
*ip
, /* incore inode pointer */
3804 xfs_extnum_t idx
, /* index of entry(entries) deleted */
3805 xfs_extnum_t cnt
, /* count of entries deleted, 1 or 2 */
3806 int whichfork
) /* data or attr fork */
3808 xfs_ifork_t
*ifp
; /* inode fork pointer */
3810 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3811 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE
, fname
, desc
, ip
, idx
,
3812 cnt
, xfs_iext_get_ext(ifp
, idx
),
3813 cnt
== 2 ? xfs_iext_get_ext(ifp
, idx
+ 1) : NULL
,
3818 * Add bmap trace entry prior to a call to xfs_iext_insert, or
3819 * reading in the extents list from the disk (in the btree).
3822 xfs_bmap_trace_insert(
3823 char *fname
, /* function name */
3824 char *desc
, /* operation description */
3825 xfs_inode_t
*ip
, /* incore inode pointer */
3826 xfs_extnum_t idx
, /* index of entry(entries) inserted */
3827 xfs_extnum_t cnt
, /* count of entries inserted, 1 or 2 */
3828 xfs_bmbt_irec_t
*r1
, /* inserted record 1 */
3829 xfs_bmbt_irec_t
*r2
, /* inserted record 2 or null */
3830 int whichfork
) /* data or attr fork */
3832 xfs_bmbt_rec_t tr1
; /* compressed record 1 */
3833 xfs_bmbt_rec_t tr2
; /* compressed record 2 if needed */
3835 xfs_bmbt_set_all(&tr1
, r1
);
3838 xfs_bmbt_set_all(&tr2
, r2
);
3843 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT
, fname
, desc
, ip
, idx
,
3844 cnt
, &tr1
, cnt
== 2 ? &tr2
: NULL
, whichfork
);
3848 * Add bmap trace entry after updating an extent record in place.
3851 xfs_bmap_trace_post_update(
3852 char *fname
, /* function name */
3853 char *desc
, /* operation description */
3854 xfs_inode_t
*ip
, /* incore inode pointer */
3855 xfs_extnum_t idx
, /* index of entry updated */
3856 int whichfork
) /* data or attr fork */
3858 xfs_ifork_t
*ifp
; /* inode fork pointer */
3860 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3861 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP
, fname
, desc
, ip
, idx
,
3862 1, xfs_iext_get_ext(ifp
, idx
), NULL
, whichfork
);
3866 * Add bmap trace entry prior to updating an extent record in place.
3869 xfs_bmap_trace_pre_update(
3870 char *fname
, /* function name */
3871 char *desc
, /* operation description */
3872 xfs_inode_t
*ip
, /* incore inode pointer */
3873 xfs_extnum_t idx
, /* index of entry to be updated */
3874 int whichfork
) /* data or attr fork */
3876 xfs_ifork_t
*ifp
; /* inode fork pointer */
3878 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3879 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP
, fname
, desc
, ip
, idx
, 1,
3880 xfs_iext_get_ext(ifp
, idx
), NULL
, whichfork
);
3882 #endif /* XFS_BMAP_TRACE */
3885 * Compute the worst-case number of indirect blocks that will be used
3886 * for ip's delayed extent of length "len".
3888 STATIC xfs_filblks_t
3889 xfs_bmap_worst_indlen(
3890 xfs_inode_t
*ip
, /* incore inode pointer */
3891 xfs_filblks_t len
) /* delayed extent length */
3893 int level
; /* btree level number */
3894 int maxrecs
; /* maximum record count at this level */
3895 xfs_mount_t
*mp
; /* mount structure */
3896 xfs_filblks_t rval
; /* return value */
3899 maxrecs
= mp
->m_bmap_dmxr
[0];
3900 for (level
= 0, rval
= 0;
3901 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
3904 do_div(len
, maxrecs
);
3907 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
3910 maxrecs
= mp
->m_bmap_dmxr
[1];
3915 #if defined(XFS_RW_TRACE)
3924 if (ip
->i_rwtrace
== NULL
)
3926 ktrace_enter(ip
->i_rwtrace
,
3927 (void *)(__psint_t
)XFS_BUNMAP
,
3929 (void *)(__psint_t
)((ip
->i_d
.di_size
>> 32) & 0xffffffff),
3930 (void *)(__psint_t
)(ip
->i_d
.di_size
& 0xffffffff),
3931 (void *)(__psint_t
)(((xfs_dfiloff_t
)bno
>> 32) & 0xffffffff),
3932 (void *)(__psint_t
)((xfs_dfiloff_t
)bno
& 0xffffffff),
3933 (void *)(__psint_t
)len
,
3934 (void *)(__psint_t
)flags
,
3935 (void *)(unsigned long)current_cpu(),
3947 * Convert inode from non-attributed to attributed.
3948 * Must not be in a transaction, ip must not be locked.
3950 int /* error code */
3951 xfs_bmap_add_attrfork(
3952 xfs_inode_t
*ip
, /* incore inode pointer */
3953 int size
, /* space new attribute needs */
3954 int rsvd
) /* xact may use reserved blks */
3956 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
3957 xfs_bmap_free_t flist
; /* freed extent records */
3958 xfs_mount_t
*mp
; /* mount structure */
3959 xfs_trans_t
*tp
; /* transaction pointer */
3960 unsigned long s
; /* spinlock spl value */
3961 int blks
; /* space reservation */
3962 int version
= 1; /* superblock attr version */
3963 int committed
; /* xaction was committed */
3964 int logflags
; /* logging flags */
3965 int error
; /* error return value */
3967 ASSERT(XFS_IFORK_Q(ip
) == 0);
3968 ASSERT(ip
->i_df
.if_ext_max
==
3969 XFS_IFORK_DSIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
));
3972 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
3973 tp
= xfs_trans_alloc(mp
, XFS_TRANS_ADDAFORK
);
3974 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
3976 tp
->t_flags
|= XFS_TRANS_RESERVE
;
3977 if ((error
= xfs_trans_reserve(tp
, blks
, XFS_ADDAFORK_LOG_RES(mp
), 0,
3978 XFS_TRANS_PERM_LOG_RES
, XFS_ADDAFORK_LOG_COUNT
)))
3980 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3981 error
= XFS_TRANS_RESERVE_QUOTA_NBLKS(mp
, tp
, ip
, blks
, 0, rsvd
?
3982 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
3983 XFS_QMOPT_RES_REGBLKS
);
3985 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3986 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
);
3989 if (XFS_IFORK_Q(ip
))
3991 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
3993 * For inodes coming from pre-6.2 filesystems.
3995 ASSERT(ip
->i_d
.di_aformat
== 0);
3996 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
3998 ASSERT(ip
->i_d
.di_anextents
== 0);
3999 VN_HOLD(XFS_ITOV(ip
));
4000 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
4001 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4002 switch (ip
->i_d
.di_format
) {
4003 case XFS_DINODE_FMT_DEV
:
4004 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
4006 case XFS_DINODE_FMT_UUID
:
4007 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
4009 case XFS_DINODE_FMT_LOCAL
:
4010 case XFS_DINODE_FMT_EXTENTS
:
4011 case XFS_DINODE_FMT_BTREE
:
4012 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
4013 if (!ip
->i_d
.di_forkoff
)
4014 ip
->i_d
.di_forkoff
= mp
->m_attroffset
>> 3;
4015 else if (mp
->m_flags
& XFS_MOUNT_ATTR2
)
4020 error
= XFS_ERROR(EINVAL
);
4023 ip
->i_df
.if_ext_max
=
4024 XFS_IFORK_DSIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
);
4025 ASSERT(ip
->i_afp
== NULL
);
4026 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
4027 ip
->i_afp
->if_ext_max
=
4028 XFS_IFORK_ASIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
);
4029 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
4031 XFS_BMAP_INIT(&flist
, &firstblock
);
4032 switch (ip
->i_d
.di_format
) {
4033 case XFS_DINODE_FMT_LOCAL
:
4034 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &flist
,
4037 case XFS_DINODE_FMT_EXTENTS
:
4038 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
4041 case XFS_DINODE_FMT_BTREE
:
4042 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &flist
,
4050 xfs_trans_log_inode(tp
, ip
, logflags
);
4053 if (!XFS_SB_VERSION_HASATTR(&mp
->m_sb
) ||
4054 (!XFS_SB_VERSION_HASATTR2(&mp
->m_sb
) && version
== 2)) {
4055 __int64_t sbfields
= 0;
4057 s
= XFS_SB_LOCK(mp
);
4058 if (!XFS_SB_VERSION_HASATTR(&mp
->m_sb
)) {
4059 XFS_SB_VERSION_ADDATTR(&mp
->m_sb
);
4060 sbfields
|= XFS_SB_VERSIONNUM
;
4062 if (!XFS_SB_VERSION_HASATTR2(&mp
->m_sb
) && version
== 2) {
4063 XFS_SB_VERSION_ADDATTR2(&mp
->m_sb
);
4064 sbfields
|= (XFS_SB_VERSIONNUM
| XFS_SB_FEATURES2
);
4067 XFS_SB_UNLOCK(mp
, s
);
4068 xfs_mod_sb(tp
, sbfields
);
4070 XFS_SB_UNLOCK(mp
, s
);
4072 if ((error
= xfs_bmap_finish(&tp
, &flist
, &committed
)))
4074 error
= xfs_trans_commit(tp
, XFS_TRANS_PERM_LOG_RES
, NULL
);
4075 ASSERT(ip
->i_df
.if_ext_max
==
4076 XFS_IFORK_DSIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
));
4079 xfs_bmap_cancel(&flist
);
4081 ASSERT(ismrlocked(&ip
->i_lock
,MR_UPDATE
));
4082 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
4084 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
4085 ASSERT(ip
->i_df
.if_ext_max
==
4086 XFS_IFORK_DSIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
));
4091 * Add the extent to the list of extents to be free at transaction end.
4092 * The list is maintained sorted (by block number).
4097 xfs_fsblock_t bno
, /* fs block number of extent */
4098 xfs_filblks_t len
, /* length of extent */
4099 xfs_bmap_free_t
*flist
, /* list of extents */
4100 xfs_mount_t
*mp
) /* mount point structure */
4102 xfs_bmap_free_item_t
*cur
; /* current (next) element */
4103 xfs_bmap_free_item_t
*new; /* new element */
4104 xfs_bmap_free_item_t
*prev
; /* previous element */
4106 xfs_agnumber_t agno
;
4107 xfs_agblock_t agbno
;
4109 ASSERT(bno
!= NULLFSBLOCK
);
4111 ASSERT(len
<= MAXEXTLEN
);
4112 ASSERT(!ISNULLSTARTBLOCK(bno
));
4113 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
4114 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
4115 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
4116 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
4117 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
4118 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
4120 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
4121 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
4122 new->xbfi_startblock
= bno
;
4123 new->xbfi_blockcount
= (xfs_extlen_t
)len
;
4124 for (prev
= NULL
, cur
= flist
->xbf_first
;
4126 prev
= cur
, cur
= cur
->xbfi_next
) {
4127 if (cur
->xbfi_startblock
>= bno
)
4131 prev
->xbfi_next
= new;
4133 flist
->xbf_first
= new;
4134 new->xbfi_next
= cur
;
4139 * Compute and fill in the value of the maximum depth of a bmap btree
4140 * in this filesystem. Done once, during mount.
4143 xfs_bmap_compute_maxlevels(
4144 xfs_mount_t
*mp
, /* file system mount structure */
4145 int whichfork
) /* data or attr fork */
4147 int level
; /* btree level */
4148 uint maxblocks
; /* max blocks at this level */
4149 uint maxleafents
; /* max leaf entries possible */
4150 int maxrootrecs
; /* max records in root block */
4151 int minleafrecs
; /* min records in leaf block */
4152 int minnoderecs
; /* min records in node block */
4153 int sz
; /* root block size */
4156 * The maximum number of extents in a file, hence the maximum
4157 * number of leaf entries, is controlled by the type of di_nextents
4158 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
4159 * (a signed 16-bit number, xfs_aextnum_t).
4161 if (whichfork
== XFS_DATA_FORK
) {
4162 maxleafents
= MAXEXTNUM
;
4163 sz
= (mp
->m_flags
& XFS_MOUNT_ATTR2
) ?
4164 XFS_BMDR_SPACE_CALC(MINDBTPTRS
) : mp
->m_attroffset
;
4166 maxleafents
= MAXAEXTNUM
;
4167 sz
= (mp
->m_flags
& XFS_MOUNT_ATTR2
) ?
4168 XFS_BMDR_SPACE_CALC(MINABTPTRS
) :
4169 mp
->m_sb
.sb_inodesize
- mp
->m_attroffset
;
4171 maxrootrecs
= (int)XFS_BTREE_BLOCK_MAXRECS(sz
, xfs_bmdr
, 0);
4172 minleafrecs
= mp
->m_bmap_dmnr
[0];
4173 minnoderecs
= mp
->m_bmap_dmnr
[1];
4174 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
4175 for (level
= 1; maxblocks
> 1; level
++) {
4176 if (maxblocks
<= maxrootrecs
)
4179 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
4181 mp
->m_bm_maxlevels
[whichfork
] = level
;
4185 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
4186 * caller. Frees all the extents that need freeing, which must be done
4187 * last due to locking considerations. We never free any extents in
4188 * the first transaction. This is to allow the caller to make the first
4189 * transaction a synchronous one so that the pointers to the data being
4190 * broken in this transaction will be permanent before the data is actually
4191 * freed. This is necessary to prevent blocks from being reallocated
4192 * and written to before the free and reallocation are actually permanent.
4193 * We do not just make the first transaction synchronous here, because
4194 * there are more efficient ways to gain the same protection in some cases
4195 * (see the file truncation code).
4197 * Return 1 if the given transaction was committed and a new one
4198 * started, and 0 otherwise in the committed parameter.
4203 xfs_trans_t
**tp
, /* transaction pointer addr */
4204 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
4205 int *committed
) /* xact committed or not */
4207 xfs_efd_log_item_t
*efd
; /* extent free data */
4208 xfs_efi_log_item_t
*efi
; /* extent free intention */
4209 int error
; /* error return value */
4210 xfs_bmap_free_item_t
*free
; /* free extent item */
4211 unsigned int logres
; /* new log reservation */
4212 unsigned int logcount
; /* new log count */
4213 xfs_mount_t
*mp
; /* filesystem mount structure */
4214 xfs_bmap_free_item_t
*next
; /* next item on free list */
4215 xfs_trans_t
*ntp
; /* new transaction pointer */
4217 ASSERT((*tp
)->t_flags
& XFS_TRANS_PERM_LOG_RES
);
4218 if (flist
->xbf_count
== 0) {
4223 efi
= xfs_trans_get_efi(ntp
, flist
->xbf_count
);
4224 for (free
= flist
->xbf_first
; free
; free
= free
->xbfi_next
)
4225 xfs_trans_log_efi_extent(ntp
, efi
, free
->xbfi_startblock
,
4226 free
->xbfi_blockcount
);
4227 logres
= ntp
->t_log_res
;
4228 logcount
= ntp
->t_log_count
;
4229 ntp
= xfs_trans_dup(*tp
);
4230 error
= xfs_trans_commit(*tp
, 0, NULL
);
4234 * We have a new transaction, so we should return committed=1,
4235 * even though we're returning an error.
4240 if ((error
= xfs_trans_reserve(ntp
, 0, logres
, 0, XFS_TRANS_PERM_LOG_RES
,
4243 efd
= xfs_trans_get_efd(ntp
, efi
, flist
->xbf_count
);
4244 for (free
= flist
->xbf_first
; free
!= NULL
; free
= next
) {
4245 next
= free
->xbfi_next
;
4246 if ((error
= xfs_free_extent(ntp
, free
->xbfi_startblock
,
4247 free
->xbfi_blockcount
))) {
4249 * The bmap free list will be cleaned up at a
4250 * higher level. The EFI will be canceled when
4251 * this transaction is aborted.
4252 * Need to force shutdown here to make sure it
4253 * happens, since this transaction may not be
4257 if (!XFS_FORCED_SHUTDOWN(mp
))
4258 xfs_force_shutdown(mp
,
4259 (error
== EFSCORRUPTED
) ?
4260 SHUTDOWN_CORRUPT_INCORE
:
4261 SHUTDOWN_META_IO_ERROR
);
4264 xfs_trans_log_efd_extent(ntp
, efd
, free
->xbfi_startblock
,
4265 free
->xbfi_blockcount
);
4266 xfs_bmap_del_free(flist
, NULL
, free
);
4272 * Free up any items left in the list.
4276 xfs_bmap_free_t
*flist
) /* list of bmap_free_items */
4278 xfs_bmap_free_item_t
*free
; /* free list item */
4279 xfs_bmap_free_item_t
*next
;
4281 if (flist
->xbf_count
== 0)
4283 ASSERT(flist
->xbf_first
!= NULL
);
4284 for (free
= flist
->xbf_first
; free
; free
= next
) {
4285 next
= free
->xbfi_next
;
4286 xfs_bmap_del_free(flist
, NULL
, free
);
4288 ASSERT(flist
->xbf_count
== 0);
4292 * Returns the file-relative block number of the first unused block(s)
4293 * in the file with at least "len" logically contiguous blocks free.
4294 * This is the lowest-address hole if the file has holes, else the first block
4295 * past the end of file.
4296 * Return 0 if the file is currently local (in-inode).
4299 xfs_bmap_first_unused(
4300 xfs_trans_t
*tp
, /* transaction pointer */
4301 xfs_inode_t
*ip
, /* incore inode */
4302 xfs_extlen_t len
, /* size of hole to find */
4303 xfs_fileoff_t
*first_unused
, /* unused block */
4304 int whichfork
) /* data or attr fork */
4306 xfs_bmbt_rec_t
*ep
; /* pointer to an extent entry */
4307 int error
; /* error return value */
4308 int idx
; /* extent record index */
4309 xfs_ifork_t
*ifp
; /* inode fork pointer */
4310 xfs_fileoff_t lastaddr
; /* last block number seen */
4311 xfs_fileoff_t lowest
; /* lowest useful block */
4312 xfs_fileoff_t max
; /* starting useful block */
4313 xfs_fileoff_t off
; /* offset for this block */
4314 xfs_extnum_t nextents
; /* number of extent entries */
4316 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
4317 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
4318 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
4319 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
4323 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4324 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
4325 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
4327 lowest
= *first_unused
;
4328 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
4329 for (idx
= 0, lastaddr
= 0, max
= lowest
; idx
< nextents
; idx
++) {
4330 ep
= xfs_iext_get_ext(ifp
, idx
);
4331 off
= xfs_bmbt_get_startoff(ep
);
4333 * See if the hole before this extent will work.
4335 if (off
>= lowest
+ len
&& off
- max
>= len
) {
4336 *first_unused
= max
;
4339 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
4340 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
4342 *first_unused
= max
;
4347 * Returns the file-relative block number of the last block + 1 before
4348 * last_block (input value) in the file.
4349 * This is not based on i_size, it is based on the extent records.
4350 * Returns 0 for local files, as they do not have extent records.
4353 xfs_bmap_last_before(
4354 xfs_trans_t
*tp
, /* transaction pointer */
4355 xfs_inode_t
*ip
, /* incore inode */
4356 xfs_fileoff_t
*last_block
, /* last block */
4357 int whichfork
) /* data or attr fork */
4359 xfs_fileoff_t bno
; /* input file offset */
4360 int eof
; /* hit end of file */
4361 xfs_bmbt_rec_t
*ep
; /* pointer to last extent */
4362 int error
; /* error return value */
4363 xfs_bmbt_irec_t got
; /* current extent value */
4364 xfs_ifork_t
*ifp
; /* inode fork pointer */
4365 xfs_extnum_t lastx
; /* last extent used */
4366 xfs_bmbt_irec_t prev
; /* previous extent value */
4368 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
4369 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4370 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
4371 return XFS_ERROR(EIO
);
4372 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
4376 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4377 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
4378 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
4380 bno
= *last_block
- 1;
4381 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
4383 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
4384 if (prev
.br_startoff
== NULLFILEOFF
)
4387 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
4390 * Otherwise *last_block is already the right answer.
4396 * Returns the file-relative block number of the first block past eof in
4397 * the file. This is not based on i_size, it is based on the extent records.
4398 * Returns 0 for local files, as they do not have extent records.
4401 xfs_bmap_last_offset(
4402 xfs_trans_t
*tp
, /* transaction pointer */
4403 xfs_inode_t
*ip
, /* incore inode */
4404 xfs_fileoff_t
*last_block
, /* last block */
4405 int whichfork
) /* data or attr fork */
4407 xfs_bmbt_rec_t
*ep
; /* pointer to last extent */
4408 int error
; /* error return value */
4409 xfs_ifork_t
*ifp
; /* inode fork pointer */
4410 xfs_extnum_t nextents
; /* number of extent entries */
4412 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
4413 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4414 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
4415 return XFS_ERROR(EIO
);
4416 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
4420 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4421 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
4422 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
4424 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
4429 ep
= xfs_iext_get_ext(ifp
, nextents
- 1);
4430 *last_block
= xfs_bmbt_get_startoff(ep
) + xfs_bmbt_get_blockcount(ep
);
4435 * Returns whether the selected fork of the inode has exactly one
4436 * block or not. For the data fork we check this matches di_size,
4437 * implying the file's range is 0..bsize-1.
4439 int /* 1=>1 block, 0=>otherwise */
4441 xfs_inode_t
*ip
, /* incore inode */
4442 int whichfork
) /* data or attr fork */
4444 xfs_bmbt_rec_t
*ep
; /* ptr to fork's extent */
4445 xfs_ifork_t
*ifp
; /* inode fork pointer */
4446 int rval
; /* return value */
4447 xfs_bmbt_irec_t s
; /* internal version of extent */
4450 if (whichfork
== XFS_DATA_FORK
)
4451 return ip
->i_d
.di_size
== ip
->i_mount
->m_sb
.sb_blocksize
;
4453 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
4455 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4457 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4458 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
4459 ep
= xfs_iext_get_ext(ifp
, 0);
4460 xfs_bmbt_get_all(ep
, &s
);
4461 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
4462 if (rval
&& whichfork
== XFS_DATA_FORK
)
4463 ASSERT(ip
->i_d
.di_size
== ip
->i_mount
->m_sb
.sb_blocksize
);
4468 * Read in the extents to if_extents.
4469 * All inode fields are set up by caller, we just traverse the btree
4470 * and copy the records in. If the file system cannot contain unwritten
4471 * extents, the records are checked for no "state" flags.
4474 xfs_bmap_read_extents(
4475 xfs_trans_t
*tp
, /* transaction pointer */
4476 xfs_inode_t
*ip
, /* incore inode */
4477 int whichfork
) /* data or attr fork */
4479 xfs_bmbt_block_t
*block
; /* current btree block */
4480 xfs_fsblock_t bno
; /* block # of "block" */
4481 xfs_buf_t
*bp
; /* buffer for "block" */
4482 int error
; /* error return value */
4483 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
4484 #ifdef XFS_BMAP_TRACE
4485 static char fname
[] = "xfs_bmap_read_extents";
4487 xfs_extnum_t i
, j
; /* index into the extents list */
4488 xfs_ifork_t
*ifp
; /* fork structure */
4489 int level
; /* btree level, for checking */
4490 xfs_mount_t
*mp
; /* file system mount structure */
4491 __be64
*pp
; /* pointer to block address */
4493 xfs_extnum_t room
; /* number of entries there's room for */
4497 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4498 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
4499 XFS_EXTFMT_INODE(ip
);
4500 block
= ifp
->if_broot
;
4502 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4504 level
= be16_to_cpu(block
->bb_level
);
4506 pp
= XFS_BMAP_BROOT_PTR_ADDR(block
, 1, ifp
->if_broot_bytes
);
4507 bno
= be64_to_cpu(*pp
);
4508 ASSERT(bno
!= NULLDFSBNO
);
4509 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
4510 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
4512 * Go down the tree until leaf level is reached, following the first
4513 * pointer (leftmost) at each level.
4515 while (level
-- > 0) {
4516 if ((error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
4517 XFS_BMAP_BTREE_REF
)))
4519 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
4520 XFS_WANT_CORRUPTED_GOTO(
4521 XFS_BMAP_SANITY_CHECK(mp
, block
, level
),
4525 pp
= XFS_BTREE_PTR_ADDR(xfs_bmbt
, block
, 1, mp
->m_bmap_dmxr
[1]);
4526 bno
= be64_to_cpu(*pp
);
4527 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
4528 xfs_trans_brelse(tp
, bp
);
4531 * Here with bp and block set to the leftmost leaf node in the tree.
4533 room
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
4536 * Loop over all leaf nodes. Copy information to the extent records.
4539 xfs_bmbt_rec_t
*frp
, *trp
;
4540 xfs_fsblock_t nextbno
;
4541 xfs_extnum_t num_recs
;
4545 num_recs
= be16_to_cpu(block
->bb_numrecs
);
4546 if (unlikely(i
+ num_recs
> room
)) {
4547 ASSERT(i
+ num_recs
<= room
);
4548 xfs_fs_repair_cmn_err(CE_WARN
, ip
->i_mount
,
4549 "corrupt dinode %Lu, (btree extents).",
4550 (unsigned long long) ip
->i_ino
);
4551 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4556 XFS_WANT_CORRUPTED_GOTO(
4557 XFS_BMAP_SANITY_CHECK(mp
, block
, 0),
4560 * Read-ahead the next leaf block, if any.
4562 nextbno
= be64_to_cpu(block
->bb_rightsib
);
4563 if (nextbno
!= NULLFSBLOCK
)
4564 xfs_btree_reada_bufl(mp
, nextbno
, 1);
4566 * Copy records into the extent records.
4568 frp
= XFS_BTREE_REC_ADDR(xfs_bmbt
, block
, 1);
4570 for (j
= 0; j
< num_recs
; j
++, i
++, frp
++) {
4571 trp
= xfs_iext_get_ext(ifp
, i
);
4572 trp
->l0
= INT_GET(frp
->l0
, ARCH_CONVERT
);
4573 trp
->l1
= INT_GET(frp
->l1
, ARCH_CONVERT
);
4575 if (exntf
== XFS_EXTFMT_NOSTATE
) {
4577 * Check all attribute bmap btree records and
4578 * any "older" data bmap btree records for a
4579 * set bit in the "extent flag" position.
4581 if (unlikely(xfs_check_nostate_extents(ifp
,
4582 start
, num_recs
))) {
4583 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4589 xfs_trans_brelse(tp
, bp
);
4592 * If we've reached the end, stop.
4594 if (bno
== NULLFSBLOCK
)
4596 if ((error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
4597 XFS_BMAP_BTREE_REF
)))
4599 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
4601 ASSERT(i
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
4602 ASSERT(i
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
4603 xfs_bmap_trace_exlist(fname
, ip
, i
, whichfork
);
4606 xfs_trans_brelse(tp
, bp
);
4607 return XFS_ERROR(EFSCORRUPTED
);
4610 #ifdef XFS_BMAP_TRACE
4612 * Add bmap trace insert entries for all the contents of the extent records.
4615 xfs_bmap_trace_exlist(
4616 char *fname
, /* function name */
4617 xfs_inode_t
*ip
, /* incore inode pointer */
4618 xfs_extnum_t cnt
, /* count of entries in the list */
4619 int whichfork
) /* data or attr fork */
4621 xfs_bmbt_rec_t
*ep
; /* current extent record */
4622 xfs_extnum_t idx
; /* extent record index */
4623 xfs_ifork_t
*ifp
; /* inode fork pointer */
4624 xfs_bmbt_irec_t s
; /* file extent record */
4626 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4627 ASSERT(cnt
== (ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
)));
4628 for (idx
= 0; idx
< cnt
; idx
++) {
4629 ep
= xfs_iext_get_ext(ifp
, idx
);
4630 xfs_bmbt_get_all(ep
, &s
);
4631 xfs_bmap_trace_insert(fname
, "exlist", ip
, idx
, 1, &s
, NULL
,
4639 * Validate that the bmbt_irecs being returned from bmapi are valid
4640 * given the callers original parameters. Specifically check the
4641 * ranges of the returned irecs to ensure that they only extent beyond
4642 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4645 xfs_bmap_validate_ret(
4649 xfs_bmbt_irec_t
*mval
,
4653 int i
; /* index to map values */
4655 ASSERT(ret_nmap
<= nmap
);
4657 for (i
= 0; i
< ret_nmap
; i
++) {
4658 ASSERT(mval
[i
].br_blockcount
> 0);
4659 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
4660 ASSERT(mval
[i
].br_startoff
>= bno
);
4661 ASSERT(mval
[i
].br_blockcount
<= len
);
4662 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
4665 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
4666 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
4670 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
4671 mval
[i
].br_startoff
);
4672 if ((flags
& XFS_BMAPI_WRITE
) && !(flags
& XFS_BMAPI_DELAY
))
4673 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
4674 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
4675 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
4676 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
4683 * Map file blocks to filesystem blocks.
4684 * File range is given by the bno/len pair.
4685 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4686 * into a hole or past eof.
4687 * Only allocates blocks from a single allocation group,
4688 * to avoid locking problems.
4689 * The returned value in "firstblock" from the first call in a transaction
4690 * must be remembered and presented to subsequent calls in "firstblock".
4691 * An upper bound for the number of blocks to be allocated is supplied to
4692 * the first call in "total"; if no allocation group has that many free
4693 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4697 xfs_trans_t
*tp
, /* transaction pointer */
4698 xfs_inode_t
*ip
, /* incore inode */
4699 xfs_fileoff_t bno
, /* starting file offs. mapped */
4700 xfs_filblks_t len
, /* length to map in file */
4701 int flags
, /* XFS_BMAPI_... */
4702 xfs_fsblock_t
*firstblock
, /* first allocated block
4703 controls a.g. for allocs */
4704 xfs_extlen_t total
, /* total blocks needed */
4705 xfs_bmbt_irec_t
*mval
, /* output: map values */
4706 int *nmap
, /* i/o: mval size/count */
4707 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
4708 xfs_extdelta_t
*delta
) /* o: change made to incore extents */
4710 xfs_fsblock_t abno
; /* allocated block number */
4711 xfs_extlen_t alen
; /* allocated extent length */
4712 xfs_fileoff_t aoff
; /* allocated file offset */
4713 xfs_bmalloca_t bma
; /* args for xfs_bmap_alloc */
4714 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
4715 xfs_fileoff_t end
; /* end of mapped file region */
4716 int eof
; /* we've hit the end of extents */
4717 xfs_bmbt_rec_t
*ep
; /* extent record pointer */
4718 int error
; /* error return */
4719 xfs_bmbt_irec_t got
; /* current file extent record */
4720 xfs_ifork_t
*ifp
; /* inode fork pointer */
4721 xfs_extlen_t indlen
; /* indirect blocks length */
4722 xfs_extnum_t lastx
; /* last useful extent number */
4723 int logflags
; /* flags for transaction logging */
4724 xfs_extlen_t minleft
; /* min blocks left after allocation */
4725 xfs_extlen_t minlen
; /* min allocation size */
4726 xfs_mount_t
*mp
; /* xfs mount structure */
4727 int n
; /* current extent index */
4728 int nallocs
; /* number of extents alloc\'d */
4729 xfs_extnum_t nextents
; /* number of extents in file */
4730 xfs_fileoff_t obno
; /* old block number (offset) */
4731 xfs_bmbt_irec_t prev
; /* previous file extent record */
4732 int tmp_logflags
; /* temp flags holder */
4733 int whichfork
; /* data or attr fork */
4734 char inhole
; /* current location is hole in file */
4735 char wasdelay
; /* old extent was delayed */
4736 char wr
; /* this is a write request */
4737 char rt
; /* this is a realtime file */
4739 xfs_fileoff_t orig_bno
; /* original block number value */
4740 int orig_flags
; /* original flags arg value */
4741 xfs_filblks_t orig_len
; /* original value of len arg */
4742 xfs_bmbt_irec_t
*orig_mval
; /* original value of mval */
4743 int orig_nmap
; /* original value of *nmap */
4752 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
|| !(flags
& XFS_BMAPI_WRITE
));
4753 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4754 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4756 if (unlikely(XFS_TEST_ERROR(
4757 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4758 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
4759 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
),
4760 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4761 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW
, mp
);
4762 return XFS_ERROR(EFSCORRUPTED
);
4764 if (XFS_FORCED_SHUTDOWN(mp
))
4765 return XFS_ERROR(EIO
);
4766 rt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
4767 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4768 ASSERT(ifp
->if_ext_max
==
4769 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
4770 if ((wr
= (flags
& XFS_BMAPI_WRITE
)) != 0)
4771 XFS_STATS_INC(xs_blk_mapw
);
4773 XFS_STATS_INC(xs_blk_mapr
);
4775 * IGSTATE flag is used to combine extents which
4776 * differ only due to the state of the extents.
4777 * This technique is used from xfs_getbmap()
4778 * when the caller does not wish to see the
4779 * separation (which is the default).
4781 * This technique is also used when writing a
4782 * buffer which has been partially written,
4783 * (usually by being flushed during a chunkread),
4784 * to ensure one write takes place. This also
4785 * prevents a change in the xfs inode extents at
4786 * this time, intentionally. This change occurs
4787 * on completion of the write operation, in
4788 * xfs_strat_comp(), where the xfs_bmapi() call
4789 * is transactioned, and the extents combined.
4791 if ((flags
& XFS_BMAPI_IGSTATE
) && wr
) /* if writing unwritten space */
4792 wr
= 0; /* no allocations are allowed */
4793 ASSERT(wr
|| !(flags
& XFS_BMAPI_DELAY
));
4797 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
4799 if ((error
= xfs_bmap_local_to_extents(tp
, ip
,
4800 firstblock
, total
, &logflags
, whichfork
)))
4803 if (wr
&& *firstblock
== NULLFSBLOCK
) {
4804 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4805 minleft
= be16_to_cpu(ifp
->if_broot
->bb_level
) + 1;
4810 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
4811 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
4813 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
4815 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
4821 delta
->xed_startoff
= NULLFILEOFF
;
4822 delta
->xed_blockcount
= 0;
4824 while (bno
< end
&& n
< *nmap
) {
4826 * Reading past eof, act as though there's a hole
4830 got
.br_startoff
= end
;
4831 inhole
= eof
|| got
.br_startoff
> bno
;
4832 wasdelay
= wr
&& !inhole
&& !(flags
& XFS_BMAPI_DELAY
) &&
4833 ISNULLSTARTBLOCK(got
.br_startblock
);
4835 * First, deal with the hole before the allocated space
4836 * that we found, if any.
4838 if (wr
&& (inhole
|| wasdelay
)) {
4840 * For the wasdelay case, we could also just
4841 * allocate the stuff asked for in this bmap call
4842 * but that wouldn't be as good.
4844 if (wasdelay
&& !(flags
& XFS_BMAPI_EXACT
)) {
4845 alen
= (xfs_extlen_t
)got
.br_blockcount
;
4846 aoff
= got
.br_startoff
;
4847 if (lastx
!= NULLEXTNUM
&& lastx
) {
4848 ep
= xfs_iext_get_ext(ifp
, lastx
- 1);
4849 xfs_bmbt_get_all(ep
, &prev
);
4851 } else if (wasdelay
) {
4852 alen
= (xfs_extlen_t
)
4853 XFS_FILBLKS_MIN(len
,
4855 got
.br_blockcount
) - bno
);
4858 alen
= (xfs_extlen_t
)
4859 XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
4861 alen
= (xfs_extlen_t
)
4862 XFS_FILBLKS_MIN(alen
,
4863 got
.br_startoff
- bno
);
4866 minlen
= (flags
& XFS_BMAPI_CONTIG
) ? alen
: 1;
4867 if (flags
& XFS_BMAPI_DELAY
) {
4870 /* Figure out the extent size, adjust alen */
4872 if (!(extsz
= ip
->i_d
.di_extsize
))
4873 extsz
= mp
->m_sb
.sb_rextsize
;
4875 extsz
= ip
->i_d
.di_extsize
;
4878 error
= xfs_bmap_extsize_align(mp
,
4881 flags
&XFS_BMAPI_DELAY
,
4882 flags
&XFS_BMAPI_CONVERT
,
4888 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4891 * Make a transaction-less quota reservation for
4892 * delayed allocation blocks. This number gets
4893 * adjusted later. We return if we haven't
4894 * allocated blocks already inside this loop.
4896 if ((error
= XFS_TRANS_RESERVE_QUOTA_NBLKS(
4897 mp
, NULL
, ip
, (long)alen
, 0,
4898 rt
? XFS_QMOPT_RES_RTBLKS
:
4899 XFS_QMOPT_RES_REGBLKS
))) {
4902 ASSERT(cur
== NULL
);
4909 * Split changing sb for alen and indlen since
4910 * they could be coming from different places.
4912 indlen
= (xfs_extlen_t
)
4913 xfs_bmap_worst_indlen(ip
, alen
);
4917 error
= xfs_mod_incore_sb(mp
,
4919 -((int64_t)extsz
), (flags
&
4920 XFS_BMAPI_RSVBLOCKS
));
4922 error
= xfs_mod_incore_sb(mp
,
4924 -((int64_t)alen
), (flags
&
4925 XFS_BMAPI_RSVBLOCKS
));
4928 error
= xfs_mod_incore_sb(mp
,
4930 -((int64_t)indlen
), (flags
&
4931 XFS_BMAPI_RSVBLOCKS
));
4933 xfs_mod_incore_sb(mp
,
4935 (int64_t)extsz
, (flags
&
4936 XFS_BMAPI_RSVBLOCKS
));
4938 xfs_mod_incore_sb(mp
,
4940 (int64_t)alen
, (flags
&
4941 XFS_BMAPI_RSVBLOCKS
));
4945 if (XFS_IS_QUOTA_ON(mp
))
4946 /* unreserve the blocks now */
4948 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
4951 XFS_QMOPT_RES_RTBLKS
:
4952 XFS_QMOPT_RES_REGBLKS
);
4956 ip
->i_delayed_blks
+= alen
;
4957 abno
= NULLSTARTBLOCK(indlen
);
4960 * If first time, allocate and fill in
4961 * once-only bma fields.
4963 if (bma
.ip
== NULL
) {
4971 /* Indicate if this is the first user data
4972 * in the file, or just any user data.
4974 if (!(flags
& XFS_BMAPI_METADATA
)) {
4975 bma
.userdata
= (aoff
== 0) ?
4976 XFS_ALLOC_INITIAL_USER_DATA
:
4980 * Fill in changeable bma fields.
4983 bma
.firstblock
= *firstblock
;
4986 bma
.conv
= !!(flags
& XFS_BMAPI_CONVERT
);
4987 bma
.wasdel
= wasdelay
;
4988 bma
.minlen
= minlen
;
4989 bma
.low
= flist
->xbf_low
;
4990 bma
.minleft
= minleft
;
4992 * Only want to do the alignment at the
4993 * eof if it is userdata and allocation length
4994 * is larger than a stripe unit.
4996 if (mp
->m_dalign
&& alen
>= mp
->m_dalign
&&
4997 (!(flags
& XFS_BMAPI_METADATA
)) &&
4998 (whichfork
== XFS_DATA_FORK
)) {
4999 if ((error
= xfs_bmap_isaeof(ip
, aoff
,
5000 whichfork
, &bma
.aeof
)))
5007 if ((error
= xfs_bmap_alloc(&bma
)))
5010 * Copy out result fields.
5013 if ((flist
->xbf_low
= bma
.low
))
5017 ASSERT(*firstblock
== NULLFSBLOCK
||
5018 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
5019 XFS_FSB_TO_AGNO(mp
, bma
.firstblock
) ||
5021 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
5022 XFS_FSB_TO_AGNO(mp
, bma
.firstblock
)));
5023 *firstblock
= bma
.firstblock
;
5025 cur
->bc_private
.b
.firstblock
=
5027 if (abno
== NULLFSBLOCK
)
5029 if ((ifp
->if_flags
& XFS_IFBROOT
) && !cur
) {
5030 cur
= xfs_btree_init_cursor(mp
,
5031 tp
, NULL
, 0, XFS_BTNUM_BMAP
,
5033 cur
->bc_private
.b
.firstblock
=
5035 cur
->bc_private
.b
.flist
= flist
;
5038 * Bump the number of extents we've allocated
5044 cur
->bc_private
.b
.flags
=
5045 wasdelay
? XFS_BTCUR_BPRV_WASDEL
: 0;
5046 got
.br_startoff
= aoff
;
5047 got
.br_startblock
= abno
;
5048 got
.br_blockcount
= alen
;
5049 got
.br_state
= XFS_EXT_NORM
; /* assume normal */
5051 * Determine state of extent, and the filesystem.
5052 * A wasdelay extent has been initialized, so
5053 * shouldn't be flagged as unwritten.
5055 if (wr
&& XFS_SB_VERSION_HASEXTFLGBIT(&mp
->m_sb
)) {
5056 if (!wasdelay
&& (flags
& XFS_BMAPI_PREALLOC
))
5057 got
.br_state
= XFS_EXT_UNWRITTEN
;
5059 error
= xfs_bmap_add_extent(ip
, lastx
, &cur
, &got
,
5060 firstblock
, flist
, &tmp_logflags
, delta
,
5061 whichfork
, (flags
& XFS_BMAPI_RSVBLOCKS
));
5062 logflags
|= tmp_logflags
;
5065 lastx
= ifp
->if_lastex
;
5066 ep
= xfs_iext_get_ext(ifp
, lastx
);
5067 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5068 xfs_bmbt_get_all(ep
, &got
);
5069 ASSERT(got
.br_startoff
<= aoff
);
5070 ASSERT(got
.br_startoff
+ got
.br_blockcount
>=
5073 if (flags
& XFS_BMAPI_DELAY
) {
5074 ASSERT(ISNULLSTARTBLOCK(got
.br_startblock
));
5075 ASSERT(STARTBLOCKVAL(got
.br_startblock
) > 0);
5077 ASSERT(got
.br_state
== XFS_EXT_NORM
||
5078 got
.br_state
== XFS_EXT_UNWRITTEN
);
5081 * Fall down into the found allocated space case.
5083 } else if (inhole
) {
5085 * Reading in a hole.
5087 mval
->br_startoff
= bno
;
5088 mval
->br_startblock
= HOLESTARTBLOCK
;
5089 mval
->br_blockcount
=
5090 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
5091 mval
->br_state
= XFS_EXT_NORM
;
5092 bno
+= mval
->br_blockcount
;
5093 len
-= mval
->br_blockcount
;
5099 * Then deal with the allocated space we found.
5102 if (!(flags
& XFS_BMAPI_ENTIRE
) &&
5103 (got
.br_startoff
+ got
.br_blockcount
> obno
)) {
5106 ASSERT((bno
>= obno
) || (n
== 0));
5108 mval
->br_startoff
= bno
;
5109 if (ISNULLSTARTBLOCK(got
.br_startblock
)) {
5110 ASSERT(!wr
|| (flags
& XFS_BMAPI_DELAY
));
5111 mval
->br_startblock
= DELAYSTARTBLOCK
;
5113 mval
->br_startblock
=
5115 (bno
- got
.br_startoff
);
5117 * Return the minimum of what we got and what we
5118 * asked for for the length. We can use the len
5119 * variable here because it is modified below
5120 * and we could have been there before coming
5121 * here if the first part of the allocation
5122 * didn't overlap what was asked for.
5124 mval
->br_blockcount
=
5125 XFS_FILBLKS_MIN(end
- bno
, got
.br_blockcount
-
5126 (bno
- got
.br_startoff
));
5127 mval
->br_state
= got
.br_state
;
5128 ASSERT(mval
->br_blockcount
<= len
);
5131 if (ISNULLSTARTBLOCK(mval
->br_startblock
)) {
5132 ASSERT(!wr
|| (flags
& XFS_BMAPI_DELAY
));
5133 mval
->br_startblock
= DELAYSTARTBLOCK
;
5138 * Check if writing previously allocated but
5139 * unwritten extents.
5141 if (wr
&& mval
->br_state
== XFS_EXT_UNWRITTEN
&&
5142 ((flags
& (XFS_BMAPI_PREALLOC
|XFS_BMAPI_DELAY
)) == 0)) {
5144 * Modify (by adding) the state flag, if writing.
5146 ASSERT(mval
->br_blockcount
<= len
);
5147 if ((ifp
->if_flags
& XFS_IFBROOT
) && !cur
) {
5148 cur
= xfs_btree_init_cursor(mp
,
5149 tp
, NULL
, 0, XFS_BTNUM_BMAP
,
5151 cur
->bc_private
.b
.firstblock
=
5153 cur
->bc_private
.b
.flist
= flist
;
5155 mval
->br_state
= XFS_EXT_NORM
;
5156 error
= xfs_bmap_add_extent(ip
, lastx
, &cur
, mval
,
5157 firstblock
, flist
, &tmp_logflags
, delta
,
5158 whichfork
, (flags
& XFS_BMAPI_RSVBLOCKS
));
5159 logflags
|= tmp_logflags
;
5162 lastx
= ifp
->if_lastex
;
5163 ep
= xfs_iext_get_ext(ifp
, lastx
);
5164 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5165 xfs_bmbt_get_all(ep
, &got
);
5167 * We may have combined previously unwritten
5168 * space with written space, so generate
5171 if (mval
->br_blockcount
< len
)
5175 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
5176 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
5177 ASSERT((flags
& XFS_BMAPI_ENTIRE
) ||
5178 (mval
->br_blockcount
<= len
) ||
5179 (mval
->br_startoff
< obno
));
5180 bno
= mval
->br_startoff
+ mval
->br_blockcount
;
5182 if (n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
5183 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
5184 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
5185 ASSERT(mval
->br_state
== mval
[-1].br_state
);
5186 mval
[-1].br_blockcount
= mval
->br_blockcount
;
5187 mval
[-1].br_state
= mval
->br_state
;
5188 } else if (n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
5189 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
5190 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
5191 mval
->br_startblock
==
5192 mval
[-1].br_startblock
+ mval
[-1].br_blockcount
&&
5193 ((flags
& XFS_BMAPI_IGSTATE
) ||
5194 mval
[-1].br_state
== mval
->br_state
)) {
5195 ASSERT(mval
->br_startoff
==
5196 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
5197 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
5199 mval
->br_startblock
== DELAYSTARTBLOCK
&&
5200 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
5201 mval
->br_startoff
==
5202 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
5203 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
5204 mval
[-1].br_state
= mval
->br_state
;
5205 } else if (!((n
== 0) &&
5206 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
5212 * If we're done, stop now. Stop when we've allocated
5213 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5214 * the transaction may get too big.
5216 if (bno
>= end
|| n
>= *nmap
|| nallocs
>= *nmap
)
5219 * Else go on to the next record.
5221 ep
= xfs_iext_get_ext(ifp
, ++lastx
);
5222 if (lastx
>= nextents
) {
5226 xfs_bmbt_get_all(ep
, &got
);
5228 ifp
->if_lastex
= lastx
;
5231 * Transform from btree to extents, give it cur.
5233 if (tp
&& XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
5234 XFS_IFORK_NEXTENTS(ip
, whichfork
) <= ifp
->if_ext_max
) {
5236 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
,
5237 &tmp_logflags
, whichfork
);
5238 logflags
|= tmp_logflags
;
5242 ASSERT(ifp
->if_ext_max
==
5243 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
5244 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
5245 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ifp
->if_ext_max
);
5247 if (delta
&& delta
->xed_startoff
!= NULLFILEOFF
) {
5248 /* A change was actually made.
5249 * Note that delta->xed_blockount is an offset at this
5250 * point and needs to be converted to a block count.
5252 ASSERT(delta
->xed_blockcount
> delta
->xed_startoff
);
5253 delta
->xed_blockcount
-= delta
->xed_startoff
;
5257 * Log everything. Do this after conversion, there's no point in
5258 * logging the extent records if we've converted to btree format.
5260 if ((logflags
& XFS_ILOG_FEXT(whichfork
)) &&
5261 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5262 logflags
&= ~XFS_ILOG_FEXT(whichfork
);
5263 else if ((logflags
& XFS_ILOG_FBROOT(whichfork
)) &&
5264 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5265 logflags
&= ~XFS_ILOG_FBROOT(whichfork
);
5267 * Log whatever the flags say, even if error. Otherwise we might miss
5268 * detecting a case where the data is changed, there's an error,
5269 * and it's not logged so we don't shutdown when we should.
5273 xfs_trans_log_inode(tp
, ip
, logflags
);
5277 ASSERT(*firstblock
== NULLFSBLOCK
||
5278 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
5280 cur
->bc_private
.b
.firstblock
) ||
5282 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
5284 cur
->bc_private
.b
.firstblock
)));
5285 *firstblock
= cur
->bc_private
.b
.firstblock
;
5287 xfs_btree_del_cursor(cur
,
5288 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5291 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
5297 * Map file blocks to filesystem blocks, simple version.
5298 * One block (extent) only, read-only.
5299 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5300 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5301 * was set and all the others were clear.
5305 xfs_trans_t
*tp
, /* transaction pointer */
5306 xfs_inode_t
*ip
, /* incore inode */
5307 int whichfork
, /* data or attr fork */
5308 xfs_fsblock_t
*fsb
, /* output: mapped block */
5309 xfs_fileoff_t bno
) /* starting file offs. mapped */
5311 int eof
; /* we've hit the end of extents */
5312 int error
; /* error return */
5313 xfs_bmbt_irec_t got
; /* current file extent record */
5314 xfs_ifork_t
*ifp
; /* inode fork pointer */
5315 xfs_extnum_t lastx
; /* last useful extent number */
5316 xfs_bmbt_irec_t prev
; /* previous file extent record */
5318 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5320 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
5321 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)) {
5322 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW
,
5324 return XFS_ERROR(EFSCORRUPTED
);
5326 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
5327 return XFS_ERROR(EIO
);
5328 XFS_STATS_INC(xs_blk_mapr
);
5329 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5330 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5332 (void)xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5335 * Reading past eof, act as though there's a hole
5338 if (eof
|| got
.br_startoff
> bno
) {
5342 ASSERT(!ISNULLSTARTBLOCK(got
.br_startblock
));
5343 ASSERT(bno
< got
.br_startoff
+ got
.br_blockcount
);
5344 *fsb
= got
.br_startblock
+ (bno
- got
.br_startoff
);
5345 ifp
->if_lastex
= lastx
;
5350 * Unmap (remove) blocks from a file.
5351 * If nexts is nonzero then the number of extents to remove is limited to
5352 * that value. If not all extents in the block range can be removed then
5357 xfs_trans_t
*tp
, /* transaction pointer */
5358 struct xfs_inode
*ip
, /* incore inode */
5359 xfs_fileoff_t bno
, /* starting offset to unmap */
5360 xfs_filblks_t len
, /* length to unmap in file */
5361 int flags
, /* misc flags */
5362 xfs_extnum_t nexts
, /* number of extents max */
5363 xfs_fsblock_t
*firstblock
, /* first allocated block
5364 controls a.g. for allocs */
5365 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
5366 xfs_extdelta_t
*delta
, /* o: change made to incore
5368 int *done
) /* set if not done yet */
5370 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5371 xfs_bmbt_irec_t del
; /* extent being deleted */
5372 int eof
; /* is deleting at eof */
5373 xfs_bmbt_rec_t
*ep
; /* extent record pointer */
5374 int error
; /* error return value */
5375 xfs_extnum_t extno
; /* extent number in list */
5376 xfs_bmbt_irec_t got
; /* current extent record */
5377 xfs_ifork_t
*ifp
; /* inode fork pointer */
5378 int isrt
; /* freeing in rt area */
5379 xfs_extnum_t lastx
; /* last extent index used */
5380 int logflags
; /* transaction logging flags */
5381 xfs_extlen_t mod
; /* rt extent offset */
5382 xfs_mount_t
*mp
; /* mount structure */
5383 xfs_extnum_t nextents
; /* number of file extents */
5384 xfs_bmbt_irec_t prev
; /* previous extent record */
5385 xfs_fileoff_t start
; /* first file offset deleted */
5386 int tmp_logflags
; /* partial logging flags */
5387 int wasdel
; /* was a delayed alloc extent */
5388 int whichfork
; /* data or attribute fork */
5389 int rsvd
; /* OK to allocate reserved blocks */
5392 xfs_bunmap_trace(ip
, bno
, len
, flags
, (inst_t
*)__return_address
);
5393 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
5394 XFS_ATTR_FORK
: XFS_DATA_FORK
;
5395 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5397 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5398 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5399 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5401 return XFS_ERROR(EFSCORRUPTED
);
5404 if (XFS_FORCED_SHUTDOWN(mp
))
5405 return XFS_ERROR(EIO
);
5406 rsvd
= (flags
& XFS_BMAPI_RSVBLOCKS
) != 0;
5409 ASSERT(ifp
->if_ext_max
==
5410 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
5411 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5412 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5414 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5415 if (nextents
== 0) {
5419 XFS_STATS_INC(xs_blk_unmap
);
5420 isrt
= (whichfork
== XFS_DATA_FORK
) && XFS_IS_REALTIME_INODE(ip
);
5422 bno
= start
+ len
- 1;
5423 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5426 delta
->xed_startoff
= NULLFILEOFF
;
5427 delta
->xed_blockcount
= 0;
5430 * Check to see if the given block number is past the end of the
5431 * file, back up to the last block if so...
5434 ep
= xfs_iext_get_ext(ifp
, --lastx
);
5435 xfs_bmbt_get_all(ep
, &got
);
5436 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5439 if (ifp
->if_flags
& XFS_IFBROOT
) {
5440 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5441 cur
= xfs_btree_init_cursor(mp
, tp
, NULL
, 0, XFS_BTNUM_BMAP
, ip
,
5443 cur
->bc_private
.b
.firstblock
= *firstblock
;
5444 cur
->bc_private
.b
.flist
= flist
;
5445 cur
->bc_private
.b
.flags
= 0;
5449 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5450 (nexts
== 0 || extno
< nexts
)) {
5452 * Is the found extent after a hole in which bno lives?
5453 * Just back up to the previous extent, if so.
5455 if (got
.br_startoff
> bno
) {
5458 ep
= xfs_iext_get_ext(ifp
, lastx
);
5459 xfs_bmbt_get_all(ep
, &got
);
5462 * Is the last block of this extent before the range
5463 * we're supposed to delete? If so, we're done.
5465 bno
= XFS_FILEOFF_MIN(bno
,
5466 got
.br_startoff
+ got
.br_blockcount
- 1);
5470 * Then deal with the (possibly delayed) allocated space
5475 wasdel
= ISNULLSTARTBLOCK(del
.br_startblock
);
5476 if (got
.br_startoff
< start
) {
5477 del
.br_startoff
= start
;
5478 del
.br_blockcount
-= start
- got
.br_startoff
;
5480 del
.br_startblock
+= start
- got
.br_startoff
;
5482 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5483 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5484 sum
= del
.br_startblock
+ del
.br_blockcount
;
5486 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5488 * Realtime extent not lined up at the end.
5489 * The extent could have been split into written
5490 * and unwritten pieces, or we could just be
5491 * unmapping part of it. But we can't really
5492 * get rid of part of a realtime extent.
5494 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5495 !XFS_SB_VERSION_HASEXTFLGBIT(&mp
->m_sb
)) {
5497 * This piece is unwritten, or we're not
5498 * using unwritten extents. Skip over it.
5501 bno
-= mod
> del
.br_blockcount
?
5502 del
.br_blockcount
: mod
;
5503 if (bno
< got
.br_startoff
) {
5505 xfs_bmbt_get_all(xfs_iext_get_ext(
5511 * It's written, turn it unwritten.
5512 * This is better than zeroing it.
5514 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5515 ASSERT(xfs_trans_get_block_res(tp
) > 0);
5517 * If this spans a realtime extent boundary,
5518 * chop it back to the start of the one we end at.
5520 if (del
.br_blockcount
> mod
) {
5521 del
.br_startoff
+= del
.br_blockcount
- mod
;
5522 del
.br_startblock
+= del
.br_blockcount
- mod
;
5523 del
.br_blockcount
= mod
;
5525 del
.br_state
= XFS_EXT_UNWRITTEN
;
5526 error
= xfs_bmap_add_extent(ip
, lastx
, &cur
, &del
,
5527 firstblock
, flist
, &logflags
, delta
,
5533 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5535 * Realtime extent is lined up at the end but not
5536 * at the front. We'll get rid of full extents if
5539 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5540 if (del
.br_blockcount
> mod
) {
5541 del
.br_blockcount
-= mod
;
5542 del
.br_startoff
+= mod
;
5543 del
.br_startblock
+= mod
;
5544 } else if ((del
.br_startoff
== start
&&
5545 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5546 xfs_trans_get_block_res(tp
) == 0)) ||
5547 !XFS_SB_VERSION_HASEXTFLGBIT(&mp
->m_sb
)) {
5549 * Can't make it unwritten. There isn't
5550 * a full extent here so just skip it.
5552 ASSERT(bno
>= del
.br_blockcount
);
5553 bno
-= del
.br_blockcount
;
5554 if (bno
< got
.br_startoff
) {
5556 xfs_bmbt_get_all(--ep
, &got
);
5559 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5561 * This one is already unwritten.
5562 * It must have a written left neighbor.
5563 * Unwrite the killed part of that one and
5567 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
,
5569 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5570 ASSERT(!ISNULLSTARTBLOCK(prev
.br_startblock
));
5571 ASSERT(del
.br_startblock
==
5572 prev
.br_startblock
+ prev
.br_blockcount
);
5573 if (prev
.br_startoff
< start
) {
5574 mod
= start
- prev
.br_startoff
;
5575 prev
.br_blockcount
-= mod
;
5576 prev
.br_startblock
+= mod
;
5577 prev
.br_startoff
= start
;
5579 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5580 error
= xfs_bmap_add_extent(ip
, lastx
- 1, &cur
,
5581 &prev
, firstblock
, flist
, &logflags
,
5582 delta
, XFS_DATA_FORK
, 0);
5587 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5588 del
.br_state
= XFS_EXT_UNWRITTEN
;
5589 error
= xfs_bmap_add_extent(ip
, lastx
, &cur
,
5590 &del
, firstblock
, flist
, &logflags
,
5591 delta
, XFS_DATA_FORK
, 0);
5598 ASSERT(STARTBLOCKVAL(del
.br_startblock
) > 0);
5599 /* Update realtime/data freespace, unreserve quota */
5601 xfs_filblks_t rtexts
;
5603 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5604 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5605 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
5606 (int64_t)rtexts
, rsvd
);
5607 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp
,
5608 NULL
, ip
, -((long)del
.br_blockcount
), 0,
5609 XFS_QMOPT_RES_RTBLKS
);
5611 xfs_mod_incore_sb(mp
, XFS_SBS_FDBLOCKS
,
5612 (int64_t)del
.br_blockcount
, rsvd
);
5613 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp
,
5614 NULL
, ip
, -((long)del
.br_blockcount
), 0,
5615 XFS_QMOPT_RES_REGBLKS
);
5617 ip
->i_delayed_blks
-= del
.br_blockcount
;
5619 cur
->bc_private
.b
.flags
|=
5620 XFS_BTCUR_BPRV_WASDEL
;
5622 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5624 * If it's the case where the directory code is running
5625 * with no block reservation, and the deleted block is in
5626 * the middle of its extent, and the resulting insert
5627 * of an extent would cause transformation to btree format,
5628 * then reject it. The calling code will then swap
5629 * blocks around instead.
5630 * We have to do this now, rather than waiting for the
5631 * conversion to btree format, since the transaction
5634 if (!wasdel
&& xfs_trans_get_block_res(tp
) == 0 &&
5635 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5636 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= ifp
->if_ext_max
&&
5637 del
.br_startoff
> got
.br_startoff
&&
5638 del
.br_startoff
+ del
.br_blockcount
<
5639 got
.br_startoff
+ got
.br_blockcount
) {
5640 error
= XFS_ERROR(ENOSPC
);
5643 error
= xfs_bmap_del_extent(ip
, tp
, lastx
, flist
, cur
, &del
,
5644 &tmp_logflags
, delta
, whichfork
, rsvd
);
5645 logflags
|= tmp_logflags
;
5648 bno
= del
.br_startoff
- 1;
5650 lastx
= ifp
->if_lastex
;
5652 * If not done go on to the next (previous) record.
5653 * Reset ep in case the extents array was re-alloced.
5655 ep
= xfs_iext_get_ext(ifp
, lastx
);
5656 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5657 if (lastx
>= XFS_IFORK_NEXTENTS(ip
, whichfork
) ||
5658 xfs_bmbt_get_startoff(ep
) > bno
) {
5660 ep
= xfs_iext_get_ext(ifp
, lastx
);
5663 xfs_bmbt_get_all(ep
, &got
);
5667 ifp
->if_lastex
= lastx
;
5668 *done
= bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0;
5669 ASSERT(ifp
->if_ext_max
==
5670 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
5672 * Convert to a btree if necessary.
5674 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5675 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ifp
->if_ext_max
) {
5676 ASSERT(cur
== NULL
);
5677 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
,
5678 &cur
, 0, &tmp_logflags
, whichfork
);
5679 logflags
|= tmp_logflags
;
5684 * transform from btree to extents, give it cur
5686 else if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
5687 XFS_IFORK_NEXTENTS(ip
, whichfork
) <= ifp
->if_ext_max
) {
5688 ASSERT(cur
!= NULL
);
5689 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5691 logflags
|= tmp_logflags
;
5696 * transform from extents to local?
5698 ASSERT(ifp
->if_ext_max
==
5699 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
5701 if (delta
&& delta
->xed_startoff
!= NULLFILEOFF
) {
5702 /* A change was actually made.
5703 * Note that delta->xed_blockount is an offset at this
5704 * point and needs to be converted to a block count.
5706 ASSERT(delta
->xed_blockcount
> delta
->xed_startoff
);
5707 delta
->xed_blockcount
-= delta
->xed_startoff
;
5711 * Log everything. Do this after conversion, there's no point in
5712 * logging the extent records if we've converted to btree format.
5714 if ((logflags
& XFS_ILOG_FEXT(whichfork
)) &&
5715 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5716 logflags
&= ~XFS_ILOG_FEXT(whichfork
);
5717 else if ((logflags
& XFS_ILOG_FBROOT(whichfork
)) &&
5718 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5719 logflags
&= ~XFS_ILOG_FBROOT(whichfork
);
5721 * Log inode even in the error case, if the transaction
5722 * is dirty we'll need to shut down the filesystem.
5725 xfs_trans_log_inode(tp
, ip
, logflags
);
5728 *firstblock
= cur
->bc_private
.b
.firstblock
;
5729 cur
->bc_private
.b
.allocated
= 0;
5731 xfs_btree_del_cursor(cur
,
5732 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5738 * Fcntl interface to xfs_bmapi.
5740 int /* error code */
5742 bhv_desc_t
*bdp
, /* XFS behavior descriptor*/
5743 struct getbmap
*bmv
, /* user bmap structure */
5744 void __user
*ap
, /* pointer to user's array */
5745 int interface
) /* interface flags */
5747 __int64_t bmvend
; /* last block requested */
5748 int error
; /* return value */
5749 __int64_t fixlen
; /* length for -1 case */
5750 int i
; /* extent number */
5751 xfs_inode_t
*ip
; /* xfs incore inode pointer */
5752 bhv_vnode_t
*vp
; /* corresponding vnode */
5753 int lock
; /* lock state */
5754 xfs_bmbt_irec_t
*map
; /* buffer for user's data */
5755 xfs_mount_t
*mp
; /* file system mount point */
5756 int nex
; /* # of user extents can do */
5757 int nexleft
; /* # of user extents left */
5758 int subnex
; /* # of bmapi's can do */
5759 int nmap
; /* number of map entries */
5760 struct getbmap out
; /* output structure */
5761 int whichfork
; /* data or attr fork */
5762 int prealloced
; /* this is a file with
5763 * preallocated data space */
5764 int sh_unwritten
; /* true, if unwritten */
5765 /* extents listed separately */
5766 int bmapi_flags
; /* flags for xfs_bmapi */
5767 __int32_t oflags
; /* getbmapx bmv_oflags field */
5769 vp
= BHV_TO_VNODE(bdp
);
5770 ip
= XFS_BHVTOI(bdp
);
5773 whichfork
= interface
& BMV_IF_ATTRFORK
? XFS_ATTR_FORK
: XFS_DATA_FORK
;
5774 sh_unwritten
= (interface
& BMV_IF_PREALLOC
) != 0;
5776 /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
5777 * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
5778 * bit is set for the file, generate a read event in order
5779 * that the DMAPI application may do its thing before we return
5780 * the extents. Usually this means restoring user file data to
5781 * regions of the file that look like holes.
5783 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5784 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5785 * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
5786 * could misinterpret holes in a DMAPI file as true holes,
5787 * when in fact they may represent offline user data.
5789 if ( (interface
& BMV_IF_NO_DMAPI_READ
) == 0
5790 && DM_EVENT_ENABLED(vp
->v_vfsp
, ip
, DM_EVENT_READ
)
5791 && whichfork
== XFS_DATA_FORK
) {
5793 error
= XFS_SEND_DATA(mp
, DM_EVENT_READ
, vp
, 0, 0, 0, NULL
);
5795 return XFS_ERROR(error
);
5798 if (whichfork
== XFS_ATTR_FORK
) {
5799 if (XFS_IFORK_Q(ip
)) {
5800 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
&&
5801 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_BTREE
&&
5802 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_LOCAL
)
5803 return XFS_ERROR(EINVAL
);
5804 } else if (unlikely(
5805 ip
->i_d
.di_aformat
!= 0 &&
5806 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
)) {
5807 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW
,
5809 return XFS_ERROR(EFSCORRUPTED
);
5811 } else if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
&&
5812 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
&&
5813 ip
->i_d
.di_format
!= XFS_DINODE_FMT_LOCAL
)
5814 return XFS_ERROR(EINVAL
);
5815 if (whichfork
== XFS_DATA_FORK
) {
5816 if ((ip
->i_d
.di_extsize
&& (ip
->i_d
.di_flags
&
5817 (XFS_DIFLAG_REALTIME
|XFS_DIFLAG_EXTSIZE
))) ||
5818 ip
->i_d
.di_flags
& (XFS_DIFLAG_PREALLOC
|XFS_DIFLAG_APPEND
)){
5820 fixlen
= XFS_MAXIOFFSET(mp
);
5823 fixlen
= ip
->i_d
.di_size
;
5830 if (bmv
->bmv_length
== -1) {
5831 fixlen
= XFS_FSB_TO_BB(mp
, XFS_B_TO_FSB(mp
, fixlen
));
5832 bmv
->bmv_length
= MAX( (__int64_t
)(fixlen
- bmv
->bmv_offset
),
5834 } else if (bmv
->bmv_length
< 0)
5835 return XFS_ERROR(EINVAL
);
5836 if (bmv
->bmv_length
== 0) {
5837 bmv
->bmv_entries
= 0;
5840 nex
= bmv
->bmv_count
- 1;
5842 return XFS_ERROR(EINVAL
);
5843 bmvend
= bmv
->bmv_offset
+ bmv
->bmv_length
;
5845 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
5847 if (whichfork
== XFS_DATA_FORK
&& ip
->i_delayed_blks
) {
5848 /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
5849 error
= bhv_vop_flush_pages(vp
, (xfs_off_t
)0, -1, 0, FI_REMAPF
);
5852 ASSERT(whichfork
== XFS_ATTR_FORK
|| ip
->i_delayed_blks
== 0);
5854 lock
= xfs_ilock_map_shared(ip
);
5857 * Don't let nex be bigger than the number of extents
5858 * we can have assuming alternating holes and real extents.
5860 if (nex
> XFS_IFORK_NEXTENTS(ip
, whichfork
) * 2 + 1)
5861 nex
= XFS_IFORK_NEXTENTS(ip
, whichfork
) * 2 + 1;
5863 bmapi_flags
= XFS_BMAPI_AFLAG(whichfork
) |
5864 ((sh_unwritten
) ? 0 : XFS_BMAPI_IGSTATE
);
5867 * Allocate enough space to handle "subnex" maps at a time.
5870 map
= kmem_alloc(subnex
* sizeof(*map
), KM_SLEEP
);
5872 bmv
->bmv_entries
= 0;
5874 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0) {
5876 goto unlock_and_return
;
5882 nmap
= (nexleft
> subnex
) ? subnex
: nexleft
;
5883 error
= xfs_bmapi(NULL
, ip
, XFS_BB_TO_FSBT(mp
, bmv
->bmv_offset
),
5884 XFS_BB_TO_FSB(mp
, bmv
->bmv_length
),
5885 bmapi_flags
, NULL
, 0, map
, &nmap
,
5888 goto unlock_and_return
;
5889 ASSERT(nmap
<= subnex
);
5891 for (i
= 0; i
< nmap
&& nexleft
&& bmv
->bmv_length
; i
++) {
5893 oflags
= (map
[i
].br_state
== XFS_EXT_UNWRITTEN
) ?
5894 BMV_OF_PREALLOC
: 0;
5895 out
.bmv_offset
= XFS_FSB_TO_BB(mp
, map
[i
].br_startoff
);
5896 out
.bmv_length
= XFS_FSB_TO_BB(mp
, map
[i
].br_blockcount
);
5897 ASSERT(map
[i
].br_startblock
!= DELAYSTARTBLOCK
);
5898 if (map
[i
].br_startblock
== HOLESTARTBLOCK
&&
5899 ((prealloced
&& out
.bmv_offset
+ out
.bmv_length
== bmvend
) ||
5900 whichfork
== XFS_ATTR_FORK
)) {
5902 * came to hole at end of file or the end of
5905 goto unlock_and_return
;
5908 (map
[i
].br_startblock
== HOLESTARTBLOCK
) ?
5910 XFS_FSB_TO_DB(ip
, map
[i
].br_startblock
);
5912 /* return either getbmap/getbmapx structure. */
5913 if (interface
& BMV_IF_EXTENDED
) {
5914 struct getbmapx outx
;
5916 GETBMAP_CONVERT(out
,outx
);
5917 outx
.bmv_oflags
= oflags
;
5918 outx
.bmv_unused1
= outx
.bmv_unused2
= 0;
5919 if (copy_to_user(ap
, &outx
,
5921 error
= XFS_ERROR(EFAULT
);
5922 goto unlock_and_return
;
5925 if (copy_to_user(ap
, &out
,
5927 error
= XFS_ERROR(EFAULT
);
5928 goto unlock_and_return
;
5932 out
.bmv_offset
+ out
.bmv_length
;
5933 bmv
->bmv_length
= MAX((__int64_t
)0,
5934 (__int64_t
)(bmvend
- bmv
->bmv_offset
));
5936 ap
= (interface
& BMV_IF_EXTENDED
) ?
5938 ((struct getbmapx __user
*)ap
+ 1) :
5940 ((struct getbmap __user
*)ap
+ 1);
5943 } while (nmap
&& nexleft
&& bmv
->bmv_length
);
5946 xfs_iunlock_map_shared(ip
, lock
);
5947 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
5949 kmem_free(map
, subnex
* sizeof(*map
));
5955 * Check the last inode extent to determine whether this allocation will result
5956 * in blocks being allocated at the end of the file. When we allocate new data
5957 * blocks at the end of the file which do not start at the previous data block,
5958 * we will try to align the new blocks at stripe unit boundaries.
5960 STATIC
int /* error */
5962 xfs_inode_t
*ip
, /* incore inode pointer */
5963 xfs_fileoff_t off
, /* file offset in fsblocks */
5964 int whichfork
, /* data or attribute fork */
5965 char *aeof
) /* return value */
5967 int error
; /* error return value */
5968 xfs_ifork_t
*ifp
; /* inode fork pointer */
5969 xfs_bmbt_rec_t
*lastrec
; /* extent record pointer */
5970 xfs_extnum_t nextents
; /* number of file extents */
5971 xfs_bmbt_irec_t s
; /* expanded extent record */
5973 ASSERT(whichfork
== XFS_DATA_FORK
);
5974 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5975 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5976 (error
= xfs_iread_extents(NULL
, ip
, whichfork
)))
5978 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5979 if (nextents
== 0) {
5984 * Go to the last extent
5986 lastrec
= xfs_iext_get_ext(ifp
, nextents
- 1);
5987 xfs_bmbt_get_all(lastrec
, &s
);
5989 * Check we are allocating in the last extent (for delayed allocations)
5990 * or past the last extent for non-delayed allocations.
5992 *aeof
= (off
>= s
.br_startoff
&&
5993 off
< s
.br_startoff
+ s
.br_blockcount
&&
5994 ISNULLSTARTBLOCK(s
.br_startblock
)) ||
5995 off
>= s
.br_startoff
+ s
.br_blockcount
;
6000 * Check if the endoff is outside the last extent. If so the caller will grow
6001 * the allocation to a stripe unit boundary.
6005 xfs_inode_t
*ip
, /* incore inode pointer */
6006 xfs_fileoff_t endoff
, /* file offset in fsblocks */
6007 int whichfork
, /* data or attribute fork */
6008 int *eof
) /* result value */
6010 xfs_fsblock_t blockcount
; /* extent block count */
6011 int error
; /* error return value */
6012 xfs_ifork_t
*ifp
; /* inode fork pointer */
6013 xfs_bmbt_rec_t
*lastrec
; /* extent record pointer */
6014 xfs_extnum_t nextents
; /* number of file extents */
6015 xfs_fileoff_t startoff
; /* extent starting file offset */
6017 ASSERT(whichfork
== XFS_DATA_FORK
);
6018 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
6019 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
6020 (error
= xfs_iread_extents(NULL
, ip
, whichfork
)))
6022 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
6023 if (nextents
== 0) {
6028 * Go to the last extent
6030 lastrec
= xfs_iext_get_ext(ifp
, nextents
- 1);
6031 startoff
= xfs_bmbt_get_startoff(lastrec
);
6032 blockcount
= xfs_bmbt_get_blockcount(lastrec
);
6033 *eof
= endoff
>= startoff
+ blockcount
;
6041 xfs_btree_cur_t
*cur
,
6051 for(i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
6052 bp
= cur
->bc_bufs
[i
];
6054 if (XFS_BUF_ADDR(bp
) == bno
)
6055 break; /* Found it */
6057 if (i
== XFS_BTREE_MAXLEVELS
)
6060 if (!bp
) { /* Chase down all the log items to see if the bp is there */
6061 xfs_log_item_chunk_t
*licp
;
6065 licp
= &tp
->t_items
;
6066 while (!bp
&& licp
!= NULL
) {
6067 if (XFS_LIC_ARE_ALL_FREE(licp
)) {
6068 licp
= licp
->lic_next
;
6071 for (i
= 0; i
< licp
->lic_unused
; i
++) {
6072 xfs_log_item_desc_t
*lidp
;
6073 xfs_log_item_t
*lip
;
6074 xfs_buf_log_item_t
*bip
;
6077 if (XFS_LIC_ISFREE(licp
, i
)) {
6081 lidp
= XFS_LIC_SLOT(licp
, i
);
6082 lip
= lidp
->lid_item
;
6083 if (lip
->li_type
!= XFS_LI_BUF
)
6086 bip
= (xfs_buf_log_item_t
*)lip
;
6089 if (XFS_BUF_ADDR(lbp
) == bno
) {
6091 break; /* Found it */
6094 licp
= licp
->lic_next
;
6102 xfs_bmbt_block_t
*block
,
6108 __be64
*pp
, *thispa
; /* pointer to block address */
6109 xfs_bmbt_key_t
*prevp
, *keyp
;
6111 ASSERT(be16_to_cpu(block
->bb_level
) > 0);
6114 for( i
= 1; i
<= be16_to_cpu(block
->bb_numrecs
); i
++) {
6115 dmxr
= mp
->m_bmap_dmxr
[0];
6118 keyp
= XFS_BMAP_BROOT_KEY_ADDR(block
, i
, sz
);
6120 keyp
= XFS_BTREE_KEY_ADDR(xfs_bmbt
, block
, i
);
6124 xfs_btree_check_key(XFS_BTNUM_BMAP
, prevp
, keyp
);
6129 * Compare the block numbers to see if there are dups.
6133 pp
= XFS_BMAP_BROOT_PTR_ADDR(block
, i
, sz
);
6135 pp
= XFS_BTREE_PTR_ADDR(xfs_bmbt
, block
, i
, dmxr
);
6137 for (j
= i
+1; j
<= be16_to_cpu(block
->bb_numrecs
); j
++) {
6139 thispa
= XFS_BMAP_BROOT_PTR_ADDR(block
, j
, sz
);
6141 thispa
= XFS_BTREE_PTR_ADDR(xfs_bmbt
, block
, j
,
6144 if (*thispa
== *pp
) {
6145 cmn_err(CE_WARN
, "%s: thispa(%d) == pp(%d) %Ld",
6147 (unsigned long long)be64_to_cpu(*thispa
));
6148 panic("%s: ptrs are equal in node\n",
6156 * Check that the extents for the inode ip are in the right order in all
6161 xfs_bmap_check_leaf_extents(
6162 xfs_btree_cur_t
*cur
, /* btree cursor or null */
6163 xfs_inode_t
*ip
, /* incore inode pointer */
6164 int whichfork
) /* data or attr fork */
6166 xfs_bmbt_block_t
*block
; /* current btree block */
6167 xfs_fsblock_t bno
; /* block # of "block" */
6168 xfs_buf_t
*bp
; /* buffer for "block" */
6169 int error
; /* error return value */
6170 xfs_extnum_t i
=0, j
; /* index into the extents list */
6171 xfs_ifork_t
*ifp
; /* fork structure */
6172 int level
; /* btree level, for checking */
6173 xfs_mount_t
*mp
; /* file system mount structure */
6174 __be64
*pp
; /* pointer to block address */
6175 xfs_bmbt_rec_t
*ep
; /* pointer to current extent */
6176 xfs_bmbt_rec_t
*lastp
; /* pointer to previous extent */
6177 xfs_bmbt_rec_t
*nextp
; /* pointer to next extent */
6180 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
6186 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
6187 block
= ifp
->if_broot
;
6189 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6191 level
= be16_to_cpu(block
->bb_level
);
6193 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
6194 pp
= XFS_BMAP_BROOT_PTR_ADDR(block
, 1, ifp
->if_broot_bytes
);
6195 bno
= be64_to_cpu(*pp
);
6197 ASSERT(bno
!= NULLDFSBNO
);
6198 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
6199 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
6202 * Go down the tree until leaf level is reached, following the first
6203 * pointer (leftmost) at each level.
6205 while (level
-- > 0) {
6206 /* See if buf is in cur first */
6207 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
6213 if (!bp
&& (error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
6214 XFS_BMAP_BTREE_REF
)))
6216 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
6217 XFS_WANT_CORRUPTED_GOTO(
6218 XFS_BMAP_SANITY_CHECK(mp
, block
, level
),
6224 * Check this block for basic sanity (increasing keys and
6225 * no duplicate blocks).
6228 xfs_check_block(block
, mp
, 0, 0);
6229 pp
= XFS_BTREE_PTR_ADDR(xfs_bmbt
, block
, 1, mp
->m_bmap_dmxr
[1]);
6230 bno
= be64_to_cpu(*pp
);
6231 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, bno
), error0
);
6234 xfs_trans_brelse(NULL
, bp
);
6239 * Here with bp and block set to the leftmost leaf node in the tree.
6244 * Loop over all leaf nodes checking that all extents are in the right order.
6248 xfs_fsblock_t nextbno
;
6249 xfs_extnum_t num_recs
;
6252 num_recs
= be16_to_cpu(block
->bb_numrecs
);
6255 * Read-ahead the next leaf block, if any.
6258 nextbno
= be64_to_cpu(block
->bb_rightsib
);
6261 * Check all the extents to make sure they are OK.
6262 * If we had a previous block, the last entry should
6263 * conform with the first entry in this one.
6266 ep
= XFS_BTREE_REC_ADDR(xfs_bmbt
, block
, 1);
6267 for (j
= 1; j
< num_recs
; j
++) {
6268 nextp
= XFS_BTREE_REC_ADDR(xfs_bmbt
, block
, j
+ 1);
6270 xfs_btree_check_rec(XFS_BTNUM_BMAP
,
6271 (void *)lastp
, (void *)ep
);
6273 xfs_btree_check_rec(XFS_BTNUM_BMAP
, (void *)ep
,
6282 xfs_trans_brelse(NULL
, bp
);
6286 * If we've reached the end, stop.
6288 if (bno
== NULLFSBLOCK
)
6291 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
6297 if (!bp
&& (error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
6298 XFS_BMAP_BTREE_REF
)))
6300 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
6304 xfs_trans_brelse(NULL
, bp
);
6309 cmn_err(CE_WARN
, "%s: at error0", __FUNCTION__
);
6311 xfs_trans_brelse(NULL
, bp
);
6313 cmn_err(CE_WARN
, "%s: BAD after btree leaves for %d extents",
6315 panic("%s: CORRUPTED BTREE OR SOMETHING", __FUNCTION__
);
6321 * Count fsblocks of the given fork.
6324 xfs_bmap_count_blocks(
6325 xfs_trans_t
*tp
, /* transaction pointer */
6326 xfs_inode_t
*ip
, /* incore inode */
6327 int whichfork
, /* data or attr fork */
6328 int *count
) /* out: count of blocks */
6330 xfs_bmbt_block_t
*block
; /* current btree block */
6331 xfs_fsblock_t bno
; /* block # of "block" */
6332 xfs_ifork_t
*ifp
; /* fork structure */
6333 int level
; /* btree level, for checking */
6334 xfs_mount_t
*mp
; /* file system mount structure */
6335 __be64
*pp
; /* pointer to block address */
6339 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
6340 if ( XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
) {
6341 if (unlikely(xfs_bmap_count_leaves(ifp
, 0,
6342 ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
),
6344 XFS_ERROR_REPORT("xfs_bmap_count_blocks(1)",
6345 XFS_ERRLEVEL_LOW
, mp
);
6346 return XFS_ERROR(EFSCORRUPTED
);
6352 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6354 block
= ifp
->if_broot
;
6355 level
= be16_to_cpu(block
->bb_level
);
6357 pp
= XFS_BMAP_BROOT_PTR_ADDR(block
, 1, ifp
->if_broot_bytes
);
6358 bno
= be64_to_cpu(*pp
);
6359 ASSERT(bno
!= NULLDFSBNO
);
6360 ASSERT(XFS_FSB_TO_AGNO(mp
, bno
) < mp
->m_sb
.sb_agcount
);
6361 ASSERT(XFS_FSB_TO_AGBNO(mp
, bno
) < mp
->m_sb
.sb_agblocks
);
6363 if (unlikely(xfs_bmap_count_tree(mp
, tp
, ifp
, bno
, level
, count
) < 0)) {
6364 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW
,
6366 return XFS_ERROR(EFSCORRUPTED
);
6373 * Recursively walks each level of a btree
6374 * to count total fsblocks is use.
6377 xfs_bmap_count_tree(
6378 xfs_mount_t
*mp
, /* file system mount point */
6379 xfs_trans_t
*tp
, /* transaction pointer */
6380 xfs_ifork_t
*ifp
, /* inode fork pointer */
6381 xfs_fsblock_t blockno
, /* file system block number */
6382 int levelin
, /* level in btree */
6383 int *count
) /* Count of blocks */
6386 xfs_buf_t
*bp
, *nbp
;
6387 int level
= levelin
;
6389 xfs_fsblock_t bno
= blockno
;
6390 xfs_fsblock_t nextbno
;
6391 xfs_bmbt_block_t
*block
, *nextblock
;
6394 if ((error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
, XFS_BMAP_BTREE_REF
)))
6397 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
6400 /* Not at node above leafs, count this level of nodes */
6401 nextbno
= be64_to_cpu(block
->bb_rightsib
);
6402 while (nextbno
!= NULLFSBLOCK
) {
6403 if ((error
= xfs_btree_read_bufl(mp
, tp
, nextbno
,
6404 0, &nbp
, XFS_BMAP_BTREE_REF
)))
6407 nextblock
= XFS_BUF_TO_BMBT_BLOCK(nbp
);
6408 nextbno
= be64_to_cpu(nextblock
->bb_rightsib
);
6409 xfs_trans_brelse(tp
, nbp
);
6412 /* Dive to the next level */
6413 pp
= XFS_BTREE_PTR_ADDR(xfs_bmbt
, block
, 1, mp
->m_bmap_dmxr
[1]);
6414 bno
= be64_to_cpu(*pp
);
6415 if (unlikely((error
=
6416 xfs_bmap_count_tree(mp
, tp
, ifp
, bno
, level
, count
)) < 0)) {
6417 xfs_trans_brelse(tp
, bp
);
6418 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6419 XFS_ERRLEVEL_LOW
, mp
);
6420 return XFS_ERROR(EFSCORRUPTED
);
6422 xfs_trans_brelse(tp
, bp
);
6424 /* count all level 1 nodes and their leaves */
6426 nextbno
= be64_to_cpu(block
->bb_rightsib
);
6427 numrecs
= be16_to_cpu(block
->bb_numrecs
);
6428 if (unlikely(xfs_bmap_disk_count_leaves(ifp
,
6429 0, block
, numrecs
, count
) < 0)) {
6430 xfs_trans_brelse(tp
, bp
);
6431 XFS_ERROR_REPORT("xfs_bmap_count_tree(2)",
6432 XFS_ERRLEVEL_LOW
, mp
);
6433 return XFS_ERROR(EFSCORRUPTED
);
6435 xfs_trans_brelse(tp
, bp
);
6436 if (nextbno
== NULLFSBLOCK
)
6439 if ((error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
6440 XFS_BMAP_BTREE_REF
)))
6443 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
6450 * Count leaf blocks given a range of extent records.
6453 xfs_bmap_count_leaves(
6460 xfs_bmbt_rec_t
*frp
;
6462 for (b
= 0; b
< numrecs
; b
++) {
6463 frp
= xfs_iext_get_ext(ifp
, idx
+ b
);
6464 *count
+= xfs_bmbt_get_blockcount(frp
);
6470 * Count leaf blocks given a range of extent records originally
6474 xfs_bmap_disk_count_leaves(
6477 xfs_bmbt_block_t
*block
,
6482 xfs_bmbt_rec_t
*frp
;
6484 for (b
= 1; b
<= numrecs
; b
++) {
6485 frp
= XFS_BTREE_REC_ADDR(xfs_bmbt
, block
, idx
+ b
);
6486 *count
+= xfs_bmbt_disk_get_blockcount(frp
);