2 * Copyright (c) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
35 #include "xfs_macros.h"
36 #include "xfs_types.h"
39 #include "xfs_trans.h"
44 #include "xfs_dmapi.h"
45 #include "xfs_mount.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_bmap_btree.h"
48 #include "xfs_ialloc_btree.h"
49 #include "xfs_btree.h"
50 #include "xfs_ialloc.h"
51 #include "xfs_attr_sf.h"
52 #include "xfs_dir_sf.h"
53 #include "xfs_dir2_sf.h"
54 #include "xfs_dinode.h"
55 #include "xfs_inode_item.h"
56 #include "xfs_inode.h"
57 #include "xfs_itable.h"
58 #include "xfs_extfree_item.h"
59 #include "xfs_alloc.h"
61 #include "xfs_rtalloc.h"
62 #include "xfs_error.h"
63 #include "xfs_da_btree.h"
64 #include "xfs_dir_leaf.h"
65 #include "xfs_attr_leaf.h"
68 #include "xfs_quota.h"
69 #include "xfs_trans_space.h"
70 #include "xfs_buf_item.h"
75 xfs_bmap_check_leaf_extents(xfs_btree_cur_t
*cur
, xfs_inode_t
*ip
, int whichfork
);
78 kmem_zone_t
*xfs_bmap_free_item_zone
;
81 * Prototypes for internal bmap routines.
86 * Called from xfs_bmap_add_attrfork to handle extents format files.
88 STATIC
int /* error */
89 xfs_bmap_add_attrfork_extents(
90 xfs_trans_t
*tp
, /* transaction pointer */
91 xfs_inode_t
*ip
, /* incore inode pointer */
92 xfs_fsblock_t
*firstblock
, /* first block allocated */
93 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
94 int *flags
); /* inode logging flags */
97 * Called from xfs_bmap_add_attrfork to handle local format files.
99 STATIC
int /* error */
100 xfs_bmap_add_attrfork_local(
101 xfs_trans_t
*tp
, /* transaction pointer */
102 xfs_inode_t
*ip
, /* incore inode pointer */
103 xfs_fsblock_t
*firstblock
, /* first block allocated */
104 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
105 int *flags
); /* inode logging flags */
108 * Called by xfs_bmapi to update extent list structure and the btree
109 * after allocating space (or doing a delayed allocation).
111 STATIC
int /* error */
113 xfs_inode_t
*ip
, /* incore inode pointer */
114 xfs_extnum_t idx
, /* extent number to update/insert */
115 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
116 xfs_bmbt_irec_t
*new, /* new data to put in extent list */
117 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
118 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
119 int *logflagsp
, /* inode logging flags */
120 int whichfork
, /* data or attr fork */
121 int rsvd
); /* OK to allocate reserved blocks */
124 * Called by xfs_bmap_add_extent to handle cases converting a delayed
125 * allocation to a real allocation.
127 STATIC
int /* error */
128 xfs_bmap_add_extent_delay_real(
129 xfs_inode_t
*ip
, /* incore inode pointer */
130 xfs_extnum_t idx
, /* extent number to update/insert */
131 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
132 xfs_bmbt_irec_t
*new, /* new data to put in extent list */
133 xfs_filblks_t
*dnew
, /* new delayed-alloc indirect blocks */
134 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
135 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
136 int *logflagsp
, /* inode logging flags */
137 int rsvd
); /* OK to allocate reserved blocks */
140 * Called by xfs_bmap_add_extent to handle cases converting a hole
141 * to a delayed allocation.
143 STATIC
int /* error */
144 xfs_bmap_add_extent_hole_delay(
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 put in extent list */
149 int *logflagsp
,/* inode logging flags */
150 int rsvd
); /* OK to allocate reserved blocks */
153 * Called by xfs_bmap_add_extent to handle cases converting a hole
154 * to a real allocation.
156 STATIC
int /* error */
157 xfs_bmap_add_extent_hole_real(
158 xfs_inode_t
*ip
, /* incore inode pointer */
159 xfs_extnum_t idx
, /* extent number to update/insert */
160 xfs_btree_cur_t
*cur
, /* if null, not a btree */
161 xfs_bmbt_irec_t
*new, /* new data to put in extent list */
162 int *logflagsp
, /* inode logging flags */
163 int whichfork
); /* data or attr fork */
166 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
167 * allocation to a real allocation or vice versa.
169 STATIC
int /* error */
170 xfs_bmap_add_extent_unwritten_real(
171 xfs_inode_t
*ip
, /* incore inode pointer */
172 xfs_extnum_t idx
, /* extent number to update/insert */
173 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
174 xfs_bmbt_irec_t
*new, /* new data to put in extent list */
175 int *logflagsp
); /* inode logging flags */
178 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
179 * It figures out where to ask the underlying allocator to put the new extent.
181 STATIC
int /* error */
183 xfs_bmalloca_t
*ap
); /* bmap alloc argument struct */
186 * Transform a btree format file with only one leaf node, where the
187 * extents list will fit in the inode, into an extents format file.
188 * Since the extent list is already in-core, all we have to do is
189 * give up the space for the btree root and pitch the leaf block.
191 STATIC
int /* error */
192 xfs_bmap_btree_to_extents(
193 xfs_trans_t
*tp
, /* transaction pointer */
194 xfs_inode_t
*ip
, /* incore inode pointer */
195 xfs_btree_cur_t
*cur
, /* btree cursor */
196 int *logflagsp
, /* inode logging flags */
197 int whichfork
); /* data or attr fork */
201 * Check that the extents list for the inode ip is in the right order.
204 xfs_bmap_check_extents(
205 xfs_inode_t
*ip
, /* incore inode pointer */
206 int whichfork
); /* data or attr fork */
210 * Called by xfs_bmapi to update extent list structure and the btree
211 * after removing space (or undoing a delayed allocation).
213 STATIC
int /* error */
215 xfs_inode_t
*ip
, /* incore inode pointer */
216 xfs_trans_t
*tp
, /* current trans pointer */
217 xfs_extnum_t idx
, /* extent number to update/insert */
218 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
219 xfs_btree_cur_t
*cur
, /* if null, not a btree */
220 xfs_bmbt_irec_t
*new, /* new data to put in extent list */
221 int *logflagsp
,/* inode logging flags */
222 int whichfork
, /* data or attr fork */
223 int rsvd
); /* OK to allocate reserved blocks */
226 * Remove the entry "free" from the free item list. Prev points to the
227 * previous entry, unless "free" is the head of the list.
231 xfs_bmap_free_t
*flist
, /* free item list header */
232 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
233 xfs_bmap_free_item_t
*free
); /* list item to be freed */
236 * Remove count entries from the extents array for inode "ip", starting
237 * at index "idx". Copies the remaining items down over the deleted ones,
238 * and gives back the excess memory.
241 xfs_bmap_delete_exlist(
242 xfs_inode_t
*ip
, /* incode inode pointer */
243 xfs_extnum_t idx
, /* starting delete index */
244 xfs_extnum_t count
, /* count of items to delete */
245 int whichfork
); /* data or attr fork */
248 * Convert an extents-format file into a btree-format file.
249 * The new file will have a root block (in the inode) and a single child block.
251 STATIC
int /* error */
252 xfs_bmap_extents_to_btree(
253 xfs_trans_t
*tp
, /* transaction pointer */
254 xfs_inode_t
*ip
, /* incore inode pointer */
255 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
256 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
257 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
258 int wasdel
, /* converting a delayed alloc */
259 int *logflagsp
, /* inode logging flags */
260 int whichfork
); /* data or attr fork */
263 * Insert new item(s) in the extent list for inode "ip".
264 * Count new items are inserted at offset idx.
267 xfs_bmap_insert_exlist(
268 xfs_inode_t
*ip
, /* incore inode pointer */
269 xfs_extnum_t idx
, /* starting index of new items */
270 xfs_extnum_t count
, /* number of inserted items */
271 xfs_bmbt_irec_t
*new, /* items to insert */
272 int whichfork
); /* data or attr fork */
275 * Convert a local file to an extents file.
276 * This code is sort of bogus, since the file data needs to get
277 * logged so it won't be lost. The bmap-level manipulations are ok, though.
279 STATIC
int /* error */
280 xfs_bmap_local_to_extents(
281 xfs_trans_t
*tp
, /* transaction pointer */
282 xfs_inode_t
*ip
, /* incore inode pointer */
283 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
284 xfs_extlen_t total
, /* total blocks needed by transaction */
285 int *logflagsp
, /* inode logging flags */
286 int whichfork
); /* data or attr fork */
289 * Search the extents list for the inode, for the extent containing bno.
290 * If bno lies in a hole, point to the next entry. If bno lies past eof,
291 * *eofp will be set, and *prevp will contain the last entry (null if none).
292 * Else, *lastxp will be set to the index of the found
293 * entry; *gotp will contain the entry.
295 STATIC xfs_bmbt_rec_t
* /* pointer to found extent entry */
296 xfs_bmap_search_extents(
297 xfs_inode_t
*ip
, /* incore inode pointer */
298 xfs_fileoff_t bno
, /* block number searched for */
299 int whichfork
, /* data or attr fork */
300 int *eofp
, /* out: end of file found */
301 xfs_extnum_t
*lastxp
, /* out: last extent index */
302 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
303 xfs_bmbt_irec_t
*prevp
); /* out: previous extent entry found */
306 * Check the last inode extent to determine whether this allocation will result
307 * in blocks being allocated at the end of the file. When we allocate new data
308 * blocks at the end of the file which do not start at the previous data block,
309 * we will try to align the new blocks at stripe unit boundaries.
311 STATIC
int /* error */
313 xfs_inode_t
*ip
, /* incore inode pointer */
314 xfs_fileoff_t off
, /* file offset in fsblocks */
315 int whichfork
, /* data or attribute fork */
316 char *aeof
); /* return value */
318 #ifdef XFS_BMAP_TRACE
320 * Add a bmap trace buffer entry. Base routine for the others.
323 xfs_bmap_trace_addentry(
324 int opcode
, /* operation */
325 char *fname
, /* function name */
326 char *desc
, /* operation description */
327 xfs_inode_t
*ip
, /* incore inode pointer */
328 xfs_extnum_t idx
, /* index of entry(ies) */
329 xfs_extnum_t cnt
, /* count of entries, 1 or 2 */
330 xfs_bmbt_rec_t
*r1
, /* first record */
331 xfs_bmbt_rec_t
*r2
, /* second record or null */
332 int whichfork
); /* data or attr fork */
335 * Add bmap trace entry prior to a call to xfs_bmap_delete_exlist.
338 xfs_bmap_trace_delete(
339 char *fname
, /* function name */
340 char *desc
, /* operation description */
341 xfs_inode_t
*ip
, /* incore inode pointer */
342 xfs_extnum_t idx
, /* index of entry(entries) deleted */
343 xfs_extnum_t cnt
, /* count of entries deleted, 1 or 2 */
344 int whichfork
); /* data or attr fork */
347 * Add bmap trace entry prior to a call to xfs_bmap_insert_exlist, or
348 * reading in the extents list from the disk (in the btree).
351 xfs_bmap_trace_insert(
352 char *fname
, /* function name */
353 char *desc
, /* operation description */
354 xfs_inode_t
*ip
, /* incore inode pointer */
355 xfs_extnum_t idx
, /* index of entry(entries) inserted */
356 xfs_extnum_t cnt
, /* count of entries inserted, 1 or 2 */
357 xfs_bmbt_irec_t
*r1
, /* inserted record 1 */
358 xfs_bmbt_irec_t
*r2
, /* inserted record 2 or null */
359 int whichfork
); /* data or attr fork */
362 * Add bmap trace entry after updating an extent list entry in place.
365 xfs_bmap_trace_post_update(
366 char *fname
, /* function name */
367 char *desc
, /* operation description */
368 xfs_inode_t
*ip
, /* incore inode pointer */
369 xfs_extnum_t idx
, /* index of entry updated */
370 int whichfork
); /* data or attr fork */
373 * Add bmap trace entry prior to updating an extent list entry in place.
376 xfs_bmap_trace_pre_update(
377 char *fname
, /* function name */
378 char *desc
, /* operation description */
379 xfs_inode_t
*ip
, /* incore inode pointer */
380 xfs_extnum_t idx
, /* index of entry to be updated */
381 int whichfork
); /* data or attr fork */
384 #define xfs_bmap_trace_delete(f,d,ip,i,c,w)
385 #define xfs_bmap_trace_insert(f,d,ip,i,c,r1,r2,w)
386 #define xfs_bmap_trace_post_update(f,d,ip,i,w)
387 #define xfs_bmap_trace_pre_update(f,d,ip,i,w)
388 #endif /* XFS_BMAP_TRACE */
391 * Compute the worst-case number of indirect blocks that will be used
392 * for ip's delayed extent of length "len".
395 xfs_bmap_worst_indlen(
396 xfs_inode_t
*ip
, /* incore inode pointer */
397 xfs_filblks_t len
); /* delayed extent length */
401 * Perform various validation checks on the values being returned
405 xfs_bmap_validate_ret(
409 xfs_bmbt_irec_t
*mval
,
413 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
416 #if defined(XFS_RW_TRACE)
425 #define xfs_bunmap_trace(ip, bno, len, flags, ra)
426 #endif /* XFS_RW_TRACE */
432 xfs_fsblock_t blockno
,
437 xfs_bmap_count_leaves(
443 * Bmap internal routines.
447 * Called from xfs_bmap_add_attrfork to handle btree format files.
449 STATIC
int /* error */
450 xfs_bmap_add_attrfork_btree(
451 xfs_trans_t
*tp
, /* transaction pointer */
452 xfs_inode_t
*ip
, /* incore inode pointer */
453 xfs_fsblock_t
*firstblock
, /* first block allocated */
454 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
455 int *flags
) /* inode logging flags */
457 xfs_btree_cur_t
*cur
; /* btree cursor */
458 int error
; /* error return value */
459 xfs_mount_t
*mp
; /* file system mount struct */
460 int stat
; /* newroot status */
463 if (ip
->i_df
.if_broot_bytes
<= XFS_IFORK_DSIZE(ip
))
464 *flags
|= XFS_ILOG_DBROOT
;
466 cur
= xfs_btree_init_cursor(mp
, tp
, NULL
, 0, XFS_BTNUM_BMAP
, ip
,
468 cur
->bc_private
.b
.flist
= flist
;
469 cur
->bc_private
.b
.firstblock
= *firstblock
;
470 if ((error
= xfs_bmbt_lookup_ge(cur
, 0, 0, 0, &stat
)))
472 ASSERT(stat
== 1); /* must be at least one entry */
473 if ((error
= xfs_bmbt_newroot(cur
, flags
, &stat
)))
476 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
477 return XFS_ERROR(ENOSPC
);
479 *firstblock
= cur
->bc_private
.b
.firstblock
;
480 cur
->bc_private
.b
.allocated
= 0;
481 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
485 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
490 * Called from xfs_bmap_add_attrfork to handle extents format files.
492 STATIC
int /* error */
493 xfs_bmap_add_attrfork_extents(
494 xfs_trans_t
*tp
, /* transaction pointer */
495 xfs_inode_t
*ip
, /* incore inode pointer */
496 xfs_fsblock_t
*firstblock
, /* first block allocated */
497 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
498 int *flags
) /* inode logging flags */
500 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
501 int error
; /* error return value */
503 if (ip
->i_d
.di_nextents
* sizeof(xfs_bmbt_rec_t
) <= XFS_IFORK_DSIZE(ip
))
506 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
, &cur
, 0,
507 flags
, XFS_DATA_FORK
);
509 cur
->bc_private
.b
.allocated
= 0;
510 xfs_btree_del_cursor(cur
,
511 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
517 * Called from xfs_bmap_add_attrfork to handle local format files.
519 STATIC
int /* error */
520 xfs_bmap_add_attrfork_local(
521 xfs_trans_t
*tp
, /* transaction pointer */
522 xfs_inode_t
*ip
, /* incore inode pointer */
523 xfs_fsblock_t
*firstblock
, /* first block allocated */
524 xfs_bmap_free_t
*flist
, /* blocks to free at commit */
525 int *flags
) /* inode logging flags */
527 xfs_da_args_t dargs
; /* args for dir/attr code */
528 int error
; /* error return value */
529 xfs_mount_t
*mp
; /* mount structure pointer */
531 if (ip
->i_df
.if_bytes
<= XFS_IFORK_DSIZE(ip
))
533 if ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
) {
535 memset(&dargs
, 0, sizeof(dargs
));
537 dargs
.firstblock
= firstblock
;
539 dargs
.total
= mp
->m_dirblkfsbs
;
540 dargs
.whichfork
= XFS_DATA_FORK
;
542 error
= XFS_DIR_SHORTFORM_TO_SINGLE(mp
, &dargs
);
544 error
= xfs_bmap_local_to_extents(tp
, ip
, firstblock
, 1, flags
,
550 * Called by xfs_bmapi to update extent list structure and the btree
551 * after allocating space (or doing a delayed allocation).
553 STATIC
int /* error */
555 xfs_inode_t
*ip
, /* incore inode pointer */
556 xfs_extnum_t idx
, /* extent number to update/insert */
557 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
558 xfs_bmbt_irec_t
*new, /* new data to put in extent list */
559 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
560 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
561 int *logflagsp
, /* inode logging flags */
562 int whichfork
, /* data or attr fork */
563 int rsvd
) /* OK to use reserved data blocks */
565 xfs_btree_cur_t
*cur
; /* btree cursor or null */
566 xfs_filblks_t da_new
; /* new count del alloc blocks used */
567 xfs_filblks_t da_old
; /* old count del alloc blocks used */
568 int error
; /* error return value */
569 #ifdef XFS_BMAP_TRACE
570 static char fname
[] = "xfs_bmap_add_extent";
572 xfs_ifork_t
*ifp
; /* inode fork ptr */
573 int logflags
; /* returned value */
574 xfs_extnum_t nextents
; /* number of extents in file now */
576 XFS_STATS_INC(xs_add_exlist
);
578 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
579 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
580 ASSERT(idx
<= nextents
);
584 * This is the first extent added to a new/empty file.
585 * Special case this one, so other routines get to assume there are
586 * already extents in the list.
589 xfs_bmap_trace_insert(fname
, "insert empty", ip
, 0, 1, new,
591 xfs_bmap_insert_exlist(ip
, 0, 1, new, whichfork
);
594 if (!ISNULLSTARTBLOCK(new->br_startblock
)) {
595 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
596 logflags
= XFS_ILOG_CORE
| XFS_ILOG_FEXT(whichfork
);
601 * Any kind of new delayed allocation goes here.
603 else if (ISNULLSTARTBLOCK(new->br_startblock
)) {
605 ASSERT((cur
->bc_private
.b
.flags
&
606 XFS_BTCUR_BPRV_WASDEL
) == 0);
607 if ((error
= xfs_bmap_add_extent_hole_delay(ip
, idx
, cur
, new,
612 * Real allocation off the end of the file.
614 else if (idx
== nextents
) {
616 ASSERT((cur
->bc_private
.b
.flags
&
617 XFS_BTCUR_BPRV_WASDEL
) == 0);
618 if ((error
= xfs_bmap_add_extent_hole_real(ip
, idx
, cur
, new,
619 &logflags
, whichfork
)))
622 xfs_bmbt_irec_t prev
; /* old extent at offset idx */
625 * Get the record referred to by idx.
627 xfs_bmbt_get_all(&ifp
->if_u1
.if_extents
[idx
], &prev
);
629 * If it's a real allocation record, and the new allocation ends
630 * after the start of the referred to record, then we're filling
631 * in a delayed or unwritten allocation with a real one, or
632 * converting real back to unwritten.
634 if (!ISNULLSTARTBLOCK(new->br_startblock
) &&
635 new->br_startoff
+ new->br_blockcount
> prev
.br_startoff
) {
636 if (prev
.br_state
!= XFS_EXT_UNWRITTEN
&&
637 ISNULLSTARTBLOCK(prev
.br_startblock
)) {
638 da_old
= STARTBLOCKVAL(prev
.br_startblock
);
640 ASSERT(cur
->bc_private
.b
.flags
&
641 XFS_BTCUR_BPRV_WASDEL
);
642 if ((error
= xfs_bmap_add_extent_delay_real(ip
,
643 idx
, &cur
, new, &da_new
, first
, flist
,
646 } else if (new->br_state
== XFS_EXT_NORM
) {
647 ASSERT(new->br_state
== XFS_EXT_NORM
);
648 if ((error
= xfs_bmap_add_extent_unwritten_real(
649 ip
, idx
, &cur
, new, &logflags
)))
652 ASSERT(new->br_state
== XFS_EXT_UNWRITTEN
);
653 if ((error
= xfs_bmap_add_extent_unwritten_real(
654 ip
, idx
, &cur
, new, &logflags
)))
657 ASSERT(*curp
== cur
|| *curp
== NULL
);
660 * Otherwise we're filling in a hole with an allocation.
664 ASSERT((cur
->bc_private
.b
.flags
&
665 XFS_BTCUR_BPRV_WASDEL
) == 0);
666 if ((error
= xfs_bmap_add_extent_hole_real(ip
, idx
, cur
,
667 new, &logflags
, whichfork
)))
672 ASSERT(*curp
== cur
|| *curp
== NULL
);
674 * Convert to a btree if necessary.
676 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
677 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ifp
->if_ext_max
) {
678 int tmp_logflags
; /* partial log flag return val */
681 error
= xfs_bmap_extents_to_btree(ip
->i_transp
, ip
, first
,
682 flist
, &cur
, da_old
> 0, &tmp_logflags
, whichfork
);
683 logflags
|= tmp_logflags
;
688 * Adjust for changes in reserved delayed indirect blocks.
689 * Nothing to do for disk quotas here.
691 if (da_old
|| da_new
) {
696 nblks
+= cur
->bc_private
.b
.allocated
;
697 ASSERT(nblks
<= da_old
);
699 xfs_mod_incore_sb(ip
->i_mount
, XFS_SBS_FDBLOCKS
,
700 (int)(da_old
- nblks
), rsvd
);
703 * Clear out the allocated field, done with it now in any case.
706 cur
->bc_private
.b
.allocated
= 0;
712 xfs_bmap_check_leaf_extents(*curp
, ip
, whichfork
);
714 *logflagsp
= logflags
;
719 * Called by xfs_bmap_add_extent to handle cases converting a delayed
720 * allocation to a real allocation.
722 STATIC
int /* error */
723 xfs_bmap_add_extent_delay_real(
724 xfs_inode_t
*ip
, /* incore inode pointer */
725 xfs_extnum_t idx
, /* extent number to update/insert */
726 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
727 xfs_bmbt_irec_t
*new, /* new data to put in extent list */
728 xfs_filblks_t
*dnew
, /* new delayed-alloc indirect blocks */
729 xfs_fsblock_t
*first
, /* pointer to firstblock variable */
730 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
731 int *logflagsp
, /* inode logging flags */
732 int rsvd
) /* OK to use reserved data block allocation */
734 xfs_bmbt_rec_t
*base
; /* base of extent entry list */
735 xfs_btree_cur_t
*cur
; /* btree cursor */
736 int diff
; /* temp value */
737 xfs_bmbt_rec_t
*ep
; /* extent entry for idx */
738 int error
; /* error return value */
739 #ifdef XFS_BMAP_TRACE
740 static char fname
[] = "xfs_bmap_add_extent_delay_real";
742 int i
; /* temp state */
743 xfs_fileoff_t new_endoff
; /* end offset of new entry */
744 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
745 /* left is 0, right is 1, prev is 2 */
746 int rval
=0; /* return value (logging flags) */
747 int state
= 0;/* state bits, accessed thru macros */
748 xfs_filblks_t temp
; /* value for dnew calculations */
749 xfs_filblks_t temp2
; /* value for dnew calculations */
750 int tmp_rval
; /* partial logging flags */
751 enum { /* bit number definitions for state */
752 LEFT_CONTIG
, RIGHT_CONTIG
,
753 LEFT_FILLING
, RIGHT_FILLING
,
754 LEFT_DELAY
, RIGHT_DELAY
,
755 LEFT_VALID
, RIGHT_VALID
761 #define MASK(b) (1 << (b))
762 #define MASK2(a,b) (MASK(a) | MASK(b))
763 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
764 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
765 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
766 #define STATE_TEST(b) (state & MASK(b))
767 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
768 ((state &= ~MASK(b)), 0))
769 #define SWITCH_STATE \
770 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
773 * Set up a bunch of variables to make the tests simpler.
776 base
= ip
->i_df
.if_u1
.if_extents
;
778 xfs_bmbt_get_all(ep
, &PREV
);
779 new_endoff
= new->br_startoff
+ new->br_blockcount
;
780 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
781 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
783 * Set flags determining what part of the previous delayed allocation
784 * extent is being replaced by a real allocation.
786 STATE_SET(LEFT_FILLING
, PREV
.br_startoff
== new->br_startoff
);
787 STATE_SET(RIGHT_FILLING
,
788 PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
);
790 * Check and set flags if this segment has a left neighbor.
791 * Don't set contiguous if the combined extent would be too large.
793 if (STATE_SET_TEST(LEFT_VALID
, idx
> 0)) {
794 xfs_bmbt_get_all(ep
- 1, &LEFT
);
795 STATE_SET(LEFT_DELAY
, ISNULLSTARTBLOCK(LEFT
.br_startblock
));
797 STATE_SET(LEFT_CONTIG
,
798 STATE_TEST(LEFT_VALID
) && !STATE_TEST(LEFT_DELAY
) &&
799 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
800 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
801 LEFT
.br_state
== new->br_state
&&
802 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
);
804 * Check and set flags if this segment has a right neighbor.
805 * Don't set contiguous if the combined extent would be too large.
806 * Also check for all-three-contiguous being too large.
808 if (STATE_SET_TEST(RIGHT_VALID
,
810 ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1)) {
811 xfs_bmbt_get_all(ep
+ 1, &RIGHT
);
812 STATE_SET(RIGHT_DELAY
, ISNULLSTARTBLOCK(RIGHT
.br_startblock
));
814 STATE_SET(RIGHT_CONTIG
,
815 STATE_TEST(RIGHT_VALID
) && !STATE_TEST(RIGHT_DELAY
) &&
816 new_endoff
== RIGHT
.br_startoff
&&
817 new->br_startblock
+ new->br_blockcount
==
818 RIGHT
.br_startblock
&&
819 new->br_state
== RIGHT
.br_state
&&
820 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
821 ((state
& MASK3(LEFT_CONTIG
, LEFT_FILLING
, RIGHT_FILLING
)) !=
822 MASK3(LEFT_CONTIG
, LEFT_FILLING
, RIGHT_FILLING
) ||
823 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
827 * Switch out based on the FILLING and CONTIG state bits.
829 switch (SWITCH_STATE
) {
831 case MASK4(LEFT_FILLING
, RIGHT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
833 * Filling in all of a previously delayed allocation extent.
834 * The left and right neighbors are both contiguous with new.
836 xfs_bmap_trace_pre_update(fname
, "LF|RF|LC|RC", ip
, idx
- 1,
838 xfs_bmbt_set_blockcount(ep
- 1,
839 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
840 RIGHT
.br_blockcount
);
841 xfs_bmap_trace_post_update(fname
, "LF|RF|LC|RC", ip
, idx
- 1,
843 xfs_bmap_trace_delete(fname
, "LF|RF|LC|RC", ip
, idx
, 2,
845 xfs_bmap_delete_exlist(ip
, idx
, 2, XFS_DATA_FORK
);
846 ip
->i_df
.if_lastex
= idx
- 1;
847 ip
->i_d
.di_nextents
--;
849 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
851 rval
= XFS_ILOG_CORE
;
852 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
854 RIGHT
.br_blockcount
, &i
)))
857 if ((error
= xfs_bmbt_delete(cur
, &i
)))
860 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
863 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
867 RIGHT
.br_blockcount
, LEFT
.br_state
)))
873 case MASK3(LEFT_FILLING
, RIGHT_FILLING
, LEFT_CONTIG
):
875 * Filling in all of a previously delayed allocation extent.
876 * The left neighbor is contiguous, the right is not.
878 xfs_bmap_trace_pre_update(fname
, "LF|RF|LC", ip
, idx
- 1,
880 xfs_bmbt_set_blockcount(ep
- 1,
881 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
882 xfs_bmap_trace_post_update(fname
, "LF|RF|LC", ip
, idx
- 1,
884 ip
->i_df
.if_lastex
= idx
- 1;
885 xfs_bmap_trace_delete(fname
, "LF|RF|LC", ip
, idx
, 1,
887 xfs_bmap_delete_exlist(ip
, idx
, 1, XFS_DATA_FORK
);
889 rval
= XFS_ILOG_DEXT
;
892 if ((error
= xfs_bmbt_lookup_eq(cur
, LEFT
.br_startoff
,
893 LEFT
.br_startblock
, LEFT
.br_blockcount
,
897 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
900 PREV
.br_blockcount
, LEFT
.br_state
)))
906 case MASK3(LEFT_FILLING
, RIGHT_FILLING
, RIGHT_CONTIG
):
908 * Filling in all of a previously delayed allocation extent.
909 * The right neighbor is contiguous, the left is not.
911 xfs_bmap_trace_pre_update(fname
, "LF|RF|RC", ip
, idx
,
913 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
914 xfs_bmbt_set_blockcount(ep
,
915 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
916 xfs_bmap_trace_post_update(fname
, "LF|RF|RC", ip
, idx
,
918 ip
->i_df
.if_lastex
= idx
;
919 xfs_bmap_trace_delete(fname
, "LF|RF|RC", ip
, idx
+ 1, 1,
921 xfs_bmap_delete_exlist(ip
, idx
+ 1, 1, XFS_DATA_FORK
);
923 rval
= XFS_ILOG_DEXT
;
926 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
928 RIGHT
.br_blockcount
, &i
)))
931 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
934 RIGHT
.br_blockcount
, PREV
.br_state
)))
940 case MASK2(LEFT_FILLING
, RIGHT_FILLING
):
942 * Filling in all of a previously delayed allocation extent.
943 * Neither the left nor right neighbors are contiguous with
946 xfs_bmap_trace_pre_update(fname
, "LF|RF", ip
, idx
,
948 xfs_bmbt_set_startblock(ep
, new->br_startblock
);
949 xfs_bmap_trace_post_update(fname
, "LF|RF", ip
, idx
,
951 ip
->i_df
.if_lastex
= idx
;
952 ip
->i_d
.di_nextents
++;
954 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
956 rval
= XFS_ILOG_CORE
;
957 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
958 new->br_startblock
, new->br_blockcount
,
962 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
963 if ((error
= xfs_bmbt_insert(cur
, &i
)))
970 case MASK2(LEFT_FILLING
, LEFT_CONTIG
):
972 * Filling in the first part of a previous delayed allocation.
973 * The left neighbor is contiguous.
975 xfs_bmap_trace_pre_update(fname
, "LF|LC", ip
, idx
- 1,
977 xfs_bmbt_set_blockcount(ep
- 1,
978 LEFT
.br_blockcount
+ new->br_blockcount
);
979 xfs_bmbt_set_startoff(ep
,
980 PREV
.br_startoff
+ new->br_blockcount
);
981 xfs_bmap_trace_post_update(fname
, "LF|LC", ip
, idx
- 1,
983 temp
= PREV
.br_blockcount
- new->br_blockcount
;
984 xfs_bmap_trace_pre_update(fname
, "LF|LC", ip
, idx
,
986 xfs_bmbt_set_blockcount(ep
, temp
);
987 ip
->i_df
.if_lastex
= idx
- 1;
989 rval
= XFS_ILOG_DEXT
;
992 if ((error
= xfs_bmbt_lookup_eq(cur
, LEFT
.br_startoff
,
993 LEFT
.br_startblock
, LEFT
.br_blockcount
,
997 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
1004 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
1005 STARTBLOCKVAL(PREV
.br_startblock
));
1006 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
1007 xfs_bmap_trace_post_update(fname
, "LF|LC", ip
, idx
,
1012 case MASK(LEFT_FILLING
):
1014 * Filling in the first part of a previous delayed allocation.
1015 * The left neighbor is not contiguous.
1017 xfs_bmap_trace_pre_update(fname
, "LF", ip
, idx
, XFS_DATA_FORK
);
1018 xfs_bmbt_set_startoff(ep
, new_endoff
);
1019 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1020 xfs_bmbt_set_blockcount(ep
, temp
);
1021 xfs_bmap_trace_insert(fname
, "LF", ip
, idx
, 1, new, NULL
,
1023 xfs_bmap_insert_exlist(ip
, idx
, 1, new, XFS_DATA_FORK
);
1024 ip
->i_df
.if_lastex
= idx
;
1025 ip
->i_d
.di_nextents
++;
1027 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1029 rval
= XFS_ILOG_CORE
;
1030 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1031 new->br_startblock
, new->br_blockcount
,
1035 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1036 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1040 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
1041 ip
->i_d
.di_nextents
> ip
->i_df
.if_ext_max
) {
1042 error
= xfs_bmap_extents_to_btree(ip
->i_transp
, ip
,
1043 first
, flist
, &cur
, 1, &tmp_rval
,
1049 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
1050 STARTBLOCKVAL(PREV
.br_startblock
) -
1051 (cur
? cur
->bc_private
.b
.allocated
: 0));
1052 base
= ip
->i_df
.if_u1
.if_extents
;
1053 ep
= &base
[idx
+ 1];
1054 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
1055 xfs_bmap_trace_post_update(fname
, "LF", ip
, idx
+ 1,
1060 case MASK2(RIGHT_FILLING
, RIGHT_CONTIG
):
1062 * Filling in the last part of a previous delayed allocation.
1063 * The right neighbor is contiguous with the new allocation.
1065 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1066 xfs_bmap_trace_pre_update(fname
, "RF|RC", ip
, idx
,
1068 xfs_bmap_trace_pre_update(fname
, "RF|RC", ip
, idx
+ 1,
1070 xfs_bmbt_set_blockcount(ep
, temp
);
1071 xfs_bmbt_set_allf(ep
+ 1, 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
,
1101 case MASK(RIGHT_FILLING
):
1103 * Filling in the last part of a previous delayed allocation.
1104 * The right neighbor is not contiguous.
1106 temp
= PREV
.br_blockcount
- new->br_blockcount
;
1107 xfs_bmap_trace_pre_update(fname
, "RF", ip
, idx
, XFS_DATA_FORK
);
1108 xfs_bmbt_set_blockcount(ep
, temp
);
1109 xfs_bmap_trace_insert(fname
, "RF", ip
, idx
+ 1, 1,
1110 new, NULL
, XFS_DATA_FORK
);
1111 xfs_bmap_insert_exlist(ip
, idx
+ 1, 1, new, XFS_DATA_FORK
);
1112 ip
->i_df
.if_lastex
= idx
+ 1;
1113 ip
->i_d
.di_nextents
++;
1115 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1117 rval
= XFS_ILOG_CORE
;
1118 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1119 new->br_startblock
, new->br_blockcount
,
1123 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1124 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1128 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
1129 ip
->i_d
.di_nextents
> ip
->i_df
.if_ext_max
) {
1130 error
= xfs_bmap_extents_to_btree(ip
->i_transp
, ip
,
1131 first
, flist
, &cur
, 1, &tmp_rval
,
1137 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
1138 STARTBLOCKVAL(PREV
.br_startblock
) -
1139 (cur
? cur
->bc_private
.b
.allocated
: 0));
1140 base
= ip
->i_df
.if_u1
.if_extents
;
1142 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
1143 xfs_bmap_trace_post_update(fname
, "RF", ip
, idx
, XFS_DATA_FORK
);
1149 * Filling in the middle part of a previous delayed allocation.
1150 * Contiguity is impossible here.
1151 * This case is avoided almost all the time.
1153 temp
= new->br_startoff
- PREV
.br_startoff
;
1154 xfs_bmap_trace_pre_update(fname
, "0", ip
, idx
, XFS_DATA_FORK
);
1155 xfs_bmbt_set_blockcount(ep
, temp
);
1157 r
[1].br_startoff
= new_endoff
;
1158 temp2
= PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1159 r
[1].br_blockcount
= temp2
;
1160 xfs_bmap_trace_insert(fname
, "0", ip
, idx
+ 1, 2, &r
[0], &r
[1],
1162 xfs_bmap_insert_exlist(ip
, idx
+ 1, 2, &r
[0], XFS_DATA_FORK
);
1163 ip
->i_df
.if_lastex
= idx
+ 1;
1164 ip
->i_d
.di_nextents
++;
1166 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1168 rval
= XFS_ILOG_CORE
;
1169 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1170 new->br_startblock
, new->br_blockcount
,
1174 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1175 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1179 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_EXTENTS
&&
1180 ip
->i_d
.di_nextents
> ip
->i_df
.if_ext_max
) {
1181 error
= xfs_bmap_extents_to_btree(ip
->i_transp
, ip
,
1182 first
, flist
, &cur
, 1, &tmp_rval
,
1188 temp
= xfs_bmap_worst_indlen(ip
, temp
);
1189 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
1190 diff
= (int)(temp
+ temp2
- STARTBLOCKVAL(PREV
.br_startblock
) -
1191 (cur
? cur
->bc_private
.b
.allocated
: 0));
1193 xfs_mod_incore_sb(ip
->i_mount
, XFS_SBS_FDBLOCKS
, -diff
, rsvd
)) {
1195 * Ick gross gag me with a spoon.
1197 ASSERT(0); /* want to see if this ever happens! */
1203 !xfs_mod_incore_sb(ip
->i_mount
,
1204 XFS_SBS_FDBLOCKS
, -diff
, rsvd
))
1211 !xfs_mod_incore_sb(ip
->i_mount
,
1212 XFS_SBS_FDBLOCKS
, -diff
, rsvd
))
1217 base
= ip
->i_df
.if_u1
.if_extents
;
1219 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
1220 xfs_bmap_trace_post_update(fname
, "0", ip
, idx
, XFS_DATA_FORK
);
1221 xfs_bmap_trace_pre_update(fname
, "0", ip
, idx
+ 2,
1223 xfs_bmbt_set_startblock(ep
+ 2, NULLSTARTBLOCK((int)temp2
));
1224 xfs_bmap_trace_post_update(fname
, "0", ip
, idx
+ 2,
1226 *dnew
= temp
+ temp2
;
1229 case MASK3(LEFT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
1230 case MASK3(RIGHT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
1231 case MASK2(LEFT_FILLING
, RIGHT_CONTIG
):
1232 case MASK2(RIGHT_FILLING
, LEFT_CONTIG
):
1233 case MASK2(LEFT_CONTIG
, RIGHT_CONTIG
):
1234 case MASK(LEFT_CONTIG
):
1235 case MASK(RIGHT_CONTIG
):
1237 * These cases are all impossible.
1254 #undef STATE_SET_TEST
1259 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1260 * allocation to a real allocation or vice versa.
1262 STATIC
int /* error */
1263 xfs_bmap_add_extent_unwritten_real(
1264 xfs_inode_t
*ip
, /* incore inode pointer */
1265 xfs_extnum_t idx
, /* extent number to update/insert */
1266 xfs_btree_cur_t
**curp
, /* if *curp is null, not a btree */
1267 xfs_bmbt_irec_t
*new, /* new data to put in extent list */
1268 int *logflagsp
) /* inode logging flags */
1270 xfs_bmbt_rec_t
*base
; /* base of extent entry list */
1271 xfs_btree_cur_t
*cur
; /* btree cursor */
1272 xfs_bmbt_rec_t
*ep
; /* extent entry for idx */
1273 int error
; /* error return value */
1274 #ifdef XFS_BMAP_TRACE
1275 static char fname
[] = "xfs_bmap_add_extent_unwritten_real";
1277 int i
; /* temp state */
1278 xfs_fileoff_t new_endoff
; /* end offset of new entry */
1279 xfs_exntst_t newext
; /* new extent state */
1280 xfs_exntst_t oldext
; /* old extent state */
1281 xfs_bmbt_irec_t r
[3]; /* neighbor extent entries */
1282 /* left is 0, right is 1, prev is 2 */
1283 int rval
=0; /* return value (logging flags) */
1284 int state
= 0;/* state bits, accessed thru macros */
1285 enum { /* bit number definitions for state */
1286 LEFT_CONTIG
, RIGHT_CONTIG
,
1287 LEFT_FILLING
, RIGHT_FILLING
,
1288 LEFT_DELAY
, RIGHT_DELAY
,
1289 LEFT_VALID
, RIGHT_VALID
1295 #define MASK(b) (1 << (b))
1296 #define MASK2(a,b) (MASK(a) | MASK(b))
1297 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
1298 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
1299 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1300 #define STATE_TEST(b) (state & MASK(b))
1301 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1302 ((state &= ~MASK(b)), 0))
1303 #define SWITCH_STATE \
1304 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
1307 * Set up a bunch of variables to make the tests simpler.
1311 base
= ip
->i_df
.if_u1
.if_extents
;
1313 xfs_bmbt_get_all(ep
, &PREV
);
1314 newext
= new->br_state
;
1315 oldext
= (newext
== XFS_EXT_UNWRITTEN
) ?
1316 XFS_EXT_NORM
: XFS_EXT_UNWRITTEN
;
1317 ASSERT(PREV
.br_state
== oldext
);
1318 new_endoff
= new->br_startoff
+ new->br_blockcount
;
1319 ASSERT(PREV
.br_startoff
<= new->br_startoff
);
1320 ASSERT(PREV
.br_startoff
+ PREV
.br_blockcount
>= new_endoff
);
1322 * Set flags determining what part of the previous oldext allocation
1323 * extent is being replaced by a newext allocation.
1325 STATE_SET(LEFT_FILLING
, PREV
.br_startoff
== new->br_startoff
);
1326 STATE_SET(RIGHT_FILLING
,
1327 PREV
.br_startoff
+ PREV
.br_blockcount
== new_endoff
);
1329 * Check and set flags if this segment has a left neighbor.
1330 * Don't set contiguous if the combined extent would be too large.
1332 if (STATE_SET_TEST(LEFT_VALID
, idx
> 0)) {
1333 xfs_bmbt_get_all(ep
- 1, &LEFT
);
1334 STATE_SET(LEFT_DELAY
, ISNULLSTARTBLOCK(LEFT
.br_startblock
));
1336 STATE_SET(LEFT_CONTIG
,
1337 STATE_TEST(LEFT_VALID
) && !STATE_TEST(LEFT_DELAY
) &&
1338 LEFT
.br_startoff
+ LEFT
.br_blockcount
== new->br_startoff
&&
1339 LEFT
.br_startblock
+ LEFT
.br_blockcount
== new->br_startblock
&&
1340 LEFT
.br_state
== newext
&&
1341 LEFT
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
);
1343 * Check and set flags if this segment has a right neighbor.
1344 * Don't set contiguous if the combined extent would be too large.
1345 * Also check for all-three-contiguous being too large.
1347 if (STATE_SET_TEST(RIGHT_VALID
,
1349 ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - 1)) {
1350 xfs_bmbt_get_all(ep
+ 1, &RIGHT
);
1351 STATE_SET(RIGHT_DELAY
, ISNULLSTARTBLOCK(RIGHT
.br_startblock
));
1353 STATE_SET(RIGHT_CONTIG
,
1354 STATE_TEST(RIGHT_VALID
) && !STATE_TEST(RIGHT_DELAY
) &&
1355 new_endoff
== RIGHT
.br_startoff
&&
1356 new->br_startblock
+ new->br_blockcount
==
1357 RIGHT
.br_startblock
&&
1358 newext
== RIGHT
.br_state
&&
1359 new->br_blockcount
+ RIGHT
.br_blockcount
<= MAXEXTLEN
&&
1360 ((state
& MASK3(LEFT_CONTIG
, LEFT_FILLING
, RIGHT_FILLING
)) !=
1361 MASK3(LEFT_CONTIG
, LEFT_FILLING
, RIGHT_FILLING
) ||
1362 LEFT
.br_blockcount
+ new->br_blockcount
+ RIGHT
.br_blockcount
1365 * Switch out based on the FILLING and CONTIG state bits.
1367 switch (SWITCH_STATE
) {
1369 case MASK4(LEFT_FILLING
, RIGHT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
1371 * Setting all of a previous oldext extent to newext.
1372 * The left and right neighbors are both contiguous with new.
1374 xfs_bmap_trace_pre_update(fname
, "LF|RF|LC|RC", ip
, idx
- 1,
1376 xfs_bmbt_set_blockcount(ep
- 1,
1377 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1378 RIGHT
.br_blockcount
);
1379 xfs_bmap_trace_post_update(fname
, "LF|RF|LC|RC", ip
, idx
- 1,
1381 xfs_bmap_trace_delete(fname
, "LF|RF|LC|RC", ip
, idx
, 2,
1383 xfs_bmap_delete_exlist(ip
, idx
, 2, XFS_DATA_FORK
);
1384 ip
->i_df
.if_lastex
= idx
- 1;
1385 ip
->i_d
.di_nextents
-= 2;
1387 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1389 rval
= XFS_ILOG_CORE
;
1390 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
1391 RIGHT
.br_startblock
,
1392 RIGHT
.br_blockcount
, &i
)))
1395 if ((error
= xfs_bmbt_delete(cur
, &i
)))
1398 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
1401 if ((error
= xfs_bmbt_delete(cur
, &i
)))
1404 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
1407 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
1409 LEFT
.br_blockcount
+ PREV
.br_blockcount
+
1410 RIGHT
.br_blockcount
, LEFT
.br_state
)))
1415 case MASK3(LEFT_FILLING
, RIGHT_FILLING
, LEFT_CONTIG
):
1417 * Setting all of a previous oldext extent to newext.
1418 * The left neighbor is contiguous, the right is not.
1420 xfs_bmap_trace_pre_update(fname
, "LF|RF|LC", ip
, idx
- 1,
1422 xfs_bmbt_set_blockcount(ep
- 1,
1423 LEFT
.br_blockcount
+ PREV
.br_blockcount
);
1424 xfs_bmap_trace_post_update(fname
, "LF|RF|LC", ip
, idx
- 1,
1426 ip
->i_df
.if_lastex
= idx
- 1;
1427 xfs_bmap_trace_delete(fname
, "LF|RF|LC", ip
, idx
, 1,
1429 xfs_bmap_delete_exlist(ip
, idx
, 1, XFS_DATA_FORK
);
1430 ip
->i_d
.di_nextents
--;
1432 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1434 rval
= XFS_ILOG_CORE
;
1435 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1436 PREV
.br_startblock
, PREV
.br_blockcount
,
1440 if ((error
= xfs_bmbt_delete(cur
, &i
)))
1443 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
1446 if ((error
= xfs_bmbt_update(cur
, LEFT
.br_startoff
,
1448 LEFT
.br_blockcount
+ PREV
.br_blockcount
,
1454 case MASK3(LEFT_FILLING
, RIGHT_FILLING
, RIGHT_CONTIG
):
1456 * Setting all of a previous oldext extent to newext.
1457 * The right neighbor is contiguous, the left is not.
1459 xfs_bmap_trace_pre_update(fname
, "LF|RF|RC", ip
, idx
,
1461 xfs_bmbt_set_blockcount(ep
,
1462 PREV
.br_blockcount
+ RIGHT
.br_blockcount
);
1463 xfs_bmbt_set_state(ep
, newext
);
1464 xfs_bmap_trace_post_update(fname
, "LF|RF|RC", ip
, idx
,
1466 ip
->i_df
.if_lastex
= idx
;
1467 xfs_bmap_trace_delete(fname
, "LF|RF|RC", ip
, idx
+ 1, 1,
1469 xfs_bmap_delete_exlist(ip
, idx
+ 1, 1, XFS_DATA_FORK
);
1470 ip
->i_d
.di_nextents
--;
1472 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1474 rval
= XFS_ILOG_CORE
;
1475 if ((error
= xfs_bmbt_lookup_eq(cur
, RIGHT
.br_startoff
,
1476 RIGHT
.br_startblock
,
1477 RIGHT
.br_blockcount
, &i
)))
1480 if ((error
= xfs_bmbt_delete(cur
, &i
)))
1483 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
1486 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
1488 new->br_blockcount
+ RIGHT
.br_blockcount
,
1494 case MASK2(LEFT_FILLING
, RIGHT_FILLING
):
1496 * Setting all of a previous oldext extent to newext.
1497 * Neither the left nor right neighbors are contiguous with
1500 xfs_bmap_trace_pre_update(fname
, "LF|RF", ip
, idx
,
1502 xfs_bmbt_set_state(ep
, newext
);
1503 xfs_bmap_trace_post_update(fname
, "LF|RF", ip
, idx
,
1505 ip
->i_df
.if_lastex
= idx
;
1507 rval
= XFS_ILOG_DEXT
;
1510 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1511 new->br_startblock
, new->br_blockcount
,
1515 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
1516 new->br_startblock
, new->br_blockcount
,
1522 case MASK2(LEFT_FILLING
, LEFT_CONTIG
):
1524 * Setting the first part of a previous oldext extent to newext.
1525 * The left neighbor is contiguous.
1527 xfs_bmap_trace_pre_update(fname
, "LF|LC", ip
, idx
- 1,
1529 xfs_bmbt_set_blockcount(ep
- 1,
1530 LEFT
.br_blockcount
+ new->br_blockcount
);
1531 xfs_bmbt_set_startoff(ep
,
1532 PREV
.br_startoff
+ new->br_blockcount
);
1533 xfs_bmap_trace_post_update(fname
, "LF|LC", ip
, idx
- 1,
1535 xfs_bmap_trace_pre_update(fname
, "LF|LC", ip
, idx
,
1537 xfs_bmbt_set_startblock(ep
,
1538 new->br_startblock
+ new->br_blockcount
);
1539 xfs_bmbt_set_blockcount(ep
,
1540 PREV
.br_blockcount
- new->br_blockcount
);
1541 xfs_bmap_trace_post_update(fname
, "LF|LC", ip
, idx
,
1543 ip
->i_df
.if_lastex
= idx
- 1;
1545 rval
= XFS_ILOG_DEXT
;
1548 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1549 PREV
.br_startblock
, PREV
.br_blockcount
,
1553 if ((error
= xfs_bmbt_update(cur
,
1554 PREV
.br_startoff
+ new->br_blockcount
,
1555 PREV
.br_startblock
+ new->br_blockcount
,
1556 PREV
.br_blockcount
- new->br_blockcount
,
1559 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
1561 if (xfs_bmbt_update(cur
, LEFT
.br_startoff
,
1563 LEFT
.br_blockcount
+ new->br_blockcount
,
1569 case MASK(LEFT_FILLING
):
1571 * Setting the first part of a previous oldext extent to newext.
1572 * The left neighbor is not contiguous.
1574 xfs_bmap_trace_pre_update(fname
, "LF", ip
, idx
, XFS_DATA_FORK
);
1575 ASSERT(ep
&& xfs_bmbt_get_state(ep
) == oldext
);
1576 xfs_bmbt_set_startoff(ep
, new_endoff
);
1577 xfs_bmbt_set_blockcount(ep
,
1578 PREV
.br_blockcount
- new->br_blockcount
);
1579 xfs_bmbt_set_startblock(ep
,
1580 new->br_startblock
+ new->br_blockcount
);
1581 xfs_bmap_trace_post_update(fname
, "LF", ip
, idx
, XFS_DATA_FORK
);
1582 xfs_bmap_trace_insert(fname
, "LF", ip
, idx
, 1, new, NULL
,
1584 xfs_bmap_insert_exlist(ip
, idx
, 1, new, XFS_DATA_FORK
);
1585 ip
->i_df
.if_lastex
= idx
;
1586 ip
->i_d
.di_nextents
++;
1588 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1590 rval
= XFS_ILOG_CORE
;
1591 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1592 PREV
.br_startblock
, PREV
.br_blockcount
,
1596 if ((error
= xfs_bmbt_update(cur
,
1597 PREV
.br_startoff
+ new->br_blockcount
,
1598 PREV
.br_startblock
+ new->br_blockcount
,
1599 PREV
.br_blockcount
- new->br_blockcount
,
1602 cur
->bc_rec
.b
= *new;
1603 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1609 case MASK2(RIGHT_FILLING
, RIGHT_CONTIG
):
1611 * Setting the last part of a previous oldext extent to newext.
1612 * The right neighbor is contiguous with the new allocation.
1614 xfs_bmap_trace_pre_update(fname
, "RF|RC", ip
, idx
,
1616 xfs_bmap_trace_pre_update(fname
, "RF|RC", ip
, idx
+ 1,
1618 xfs_bmbt_set_blockcount(ep
,
1619 PREV
.br_blockcount
- new->br_blockcount
);
1620 xfs_bmap_trace_post_update(fname
, "RF|RC", ip
, idx
,
1622 xfs_bmbt_set_allf(ep
+ 1, new->br_startoff
, new->br_startblock
,
1623 new->br_blockcount
+ RIGHT
.br_blockcount
, newext
);
1624 xfs_bmap_trace_post_update(fname
, "RF|RC", ip
, idx
+ 1,
1626 ip
->i_df
.if_lastex
= idx
+ 1;
1628 rval
= XFS_ILOG_DEXT
;
1631 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1633 PREV
.br_blockcount
, &i
)))
1636 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
1638 PREV
.br_blockcount
- new->br_blockcount
,
1641 if ((error
= xfs_bmbt_increment(cur
, 0, &i
)))
1643 if ((error
= xfs_bmbt_update(cur
, new->br_startoff
,
1645 new->br_blockcount
+ RIGHT
.br_blockcount
,
1651 case MASK(RIGHT_FILLING
):
1653 * Setting the last part of a previous oldext extent to newext.
1654 * The right neighbor is not contiguous.
1656 xfs_bmap_trace_pre_update(fname
, "RF", ip
, idx
, XFS_DATA_FORK
);
1657 xfs_bmbt_set_blockcount(ep
,
1658 PREV
.br_blockcount
- new->br_blockcount
);
1659 xfs_bmap_trace_post_update(fname
, "RF", ip
, idx
, XFS_DATA_FORK
);
1660 xfs_bmap_trace_insert(fname
, "RF", ip
, idx
+ 1, 1,
1661 new, NULL
, XFS_DATA_FORK
);
1662 xfs_bmap_insert_exlist(ip
, idx
+ 1, 1, new, XFS_DATA_FORK
);
1663 ip
->i_df
.if_lastex
= idx
+ 1;
1664 ip
->i_d
.di_nextents
++;
1666 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1668 rval
= XFS_ILOG_CORE
;
1669 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1670 PREV
.br_startblock
, PREV
.br_blockcount
,
1674 if ((error
= xfs_bmbt_update(cur
, PREV
.br_startoff
,
1676 PREV
.br_blockcount
- new->br_blockcount
,
1679 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
1680 new->br_startblock
, new->br_blockcount
,
1684 cur
->bc_rec
.b
.br_state
= XFS_EXT_NORM
;
1685 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1693 * Setting the middle part of a previous oldext extent to
1694 * newext. Contiguity is impossible here.
1695 * One extent becomes three extents.
1697 xfs_bmap_trace_pre_update(fname
, "0", ip
, idx
, XFS_DATA_FORK
);
1698 xfs_bmbt_set_blockcount(ep
,
1699 new->br_startoff
- PREV
.br_startoff
);
1700 xfs_bmap_trace_post_update(fname
, "0", ip
, idx
, XFS_DATA_FORK
);
1702 r
[1].br_startoff
= new_endoff
;
1703 r
[1].br_blockcount
=
1704 PREV
.br_startoff
+ PREV
.br_blockcount
- new_endoff
;
1705 r
[1].br_startblock
= new->br_startblock
+ new->br_blockcount
;
1706 r
[1].br_state
= oldext
;
1707 xfs_bmap_trace_insert(fname
, "0", ip
, idx
+ 1, 2, &r
[0], &r
[1],
1709 xfs_bmap_insert_exlist(ip
, idx
+ 1, 2, &r
[0], XFS_DATA_FORK
);
1710 ip
->i_df
.if_lastex
= idx
+ 1;
1711 ip
->i_d
.di_nextents
+= 2;
1713 rval
= XFS_ILOG_CORE
| XFS_ILOG_DEXT
;
1715 rval
= XFS_ILOG_CORE
;
1716 if ((error
= xfs_bmbt_lookup_eq(cur
, PREV
.br_startoff
,
1717 PREV
.br_startblock
, PREV
.br_blockcount
,
1721 /* new right extent - oldext */
1722 if ((error
= xfs_bmbt_update(cur
, r
[1].br_startoff
,
1723 r
[1].br_startblock
, r
[1].br_blockcount
,
1726 /* new left extent - oldext */
1727 PREV
.br_blockcount
=
1728 new->br_startoff
- PREV
.br_startoff
;
1729 cur
->bc_rec
.b
= PREV
;
1730 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1733 if ((error
= xfs_bmbt_increment(cur
, 0, &i
)))
1736 /* new middle extent - newext */
1737 cur
->bc_rec
.b
= *new;
1738 if ((error
= xfs_bmbt_insert(cur
, &i
)))
1744 case MASK3(LEFT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
1745 case MASK3(RIGHT_FILLING
, LEFT_CONTIG
, RIGHT_CONTIG
):
1746 case MASK2(LEFT_FILLING
, RIGHT_CONTIG
):
1747 case MASK2(RIGHT_FILLING
, LEFT_CONTIG
):
1748 case MASK2(LEFT_CONTIG
, RIGHT_CONTIG
):
1749 case MASK(LEFT_CONTIG
):
1750 case MASK(RIGHT_CONTIG
):
1752 * These cases are all impossible.
1769 #undef STATE_SET_TEST
1774 * Called by xfs_bmap_add_extent to handle cases converting a hole
1775 * to a delayed allocation.
1778 STATIC
int /* error */
1779 xfs_bmap_add_extent_hole_delay(
1780 xfs_inode_t
*ip
, /* incore inode pointer */
1781 xfs_extnum_t idx
, /* extent number to update/insert */
1782 xfs_btree_cur_t
*cur
, /* if null, not a btree */
1783 xfs_bmbt_irec_t
*new, /* new data to put in extent list */
1784 int *logflagsp
, /* inode logging flags */
1785 int rsvd
) /* OK to allocate reserved blocks */
1787 xfs_bmbt_rec_t
*base
; /* base of extent entry list */
1788 xfs_bmbt_rec_t
*ep
; /* extent list entry for idx */
1789 #ifdef XFS_BMAP_TRACE
1790 static char fname
[] = "xfs_bmap_add_extent_hole_delay";
1792 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
1793 xfs_filblks_t newlen
=0; /* new indirect size */
1794 xfs_filblks_t oldlen
=0; /* old indirect size */
1795 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
1796 int state
; /* state bits, accessed thru macros */
1797 xfs_filblks_t temp
; /* temp for indirect calculations */
1798 enum { /* bit number definitions for state */
1799 LEFT_CONTIG
, RIGHT_CONTIG
,
1800 LEFT_DELAY
, RIGHT_DELAY
,
1801 LEFT_VALID
, RIGHT_VALID
1804 #define MASK(b) (1 << (b))
1805 #define MASK2(a,b) (MASK(a) | MASK(b))
1806 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1807 #define STATE_TEST(b) (state & MASK(b))
1808 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1809 ((state &= ~MASK(b)), 0))
1810 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1812 base
= ip
->i_df
.if_u1
.if_extents
;
1815 ASSERT(ISNULLSTARTBLOCK(new->br_startblock
));
1817 * Check and set flags if this segment has a left neighbor
1819 if (STATE_SET_TEST(LEFT_VALID
, idx
> 0)) {
1820 xfs_bmbt_get_all(ep
- 1, &left
);
1821 STATE_SET(LEFT_DELAY
, ISNULLSTARTBLOCK(left
.br_startblock
));
1824 * Check and set flags if the current (right) segment exists.
1825 * If it doesn't exist, we're converting the hole at end-of-file.
1827 if (STATE_SET_TEST(RIGHT_VALID
,
1829 ip
->i_df
.if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1830 xfs_bmbt_get_all(ep
, &right
);
1831 STATE_SET(RIGHT_DELAY
, ISNULLSTARTBLOCK(right
.br_startblock
));
1834 * Set contiguity flags on the left and right neighbors.
1835 * Don't let extents get too large, even if the pieces are contiguous.
1837 STATE_SET(LEFT_CONTIG
,
1838 STATE_TEST(LEFT_VALID
) && STATE_TEST(LEFT_DELAY
) &&
1839 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
1840 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
);
1841 STATE_SET(RIGHT_CONTIG
,
1842 STATE_TEST(RIGHT_VALID
) && STATE_TEST(RIGHT_DELAY
) &&
1843 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
1844 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
1845 (!STATE_TEST(LEFT_CONTIG
) ||
1846 (left
.br_blockcount
+ new->br_blockcount
+
1847 right
.br_blockcount
<= MAXEXTLEN
)));
1849 * Switch out based on the contiguity flags.
1851 switch (SWITCH_STATE
) {
1853 case MASK2(LEFT_CONTIG
, RIGHT_CONTIG
):
1855 * New allocation is contiguous with delayed allocations
1856 * on the left and on the right.
1857 * Merge all three into a single extent list entry.
1859 temp
= left
.br_blockcount
+ new->br_blockcount
+
1860 right
.br_blockcount
;
1861 xfs_bmap_trace_pre_update(fname
, "LC|RC", ip
, idx
- 1,
1863 xfs_bmbt_set_blockcount(ep
- 1, temp
);
1864 oldlen
= STARTBLOCKVAL(left
.br_startblock
) +
1865 STARTBLOCKVAL(new->br_startblock
) +
1866 STARTBLOCKVAL(right
.br_startblock
);
1867 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
1868 xfs_bmbt_set_startblock(ep
- 1, NULLSTARTBLOCK((int)newlen
));
1869 xfs_bmap_trace_post_update(fname
, "LC|RC", ip
, idx
- 1,
1871 xfs_bmap_trace_delete(fname
, "LC|RC", ip
, idx
, 1,
1873 xfs_bmap_delete_exlist(ip
, idx
, 1, XFS_DATA_FORK
);
1874 ip
->i_df
.if_lastex
= idx
- 1;
1877 case MASK(LEFT_CONTIG
):
1879 * New allocation is contiguous with a delayed allocation
1881 * Merge the new allocation with the left neighbor.
1883 temp
= left
.br_blockcount
+ new->br_blockcount
;
1884 xfs_bmap_trace_pre_update(fname
, "LC", ip
, idx
- 1,
1886 xfs_bmbt_set_blockcount(ep
- 1, temp
);
1887 oldlen
= STARTBLOCKVAL(left
.br_startblock
) +
1888 STARTBLOCKVAL(new->br_startblock
);
1889 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
1890 xfs_bmbt_set_startblock(ep
- 1, NULLSTARTBLOCK((int)newlen
));
1891 xfs_bmap_trace_post_update(fname
, "LC", ip
, idx
- 1,
1893 ip
->i_df
.if_lastex
= idx
- 1;
1896 case MASK(RIGHT_CONTIG
):
1898 * New allocation is contiguous with a delayed allocation
1900 * Merge the new allocation with the right neighbor.
1902 xfs_bmap_trace_pre_update(fname
, "RC", ip
, idx
, XFS_DATA_FORK
);
1903 temp
= new->br_blockcount
+ right
.br_blockcount
;
1904 oldlen
= STARTBLOCKVAL(new->br_startblock
) +
1905 STARTBLOCKVAL(right
.br_startblock
);
1906 newlen
= xfs_bmap_worst_indlen(ip
, temp
);
1907 xfs_bmbt_set_allf(ep
, new->br_startoff
,
1908 NULLSTARTBLOCK((int)newlen
), temp
, right
.br_state
);
1909 xfs_bmap_trace_post_update(fname
, "RC", ip
, idx
, XFS_DATA_FORK
);
1910 ip
->i_df
.if_lastex
= idx
;
1915 * New allocation is not contiguous with another
1916 * delayed allocation.
1917 * Insert a new entry.
1919 oldlen
= newlen
= 0;
1920 xfs_bmap_trace_insert(fname
, "0", ip
, idx
, 1, new, NULL
,
1922 xfs_bmap_insert_exlist(ip
, idx
, 1, new, XFS_DATA_FORK
);
1923 ip
->i_df
.if_lastex
= idx
;
1926 if (oldlen
!= newlen
) {
1927 ASSERT(oldlen
> newlen
);
1928 xfs_mod_incore_sb(ip
->i_mount
, XFS_SBS_FDBLOCKS
,
1929 (int)(oldlen
- newlen
), rsvd
);
1931 * Nothing to do for disk quota accounting here.
1940 #undef STATE_SET_TEST
1945 * Called by xfs_bmap_add_extent to handle cases converting a hole
1946 * to a real allocation.
1948 STATIC
int /* error */
1949 xfs_bmap_add_extent_hole_real(
1950 xfs_inode_t
*ip
, /* incore inode pointer */
1951 xfs_extnum_t idx
, /* extent number to update/insert */
1952 xfs_btree_cur_t
*cur
, /* if null, not a btree */
1953 xfs_bmbt_irec_t
*new, /* new data to put in extent list */
1954 int *logflagsp
, /* inode logging flags */
1955 int whichfork
) /* data or attr fork */
1957 xfs_bmbt_rec_t
*ep
; /* pointer to extent entry ins. point */
1958 int error
; /* error return value */
1959 #ifdef XFS_BMAP_TRACE
1960 static char fname
[] = "xfs_bmap_add_extent_hole_real";
1962 int i
; /* temp state */
1963 xfs_ifork_t
*ifp
; /* inode fork pointer */
1964 xfs_bmbt_irec_t left
; /* left neighbor extent entry */
1965 xfs_bmbt_irec_t right
; /* right neighbor extent entry */
1966 int state
; /* state bits, accessed thru macros */
1967 enum { /* bit number definitions for state */
1968 LEFT_CONTIG
, RIGHT_CONTIG
,
1969 LEFT_DELAY
, RIGHT_DELAY
,
1970 LEFT_VALID
, RIGHT_VALID
1973 #define MASK(b) (1 << (b))
1974 #define MASK2(a,b) (MASK(a) | MASK(b))
1975 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1976 #define STATE_TEST(b) (state & MASK(b))
1977 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1978 ((state &= ~MASK(b)), 0))
1979 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1981 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
1982 ASSERT(idx
<= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
));
1983 ep
= &ifp
->if_u1
.if_extents
[idx
];
1986 * Check and set flags if this segment has a left neighbor.
1988 if (STATE_SET_TEST(LEFT_VALID
, idx
> 0)) {
1989 xfs_bmbt_get_all(ep
- 1, &left
);
1990 STATE_SET(LEFT_DELAY
, ISNULLSTARTBLOCK(left
.br_startblock
));
1993 * Check and set flags if this segment has a current value.
1994 * Not true if we're inserting into the "hole" at eof.
1996 if (STATE_SET_TEST(RIGHT_VALID
,
1998 ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
))) {
1999 xfs_bmbt_get_all(ep
, &right
);
2000 STATE_SET(RIGHT_DELAY
, ISNULLSTARTBLOCK(right
.br_startblock
));
2003 * We're inserting a real allocation between "left" and "right".
2004 * Set the contiguity flags. Don't let extents get too large.
2006 STATE_SET(LEFT_CONTIG
,
2007 STATE_TEST(LEFT_VALID
) && !STATE_TEST(LEFT_DELAY
) &&
2008 left
.br_startoff
+ left
.br_blockcount
== new->br_startoff
&&
2009 left
.br_startblock
+ left
.br_blockcount
== new->br_startblock
&&
2010 left
.br_state
== new->br_state
&&
2011 left
.br_blockcount
+ new->br_blockcount
<= MAXEXTLEN
);
2012 STATE_SET(RIGHT_CONTIG
,
2013 STATE_TEST(RIGHT_VALID
) && !STATE_TEST(RIGHT_DELAY
) &&
2014 new->br_startoff
+ new->br_blockcount
== right
.br_startoff
&&
2015 new->br_startblock
+ new->br_blockcount
==
2016 right
.br_startblock
&&
2017 new->br_state
== right
.br_state
&&
2018 new->br_blockcount
+ right
.br_blockcount
<= MAXEXTLEN
&&
2019 (!STATE_TEST(LEFT_CONTIG
) ||
2020 left
.br_blockcount
+ new->br_blockcount
+
2021 right
.br_blockcount
<= MAXEXTLEN
));
2024 * Select which case we're in here, and implement it.
2026 switch (SWITCH_STATE
) {
2028 case MASK2(LEFT_CONTIG
, RIGHT_CONTIG
):
2030 * New allocation is contiguous with real allocations on the
2031 * left and on the right.
2032 * Merge all three into a single extent list entry.
2034 xfs_bmap_trace_pre_update(fname
, "LC|RC", ip
, idx
- 1,
2036 xfs_bmbt_set_blockcount(ep
- 1,
2037 left
.br_blockcount
+ new->br_blockcount
+
2038 right
.br_blockcount
);
2039 xfs_bmap_trace_post_update(fname
, "LC|RC", ip
, idx
- 1,
2041 xfs_bmap_trace_delete(fname
, "LC|RC", ip
,
2043 xfs_bmap_delete_exlist(ip
, idx
, 1, whichfork
);
2044 ifp
->if_lastex
= idx
- 1;
2045 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2046 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2048 *logflagsp
= XFS_ILOG_CORE
| XFS_ILOG_FEXT(whichfork
);
2051 *logflagsp
= XFS_ILOG_CORE
;
2052 if ((error
= xfs_bmbt_lookup_eq(cur
, right
.br_startoff
,
2053 right
.br_startblock
, right
.br_blockcount
, &i
)))
2056 if ((error
= xfs_bmbt_delete(cur
, &i
)))
2059 if ((error
= xfs_bmbt_decrement(cur
, 0, &i
)))
2062 error
= xfs_bmbt_update(cur
, left
.br_startoff
,
2064 left
.br_blockcount
+ new->br_blockcount
+
2065 right
.br_blockcount
, left
.br_state
);
2068 case MASK(LEFT_CONTIG
):
2070 * New allocation is contiguous with a real allocation
2072 * Merge the new allocation with the left neighbor.
2074 xfs_bmap_trace_pre_update(fname
, "LC", ip
, idx
- 1, whichfork
);
2075 xfs_bmbt_set_blockcount(ep
- 1,
2076 left
.br_blockcount
+ new->br_blockcount
);
2077 xfs_bmap_trace_post_update(fname
, "LC", ip
, idx
- 1, whichfork
);
2078 ifp
->if_lastex
= idx
- 1;
2080 *logflagsp
= XFS_ILOG_FEXT(whichfork
);
2084 if ((error
= xfs_bmbt_lookup_eq(cur
, left
.br_startoff
,
2085 left
.br_startblock
, left
.br_blockcount
, &i
)))
2088 error
= xfs_bmbt_update(cur
, left
.br_startoff
,
2090 left
.br_blockcount
+ new->br_blockcount
,
2094 case MASK(RIGHT_CONTIG
):
2096 * New allocation is contiguous with a real allocation
2098 * Merge the new allocation with the right neighbor.
2100 xfs_bmap_trace_pre_update(fname
, "RC", ip
, idx
, whichfork
);
2101 xfs_bmbt_set_allf(ep
, new->br_startoff
, new->br_startblock
,
2102 new->br_blockcount
+ right
.br_blockcount
,
2104 xfs_bmap_trace_post_update(fname
, "RC", ip
, idx
, whichfork
);
2105 ifp
->if_lastex
= idx
;
2107 *logflagsp
= XFS_ILOG_FEXT(whichfork
);
2111 if ((error
= xfs_bmbt_lookup_eq(cur
, right
.br_startoff
,
2112 right
.br_startblock
, right
.br_blockcount
, &i
)))
2115 error
= xfs_bmbt_update(cur
, new->br_startoff
,
2117 new->br_blockcount
+ right
.br_blockcount
,
2123 * New allocation is not contiguous with another
2125 * Insert a new entry.
2127 xfs_bmap_trace_insert(fname
, "0", ip
, idx
, 1, new, NULL
,
2129 xfs_bmap_insert_exlist(ip
, idx
, 1, new, whichfork
);
2130 ifp
->if_lastex
= idx
;
2131 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2132 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
2134 *logflagsp
= XFS_ILOG_CORE
| XFS_ILOG_FEXT(whichfork
);
2137 *logflagsp
= XFS_ILOG_CORE
;
2138 if ((error
= xfs_bmbt_lookup_eq(cur
, new->br_startoff
,
2139 new->br_startblock
, new->br_blockcount
, &i
)))
2142 cur
->bc_rec
.b
.br_state
= new->br_state
;
2143 if ((error
= xfs_bmbt_insert(cur
, &i
)))
2152 #undef STATE_SET_TEST
2156 return 0; /* keep gcc quite */
2159 #define XFS_ALLOC_GAP_UNITS 4
2162 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2163 * It figures out where to ask the underlying allocator to put the new extent.
2165 STATIC
int /* error */
2167 xfs_bmalloca_t
*ap
) /* bmap alloc argument struct */
2169 xfs_fsblock_t adjust
; /* adjustment to block numbers */
2170 xfs_alloctype_t atype
=0; /* type for allocation routines */
2171 int error
; /* error return value */
2172 xfs_agnumber_t fb_agno
; /* ag number of ap->firstblock */
2173 xfs_mount_t
*mp
; /* mount point structure */
2174 int nullfb
; /* true if ap->firstblock isn't set */
2175 int rt
; /* true if inode is realtime */
2177 xfs_extlen_t prod
=0; /* product factor for allocators */
2178 xfs_extlen_t ralen
=0; /* realtime allocation length */
2181 #define ISVALID(x,y) \
2183 (x) < mp->m_sb.sb_rblocks : \
2184 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2185 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2186 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2191 mp
= ap
->ip
->i_mount
;
2192 nullfb
= ap
->firstblock
== NULLFSBLOCK
;
2193 rt
= XFS_IS_REALTIME_INODE(ap
->ip
) && ap
->userdata
;
2194 fb_agno
= nullfb
? NULLAGNUMBER
: XFS_FSB_TO_AGNO(mp
, ap
->firstblock
);
2197 xfs_extlen_t extsz
; /* file extent size for rt */
2198 xfs_fileoff_t nexto
; /* next file offset */
2199 xfs_extlen_t orig_alen
; /* original ap->alen */
2200 xfs_fileoff_t orig_end
; /* original off+len */
2201 xfs_fileoff_t orig_off
; /* original ap->off */
2202 xfs_extlen_t mod_off
; /* modulus calculations */
2203 xfs_fileoff_t prevo
; /* previous file offset */
2204 xfs_rtblock_t rtx
; /* realtime extent number */
2205 xfs_extlen_t temp
; /* temp for rt calculations */
2208 * Set prod to match the realtime extent size.
2210 if (!(extsz
= ap
->ip
->i_d
.di_extsize
))
2211 extsz
= mp
->m_sb
.sb_rextsize
;
2212 prod
= extsz
/ mp
->m_sb
.sb_rextsize
;
2214 orig_alen
= ap
->alen
;
2215 orig_end
= orig_off
+ orig_alen
;
2217 * If the file offset is unaligned vs. the extent size
2218 * we need to align it. This will be possible unless
2219 * the file was previously written with a kernel that didn't
2220 * perform this alignment.
2222 mod_off
= do_mod(orig_off
, extsz
);
2224 ap
->alen
+= mod_off
;
2228 * Same adjustment for the end of the requested area.
2230 if ((temp
= (ap
->alen
% extsz
)))
2231 ap
->alen
+= extsz
- temp
;
2233 * If the previous block overlaps with this proposed allocation
2234 * then move the start forward without adjusting the length.
2237 ap
->prevp
->br_startoff
== NULLFILEOFF
?
2239 (ap
->prevp
->br_startoff
+
2240 ap
->prevp
->br_blockcount
);
2241 if (ap
->off
!= orig_off
&& ap
->off
< prevo
)
2244 * If the next block overlaps with this proposed allocation
2245 * then move the start back without adjusting the length,
2246 * but not before offset 0.
2247 * This may of course make the start overlap previous block,
2248 * and if we hit the offset 0 limit then the next block
2249 * can still overlap too.
2251 nexto
= (ap
->eof
|| ap
->gotp
->br_startoff
== NULLFILEOFF
) ?
2252 NULLFILEOFF
: ap
->gotp
->br_startoff
;
2254 ap
->off
+ ap
->alen
!= orig_end
&&
2255 ap
->off
+ ap
->alen
> nexto
)
2256 ap
->off
= nexto
> ap
->alen
? nexto
- ap
->alen
: 0;
2258 * If we're now overlapping the next or previous extent that
2259 * means we can't fit an extsz piece in this hole. Just move
2260 * the start forward to the first valid spot and set
2261 * the length so we hit the end.
2263 if ((ap
->off
!= orig_off
&& ap
->off
< prevo
) ||
2264 (ap
->off
+ ap
->alen
!= orig_end
&&
2265 ap
->off
+ ap
->alen
> nexto
)) {
2267 ap
->alen
= nexto
- prevo
;
2270 * If the result isn't a multiple of rtextents we need to
2271 * remove blocks until it is.
2273 if ((temp
= (ap
->alen
% mp
->m_sb
.sb_rextsize
))) {
2275 * We're not covering the original request, or
2276 * we won't be able to once we fix the length.
2278 if (orig_off
< ap
->off
||
2279 orig_end
> ap
->off
+ ap
->alen
||
2280 ap
->alen
- temp
< orig_alen
)
2281 return XFS_ERROR(EINVAL
);
2283 * Try to fix it by moving the start up.
2285 if (ap
->off
+ temp
<= orig_off
) {
2290 * Try to fix it by moving the end in.
2292 else if (ap
->off
+ ap
->alen
- temp
>= orig_end
)
2295 * Set the start to the minimum then trim the length.
2298 ap
->alen
-= orig_off
- ap
->off
;
2300 ap
->alen
-= ap
->alen
% mp
->m_sb
.sb_rextsize
;
2303 * Result doesn't cover the request, fail it.
2305 if (orig_off
< ap
->off
|| orig_end
> ap
->off
+ ap
->alen
)
2306 return XFS_ERROR(EINVAL
);
2308 ASSERT(ap
->alen
% mp
->m_sb
.sb_rextsize
== 0);
2310 * If the offset & length are not perfectly aligned
2311 * then kill prod, it will just get us in trouble.
2313 if (do_mod(ap
->off
, extsz
) || ap
->alen
% extsz
)
2316 * Set ralen to be the actual requested length in rtextents.
2318 ralen
= ap
->alen
/ mp
->m_sb
.sb_rextsize
;
2320 * If the old value was close enough to MAXEXTLEN that
2321 * we rounded up to it, cut it back so it's valid again.
2322 * Note that if it's a really large request (bigger than
2323 * MAXEXTLEN), we don't hear about that number, and can't
2324 * adjust the starting point to match it.
2326 if (ralen
* mp
->m_sb
.sb_rextsize
>= MAXEXTLEN
)
2327 ralen
= MAXEXTLEN
/ mp
->m_sb
.sb_rextsize
;
2329 * If it's an allocation to an empty file at offset 0,
2330 * pick an extent that will space things out in the rt area.
2332 if (ap
->eof
&& ap
->off
== 0) {
2333 error
= xfs_rtpick_extent(mp
, ap
->tp
, ralen
, &rtx
);
2336 ap
->rval
= rtx
* mp
->m_sb
.sb_rextsize
;
2343 #endif /* __KERNEL__ */
2345 ap
->rval
= XFS_INO_TO_FSB(mp
, ap
->ip
->i_ino
);
2347 ap
->rval
= ap
->firstblock
;
2349 * If allocating at eof, and there's a previous real block,
2350 * try to use it's last block as our starting point.
2352 if (ap
->eof
&& ap
->prevp
->br_startoff
!= NULLFILEOFF
&&
2353 !ISNULLSTARTBLOCK(ap
->prevp
->br_startblock
) &&
2354 ISVALID(ap
->prevp
->br_startblock
+ ap
->prevp
->br_blockcount
,
2355 ap
->prevp
->br_startblock
)) {
2356 ap
->rval
= ap
->prevp
->br_startblock
+ ap
->prevp
->br_blockcount
;
2358 * Adjust for the gap between prevp and us.
2361 (ap
->prevp
->br_startoff
+ ap
->prevp
->br_blockcount
);
2363 ISVALID(ap
->rval
+ adjust
, ap
->prevp
->br_startblock
))
2367 * If not at eof, then compare the two neighbor blocks.
2368 * Figure out whether either one gives us a good starting point,
2369 * and pick the better one.
2371 else if (!ap
->eof
) {
2372 xfs_fsblock_t gotbno
; /* right side block number */
2373 xfs_fsblock_t gotdiff
=0; /* right side difference */
2374 xfs_fsblock_t prevbno
; /* left side block number */
2375 xfs_fsblock_t prevdiff
=0; /* left side difference */
2378 * If there's a previous (left) block, select a requested
2379 * start block based on it.
2381 if (ap
->prevp
->br_startoff
!= NULLFILEOFF
&&
2382 !ISNULLSTARTBLOCK(ap
->prevp
->br_startblock
) &&
2383 (prevbno
= ap
->prevp
->br_startblock
+
2384 ap
->prevp
->br_blockcount
) &&
2385 ISVALID(prevbno
, ap
->prevp
->br_startblock
)) {
2387 * Calculate gap to end of previous block.
2389 adjust
= prevdiff
= ap
->off
-
2390 (ap
->prevp
->br_startoff
+
2391 ap
->prevp
->br_blockcount
);
2393 * Figure the startblock based on the previous block's
2394 * end and the gap size.
2396 * If the gap is large relative to the piece we're
2397 * allocating, or using it gives us an invalid block
2398 * number, then just use the end of the previous block.
2400 if (prevdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->alen
&&
2401 ISVALID(prevbno
+ prevdiff
,
2402 ap
->prevp
->br_startblock
))
2407 * If the firstblock forbids it, can't use it,
2410 if (!rt
&& !nullfb
&&
2411 XFS_FSB_TO_AGNO(mp
, prevbno
) != fb_agno
)
2412 prevbno
= NULLFSBLOCK
;
2415 * No previous block or can't follow it, just default.
2418 prevbno
= NULLFSBLOCK
;
2420 * If there's a following (right) block, select a requested
2421 * start block based on it.
2423 if (!ISNULLSTARTBLOCK(ap
->gotp
->br_startblock
)) {
2425 * Calculate gap to start of next block.
2427 adjust
= gotdiff
= ap
->gotp
->br_startoff
- ap
->off
;
2429 * Figure the startblock based on the next block's
2430 * start and the gap size.
2432 gotbno
= ap
->gotp
->br_startblock
;
2435 * If the gap is large relative to the piece we're
2436 * allocating, or using it gives us an invalid block
2437 * number, then just use the start of the next block
2438 * offset by our length.
2440 if (gotdiff
<= XFS_ALLOC_GAP_UNITS
* ap
->alen
&&
2441 ISVALID(gotbno
- gotdiff
, gotbno
))
2443 else if (ISVALID(gotbno
- ap
->alen
, gotbno
)) {
2445 gotdiff
+= adjust
- ap
->alen
;
2449 * If the firstblock forbids it, can't use it,
2452 if (!rt
&& !nullfb
&&
2453 XFS_FSB_TO_AGNO(mp
, gotbno
) != fb_agno
)
2454 gotbno
= NULLFSBLOCK
;
2457 * No next block, just default.
2460 gotbno
= NULLFSBLOCK
;
2462 * If both valid, pick the better one, else the only good
2463 * one, else ap->rval is already set (to 0 or the inode block).
2465 if (prevbno
!= NULLFSBLOCK
&& gotbno
!= NULLFSBLOCK
)
2466 ap
->rval
= prevdiff
<= gotdiff
? prevbno
: gotbno
;
2467 else if (prevbno
!= NULLFSBLOCK
)
2469 else if (gotbno
!= NULLFSBLOCK
)
2473 * If allowed, use ap->rval; otherwise must use firstblock since
2474 * it's in the right allocation group.
2476 if (nullfb
|| rt
|| XFS_FSB_TO_AGNO(mp
, ap
->rval
) == fb_agno
)
2479 ap
->rval
= ap
->firstblock
;
2481 * Realtime allocation, done through xfs_rtallocate_extent.
2489 atype
= ap
->rval
== 0 ?
2490 XFS_ALLOCTYPE_ANY_AG
: XFS_ALLOCTYPE_NEAR_BNO
;
2491 do_div(ap
->rval
, mp
->m_sb
.sb_rextsize
);
2494 if ((error
= xfs_rtallocate_extent(ap
->tp
, ap
->rval
, 1, ap
->alen
,
2495 &ralen
, atype
, ap
->wasdel
, prod
, &rtb
)))
2497 if (rtb
== NULLFSBLOCK
&& prod
> 1 &&
2498 (error
= xfs_rtallocate_extent(ap
->tp
, ap
->rval
, 1,
2499 ap
->alen
, &ralen
, atype
,
2500 ap
->wasdel
, 1, &rtb
)))
2503 if (ap
->rval
!= NULLFSBLOCK
) {
2504 ap
->rval
*= mp
->m_sb
.sb_rextsize
;
2505 ralen
*= mp
->m_sb
.sb_rextsize
;
2507 ap
->ip
->i_d
.di_nblocks
+= ralen
;
2508 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
2510 ap
->ip
->i_delayed_blks
-= ralen
;
2512 * Adjust the disk quota also. This was reserved
2515 XFS_TRANS_MOD_DQUOT_BYINO(mp
, ap
->tp
, ap
->ip
,
2516 ap
->wasdel
? XFS_TRANS_DQ_DELRTBCOUNT
:
2517 XFS_TRANS_DQ_RTBCOUNT
,
2521 #endif /* __KERNEL__ */
2524 * Normal allocation, done through xfs_alloc_vextent.
2528 xfs_alloc_arg_t args
;
2532 xfs_extlen_t longest
;
2534 xfs_extlen_t nextminlen
=0;
2537 xfs_agnumber_t startag
;
2540 tryagain
= isaligned
= 0;
2543 args
.fsbno
= ap
->rval
;
2544 args
.maxlen
= MIN(ap
->alen
, mp
->m_sb
.sb_agblocks
);
2547 args
.type
= XFS_ALLOCTYPE_START_BNO
;
2548 args
.total
= ap
->total
;
2550 * Find the longest available space.
2551 * We're going to try for the whole allocation at once.
2553 startag
= ag
= XFS_FSB_TO_AGNO(mp
, args
.fsbno
);
2555 down_read(&mp
->m_peraglock
);
2556 while (blen
< ap
->alen
) {
2557 pag
= &mp
->m_perag
[ag
];
2558 if (!pag
->pagf_init
&&
2559 (error
= xfs_alloc_pagf_init(mp
, args
.tp
,
2560 ag
, XFS_ALLOC_FLAG_TRYLOCK
))) {
2561 up_read(&mp
->m_peraglock
);
2565 * See xfs_alloc_fix_freelist...
2567 if (pag
->pagf_init
) {
2568 need
= XFS_MIN_FREELIST_PAG(pag
, mp
);
2569 delta
= need
> pag
->pagf_flcount
?
2570 need
- pag
->pagf_flcount
: 0;
2571 longest
= (pag
->pagf_longest
> delta
) ?
2572 (pag
->pagf_longest
- delta
) :
2573 (pag
->pagf_flcount
> 0 ||
2574 pag
->pagf_longest
> 0);
2579 if (++ag
== mp
->m_sb
.sb_agcount
)
2584 up_read(&mp
->m_peraglock
);
2586 * Since the above loop did a BUF_TRYLOCK, it is
2587 * possible that there is space for this request.
2589 if (notinit
|| blen
< ap
->minlen
)
2590 args
.minlen
= ap
->minlen
;
2592 * If the best seen length is less than the request
2593 * length, use the best as the minimum.
2595 else if (blen
< ap
->alen
)
2598 * Otherwise we've seen an extent as big as alen,
2599 * use that as the minimum.
2602 args
.minlen
= ap
->alen
;
2603 } else if (ap
->low
) {
2604 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
2605 args
.total
= args
.minlen
= ap
->minlen
;
2607 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
2608 args
.total
= ap
->total
;
2609 args
.minlen
= ap
->minlen
;
2611 if (ap
->ip
->i_d
.di_extsize
) {
2612 args
.prod
= ap
->ip
->i_d
.di_extsize
;
2613 if ((args
.mod
= (xfs_extlen_t
)do_mod(ap
->off
, args
.prod
)))
2614 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
2615 } else if (mp
->m_sb
.sb_blocksize
>= NBPP
) {
2619 args
.prod
= NBPP
>> mp
->m_sb
.sb_blocklog
;
2620 if ((args
.mod
= (xfs_extlen_t
)(do_mod(ap
->off
, args
.prod
))))
2621 args
.mod
= (xfs_extlen_t
)(args
.prod
- args
.mod
);
2624 * If we are not low on available data blocks, and the
2625 * underlying logical volume manager is a stripe, and
2626 * the file offset is zero then try to allocate data
2627 * blocks on stripe unit boundary.
2628 * NOTE: ap->aeof is only set if the allocation length
2629 * is >= the stripe unit and the allocation offset is
2630 * at the end of file.
2632 if (!ap
->low
&& ap
->aeof
) {
2634 args
.alignment
= mp
->m_dalign
;
2638 * Adjust for alignment
2640 if (blen
> args
.alignment
&& blen
<= ap
->alen
)
2641 args
.minlen
= blen
- args
.alignment
;
2642 args
.minalignslop
= 0;
2645 * First try an exact bno allocation.
2646 * If it fails then do a near or start bno
2647 * allocation with alignment turned on.
2651 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
2654 * Compute the minlen+alignment for the
2655 * next case. Set slop so that the value
2656 * of minlen+alignment+slop doesn't go up
2657 * between the calls.
2659 if (blen
> mp
->m_dalign
&& blen
<= ap
->alen
)
2660 nextminlen
= blen
- mp
->m_dalign
;
2662 nextminlen
= args
.minlen
;
2663 if (nextminlen
+ mp
->m_dalign
> args
.minlen
+ 1)
2665 nextminlen
+ mp
->m_dalign
-
2668 args
.minalignslop
= 0;
2672 args
.minalignslop
= 0;
2674 args
.minleft
= ap
->minleft
;
2675 args
.wasdel
= ap
->wasdel
;
2677 args
.userdata
= ap
->userdata
;
2678 if ((error
= xfs_alloc_vextent(&args
)))
2680 if (tryagain
&& args
.fsbno
== NULLFSBLOCK
) {
2682 * Exact allocation failed. Now try with alignment
2686 args
.fsbno
= ap
->rval
;
2687 args
.alignment
= mp
->m_dalign
;
2688 args
.minlen
= nextminlen
;
2689 args
.minalignslop
= 0;
2691 if ((error
= xfs_alloc_vextent(&args
)))
2694 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
2696 * allocation failed, so turn off alignment and
2700 args
.fsbno
= ap
->rval
;
2702 if ((error
= xfs_alloc_vextent(&args
)))
2705 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
&&
2706 args
.minlen
> ap
->minlen
) {
2707 args
.minlen
= ap
->minlen
;
2708 args
.type
= XFS_ALLOCTYPE_START_BNO
;
2709 args
.fsbno
= ap
->rval
;
2710 if ((error
= xfs_alloc_vextent(&args
)))
2713 if (args
.fsbno
== NULLFSBLOCK
&& nullfb
) {
2715 args
.type
= XFS_ALLOCTYPE_FIRST_AG
;
2716 args
.total
= ap
->minlen
;
2718 if ((error
= xfs_alloc_vextent(&args
)))
2722 if (args
.fsbno
!= NULLFSBLOCK
) {
2723 ap
->firstblock
= ap
->rval
= args
.fsbno
;
2724 ASSERT(nullfb
|| fb_agno
== args
.agno
||
2725 (ap
->low
&& fb_agno
< args
.agno
));
2726 ap
->alen
= args
.len
;
2727 ap
->ip
->i_d
.di_nblocks
+= args
.len
;
2728 xfs_trans_log_inode(ap
->tp
, ap
->ip
, XFS_ILOG_CORE
);
2730 ap
->ip
->i_delayed_blks
-= args
.len
;
2732 * Adjust the disk quota also. This was reserved
2735 XFS_TRANS_MOD_DQUOT_BYINO(mp
, ap
->tp
, ap
->ip
,
2736 ap
->wasdel
? XFS_TRANS_DQ_DELBCOUNT
:
2737 XFS_TRANS_DQ_BCOUNT
,
2740 ap
->rval
= NULLFSBLOCK
;
2749 * Transform a btree format file with only one leaf node, where the
2750 * extents list will fit in the inode, into an extents format file.
2751 * Since the extent list is already in-core, all we have to do is
2752 * give up the space for the btree root and pitch the leaf block.
2754 STATIC
int /* error */
2755 xfs_bmap_btree_to_extents(
2756 xfs_trans_t
*tp
, /* transaction pointer */
2757 xfs_inode_t
*ip
, /* incore inode pointer */
2758 xfs_btree_cur_t
*cur
, /* btree cursor */
2759 int *logflagsp
, /* inode logging flags */
2760 int whichfork
) /* data or attr fork */
2763 xfs_bmbt_block_t
*cblock
;/* child btree block */
2764 xfs_fsblock_t cbno
; /* child block number */
2765 xfs_buf_t
*cbp
; /* child block's buffer */
2766 int error
; /* error return value */
2767 xfs_ifork_t
*ifp
; /* inode fork data */
2768 xfs_mount_t
*mp
; /* mount point structure */
2769 xfs_bmbt_ptr_t
*pp
; /* ptr to block address */
2770 xfs_bmbt_block_t
*rblock
;/* root btree block */
2772 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2773 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
2774 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
2775 rblock
= ifp
->if_broot
;
2776 ASSERT(INT_GET(rblock
->bb_level
, ARCH_CONVERT
) == 1);
2777 ASSERT(INT_GET(rblock
->bb_numrecs
, ARCH_CONVERT
) == 1);
2778 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp
->if_broot_bytes
) == 1);
2780 pp
= XFS_BMAP_BROOT_PTR_ADDR(rblock
, 1, ifp
->if_broot_bytes
);
2783 if ((error
= xfs_btree_check_lptr(cur
, INT_GET(*pp
, ARCH_CONVERT
), 1)))
2786 cbno
= INT_GET(*pp
, ARCH_CONVERT
);
2787 if ((error
= xfs_btree_read_bufl(mp
, tp
, cbno
, 0, &cbp
,
2788 XFS_BMAP_BTREE_REF
)))
2790 cblock
= XFS_BUF_TO_BMBT_BLOCK(cbp
);
2791 if ((error
= xfs_btree_check_lblock(cur
, cblock
, 0, cbp
)))
2793 xfs_bmap_add_free(cbno
, 1, cur
->bc_private
.b
.flist
, mp
);
2794 ip
->i_d
.di_nblocks
--;
2795 XFS_TRANS_MOD_DQUOT_BYINO(mp
, tp
, ip
, XFS_TRANS_DQ_BCOUNT
, -1L);
2796 xfs_trans_binval(tp
, cbp
);
2797 if (cur
->bc_bufs
[0] == cbp
)
2798 cur
->bc_bufs
[0] = NULL
;
2799 xfs_iroot_realloc(ip
, -1, whichfork
);
2800 ASSERT(ifp
->if_broot
== NULL
);
2801 ASSERT((ifp
->if_flags
& XFS_IFBROOT
) == 0);
2802 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
2803 *logflagsp
= XFS_ILOG_CORE
| XFS_ILOG_FEXT(whichfork
);
2808 * Called by xfs_bmapi to update extent list structure and the btree
2809 * after removing space (or undoing a delayed allocation).
2811 STATIC
int /* error */
2812 xfs_bmap_del_extent(
2813 xfs_inode_t
*ip
, /* incore inode pointer */
2814 xfs_trans_t
*tp
, /* current transaction pointer */
2815 xfs_extnum_t idx
, /* extent number to update/delete */
2816 xfs_bmap_free_t
*flist
, /* list of extents to be freed */
2817 xfs_btree_cur_t
*cur
, /* if null, not a btree */
2818 xfs_bmbt_irec_t
*del
, /* data to remove from extent list */
2819 int *logflagsp
, /* inode logging flags */
2820 int whichfork
, /* data or attr fork */
2821 int rsvd
) /* OK to allocate reserved blocks */
2823 xfs_filblks_t da_new
; /* new delay-alloc indirect blocks */
2824 xfs_filblks_t da_old
; /* old delay-alloc indirect blocks */
2825 xfs_fsblock_t del_endblock
=0; /* first block past del */
2826 xfs_fileoff_t del_endoff
; /* first offset past del */
2827 int delay
; /* current block is delayed allocated */
2828 int do_fx
; /* free extent at end of routine */
2829 xfs_bmbt_rec_t
*ep
; /* current extent entry pointer */
2830 int error
; /* error return value */
2831 int flags
; /* inode logging flags */
2832 #ifdef XFS_BMAP_TRACE
2833 static char fname
[] = "xfs_bmap_del_extent";
2835 xfs_bmbt_irec_t got
; /* current extent entry */
2836 xfs_fileoff_t got_endoff
; /* first offset past got */
2837 int i
; /* temp state */
2838 xfs_ifork_t
*ifp
; /* inode fork pointer */
2839 xfs_mount_t
*mp
; /* mount structure */
2840 xfs_filblks_t nblks
; /* quota/sb block count */
2841 xfs_bmbt_irec_t
new; /* new record to be inserted */
2843 xfs_extnum_t nextents
; /* number of extents in list */
2844 uint qfield
; /* quota field to update */
2845 xfs_filblks_t temp
; /* for indirect length calculations */
2846 xfs_filblks_t temp2
; /* for indirect length calculations */
2848 XFS_STATS_INC(xs_del_exlist
);
2850 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
2851 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
2852 ASSERT(idx
>= 0 && idx
< nextents
);
2853 ASSERT(del
->br_blockcount
> 0);
2854 ep
= &ifp
->if_u1
.if_extents
[idx
];
2855 xfs_bmbt_get_all(ep
, &got
);
2856 ASSERT(got
.br_startoff
<= del
->br_startoff
);
2857 del_endoff
= del
->br_startoff
+ del
->br_blockcount
;
2858 got_endoff
= got
.br_startoff
+ got
.br_blockcount
;
2859 ASSERT(got_endoff
>= del_endoff
);
2860 delay
= ISNULLSTARTBLOCK(got
.br_startblock
);
2861 ASSERT(ISNULLSTARTBLOCK(del
->br_startblock
) == delay
);
2866 * If deleting a real allocation, must free up the disk space.
2869 flags
= XFS_ILOG_CORE
;
2871 * Realtime allocation. Free it and record di_nblocks update.
2873 if (whichfork
== XFS_DATA_FORK
&&
2874 (ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
)) {
2878 ASSERT(do_mod(del
->br_blockcount
,
2879 mp
->m_sb
.sb_rextsize
) == 0);
2880 ASSERT(do_mod(del
->br_startblock
,
2881 mp
->m_sb
.sb_rextsize
) == 0);
2882 bno
= del
->br_startblock
;
2883 len
= del
->br_blockcount
;
2884 do_div(bno
, mp
->m_sb
.sb_rextsize
);
2885 do_div(len
, mp
->m_sb
.sb_rextsize
);
2886 if ((error
= xfs_rtfree_extent(ip
->i_transp
, bno
,
2887 (xfs_extlen_t
)len
)))
2890 nblks
= len
* mp
->m_sb
.sb_rextsize
;
2891 qfield
= XFS_TRANS_DQ_RTBCOUNT
;
2894 * Ordinary allocation.
2898 nblks
= del
->br_blockcount
;
2899 qfield
= XFS_TRANS_DQ_BCOUNT
;
2902 * Set up del_endblock and cur for later.
2904 del_endblock
= del
->br_startblock
+ del
->br_blockcount
;
2906 if ((error
= xfs_bmbt_lookup_eq(cur
, got
.br_startoff
,
2907 got
.br_startblock
, got
.br_blockcount
,
2912 da_old
= da_new
= 0;
2914 da_old
= STARTBLOCKVAL(got
.br_startblock
);
2920 * Set flag value to use in switch statement.
2921 * Left-contig is 2, right-contig is 1.
2923 switch (((got
.br_startoff
== del
->br_startoff
) << 1) |
2924 (got_endoff
== del_endoff
)) {
2927 * Matches the whole extent. Delete the entry.
2929 xfs_bmap_trace_delete(fname
, "3", ip
, idx
, 1, whichfork
);
2930 xfs_bmap_delete_exlist(ip
, idx
, 1, whichfork
);
2931 ifp
->if_lastex
= idx
;
2934 XFS_IFORK_NEXT_SET(ip
, whichfork
,
2935 XFS_IFORK_NEXTENTS(ip
, whichfork
) - 1);
2936 flags
|= XFS_ILOG_CORE
;
2938 flags
|= XFS_ILOG_FEXT(whichfork
);
2941 if ((error
= xfs_bmbt_delete(cur
, &i
)))
2948 * Deleting the first part of the extent.
2950 xfs_bmap_trace_pre_update(fname
, "2", ip
, idx
, whichfork
);
2951 xfs_bmbt_set_startoff(ep
, del_endoff
);
2952 temp
= got
.br_blockcount
- del
->br_blockcount
;
2953 xfs_bmbt_set_blockcount(ep
, temp
);
2954 ifp
->if_lastex
= idx
;
2956 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2958 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
2959 xfs_bmap_trace_post_update(fname
, "2", ip
, idx
,
2964 xfs_bmbt_set_startblock(ep
, del_endblock
);
2965 xfs_bmap_trace_post_update(fname
, "2", ip
, idx
, whichfork
);
2967 flags
|= XFS_ILOG_FEXT(whichfork
);
2970 if ((error
= xfs_bmbt_update(cur
, del_endoff
, del_endblock
,
2971 got
.br_blockcount
- del
->br_blockcount
,
2978 * Deleting the last part of the extent.
2980 temp
= got
.br_blockcount
- del
->br_blockcount
;
2981 xfs_bmap_trace_pre_update(fname
, "1", ip
, idx
, whichfork
);
2982 xfs_bmbt_set_blockcount(ep
, temp
);
2983 ifp
->if_lastex
= idx
;
2985 temp
= XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip
, temp
),
2987 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
2988 xfs_bmap_trace_post_update(fname
, "1", ip
, idx
,
2993 xfs_bmap_trace_post_update(fname
, "1", ip
, idx
, whichfork
);
2995 flags
|= XFS_ILOG_FEXT(whichfork
);
2998 if ((error
= xfs_bmbt_update(cur
, got
.br_startoff
,
3000 got
.br_blockcount
- del
->br_blockcount
,
3007 * Deleting the middle of the extent.
3009 temp
= del
->br_startoff
- got
.br_startoff
;
3010 xfs_bmap_trace_pre_update(fname
, "0", ip
, idx
, whichfork
);
3011 xfs_bmbt_set_blockcount(ep
, temp
);
3012 new.br_startoff
= del_endoff
;
3013 temp2
= got_endoff
- del_endoff
;
3014 new.br_blockcount
= temp2
;
3015 new.br_state
= got
.br_state
;
3017 new.br_startblock
= del_endblock
;
3018 flags
|= XFS_ILOG_CORE
;
3020 if ((error
= xfs_bmbt_update(cur
,
3022 got
.br_startblock
, temp
,
3025 if ((error
= xfs_bmbt_increment(cur
, 0, &i
)))
3027 cur
->bc_rec
.b
= new;
3028 error
= xfs_bmbt_insert(cur
, &i
);
3029 if (error
&& error
!= ENOSPC
)
3032 * If get no-space back from btree insert,
3033 * it tried a split, and we have a zero
3034 * block reservation.
3035 * Fix up our state and return the error.
3037 if (error
== ENOSPC
) {
3039 * Reset the cursor, don't trust
3040 * it after any insert operation.
3042 if ((error
= xfs_bmbt_lookup_eq(cur
,
3049 * Update the btree record back
3050 * to the original value.
3052 if ((error
= xfs_bmbt_update(cur
,
3059 * Reset the extent record back
3060 * to the original value.
3062 xfs_bmbt_set_blockcount(ep
,
3065 error
= XFS_ERROR(ENOSPC
);
3070 flags
|= XFS_ILOG_FEXT(whichfork
);
3071 XFS_IFORK_NEXT_SET(ip
, whichfork
,
3072 XFS_IFORK_NEXTENTS(ip
, whichfork
) + 1);
3074 ASSERT(whichfork
== XFS_DATA_FORK
);
3075 temp
= xfs_bmap_worst_indlen(ip
, temp
);
3076 xfs_bmbt_set_startblock(ep
, NULLSTARTBLOCK((int)temp
));
3077 temp2
= xfs_bmap_worst_indlen(ip
, temp2
);
3078 new.br_startblock
= NULLSTARTBLOCK((int)temp2
);
3079 da_new
= temp
+ temp2
;
3080 while (da_new
> da_old
) {
3084 xfs_bmbt_set_startblock(ep
,
3085 NULLSTARTBLOCK((int)temp
));
3087 if (da_new
== da_old
)
3093 NULLSTARTBLOCK((int)temp2
);
3097 xfs_bmap_trace_post_update(fname
, "0", ip
, idx
, whichfork
);
3098 xfs_bmap_trace_insert(fname
, "0", ip
, idx
+ 1, 1, &new, NULL
,
3100 xfs_bmap_insert_exlist(ip
, idx
+ 1, 1, &new, whichfork
);
3101 ifp
->if_lastex
= idx
+ 1;
3105 * If we need to, add to list of extents to delete.
3108 xfs_bmap_add_free(del
->br_startblock
, del
->br_blockcount
, flist
,
3111 * Adjust inode # blocks in the file.
3114 ip
->i_d
.di_nblocks
-= nblks
;
3116 * Adjust quota data.
3119 XFS_TRANS_MOD_DQUOT_BYINO(mp
, tp
, ip
, qfield
, (long)-nblks
);
3122 * Account for change in delayed indirect blocks.
3123 * Nothing to do for disk quota accounting here.
3125 ASSERT(da_old
>= da_new
);
3126 if (da_old
> da_new
)
3127 xfs_mod_incore_sb(mp
, XFS_SBS_FDBLOCKS
, (int)(da_old
- da_new
),
3135 * Remove the entry "free" from the free item list. Prev points to the
3136 * previous entry, unless "free" is the head of the list.
3140 xfs_bmap_free_t
*flist
, /* free item list header */
3141 xfs_bmap_free_item_t
*prev
, /* previous item on list, if any */
3142 xfs_bmap_free_item_t
*free
) /* list item to be freed */
3145 prev
->xbfi_next
= free
->xbfi_next
;
3147 flist
->xbf_first
= free
->xbfi_next
;
3149 kmem_zone_free(xfs_bmap_free_item_zone
, free
);
3153 * Remove count entries from the extents array for inode "ip", starting
3154 * at index "idx". Copies the remaining items down over the deleted ones,
3155 * and gives back the excess memory.
3158 xfs_bmap_delete_exlist(
3159 xfs_inode_t
*ip
, /* incore inode pointer */
3160 xfs_extnum_t idx
, /* starting delete index */
3161 xfs_extnum_t count
, /* count of items to delete */
3162 int whichfork
) /* data or attr fork */
3164 xfs_bmbt_rec_t
*base
; /* base of extent list */
3165 xfs_ifork_t
*ifp
; /* inode fork pointer */
3166 xfs_extnum_t nextents
; /* number of extents in list after */
3168 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3169 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
3170 base
= ifp
->if_u1
.if_extents
;
3171 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
) - count
;
3172 memmove(&base
[idx
], &base
[idx
+ count
],
3173 (nextents
- idx
) * sizeof(*base
));
3174 xfs_iext_realloc(ip
, -count
, whichfork
);
3178 * Convert an extents-format file into a btree-format file.
3179 * The new file will have a root block (in the inode) and a single child block.
3181 STATIC
int /* error */
3182 xfs_bmap_extents_to_btree(
3183 xfs_trans_t
*tp
, /* transaction pointer */
3184 xfs_inode_t
*ip
, /* incore inode pointer */
3185 xfs_fsblock_t
*firstblock
, /* first-block-allocated */
3186 xfs_bmap_free_t
*flist
, /* blocks freed in xaction */
3187 xfs_btree_cur_t
**curp
, /* cursor returned to caller */
3188 int wasdel
, /* converting a delayed alloc */
3189 int *logflagsp
, /* inode logging flags */
3190 int whichfork
) /* data or attr fork */
3192 xfs_bmbt_block_t
*ablock
; /* allocated (child) bt block */
3193 xfs_buf_t
*abp
; /* buffer for ablock */
3194 xfs_alloc_arg_t args
; /* allocation arguments */
3195 xfs_bmbt_rec_t
*arp
; /* child record pointer */
3196 xfs_bmbt_block_t
*block
; /* btree root block */
3197 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
3198 xfs_bmbt_rec_t
*ep
; /* extent list pointer */
3199 int error
; /* error return value */
3200 xfs_extnum_t i
, cnt
; /* extent list index */
3201 xfs_ifork_t
*ifp
; /* inode fork pointer */
3202 xfs_bmbt_key_t
*kp
; /* root block key pointer */
3203 xfs_mount_t
*mp
; /* mount structure */
3204 xfs_extnum_t nextents
; /* extent list size */
3205 xfs_bmbt_ptr_t
*pp
; /* root block address pointer */
3207 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3208 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
);
3209 ASSERT(ifp
->if_ext_max
==
3210 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
3212 * Make space in the inode incore.
3214 xfs_iroot_realloc(ip
, 1, whichfork
);
3215 ifp
->if_flags
|= XFS_IFBROOT
;
3219 block
= ifp
->if_broot
;
3220 INT_SET(block
->bb_magic
, ARCH_CONVERT
, XFS_BMAP_MAGIC
);
3221 INT_SET(block
->bb_level
, ARCH_CONVERT
, 1);
3222 INT_SET(block
->bb_numrecs
, ARCH_CONVERT
, 1);
3223 INT_SET(block
->bb_leftsib
, ARCH_CONVERT
, NULLDFSBNO
);
3224 INT_SET(block
->bb_rightsib
, ARCH_CONVERT
, NULLDFSBNO
);
3226 * Need a cursor. Can't allocate until bb_level is filled in.
3229 cur
= xfs_btree_init_cursor(mp
, tp
, NULL
, 0, XFS_BTNUM_BMAP
, ip
,
3231 cur
->bc_private
.b
.firstblock
= *firstblock
;
3232 cur
->bc_private
.b
.flist
= flist
;
3233 cur
->bc_private
.b
.flags
= wasdel
? XFS_BTCUR_BPRV_WASDEL
: 0;
3235 * Convert to a btree with two levels, one record in root.
3237 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_BTREE
);
3240 if (*firstblock
== NULLFSBLOCK
) {
3241 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3242 args
.fsbno
= XFS_INO_TO_FSB(mp
, ip
->i_ino
);
3243 } else if (flist
->xbf_low
) {
3244 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3245 args
.fsbno
= *firstblock
;
3247 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3248 args
.fsbno
= *firstblock
;
3250 args
.minlen
= args
.maxlen
= args
.prod
= 1;
3251 args
.total
= args
.minleft
= args
.alignment
= args
.mod
= args
.isfl
=
3252 args
.minalignslop
= 0;
3253 args
.wasdel
= wasdel
;
3255 if ((error
= xfs_alloc_vextent(&args
))) {
3256 xfs_iroot_realloc(ip
, -1, whichfork
);
3257 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
3261 * Allocation can't fail, the space was reserved.
3263 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
3264 ASSERT(*firstblock
== NULLFSBLOCK
||
3265 args
.agno
== XFS_FSB_TO_AGNO(mp
, *firstblock
) ||
3267 args
.agno
> XFS_FSB_TO_AGNO(mp
, *firstblock
)));
3268 *firstblock
= cur
->bc_private
.b
.firstblock
= args
.fsbno
;
3269 cur
->bc_private
.b
.allocated
++;
3270 ip
->i_d
.di_nblocks
++;
3271 XFS_TRANS_MOD_DQUOT_BYINO(mp
, tp
, ip
, XFS_TRANS_DQ_BCOUNT
, 1L);
3272 abp
= xfs_btree_get_bufl(mp
, tp
, args
.fsbno
, 0);
3274 * Fill in the child block.
3276 ablock
= XFS_BUF_TO_BMBT_BLOCK(abp
);
3277 INT_SET(ablock
->bb_magic
, ARCH_CONVERT
, XFS_BMAP_MAGIC
);
3278 ablock
->bb_level
= 0;
3279 INT_SET(ablock
->bb_leftsib
, ARCH_CONVERT
, NULLDFSBNO
);
3280 INT_SET(ablock
->bb_rightsib
, ARCH_CONVERT
, NULLDFSBNO
);
3281 arp
= XFS_BMAP_REC_IADDR(ablock
, 1, cur
);
3282 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
3283 for (ep
= ifp
->if_u1
.if_extents
, cnt
= i
= 0; i
< nextents
; i
++, ep
++) {
3284 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep
))) {
3285 arp
->l0
= INT_GET(ep
->l0
, ARCH_CONVERT
);
3286 arp
->l1
= INT_GET(ep
->l1
, ARCH_CONVERT
);
3290 INT_SET(ablock
->bb_numrecs
, ARCH_CONVERT
, cnt
);
3291 ASSERT(INT_GET(ablock
->bb_numrecs
, ARCH_CONVERT
) == XFS_IFORK_NEXTENTS(ip
, whichfork
));
3293 * Fill in the root key and pointer.
3295 kp
= XFS_BMAP_KEY_IADDR(block
, 1, cur
);
3296 arp
= XFS_BMAP_REC_IADDR(ablock
, 1, cur
);
3297 INT_SET(kp
->br_startoff
, ARCH_CONVERT
, xfs_bmbt_disk_get_startoff(arp
));
3298 pp
= XFS_BMAP_PTR_IADDR(block
, 1, cur
);
3299 INT_SET(*pp
, ARCH_CONVERT
, args
.fsbno
);
3301 * Do all this logging at the end so that
3302 * the root is at the right level.
3304 xfs_bmbt_log_block(cur
, abp
, XFS_BB_ALL_BITS
);
3305 xfs_bmbt_log_recs(cur
, abp
, 1, INT_GET(ablock
->bb_numrecs
, ARCH_CONVERT
));
3306 ASSERT(*curp
== NULL
);
3308 *logflagsp
= XFS_ILOG_CORE
| XFS_ILOG_FBROOT(whichfork
);
3313 * Insert new item(s) in the extent list for inode "ip".
3314 * Count new items are inserted at offset idx.
3317 xfs_bmap_insert_exlist(
3318 xfs_inode_t
*ip
, /* incore inode pointer */
3319 xfs_extnum_t idx
, /* starting index of new items */
3320 xfs_extnum_t count
, /* number of inserted items */
3321 xfs_bmbt_irec_t
*new, /* items to insert */
3322 int whichfork
) /* data or attr fork */
3324 xfs_bmbt_rec_t
*base
; /* extent list base */
3325 xfs_ifork_t
*ifp
; /* inode fork pointer */
3326 xfs_extnum_t nextents
; /* extent list size */
3327 xfs_extnum_t to
; /* extent list index */
3329 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3330 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
3331 xfs_iext_realloc(ip
, count
, whichfork
);
3332 base
= ifp
->if_u1
.if_extents
;
3333 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
3334 memmove(&base
[idx
+ count
], &base
[idx
],
3335 (nextents
- (idx
+ count
)) * sizeof(*base
));
3336 for (to
= idx
; to
< idx
+ count
; to
++, new++)
3337 xfs_bmbt_set_all(&base
[to
], new);
3341 * Helper routine to reset inode di_forkoff field when switching
3342 * attribute fork from local to extent format - we reset it where
3343 * possible to make space available for inline data fork extents.
3346 xfs_bmap_forkoff_reset(
3351 if (whichfork
== XFS_ATTR_FORK
&&
3352 (ip
->i_d
.di_format
!= XFS_DINODE_FMT_DEV
) &&
3353 (ip
->i_d
.di_format
!= XFS_DINODE_FMT_UUID
) &&
3354 ((mp
->m_attroffset
>> 3) > ip
->i_d
.di_forkoff
)) {
3355 ip
->i_d
.di_forkoff
= mp
->m_attroffset
>> 3;
3356 ip
->i_df
.if_ext_max
= XFS_IFORK_DSIZE(ip
) /
3357 (uint
)sizeof(xfs_bmbt_rec_t
);
3358 ip
->i_afp
->if_ext_max
= XFS_IFORK_ASIZE(ip
) /
3359 (uint
)sizeof(xfs_bmbt_rec_t
);
3364 * Convert a local file to an extents file.
3365 * This code is out of bounds for data forks of regular files,
3366 * since the file data needs to get logged so things will stay consistent.
3367 * (The bmap-level manipulations are ok, though).
3369 STATIC
int /* error */
3370 xfs_bmap_local_to_extents(
3371 xfs_trans_t
*tp
, /* transaction pointer */
3372 xfs_inode_t
*ip
, /* incore inode pointer */
3373 xfs_fsblock_t
*firstblock
, /* first block allocated in xaction */
3374 xfs_extlen_t total
, /* total blocks needed by transaction */
3375 int *logflagsp
, /* inode logging flags */
3376 int whichfork
) /* data or attr fork */
3378 int error
; /* error return value */
3379 int flags
; /* logging flags returned */
3380 #ifdef XFS_BMAP_TRACE
3381 static char fname
[] = "xfs_bmap_local_to_extents";
3383 xfs_ifork_t
*ifp
; /* inode fork pointer */
3386 * We don't want to deal with the case of keeping inode data inline yet.
3387 * So sending the data fork of a regular inode is invalid.
3389 ASSERT(!((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
&&
3390 whichfork
== XFS_DATA_FORK
));
3391 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3392 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
3395 if (ifp
->if_bytes
) {
3396 xfs_alloc_arg_t args
; /* allocation arguments */
3397 xfs_buf_t
*bp
; /* buffer for extent list block */
3398 xfs_bmbt_rec_t
*ep
; /* extent list pointer */
3401 args
.mp
= ip
->i_mount
;
3402 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
3404 * Allocate a block. We know we need only one, since the
3405 * file currently fits in an inode.
3407 if (*firstblock
== NULLFSBLOCK
) {
3408 args
.fsbno
= XFS_INO_TO_FSB(args
.mp
, ip
->i_ino
);
3409 args
.type
= XFS_ALLOCTYPE_START_BNO
;
3411 args
.fsbno
= *firstblock
;
3412 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
3415 args
.mod
= args
.minleft
= args
.alignment
= args
.wasdel
=
3416 args
.isfl
= args
.minalignslop
= 0;
3417 args
.minlen
= args
.maxlen
= args
.prod
= 1;
3418 if ((error
= xfs_alloc_vextent(&args
)))
3421 * Can't fail, the space was reserved.
3423 ASSERT(args
.fsbno
!= NULLFSBLOCK
);
3424 ASSERT(args
.len
== 1);
3425 *firstblock
= args
.fsbno
;
3426 bp
= xfs_btree_get_bufl(args
.mp
, tp
, args
.fsbno
, 0);
3427 memcpy((char *)XFS_BUF_PTR(bp
), ifp
->if_u1
.if_data
,
3429 xfs_trans_log_buf(tp
, bp
, 0, ifp
->if_bytes
- 1);
3430 xfs_bmap_forkoff_reset(args
.mp
, ip
, whichfork
);
3431 xfs_idata_realloc(ip
, -ifp
->if_bytes
, whichfork
);
3432 xfs_iext_realloc(ip
, 1, whichfork
);
3433 ep
= ifp
->if_u1
.if_extents
;
3434 xfs_bmbt_set_allf(ep
, 0, args
.fsbno
, 1, XFS_EXT_NORM
);
3435 xfs_bmap_trace_post_update(fname
, "new", ip
, 0, whichfork
);
3436 XFS_IFORK_NEXT_SET(ip
, whichfork
, 1);
3437 ip
->i_d
.di_nblocks
= 1;
3438 XFS_TRANS_MOD_DQUOT_BYINO(args
.mp
, tp
, ip
,
3439 XFS_TRANS_DQ_BCOUNT
, 1L);
3440 flags
|= XFS_ILOG_FEXT(whichfork
);
3442 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0);
3443 xfs_bmap_forkoff_reset(ip
->i_mount
, ip
, whichfork
);
3445 ifp
->if_flags
&= ~XFS_IFINLINE
;
3446 ifp
->if_flags
|= XFS_IFEXTENTS
;
3447 XFS_IFORK_FMT_SET(ip
, whichfork
, XFS_DINODE_FMT_EXTENTS
);
3448 flags
|= XFS_ILOG_CORE
;
3454 xfs_bmbt_rec_t
* /* pointer to found extent entry */
3455 xfs_bmap_do_search_extents(
3456 xfs_bmbt_rec_t
*base
, /* base of extent list */
3457 xfs_extnum_t lastx
, /* last extent index used */
3458 xfs_extnum_t nextents
, /* extent list size */
3459 xfs_fileoff_t bno
, /* block number searched for */
3460 int *eofp
, /* out: end of file found */
3461 xfs_extnum_t
*lastxp
, /* out: last extent index */
3462 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
3463 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
3465 xfs_bmbt_rec_t
*ep
; /* extent list entry pointer */
3466 xfs_bmbt_irec_t got
; /* extent list entry, decoded */
3467 int high
; /* high index of binary search */
3468 int low
; /* low index of binary search */
3471 * Initialize the extent entry structure to catch access to
3472 * uninitialized br_startblock field.
3474 got
.br_startoff
= 0xffa5a5a5a5a5a5a5LL
;
3475 got
.br_blockcount
= 0xa55a5a5a5a5a5a5aLL
;
3476 got
.br_state
= XFS_EXT_INVALID
;
3479 got
.br_startblock
= 0xffffa5a5a5a5a5a5LL
;
3481 got
.br_startblock
= 0xffffa5a5;
3484 if (lastx
!= NULLEXTNUM
&& lastx
< nextents
)
3488 prevp
->br_startoff
= NULLFILEOFF
;
3489 if (ep
&& bno
>= (got
.br_startoff
= xfs_bmbt_get_startoff(ep
)) &&
3490 bno
< got
.br_startoff
+
3491 (got
.br_blockcount
= xfs_bmbt_get_blockcount(ep
)))
3493 else if (ep
&& lastx
< nextents
- 1 &&
3494 bno
>= (got
.br_startoff
= xfs_bmbt_get_startoff(ep
+ 1)) &&
3495 bno
< got
.br_startoff
+
3496 (got
.br_blockcount
= xfs_bmbt_get_blockcount(ep
+ 1))) {
3500 } else if (nextents
== 0)
3502 else if (bno
== 0 &&
3503 (got
.br_startoff
= xfs_bmbt_get_startoff(base
)) == 0) {
3506 got
.br_blockcount
= xfs_bmbt_get_blockcount(ep
);
3509 /* binary search the extents array */
3511 high
= nextents
- 1;
3512 while (low
<= high
) {
3513 XFS_STATS_INC(xs_cmp_exlist
);
3514 lastx
= (low
+ high
) >> 1;
3516 got
.br_startoff
= xfs_bmbt_get_startoff(ep
);
3517 got
.br_blockcount
= xfs_bmbt_get_blockcount(ep
);
3518 if (bno
< got
.br_startoff
)
3520 else if (bno
>= got
.br_startoff
+ got
.br_blockcount
)
3523 got
.br_startblock
= xfs_bmbt_get_startblock(ep
);
3524 got
.br_state
= xfs_bmbt_get_state(ep
);
3531 if (bno
>= got
.br_startoff
+ got
.br_blockcount
) {
3533 if (lastx
== nextents
) {
3535 got
.br_startblock
= xfs_bmbt_get_startblock(ep
);
3536 got
.br_state
= xfs_bmbt_get_state(ep
);
3541 xfs_bmbt_get_all(ep
, prevp
);
3543 got
.br_startoff
= xfs_bmbt_get_startoff(ep
);
3544 got
.br_blockcount
= xfs_bmbt_get_blockcount(ep
);
3549 xfs_bmbt_get_all(ep
- 1, prevp
);
3553 got
.br_startblock
= xfs_bmbt_get_startblock(ep
);
3554 got
.br_state
= xfs_bmbt_get_state(ep
);
3562 * Search the extents list for the inode, for the extent containing bno.
3563 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3564 * *eofp will be set, and *prevp will contain the last entry (null if none).
3565 * Else, *lastxp will be set to the index of the found
3566 * entry; *gotp will contain the entry.
3568 STATIC xfs_bmbt_rec_t
* /* pointer to found extent entry */
3569 xfs_bmap_search_extents(
3570 xfs_inode_t
*ip
, /* incore inode pointer */
3571 xfs_fileoff_t bno
, /* block number searched for */
3572 int whichfork
, /* data or attr fork */
3573 int *eofp
, /* out: end of file found */
3574 xfs_extnum_t
*lastxp
, /* out: last extent index */
3575 xfs_bmbt_irec_t
*gotp
, /* out: extent entry found */
3576 xfs_bmbt_irec_t
*prevp
) /* out: previous extent entry found */
3578 xfs_ifork_t
*ifp
; /* inode fork pointer */
3579 xfs_bmbt_rec_t
*base
; /* base of extent list */
3580 xfs_extnum_t lastx
; /* last extent index used */
3581 xfs_extnum_t nextents
; /* extent list size */
3582 xfs_bmbt_rec_t
*ep
; /* extent list entry pointer */
3583 int rt
; /* realtime flag */
3585 XFS_STATS_INC(xs_look_exlist
);
3586 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3587 lastx
= ifp
->if_lastex
;
3588 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
3589 base
= &ifp
->if_u1
.if_extents
[0];
3591 ep
= xfs_bmap_do_search_extents(base
, lastx
, nextents
, bno
, eofp
,
3592 lastxp
, gotp
, prevp
);
3593 rt
= ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
;
3594 if(!rt
&& !gotp
->br_startblock
&& (*lastxp
!= NULLEXTNUM
)) {
3595 cmn_err(CE_PANIC
,"Access to block zero: fs: <%s> inode: %lld "
3596 "start_block : %llx start_off : %llx blkcnt : %llx "
3597 "extent-state : %x \n",
3598 (ip
->i_mount
)->m_fsname
,(long long)ip
->i_ino
,
3599 gotp
->br_startblock
, gotp
->br_startoff
,
3600 gotp
->br_blockcount
,gotp
->br_state
);
3606 #ifdef XFS_BMAP_TRACE
3607 ktrace_t
*xfs_bmap_trace_buf
;
3610 * Add a bmap trace buffer entry. Base routine for the others.
3613 xfs_bmap_trace_addentry(
3614 int opcode
, /* operation */
3615 char *fname
, /* function name */
3616 char *desc
, /* operation description */
3617 xfs_inode_t
*ip
, /* incore inode pointer */
3618 xfs_extnum_t idx
, /* index of entry(ies) */
3619 xfs_extnum_t cnt
, /* count of entries, 1 or 2 */
3620 xfs_bmbt_rec_t
*r1
, /* first record */
3621 xfs_bmbt_rec_t
*r2
, /* second record or null */
3622 int whichfork
) /* data or attr fork */
3626 ASSERT(cnt
== 1 || cnt
== 2);
3631 memset(&tr2
, 0, sizeof(tr2
));
3634 ktrace_enter(xfs_bmap_trace_buf
,
3635 (void *)(__psint_t
)(opcode
| (whichfork
<< 16)),
3636 (void *)fname
, (void *)desc
, (void *)ip
,
3637 (void *)(__psint_t
)idx
,
3638 (void *)(__psint_t
)cnt
,
3639 (void *)(__psunsigned_t
)(ip
->i_ino
>> 32),
3640 (void *)(__psunsigned_t
)(unsigned)ip
->i_ino
,
3641 (void *)(__psunsigned_t
)(r1
->l0
>> 32),
3642 (void *)(__psunsigned_t
)(unsigned)(r1
->l0
),
3643 (void *)(__psunsigned_t
)(r1
->l1
>> 32),
3644 (void *)(__psunsigned_t
)(unsigned)(r1
->l1
),
3645 (void *)(__psunsigned_t
)(r2
->l0
>> 32),
3646 (void *)(__psunsigned_t
)(unsigned)(r2
->l0
),
3647 (void *)(__psunsigned_t
)(r2
->l1
>> 32),
3648 (void *)(__psunsigned_t
)(unsigned)(r2
->l1
)
3650 ASSERT(ip
->i_xtrace
);
3651 ktrace_enter(ip
->i_xtrace
,
3652 (void *)(__psint_t
)(opcode
| (whichfork
<< 16)),
3653 (void *)fname
, (void *)desc
, (void *)ip
,
3654 (void *)(__psint_t
)idx
,
3655 (void *)(__psint_t
)cnt
,
3656 (void *)(__psunsigned_t
)(ip
->i_ino
>> 32),
3657 (void *)(__psunsigned_t
)(unsigned)ip
->i_ino
,
3658 (void *)(__psunsigned_t
)(r1
->l0
>> 32),
3659 (void *)(__psunsigned_t
)(unsigned)(r1
->l0
),
3660 (void *)(__psunsigned_t
)(r1
->l1
>> 32),
3661 (void *)(__psunsigned_t
)(unsigned)(r1
->l1
),
3662 (void *)(__psunsigned_t
)(r2
->l0
>> 32),
3663 (void *)(__psunsigned_t
)(unsigned)(r2
->l0
),
3664 (void *)(__psunsigned_t
)(r2
->l1
>> 32),
3665 (void *)(__psunsigned_t
)(unsigned)(r2
->l1
)
3670 * Add bmap trace entry prior to a call to xfs_bmap_delete_exlist.
3673 xfs_bmap_trace_delete(
3674 char *fname
, /* function name */
3675 char *desc
, /* operation description */
3676 xfs_inode_t
*ip
, /* incore inode pointer */
3677 xfs_extnum_t idx
, /* index of entry(entries) deleted */
3678 xfs_extnum_t cnt
, /* count of entries deleted, 1 or 2 */
3679 int whichfork
) /* data or attr fork */
3681 xfs_ifork_t
*ifp
; /* inode fork pointer */
3683 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3684 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE
, fname
, desc
, ip
, idx
,
3685 cnt
, &ifp
->if_u1
.if_extents
[idx
],
3686 cnt
== 2 ? &ifp
->if_u1
.if_extents
[idx
+ 1] : NULL
,
3691 * Add bmap trace entry prior to a call to xfs_bmap_insert_exlist, or
3692 * reading in the extents list from the disk (in the btree).
3695 xfs_bmap_trace_insert(
3696 char *fname
, /* function name */
3697 char *desc
, /* operation description */
3698 xfs_inode_t
*ip
, /* incore inode pointer */
3699 xfs_extnum_t idx
, /* index of entry(entries) inserted */
3700 xfs_extnum_t cnt
, /* count of entries inserted, 1 or 2 */
3701 xfs_bmbt_irec_t
*r1
, /* inserted record 1 */
3702 xfs_bmbt_irec_t
*r2
, /* inserted record 2 or null */
3703 int whichfork
) /* data or attr fork */
3705 xfs_bmbt_rec_t tr1
; /* compressed record 1 */
3706 xfs_bmbt_rec_t tr2
; /* compressed record 2 if needed */
3708 xfs_bmbt_set_all(&tr1
, r1
);
3711 xfs_bmbt_set_all(&tr2
, r2
);
3716 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT
, fname
, desc
, ip
, idx
,
3717 cnt
, &tr1
, cnt
== 2 ? &tr2
: NULL
, whichfork
);
3721 * Add bmap trace entry after updating an extent list entry in place.
3724 xfs_bmap_trace_post_update(
3725 char *fname
, /* function name */
3726 char *desc
, /* operation description */
3727 xfs_inode_t
*ip
, /* incore inode pointer */
3728 xfs_extnum_t idx
, /* index of entry updated */
3729 int whichfork
) /* data or attr fork */
3731 xfs_ifork_t
*ifp
; /* inode fork pointer */
3733 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3734 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP
, fname
, desc
, ip
, idx
,
3735 1, &ifp
->if_u1
.if_extents
[idx
], NULL
, whichfork
);
3739 * Add bmap trace entry prior to updating an extent list entry in place.
3742 xfs_bmap_trace_pre_update(
3743 char *fname
, /* function name */
3744 char *desc
, /* operation description */
3745 xfs_inode_t
*ip
, /* incore inode pointer */
3746 xfs_extnum_t idx
, /* index of entry to be updated */
3747 int whichfork
) /* data or attr fork */
3749 xfs_ifork_t
*ifp
; /* inode fork pointer */
3751 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
3752 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP
, fname
, desc
, ip
, idx
, 1,
3753 &ifp
->if_u1
.if_extents
[idx
], NULL
, whichfork
);
3755 #endif /* XFS_BMAP_TRACE */
3758 * Compute the worst-case number of indirect blocks that will be used
3759 * for ip's delayed extent of length "len".
3761 STATIC xfs_filblks_t
3762 xfs_bmap_worst_indlen(
3763 xfs_inode_t
*ip
, /* incore inode pointer */
3764 xfs_filblks_t len
) /* delayed extent length */
3766 int level
; /* btree level number */
3767 int maxrecs
; /* maximum record count at this level */
3768 xfs_mount_t
*mp
; /* mount structure */
3769 xfs_filblks_t rval
; /* return value */
3772 maxrecs
= mp
->m_bmap_dmxr
[0];
3773 for (level
= 0, rval
= 0;
3774 level
< XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
);
3777 do_div(len
, maxrecs
);
3780 return rval
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) -
3783 maxrecs
= mp
->m_bmap_dmxr
[1];
3788 #if defined(XFS_RW_TRACE)
3797 if (ip
->i_rwtrace
== NULL
)
3799 ktrace_enter(ip
->i_rwtrace
,
3800 (void *)(__psint_t
)XFS_BUNMAPI
,
3802 (void *)(__psint_t
)((ip
->i_d
.di_size
>> 32) & 0xffffffff),
3803 (void *)(__psint_t
)(ip
->i_d
.di_size
& 0xffffffff),
3804 (void *)(__psint_t
)(((xfs_dfiloff_t
)bno
>> 32) & 0xffffffff),
3805 (void *)(__psint_t
)((xfs_dfiloff_t
)bno
& 0xffffffff),
3806 (void *)(__psint_t
)len
,
3807 (void *)(__psint_t
)flags
,
3808 (void *)(unsigned long)current_cpu(),
3820 * Convert inode from non-attributed to attributed.
3821 * Must not be in a transaction, ip must not be locked.
3823 int /* error code */
3824 xfs_bmap_add_attrfork(
3825 xfs_inode_t
*ip
, /* incore inode pointer */
3826 int size
, /* space new attribute needs */
3827 int rsvd
) /* xact may use reserved blks */
3829 xfs_fsblock_t firstblock
; /* 1st block/ag allocated */
3830 xfs_bmap_free_t flist
; /* freed extent list */
3831 xfs_mount_t
*mp
; /* mount structure */
3832 xfs_trans_t
*tp
; /* transaction pointer */
3833 unsigned long s
; /* spinlock spl value */
3834 int blks
; /* space reservation */
3835 int version
= 1; /* superblock attr version */
3836 int committed
; /* xaction was committed */
3837 int logflags
; /* logging flags */
3838 int error
; /* error return value */
3840 ASSERT(XFS_IFORK_Q(ip
) == 0);
3841 ASSERT(ip
->i_df
.if_ext_max
==
3842 XFS_IFORK_DSIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
));
3845 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
3846 tp
= xfs_trans_alloc(mp
, XFS_TRANS_ADDAFORK
);
3847 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
3849 tp
->t_flags
|= XFS_TRANS_RESERVE
;
3850 if ((error
= xfs_trans_reserve(tp
, blks
, XFS_ADDAFORK_LOG_RES(mp
), 0,
3851 XFS_TRANS_PERM_LOG_RES
, XFS_ADDAFORK_LOG_COUNT
)))
3853 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3854 error
= XFS_TRANS_RESERVE_QUOTA_NBLKS(mp
, tp
, ip
, blks
, 0, rsvd
?
3855 XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
3856 XFS_QMOPT_RES_REGBLKS
);
3858 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3859 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
);
3862 if (XFS_IFORK_Q(ip
))
3864 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
) {
3866 * For inodes coming from pre-6.2 filesystems.
3868 ASSERT(ip
->i_d
.di_aformat
== 0);
3869 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
3871 ASSERT(ip
->i_d
.di_anextents
== 0);
3872 VN_HOLD(XFS_ITOV(ip
));
3873 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
3874 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
3875 switch (ip
->i_d
.di_format
) {
3876 case XFS_DINODE_FMT_DEV
:
3877 ip
->i_d
.di_forkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
3879 case XFS_DINODE_FMT_UUID
:
3880 ip
->i_d
.di_forkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
3882 case XFS_DINODE_FMT_LOCAL
:
3883 case XFS_DINODE_FMT_EXTENTS
:
3884 case XFS_DINODE_FMT_BTREE
:
3885 ip
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(ip
, size
);
3886 if (!ip
->i_d
.di_forkoff
)
3887 ip
->i_d
.di_forkoff
= mp
->m_attroffset
>> 3;
3888 else if (!(mp
->m_flags
& XFS_MOUNT_COMPAT_ATTR
))
3893 error
= XFS_ERROR(EINVAL
);
3896 ip
->i_df
.if_ext_max
=
3897 XFS_IFORK_DSIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
);
3898 ASSERT(ip
->i_afp
== NULL
);
3899 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
);
3900 ip
->i_afp
->if_ext_max
=
3901 XFS_IFORK_ASIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
);
3902 ip
->i_afp
->if_flags
= XFS_IFEXTENTS
;
3904 XFS_BMAP_INIT(&flist
, &firstblock
);
3905 switch (ip
->i_d
.di_format
) {
3906 case XFS_DINODE_FMT_LOCAL
:
3907 error
= xfs_bmap_add_attrfork_local(tp
, ip
, &firstblock
, &flist
,
3910 case XFS_DINODE_FMT_EXTENTS
:
3911 error
= xfs_bmap_add_attrfork_extents(tp
, ip
, &firstblock
,
3914 case XFS_DINODE_FMT_BTREE
:
3915 error
= xfs_bmap_add_attrfork_btree(tp
, ip
, &firstblock
, &flist
,
3923 xfs_trans_log_inode(tp
, ip
, logflags
);
3926 if (!XFS_SB_VERSION_HASATTR(&mp
->m_sb
) ||
3927 (!XFS_SB_VERSION_HASATTR2(&mp
->m_sb
) && version
== 2)) {
3929 s
= XFS_SB_LOCK(mp
);
3930 if (!XFS_SB_VERSION_HASATTR(&mp
->m_sb
)) {
3931 XFS_SB_VERSION_ADDATTR(&mp
->m_sb
);
3932 logflags
|= XFS_SB_VERSIONNUM
;
3934 if (!XFS_SB_VERSION_HASATTR2(&mp
->m_sb
) && version
== 2) {
3935 XFS_SB_VERSION_ADDATTR2(&mp
->m_sb
);
3936 logflags
|= (XFS_SB_VERSIONNUM
| XFS_SB_FEATURES2
);
3939 XFS_SB_UNLOCK(mp
, s
);
3940 xfs_mod_sb(tp
, logflags
);
3942 XFS_SB_UNLOCK(mp
, s
);
3944 if ((error
= xfs_bmap_finish(&tp
, &flist
, firstblock
, &committed
)))
3946 error
= xfs_trans_commit(tp
, XFS_TRANS_PERM_LOG_RES
, NULL
);
3947 ASSERT(ip
->i_df
.if_ext_max
==
3948 XFS_IFORK_DSIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
));
3951 xfs_bmap_cancel(&flist
);
3953 ASSERT(ismrlocked(&ip
->i_lock
,MR_UPDATE
));
3954 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3956 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
3957 ASSERT(ip
->i_df
.if_ext_max
==
3958 XFS_IFORK_DSIZE(ip
) / (uint
)sizeof(xfs_bmbt_rec_t
));
3963 * Add the extent to the list of extents to be free at transaction end.
3964 * The list is maintained sorted (by block number).
3969 xfs_fsblock_t bno
, /* fs block number of extent */
3970 xfs_filblks_t len
, /* length of extent */
3971 xfs_bmap_free_t
*flist
, /* list of extents */
3972 xfs_mount_t
*mp
) /* mount point structure */
3974 xfs_bmap_free_item_t
*cur
; /* current (next) element */
3975 xfs_bmap_free_item_t
*new; /* new element */
3976 xfs_bmap_free_item_t
*prev
; /* previous element */
3978 xfs_agnumber_t agno
;
3979 xfs_agblock_t agbno
;
3981 ASSERT(bno
!= NULLFSBLOCK
);
3983 ASSERT(len
<= MAXEXTLEN
);
3984 ASSERT(!ISNULLSTARTBLOCK(bno
));
3985 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
3986 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
3987 ASSERT(agno
< mp
->m_sb
.sb_agcount
);
3988 ASSERT(agbno
< mp
->m_sb
.sb_agblocks
);
3989 ASSERT(len
< mp
->m_sb
.sb_agblocks
);
3990 ASSERT(agbno
+ len
<= mp
->m_sb
.sb_agblocks
);
3992 ASSERT(xfs_bmap_free_item_zone
!= NULL
);
3993 new = kmem_zone_alloc(xfs_bmap_free_item_zone
, KM_SLEEP
);
3994 new->xbfi_startblock
= bno
;
3995 new->xbfi_blockcount
= (xfs_extlen_t
)len
;
3996 for (prev
= NULL
, cur
= flist
->xbf_first
;
3998 prev
= cur
, cur
= cur
->xbfi_next
) {
3999 if (cur
->xbfi_startblock
>= bno
)
4003 prev
->xbfi_next
= new;
4005 flist
->xbf_first
= new;
4006 new->xbfi_next
= cur
;
4011 * Compute and fill in the value of the maximum depth of a bmap btree
4012 * in this filesystem. Done once, during mount.
4015 xfs_bmap_compute_maxlevels(
4016 xfs_mount_t
*mp
, /* file system mount structure */
4017 int whichfork
) /* data or attr fork */
4019 int level
; /* btree level */
4020 uint maxblocks
; /* max blocks at this level */
4021 uint maxleafents
; /* max leaf entries possible */
4022 int maxrootrecs
; /* max records in root block */
4023 int minleafrecs
; /* min records in leaf block */
4024 int minnoderecs
; /* min records in node block */
4025 int sz
; /* root block size */
4028 * The maximum number of extents in a file, hence the maximum
4029 * number of leaf entries, is controlled by the type of di_nextents
4030 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
4031 * (a signed 16-bit number, xfs_aextnum_t).
4033 if (whichfork
== XFS_DATA_FORK
) {
4034 maxleafents
= MAXEXTNUM
;
4035 sz
= (mp
->m_flags
& XFS_MOUNT_COMPAT_ATTR
) ?
4036 mp
->m_attroffset
: XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
4038 maxleafents
= MAXAEXTNUM
;
4039 sz
= (mp
->m_flags
& XFS_MOUNT_COMPAT_ATTR
) ?
4040 mp
->m_sb
.sb_inodesize
- mp
->m_attroffset
:
4041 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
4043 maxrootrecs
= (int)XFS_BTREE_BLOCK_MAXRECS(sz
, xfs_bmdr
, 0);
4044 minleafrecs
= mp
->m_bmap_dmnr
[0];
4045 minnoderecs
= mp
->m_bmap_dmnr
[1];
4046 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
4047 for (level
= 1; maxblocks
> 1; level
++) {
4048 if (maxblocks
<= maxrootrecs
)
4051 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
4053 mp
->m_bm_maxlevels
[whichfork
] = level
;
4057 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
4058 * caller. Frees all the extents that need freeing, which must be done
4059 * last due to locking considerations. We never free any extents in
4060 * the first transaction. This is to allow the caller to make the first
4061 * transaction a synchronous one so that the pointers to the data being
4062 * broken in this transaction will be permanent before the data is actually
4063 * freed. This is necessary to prevent blocks from being reallocated
4064 * and written to before the free and reallocation are actually permanent.
4065 * We do not just make the first transaction synchronous here, because
4066 * there are more efficient ways to gain the same protection in some cases
4067 * (see the file truncation code).
4069 * Return 1 if the given transaction was committed and a new one
4070 * started, and 0 otherwise in the committed parameter.
4075 xfs_trans_t
**tp
, /* transaction pointer addr */
4076 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
4077 xfs_fsblock_t firstblock
, /* controlled ag for allocs */
4078 int *committed
) /* xact committed or not */
4080 xfs_efd_log_item_t
*efd
; /* extent free data */
4081 xfs_efi_log_item_t
*efi
; /* extent free intention */
4082 int error
; /* error return value */
4083 xfs_bmap_free_item_t
*free
; /* free extent list item */
4084 unsigned int logres
; /* new log reservation */
4085 unsigned int logcount
; /* new log count */
4086 xfs_mount_t
*mp
; /* filesystem mount structure */
4087 xfs_bmap_free_item_t
*next
; /* next item on free list */
4088 xfs_trans_t
*ntp
; /* new transaction pointer */
4090 ASSERT((*tp
)->t_flags
& XFS_TRANS_PERM_LOG_RES
);
4091 if (flist
->xbf_count
== 0) {
4096 efi
= xfs_trans_get_efi(ntp
, flist
->xbf_count
);
4097 for (free
= flist
->xbf_first
; free
; free
= free
->xbfi_next
)
4098 xfs_trans_log_efi_extent(ntp
, efi
, free
->xbfi_startblock
,
4099 free
->xbfi_blockcount
);
4100 logres
= ntp
->t_log_res
;
4101 logcount
= ntp
->t_log_count
;
4102 ntp
= xfs_trans_dup(*tp
);
4103 error
= xfs_trans_commit(*tp
, 0, NULL
);
4107 * We have a new transaction, so we should return committed=1,
4108 * even though we're returning an error.
4113 if ((error
= xfs_trans_reserve(ntp
, 0, logres
, 0, XFS_TRANS_PERM_LOG_RES
,
4116 efd
= xfs_trans_get_efd(ntp
, efi
, flist
->xbf_count
);
4117 for (free
= flist
->xbf_first
; free
!= NULL
; free
= next
) {
4118 next
= free
->xbfi_next
;
4119 if ((error
= xfs_free_extent(ntp
, free
->xbfi_startblock
,
4120 free
->xbfi_blockcount
))) {
4122 * The bmap free list will be cleaned up at a
4123 * higher level. The EFI will be canceled when
4124 * this transaction is aborted.
4125 * Need to force shutdown here to make sure it
4126 * happens, since this transaction may not be
4130 if (!XFS_FORCED_SHUTDOWN(mp
))
4131 xfs_force_shutdown(mp
,
4132 (error
== EFSCORRUPTED
) ?
4133 XFS_CORRUPT_INCORE
:
4134 XFS_METADATA_IO_ERROR
);
4137 xfs_trans_log_efd_extent(ntp
, efd
, free
->xbfi_startblock
,
4138 free
->xbfi_blockcount
);
4139 xfs_bmap_del_free(flist
, NULL
, free
);
4145 * Free up any items left in the list.
4149 xfs_bmap_free_t
*flist
) /* list of bmap_free_items */
4151 xfs_bmap_free_item_t
*free
; /* free list item */
4152 xfs_bmap_free_item_t
*next
;
4154 if (flist
->xbf_count
== 0)
4156 ASSERT(flist
->xbf_first
!= NULL
);
4157 for (free
= flist
->xbf_first
; free
; free
= next
) {
4158 next
= free
->xbfi_next
;
4159 xfs_bmap_del_free(flist
, NULL
, free
);
4161 ASSERT(flist
->xbf_count
== 0);
4165 * Returns the file-relative block number of the first unused block(s)
4166 * in the file with at least "len" logically contiguous blocks free.
4167 * This is the lowest-address hole if the file has holes, else the first block
4168 * past the end of file.
4169 * Return 0 if the file is currently local (in-inode).
4172 xfs_bmap_first_unused(
4173 xfs_trans_t
*tp
, /* transaction pointer */
4174 xfs_inode_t
*ip
, /* incore inode */
4175 xfs_extlen_t len
, /* size of hole to find */
4176 xfs_fileoff_t
*first_unused
, /* unused block */
4177 int whichfork
) /* data or attr fork */
4179 xfs_bmbt_rec_t
*base
; /* base of extent array */
4180 xfs_bmbt_rec_t
*ep
; /* pointer to an extent entry */
4181 int error
; /* error return value */
4182 xfs_ifork_t
*ifp
; /* inode fork pointer */
4183 xfs_fileoff_t lastaddr
; /* last block number seen */
4184 xfs_fileoff_t lowest
; /* lowest useful block */
4185 xfs_fileoff_t max
; /* starting useful block */
4186 xfs_fileoff_t off
; /* offset for this block */
4187 xfs_extnum_t nextents
; /* number of extent entries */
4189 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
||
4190 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
||
4191 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
);
4192 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
4196 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4197 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
4198 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
4200 lowest
= *first_unused
;
4201 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
4202 base
= &ifp
->if_u1
.if_extents
[0];
4203 for (lastaddr
= 0, max
= lowest
, ep
= base
;
4204 ep
< &base
[nextents
];
4206 off
= xfs_bmbt_get_startoff(ep
);
4208 * See if the hole before this extent will work.
4210 if (off
>= lowest
+ len
&& off
- max
>= len
) {
4211 *first_unused
= max
;
4214 lastaddr
= off
+ xfs_bmbt_get_blockcount(ep
);
4215 max
= XFS_FILEOFF_MAX(lastaddr
, lowest
);
4217 *first_unused
= max
;
4222 * Returns the file-relative block number of the last block + 1 before
4223 * last_block (input value) in the file.
4224 * This is not based on i_size, it is based on the extent list.
4225 * Returns 0 for local files, as they do not have an extent list.
4228 xfs_bmap_last_before(
4229 xfs_trans_t
*tp
, /* transaction pointer */
4230 xfs_inode_t
*ip
, /* incore inode */
4231 xfs_fileoff_t
*last_block
, /* last block */
4232 int whichfork
) /* data or attr fork */
4234 xfs_fileoff_t bno
; /* input file offset */
4235 int eof
; /* hit end of file */
4236 xfs_bmbt_rec_t
*ep
; /* pointer to last extent */
4237 int error
; /* error return value */
4238 xfs_bmbt_irec_t got
; /* current extent value */
4239 xfs_ifork_t
*ifp
; /* inode fork pointer */
4240 xfs_extnum_t lastx
; /* last extent used */
4241 xfs_bmbt_irec_t prev
; /* previous extent value */
4243 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
4244 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4245 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
4246 return XFS_ERROR(EIO
);
4247 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
4251 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4252 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
4253 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
4255 bno
= *last_block
- 1;
4256 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
4258 if (eof
|| xfs_bmbt_get_startoff(ep
) > bno
) {
4259 if (prev
.br_startoff
== NULLFILEOFF
)
4262 *last_block
= prev
.br_startoff
+ prev
.br_blockcount
;
4265 * Otherwise *last_block is already the right answer.
4271 * Returns the file-relative block number of the first block past eof in
4272 * the file. This is not based on i_size, it is based on the extent list.
4273 * Returns 0 for local files, as they do not have an extent list.
4276 xfs_bmap_last_offset(
4277 xfs_trans_t
*tp
, /* transaction pointer */
4278 xfs_inode_t
*ip
, /* incore inode */
4279 xfs_fileoff_t
*last_block
, /* last block */
4280 int whichfork
) /* data or attr fork */
4282 xfs_bmbt_rec_t
*base
; /* base of extent array */
4283 xfs_bmbt_rec_t
*ep
; /* pointer to last extent */
4284 int error
; /* error return value */
4285 xfs_ifork_t
*ifp
; /* inode fork pointer */
4286 xfs_extnum_t nextents
; /* number of extent entries */
4288 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
4289 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4290 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
4291 return XFS_ERROR(EIO
);
4292 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
4296 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4297 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
4298 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
4300 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
4305 base
= &ifp
->if_u1
.if_extents
[0];
4306 ASSERT(base
!= NULL
);
4307 ep
= &base
[nextents
- 1];
4308 *last_block
= xfs_bmbt_get_startoff(ep
) + xfs_bmbt_get_blockcount(ep
);
4313 * Returns whether the selected fork of the inode has exactly one
4314 * block or not. For the data fork we check this matches di_size,
4315 * implying the file's range is 0..bsize-1.
4317 int /* 1=>1 block, 0=>otherwise */
4319 xfs_inode_t
*ip
, /* incore inode */
4320 int whichfork
) /* data or attr fork */
4322 xfs_bmbt_rec_t
*ep
; /* ptr to fork's extent */
4323 xfs_ifork_t
*ifp
; /* inode fork pointer */
4324 int rval
; /* return value */
4325 xfs_bmbt_irec_t s
; /* internal version of extent */
4328 if (whichfork
== XFS_DATA_FORK
)
4329 return ip
->i_d
.di_size
== ip
->i_mount
->m_sb
.sb_blocksize
;
4331 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) != 1)
4333 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
4335 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4336 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
4337 ep
= ifp
->if_u1
.if_extents
;
4338 xfs_bmbt_get_all(ep
, &s
);
4339 rval
= s
.br_startoff
== 0 && s
.br_blockcount
== 1;
4340 if (rval
&& whichfork
== XFS_DATA_FORK
)
4341 ASSERT(ip
->i_d
.di_size
== ip
->i_mount
->m_sb
.sb_blocksize
);
4346 * Read in the extents to if_extents.
4347 * All inode fields are set up by caller, we just traverse the btree
4348 * and copy the records in. If the file system cannot contain unwritten
4349 * extents, the records are checked for no "state" flags.
4352 xfs_bmap_read_extents(
4353 xfs_trans_t
*tp
, /* transaction pointer */
4354 xfs_inode_t
*ip
, /* incore inode */
4355 int whichfork
) /* data or attr fork */
4357 xfs_bmbt_block_t
*block
; /* current btree block */
4358 xfs_fsblock_t bno
; /* block # of "block" */
4359 xfs_buf_t
*bp
; /* buffer for "block" */
4360 int error
; /* error return value */
4361 xfs_exntfmt_t exntf
; /* XFS_EXTFMT_NOSTATE, if checking */
4362 #ifdef XFS_BMAP_TRACE
4363 static char fname
[] = "xfs_bmap_read_extents";
4365 xfs_extnum_t i
, j
; /* index into the extents list */
4366 xfs_ifork_t
*ifp
; /* fork structure */
4367 int level
; /* btree level, for checking */
4368 xfs_mount_t
*mp
; /* file system mount structure */
4369 xfs_bmbt_ptr_t
*pp
; /* pointer to block address */
4371 xfs_extnum_t room
; /* number of entries there's room for */
4372 xfs_bmbt_rec_t
*trp
; /* target record pointer */
4376 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4377 exntf
= (whichfork
!= XFS_DATA_FORK
) ? XFS_EXTFMT_NOSTATE
:
4378 XFS_EXTFMT_INODE(ip
);
4379 block
= ifp
->if_broot
;
4381 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4383 ASSERT(INT_GET(block
->bb_level
, ARCH_CONVERT
) > 0);
4384 level
= INT_GET(block
->bb_level
, ARCH_CONVERT
);
4385 pp
= XFS_BMAP_BROOT_PTR_ADDR(block
, 1, ifp
->if_broot_bytes
);
4386 ASSERT(INT_GET(*pp
, ARCH_CONVERT
) != NULLDFSBNO
);
4387 ASSERT(XFS_FSB_TO_AGNO(mp
, INT_GET(*pp
, ARCH_CONVERT
)) < mp
->m_sb
.sb_agcount
);
4388 ASSERT(XFS_FSB_TO_AGBNO(mp
, INT_GET(*pp
, ARCH_CONVERT
)) < mp
->m_sb
.sb_agblocks
);
4389 bno
= INT_GET(*pp
, ARCH_CONVERT
);
4391 * Go down the tree until leaf level is reached, following the first
4392 * pointer (leftmost) at each level.
4394 while (level
-- > 0) {
4395 if ((error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
4396 XFS_BMAP_BTREE_REF
)))
4398 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
4399 XFS_WANT_CORRUPTED_GOTO(
4400 XFS_BMAP_SANITY_CHECK(mp
, block
, level
),
4404 pp
= XFS_BTREE_PTR_ADDR(mp
->m_sb
.sb_blocksize
, xfs_bmbt
, block
,
4405 1, mp
->m_bmap_dmxr
[1]);
4406 XFS_WANT_CORRUPTED_GOTO(
4407 XFS_FSB_SANITY_CHECK(mp
, INT_GET(*pp
, ARCH_CONVERT
)),
4409 bno
= INT_GET(*pp
, ARCH_CONVERT
);
4410 xfs_trans_brelse(tp
, bp
);
4413 * Here with bp and block set to the leftmost leaf node in the tree.
4415 room
= ifp
->if_bytes
/ (uint
)sizeof(*trp
);
4416 trp
= ifp
->if_u1
.if_extents
;
4419 * Loop over all leaf nodes. Copy information to the extent list.
4422 xfs_bmbt_rec_t
*frp
, *temp
;
4423 xfs_fsblock_t nextbno
;
4424 xfs_extnum_t num_recs
;
4427 num_recs
= INT_GET(block
->bb_numrecs
, ARCH_CONVERT
);
4428 if (unlikely(i
+ num_recs
> room
)) {
4429 ASSERT(i
+ num_recs
<= room
);
4430 xfs_fs_cmn_err(CE_WARN
, ip
->i_mount
,
4431 "corrupt dinode %Lu, (btree extents). Unmount and run xfs_repair.",
4432 (unsigned long long) ip
->i_ino
);
4433 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4438 XFS_WANT_CORRUPTED_GOTO(
4439 XFS_BMAP_SANITY_CHECK(mp
, block
, 0),
4442 * Read-ahead the next leaf block, if any.
4444 nextbno
= INT_GET(block
->bb_rightsib
, ARCH_CONVERT
);
4445 if (nextbno
!= NULLFSBLOCK
)
4446 xfs_btree_reada_bufl(mp
, nextbno
, 1);
4448 * Copy records into the extent list.
4450 frp
= XFS_BTREE_REC_ADDR(mp
->m_sb
.sb_blocksize
, xfs_bmbt
,
4451 block
, 1, mp
->m_bmap_dmxr
[0]);
4453 for (j
= 0; j
< num_recs
; j
++, frp
++, trp
++) {
4454 trp
->l0
= INT_GET(frp
->l0
, ARCH_CONVERT
);
4455 trp
->l1
= INT_GET(frp
->l1
, ARCH_CONVERT
);
4457 if (exntf
== XFS_EXTFMT_NOSTATE
) {
4459 * Check all attribute bmap btree records and
4460 * any "older" data bmap btree records for a
4461 * set bit in the "extent flag" position.
4463 if (unlikely(xfs_check_nostate_extents(temp
, num_recs
))) {
4464 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4471 xfs_trans_brelse(tp
, bp
);
4474 * If we've reached the end, stop.
4476 if (bno
== NULLFSBLOCK
)
4478 if ((error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
4479 XFS_BMAP_BTREE_REF
)))
4481 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
4483 ASSERT(i
== ifp
->if_bytes
/ (uint
)sizeof(*trp
));
4484 ASSERT(i
== XFS_IFORK_NEXTENTS(ip
, whichfork
));
4485 xfs_bmap_trace_exlist(fname
, ip
, i
, whichfork
);
4488 xfs_trans_brelse(tp
, bp
);
4489 return XFS_ERROR(EFSCORRUPTED
);
4492 #ifdef XFS_BMAP_TRACE
4494 * Add bmap trace insert entries for all the contents of the extent list.
4497 xfs_bmap_trace_exlist(
4498 char *fname
, /* function name */
4499 xfs_inode_t
*ip
, /* incore inode pointer */
4500 xfs_extnum_t cnt
, /* count of entries in the list */
4501 int whichfork
) /* data or attr fork */
4503 xfs_bmbt_rec_t
*base
; /* base of extent list */
4504 xfs_bmbt_rec_t
*ep
; /* current entry in extent list */
4505 xfs_extnum_t idx
; /* extent list entry number */
4506 xfs_ifork_t
*ifp
; /* inode fork pointer */
4507 xfs_bmbt_irec_t s
; /* extent list record */
4509 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4510 ASSERT(cnt
== ifp
->if_bytes
/ (uint
)sizeof(*base
));
4511 base
= ifp
->if_u1
.if_extents
;
4512 for (idx
= 0, ep
= base
; idx
< cnt
; idx
++, ep
++) {
4513 xfs_bmbt_get_all(ep
, &s
);
4514 xfs_bmap_trace_insert(fname
, "exlist", ip
, idx
, 1, &s
, NULL
,
4522 * Validate that the bmbt_irecs being returned from bmapi are valid
4523 * given the callers original parameters. Specifically check the
4524 * ranges of the returned irecs to ensure that they only extent beyond
4525 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4528 xfs_bmap_validate_ret(
4532 xfs_bmbt_irec_t
*mval
,
4536 int i
; /* index to map values */
4538 ASSERT(ret_nmap
<= nmap
);
4540 for (i
= 0; i
< ret_nmap
; i
++) {
4541 ASSERT(mval
[i
].br_blockcount
> 0);
4542 if (!(flags
& XFS_BMAPI_ENTIRE
)) {
4543 ASSERT(mval
[i
].br_startoff
>= bno
);
4544 ASSERT(mval
[i
].br_blockcount
<= len
);
4545 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
<=
4548 ASSERT(mval
[i
].br_startoff
< bno
+ len
);
4549 ASSERT(mval
[i
].br_startoff
+ mval
[i
].br_blockcount
>
4553 mval
[i
- 1].br_startoff
+ mval
[i
- 1].br_blockcount
==
4554 mval
[i
].br_startoff
);
4555 if ((flags
& XFS_BMAPI_WRITE
) && !(flags
& XFS_BMAPI_DELAY
))
4556 ASSERT(mval
[i
].br_startblock
!= DELAYSTARTBLOCK
&&
4557 mval
[i
].br_startblock
!= HOLESTARTBLOCK
);
4558 ASSERT(mval
[i
].br_state
== XFS_EXT_NORM
||
4559 mval
[i
].br_state
== XFS_EXT_UNWRITTEN
);
4566 * Map file blocks to filesystem blocks.
4567 * File range is given by the bno/len pair.
4568 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4569 * into a hole or past eof.
4570 * Only allocates blocks from a single allocation group,
4571 * to avoid locking problems.
4572 * The returned value in "firstblock" from the first call in a transaction
4573 * must be remembered and presented to subsequent calls in "firstblock".
4574 * An upper bound for the number of blocks to be allocated is supplied to
4575 * the first call in "total"; if no allocation group has that many free
4576 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4580 xfs_trans_t
*tp
, /* transaction pointer */
4581 xfs_inode_t
*ip
, /* incore inode */
4582 xfs_fileoff_t bno
, /* starting file offs. mapped */
4583 xfs_filblks_t len
, /* length to map in file */
4584 int flags
, /* XFS_BMAPI_... */
4585 xfs_fsblock_t
*firstblock
, /* first allocated block
4586 controls a.g. for allocs */
4587 xfs_extlen_t total
, /* total blocks needed */
4588 xfs_bmbt_irec_t
*mval
, /* output: map values */
4589 int *nmap
, /* i/o: mval size/count */
4590 xfs_bmap_free_t
*flist
) /* i/o: list extents to free */
4592 xfs_fsblock_t abno
; /* allocated block number */
4593 xfs_extlen_t alen
; /* allocated extent length */
4594 xfs_fileoff_t aoff
; /* allocated file offset */
4595 xfs_bmalloca_t bma
; /* args for xfs_bmap_alloc */
4596 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
4597 xfs_fileoff_t end
; /* end of mapped file region */
4598 int eof
; /* we've hit the end of extent list */
4599 char contig
; /* allocation must be one extent */
4600 char delay
; /* this request is for delayed alloc */
4601 char exact
; /* don't do all of wasdelayed extent */
4602 xfs_bmbt_rec_t
*ep
; /* extent list entry pointer */
4603 int error
; /* error return */
4604 xfs_bmbt_irec_t got
; /* current extent list record */
4605 xfs_ifork_t
*ifp
; /* inode fork pointer */
4606 xfs_extlen_t indlen
; /* indirect blocks length */
4607 xfs_extnum_t lastx
; /* last useful extent number */
4608 int logflags
; /* flags for transaction logging */
4609 xfs_extlen_t minleft
; /* min blocks left after allocation */
4610 xfs_extlen_t minlen
; /* min allocation size */
4611 xfs_mount_t
*mp
; /* xfs mount structure */
4612 int n
; /* current extent index */
4613 int nallocs
; /* number of extents alloc\'d */
4614 xfs_extnum_t nextents
; /* number of extents in file */
4615 xfs_fileoff_t obno
; /* old block number (offset) */
4616 xfs_bmbt_irec_t prev
; /* previous extent list record */
4617 int tmp_logflags
; /* temp flags holder */
4618 int whichfork
; /* data or attr fork */
4619 char inhole
; /* current location is hole in file */
4620 char stateless
; /* ignore state flag set */
4621 char trim
; /* output trimmed to match range */
4622 char userdata
; /* allocating non-metadata */
4623 char wasdelay
; /* old extent was delayed */
4624 char wr
; /* this is a write request */
4625 char rt
; /* this is a realtime file */
4626 char rsvd
; /* OK to allocate reserved blocks */
4628 xfs_fileoff_t orig_bno
; /* original block number value */
4629 int orig_flags
; /* original flags arg value */
4630 xfs_filblks_t orig_len
; /* original value of len arg */
4631 xfs_bmbt_irec_t
*orig_mval
; /* original value of mval */
4632 int orig_nmap
; /* original value of *nmap */
4641 ASSERT(*nmap
<= XFS_BMAP_MAX_NMAP
|| !(flags
& XFS_BMAPI_WRITE
));
4642 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
4643 XFS_ATTR_FORK
: XFS_DATA_FORK
;
4645 if (unlikely(XFS_TEST_ERROR(
4646 (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
4647 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
4648 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
),
4649 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
4650 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW
, mp
);
4651 return XFS_ERROR(EFSCORRUPTED
);
4653 if (XFS_FORCED_SHUTDOWN(mp
))
4654 return XFS_ERROR(EIO
);
4655 rt
= XFS_IS_REALTIME_INODE(ip
);
4656 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
4657 ASSERT(ifp
->if_ext_max
==
4658 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
4659 if ((wr
= (flags
& XFS_BMAPI_WRITE
)) != 0)
4660 XFS_STATS_INC(xs_blk_mapw
);
4662 XFS_STATS_INC(xs_blk_mapr
);
4663 delay
= (flags
& XFS_BMAPI_DELAY
) != 0;
4664 trim
= (flags
& XFS_BMAPI_ENTIRE
) == 0;
4665 userdata
= (flags
& XFS_BMAPI_METADATA
) == 0;
4666 exact
= (flags
& XFS_BMAPI_EXACT
) != 0;
4667 rsvd
= (flags
& XFS_BMAPI_RSVBLOCKS
) != 0;
4668 contig
= (flags
& XFS_BMAPI_CONTIG
) != 0;
4670 * stateless is used to combine extents which
4671 * differ only due to the state of the extents.
4672 * This technique is used from xfs_getbmap()
4673 * when the caller does not wish to see the
4674 * separation (which is the default).
4676 * This technique is also used when writing a
4677 * buffer which has been partially written,
4678 * (usually by being flushed during a chunkread),
4679 * to ensure one write takes place. This also
4680 * prevents a change in the xfs inode extents at
4681 * this time, intentionally. This change occurs
4682 * on completion of the write operation, in
4683 * xfs_strat_comp(), where the xfs_bmapi() call
4684 * is transactioned, and the extents combined.
4686 stateless
= (flags
& XFS_BMAPI_IGSTATE
) != 0;
4687 if (stateless
&& wr
) /* if writing unwritten space, no */
4688 wr
= 0; /* allocations are allowed */
4689 ASSERT(wr
|| !delay
);
4693 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
4695 if ((error
= xfs_bmap_local_to_extents(tp
, ip
,
4696 firstblock
, total
, &logflags
, whichfork
)))
4699 if (wr
&& *firstblock
== NULLFSBLOCK
) {
4700 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
)
4701 minleft
= INT_GET(ifp
->if_broot
->bb_level
, ARCH_CONVERT
) + 1;
4706 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
4707 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
4709 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
4711 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
4716 while (bno
< end
&& n
< *nmap
) {
4718 * Reading past eof, act as though there's a hole
4722 got
.br_startoff
= end
;
4723 inhole
= eof
|| got
.br_startoff
> bno
;
4724 wasdelay
= wr
&& !inhole
&& !delay
&&
4725 ISNULLSTARTBLOCK(got
.br_startblock
);
4727 * First, deal with the hole before the allocated space
4728 * that we found, if any.
4730 if (wr
&& (inhole
|| wasdelay
)) {
4732 * For the wasdelay case, we could also just
4733 * allocate the stuff asked for in this bmap call
4734 * but that wouldn't be as good.
4736 if (wasdelay
&& !exact
) {
4737 alen
= (xfs_extlen_t
)got
.br_blockcount
;
4738 aoff
= got
.br_startoff
;
4739 if (lastx
!= NULLEXTNUM
&& lastx
) {
4740 ep
= &ifp
->if_u1
.if_extents
[lastx
- 1];
4741 xfs_bmbt_get_all(ep
, &prev
);
4743 } else if (wasdelay
) {
4744 alen
= (xfs_extlen_t
)
4745 XFS_FILBLKS_MIN(len
,
4747 got
.br_blockcount
) - bno
);
4750 alen
= (xfs_extlen_t
)
4751 XFS_FILBLKS_MIN(len
, MAXEXTLEN
);
4753 alen
= (xfs_extlen_t
)
4754 XFS_FILBLKS_MIN(alen
,
4755 got
.br_startoff
- bno
);
4758 minlen
= contig
? alen
: 1;
4760 xfs_extlen_t extsz
= 0;
4762 /* Figure out the extent size, adjust alen */
4764 if (!(extsz
= ip
->i_d
.di_extsize
))
4765 extsz
= mp
->m_sb
.sb_rextsize
;
4766 alen
= roundup(alen
, extsz
);
4767 extsz
= alen
/ mp
->m_sb
.sb_rextsize
;
4771 * Make a transaction-less quota reservation for
4772 * delayed allocation blocks. This number gets
4774 * We return EDQUOT if we haven't allocated
4775 * blks already inside this loop;
4777 if (XFS_TRANS_RESERVE_QUOTA_NBLKS(
4778 mp
, NULL
, ip
, (long)alen
, 0,
4779 rt
? XFS_QMOPT_RES_RTBLKS
:
4780 XFS_QMOPT_RES_REGBLKS
)) {
4783 ASSERT(cur
== NULL
);
4784 return XFS_ERROR(EDQUOT
);
4790 * Split changing sb for alen and indlen since
4791 * they could be coming from different places.
4793 indlen
= (xfs_extlen_t
)
4794 xfs_bmap_worst_indlen(ip
, alen
);
4798 error
= xfs_mod_incore_sb(mp
,
4802 error
= xfs_mod_incore_sb(mp
,
4806 error
= xfs_mod_incore_sb(mp
,
4810 xfs_mod_incore_sb(ip
->i_mount
,
4814 xfs_mod_incore_sb(ip
->i_mount
,
4821 if (XFS_IS_QUOTA_ON(ip
->i_mount
))
4822 /* unreserve the blocks now */
4823 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
4826 XFS_QMOPT_RES_RTBLKS
:
4827 XFS_QMOPT_RES_REGBLKS
);
4831 ip
->i_delayed_blks
+= alen
;
4832 abno
= NULLSTARTBLOCK(indlen
);
4835 * If first time, allocate and fill in
4836 * once-only bma fields.
4838 if (bma
.ip
== NULL
) {
4846 /* Indicate if this is the first user data
4847 * in the file, or just any user data.
4850 bma
.userdata
= (aoff
== 0) ?
4851 XFS_ALLOC_INITIAL_USER_DATA
:
4855 * Fill in changeable bma fields.
4858 bma
.firstblock
= *firstblock
;
4861 bma
.wasdel
= wasdelay
;
4862 bma
.minlen
= minlen
;
4863 bma
.low
= flist
->xbf_low
;
4864 bma
.minleft
= minleft
;
4866 * Only want to do the alignment at the
4867 * eof if it is userdata and allocation length
4868 * is larger than a stripe unit.
4870 if (mp
->m_dalign
&& alen
>= mp
->m_dalign
&&
4871 userdata
&& whichfork
== XFS_DATA_FORK
) {
4872 if ((error
= xfs_bmap_isaeof(ip
, aoff
,
4873 whichfork
, &bma
.aeof
)))
4880 if ((error
= xfs_bmap_alloc(&bma
)))
4883 * Copy out result fields.
4886 if ((flist
->xbf_low
= bma
.low
))
4890 ASSERT(*firstblock
== NULLFSBLOCK
||
4891 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
4892 XFS_FSB_TO_AGNO(mp
, bma
.firstblock
) ||
4894 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
4895 XFS_FSB_TO_AGNO(mp
, bma
.firstblock
)));
4896 *firstblock
= bma
.firstblock
;
4898 cur
->bc_private
.b
.firstblock
=
4900 if (abno
== NULLFSBLOCK
)
4902 if ((ifp
->if_flags
& XFS_IFBROOT
) && !cur
) {
4903 cur
= xfs_btree_init_cursor(mp
,
4904 tp
, NULL
, 0, XFS_BTNUM_BMAP
,
4906 cur
->bc_private
.b
.firstblock
=
4908 cur
->bc_private
.b
.flist
= flist
;
4911 * Bump the number of extents we've allocated
4917 cur
->bc_private
.b
.flags
=
4918 wasdelay
? XFS_BTCUR_BPRV_WASDEL
: 0;
4919 got
.br_startoff
= aoff
;
4920 got
.br_startblock
= abno
;
4921 got
.br_blockcount
= alen
;
4922 got
.br_state
= XFS_EXT_NORM
; /* assume normal */
4924 * Determine state of extent, and the filesystem.
4925 * A wasdelay extent has been initialized, so
4926 * shouldn't be flagged as unwritten.
4928 if (wr
&& XFS_SB_VERSION_HASEXTFLGBIT(&mp
->m_sb
)) {
4929 if (!wasdelay
&& (flags
& XFS_BMAPI_PREALLOC
))
4930 got
.br_state
= XFS_EXT_UNWRITTEN
;
4932 error
= xfs_bmap_add_extent(ip
, lastx
, &cur
, &got
,
4933 firstblock
, flist
, &tmp_logflags
, whichfork
,
4935 logflags
|= tmp_logflags
;
4938 lastx
= ifp
->if_lastex
;
4939 ep
= &ifp
->if_u1
.if_extents
[lastx
];
4940 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
4941 xfs_bmbt_get_all(ep
, &got
);
4942 ASSERT(got
.br_startoff
<= aoff
);
4943 ASSERT(got
.br_startoff
+ got
.br_blockcount
>=
4947 ASSERT(ISNULLSTARTBLOCK(got
.br_startblock
));
4948 ASSERT(STARTBLOCKVAL(got
.br_startblock
) > 0);
4950 ASSERT(got
.br_state
== XFS_EXT_NORM
||
4951 got
.br_state
== XFS_EXT_UNWRITTEN
);
4954 * Fall down into the found allocated space case.
4956 } else if (inhole
) {
4958 * Reading in a hole.
4960 mval
->br_startoff
= bno
;
4961 mval
->br_startblock
= HOLESTARTBLOCK
;
4962 mval
->br_blockcount
=
4963 XFS_FILBLKS_MIN(len
, got
.br_startoff
- bno
);
4964 mval
->br_state
= XFS_EXT_NORM
;
4965 bno
+= mval
->br_blockcount
;
4966 len
-= mval
->br_blockcount
;
4972 * Then deal with the allocated space we found.
4975 if (trim
&& (got
.br_startoff
+ got
.br_blockcount
> obno
)) {
4978 ASSERT((bno
>= obno
) || (n
== 0));
4980 mval
->br_startoff
= bno
;
4981 if (ISNULLSTARTBLOCK(got
.br_startblock
)) {
4982 ASSERT(!wr
|| delay
);
4983 mval
->br_startblock
= DELAYSTARTBLOCK
;
4985 mval
->br_startblock
=
4987 (bno
- got
.br_startoff
);
4989 * Return the minimum of what we got and what we
4990 * asked for for the length. We can use the len
4991 * variable here because it is modified below
4992 * and we could have been there before coming
4993 * here if the first part of the allocation
4994 * didn't overlap what was asked for.
4996 mval
->br_blockcount
=
4997 XFS_FILBLKS_MIN(end
- bno
, got
.br_blockcount
-
4998 (bno
- got
.br_startoff
));
4999 mval
->br_state
= got
.br_state
;
5000 ASSERT(mval
->br_blockcount
<= len
);
5003 if (ISNULLSTARTBLOCK(mval
->br_startblock
)) {
5004 ASSERT(!wr
|| delay
);
5005 mval
->br_startblock
= DELAYSTARTBLOCK
;
5010 * Check if writing previously allocated but
5011 * unwritten extents.
5013 if (wr
&& mval
->br_state
== XFS_EXT_UNWRITTEN
&&
5014 ((flags
& (XFS_BMAPI_PREALLOC
|XFS_BMAPI_DELAY
)) == 0)) {
5016 * Modify (by adding) the state flag, if writing.
5018 ASSERT(mval
->br_blockcount
<= len
);
5019 if ((ifp
->if_flags
& XFS_IFBROOT
) && !cur
) {
5020 cur
= xfs_btree_init_cursor(mp
,
5021 tp
, NULL
, 0, XFS_BTNUM_BMAP
,
5023 cur
->bc_private
.b
.firstblock
=
5025 cur
->bc_private
.b
.flist
= flist
;
5027 mval
->br_state
= XFS_EXT_NORM
;
5028 error
= xfs_bmap_add_extent(ip
, lastx
, &cur
, mval
,
5029 firstblock
, flist
, &tmp_logflags
, whichfork
,
5031 logflags
|= tmp_logflags
;
5034 lastx
= ifp
->if_lastex
;
5035 ep
= &ifp
->if_u1
.if_extents
[lastx
];
5036 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5037 xfs_bmbt_get_all(ep
, &got
);
5039 * We may have combined previously unwritten
5040 * space with written space, so generate
5043 if (mval
->br_blockcount
< len
)
5048 ((mval
->br_startoff
+ mval
->br_blockcount
) <= end
));
5049 ASSERT(!trim
|| (mval
->br_blockcount
<= len
) ||
5050 (mval
->br_startoff
< obno
));
5051 bno
= mval
->br_startoff
+ mval
->br_blockcount
;
5053 if (n
> 0 && mval
->br_startoff
== mval
[-1].br_startoff
) {
5054 ASSERT(mval
->br_startblock
== mval
[-1].br_startblock
);
5055 ASSERT(mval
->br_blockcount
> mval
[-1].br_blockcount
);
5056 ASSERT(mval
->br_state
== mval
[-1].br_state
);
5057 mval
[-1].br_blockcount
= mval
->br_blockcount
;
5058 mval
[-1].br_state
= mval
->br_state
;
5059 } else if (n
> 0 && mval
->br_startblock
!= DELAYSTARTBLOCK
&&
5060 mval
[-1].br_startblock
!= DELAYSTARTBLOCK
&&
5061 mval
[-1].br_startblock
!= HOLESTARTBLOCK
&&
5062 mval
->br_startblock
==
5063 mval
[-1].br_startblock
+ mval
[-1].br_blockcount
&&
5064 (stateless
|| mval
[-1].br_state
== mval
->br_state
)) {
5065 ASSERT(mval
->br_startoff
==
5066 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
);
5067 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
5069 mval
->br_startblock
== DELAYSTARTBLOCK
&&
5070 mval
[-1].br_startblock
== DELAYSTARTBLOCK
&&
5071 mval
->br_startoff
==
5072 mval
[-1].br_startoff
+ mval
[-1].br_blockcount
) {
5073 mval
[-1].br_blockcount
+= mval
->br_blockcount
;
5074 mval
[-1].br_state
= mval
->br_state
;
5075 } else if (!((n
== 0) &&
5076 ((mval
->br_startoff
+ mval
->br_blockcount
) <=
5082 * If we're done, stop now. Stop when we've allocated
5083 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5084 * the transaction may get too big.
5086 if (bno
>= end
|| n
>= *nmap
|| nallocs
>= *nmap
)
5089 * Else go on to the next record.
5093 if (lastx
>= nextents
) {
5097 xfs_bmbt_get_all(ep
, &got
);
5099 ifp
->if_lastex
= lastx
;
5102 * Transform from btree to extents, give it cur.
5104 if (tp
&& XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
5105 XFS_IFORK_NEXTENTS(ip
, whichfork
) <= ifp
->if_ext_max
) {
5107 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
,
5108 &tmp_logflags
, whichfork
);
5109 logflags
|= tmp_logflags
;
5113 ASSERT(ifp
->if_ext_max
==
5114 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
5115 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
||
5116 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ifp
->if_ext_max
);
5121 * Log everything. Do this after conversion, there's no point in
5122 * logging the extent list if we've converted to btree format.
5124 if ((logflags
& XFS_ILOG_FEXT(whichfork
)) &&
5125 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5126 logflags
&= ~XFS_ILOG_FEXT(whichfork
);
5127 else if ((logflags
& XFS_ILOG_FBROOT(whichfork
)) &&
5128 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5129 logflags
&= ~XFS_ILOG_FBROOT(whichfork
);
5131 * Log whatever the flags say, even if error. Otherwise we might miss
5132 * detecting a case where the data is changed, there's an error,
5133 * and it's not logged so we don't shutdown when we should.
5137 xfs_trans_log_inode(tp
, ip
, logflags
);
5141 ASSERT(*firstblock
== NULLFSBLOCK
||
5142 XFS_FSB_TO_AGNO(mp
, *firstblock
) ==
5144 cur
->bc_private
.b
.firstblock
) ||
5146 XFS_FSB_TO_AGNO(mp
, *firstblock
) <
5148 cur
->bc_private
.b
.firstblock
)));
5149 *firstblock
= cur
->bc_private
.b
.firstblock
;
5151 xfs_btree_del_cursor(cur
,
5152 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5155 xfs_bmap_validate_ret(orig_bno
, orig_len
, orig_flags
, orig_mval
,
5161 * Map file blocks to filesystem blocks, simple version.
5162 * One block (extent) only, read-only.
5163 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5164 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5165 * was set and all the others were clear.
5169 xfs_trans_t
*tp
, /* transaction pointer */
5170 xfs_inode_t
*ip
, /* incore inode */
5171 int whichfork
, /* data or attr fork */
5172 xfs_fsblock_t
*fsb
, /* output: mapped block */
5173 xfs_fileoff_t bno
) /* starting file offs. mapped */
5175 int eof
; /* we've hit the end of extent list */
5176 int error
; /* error return */
5177 xfs_bmbt_irec_t got
; /* current extent list record */
5178 xfs_ifork_t
*ifp
; /* inode fork pointer */
5179 xfs_extnum_t lastx
; /* last useful extent number */
5180 xfs_bmbt_irec_t prev
; /* previous extent list record */
5182 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5184 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
5185 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)) {
5186 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW
,
5188 return XFS_ERROR(EFSCORRUPTED
);
5190 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
5191 return XFS_ERROR(EIO
);
5192 XFS_STATS_INC(xs_blk_mapr
);
5193 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5194 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5196 (void)xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5199 * Reading past eof, act as though there's a hole
5202 if (eof
|| got
.br_startoff
> bno
) {
5206 ASSERT(!ISNULLSTARTBLOCK(got
.br_startblock
));
5207 ASSERT(bno
< got
.br_startoff
+ got
.br_blockcount
);
5208 *fsb
= got
.br_startblock
+ (bno
- got
.br_startoff
);
5209 ifp
->if_lastex
= lastx
;
5214 * Unmap (remove) blocks from a file.
5215 * If nexts is nonzero then the number of extents to remove is limited to
5216 * that value. If not all extents in the block range can be removed then
5221 xfs_trans_t
*tp
, /* transaction pointer */
5222 struct xfs_inode
*ip
, /* incore inode */
5223 xfs_fileoff_t bno
, /* starting offset to unmap */
5224 xfs_filblks_t len
, /* length to unmap in file */
5225 int flags
, /* misc flags */
5226 xfs_extnum_t nexts
, /* number of extents max */
5227 xfs_fsblock_t
*firstblock
, /* first allocated block
5228 controls a.g. for allocs */
5229 xfs_bmap_free_t
*flist
, /* i/o: list extents to free */
5230 int *done
) /* set if not done yet */
5232 xfs_btree_cur_t
*cur
; /* bmap btree cursor */
5233 xfs_bmbt_irec_t del
; /* extent being deleted */
5234 int eof
; /* is deleting at eof */
5235 xfs_bmbt_rec_t
*ep
; /* extent list entry pointer */
5236 int error
; /* error return value */
5237 xfs_extnum_t extno
; /* extent number in list */
5238 xfs_bmbt_irec_t got
; /* current extent list entry */
5239 xfs_ifork_t
*ifp
; /* inode fork pointer */
5240 int isrt
; /* freeing in rt area */
5241 xfs_extnum_t lastx
; /* last extent index used */
5242 int logflags
; /* transaction logging flags */
5243 xfs_extlen_t mod
; /* rt extent offset */
5244 xfs_mount_t
*mp
; /* mount structure */
5245 xfs_extnum_t nextents
; /* size of extent list */
5246 xfs_bmbt_irec_t prev
; /* previous extent list entry */
5247 xfs_fileoff_t start
; /* first file offset deleted */
5248 int tmp_logflags
; /* partial logging flags */
5249 int wasdel
; /* was a delayed alloc extent */
5250 int whichfork
; /* data or attribute fork */
5251 int rsvd
; /* OK to allocate reserved blocks */
5254 xfs_bunmap_trace(ip
, bno
, len
, flags
, (inst_t
*)__return_address
);
5255 whichfork
= (flags
& XFS_BMAPI_ATTRFORK
) ?
5256 XFS_ATTR_FORK
: XFS_DATA_FORK
;
5257 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5259 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
5260 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
5261 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW
,
5263 return XFS_ERROR(EFSCORRUPTED
);
5266 if (XFS_FORCED_SHUTDOWN(mp
))
5267 return XFS_ERROR(EIO
);
5268 rsvd
= (flags
& XFS_BMAPI_RSVBLOCKS
) != 0;
5271 ASSERT(ifp
->if_ext_max
==
5272 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
5273 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5274 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
5276 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5277 if (nextents
== 0) {
5281 XFS_STATS_INC(xs_blk_unmap
);
5282 isrt
= (whichfork
== XFS_DATA_FORK
) &&
5283 (ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
);
5285 bno
= start
+ len
- 1;
5286 ep
= xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
,
5289 * Check to see if the given block number is past the end of the
5290 * file, back up to the last block if so...
5293 ep
= &ifp
->if_u1
.if_extents
[--lastx
];
5294 xfs_bmbt_get_all(ep
, &got
);
5295 bno
= got
.br_startoff
+ got
.br_blockcount
- 1;
5298 if (ifp
->if_flags
& XFS_IFBROOT
) {
5299 ASSERT(XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
);
5300 cur
= xfs_btree_init_cursor(mp
, tp
, NULL
, 0, XFS_BTNUM_BMAP
, ip
,
5302 cur
->bc_private
.b
.firstblock
= *firstblock
;
5303 cur
->bc_private
.b
.flist
= flist
;
5304 cur
->bc_private
.b
.flags
= 0;
5308 while (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
&& lastx
>= 0 &&
5309 (nexts
== 0 || extno
< nexts
)) {
5311 * Is the found extent after a hole in which bno lives?
5312 * Just back up to the previous extent, if so.
5314 if (got
.br_startoff
> bno
) {
5318 xfs_bmbt_get_all(ep
, &got
);
5321 * Is the last block of this extent before the range
5322 * we're supposed to delete? If so, we're done.
5324 bno
= XFS_FILEOFF_MIN(bno
,
5325 got
.br_startoff
+ got
.br_blockcount
- 1);
5329 * Then deal with the (possibly delayed) allocated space
5334 wasdel
= ISNULLSTARTBLOCK(del
.br_startblock
);
5335 if (got
.br_startoff
< start
) {
5336 del
.br_startoff
= start
;
5337 del
.br_blockcount
-= start
- got
.br_startoff
;
5339 del
.br_startblock
+= start
- got
.br_startoff
;
5341 if (del
.br_startoff
+ del
.br_blockcount
> bno
+ 1)
5342 del
.br_blockcount
= bno
+ 1 - del
.br_startoff
;
5343 sum
= del
.br_startblock
+ del
.br_blockcount
;
5345 (mod
= do_mod(sum
, mp
->m_sb
.sb_rextsize
))) {
5347 * Realtime extent not lined up at the end.
5348 * The extent could have been split into written
5349 * and unwritten pieces, or we could just be
5350 * unmapping part of it. But we can't really
5351 * get rid of part of a realtime extent.
5353 if (del
.br_state
== XFS_EXT_UNWRITTEN
||
5354 !XFS_SB_VERSION_HASEXTFLGBIT(&mp
->m_sb
)) {
5356 * This piece is unwritten, or we're not
5357 * using unwritten extents. Skip over it.
5360 bno
-= mod
> del
.br_blockcount
?
5361 del
.br_blockcount
: mod
;
5362 if (bno
< got
.br_startoff
) {
5364 xfs_bmbt_get_all(--ep
, &got
);
5369 * It's written, turn it unwritten.
5370 * This is better than zeroing it.
5372 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5373 ASSERT(xfs_trans_get_block_res(tp
) > 0);
5375 * If this spans a realtime extent boundary,
5376 * chop it back to the start of the one we end at.
5378 if (del
.br_blockcount
> mod
) {
5379 del
.br_startoff
+= del
.br_blockcount
- mod
;
5380 del
.br_startblock
+= del
.br_blockcount
- mod
;
5381 del
.br_blockcount
= mod
;
5383 del
.br_state
= XFS_EXT_UNWRITTEN
;
5384 error
= xfs_bmap_add_extent(ip
, lastx
, &cur
, &del
,
5385 firstblock
, flist
, &logflags
, XFS_DATA_FORK
, 0);
5390 if (isrt
&& (mod
= do_mod(del
.br_startblock
, mp
->m_sb
.sb_rextsize
))) {
5392 * Realtime extent is lined up at the end but not
5393 * at the front. We'll get rid of full extents if
5396 mod
= mp
->m_sb
.sb_rextsize
- mod
;
5397 if (del
.br_blockcount
> mod
) {
5398 del
.br_blockcount
-= mod
;
5399 del
.br_startoff
+= mod
;
5400 del
.br_startblock
+= mod
;
5401 } else if ((del
.br_startoff
== start
&&
5402 (del
.br_state
== XFS_EXT_UNWRITTEN
||
5403 xfs_trans_get_block_res(tp
) == 0)) ||
5404 !XFS_SB_VERSION_HASEXTFLGBIT(&mp
->m_sb
)) {
5406 * Can't make it unwritten. There isn't
5407 * a full extent here so just skip it.
5409 ASSERT(bno
>= del
.br_blockcount
);
5410 bno
-= del
.br_blockcount
;
5411 if (bno
< got
.br_startoff
) {
5413 xfs_bmbt_get_all(--ep
, &got
);
5416 } else if (del
.br_state
== XFS_EXT_UNWRITTEN
) {
5418 * This one is already unwritten.
5419 * It must have a written left neighbor.
5420 * Unwrite the killed part of that one and
5424 xfs_bmbt_get_all(ep
- 1, &prev
);
5425 ASSERT(prev
.br_state
== XFS_EXT_NORM
);
5426 ASSERT(!ISNULLSTARTBLOCK(prev
.br_startblock
));
5427 ASSERT(del
.br_startblock
==
5428 prev
.br_startblock
+ prev
.br_blockcount
);
5429 if (prev
.br_startoff
< start
) {
5430 mod
= start
- prev
.br_startoff
;
5431 prev
.br_blockcount
-= mod
;
5432 prev
.br_startblock
+= mod
;
5433 prev
.br_startoff
= start
;
5435 prev
.br_state
= XFS_EXT_UNWRITTEN
;
5436 error
= xfs_bmap_add_extent(ip
, lastx
- 1, &cur
,
5437 &prev
, firstblock
, flist
, &logflags
,
5443 ASSERT(del
.br_state
== XFS_EXT_NORM
);
5444 del
.br_state
= XFS_EXT_UNWRITTEN
;
5445 error
= xfs_bmap_add_extent(ip
, lastx
, &cur
,
5446 &del
, firstblock
, flist
, &logflags
,
5454 ASSERT(STARTBLOCKVAL(del
.br_startblock
) > 0);
5455 /* Update realtim/data freespace, unreserve quota */
5457 xfs_filblks_t rtexts
;
5459 rtexts
= XFS_FSB_TO_B(mp
, del
.br_blockcount
);
5460 do_div(rtexts
, mp
->m_sb
.sb_rextsize
);
5461 xfs_mod_incore_sb(mp
, XFS_SBS_FREXTENTS
,
5463 XFS_TRANS_RESERVE_QUOTA_NBLKS(mp
, NULL
, ip
,
5464 -((long)del
.br_blockcount
), 0,
5465 XFS_QMOPT_RES_RTBLKS
);
5467 xfs_mod_incore_sb(mp
, XFS_SBS_FDBLOCKS
,
5468 (int)del
.br_blockcount
, rsvd
);
5469 XFS_TRANS_RESERVE_QUOTA_NBLKS(mp
, NULL
, ip
,
5470 -((long)del
.br_blockcount
), 0,
5471 XFS_QMOPT_RES_REGBLKS
);
5473 ip
->i_delayed_blks
-= del
.br_blockcount
;
5475 cur
->bc_private
.b
.flags
|=
5476 XFS_BTCUR_BPRV_WASDEL
;
5478 cur
->bc_private
.b
.flags
&= ~XFS_BTCUR_BPRV_WASDEL
;
5480 * If it's the case where the directory code is running
5481 * with no block reservation, and the deleted block is in
5482 * the middle of its extent, and the resulting insert
5483 * of an extent would cause transformation to btree format,
5484 * then reject it. The calling code will then swap
5485 * blocks around instead.
5486 * We have to do this now, rather than waiting for the
5487 * conversion to btree format, since the transaction
5490 if (!wasdel
&& xfs_trans_get_block_res(tp
) == 0 &&
5491 XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5492 XFS_IFORK_NEXTENTS(ip
, whichfork
) >= ifp
->if_ext_max
&&
5493 del
.br_startoff
> got
.br_startoff
&&
5494 del
.br_startoff
+ del
.br_blockcount
<
5495 got
.br_startoff
+ got
.br_blockcount
) {
5496 error
= XFS_ERROR(ENOSPC
);
5499 error
= xfs_bmap_del_extent(ip
, tp
, lastx
, flist
, cur
, &del
,
5500 &tmp_logflags
, whichfork
, rsvd
);
5501 logflags
|= tmp_logflags
;
5504 bno
= del
.br_startoff
- 1;
5506 lastx
= ifp
->if_lastex
;
5508 * If not done go on to the next (previous) record.
5509 * Reset ep in case the extents array was re-alloced.
5511 ep
= &ifp
->if_u1
.if_extents
[lastx
];
5512 if (bno
!= (xfs_fileoff_t
)-1 && bno
>= start
) {
5513 if (lastx
>= XFS_IFORK_NEXTENTS(ip
, whichfork
) ||
5514 xfs_bmbt_get_startoff(ep
) > bno
) {
5519 xfs_bmbt_get_all(ep
, &got
);
5523 ifp
->if_lastex
= lastx
;
5524 *done
= bno
== (xfs_fileoff_t
)-1 || bno
< start
|| lastx
< 0;
5525 ASSERT(ifp
->if_ext_max
==
5526 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
5528 * Convert to a btree if necessary.
5530 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
&&
5531 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ifp
->if_ext_max
) {
5532 ASSERT(cur
== NULL
);
5533 error
= xfs_bmap_extents_to_btree(tp
, ip
, firstblock
, flist
,
5534 &cur
, 0, &tmp_logflags
, whichfork
);
5535 logflags
|= tmp_logflags
;
5540 * transform from btree to extents, give it cur
5542 else if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_BTREE
&&
5543 XFS_IFORK_NEXTENTS(ip
, whichfork
) <= ifp
->if_ext_max
) {
5544 ASSERT(cur
!= NULL
);
5545 error
= xfs_bmap_btree_to_extents(tp
, ip
, cur
, &tmp_logflags
,
5547 logflags
|= tmp_logflags
;
5552 * transform from extents to local?
5554 ASSERT(ifp
->if_ext_max
==
5555 XFS_IFORK_SIZE(ip
, whichfork
) / (uint
)sizeof(xfs_bmbt_rec_t
));
5559 * Log everything. Do this after conversion, there's no point in
5560 * logging the extent list if we've converted to btree format.
5562 if ((logflags
& XFS_ILOG_FEXT(whichfork
)) &&
5563 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
)
5564 logflags
&= ~XFS_ILOG_FEXT(whichfork
);
5565 else if ((logflags
& XFS_ILOG_FBROOT(whichfork
)) &&
5566 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)
5567 logflags
&= ~XFS_ILOG_FBROOT(whichfork
);
5569 * Log inode even in the error case, if the transaction
5570 * is dirty we'll need to shut down the filesystem.
5573 xfs_trans_log_inode(tp
, ip
, logflags
);
5576 *firstblock
= cur
->bc_private
.b
.firstblock
;
5577 cur
->bc_private
.b
.allocated
= 0;
5579 xfs_btree_del_cursor(cur
,
5580 error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
5586 * Fcntl interface to xfs_bmapi.
5588 int /* error code */
5590 bhv_desc_t
*bdp
, /* XFS behavior descriptor*/
5591 struct getbmap
*bmv
, /* user bmap structure */
5592 void __user
*ap
, /* pointer to user's array */
5593 int interface
) /* interface flags */
5595 __int64_t bmvend
; /* last block requested */
5596 int error
; /* return value */
5597 __int64_t fixlen
; /* length for -1 case */
5598 int i
; /* extent number */
5599 xfs_inode_t
*ip
; /* xfs incore inode pointer */
5600 vnode_t
*vp
; /* corresponding vnode */
5601 int lock
; /* lock state */
5602 xfs_bmbt_irec_t
*map
; /* buffer for user's data */
5603 xfs_mount_t
*mp
; /* file system mount point */
5604 int nex
; /* # of user extents can do */
5605 int nexleft
; /* # of user extents left */
5606 int subnex
; /* # of bmapi's can do */
5607 int nmap
; /* number of map entries */
5608 struct getbmap out
; /* output structure */
5609 int whichfork
; /* data or attr fork */
5610 int prealloced
; /* this is a file with
5611 * preallocated data space */
5612 int sh_unwritten
; /* true, if unwritten */
5613 /* extents listed separately */
5614 int bmapi_flags
; /* flags for xfs_bmapi */
5615 __int32_t oflags
; /* getbmapx bmv_oflags field */
5617 vp
= BHV_TO_VNODE(bdp
);
5618 ip
= XFS_BHVTOI(bdp
);
5621 whichfork
= interface
& BMV_IF_ATTRFORK
? XFS_ATTR_FORK
: XFS_DATA_FORK
;
5622 sh_unwritten
= (interface
& BMV_IF_PREALLOC
) != 0;
5624 /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
5625 * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
5626 * bit is set for the file, generate a read event in order
5627 * that the DMAPI application may do its thing before we return
5628 * the extents. Usually this means restoring user file data to
5629 * regions of the file that look like holes.
5631 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5632 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5633 * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
5634 * could misinterpret holes in a DMAPI file as true holes,
5635 * when in fact they may represent offline user data.
5637 if ( (interface
& BMV_IF_NO_DMAPI_READ
) == 0
5638 && DM_EVENT_ENABLED(vp
->v_vfsp
, ip
, DM_EVENT_READ
)
5639 && whichfork
== XFS_DATA_FORK
) {
5641 error
= XFS_SEND_DATA(mp
, DM_EVENT_READ
, vp
, 0, 0, 0, NULL
);
5643 return XFS_ERROR(error
);
5646 if (whichfork
== XFS_ATTR_FORK
) {
5647 if (XFS_IFORK_Q(ip
)) {
5648 if (ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
&&
5649 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_BTREE
&&
5650 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_LOCAL
)
5651 return XFS_ERROR(EINVAL
);
5652 } else if (unlikely(
5653 ip
->i_d
.di_aformat
!= 0 &&
5654 ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_EXTENTS
)) {
5655 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW
,
5657 return XFS_ERROR(EFSCORRUPTED
);
5659 } else if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_EXTENTS
&&
5660 ip
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
&&
5661 ip
->i_d
.di_format
!= XFS_DINODE_FMT_LOCAL
)
5662 return XFS_ERROR(EINVAL
);
5663 if (whichfork
== XFS_DATA_FORK
) {
5664 if (ip
->i_d
.di_flags
& XFS_DIFLAG_PREALLOC
) {
5666 fixlen
= XFS_MAXIOFFSET(mp
);
5669 fixlen
= ip
->i_d
.di_size
;
5676 if (bmv
->bmv_length
== -1) {
5677 fixlen
= XFS_FSB_TO_BB(mp
, XFS_B_TO_FSB(mp
, fixlen
));
5678 bmv
->bmv_length
= MAX( (__int64_t
)(fixlen
- bmv
->bmv_offset
),
5680 } else if (bmv
->bmv_length
< 0)
5681 return XFS_ERROR(EINVAL
);
5682 if (bmv
->bmv_length
== 0) {
5683 bmv
->bmv_entries
= 0;
5686 nex
= bmv
->bmv_count
- 1;
5688 return XFS_ERROR(EINVAL
);
5689 bmvend
= bmv
->bmv_offset
+ bmv
->bmv_length
;
5691 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
5693 if (whichfork
== XFS_DATA_FORK
&& ip
->i_delayed_blks
) {
5694 /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
5695 VOP_FLUSH_PAGES(vp
, (xfs_off_t
)0, -1, 0, FI_REMAPF
, error
);
5698 ASSERT(whichfork
== XFS_ATTR_FORK
|| ip
->i_delayed_blks
== 0);
5700 lock
= xfs_ilock_map_shared(ip
);
5703 * Don't let nex be bigger than the number of extents
5704 * we can have assuming alternating holes and real extents.
5706 if (nex
> XFS_IFORK_NEXTENTS(ip
, whichfork
) * 2 + 1)
5707 nex
= XFS_IFORK_NEXTENTS(ip
, whichfork
) * 2 + 1;
5709 bmapi_flags
= XFS_BMAPI_AFLAG(whichfork
) |
5710 ((sh_unwritten
) ? 0 : XFS_BMAPI_IGSTATE
);
5713 * Allocate enough space to handle "subnex" maps at a time.
5716 map
= kmem_alloc(subnex
* sizeof(*map
), KM_SLEEP
);
5718 bmv
->bmv_entries
= 0;
5720 if (XFS_IFORK_NEXTENTS(ip
, whichfork
) == 0) {
5722 goto unlock_and_return
;
5728 nmap
= (nexleft
> subnex
) ? subnex
: nexleft
;
5729 error
= xfs_bmapi(NULL
, ip
, XFS_BB_TO_FSBT(mp
, bmv
->bmv_offset
),
5730 XFS_BB_TO_FSB(mp
, bmv
->bmv_length
),
5731 bmapi_flags
, NULL
, 0, map
, &nmap
, NULL
);
5733 goto unlock_and_return
;
5734 ASSERT(nmap
<= subnex
);
5736 for (i
= 0; i
< nmap
&& nexleft
&& bmv
->bmv_length
; i
++) {
5738 oflags
= (map
[i
].br_state
== XFS_EXT_UNWRITTEN
) ?
5739 BMV_OF_PREALLOC
: 0;
5740 out
.bmv_offset
= XFS_FSB_TO_BB(mp
, map
[i
].br_startoff
);
5741 out
.bmv_length
= XFS_FSB_TO_BB(mp
, map
[i
].br_blockcount
);
5742 ASSERT(map
[i
].br_startblock
!= DELAYSTARTBLOCK
);
5744 map
[i
].br_startblock
== HOLESTARTBLOCK
&&
5745 out
.bmv_offset
+ out
.bmv_length
== bmvend
) {
5747 * came to hole at end of file
5749 goto unlock_and_return
;
5752 (map
[i
].br_startblock
== HOLESTARTBLOCK
) ?
5754 XFS_FSB_TO_DB(ip
, map
[i
].br_startblock
);
5756 /* return either getbmap/getbmapx structure. */
5757 if (interface
& BMV_IF_EXTENDED
) {
5758 struct getbmapx outx
;
5760 GETBMAP_CONVERT(out
,outx
);
5761 outx
.bmv_oflags
= oflags
;
5762 outx
.bmv_unused1
= outx
.bmv_unused2
= 0;
5763 if (copy_to_user(ap
, &outx
,
5765 error
= XFS_ERROR(EFAULT
);
5766 goto unlock_and_return
;
5769 if (copy_to_user(ap
, &out
,
5771 error
= XFS_ERROR(EFAULT
);
5772 goto unlock_and_return
;
5776 out
.bmv_offset
+ out
.bmv_length
;
5777 bmv
->bmv_length
= MAX((__int64_t
)0,
5778 (__int64_t
)(bmvend
- bmv
->bmv_offset
));
5780 ap
= (interface
& BMV_IF_EXTENDED
) ?
5782 ((struct getbmapx __user
*)ap
+ 1) :
5784 ((struct getbmap __user
*)ap
+ 1);
5787 } while (nmap
&& nexleft
&& bmv
->bmv_length
);
5790 xfs_iunlock_map_shared(ip
, lock
);
5791 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
5793 kmem_free(map
, subnex
* sizeof(*map
));
5799 * Check the last inode extent to determine whether this allocation will result
5800 * in blocks being allocated at the end of the file. When we allocate new data
5801 * blocks at the end of the file which do not start at the previous data block,
5802 * we will try to align the new blocks at stripe unit boundaries.
5804 STATIC
int /* error */
5806 xfs_inode_t
*ip
, /* incore inode pointer */
5807 xfs_fileoff_t off
, /* file offset in fsblocks */
5808 int whichfork
, /* data or attribute fork */
5809 char *aeof
) /* return value */
5811 int error
; /* error return value */
5812 xfs_ifork_t
*ifp
; /* inode fork pointer */
5813 xfs_bmbt_rec_t
*lastrec
; /* extent list entry pointer */
5814 xfs_extnum_t nextents
; /* size of extent list */
5815 xfs_bmbt_irec_t s
; /* expanded extent list entry */
5817 ASSERT(whichfork
== XFS_DATA_FORK
);
5818 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5819 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5820 (error
= xfs_iread_extents(NULL
, ip
, whichfork
)))
5822 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5823 if (nextents
== 0) {
5828 * Go to the last extent
5830 lastrec
= &ifp
->if_u1
.if_extents
[nextents
- 1];
5831 xfs_bmbt_get_all(lastrec
, &s
);
5833 * Check we are allocating in the last extent (for delayed allocations)
5834 * or past the last extent for non-delayed allocations.
5836 *aeof
= (off
>= s
.br_startoff
&&
5837 off
< s
.br_startoff
+ s
.br_blockcount
&&
5838 ISNULLSTARTBLOCK(s
.br_startblock
)) ||
5839 off
>= s
.br_startoff
+ s
.br_blockcount
;
5844 * Check if the endoff is outside the last extent. If so the caller will grow
5845 * the allocation to a stripe unit boundary.
5849 xfs_inode_t
*ip
, /* incore inode pointer */
5850 xfs_fileoff_t endoff
, /* file offset in fsblocks */
5851 int whichfork
, /* data or attribute fork */
5852 int *eof
) /* result value */
5854 xfs_fsblock_t blockcount
; /* extent block count */
5855 int error
; /* error return value */
5856 xfs_ifork_t
*ifp
; /* inode fork pointer */
5857 xfs_bmbt_rec_t
*lastrec
; /* extent list entry pointer */
5858 xfs_extnum_t nextents
; /* size of extent list */
5859 xfs_fileoff_t startoff
; /* extent starting file offset */
5861 ASSERT(whichfork
== XFS_DATA_FORK
);
5862 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5863 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
5864 (error
= xfs_iread_extents(NULL
, ip
, whichfork
)))
5866 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5867 if (nextents
== 0) {
5872 * Go to the last extent
5874 lastrec
= &ifp
->if_u1
.if_extents
[nextents
- 1];
5875 startoff
= xfs_bmbt_get_startoff(lastrec
);
5876 blockcount
= xfs_bmbt_get_blockcount(lastrec
);
5877 *eof
= endoff
>= startoff
+ blockcount
;
5883 * Check that the extents list for the inode ip is in the right order.
5886 xfs_bmap_check_extents(
5887 xfs_inode_t
*ip
, /* incore inode pointer */
5888 int whichfork
) /* data or attr fork */
5890 xfs_bmbt_rec_t
*base
; /* base of extents list */
5891 xfs_bmbt_rec_t
*ep
; /* current extent entry */
5892 xfs_ifork_t
*ifp
; /* inode fork pointer */
5893 xfs_extnum_t nextents
; /* number of extents in list */
5895 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
5896 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
5897 base
= ifp
->if_u1
.if_extents
;
5898 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
5899 for (ep
= base
; ep
< &base
[nextents
- 1]; ep
++) {
5900 xfs_btree_check_rec(XFS_BTNUM_BMAP
, (void *)ep
,
5908 xfs_btree_cur_t
*cur
,
5918 for(i
= 0; i
< XFS_BTREE_MAXLEVELS
; i
++) {
5919 bp
= cur
->bc_bufs
[i
];
5921 if (XFS_BUF_ADDR(bp
) == bno
)
5922 break; /* Found it */
5924 if (i
== XFS_BTREE_MAXLEVELS
)
5927 if (!bp
) { /* Chase down all the log items to see if the bp is there */
5928 xfs_log_item_chunk_t
*licp
;
5932 licp
= &tp
->t_items
;
5933 while (!bp
&& licp
!= NULL
) {
5934 if (XFS_LIC_ARE_ALL_FREE(licp
)) {
5935 licp
= licp
->lic_next
;
5938 for (i
= 0; i
< licp
->lic_unused
; i
++) {
5939 xfs_log_item_desc_t
*lidp
;
5940 xfs_log_item_t
*lip
;
5941 xfs_buf_log_item_t
*bip
;
5944 if (XFS_LIC_ISFREE(licp
, i
)) {
5948 lidp
= XFS_LIC_SLOT(licp
, i
);
5949 lip
= lidp
->lid_item
;
5950 if (lip
->li_type
!= XFS_LI_BUF
)
5953 bip
= (xfs_buf_log_item_t
*)lip
;
5956 if (XFS_BUF_ADDR(lbp
) == bno
) {
5958 break; /* Found it */
5961 licp
= licp
->lic_next
;
5969 xfs_bmbt_block_t
*block
,
5975 xfs_bmbt_ptr_t
*pp
, *thispa
; /* pointer to block address */
5976 xfs_bmbt_key_t
*prevp
, *keyp
;
5978 ASSERT(INT_GET(block
->bb_level
, ARCH_CONVERT
) > 0);
5981 for( i
= 1; i
<= INT_GET(block
->bb_numrecs
, ARCH_CONVERT
);i
++) {
5982 dmxr
= mp
->m_bmap_dmxr
[0];
5985 keyp
= XFS_BMAP_BROOT_KEY_ADDR(block
, i
, sz
);
5987 keyp
= XFS_BTREE_KEY_ADDR(mp
->m_sb
.sb_blocksize
,
5988 xfs_bmbt
, block
, i
, dmxr
);
5992 xfs_btree_check_key(XFS_BTNUM_BMAP
, prevp
, keyp
);
5997 * Compare the block numbers to see if there are dups.
6001 pp
= XFS_BMAP_BROOT_PTR_ADDR(block
, i
, sz
);
6003 pp
= XFS_BTREE_PTR_ADDR(mp
->m_sb
.sb_blocksize
,
6004 xfs_bmbt
, block
, i
, dmxr
);
6006 for (j
= i
+1; j
<= INT_GET(block
->bb_numrecs
, ARCH_CONVERT
); j
++) {
6008 thispa
= XFS_BMAP_BROOT_PTR_ADDR(block
, j
, sz
);
6010 thispa
= XFS_BTREE_PTR_ADDR(mp
->m_sb
.sb_blocksize
,
6011 xfs_bmbt
, block
, j
, dmxr
);
6013 if (INT_GET(*thispa
, ARCH_CONVERT
) ==
6014 INT_GET(*pp
, ARCH_CONVERT
)) {
6015 cmn_err(CE_WARN
, "%s: thispa(%d) == pp(%d) %Ld",
6017 INT_GET(*thispa
, ARCH_CONVERT
));
6018 panic("%s: ptrs are equal in node\n",
6026 * Check that the extents for the inode ip are in the right order in all
6031 xfs_bmap_check_leaf_extents(
6032 xfs_btree_cur_t
*cur
, /* btree cursor or null */
6033 xfs_inode_t
*ip
, /* incore inode pointer */
6034 int whichfork
) /* data or attr fork */
6036 xfs_bmbt_block_t
*block
; /* current btree block */
6037 xfs_fsblock_t bno
; /* block # of "block" */
6038 xfs_buf_t
*bp
; /* buffer for "block" */
6039 int error
; /* error return value */
6040 xfs_extnum_t i
=0; /* index into the extents list */
6041 xfs_ifork_t
*ifp
; /* fork structure */
6042 int level
; /* btree level, for checking */
6043 xfs_mount_t
*mp
; /* file system mount structure */
6044 xfs_bmbt_ptr_t
*pp
; /* pointer to block address */
6045 xfs_bmbt_rec_t
*ep
, *lastp
; /* extent pointers in block entry */
6048 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
) {
6054 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
6055 block
= ifp
->if_broot
;
6057 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6059 ASSERT(INT_GET(block
->bb_level
, ARCH_CONVERT
) > 0);
6060 level
= INT_GET(block
->bb_level
, ARCH_CONVERT
);
6061 xfs_check_block(block
, mp
, 1, ifp
->if_broot_bytes
);
6062 pp
= XFS_BMAP_BROOT_PTR_ADDR(block
, 1, ifp
->if_broot_bytes
);
6063 ASSERT(INT_GET(*pp
, ARCH_CONVERT
) != NULLDFSBNO
);
6064 ASSERT(XFS_FSB_TO_AGNO(mp
, INT_GET(*pp
, ARCH_CONVERT
)) < mp
->m_sb
.sb_agcount
);
6065 ASSERT(XFS_FSB_TO_AGBNO(mp
, INT_GET(*pp
, ARCH_CONVERT
)) < mp
->m_sb
.sb_agblocks
);
6066 bno
= INT_GET(*pp
, ARCH_CONVERT
);
6068 * Go down the tree until leaf level is reached, following the first
6069 * pointer (leftmost) at each level.
6071 while (level
-- > 0) {
6072 /* See if buf is in cur first */
6073 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
6079 if (!bp
&& (error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
6080 XFS_BMAP_BTREE_REF
)))
6082 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
6083 XFS_WANT_CORRUPTED_GOTO(
6084 XFS_BMAP_SANITY_CHECK(mp
, block
, level
),
6090 * Check this block for basic sanity (increasing keys and
6091 * no duplicate blocks).
6094 xfs_check_block(block
, mp
, 0, 0);
6095 pp
= XFS_BTREE_PTR_ADDR(mp
->m_sb
.sb_blocksize
, xfs_bmbt
, block
,
6096 1, mp
->m_bmap_dmxr
[1]);
6097 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp
, INT_GET(*pp
, ARCH_CONVERT
)), error0
);
6098 bno
= INT_GET(*pp
, ARCH_CONVERT
);
6101 xfs_trans_brelse(NULL
, bp
);
6106 * Here with bp and block set to the leftmost leaf node in the tree.
6111 * Loop over all leaf nodes checking that all extents are in the right order.
6115 xfs_bmbt_rec_t
*frp
;
6116 xfs_fsblock_t nextbno
;
6117 xfs_extnum_t num_recs
;
6120 num_recs
= INT_GET(block
->bb_numrecs
, ARCH_CONVERT
);
6123 * Read-ahead the next leaf block, if any.
6126 nextbno
= INT_GET(block
->bb_rightsib
, ARCH_CONVERT
);
6129 * Check all the extents to make sure they are OK.
6130 * If we had a previous block, the last entry should
6131 * conform with the first entry in this one.
6134 frp
= XFS_BTREE_REC_ADDR(mp
->m_sb
.sb_blocksize
, xfs_bmbt
,
6135 block
, 1, mp
->m_bmap_dmxr
[0]);
6137 for (ep
= frp
;ep
< frp
+ (num_recs
- 1); ep
++) {
6139 xfs_btree_check_rec(XFS_BTNUM_BMAP
,
6140 (void *)lastp
, (void *)ep
);
6142 xfs_btree_check_rec(XFS_BTNUM_BMAP
, (void *)ep
,
6145 lastp
= frp
+ num_recs
- 1; /* For the next iteration */
6150 xfs_trans_brelse(NULL
, bp
);
6154 * If we've reached the end, stop.
6156 if (bno
== NULLFSBLOCK
)
6159 bp
= xfs_bmap_get_bp(cur
, XFS_FSB_TO_DADDR(mp
, bno
));
6165 if (!bp
&& (error
= xfs_btree_read_bufl(mp
, NULL
, bno
, 0, &bp
,
6166 XFS_BMAP_BTREE_REF
)))
6168 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
6172 xfs_trans_brelse(NULL
, bp
);
6177 cmn_err(CE_WARN
, "%s: at error0", __FUNCTION__
);
6179 xfs_trans_brelse(NULL
, bp
);
6181 cmn_err(CE_WARN
, "%s: BAD after btree leaves for %d extents",
6183 panic("%s: CORRUPTED BTREE OR SOMETHING", __FUNCTION__
);
6189 * Count fsblocks of the given fork.
6192 xfs_bmap_count_blocks(
6193 xfs_trans_t
*tp
, /* transaction pointer */
6194 xfs_inode_t
*ip
, /* incore inode */
6195 int whichfork
, /* data or attr fork */
6196 int *count
) /* out: count of blocks */
6198 xfs_bmbt_block_t
*block
; /* current btree block */
6199 xfs_fsblock_t bno
; /* block # of "block" */
6200 xfs_ifork_t
*ifp
; /* fork structure */
6201 int level
; /* btree level, for checking */
6202 xfs_mount_t
*mp
; /* file system mount structure */
6203 xfs_bmbt_ptr_t
*pp
; /* pointer to block address */
6207 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
6208 if ( XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_EXTENTS
) {
6209 if (unlikely(xfs_bmap_count_leaves(ifp
->if_u1
.if_extents
,
6210 ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
),
6212 XFS_ERROR_REPORT("xfs_bmap_count_blocks(1)",
6213 XFS_ERRLEVEL_LOW
, mp
);
6214 return XFS_ERROR(EFSCORRUPTED
);
6220 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6222 block
= ifp
->if_broot
;
6223 ASSERT(INT_GET(block
->bb_level
, ARCH_CONVERT
) > 0);
6224 level
= INT_GET(block
->bb_level
, ARCH_CONVERT
);
6225 pp
= XFS_BMAP_BROOT_PTR_ADDR(block
, 1, ifp
->if_broot_bytes
);
6226 ASSERT(INT_GET(*pp
, ARCH_CONVERT
) != NULLDFSBNO
);
6227 ASSERT(XFS_FSB_TO_AGNO(mp
, INT_GET(*pp
, ARCH_CONVERT
)) < mp
->m_sb
.sb_agcount
);
6228 ASSERT(XFS_FSB_TO_AGBNO(mp
, INT_GET(*pp
, ARCH_CONVERT
)) < mp
->m_sb
.sb_agblocks
);
6229 bno
= INT_GET(*pp
, ARCH_CONVERT
);
6231 if (unlikely(xfs_bmap_count_tree(mp
, tp
, bno
, level
, count
) < 0)) {
6232 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW
,
6234 return XFS_ERROR(EFSCORRUPTED
);
6241 * Recursively walks each level of a btree
6242 * to count total fsblocks is use.
6245 xfs_bmap_count_tree(
6246 xfs_mount_t
*mp
, /* file system mount point */
6247 xfs_trans_t
*tp
, /* transaction pointer */
6248 xfs_fsblock_t blockno
, /* file system block number */
6249 int levelin
, /* level in btree */
6250 int *count
) /* Count of blocks */
6253 xfs_buf_t
*bp
, *nbp
;
6254 int level
= levelin
;
6256 xfs_fsblock_t bno
= blockno
;
6257 xfs_fsblock_t nextbno
;
6258 xfs_bmbt_block_t
*block
, *nextblock
;
6260 xfs_bmbt_rec_t
*frp
;
6262 if ((error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
, XFS_BMAP_BTREE_REF
)))
6265 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
6268 /* Not at node above leafs, count this level of nodes */
6269 nextbno
= INT_GET(block
->bb_rightsib
, ARCH_CONVERT
);
6270 while (nextbno
!= NULLFSBLOCK
) {
6271 if ((error
= xfs_btree_read_bufl(mp
, tp
, nextbno
,
6272 0, &nbp
, XFS_BMAP_BTREE_REF
)))
6275 nextblock
= XFS_BUF_TO_BMBT_BLOCK(nbp
);
6276 nextbno
= INT_GET(nextblock
->bb_rightsib
, ARCH_CONVERT
);
6277 xfs_trans_brelse(tp
, nbp
);
6280 /* Dive to the next level */
6281 pp
= XFS_BTREE_PTR_ADDR(mp
->m_sb
.sb_blocksize
,
6282 xfs_bmbt
, block
, 1, mp
->m_bmap_dmxr
[1]);
6283 bno
= INT_GET(*pp
, ARCH_CONVERT
);
6284 if (unlikely((error
=
6285 xfs_bmap_count_tree(mp
, tp
, bno
, level
, count
)) < 0)) {
6286 xfs_trans_brelse(tp
, bp
);
6287 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6288 XFS_ERRLEVEL_LOW
, mp
);
6289 return XFS_ERROR(EFSCORRUPTED
);
6291 xfs_trans_brelse(tp
, bp
);
6293 /* count all level 1 nodes and their leaves */
6295 nextbno
= INT_GET(block
->bb_rightsib
, ARCH_CONVERT
);
6296 numrecs
= INT_GET(block
->bb_numrecs
, ARCH_CONVERT
);
6297 frp
= XFS_BTREE_REC_ADDR(mp
->m_sb
.sb_blocksize
,
6298 xfs_bmbt
, block
, 1, mp
->m_bmap_dmxr
[0]);
6299 if (unlikely(xfs_bmap_count_leaves(frp
, numrecs
, count
) < 0)) {
6300 xfs_trans_brelse(tp
, bp
);
6301 XFS_ERROR_REPORT("xfs_bmap_count_tree(2)",
6302 XFS_ERRLEVEL_LOW
, mp
);
6303 return XFS_ERROR(EFSCORRUPTED
);
6305 xfs_trans_brelse(tp
, bp
);
6306 if (nextbno
== NULLFSBLOCK
)
6309 if ((error
= xfs_btree_read_bufl(mp
, tp
, bno
, 0, &bp
,
6310 XFS_BMAP_BTREE_REF
)))
6313 block
= XFS_BUF_TO_BMBT_BLOCK(bp
);
6320 * Count leaf blocks given a pointer to an extent list.
6323 xfs_bmap_count_leaves(
6324 xfs_bmbt_rec_t
*frp
,
6330 for ( b
= 1; b
<= numrecs
; b
++, frp
++)
6331 *count
+= xfs_bmbt_disk_get_blockcount(frp
);