2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_alloc_btree.h"
31 #include "xfs_ialloc_btree.h"
32 #include "xfs_dir2_sf.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_dinode.h"
35 #include "xfs_inode.h"
36 #include "xfs_btree.h"
37 #include "xfs_dmapi.h"
38 #include "xfs_mount.h"
39 #include "xfs_ialloc.h"
40 #include "xfs_itable.h"
41 #include "xfs_dir2_data.h"
42 #include "xfs_dir2_leaf.h"
43 #include "xfs_dir2_block.h"
44 #include "xfs_inode_item.h"
45 #include "xfs_extfree_item.h"
46 #include "xfs_alloc.h"
48 #include "xfs_rtalloc.h"
49 #include "xfs_error.h"
50 #include "xfs_attr_leaf.h"
52 #include "xfs_quota.h"
53 #include "xfs_trans_space.h"
54 #include "xfs_buf_item.h"
55 #include "xfs_filestream.h"
56 #include "xfs_vnodeops.h"
61 xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
64 kmem_zone_t *xfs_bmap_free_item_zone;
67 * Prototypes for internal bmap routines.
72 * Called from xfs_bmap_add_attrfork to handle extents format files.
74 STATIC int /* error */
75 xfs_bmap_add_attrfork_extents(
76 xfs_trans_t *tp, /* transaction pointer */
77 xfs_inode_t *ip, /* incore inode pointer */
78 xfs_fsblock_t *firstblock, /* first block allocated */
79 xfs_bmap_free_t *flist, /* blocks to free at commit */
80 int *flags); /* inode logging flags */
83 * Called from xfs_bmap_add_attrfork to handle local format files.
85 STATIC int /* error */
86 xfs_bmap_add_attrfork_local(
87 xfs_trans_t *tp, /* transaction pointer */
88 xfs_inode_t *ip, /* incore inode pointer */
89 xfs_fsblock_t *firstblock, /* first block allocated */
90 xfs_bmap_free_t *flist, /* blocks to free at commit */
91 int *flags); /* inode logging flags */
94 * Called by xfs_bmapi to update file extent records and the btree
95 * after allocating space (or doing a delayed allocation).
97 STATIC int /* error */
99 xfs_inode_t *ip, /* incore inode pointer */
100 xfs_extnum_t idx, /* extent number to update/insert */
101 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
102 xfs_bmbt_irec_t *new, /* new data to add to file extents */
103 xfs_fsblock_t *first, /* pointer to firstblock variable */
104 xfs_bmap_free_t *flist, /* list of extents to be freed */
105 int *logflagsp, /* inode logging flags */
106 xfs_extdelta_t *delta, /* Change made to incore extents */
107 int whichfork, /* data or attr fork */
108 int rsvd); /* OK to allocate reserved blocks */
111 * Called by xfs_bmap_add_extent to handle cases converting a delayed
112 * allocation to a real allocation.
114 STATIC int /* error */
115 xfs_bmap_add_extent_delay_real(
116 xfs_inode_t *ip, /* incore inode pointer */
117 xfs_extnum_t idx, /* extent number to update/insert */
118 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
119 xfs_bmbt_irec_t *new, /* new data to add to file extents */
120 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
121 xfs_fsblock_t *first, /* pointer to firstblock variable */
122 xfs_bmap_free_t *flist, /* list of extents to be freed */
123 int *logflagsp, /* inode logging flags */
124 xfs_extdelta_t *delta, /* Change made to incore extents */
125 int rsvd); /* OK to allocate reserved blocks */
128 * Called by xfs_bmap_add_extent to handle cases converting a hole
129 * to a delayed allocation.
131 STATIC int /* error */
132 xfs_bmap_add_extent_hole_delay(
133 xfs_inode_t *ip, /* incore inode pointer */
134 xfs_extnum_t idx, /* extent number to update/insert */
135 xfs_bmbt_irec_t *new, /* new data to add to file extents */
136 int *logflagsp,/* inode logging flags */
137 xfs_extdelta_t *delta, /* Change made to incore extents */
138 int rsvd); /* OK to allocate reserved blocks */
141 * Called by xfs_bmap_add_extent to handle cases converting a hole
142 * to a real allocation.
144 STATIC int /* error */
145 xfs_bmap_add_extent_hole_real(
146 xfs_inode_t *ip, /* incore inode pointer */
147 xfs_extnum_t idx, /* extent number to update/insert */
148 xfs_btree_cur_t *cur, /* if null, not a btree */
149 xfs_bmbt_irec_t *new, /* new data to add to file extents */
150 int *logflagsp, /* inode logging flags */
151 xfs_extdelta_t *delta, /* Change made to incore extents */
152 int whichfork); /* data or attr fork */
155 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
156 * allocation to a real allocation or vice versa.
158 STATIC int /* error */
159 xfs_bmap_add_extent_unwritten_real(
160 xfs_inode_t *ip, /* incore inode pointer */
161 xfs_extnum_t idx, /* extent number to update/insert */
162 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
163 xfs_bmbt_irec_t *new, /* new data to add to file extents */
164 int *logflagsp, /* inode logging flags */
165 xfs_extdelta_t *delta); /* Change made to incore extents */
168 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
169 * It figures out where to ask the underlying allocator to put the new extent.
171 STATIC int /* error */
173 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
176 * Transform a btree format file with only one leaf node, where the
177 * extents list will fit in the inode, into an extents format file.
178 * Since the file extents are already in-core, all we have to do is
179 * give up the space for the btree root and pitch the leaf block.
181 STATIC int /* error */
182 xfs_bmap_btree_to_extents(
183 xfs_trans_t *tp, /* transaction pointer */
184 xfs_inode_t *ip, /* incore inode pointer */
185 xfs_btree_cur_t *cur, /* btree cursor */
186 int *logflagsp, /* inode logging flags */
187 int whichfork); /* data or attr fork */
190 * Called by xfs_bmapi to update file extent records and the btree
191 * after removing space (or undoing a delayed allocation).
193 STATIC int /* error */
195 xfs_inode_t *ip, /* incore inode pointer */
196 xfs_trans_t *tp, /* current trans pointer */
197 xfs_extnum_t idx, /* extent number to update/insert */
198 xfs_bmap_free_t *flist, /* list of extents to be freed */
199 xfs_btree_cur_t *cur, /* if null, not a btree */
200 xfs_bmbt_irec_t *new, /* new data to add to file extents */
201 int *logflagsp,/* inode logging flags */
202 xfs_extdelta_t *delta, /* Change made to incore extents */
203 int whichfork, /* data or attr fork */
204 int rsvd); /* OK to allocate reserved blocks */
207 * Remove the entry "free" from the free item list. Prev points to the
208 * previous entry, unless "free" is the head of the list.
212 xfs_bmap_free_t *flist, /* free item list header */
213 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
214 xfs_bmap_free_item_t *free); /* list item to be freed */
217 * Convert an extents-format file into a btree-format file.
218 * The new file will have a root block (in the inode) and a single child block.
220 STATIC int /* error */
221 xfs_bmap_extents_to_btree(
222 xfs_trans_t *tp, /* transaction pointer */
223 xfs_inode_t *ip, /* incore inode pointer */
224 xfs_fsblock_t *firstblock, /* first-block-allocated */
225 xfs_bmap_free_t *flist, /* blocks freed in xaction */
226 xfs_btree_cur_t **curp, /* cursor returned to caller */
227 int wasdel, /* converting a delayed alloc */
228 int *logflagsp, /* inode logging flags */
229 int whichfork); /* data or attr fork */
232 * Convert a local file to an extents file.
233 * This code is sort of bogus, since the file data needs to get
234 * logged so it won't be lost. The bmap-level manipulations are ok, though.
236 STATIC int /* error */
237 xfs_bmap_local_to_extents(
238 xfs_trans_t *tp, /* transaction pointer */
239 xfs_inode_t *ip, /* incore inode pointer */
240 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
241 xfs_extlen_t total, /* total blocks needed by transaction */
242 int *logflagsp, /* inode logging flags */
243 int whichfork); /* data or attr fork */
246 * Search the extents list for the inode, for the extent containing bno.
247 * If bno lies in a hole, point to the next entry. If bno lies past eof,
248 * *eofp will be set, and *prevp will contain the last entry (null if none).
249 * Else, *lastxp will be set to the index of the found
250 * entry; *gotp will contain the entry.
252 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
253 xfs_bmap_search_extents(
254 xfs_inode_t *ip, /* incore inode pointer */
255 xfs_fileoff_t bno, /* block number searched for */
256 int whichfork, /* data or attr fork */
257 int *eofp, /* out: end of file found */
258 xfs_extnum_t *lastxp, /* out: last extent index */
259 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
260 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
263 * Check the last inode extent to determine whether this allocation will result
264 * in blocks being allocated at the end of the file. When we allocate new data
265 * blocks at the end of the file which do not start at the previous data block,
266 * we will try to align the new blocks at stripe unit boundaries.
268 STATIC int /* error */
270 xfs_inode_t *ip, /* incore inode pointer */
271 xfs_fileoff_t off, /* file offset in fsblocks */
272 int whichfork, /* data or attribute fork */
273 char *aeof); /* return value */
275 #ifdef XFS_BMAP_TRACE
277 * Add bmap trace entry prior to a call to xfs_iext_remove.
280 xfs_bmap_trace_delete(
281 const char *fname, /* function name */
282 char *desc, /* operation description */
283 xfs_inode_t *ip, /* incore inode pointer */
284 xfs_extnum_t idx, /* index of entry(entries) deleted */
285 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
286 int whichfork); /* data or attr fork */
289 * Add bmap trace entry prior to a call to xfs_iext_insert, or
290 * reading in the extents list from the disk (in the btree).
293 xfs_bmap_trace_insert(
294 const char *fname, /* function name */
295 char *desc, /* operation description */
296 xfs_inode_t *ip, /* incore inode pointer */
297 xfs_extnum_t idx, /* index of entry(entries) inserted */
298 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
299 xfs_bmbt_irec_t *r1, /* inserted record 1 */
300 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
301 int whichfork); /* data or attr fork */
304 * Add bmap trace entry after updating an extent record in place.
307 xfs_bmap_trace_post_update(
308 const char *fname, /* function name */
309 char *desc, /* operation description */
310 xfs_inode_t *ip, /* incore inode pointer */
311 xfs_extnum_t idx, /* index of entry updated */
312 int whichfork); /* data or attr fork */
315 * Add bmap trace entry prior to updating an extent record in place.
318 xfs_bmap_trace_pre_update(
319 const char *fname, /* function name */
320 char *desc, /* operation description */
321 xfs_inode_t *ip, /* incore inode pointer */
322 xfs_extnum_t idx, /* index of entry to be updated */
323 int whichfork); /* data or attr fork */
325 #define XFS_BMAP_TRACE_DELETE(d,ip,i,c,w) \
326 xfs_bmap_trace_delete(__func__,d,ip,i,c,w)
327 #define XFS_BMAP_TRACE_INSERT(d,ip,i,c,r1,r2,w) \
328 xfs_bmap_trace_insert(__func__,d,ip,i,c,r1,r2,w)
329 #define XFS_BMAP_TRACE_POST_UPDATE(d,ip,i,w) \
330 xfs_bmap_trace_post_update(__func__,d,ip,i,w)
331 #define XFS_BMAP_TRACE_PRE_UPDATE(d,ip,i,w) \
332 xfs_bmap_trace_pre_update(__func__,d,ip,i,w)
334 #define XFS_BMAP_TRACE_DELETE(d,ip,i,c,w)
335 #define XFS_BMAP_TRACE_INSERT(d,ip,i,c,r1,r2,w)
336 #define XFS_BMAP_TRACE_POST_UPDATE(d,ip,i,w)
337 #define XFS_BMAP_TRACE_PRE_UPDATE(d,ip,i,w)
338 #endif /* XFS_BMAP_TRACE */
341 * Compute the worst-case number of indirect blocks that will be used
342 * for ip's delayed extent of length "len".
345 xfs_bmap_worst_indlen(
346 xfs_inode_t *ip, /* incore inode pointer */
347 xfs_filblks_t len); /* delayed extent length */
351 * Perform various validation checks on the values being returned
355 xfs_bmap_validate_ret(
359 xfs_bmbt_irec_t *mval,
363 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
366 #if defined(XFS_RW_TRACE)
375 #define xfs_bunmap_trace(ip, bno, len, flags, ra)
376 #endif /* XFS_RW_TRACE */
383 xfs_fsblock_t blockno,
388 xfs_bmap_count_leaves(
395 xfs_bmap_disk_count_leaves(
397 xfs_bmbt_block_t *block,
402 * Bmap internal routines.
406 * Called from xfs_bmap_add_attrfork to handle btree format files.
408 STATIC int /* error */
409 xfs_bmap_add_attrfork_btree(
410 xfs_trans_t *tp, /* transaction pointer */
411 xfs_inode_t *ip, /* incore inode pointer */
412 xfs_fsblock_t *firstblock, /* first block allocated */
413 xfs_bmap_free_t *flist, /* blocks to free at commit */
414 int *flags) /* inode logging flags */
416 xfs_btree_cur_t *cur; /* btree cursor */
417 int error; /* error return value */
418 xfs_mount_t *mp; /* file system mount struct */
419 int stat; /* newroot status */
422 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
423 *flags |= XFS_ILOG_DBROOT;
425 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
426 cur->bc_private.b.flist = flist;
427 cur->bc_private.b.firstblock = *firstblock;
428 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
430 /* must be at least one entry */
431 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
432 if ((error = xfs_bmbt_newroot(cur, flags, &stat)))
435 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
436 return XFS_ERROR(ENOSPC);
438 *firstblock = cur->bc_private.b.firstblock;
439 cur->bc_private.b.allocated = 0;
440 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
444 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
449 * Called from xfs_bmap_add_attrfork to handle extents format files.
451 STATIC int /* error */
452 xfs_bmap_add_attrfork_extents(
453 xfs_trans_t *tp, /* transaction pointer */
454 xfs_inode_t *ip, /* incore inode pointer */
455 xfs_fsblock_t *firstblock, /* first block allocated */
456 xfs_bmap_free_t *flist, /* blocks to free at commit */
457 int *flags) /* inode logging flags */
459 xfs_btree_cur_t *cur; /* bmap btree cursor */
460 int error; /* error return value */
462 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
465 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
466 flags, XFS_DATA_FORK);
468 cur->bc_private.b.allocated = 0;
469 xfs_btree_del_cursor(cur,
470 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
476 * Called from xfs_bmap_add_attrfork to handle local format files.
478 STATIC int /* error */
479 xfs_bmap_add_attrfork_local(
480 xfs_trans_t *tp, /* transaction pointer */
481 xfs_inode_t *ip, /* incore inode pointer */
482 xfs_fsblock_t *firstblock, /* first block allocated */
483 xfs_bmap_free_t *flist, /* blocks to free at commit */
484 int *flags) /* inode logging flags */
486 xfs_da_args_t dargs; /* args for dir/attr code */
487 int error; /* error return value */
488 xfs_mount_t *mp; /* mount structure pointer */
490 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
492 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
494 memset(&dargs, 0, sizeof(dargs));
496 dargs.firstblock = firstblock;
498 dargs.total = mp->m_dirblkfsbs;
499 dargs.whichfork = XFS_DATA_FORK;
501 error = xfs_dir2_sf_to_block(&dargs);
503 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
509 * Called by xfs_bmapi to update file extent records and the btree
510 * after allocating space (or doing a delayed allocation).
512 STATIC int /* error */
514 xfs_inode_t *ip, /* incore inode pointer */
515 xfs_extnum_t idx, /* extent number to update/insert */
516 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
517 xfs_bmbt_irec_t *new, /* new data to add to file extents */
518 xfs_fsblock_t *first, /* pointer to firstblock variable */
519 xfs_bmap_free_t *flist, /* list of extents to be freed */
520 int *logflagsp, /* inode logging flags */
521 xfs_extdelta_t *delta, /* Change made to incore extents */
522 int whichfork, /* data or attr fork */
523 int rsvd) /* OK to use reserved data blocks */
525 xfs_btree_cur_t *cur; /* btree cursor or null */
526 xfs_filblks_t da_new; /* new count del alloc blocks used */
527 xfs_filblks_t da_old; /* old count del alloc blocks used */
528 int error; /* error return value */
529 xfs_ifork_t *ifp; /* inode fork ptr */
530 int logflags; /* returned value */
531 xfs_extnum_t nextents; /* number of extents in file now */
533 XFS_STATS_INC(xs_add_exlist);
535 ifp = XFS_IFORK_PTR(ip, whichfork);
536 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
537 ASSERT(idx <= nextents);
541 * This is the first extent added to a new/empty file.
542 * Special case this one, so other routines get to assume there are
543 * already extents in the list.
546 XFS_BMAP_TRACE_INSERT("insert empty", ip, 0, 1, new, NULL,
548 xfs_iext_insert(ifp, 0, 1, new);
551 if (!ISNULLSTARTBLOCK(new->br_startblock)) {
552 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
553 logflags = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
556 /* DELTA: single new extent */
558 if (delta->xed_startoff > new->br_startoff)
559 delta->xed_startoff = new->br_startoff;
560 if (delta->xed_blockcount <
561 new->br_startoff + new->br_blockcount)
562 delta->xed_blockcount = new->br_startoff +
567 * Any kind of new delayed allocation goes here.
569 else if (ISNULLSTARTBLOCK(new->br_startblock)) {
571 ASSERT((cur->bc_private.b.flags &
572 XFS_BTCUR_BPRV_WASDEL) == 0);
573 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
574 &logflags, delta, rsvd)))
578 * Real allocation off the end of the file.
580 else if (idx == nextents) {
582 ASSERT((cur->bc_private.b.flags &
583 XFS_BTCUR_BPRV_WASDEL) == 0);
584 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
585 &logflags, delta, whichfork)))
588 xfs_bmbt_irec_t prev; /* old extent at offset idx */
591 * Get the record referred to by idx.
593 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
595 * If it's a real allocation record, and the new allocation ends
596 * after the start of the referred to record, then we're filling
597 * in a delayed or unwritten allocation with a real one, or
598 * converting real back to unwritten.
600 if (!ISNULLSTARTBLOCK(new->br_startblock) &&
601 new->br_startoff + new->br_blockcount > prev.br_startoff) {
602 if (prev.br_state != XFS_EXT_UNWRITTEN &&
603 ISNULLSTARTBLOCK(prev.br_startblock)) {
604 da_old = STARTBLOCKVAL(prev.br_startblock);
606 ASSERT(cur->bc_private.b.flags &
607 XFS_BTCUR_BPRV_WASDEL);
608 if ((error = xfs_bmap_add_extent_delay_real(ip,
609 idx, &cur, new, &da_new, first, flist,
610 &logflags, delta, rsvd)))
612 } else if (new->br_state == XFS_EXT_NORM) {
613 ASSERT(new->br_state == XFS_EXT_NORM);
614 if ((error = xfs_bmap_add_extent_unwritten_real(
615 ip, idx, &cur, new, &logflags, delta)))
618 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
619 if ((error = xfs_bmap_add_extent_unwritten_real(
620 ip, idx, &cur, new, &logflags, delta)))
623 ASSERT(*curp == cur || *curp == NULL);
626 * Otherwise we're filling in a hole with an allocation.
630 ASSERT((cur->bc_private.b.flags &
631 XFS_BTCUR_BPRV_WASDEL) == 0);
632 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
633 new, &logflags, delta, whichfork)))
638 ASSERT(*curp == cur || *curp == NULL);
640 * Convert to a btree if necessary.
642 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
643 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
644 int tmp_logflags; /* partial log flag return val */
647 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
648 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
649 logflags |= tmp_logflags;
654 * Adjust for changes in reserved delayed indirect blocks.
655 * Nothing to do for disk quotas here.
657 if (da_old || da_new) {
662 nblks += cur->bc_private.b.allocated;
663 ASSERT(nblks <= da_old);
665 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
666 (int64_t)(da_old - nblks), rsvd);
669 * Clear out the allocated field, done with it now in any case.
672 cur->bc_private.b.allocated = 0;
678 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
680 *logflagsp = logflags;
685 * Called by xfs_bmap_add_extent to handle cases converting a delayed
686 * allocation to a real allocation.
688 STATIC int /* error */
689 xfs_bmap_add_extent_delay_real(
690 xfs_inode_t *ip, /* incore inode pointer */
691 xfs_extnum_t idx, /* extent number to update/insert */
692 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
693 xfs_bmbt_irec_t *new, /* new data to add to file extents */
694 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
695 xfs_fsblock_t *first, /* pointer to firstblock variable */
696 xfs_bmap_free_t *flist, /* list of extents to be freed */
697 int *logflagsp, /* inode logging flags */
698 xfs_extdelta_t *delta, /* Change made to incore extents */
699 int rsvd) /* OK to use reserved data block allocation */
701 xfs_btree_cur_t *cur; /* btree cursor */
702 int diff; /* temp value */
703 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
704 int error; /* error return value */
705 int i; /* temp state */
706 xfs_ifork_t *ifp; /* inode fork pointer */
707 xfs_fileoff_t new_endoff; /* end offset of new entry */
708 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
709 /* left is 0, right is 1, prev is 2 */
710 int rval=0; /* return value (logging flags) */
711 int state = 0;/* state bits, accessed thru macros */
712 xfs_filblks_t temp=0; /* value for dnew calculations */
713 xfs_filblks_t temp2=0;/* value for dnew calculations */
714 int tmp_rval; /* partial logging flags */
715 enum { /* bit number definitions for state */
716 LEFT_CONTIG, RIGHT_CONTIG,
717 LEFT_FILLING, RIGHT_FILLING,
718 LEFT_DELAY, RIGHT_DELAY,
719 LEFT_VALID, RIGHT_VALID
725 #define MASK(b) (1 << (b))
726 #define MASK2(a,b) (MASK(a) | MASK(b))
727 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
728 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
729 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
730 #define STATE_TEST(b) (state & MASK(b))
731 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
732 ((state &= ~MASK(b)), 0))
733 #define SWITCH_STATE \
734 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
737 * Set up a bunch of variables to make the tests simpler.
740 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
741 ep = xfs_iext_get_ext(ifp, idx);
742 xfs_bmbt_get_all(ep, &PREV);
743 new_endoff = new->br_startoff + new->br_blockcount;
744 ASSERT(PREV.br_startoff <= new->br_startoff);
745 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
747 * Set flags determining what part of the previous delayed allocation
748 * extent is being replaced by a real allocation.
750 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
751 STATE_SET(RIGHT_FILLING,
752 PREV.br_startoff + PREV.br_blockcount == new_endoff);
754 * Check and set flags if this segment has a left neighbor.
755 * Don't set contiguous if the combined extent would be too large.
757 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
758 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
759 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
761 STATE_SET(LEFT_CONTIG,
762 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
763 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
764 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
765 LEFT.br_state == new->br_state &&
766 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
768 * Check and set flags if this segment has a right neighbor.
769 * Don't set contiguous if the combined extent would be too large.
770 * Also check for all-three-contiguous being too large.
772 if (STATE_SET_TEST(RIGHT_VALID,
774 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
775 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
776 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
778 STATE_SET(RIGHT_CONTIG,
779 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
780 new_endoff == RIGHT.br_startoff &&
781 new->br_startblock + new->br_blockcount ==
782 RIGHT.br_startblock &&
783 new->br_state == RIGHT.br_state &&
784 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
785 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
786 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
787 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
791 * Switch out based on the FILLING and CONTIG state bits.
793 switch (SWITCH_STATE) {
795 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
797 * Filling in all of a previously delayed allocation extent.
798 * The left and right neighbors are both contiguous with new.
800 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
802 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
803 LEFT.br_blockcount + PREV.br_blockcount +
804 RIGHT.br_blockcount);
805 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
807 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
808 xfs_iext_remove(ifp, idx, 2);
809 ip->i_df.if_lastex = idx - 1;
810 ip->i_d.di_nextents--;
812 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
814 rval = XFS_ILOG_CORE;
815 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
817 RIGHT.br_blockcount, &i)))
819 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
820 if ((error = xfs_bmbt_delete(cur, &i)))
822 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
823 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
825 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
826 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
830 RIGHT.br_blockcount, LEFT.br_state)))
834 /* DELTA: Three in-core extents are replaced by one. */
835 temp = LEFT.br_startoff;
836 temp2 = LEFT.br_blockcount +
841 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
843 * Filling in all of a previously delayed allocation extent.
844 * The left neighbor is contiguous, the right is not.
846 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
848 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
849 LEFT.br_blockcount + PREV.br_blockcount);
850 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
852 ip->i_df.if_lastex = idx - 1;
853 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
854 xfs_iext_remove(ifp, idx, 1);
856 rval = XFS_ILOG_DEXT;
859 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
860 LEFT.br_startblock, LEFT.br_blockcount,
863 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
864 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
867 PREV.br_blockcount, LEFT.br_state)))
871 /* DELTA: Two in-core extents are replaced by one. */
872 temp = LEFT.br_startoff;
873 temp2 = LEFT.br_blockcount +
877 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
879 * Filling in all of a previously delayed allocation extent.
880 * The right neighbor is contiguous, the left is not.
882 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
883 xfs_bmbt_set_startblock(ep, new->br_startblock);
884 xfs_bmbt_set_blockcount(ep,
885 PREV.br_blockcount + RIGHT.br_blockcount);
886 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
887 ip->i_df.if_lastex = idx;
888 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
889 xfs_iext_remove(ifp, idx + 1, 1);
891 rval = XFS_ILOG_DEXT;
894 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
896 RIGHT.br_blockcount, &i)))
898 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
899 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
902 RIGHT.br_blockcount, PREV.br_state)))
906 /* DELTA: Two in-core extents are replaced by one. */
907 temp = PREV.br_startoff;
908 temp2 = PREV.br_blockcount +
912 case MASK2(LEFT_FILLING, RIGHT_FILLING):
914 * Filling in all of a previously delayed allocation extent.
915 * Neither the left nor right neighbors are contiguous with
918 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
919 xfs_bmbt_set_startblock(ep, new->br_startblock);
920 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
921 ip->i_df.if_lastex = idx;
922 ip->i_d.di_nextents++;
924 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
926 rval = XFS_ILOG_CORE;
927 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
928 new->br_startblock, new->br_blockcount,
931 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
932 cur->bc_rec.b.br_state = XFS_EXT_NORM;
933 if ((error = xfs_bmbt_insert(cur, &i)))
935 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
938 /* DELTA: The in-core extent described by new changed type. */
939 temp = new->br_startoff;
940 temp2 = new->br_blockcount;
943 case MASK2(LEFT_FILLING, LEFT_CONTIG):
945 * Filling in the first part of a previous delayed allocation.
946 * The left neighbor is contiguous.
948 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
949 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
950 LEFT.br_blockcount + new->br_blockcount);
951 xfs_bmbt_set_startoff(ep,
952 PREV.br_startoff + new->br_blockcount);
953 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
954 temp = PREV.br_blockcount - new->br_blockcount;
955 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
956 xfs_bmbt_set_blockcount(ep, temp);
957 ip->i_df.if_lastex = idx - 1;
959 rval = XFS_ILOG_DEXT;
962 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
963 LEFT.br_startblock, LEFT.br_blockcount,
966 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
967 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
974 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
975 STARTBLOCKVAL(PREV.br_startblock));
976 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
977 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
979 /* DELTA: The boundary between two in-core extents moved. */
980 temp = LEFT.br_startoff;
981 temp2 = LEFT.br_blockcount +
985 case MASK(LEFT_FILLING):
987 * Filling in the first part of a previous delayed allocation.
988 * The left neighbor is not contiguous.
990 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
991 xfs_bmbt_set_startoff(ep, new_endoff);
992 temp = PREV.br_blockcount - new->br_blockcount;
993 xfs_bmbt_set_blockcount(ep, temp);
994 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
996 xfs_iext_insert(ifp, idx, 1, new);
997 ip->i_df.if_lastex = idx;
998 ip->i_d.di_nextents++;
1000 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1002 rval = XFS_ILOG_CORE;
1003 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1004 new->br_startblock, new->br_blockcount,
1007 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1008 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1009 if ((error = xfs_bmbt_insert(cur, &i)))
1011 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1013 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1014 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1015 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1016 first, flist, &cur, 1, &tmp_rval,
1022 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1023 STARTBLOCKVAL(PREV.br_startblock) -
1024 (cur ? cur->bc_private.b.allocated : 0));
1025 ep = xfs_iext_get_ext(ifp, idx + 1);
1026 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1027 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx + 1, XFS_DATA_FORK);
1029 /* DELTA: One in-core extent is split in two. */
1030 temp = PREV.br_startoff;
1031 temp2 = PREV.br_blockcount;
1034 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1036 * Filling in the last part of a previous delayed allocation.
1037 * The right neighbor is contiguous with the new allocation.
1039 temp = PREV.br_blockcount - new->br_blockcount;
1040 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
1041 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
1042 xfs_bmbt_set_blockcount(ep, temp);
1043 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1044 new->br_startoff, new->br_startblock,
1045 new->br_blockcount + RIGHT.br_blockcount,
1047 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
1048 ip->i_df.if_lastex = idx + 1;
1050 rval = XFS_ILOG_DEXT;
1053 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1054 RIGHT.br_startblock,
1055 RIGHT.br_blockcount, &i)))
1057 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1058 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1060 new->br_blockcount +
1061 RIGHT.br_blockcount,
1065 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1066 STARTBLOCKVAL(PREV.br_startblock));
1067 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1068 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
1070 /* DELTA: The boundary between two in-core extents moved. */
1071 temp = PREV.br_startoff;
1072 temp2 = PREV.br_blockcount +
1073 RIGHT.br_blockcount;
1076 case MASK(RIGHT_FILLING):
1078 * Filling in the last part of a previous delayed allocation.
1079 * The right neighbor is not contiguous.
1081 temp = PREV.br_blockcount - new->br_blockcount;
1082 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1083 xfs_bmbt_set_blockcount(ep, temp);
1084 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1086 xfs_iext_insert(ifp, idx + 1, 1, new);
1087 ip->i_df.if_lastex = idx + 1;
1088 ip->i_d.di_nextents++;
1090 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1092 rval = XFS_ILOG_CORE;
1093 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1094 new->br_startblock, new->br_blockcount,
1097 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1098 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1099 if ((error = xfs_bmbt_insert(cur, &i)))
1101 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1103 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1104 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1105 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1106 first, flist, &cur, 1, &tmp_rval,
1112 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1113 STARTBLOCKVAL(PREV.br_startblock) -
1114 (cur ? cur->bc_private.b.allocated : 0));
1115 ep = xfs_iext_get_ext(ifp, idx);
1116 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1117 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1119 /* DELTA: One in-core extent is split in two. */
1120 temp = PREV.br_startoff;
1121 temp2 = PREV.br_blockcount;
1126 * Filling in the middle part of a previous delayed allocation.
1127 * Contiguity is impossible here.
1128 * This case is avoided almost all the time.
1130 temp = new->br_startoff - PREV.br_startoff;
1131 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
1132 xfs_bmbt_set_blockcount(ep, temp);
1134 r[1].br_state = PREV.br_state;
1135 r[1].br_startblock = 0;
1136 r[1].br_startoff = new_endoff;
1137 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1138 r[1].br_blockcount = temp2;
1139 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
1141 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
1142 ip->i_df.if_lastex = idx + 1;
1143 ip->i_d.di_nextents++;
1145 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1147 rval = XFS_ILOG_CORE;
1148 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1149 new->br_startblock, new->br_blockcount,
1152 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1153 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1154 if ((error = xfs_bmbt_insert(cur, &i)))
1156 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1158 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1159 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1160 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1161 first, flist, &cur, 1, &tmp_rval,
1167 temp = xfs_bmap_worst_indlen(ip, temp);
1168 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1169 diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) -
1170 (cur ? cur->bc_private.b.allocated : 0));
1172 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
1174 * Ick gross gag me with a spoon.
1176 ASSERT(0); /* want to see if this ever happens! */
1182 !xfs_mod_incore_sb(ip->i_mount,
1183 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1190 !xfs_mod_incore_sb(ip->i_mount,
1191 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1196 ep = xfs_iext_get_ext(ifp, idx);
1197 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1198 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
1199 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
1200 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1201 NULLSTARTBLOCK((int)temp2));
1202 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
1203 *dnew = temp + temp2;
1204 /* DELTA: One in-core extent is split in three. */
1205 temp = PREV.br_startoff;
1206 temp2 = PREV.br_blockcount;
1209 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1210 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1211 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1212 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1213 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1214 case MASK(LEFT_CONTIG):
1215 case MASK(RIGHT_CONTIG):
1217 * These cases are all impossible.
1224 if (delta->xed_startoff > temp)
1225 delta->xed_startoff = temp;
1226 if (delta->xed_blockcount < temp2)
1227 delta->xed_blockcount = temp2;
1241 #undef STATE_SET_TEST
1246 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1247 * allocation to a real allocation or vice versa.
1249 STATIC int /* error */
1250 xfs_bmap_add_extent_unwritten_real(
1251 xfs_inode_t *ip, /* incore inode pointer */
1252 xfs_extnum_t idx, /* extent number to update/insert */
1253 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
1254 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1255 int *logflagsp, /* inode logging flags */
1256 xfs_extdelta_t *delta) /* Change made to incore extents */
1258 xfs_btree_cur_t *cur; /* btree cursor */
1259 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1260 int error; /* error return value */
1261 int i; /* temp state */
1262 xfs_ifork_t *ifp; /* inode fork pointer */
1263 xfs_fileoff_t new_endoff; /* end offset of new entry */
1264 xfs_exntst_t newext; /* new extent state */
1265 xfs_exntst_t oldext; /* old extent state */
1266 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1267 /* left is 0, right is 1, prev is 2 */
1268 int rval=0; /* return value (logging flags) */
1269 int state = 0;/* state bits, accessed thru macros */
1270 xfs_filblks_t temp=0;
1271 xfs_filblks_t temp2=0;
1272 enum { /* bit number definitions for state */
1273 LEFT_CONTIG, RIGHT_CONTIG,
1274 LEFT_FILLING, RIGHT_FILLING,
1275 LEFT_DELAY, RIGHT_DELAY,
1276 LEFT_VALID, RIGHT_VALID
1282 #define MASK(b) (1 << (b))
1283 #define MASK2(a,b) (MASK(a) | MASK(b))
1284 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
1285 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
1286 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1287 #define STATE_TEST(b) (state & MASK(b))
1288 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1289 ((state &= ~MASK(b)), 0))
1290 #define SWITCH_STATE \
1291 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
1294 * Set up a bunch of variables to make the tests simpler.
1298 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1299 ep = xfs_iext_get_ext(ifp, idx);
1300 xfs_bmbt_get_all(ep, &PREV);
1301 newext = new->br_state;
1302 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1303 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1304 ASSERT(PREV.br_state == oldext);
1305 new_endoff = new->br_startoff + new->br_blockcount;
1306 ASSERT(PREV.br_startoff <= new->br_startoff);
1307 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1309 * Set flags determining what part of the previous oldext allocation
1310 * extent is being replaced by a newext allocation.
1312 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
1313 STATE_SET(RIGHT_FILLING,
1314 PREV.br_startoff + PREV.br_blockcount == new_endoff);
1316 * Check and set flags if this segment has a left neighbor.
1317 * Don't set contiguous if the combined extent would be too large.
1319 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1320 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
1321 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
1323 STATE_SET(LEFT_CONTIG,
1324 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
1325 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1326 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1327 LEFT.br_state == newext &&
1328 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1330 * Check and set flags if this segment has a right neighbor.
1331 * Don't set contiguous if the combined extent would be too large.
1332 * Also check for all-three-contiguous being too large.
1334 if (STATE_SET_TEST(RIGHT_VALID,
1336 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
1337 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
1338 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
1340 STATE_SET(RIGHT_CONTIG,
1341 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
1342 new_endoff == RIGHT.br_startoff &&
1343 new->br_startblock + new->br_blockcount ==
1344 RIGHT.br_startblock &&
1345 newext == RIGHT.br_state &&
1346 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1347 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
1348 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
1349 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1352 * Switch out based on the FILLING and CONTIG state bits.
1354 switch (SWITCH_STATE) {
1356 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1358 * Setting all of a previous oldext extent to newext.
1359 * The left and right neighbors are both contiguous with new.
1361 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
1363 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1364 LEFT.br_blockcount + PREV.br_blockcount +
1365 RIGHT.br_blockcount);
1366 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
1368 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
1369 xfs_iext_remove(ifp, idx, 2);
1370 ip->i_df.if_lastex = idx - 1;
1371 ip->i_d.di_nextents -= 2;
1373 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1375 rval = XFS_ILOG_CORE;
1376 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1377 RIGHT.br_startblock,
1378 RIGHT.br_blockcount, &i)))
1380 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1381 if ((error = xfs_bmbt_delete(cur, &i)))
1383 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1384 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1386 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1387 if ((error = xfs_bmbt_delete(cur, &i)))
1389 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1390 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1392 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1393 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1395 LEFT.br_blockcount + PREV.br_blockcount +
1396 RIGHT.br_blockcount, LEFT.br_state)))
1399 /* DELTA: Three in-core extents are replaced by one. */
1400 temp = LEFT.br_startoff;
1401 temp2 = LEFT.br_blockcount +
1402 PREV.br_blockcount +
1403 RIGHT.br_blockcount;
1406 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
1408 * Setting all of a previous oldext extent to newext.
1409 * The left neighbor is contiguous, the right is not.
1411 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
1413 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1414 LEFT.br_blockcount + PREV.br_blockcount);
1415 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
1417 ip->i_df.if_lastex = idx - 1;
1418 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
1419 xfs_iext_remove(ifp, idx, 1);
1420 ip->i_d.di_nextents--;
1422 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1424 rval = XFS_ILOG_CORE;
1425 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1426 PREV.br_startblock, PREV.br_blockcount,
1429 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1430 if ((error = xfs_bmbt_delete(cur, &i)))
1432 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1433 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1435 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1436 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1438 LEFT.br_blockcount + PREV.br_blockcount,
1442 /* DELTA: Two in-core extents are replaced by one. */
1443 temp = LEFT.br_startoff;
1444 temp2 = LEFT.br_blockcount +
1448 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
1450 * Setting all of a previous oldext extent to newext.
1451 * The right neighbor is contiguous, the left is not.
1453 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx,
1455 xfs_bmbt_set_blockcount(ep,
1456 PREV.br_blockcount + RIGHT.br_blockcount);
1457 xfs_bmbt_set_state(ep, newext);
1458 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx,
1460 ip->i_df.if_lastex = idx;
1461 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
1462 xfs_iext_remove(ifp, idx + 1, 1);
1463 ip->i_d.di_nextents--;
1465 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1467 rval = XFS_ILOG_CORE;
1468 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1469 RIGHT.br_startblock,
1470 RIGHT.br_blockcount, &i)))
1472 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1473 if ((error = xfs_bmbt_delete(cur, &i)))
1475 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1476 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1478 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1479 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1481 new->br_blockcount + RIGHT.br_blockcount,
1485 /* DELTA: Two in-core extents are replaced by one. */
1486 temp = PREV.br_startoff;
1487 temp2 = PREV.br_blockcount +
1488 RIGHT.br_blockcount;
1491 case MASK2(LEFT_FILLING, RIGHT_FILLING):
1493 * Setting all of a previous oldext extent to newext.
1494 * Neither the left nor right neighbors are contiguous with
1497 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx,
1499 xfs_bmbt_set_state(ep, newext);
1500 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx,
1502 ip->i_df.if_lastex = idx;
1504 rval = XFS_ILOG_DEXT;
1507 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1508 new->br_startblock, new->br_blockcount,
1511 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1512 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1513 new->br_startblock, new->br_blockcount,
1517 /* DELTA: The in-core extent described by new changed type. */
1518 temp = new->br_startoff;
1519 temp2 = 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("LF|LC", ip, idx - 1,
1529 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 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("LF|LC", ip, idx - 1,
1535 XFS_BMAP_TRACE_PRE_UPDATE("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("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,
1552 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
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,
1567 /* DELTA: The boundary between two in-core extents moved. */
1568 temp = LEFT.br_startoff;
1569 temp2 = LEFT.br_blockcount +
1573 case MASK(LEFT_FILLING):
1575 * Setting the first part of a previous oldext extent to newext.
1576 * The left neighbor is not contiguous.
1578 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
1579 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1580 xfs_bmbt_set_startoff(ep, new_endoff);
1581 xfs_bmbt_set_blockcount(ep,
1582 PREV.br_blockcount - new->br_blockcount);
1583 xfs_bmbt_set_startblock(ep,
1584 new->br_startblock + new->br_blockcount);
1585 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx, XFS_DATA_FORK);
1586 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
1588 xfs_iext_insert(ifp, idx, 1, new);
1589 ip->i_df.if_lastex = idx;
1590 ip->i_d.di_nextents++;
1592 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1594 rval = XFS_ILOG_CORE;
1595 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1596 PREV.br_startblock, PREV.br_blockcount,
1599 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1600 if ((error = xfs_bmbt_update(cur,
1601 PREV.br_startoff + new->br_blockcount,
1602 PREV.br_startblock + new->br_blockcount,
1603 PREV.br_blockcount - new->br_blockcount,
1606 cur->bc_rec.b = *new;
1607 if ((error = xfs_bmbt_insert(cur, &i)))
1609 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1611 /* DELTA: One in-core extent is split in two. */
1612 temp = PREV.br_startoff;
1613 temp2 = PREV.br_blockcount;
1616 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1618 * Setting the last part of a previous oldext extent to newext.
1619 * The right neighbor is contiguous with the new allocation.
1621 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx,
1623 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1,
1625 xfs_bmbt_set_blockcount(ep,
1626 PREV.br_blockcount - new->br_blockcount);
1627 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx,
1629 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1630 new->br_startoff, new->br_startblock,
1631 new->br_blockcount + RIGHT.br_blockcount, newext);
1632 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1,
1634 ip->i_df.if_lastex = idx + 1;
1636 rval = XFS_ILOG_DEXT;
1639 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1641 PREV.br_blockcount, &i)))
1643 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1644 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1646 PREV.br_blockcount - new->br_blockcount,
1649 if ((error = xfs_btree_increment(cur, 0, &i)))
1651 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1653 new->br_blockcount + RIGHT.br_blockcount,
1657 /* DELTA: The boundary between two in-core extents moved. */
1658 temp = PREV.br_startoff;
1659 temp2 = PREV.br_blockcount +
1660 RIGHT.br_blockcount;
1663 case MASK(RIGHT_FILLING):
1665 * Setting the last part of a previous oldext extent to newext.
1666 * The right neighbor is not contiguous.
1668 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1669 xfs_bmbt_set_blockcount(ep,
1670 PREV.br_blockcount - new->br_blockcount);
1671 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1672 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1674 xfs_iext_insert(ifp, idx + 1, 1, new);
1675 ip->i_df.if_lastex = idx + 1;
1676 ip->i_d.di_nextents++;
1678 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1680 rval = XFS_ILOG_CORE;
1681 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1682 PREV.br_startblock, PREV.br_blockcount,
1685 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1686 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1688 PREV.br_blockcount - new->br_blockcount,
1691 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1692 new->br_startblock, new->br_blockcount,
1695 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1696 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1697 if ((error = xfs_bmbt_insert(cur, &i)))
1699 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1701 /* DELTA: One in-core extent is split in two. */
1702 temp = PREV.br_startoff;
1703 temp2 = PREV.br_blockcount;
1708 * Setting the middle part of a previous oldext extent to
1709 * newext. Contiguity is impossible here.
1710 * One extent becomes three extents.
1712 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
1713 xfs_bmbt_set_blockcount(ep,
1714 new->br_startoff - PREV.br_startoff);
1715 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
1717 r[1].br_startoff = new_endoff;
1718 r[1].br_blockcount =
1719 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1720 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1721 r[1].br_state = oldext;
1722 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
1724 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
1725 ip->i_df.if_lastex = idx + 1;
1726 ip->i_d.di_nextents += 2;
1728 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1730 rval = XFS_ILOG_CORE;
1731 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1732 PREV.br_startblock, PREV.br_blockcount,
1735 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1736 /* new right extent - oldext */
1737 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1738 r[1].br_startblock, r[1].br_blockcount,
1741 /* new left extent - oldext */
1742 cur->bc_rec.b = PREV;
1743 cur->bc_rec.b.br_blockcount =
1744 new->br_startoff - PREV.br_startoff;
1745 if ((error = xfs_bmbt_insert(cur, &i)))
1747 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1749 * Reset the cursor to the position of the new extent
1750 * we are about to insert as we can't trust it after
1751 * the previous insert.
1753 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1754 new->br_startblock, new->br_blockcount,
1757 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1758 /* new middle extent - newext */
1759 cur->bc_rec.b.br_state = new->br_state;
1760 if ((error = xfs_bmbt_insert(cur, &i)))
1762 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1764 /* DELTA: One in-core extent is split in three. */
1765 temp = PREV.br_startoff;
1766 temp2 = PREV.br_blockcount;
1769 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1770 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1771 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1772 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1773 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1774 case MASK(LEFT_CONTIG):
1775 case MASK(RIGHT_CONTIG):
1777 * These cases are all impossible.
1784 if (delta->xed_startoff > temp)
1785 delta->xed_startoff = temp;
1786 if (delta->xed_blockcount < temp2)
1787 delta->xed_blockcount = temp2;
1801 #undef STATE_SET_TEST
1806 * Called by xfs_bmap_add_extent to handle cases converting a hole
1807 * to a delayed allocation.
1810 STATIC int /* error */
1811 xfs_bmap_add_extent_hole_delay(
1812 xfs_inode_t *ip, /* incore inode pointer */
1813 xfs_extnum_t idx, /* extent number to update/insert */
1814 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1815 int *logflagsp, /* inode logging flags */
1816 xfs_extdelta_t *delta, /* Change made to incore extents */
1817 int rsvd) /* OK to allocate reserved blocks */
1819 xfs_bmbt_rec_host_t *ep; /* extent record for idx */
1820 xfs_ifork_t *ifp; /* inode fork pointer */
1821 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1822 xfs_filblks_t newlen=0; /* new indirect size */
1823 xfs_filblks_t oldlen=0; /* old indirect size */
1824 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1825 int state; /* state bits, accessed thru macros */
1826 xfs_filblks_t temp=0; /* temp for indirect calculations */
1827 xfs_filblks_t temp2=0;
1828 enum { /* bit number definitions for state */
1829 LEFT_CONTIG, RIGHT_CONTIG,
1830 LEFT_DELAY, RIGHT_DELAY,
1831 LEFT_VALID, RIGHT_VALID
1834 #define MASK(b) (1 << (b))
1835 #define MASK2(a,b) (MASK(a) | MASK(b))
1836 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1837 #define STATE_TEST(b) (state & MASK(b))
1838 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1839 ((state &= ~MASK(b)), 0))
1840 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1842 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1843 ep = xfs_iext_get_ext(ifp, idx);
1845 ASSERT(ISNULLSTARTBLOCK(new->br_startblock));
1847 * Check and set flags if this segment has a left neighbor
1849 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1850 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1851 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1854 * Check and set flags if the current (right) segment exists.
1855 * If it doesn't exist, we're converting the hole at end-of-file.
1857 if (STATE_SET_TEST(RIGHT_VALID,
1859 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1860 xfs_bmbt_get_all(ep, &right);
1861 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1864 * Set contiguity flags on the left and right neighbors.
1865 * Don't let extents get too large, even if the pieces are contiguous.
1867 STATE_SET(LEFT_CONTIG,
1868 STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) &&
1869 left.br_startoff + left.br_blockcount == new->br_startoff &&
1870 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1871 STATE_SET(RIGHT_CONTIG,
1872 STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) &&
1873 new->br_startoff + new->br_blockcount == right.br_startoff &&
1874 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1875 (!STATE_TEST(LEFT_CONTIG) ||
1876 (left.br_blockcount + new->br_blockcount +
1877 right.br_blockcount <= MAXEXTLEN)));
1879 * Switch out based on the contiguity flags.
1881 switch (SWITCH_STATE) {
1883 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1885 * New allocation is contiguous with delayed allocations
1886 * on the left and on the right.
1887 * Merge all three into a single extent record.
1889 temp = left.br_blockcount + new->br_blockcount +
1890 right.br_blockcount;
1891 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
1893 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1894 oldlen = STARTBLOCKVAL(left.br_startblock) +
1895 STARTBLOCKVAL(new->br_startblock) +
1896 STARTBLOCKVAL(right.br_startblock);
1897 newlen = xfs_bmap_worst_indlen(ip, temp);
1898 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1899 NULLSTARTBLOCK((int)newlen));
1900 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
1902 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, XFS_DATA_FORK);
1903 xfs_iext_remove(ifp, idx, 1);
1904 ip->i_df.if_lastex = idx - 1;
1905 /* DELTA: Two in-core extents were replaced by one. */
1907 temp = left.br_startoff;
1910 case MASK(LEFT_CONTIG):
1912 * New allocation is contiguous with a delayed allocation
1914 * Merge the new allocation with the left neighbor.
1916 temp = left.br_blockcount + new->br_blockcount;
1917 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1,
1919 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1920 oldlen = STARTBLOCKVAL(left.br_startblock) +
1921 STARTBLOCKVAL(new->br_startblock);
1922 newlen = xfs_bmap_worst_indlen(ip, temp);
1923 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1924 NULLSTARTBLOCK((int)newlen));
1925 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1,
1927 ip->i_df.if_lastex = idx - 1;
1928 /* DELTA: One in-core extent grew into a hole. */
1930 temp = left.br_startoff;
1933 case MASK(RIGHT_CONTIG):
1935 * New allocation is contiguous with a delayed allocation
1937 * Merge the new allocation with the right neighbor.
1939 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, XFS_DATA_FORK);
1940 temp = new->br_blockcount + right.br_blockcount;
1941 oldlen = STARTBLOCKVAL(new->br_startblock) +
1942 STARTBLOCKVAL(right.br_startblock);
1943 newlen = xfs_bmap_worst_indlen(ip, temp);
1944 xfs_bmbt_set_allf(ep, new->br_startoff,
1945 NULLSTARTBLOCK((int)newlen), temp, right.br_state);
1946 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, XFS_DATA_FORK);
1947 ip->i_df.if_lastex = idx;
1948 /* DELTA: One in-core extent grew into a hole. */
1950 temp = new->br_startoff;
1955 * New allocation is not contiguous with another
1956 * delayed allocation.
1957 * Insert a new entry.
1959 oldlen = newlen = 0;
1960 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL,
1962 xfs_iext_insert(ifp, idx, 1, new);
1963 ip->i_df.if_lastex = idx;
1964 /* DELTA: A new in-core extent was added in a hole. */
1965 temp2 = new->br_blockcount;
1966 temp = new->br_startoff;
1969 if (oldlen != newlen) {
1970 ASSERT(oldlen > newlen);
1971 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
1972 (int64_t)(oldlen - newlen), rsvd);
1974 * Nothing to do for disk quota accounting here.
1979 if (delta->xed_startoff > temp)
1980 delta->xed_startoff = temp;
1981 if (delta->xed_blockcount < temp2)
1982 delta->xed_blockcount = temp2;
1990 #undef STATE_SET_TEST
1995 * Called by xfs_bmap_add_extent to handle cases converting a hole
1996 * to a real allocation.
1998 STATIC int /* error */
1999 xfs_bmap_add_extent_hole_real(
2000 xfs_inode_t *ip, /* incore inode pointer */
2001 xfs_extnum_t idx, /* extent number to update/insert */
2002 xfs_btree_cur_t *cur, /* if null, not a btree */
2003 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2004 int *logflagsp, /* inode logging flags */
2005 xfs_extdelta_t *delta, /* Change made to incore extents */
2006 int whichfork) /* data or attr fork */
2008 xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */
2009 int error; /* error return value */
2010 int i; /* temp state */
2011 xfs_ifork_t *ifp; /* inode fork pointer */
2012 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2013 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2014 int rval=0; /* return value (logging flags) */
2015 int state; /* state bits, accessed thru macros */
2016 xfs_filblks_t temp=0;
2017 xfs_filblks_t temp2=0;
2018 enum { /* bit number definitions for state */
2019 LEFT_CONTIG, RIGHT_CONTIG,
2020 LEFT_DELAY, RIGHT_DELAY,
2021 LEFT_VALID, RIGHT_VALID
2024 #define MASK(b) (1 << (b))
2025 #define MASK2(a,b) (MASK(a) | MASK(b))
2026 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
2027 #define STATE_TEST(b) (state & MASK(b))
2028 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
2029 ((state &= ~MASK(b)), 0))
2030 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
2032 ifp = XFS_IFORK_PTR(ip, whichfork);
2033 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
2034 ep = xfs_iext_get_ext(ifp, idx);
2037 * Check and set flags if this segment has a left neighbor.
2039 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
2040 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
2041 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
2044 * Check and set flags if this segment has a current value.
2045 * Not true if we're inserting into the "hole" at eof.
2047 if (STATE_SET_TEST(RIGHT_VALID,
2049 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
2050 xfs_bmbt_get_all(ep, &right);
2051 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
2054 * We're inserting a real allocation between "left" and "right".
2055 * Set the contiguity flags. Don't let extents get too large.
2057 STATE_SET(LEFT_CONTIG,
2058 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
2059 left.br_startoff + left.br_blockcount == new->br_startoff &&
2060 left.br_startblock + left.br_blockcount == new->br_startblock &&
2061 left.br_state == new->br_state &&
2062 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
2063 STATE_SET(RIGHT_CONTIG,
2064 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
2065 new->br_startoff + new->br_blockcount == right.br_startoff &&
2066 new->br_startblock + new->br_blockcount ==
2067 right.br_startblock &&
2068 new->br_state == right.br_state &&
2069 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2070 (!STATE_TEST(LEFT_CONTIG) ||
2071 left.br_blockcount + new->br_blockcount +
2072 right.br_blockcount <= MAXEXTLEN));
2076 * Select which case we're in here, and implement it.
2078 switch (SWITCH_STATE) {
2080 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
2082 * New allocation is contiguous with real allocations on the
2083 * left and on the right.
2084 * Merge all three into a single extent record.
2086 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
2088 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2089 left.br_blockcount + new->br_blockcount +
2090 right.br_blockcount);
2091 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
2093 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, whichfork);
2094 xfs_iext_remove(ifp, idx, 1);
2095 ifp->if_lastex = idx - 1;
2096 XFS_IFORK_NEXT_SET(ip, whichfork,
2097 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2099 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2101 rval = XFS_ILOG_CORE;
2102 if ((error = xfs_bmbt_lookup_eq(cur,
2104 right.br_startblock,
2105 right.br_blockcount, &i)))
2107 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2108 if ((error = xfs_bmbt_delete(cur, &i)))
2110 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2111 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2113 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2114 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2116 left.br_blockcount +
2117 new->br_blockcount +
2118 right.br_blockcount,
2122 /* DELTA: Two in-core extents were replaced by one. */
2123 temp = left.br_startoff;
2124 temp2 = left.br_blockcount +
2125 new->br_blockcount +
2126 right.br_blockcount;
2129 case MASK(LEFT_CONTIG):
2131 * New allocation is contiguous with a real allocation
2133 * Merge the new allocation with the left neighbor.
2135 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1, whichfork);
2136 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2137 left.br_blockcount + new->br_blockcount);
2138 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1, whichfork);
2139 ifp->if_lastex = idx - 1;
2141 rval = XFS_ILOG_FEXT(whichfork);
2144 if ((error = xfs_bmbt_lookup_eq(cur,
2147 left.br_blockcount, &i)))
2149 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2150 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2152 left.br_blockcount +
2157 /* DELTA: One in-core extent grew. */
2158 temp = left.br_startoff;
2159 temp2 = left.br_blockcount +
2163 case MASK(RIGHT_CONTIG):
2165 * New allocation is contiguous with a real allocation
2167 * Merge the new allocation with the right neighbor.
2169 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, whichfork);
2170 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2171 new->br_blockcount + right.br_blockcount,
2173 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, whichfork);
2174 ifp->if_lastex = idx;
2176 rval = XFS_ILOG_FEXT(whichfork);
2179 if ((error = xfs_bmbt_lookup_eq(cur,
2181 right.br_startblock,
2182 right.br_blockcount, &i)))
2184 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2185 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2187 new->br_blockcount +
2188 right.br_blockcount,
2192 /* DELTA: One in-core extent grew. */
2193 temp = new->br_startoff;
2194 temp2 = new->br_blockcount +
2195 right.br_blockcount;
2200 * New allocation is not contiguous with another
2202 * Insert a new entry.
2204 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL, whichfork);
2205 xfs_iext_insert(ifp, idx, 1, new);
2206 ifp->if_lastex = idx;
2207 XFS_IFORK_NEXT_SET(ip, whichfork,
2208 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2210 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2212 rval = XFS_ILOG_CORE;
2213 if ((error = xfs_bmbt_lookup_eq(cur,
2216 new->br_blockcount, &i)))
2218 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
2219 cur->bc_rec.b.br_state = new->br_state;
2220 if ((error = xfs_bmbt_insert(cur, &i)))
2222 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2224 /* DELTA: A new extent was added in a hole. */
2225 temp = new->br_startoff;
2226 temp2 = new->br_blockcount;
2231 if (delta->xed_startoff > temp)
2232 delta->xed_startoff = temp;
2233 if (delta->xed_blockcount < temp2)
2234 delta->xed_blockcount = temp2;
2243 #undef STATE_SET_TEST
2248 * Adjust the size of the new extent based on di_extsize and rt extsize.
2251 xfs_bmap_extsize_align(
2253 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2254 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2255 xfs_extlen_t extsz, /* align to this extent size */
2256 int rt, /* is this a realtime inode? */
2257 int eof, /* is extent at end-of-file? */
2258 int delay, /* creating delalloc extent? */
2259 int convert, /* overwriting unwritten extent? */
2260 xfs_fileoff_t *offp, /* in/out: aligned offset */
2261 xfs_extlen_t *lenp) /* in/out: aligned length */
2263 xfs_fileoff_t orig_off; /* original offset */
2264 xfs_extlen_t orig_alen; /* original length */
2265 xfs_fileoff_t orig_end; /* original off+len */
2266 xfs_fileoff_t nexto; /* next file offset */
2267 xfs_fileoff_t prevo; /* previous file offset */
2268 xfs_fileoff_t align_off; /* temp for offset */
2269 xfs_extlen_t align_alen; /* temp for length */
2270 xfs_extlen_t temp; /* temp for calculations */
2275 orig_off = align_off = *offp;
2276 orig_alen = align_alen = *lenp;
2277 orig_end = orig_off + orig_alen;
2280 * If this request overlaps an existing extent, then don't
2281 * attempt to perform any additional alignment.
2283 if (!delay && !eof &&
2284 (orig_off >= gotp->br_startoff) &&
2285 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2290 * If the file offset is unaligned vs. the extent size
2291 * we need to align it. This will be possible unless
2292 * the file was previously written with a kernel that didn't
2293 * perform this alignment, or if a truncate shot us in the
2296 temp = do_mod(orig_off, extsz);
2302 * Same adjustment for the end of the requested area.
2304 if ((temp = (align_alen % extsz))) {
2305 align_alen += extsz - temp;
2308 * If the previous block overlaps with this proposed allocation
2309 * then move the start forward without adjusting the length.
2311 if (prevp->br_startoff != NULLFILEOFF) {
2312 if (prevp->br_startblock == HOLESTARTBLOCK)
2313 prevo = prevp->br_startoff;
2315 prevo = prevp->br_startoff + prevp->br_blockcount;
2318 if (align_off != orig_off && align_off < prevo)
2321 * If the next block overlaps with this proposed allocation
2322 * then move the start back without adjusting the length,
2323 * but not before offset 0.
2324 * This may of course make the start overlap previous block,
2325 * and if we hit the offset 0 limit then the next block
2326 * can still overlap too.
2328 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2329 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2330 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2331 nexto = gotp->br_startoff + gotp->br_blockcount;
2333 nexto = gotp->br_startoff;
2335 nexto = NULLFILEOFF;
2337 align_off + align_alen != orig_end &&
2338 align_off + align_alen > nexto)
2339 align_off = nexto > align_alen ? nexto - align_alen : 0;
2341 * If we're now overlapping the next or previous extent that
2342 * means we can't fit an extsz piece in this hole. Just move
2343 * the start forward to the first valid spot and set
2344 * the length so we hit the end.
2346 if (align_off != orig_off && align_off < prevo)
2348 if (align_off + align_alen != orig_end &&
2349 align_off + align_alen > nexto &&
2350 nexto != NULLFILEOFF) {
2351 ASSERT(nexto > prevo);
2352 align_alen = nexto - align_off;
2356 * If realtime, and the result isn't a multiple of the realtime
2357 * extent size we need to remove blocks until it is.
2359 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2361 * We're not covering the original request, or
2362 * we won't be able to once we fix the length.
2364 if (orig_off < align_off ||
2365 orig_end > align_off + align_alen ||
2366 align_alen - temp < orig_alen)
2367 return XFS_ERROR(EINVAL);
2369 * Try to fix it by moving the start up.
2371 if (align_off + temp <= orig_off) {
2376 * Try to fix it by moving the end in.
2378 else if (align_off + align_alen - temp >= orig_end)
2381 * Set the start to the minimum then trim the length.
2384 align_alen -= orig_off - align_off;
2385 align_off = orig_off;
2386 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2389 * Result doesn't cover the request, fail it.
2391 if (orig_off < align_off || orig_end > align_off + align_alen)
2392 return XFS_ERROR(EINVAL);
2394 ASSERT(orig_off >= align_off);
2395 ASSERT(orig_end <= align_off + align_alen);
2399 if (!eof && gotp->br_startoff != NULLFILEOFF)
2400 ASSERT(align_off + align_alen <= gotp->br_startoff);
2401 if (prevp->br_startoff != NULLFILEOFF)
2402 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2410 #define XFS_ALLOC_GAP_UNITS 4
2414 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2416 xfs_fsblock_t adjust; /* adjustment to block numbers */
2417 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2418 xfs_mount_t *mp; /* mount point structure */
2419 int nullfb; /* true if ap->firstblock isn't set */
2420 int rt; /* true if inode is realtime */
2422 #define ISVALID(x,y) \
2424 (x) < mp->m_sb.sb_rblocks : \
2425 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2426 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2427 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2429 mp = ap->ip->i_mount;
2430 nullfb = ap->firstblock == NULLFSBLOCK;
2431 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2432 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2434 * If allocating at eof, and there's a previous real block,
2435 * try to use it's last block as our starting point.
2437 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2438 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2439 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2440 ap->prevp->br_startblock)) {
2441 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2443 * Adjust for the gap between prevp and us.
2446 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2448 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2452 * If not at eof, then compare the two neighbor blocks.
2453 * Figure out whether either one gives us a good starting point,
2454 * and pick the better one.
2456 else if (!ap->eof) {
2457 xfs_fsblock_t gotbno; /* right side block number */
2458 xfs_fsblock_t gotdiff=0; /* right side difference */
2459 xfs_fsblock_t prevbno; /* left side block number */
2460 xfs_fsblock_t prevdiff=0; /* left side difference */
2463 * If there's a previous (left) block, select a requested
2464 * start block based on it.
2466 if (ap->prevp->br_startoff != NULLFILEOFF &&
2467 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2468 (prevbno = ap->prevp->br_startblock +
2469 ap->prevp->br_blockcount) &&
2470 ISVALID(prevbno, ap->prevp->br_startblock)) {
2472 * Calculate gap to end of previous block.
2474 adjust = prevdiff = ap->off -
2475 (ap->prevp->br_startoff +
2476 ap->prevp->br_blockcount);
2478 * Figure the startblock based on the previous block's
2479 * end and the gap size.
2481 * If the gap is large relative to the piece we're
2482 * allocating, or using it gives us an invalid block
2483 * number, then just use the end of the previous block.
2485 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2486 ISVALID(prevbno + prevdiff,
2487 ap->prevp->br_startblock))
2492 * If the firstblock forbids it, can't use it,
2495 if (!rt && !nullfb &&
2496 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2497 prevbno = NULLFSBLOCK;
2500 * No previous block or can't follow it, just default.
2503 prevbno = NULLFSBLOCK;
2505 * If there's a following (right) block, select a requested
2506 * start block based on it.
2508 if (!ISNULLSTARTBLOCK(ap->gotp->br_startblock)) {
2510 * Calculate gap to start of next block.
2512 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2514 * Figure the startblock based on the next block's
2515 * start and the gap size.
2517 gotbno = ap->gotp->br_startblock;
2520 * If the gap is large relative to the piece we're
2521 * allocating, or using it gives us an invalid block
2522 * number, then just use the start of the next block
2523 * offset by our length.
2525 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2526 ISVALID(gotbno - gotdiff, gotbno))
2528 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2530 gotdiff += adjust - ap->alen;
2534 * If the firstblock forbids it, can't use it,
2537 if (!rt && !nullfb &&
2538 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2539 gotbno = NULLFSBLOCK;
2542 * No next block, just default.
2545 gotbno = NULLFSBLOCK;
2547 * If both valid, pick the better one, else the only good
2548 * one, else ap->rval is already set (to 0 or the inode block).
2550 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2551 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2552 else if (prevbno != NULLFSBLOCK)
2554 else if (gotbno != NULLFSBLOCK)
2562 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2564 xfs_alloctype_t atype = 0; /* type for allocation routines */
2565 int error; /* error return value */
2566 xfs_mount_t *mp; /* mount point structure */
2567 xfs_extlen_t prod = 0; /* product factor for allocators */
2568 xfs_extlen_t ralen = 0; /* realtime allocation length */
2569 xfs_extlen_t align; /* minimum allocation alignment */
2572 mp = ap->ip->i_mount;
2573 align = xfs_get_extsz_hint(ap->ip);
2574 prod = align / mp->m_sb.sb_rextsize;
2575 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2576 align, 1, ap->eof, 0,
2577 ap->conv, &ap->off, &ap->alen);
2581 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2584 * If the offset & length are not perfectly aligned
2585 * then kill prod, it will just get us in trouble.
2587 if (do_mod(ap->off, align) || ap->alen % align)
2590 * Set ralen to be the actual requested length in rtextents.
2592 ralen = ap->alen / mp->m_sb.sb_rextsize;
2594 * If the old value was close enough to MAXEXTLEN that
2595 * we rounded up to it, cut it back so it's valid again.
2596 * Note that if it's a really large request (bigger than
2597 * MAXEXTLEN), we don't hear about that number, and can't
2598 * adjust the starting point to match it.
2600 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2601 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2603 * If it's an allocation to an empty file at offset 0,
2604 * pick an extent that will space things out in the rt area.
2606 if (ap->eof && ap->off == 0) {
2607 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2609 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2612 ap->rval = rtx * mp->m_sb.sb_rextsize;
2617 xfs_bmap_adjacent(ap);
2620 * Realtime allocation, done through xfs_rtallocate_extent.
2622 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2623 do_div(ap->rval, mp->m_sb.sb_rextsize);
2626 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2627 &ralen, atype, ap->wasdel, prod, &rtb)))
2629 if (rtb == NULLFSBLOCK && prod > 1 &&
2630 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2631 ap->alen, &ralen, atype,
2632 ap->wasdel, 1, &rtb)))
2635 if (ap->rval != NULLFSBLOCK) {
2636 ap->rval *= mp->m_sb.sb_rextsize;
2637 ralen *= mp->m_sb.sb_rextsize;
2639 ap->ip->i_d.di_nblocks += ralen;
2640 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2642 ap->ip->i_delayed_blks -= ralen;
2644 * Adjust the disk quota also. This was reserved
2647 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2648 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2649 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2658 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2660 xfs_mount_t *mp; /* mount point structure */
2661 xfs_alloctype_t atype = 0; /* type for allocation routines */
2662 xfs_extlen_t align; /* minimum allocation alignment */
2664 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2665 xfs_agnumber_t startag;
2666 xfs_alloc_arg_t args;
2669 xfs_extlen_t longest;
2671 xfs_extlen_t nextminlen = 0;
2673 int nullfb; /* true if ap->firstblock isn't set */
2679 mp = ap->ip->i_mount;
2680 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
2681 if (unlikely(align)) {
2682 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2683 align, 0, ap->eof, 0, ap->conv,
2684 &ap->off, &ap->alen);
2688 nullfb = ap->firstblock == NULLFSBLOCK;
2689 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2691 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2692 ag = xfs_filestream_lookup_ag(ap->ip);
2693 ag = (ag != NULLAGNUMBER) ? ag : 0;
2694 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2696 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2699 ap->rval = ap->firstblock;
2701 xfs_bmap_adjacent(ap);
2704 * If allowed, use ap->rval; otherwise must use firstblock since
2705 * it's in the right allocation group.
2707 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2710 ap->rval = ap->firstblock;
2712 * Normal allocation, done through xfs_alloc_vextent.
2714 tryagain = isaligned = 0;
2717 args.fsbno = ap->rval;
2718 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
2719 args.firstblock = ap->firstblock;
2722 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2723 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2725 args.type = XFS_ALLOCTYPE_START_BNO;
2726 args.total = ap->total;
2729 * Search for an allocation group with a single extent
2730 * large enough for the request.
2732 * If one isn't found, then adjust the minimum allocation
2733 * size to the largest space found.
2735 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
2736 if (startag == NULLAGNUMBER)
2739 down_read(&mp->m_peraglock);
2740 while (blen < ap->alen) {
2741 pag = &mp->m_perag[ag];
2742 if (!pag->pagf_init &&
2743 (error = xfs_alloc_pagf_init(mp, args.tp,
2744 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2745 up_read(&mp->m_peraglock);
2749 * See xfs_alloc_fix_freelist...
2751 if (pag->pagf_init) {
2752 need = XFS_MIN_FREELIST_PAG(pag, mp);
2753 delta = need > pag->pagf_flcount ?
2754 need - pag->pagf_flcount : 0;
2755 longest = (pag->pagf_longest > delta) ?
2756 (pag->pagf_longest - delta) :
2757 (pag->pagf_flcount > 0 ||
2758 pag->pagf_longest > 0);
2764 if (xfs_inode_is_filestream(ap->ip)) {
2765 if (blen >= ap->alen)
2770 * If startag is an invalid AG, we've
2771 * come here once before and
2772 * xfs_filestream_new_ag picked the
2773 * best currently available.
2775 * Don't continue looping, since we
2776 * could loop forever.
2778 if (startag == NULLAGNUMBER)
2781 error = xfs_filestream_new_ag(ap, &ag);
2783 up_read(&mp->m_peraglock);
2787 /* loop again to set 'blen'*/
2788 startag = NULLAGNUMBER;
2792 if (++ag == mp->m_sb.sb_agcount)
2797 up_read(&mp->m_peraglock);
2799 * Since the above loop did a BUF_TRYLOCK, it is
2800 * possible that there is space for this request.
2802 if (notinit || blen < ap->minlen)
2803 args.minlen = ap->minlen;
2805 * If the best seen length is less than the request
2806 * length, use the best as the minimum.
2808 else if (blen < ap->alen)
2811 * Otherwise we've seen an extent as big as alen,
2812 * use that as the minimum.
2815 args.minlen = ap->alen;
2818 * set the failure fallback case to look in the selected
2819 * AG as the stream may have moved.
2821 if (xfs_inode_is_filestream(ap->ip))
2822 ap->rval = args.fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2823 } else if (ap->low) {
2824 if (xfs_inode_is_filestream(ap->ip))
2825 args.type = XFS_ALLOCTYPE_FIRST_AG;
2827 args.type = XFS_ALLOCTYPE_START_BNO;
2828 args.total = args.minlen = ap->minlen;
2830 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2831 args.total = ap->total;
2832 args.minlen = ap->minlen;
2834 /* apply extent size hints if obtained earlier */
2835 if (unlikely(align)) {
2837 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2838 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2839 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
2843 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
2844 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2845 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2848 * If we are not low on available data blocks, and the
2849 * underlying logical volume manager is a stripe, and
2850 * the file offset is zero then try to allocate data
2851 * blocks on stripe unit boundary.
2852 * NOTE: ap->aeof is only set if the allocation length
2853 * is >= the stripe unit and the allocation offset is
2854 * at the end of file.
2856 if (!ap->low && ap->aeof) {
2858 args.alignment = mp->m_dalign;
2862 * Adjust for alignment
2864 if (blen > args.alignment && blen <= ap->alen)
2865 args.minlen = blen - args.alignment;
2866 args.minalignslop = 0;
2869 * First try an exact bno allocation.
2870 * If it fails then do a near or start bno
2871 * allocation with alignment turned on.
2875 args.type = XFS_ALLOCTYPE_THIS_BNO;
2878 * Compute the minlen+alignment for the
2879 * next case. Set slop so that the value
2880 * of minlen+alignment+slop doesn't go up
2881 * between the calls.
2883 if (blen > mp->m_dalign && blen <= ap->alen)
2884 nextminlen = blen - mp->m_dalign;
2886 nextminlen = args.minlen;
2887 if (nextminlen + mp->m_dalign > args.minlen + 1)
2889 nextminlen + mp->m_dalign -
2892 args.minalignslop = 0;
2896 args.minalignslop = 0;
2898 args.minleft = ap->minleft;
2899 args.wasdel = ap->wasdel;
2901 args.userdata = ap->userdata;
2902 if ((error = xfs_alloc_vextent(&args)))
2904 if (tryagain && args.fsbno == NULLFSBLOCK) {
2906 * Exact allocation failed. Now try with alignment
2910 args.fsbno = ap->rval;
2911 args.alignment = mp->m_dalign;
2912 args.minlen = nextminlen;
2913 args.minalignslop = 0;
2915 if ((error = xfs_alloc_vextent(&args)))
2918 if (isaligned && args.fsbno == NULLFSBLOCK) {
2920 * allocation failed, so turn off alignment and
2924 args.fsbno = ap->rval;
2926 if ((error = xfs_alloc_vextent(&args)))
2929 if (args.fsbno == NULLFSBLOCK && nullfb &&
2930 args.minlen > ap->minlen) {
2931 args.minlen = ap->minlen;
2932 args.type = XFS_ALLOCTYPE_START_BNO;
2933 args.fsbno = ap->rval;
2934 if ((error = xfs_alloc_vextent(&args)))
2937 if (args.fsbno == NULLFSBLOCK && nullfb) {
2939 args.type = XFS_ALLOCTYPE_FIRST_AG;
2940 args.total = ap->minlen;
2942 if ((error = xfs_alloc_vextent(&args)))
2946 if (args.fsbno != NULLFSBLOCK) {
2947 ap->firstblock = ap->rval = args.fsbno;
2948 ASSERT(nullfb || fb_agno == args.agno ||
2949 (ap->low && fb_agno < args.agno));
2950 ap->alen = args.len;
2951 ap->ip->i_d.di_nblocks += args.len;
2952 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2954 ap->ip->i_delayed_blks -= args.len;
2956 * Adjust the disk quota also. This was reserved
2959 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2960 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2961 XFS_TRANS_DQ_BCOUNT,
2964 ap->rval = NULLFSBLOCK;
2971 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2972 * It figures out where to ask the underlying allocator to put the new extent.
2976 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2978 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
2979 return xfs_bmap_rtalloc(ap);
2980 return xfs_bmap_btalloc(ap);
2984 * Transform a btree format file with only one leaf node, where the
2985 * extents list will fit in the inode, into an extents format file.
2986 * Since the file extents are already in-core, all we have to do is
2987 * give up the space for the btree root and pitch the leaf block.
2989 STATIC int /* error */
2990 xfs_bmap_btree_to_extents(
2991 xfs_trans_t *tp, /* transaction pointer */
2992 xfs_inode_t *ip, /* incore inode pointer */
2993 xfs_btree_cur_t *cur, /* btree cursor */
2994 int *logflagsp, /* inode logging flags */
2995 int whichfork) /* data or attr fork */
2998 xfs_bmbt_block_t *cblock;/* child btree block */
2999 xfs_fsblock_t cbno; /* child block number */
3000 xfs_buf_t *cbp; /* child block's buffer */
3001 int error; /* error return value */
3002 xfs_ifork_t *ifp; /* inode fork data */
3003 xfs_mount_t *mp; /* mount point structure */
3004 __be64 *pp; /* ptr to block address */
3005 xfs_bmbt_block_t *rblock;/* root btree block */
3007 ifp = XFS_IFORK_PTR(ip, whichfork);
3008 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3009 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
3010 rblock = ifp->if_broot;
3011 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
3012 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
3013 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
3015 pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
3016 cbno = be64_to_cpu(*pp);
3019 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
3022 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
3023 XFS_BMAP_BTREE_REF)))
3025 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
3026 if ((error = xfs_btree_check_lblock(cur, cblock, 0, cbp)))
3028 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
3029 ip->i_d.di_nblocks--;
3030 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
3031 xfs_trans_binval(tp, cbp);
3032 if (cur->bc_bufs[0] == cbp)
3033 cur->bc_bufs[0] = NULL;
3034 xfs_iroot_realloc(ip, -1, whichfork);
3035 ASSERT(ifp->if_broot == NULL);
3036 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
3037 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3038 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
3043 * Called by xfs_bmapi to update file extent records and the btree
3044 * after removing space (or undoing a delayed allocation).
3046 STATIC int /* error */
3047 xfs_bmap_del_extent(
3048 xfs_inode_t *ip, /* incore inode pointer */
3049 xfs_trans_t *tp, /* current transaction pointer */
3050 xfs_extnum_t idx, /* extent number to update/delete */
3051 xfs_bmap_free_t *flist, /* list of extents to be freed */
3052 xfs_btree_cur_t *cur, /* if null, not a btree */
3053 xfs_bmbt_irec_t *del, /* data to remove from extents */
3054 int *logflagsp, /* inode logging flags */
3055 xfs_extdelta_t *delta, /* Change made to incore extents */
3056 int whichfork, /* data or attr fork */
3057 int rsvd) /* OK to allocate reserved blocks */
3059 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
3060 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
3061 xfs_fsblock_t del_endblock=0; /* first block past del */
3062 xfs_fileoff_t del_endoff; /* first offset past del */
3063 int delay; /* current block is delayed allocated */
3064 int do_fx; /* free extent at end of routine */
3065 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
3066 int error; /* error return value */
3067 int flags; /* inode logging flags */
3068 xfs_bmbt_irec_t got; /* current extent entry */
3069 xfs_fileoff_t got_endoff; /* first offset past got */
3070 int i; /* temp state */
3071 xfs_ifork_t *ifp; /* inode fork pointer */
3072 xfs_mount_t *mp; /* mount structure */
3073 xfs_filblks_t nblks; /* quota/sb block count */
3074 xfs_bmbt_irec_t new; /* new record to be inserted */
3076 uint qfield; /* quota field to update */
3077 xfs_filblks_t temp; /* for indirect length calculations */
3078 xfs_filblks_t temp2; /* for indirect length calculations */
3080 XFS_STATS_INC(xs_del_exlist);
3082 ifp = XFS_IFORK_PTR(ip, whichfork);
3083 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
3084 (uint)sizeof(xfs_bmbt_rec_t)));
3085 ASSERT(del->br_blockcount > 0);
3086 ep = xfs_iext_get_ext(ifp, idx);
3087 xfs_bmbt_get_all(ep, &got);
3088 ASSERT(got.br_startoff <= del->br_startoff);
3089 del_endoff = del->br_startoff + del->br_blockcount;
3090 got_endoff = got.br_startoff + got.br_blockcount;
3091 ASSERT(got_endoff >= del_endoff);
3092 delay = ISNULLSTARTBLOCK(got.br_startblock);
3093 ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
3098 * If deleting a real allocation, must free up the disk space.
3101 flags = XFS_ILOG_CORE;
3103 * Realtime allocation. Free it and record di_nblocks update.
3105 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
3109 ASSERT(do_mod(del->br_blockcount,
3110 mp->m_sb.sb_rextsize) == 0);
3111 ASSERT(do_mod(del->br_startblock,
3112 mp->m_sb.sb_rextsize) == 0);
3113 bno = del->br_startblock;
3114 len = del->br_blockcount;
3115 do_div(bno, mp->m_sb.sb_rextsize);
3116 do_div(len, mp->m_sb.sb_rextsize);
3117 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
3118 (xfs_extlen_t)len)))
3121 nblks = len * mp->m_sb.sb_rextsize;
3122 qfield = XFS_TRANS_DQ_RTBCOUNT;
3125 * Ordinary allocation.
3129 nblks = del->br_blockcount;
3130 qfield = XFS_TRANS_DQ_BCOUNT;
3133 * Set up del_endblock and cur for later.
3135 del_endblock = del->br_startblock + del->br_blockcount;
3137 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
3138 got.br_startblock, got.br_blockcount,
3141 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3143 da_old = da_new = 0;
3145 da_old = STARTBLOCKVAL(got.br_startblock);
3151 * Set flag value to use in switch statement.
3152 * Left-contig is 2, right-contig is 1.
3154 switch (((got.br_startoff == del->br_startoff) << 1) |
3155 (got_endoff == del_endoff)) {
3158 * Matches the whole extent. Delete the entry.
3160 XFS_BMAP_TRACE_DELETE("3", ip, idx, 1, whichfork);
3161 xfs_iext_remove(ifp, idx, 1);
3162 ifp->if_lastex = idx;
3165 XFS_IFORK_NEXT_SET(ip, whichfork,
3166 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
3167 flags |= XFS_ILOG_CORE;
3169 flags |= XFS_ILOG_FEXT(whichfork);
3172 if ((error = xfs_bmbt_delete(cur, &i)))
3174 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3179 * Deleting the first part of the extent.
3181 XFS_BMAP_TRACE_PRE_UPDATE("2", ip, idx, whichfork);
3182 xfs_bmbt_set_startoff(ep, del_endoff);
3183 temp = got.br_blockcount - del->br_blockcount;
3184 xfs_bmbt_set_blockcount(ep, temp);
3185 ifp->if_lastex = idx;
3187 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3189 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3190 XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx,
3195 xfs_bmbt_set_startblock(ep, del_endblock);
3196 XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx, whichfork);
3198 flags |= XFS_ILOG_FEXT(whichfork);
3201 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3202 got.br_blockcount - del->br_blockcount,
3209 * Deleting the last part of the extent.
3211 temp = got.br_blockcount - del->br_blockcount;
3212 XFS_BMAP_TRACE_PRE_UPDATE("1", ip, idx, whichfork);
3213 xfs_bmbt_set_blockcount(ep, temp);
3214 ifp->if_lastex = idx;
3216 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3218 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3219 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx,
3224 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx, whichfork);
3226 flags |= XFS_ILOG_FEXT(whichfork);
3229 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3231 got.br_blockcount - del->br_blockcount,
3238 * Deleting the middle of the extent.
3240 temp = del->br_startoff - got.br_startoff;
3241 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, whichfork);
3242 xfs_bmbt_set_blockcount(ep, temp);
3243 new.br_startoff = del_endoff;
3244 temp2 = got_endoff - del_endoff;
3245 new.br_blockcount = temp2;
3246 new.br_state = got.br_state;
3248 new.br_startblock = del_endblock;
3249 flags |= XFS_ILOG_CORE;
3251 if ((error = xfs_bmbt_update(cur,
3253 got.br_startblock, temp,
3256 if ((error = xfs_btree_increment(cur, 0, &i)))
3258 cur->bc_rec.b = new;
3259 error = xfs_bmbt_insert(cur, &i);
3260 if (error && error != ENOSPC)
3263 * If get no-space back from btree insert,
3264 * it tried a split, and we have a zero
3265 * block reservation.
3266 * Fix up our state and return the error.
3268 if (error == ENOSPC) {
3270 * Reset the cursor, don't trust
3271 * it after any insert operation.
3273 if ((error = xfs_bmbt_lookup_eq(cur,
3278 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3280 * Update the btree record back
3281 * to the original value.
3283 if ((error = xfs_bmbt_update(cur,
3290 * Reset the extent record back
3291 * to the original value.
3293 xfs_bmbt_set_blockcount(ep,
3296 error = XFS_ERROR(ENOSPC);
3299 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3301 flags |= XFS_ILOG_FEXT(whichfork);
3302 XFS_IFORK_NEXT_SET(ip, whichfork,
3303 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3305 ASSERT(whichfork == XFS_DATA_FORK);
3306 temp = xfs_bmap_worst_indlen(ip, temp);
3307 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3308 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3309 new.br_startblock = NULLSTARTBLOCK((int)temp2);
3310 da_new = temp + temp2;
3311 while (da_new > da_old) {
3315 xfs_bmbt_set_startblock(ep,
3316 NULLSTARTBLOCK((int)temp));
3318 if (da_new == da_old)
3324 NULLSTARTBLOCK((int)temp2);
3328 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, whichfork);
3329 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 1, &new, NULL,
3331 xfs_iext_insert(ifp, idx + 1, 1, &new);
3332 ifp->if_lastex = idx + 1;
3336 * If we need to, add to list of extents to delete.
3339 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3342 * Adjust inode # blocks in the file.
3345 ip->i_d.di_nblocks -= nblks;
3347 * Adjust quota data.
3350 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks);
3353 * Account for change in delayed indirect blocks.
3354 * Nothing to do for disk quota accounting here.
3356 ASSERT(da_old >= da_new);
3357 if (da_old > da_new)
3358 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
3361 /* DELTA: report the original extent. */
3362 if (delta->xed_startoff > got.br_startoff)
3363 delta->xed_startoff = got.br_startoff;
3364 if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
3365 delta->xed_blockcount = got.br_startoff +
3374 * Remove the entry "free" from the free item list. Prev points to the
3375 * previous entry, unless "free" is the head of the list.
3379 xfs_bmap_free_t *flist, /* free item list header */
3380 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3381 xfs_bmap_free_item_t *free) /* list item to be freed */
3384 prev->xbfi_next = free->xbfi_next;
3386 flist->xbf_first = free->xbfi_next;
3388 kmem_zone_free(xfs_bmap_free_item_zone, free);
3392 * Convert an extents-format file into a btree-format file.
3393 * The new file will have a root block (in the inode) and a single child block.
3395 STATIC int /* error */
3396 xfs_bmap_extents_to_btree(
3397 xfs_trans_t *tp, /* transaction pointer */
3398 xfs_inode_t *ip, /* incore inode pointer */
3399 xfs_fsblock_t *firstblock, /* first-block-allocated */
3400 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3401 xfs_btree_cur_t **curp, /* cursor returned to caller */
3402 int wasdel, /* converting a delayed alloc */
3403 int *logflagsp, /* inode logging flags */
3404 int whichfork) /* data or attr fork */
3406 xfs_bmbt_block_t *ablock; /* allocated (child) bt block */
3407 xfs_buf_t *abp; /* buffer for ablock */
3408 xfs_alloc_arg_t args; /* allocation arguments */
3409 xfs_bmbt_rec_t *arp; /* child record pointer */
3410 xfs_bmbt_block_t *block; /* btree root block */
3411 xfs_btree_cur_t *cur; /* bmap btree cursor */
3412 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3413 int error; /* error return value */
3414 xfs_extnum_t i, cnt; /* extent record index */
3415 xfs_ifork_t *ifp; /* inode fork pointer */
3416 xfs_bmbt_key_t *kp; /* root block key pointer */
3417 xfs_mount_t *mp; /* mount structure */
3418 xfs_extnum_t nextents; /* number of file extents */
3419 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3421 ifp = XFS_IFORK_PTR(ip, whichfork);
3422 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3423 ASSERT(ifp->if_ext_max ==
3424 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3426 * Make space in the inode incore.
3428 xfs_iroot_realloc(ip, 1, whichfork);
3429 ifp->if_flags |= XFS_IFBROOT;
3433 block = ifp->if_broot;
3434 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3435 block->bb_level = cpu_to_be16(1);
3436 block->bb_numrecs = cpu_to_be16(1);
3437 block->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3438 block->bb_rightsib = cpu_to_be64(NULLDFSBNO);
3440 * Need a cursor. Can't allocate until bb_level is filled in.
3443 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
3444 cur->bc_private.b.firstblock = *firstblock;
3445 cur->bc_private.b.flist = flist;
3446 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3448 * Convert to a btree with two levels, one record in root.
3450 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3453 args.firstblock = *firstblock;
3454 if (*firstblock == NULLFSBLOCK) {
3455 args.type = XFS_ALLOCTYPE_START_BNO;
3456 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3457 } else if (flist->xbf_low) {
3458 args.type = XFS_ALLOCTYPE_START_BNO;
3459 args.fsbno = *firstblock;
3461 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3462 args.fsbno = *firstblock;
3464 args.minlen = args.maxlen = args.prod = 1;
3465 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3466 args.minalignslop = 0;
3467 args.wasdel = wasdel;
3469 if ((error = xfs_alloc_vextent(&args))) {
3470 xfs_iroot_realloc(ip, -1, whichfork);
3471 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3475 * Allocation can't fail, the space was reserved.
3477 ASSERT(args.fsbno != NULLFSBLOCK);
3478 ASSERT(*firstblock == NULLFSBLOCK ||
3479 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3481 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3482 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3483 cur->bc_private.b.allocated++;
3484 ip->i_d.di_nblocks++;
3485 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3486 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3488 * Fill in the child block.
3490 ablock = XFS_BUF_TO_BMBT_BLOCK(abp);
3491 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3492 ablock->bb_level = 0;
3493 ablock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3494 ablock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
3495 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3496 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3497 for (cnt = i = 0; i < nextents; i++) {
3498 ep = xfs_iext_get_ext(ifp, i);
3499 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
3500 arp->l0 = cpu_to_be64(ep->l0);
3501 arp->l1 = cpu_to_be64(ep->l1);
3505 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3506 ablock->bb_numrecs = cpu_to_be16(cnt);
3508 * Fill in the root key and pointer.
3510 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
3511 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3512 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
3513 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
3514 *pp = cpu_to_be64(args.fsbno);
3516 * Do all this logging at the end so that
3517 * the root is at the right level.
3519 xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS);
3520 xfs_bmbt_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3521 ASSERT(*curp == NULL);
3523 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
3528 * Helper routine to reset inode di_forkoff field when switching
3529 * attribute fork from local to extent format - we reset it where
3530 * possible to make space available for inline data fork extents.
3533 xfs_bmap_forkoff_reset(
3538 if (whichfork == XFS_ATTR_FORK &&
3539 (ip->i_d.di_format != XFS_DINODE_FMT_DEV) &&
3540 (ip->i_d.di_format != XFS_DINODE_FMT_UUID) &&
3541 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
3542 ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) {
3543 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
3544 ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) /
3545 (uint)sizeof(xfs_bmbt_rec_t);
3546 ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) /
3547 (uint)sizeof(xfs_bmbt_rec_t);
3552 * Convert a local file to an extents file.
3553 * This code is out of bounds for data forks of regular files,
3554 * since the file data needs to get logged so things will stay consistent.
3555 * (The bmap-level manipulations are ok, though).
3557 STATIC int /* error */
3558 xfs_bmap_local_to_extents(
3559 xfs_trans_t *tp, /* transaction pointer */
3560 xfs_inode_t *ip, /* incore inode pointer */
3561 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3562 xfs_extlen_t total, /* total blocks needed by transaction */
3563 int *logflagsp, /* inode logging flags */
3564 int whichfork) /* data or attr fork */
3566 int error; /* error return value */
3567 int flags; /* logging flags returned */
3568 xfs_ifork_t *ifp; /* inode fork pointer */
3571 * We don't want to deal with the case of keeping inode data inline yet.
3572 * So sending the data fork of a regular inode is invalid.
3574 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3575 whichfork == XFS_DATA_FORK));
3576 ifp = XFS_IFORK_PTR(ip, whichfork);
3577 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3580 if (ifp->if_bytes) {
3581 xfs_alloc_arg_t args; /* allocation arguments */
3582 xfs_buf_t *bp; /* buffer for extent block */
3583 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
3586 args.mp = ip->i_mount;
3587 args.firstblock = *firstblock;
3588 ASSERT((ifp->if_flags &
3589 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
3591 * Allocate a block. We know we need only one, since the
3592 * file currently fits in an inode.
3594 if (*firstblock == NULLFSBLOCK) {
3595 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3596 args.type = XFS_ALLOCTYPE_START_BNO;
3598 args.fsbno = *firstblock;
3599 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3602 args.mod = args.minleft = args.alignment = args.wasdel =
3603 args.isfl = args.minalignslop = 0;
3604 args.minlen = args.maxlen = args.prod = 1;
3605 if ((error = xfs_alloc_vextent(&args)))
3608 * Can't fail, the space was reserved.
3610 ASSERT(args.fsbno != NULLFSBLOCK);
3611 ASSERT(args.len == 1);
3612 *firstblock = args.fsbno;
3613 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3614 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3616 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3617 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3618 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3619 xfs_iext_add(ifp, 0, 1);
3620 ep = xfs_iext_get_ext(ifp, 0);
3621 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3622 XFS_BMAP_TRACE_POST_UPDATE("new", ip, 0, whichfork);
3623 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3624 ip->i_d.di_nblocks = 1;
3625 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
3626 XFS_TRANS_DQ_BCOUNT, 1L);
3627 flags |= XFS_ILOG_FEXT(whichfork);
3629 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3630 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3632 ifp->if_flags &= ~XFS_IFINLINE;
3633 ifp->if_flags |= XFS_IFEXTENTS;
3634 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3635 flags |= XFS_ILOG_CORE;
3642 * Search the extent records for the entry containing block bno.
3643 * If bno lies in a hole, point to the next entry. If bno lies
3644 * past eof, *eofp will be set, and *prevp will contain the last
3645 * entry (null if none). Else, *lastxp will be set to the index
3646 * of the found entry; *gotp will contain the entry.
3648 xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3649 xfs_bmap_search_multi_extents(
3650 xfs_ifork_t *ifp, /* inode fork pointer */
3651 xfs_fileoff_t bno, /* block number searched for */
3652 int *eofp, /* out: end of file found */
3653 xfs_extnum_t *lastxp, /* out: last extent index */
3654 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3655 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3657 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3658 xfs_extnum_t lastx; /* last extent index */
3661 * Initialize the extent entry structure to catch access to
3662 * uninitialized br_startblock field.
3664 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3665 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3666 gotp->br_state = XFS_EXT_INVALID;
3668 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3670 gotp->br_startblock = 0xffffa5a5;
3672 prevp->br_startoff = NULLFILEOFF;
3674 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3676 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3678 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3679 xfs_bmbt_get_all(ep, gotp);
3693 * Search the extents list for the inode, for the extent containing bno.
3694 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3695 * *eofp will be set, and *prevp will contain the last entry (null if none).
3696 * Else, *lastxp will be set to the index of the found
3697 * entry; *gotp will contain the entry.
3699 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3700 xfs_bmap_search_extents(
3701 xfs_inode_t *ip, /* incore inode pointer */
3702 xfs_fileoff_t bno, /* block number searched for */
3703 int fork, /* data or attr fork */
3704 int *eofp, /* out: end of file found */
3705 xfs_extnum_t *lastxp, /* out: last extent index */
3706 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3707 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3709 xfs_ifork_t *ifp; /* inode fork pointer */
3710 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3712 XFS_STATS_INC(xs_look_exlist);
3713 ifp = XFS_IFORK_PTR(ip, fork);
3715 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3717 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3718 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3719 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3720 "Access to block zero in inode %llu "
3721 "start_block: %llx start_off: %llx "
3722 "blkcnt: %llx extent-state: %x lastx: %x\n",
3723 (unsigned long long)ip->i_ino,
3724 (unsigned long long)gotp->br_startblock,
3725 (unsigned long long)gotp->br_startoff,
3726 (unsigned long long)gotp->br_blockcount,
3727 gotp->br_state, *lastxp);
3728 *lastxp = NULLEXTNUM;
3736 #ifdef XFS_BMAP_TRACE
3737 ktrace_t *xfs_bmap_trace_buf;
3740 * Add a bmap trace buffer entry. Base routine for the others.
3743 xfs_bmap_trace_addentry(
3744 int opcode, /* operation */
3745 const char *fname, /* function name */
3746 char *desc, /* operation description */
3747 xfs_inode_t *ip, /* incore inode pointer */
3748 xfs_extnum_t idx, /* index of entry(ies) */
3749 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
3750 xfs_bmbt_rec_host_t *r1, /* first record */
3751 xfs_bmbt_rec_host_t *r2, /* second record or null */
3752 int whichfork) /* data or attr fork */
3754 xfs_bmbt_rec_host_t tr2;
3756 ASSERT(cnt == 1 || cnt == 2);
3761 memset(&tr2, 0, sizeof(tr2));
3764 ktrace_enter(xfs_bmap_trace_buf,
3765 (void *)(__psint_t)(opcode | (whichfork << 16)),
3766 (void *)fname, (void *)desc, (void *)ip,
3767 (void *)(__psint_t)idx,
3768 (void *)(__psint_t)cnt,
3769 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3770 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3771 (void *)(__psunsigned_t)(r1->l0 >> 32),
3772 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3773 (void *)(__psunsigned_t)(r1->l1 >> 32),
3774 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3775 (void *)(__psunsigned_t)(r2->l0 >> 32),
3776 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3777 (void *)(__psunsigned_t)(r2->l1 >> 32),
3778 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3780 ASSERT(ip->i_xtrace);
3781 ktrace_enter(ip->i_xtrace,
3782 (void *)(__psint_t)(opcode | (whichfork << 16)),
3783 (void *)fname, (void *)desc, (void *)ip,
3784 (void *)(__psint_t)idx,
3785 (void *)(__psint_t)cnt,
3786 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3787 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3788 (void *)(__psunsigned_t)(r1->l0 >> 32),
3789 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3790 (void *)(__psunsigned_t)(r1->l1 >> 32),
3791 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3792 (void *)(__psunsigned_t)(r2->l0 >> 32),
3793 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3794 (void *)(__psunsigned_t)(r2->l1 >> 32),
3795 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3800 * Add bmap trace entry prior to a call to xfs_iext_remove.
3803 xfs_bmap_trace_delete(
3804 const char *fname, /* function name */
3805 char *desc, /* operation description */
3806 xfs_inode_t *ip, /* incore inode pointer */
3807 xfs_extnum_t idx, /* index of entry(entries) deleted */
3808 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
3809 int whichfork) /* data or attr fork */
3811 xfs_ifork_t *ifp; /* inode fork pointer */
3813 ifp = XFS_IFORK_PTR(ip, whichfork);
3814 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
3815 cnt, xfs_iext_get_ext(ifp, idx),
3816 cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL,
3821 * Add bmap trace entry prior to a call to xfs_iext_insert, or
3822 * reading in the extents list from the disk (in the btree).
3825 xfs_bmap_trace_insert(
3826 const char *fname, /* function name */
3827 char *desc, /* operation description */
3828 xfs_inode_t *ip, /* incore inode pointer */
3829 xfs_extnum_t idx, /* index of entry(entries) inserted */
3830 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
3831 xfs_bmbt_irec_t *r1, /* inserted record 1 */
3832 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
3833 int whichfork) /* data or attr fork */
3835 xfs_bmbt_rec_host_t tr1; /* compressed record 1 */
3836 xfs_bmbt_rec_host_t tr2; /* compressed record 2 if needed */
3838 xfs_bmbt_set_all(&tr1, r1);
3841 xfs_bmbt_set_all(&tr2, r2);
3846 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
3847 cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
3851 * Add bmap trace entry after updating an extent record in place.
3854 xfs_bmap_trace_post_update(
3855 const char *fname, /* function name */
3856 char *desc, /* operation description */
3857 xfs_inode_t *ip, /* incore inode pointer */
3858 xfs_extnum_t idx, /* index of entry updated */
3859 int whichfork) /* data or attr fork */
3861 xfs_ifork_t *ifp; /* inode fork pointer */
3863 ifp = XFS_IFORK_PTR(ip, whichfork);
3864 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
3865 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3869 * Add bmap trace entry prior to updating an extent record in place.
3872 xfs_bmap_trace_pre_update(
3873 const char *fname, /* function name */
3874 char *desc, /* operation description */
3875 xfs_inode_t *ip, /* incore inode pointer */
3876 xfs_extnum_t idx, /* index of entry to be updated */
3877 int whichfork) /* data or attr fork */
3879 xfs_ifork_t *ifp; /* inode fork pointer */
3881 ifp = XFS_IFORK_PTR(ip, whichfork);
3882 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
3883 xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3885 #endif /* XFS_BMAP_TRACE */
3888 * Compute the worst-case number of indirect blocks that will be used
3889 * for ip's delayed extent of length "len".
3891 STATIC xfs_filblks_t
3892 xfs_bmap_worst_indlen(
3893 xfs_inode_t *ip, /* incore inode pointer */
3894 xfs_filblks_t len) /* delayed extent length */
3896 int level; /* btree level number */
3897 int maxrecs; /* maximum record count at this level */
3898 xfs_mount_t *mp; /* mount structure */
3899 xfs_filblks_t rval; /* return value */
3902 maxrecs = mp->m_bmap_dmxr[0];
3903 for (level = 0, rval = 0;
3904 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3907 do_div(len, maxrecs);
3910 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3913 maxrecs = mp->m_bmap_dmxr[1];
3918 #if defined(XFS_RW_TRACE)
3927 if (ip->i_rwtrace == NULL)
3929 ktrace_enter(ip->i_rwtrace,
3930 (void *)(__psint_t)XFS_BUNMAP,
3932 (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
3933 (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
3934 (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
3935 (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
3936 (void *)(__psint_t)len,
3937 (void *)(__psint_t)flags,
3938 (void *)(unsigned long)current_cpu(),
3950 * Convert inode from non-attributed to attributed.
3951 * Must not be in a transaction, ip must not be locked.
3953 int /* error code */
3954 xfs_bmap_add_attrfork(
3955 xfs_inode_t *ip, /* incore inode pointer */
3956 int size, /* space new attribute needs */
3957 int rsvd) /* xact may use reserved blks */
3959 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
3960 xfs_bmap_free_t flist; /* freed extent records */
3961 xfs_mount_t *mp; /* mount structure */
3962 xfs_trans_t *tp; /* transaction pointer */
3963 int blks; /* space reservation */
3964 int version = 1; /* superblock attr version */
3965 int committed; /* xaction was committed */
3966 int logflags; /* logging flags */
3967 int error; /* error return value */
3969 ASSERT(XFS_IFORK_Q(ip) == 0);
3970 ASSERT(ip->i_df.if_ext_max ==
3971 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3974 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3975 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3976 blks = XFS_ADDAFORK_SPACE_RES(mp);
3978 tp->t_flags |= XFS_TRANS_RESERVE;
3979 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3980 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3982 xfs_ilock(ip, XFS_ILOCK_EXCL);
3983 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ?
3984 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3985 XFS_QMOPT_RES_REGBLKS);
3987 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3988 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3991 if (XFS_IFORK_Q(ip))
3993 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3995 * For inodes coming from pre-6.2 filesystems.
3997 ASSERT(ip->i_d.di_aformat == 0);
3998 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
4000 ASSERT(ip->i_d.di_anextents == 0);
4002 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4003 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4004 switch (ip->i_d.di_format) {
4005 case XFS_DINODE_FMT_DEV:
4006 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
4008 case XFS_DINODE_FMT_UUID:
4009 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
4011 case XFS_DINODE_FMT_LOCAL:
4012 case XFS_DINODE_FMT_EXTENTS:
4013 case XFS_DINODE_FMT_BTREE:
4014 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
4015 if (!ip->i_d.di_forkoff)
4016 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
4017 else if (mp->m_flags & XFS_MOUNT_ATTR2)
4022 error = XFS_ERROR(EINVAL);
4025 ip->i_df.if_ext_max =
4026 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4027 ASSERT(ip->i_afp == NULL);
4028 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
4029 ip->i_afp->if_ext_max =
4030 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4031 ip->i_afp->if_flags = XFS_IFEXTENTS;
4033 XFS_BMAP_INIT(&flist, &firstblock);
4034 switch (ip->i_d.di_format) {
4035 case XFS_DINODE_FMT_LOCAL:
4036 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
4039 case XFS_DINODE_FMT_EXTENTS:
4040 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
4043 case XFS_DINODE_FMT_BTREE:
4044 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
4052 xfs_trans_log_inode(tp, ip, logflags);
4055 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
4056 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
4057 __int64_t sbfields = 0;
4059 spin_lock(&mp->m_sb_lock);
4060 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
4061 xfs_sb_version_addattr(&mp->m_sb);
4062 sbfields |= XFS_SB_VERSIONNUM;
4064 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
4065 xfs_sb_version_addattr2(&mp->m_sb);
4066 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
4069 spin_unlock(&mp->m_sb_lock);
4070 xfs_mod_sb(tp, sbfields);
4072 spin_unlock(&mp->m_sb_lock);
4074 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
4076 error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES);
4077 ASSERT(ip->i_df.if_ext_max ==
4078 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4081 xfs_bmap_cancel(&flist);
4083 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4085 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
4086 ASSERT(ip->i_df.if_ext_max ==
4087 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4092 * Add the extent to the list of extents to be free at transaction end.
4093 * The list is maintained sorted (by block number).
4098 xfs_fsblock_t bno, /* fs block number of extent */
4099 xfs_filblks_t len, /* length of extent */
4100 xfs_bmap_free_t *flist, /* list of extents */
4101 xfs_mount_t *mp) /* mount point structure */
4103 xfs_bmap_free_item_t *cur; /* current (next) element */
4104 xfs_bmap_free_item_t *new; /* new element */
4105 xfs_bmap_free_item_t *prev; /* previous element */
4107 xfs_agnumber_t agno;
4108 xfs_agblock_t agbno;
4110 ASSERT(bno != NULLFSBLOCK);
4112 ASSERT(len <= MAXEXTLEN);
4113 ASSERT(!ISNULLSTARTBLOCK(bno));
4114 agno = XFS_FSB_TO_AGNO(mp, bno);
4115 agbno = XFS_FSB_TO_AGBNO(mp, bno);
4116 ASSERT(agno < mp->m_sb.sb_agcount);
4117 ASSERT(agbno < mp->m_sb.sb_agblocks);
4118 ASSERT(len < mp->m_sb.sb_agblocks);
4119 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
4121 ASSERT(xfs_bmap_free_item_zone != NULL);
4122 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
4123 new->xbfi_startblock = bno;
4124 new->xbfi_blockcount = (xfs_extlen_t)len;
4125 for (prev = NULL, cur = flist->xbf_first;
4127 prev = cur, cur = cur->xbfi_next) {
4128 if (cur->xbfi_startblock >= bno)
4132 prev->xbfi_next = new;
4134 flist->xbf_first = new;
4135 new->xbfi_next = cur;
4140 * Compute and fill in the value of the maximum depth of a bmap btree
4141 * in this filesystem. Done once, during mount.
4144 xfs_bmap_compute_maxlevels(
4145 xfs_mount_t *mp, /* file system mount structure */
4146 int whichfork) /* data or attr fork */
4148 int level; /* btree level */
4149 uint maxblocks; /* max blocks at this level */
4150 uint maxleafents; /* max leaf entries possible */
4151 int maxrootrecs; /* max records in root block */
4152 int minleafrecs; /* min records in leaf block */
4153 int minnoderecs; /* min records in node block */
4154 int sz; /* root block size */
4157 * The maximum number of extents in a file, hence the maximum
4158 * number of leaf entries, is controlled by the type of di_nextents
4159 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
4160 * (a signed 16-bit number, xfs_aextnum_t).
4162 * Note that we can no longer assume that if we are in ATTR1 that
4163 * the fork offset of all the inodes will be (m_attroffset >> 3)
4164 * because we could have mounted with ATTR2 and then mounted back
4165 * with ATTR1, keeping the di_forkoff's fixed but probably at
4166 * various positions. Therefore, for both ATTR1 and ATTR2
4167 * we have to assume the worst case scenario of a minimum size
4170 if (whichfork == XFS_DATA_FORK) {
4171 maxleafents = MAXEXTNUM;
4172 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
4174 maxleafents = MAXAEXTNUM;
4175 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
4177 maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
4178 minleafrecs = mp->m_bmap_dmnr[0];
4179 minnoderecs = mp->m_bmap_dmnr[1];
4180 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
4181 for (level = 1; maxblocks > 1; level++) {
4182 if (maxblocks <= maxrootrecs)
4185 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
4187 mp->m_bm_maxlevels[whichfork] = level;
4191 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
4192 * caller. Frees all the extents that need freeing, which must be done
4193 * last due to locking considerations. We never free any extents in
4194 * the first transaction. This is to allow the caller to make the first
4195 * transaction a synchronous one so that the pointers to the data being
4196 * broken in this transaction will be permanent before the data is actually
4197 * freed. This is necessary to prevent blocks from being reallocated
4198 * and written to before the free and reallocation are actually permanent.
4199 * We do not just make the first transaction synchronous here, because
4200 * there are more efficient ways to gain the same protection in some cases
4201 * (see the file truncation code).
4203 * Return 1 if the given transaction was committed and a new one
4204 * started, and 0 otherwise in the committed parameter.
4209 xfs_trans_t **tp, /* transaction pointer addr */
4210 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4211 int *committed) /* xact committed or not */
4213 xfs_efd_log_item_t *efd; /* extent free data */
4214 xfs_efi_log_item_t *efi; /* extent free intention */
4215 int error; /* error return value */
4216 xfs_bmap_free_item_t *free; /* free extent item */
4217 unsigned int logres; /* new log reservation */
4218 unsigned int logcount; /* new log count */
4219 xfs_mount_t *mp; /* filesystem mount structure */
4220 xfs_bmap_free_item_t *next; /* next item on free list */
4221 xfs_trans_t *ntp; /* new transaction pointer */
4223 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
4224 if (flist->xbf_count == 0) {
4229 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
4230 for (free = flist->xbf_first; free; free = free->xbfi_next)
4231 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
4232 free->xbfi_blockcount);
4233 logres = ntp->t_log_res;
4234 logcount = ntp->t_log_count;
4235 ntp = xfs_trans_dup(*tp);
4236 error = xfs_trans_commit(*tp, 0);
4240 * We have a new transaction, so we should return committed=1,
4241 * even though we're returning an error.
4246 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
4249 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
4250 for (free = flist->xbf_first; free != NULL; free = next) {
4251 next = free->xbfi_next;
4252 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4253 free->xbfi_blockcount))) {
4255 * The bmap free list will be cleaned up at a
4256 * higher level. The EFI will be canceled when
4257 * this transaction is aborted.
4258 * Need to force shutdown here to make sure it
4259 * happens, since this transaction may not be
4263 if (!XFS_FORCED_SHUTDOWN(mp))
4264 xfs_force_shutdown(mp,
4265 (error == EFSCORRUPTED) ?
4266 SHUTDOWN_CORRUPT_INCORE :
4267 SHUTDOWN_META_IO_ERROR);
4270 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4271 free->xbfi_blockcount);
4272 xfs_bmap_del_free(flist, NULL, free);
4278 * Free up any items left in the list.
4282 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4284 xfs_bmap_free_item_t *free; /* free list item */
4285 xfs_bmap_free_item_t *next;
4287 if (flist->xbf_count == 0)
4289 ASSERT(flist->xbf_first != NULL);
4290 for (free = flist->xbf_first; free; free = next) {
4291 next = free->xbfi_next;
4292 xfs_bmap_del_free(flist, NULL, free);
4294 ASSERT(flist->xbf_count == 0);
4298 * Returns the file-relative block number of the first unused block(s)
4299 * in the file with at least "len" logically contiguous blocks free.
4300 * This is the lowest-address hole if the file has holes, else the first block
4301 * past the end of file.
4302 * Return 0 if the file is currently local (in-inode).
4305 xfs_bmap_first_unused(
4306 xfs_trans_t *tp, /* transaction pointer */
4307 xfs_inode_t *ip, /* incore inode */
4308 xfs_extlen_t len, /* size of hole to find */
4309 xfs_fileoff_t *first_unused, /* unused block */
4310 int whichfork) /* data or attr fork */
4312 int error; /* error return value */
4313 int idx; /* extent record index */
4314 xfs_ifork_t *ifp; /* inode fork pointer */
4315 xfs_fileoff_t lastaddr; /* last block number seen */
4316 xfs_fileoff_t lowest; /* lowest useful block */
4317 xfs_fileoff_t max; /* starting useful block */
4318 xfs_fileoff_t off; /* offset for this block */
4319 xfs_extnum_t nextents; /* number of extent entries */
4321 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4322 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4323 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4324 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4328 ifp = XFS_IFORK_PTR(ip, whichfork);
4329 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4330 (error = xfs_iread_extents(tp, ip, whichfork)))
4332 lowest = *first_unused;
4333 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4334 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
4335 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
4336 off = xfs_bmbt_get_startoff(ep);
4338 * See if the hole before this extent will work.
4340 if (off >= lowest + len && off - max >= len) {
4341 *first_unused = max;
4344 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4345 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4347 *first_unused = max;
4352 * Returns the file-relative block number of the last block + 1 before
4353 * last_block (input value) in the file.
4354 * This is not based on i_size, it is based on the extent records.
4355 * Returns 0 for local files, as they do not have extent records.
4358 xfs_bmap_last_before(
4359 xfs_trans_t *tp, /* transaction pointer */
4360 xfs_inode_t *ip, /* incore inode */
4361 xfs_fileoff_t *last_block, /* last block */
4362 int whichfork) /* data or attr fork */
4364 xfs_fileoff_t bno; /* input file offset */
4365 int eof; /* hit end of file */
4366 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
4367 int error; /* error return value */
4368 xfs_bmbt_irec_t got; /* current extent value */
4369 xfs_ifork_t *ifp; /* inode fork pointer */
4370 xfs_extnum_t lastx; /* last extent used */
4371 xfs_bmbt_irec_t prev; /* previous extent value */
4373 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4374 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4375 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4376 return XFS_ERROR(EIO);
4377 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4381 ifp = XFS_IFORK_PTR(ip, whichfork);
4382 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4383 (error = xfs_iread_extents(tp, ip, whichfork)))
4385 bno = *last_block - 1;
4386 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4388 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4389 if (prev.br_startoff == NULLFILEOFF)
4392 *last_block = prev.br_startoff + prev.br_blockcount;
4395 * Otherwise *last_block is already the right answer.
4401 * Returns the file-relative block number of the first block past eof in
4402 * the file. This is not based on i_size, it is based on the extent records.
4403 * Returns 0 for local files, as they do not have extent records.
4406 xfs_bmap_last_offset(
4407 xfs_trans_t *tp, /* transaction pointer */
4408 xfs_inode_t *ip, /* incore inode */
4409 xfs_fileoff_t *last_block, /* last block */
4410 int whichfork) /* data or attr fork */
4412 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
4413 int error; /* error return value */
4414 xfs_ifork_t *ifp; /* inode fork pointer */
4415 xfs_extnum_t nextents; /* number of extent entries */
4417 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4418 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4419 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4420 return XFS_ERROR(EIO);
4421 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4425 ifp = XFS_IFORK_PTR(ip, whichfork);
4426 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4427 (error = xfs_iread_extents(tp, ip, whichfork)))
4429 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4434 ep = xfs_iext_get_ext(ifp, nextents - 1);
4435 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4440 * Returns whether the selected fork of the inode has exactly one
4441 * block or not. For the data fork we check this matches di_size,
4442 * implying the file's range is 0..bsize-1.
4444 int /* 1=>1 block, 0=>otherwise */
4446 xfs_inode_t *ip, /* incore inode */
4447 int whichfork) /* data or attr fork */
4449 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
4450 xfs_ifork_t *ifp; /* inode fork pointer */
4451 int rval; /* return value */
4452 xfs_bmbt_irec_t s; /* internal version of extent */
4455 if (whichfork == XFS_DATA_FORK) {
4456 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4457 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4458 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4461 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4463 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4465 ifp = XFS_IFORK_PTR(ip, whichfork);
4466 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4467 ep = xfs_iext_get_ext(ifp, 0);
4468 xfs_bmbt_get_all(ep, &s);
4469 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4470 if (rval && whichfork == XFS_DATA_FORK)
4471 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
4476 * Read in the extents to if_extents.
4477 * All inode fields are set up by caller, we just traverse the btree
4478 * and copy the records in. If the file system cannot contain unwritten
4479 * extents, the records are checked for no "state" flags.
4482 xfs_bmap_read_extents(
4483 xfs_trans_t *tp, /* transaction pointer */
4484 xfs_inode_t *ip, /* incore inode */
4485 int whichfork) /* data or attr fork */
4487 xfs_bmbt_block_t *block; /* current btree block */
4488 xfs_fsblock_t bno; /* block # of "block" */
4489 xfs_buf_t *bp; /* buffer for "block" */
4490 int error; /* error return value */
4491 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4492 xfs_extnum_t i, j; /* index into the extents list */
4493 xfs_ifork_t *ifp; /* fork structure */
4494 int level; /* btree level, for checking */
4495 xfs_mount_t *mp; /* file system mount structure */
4496 __be64 *pp; /* pointer to block address */
4498 xfs_extnum_t room; /* number of entries there's room for */
4502 ifp = XFS_IFORK_PTR(ip, whichfork);
4503 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4504 XFS_EXTFMT_INODE(ip);
4505 block = ifp->if_broot;
4507 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4509 level = be16_to_cpu(block->bb_level);
4511 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
4512 bno = be64_to_cpu(*pp);
4513 ASSERT(bno != NULLDFSBNO);
4514 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4515 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
4517 * Go down the tree until leaf level is reached, following the first
4518 * pointer (leftmost) at each level.
4520 while (level-- > 0) {
4521 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4522 XFS_BMAP_BTREE_REF)))
4524 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4525 XFS_WANT_CORRUPTED_GOTO(
4526 XFS_BMAP_SANITY_CHECK(mp, block, level),
4530 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
4531 bno = be64_to_cpu(*pp);
4532 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
4533 xfs_trans_brelse(tp, bp);
4536 * Here with bp and block set to the leftmost leaf node in the tree.
4538 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4541 * Loop over all leaf nodes. Copy information to the extent records.
4544 xfs_bmbt_rec_t *frp;
4545 xfs_fsblock_t nextbno;
4546 xfs_extnum_t num_recs;
4550 num_recs = be16_to_cpu(block->bb_numrecs);
4551 if (unlikely(i + num_recs > room)) {
4552 ASSERT(i + num_recs <= room);
4553 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4554 "corrupt dinode %Lu, (btree extents).",
4555 (unsigned long long) ip->i_ino);
4556 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4561 XFS_WANT_CORRUPTED_GOTO(
4562 XFS_BMAP_SANITY_CHECK(mp, block, 0),
4565 * Read-ahead the next leaf block, if any.
4567 nextbno = be64_to_cpu(block->bb_rightsib);
4568 if (nextbno != NULLFSBLOCK)
4569 xfs_btree_reada_bufl(mp, nextbno, 1);
4571 * Copy records into the extent records.
4573 frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
4575 for (j = 0; j < num_recs; j++, i++, frp++) {
4576 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
4577 trp->l0 = be64_to_cpu(frp->l0);
4578 trp->l1 = be64_to_cpu(frp->l1);
4580 if (exntf == XFS_EXTFMT_NOSTATE) {
4582 * Check all attribute bmap btree records and
4583 * any "older" data bmap btree records for a
4584 * set bit in the "extent flag" position.
4586 if (unlikely(xfs_check_nostate_extents(ifp,
4587 start, num_recs))) {
4588 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4594 xfs_trans_brelse(tp, bp);
4597 * If we've reached the end, stop.
4599 if (bno == NULLFSBLOCK)
4601 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4602 XFS_BMAP_BTREE_REF)))
4604 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4606 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4607 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4608 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
4611 xfs_trans_brelse(tp, bp);
4612 return XFS_ERROR(EFSCORRUPTED);
4615 #ifdef XFS_BMAP_TRACE
4617 * Add bmap trace insert entries for all the contents of the extent records.
4620 xfs_bmap_trace_exlist(
4621 const char *fname, /* function name */
4622 xfs_inode_t *ip, /* incore inode pointer */
4623 xfs_extnum_t cnt, /* count of entries in the list */
4624 int whichfork) /* data or attr fork */
4626 xfs_bmbt_rec_host_t *ep; /* current extent record */
4627 xfs_extnum_t idx; /* extent record index */
4628 xfs_ifork_t *ifp; /* inode fork pointer */
4629 xfs_bmbt_irec_t s; /* file extent record */
4631 ifp = XFS_IFORK_PTR(ip, whichfork);
4632 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4633 for (idx = 0; idx < cnt; idx++) {
4634 ep = xfs_iext_get_ext(ifp, idx);
4635 xfs_bmbt_get_all(ep, &s);
4636 XFS_BMAP_TRACE_INSERT("exlist", ip, idx, 1, &s, NULL,
4644 * Validate that the bmbt_irecs being returned from bmapi are valid
4645 * given the callers original parameters. Specifically check the
4646 * ranges of the returned irecs to ensure that they only extent beyond
4647 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4650 xfs_bmap_validate_ret(
4654 xfs_bmbt_irec_t *mval,
4658 int i; /* index to map values */
4660 ASSERT(ret_nmap <= nmap);
4662 for (i = 0; i < ret_nmap; i++) {
4663 ASSERT(mval[i].br_blockcount > 0);
4664 if (!(flags & XFS_BMAPI_ENTIRE)) {
4665 ASSERT(mval[i].br_startoff >= bno);
4666 ASSERT(mval[i].br_blockcount <= len);
4667 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4670 ASSERT(mval[i].br_startoff < bno + len);
4671 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4675 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4676 mval[i].br_startoff);
4677 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4678 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4679 mval[i].br_startblock != HOLESTARTBLOCK);
4680 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4681 mval[i].br_state == XFS_EXT_UNWRITTEN);
4688 * Map file blocks to filesystem blocks.
4689 * File range is given by the bno/len pair.
4690 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4691 * into a hole or past eof.
4692 * Only allocates blocks from a single allocation group,
4693 * to avoid locking problems.
4694 * The returned value in "firstblock" from the first call in a transaction
4695 * must be remembered and presented to subsequent calls in "firstblock".
4696 * An upper bound for the number of blocks to be allocated is supplied to
4697 * the first call in "total"; if no allocation group has that many free
4698 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4702 xfs_trans_t *tp, /* transaction pointer */
4703 xfs_inode_t *ip, /* incore inode */
4704 xfs_fileoff_t bno, /* starting file offs. mapped */
4705 xfs_filblks_t len, /* length to map in file */
4706 int flags, /* XFS_BMAPI_... */
4707 xfs_fsblock_t *firstblock, /* first allocated block
4708 controls a.g. for allocs */
4709 xfs_extlen_t total, /* total blocks needed */
4710 xfs_bmbt_irec_t *mval, /* output: map values */
4711 int *nmap, /* i/o: mval size/count */
4712 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4713 xfs_extdelta_t *delta) /* o: change made to incore extents */
4715 xfs_fsblock_t abno; /* allocated block number */
4716 xfs_extlen_t alen; /* allocated extent length */
4717 xfs_fileoff_t aoff; /* allocated file offset */
4718 xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
4719 xfs_btree_cur_t *cur; /* bmap btree cursor */
4720 xfs_fileoff_t end; /* end of mapped file region */
4721 int eof; /* we've hit the end of extents */
4722 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
4723 int error; /* error return */
4724 xfs_bmbt_irec_t got; /* current file extent record */
4725 xfs_ifork_t *ifp; /* inode fork pointer */
4726 xfs_extlen_t indlen; /* indirect blocks length */
4727 xfs_extnum_t lastx; /* last useful extent number */
4728 int logflags; /* flags for transaction logging */
4729 xfs_extlen_t minleft; /* min blocks left after allocation */
4730 xfs_extlen_t minlen; /* min allocation size */
4731 xfs_mount_t *mp; /* xfs mount structure */
4732 int n; /* current extent index */
4733 int nallocs; /* number of extents alloc\'d */
4734 xfs_extnum_t nextents; /* number of extents in file */
4735 xfs_fileoff_t obno; /* old block number (offset) */
4736 xfs_bmbt_irec_t prev; /* previous file extent record */
4737 int tmp_logflags; /* temp flags holder */
4738 int whichfork; /* data or attr fork */
4739 char inhole; /* current location is hole in file */
4740 char wasdelay; /* old extent was delayed */
4741 char wr; /* this is a write request */
4742 char rt; /* this is a realtime file */
4744 xfs_fileoff_t orig_bno; /* original block number value */
4745 int orig_flags; /* original flags arg value */
4746 xfs_filblks_t orig_len; /* original value of len arg */
4747 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4748 int orig_nmap; /* original value of *nmap */
4757 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4758 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4759 XFS_ATTR_FORK : XFS_DATA_FORK;
4761 if (unlikely(XFS_TEST_ERROR(
4762 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4763 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4764 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4765 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4766 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4767 return XFS_ERROR(EFSCORRUPTED);
4769 if (XFS_FORCED_SHUTDOWN(mp))
4770 return XFS_ERROR(EIO);
4771 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4772 ifp = XFS_IFORK_PTR(ip, whichfork);
4773 ASSERT(ifp->if_ext_max ==
4774 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4775 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4776 XFS_STATS_INC(xs_blk_mapw);
4778 XFS_STATS_INC(xs_blk_mapr);
4780 * IGSTATE flag is used to combine extents which
4781 * differ only due to the state of the extents.
4782 * This technique is used from xfs_getbmap()
4783 * when the caller does not wish to see the
4784 * separation (which is the default).
4786 * This technique is also used when writing a
4787 * buffer which has been partially written,
4788 * (usually by being flushed during a chunkread),
4789 * to ensure one write takes place. This also
4790 * prevents a change in the xfs inode extents at
4791 * this time, intentionally. This change occurs
4792 * on completion of the write operation, in
4793 * xfs_strat_comp(), where the xfs_bmapi() call
4794 * is transactioned, and the extents combined.
4796 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4797 wr = 0; /* no allocations are allowed */
4798 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
4802 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4804 if ((error = xfs_bmap_local_to_extents(tp, ip,
4805 firstblock, total, &logflags, whichfork)))
4808 if (wr && *firstblock == NULLFSBLOCK) {
4809 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4810 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4815 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4816 (error = xfs_iread_extents(tp, ip, whichfork)))
4818 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4820 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4826 delta->xed_startoff = NULLFILEOFF;
4827 delta->xed_blockcount = 0;
4829 while (bno < end && n < *nmap) {
4831 * Reading past eof, act as though there's a hole
4835 got.br_startoff = end;
4836 inhole = eof || got.br_startoff > bno;
4837 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
4838 ISNULLSTARTBLOCK(got.br_startblock);
4840 * First, deal with the hole before the allocated space
4841 * that we found, if any.
4843 if (wr && (inhole || wasdelay)) {
4845 * For the wasdelay case, we could also just
4846 * allocate the stuff asked for in this bmap call
4847 * but that wouldn't be as good.
4849 if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
4850 alen = (xfs_extlen_t)got.br_blockcount;
4851 aoff = got.br_startoff;
4852 if (lastx != NULLEXTNUM && lastx) {
4853 ep = xfs_iext_get_ext(ifp, lastx - 1);
4854 xfs_bmbt_get_all(ep, &prev);
4856 } else if (wasdelay) {
4857 alen = (xfs_extlen_t)
4858 XFS_FILBLKS_MIN(len,
4860 got.br_blockcount) - bno);
4863 alen = (xfs_extlen_t)
4864 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4866 alen = (xfs_extlen_t)
4867 XFS_FILBLKS_MIN(alen,
4868 got.br_startoff - bno);
4871 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4872 if (flags & XFS_BMAPI_DELAY) {
4875 /* Figure out the extent size, adjust alen */
4876 extsz = xfs_get_extsz_hint(ip);
4878 error = xfs_bmap_extsize_align(mp,
4881 flags&XFS_BMAPI_DELAY,
4882 flags&XFS_BMAPI_CONVERT,
4888 extsz = alen / mp->m_sb.sb_rextsize;
4891 * Make a transaction-less quota reservation for
4892 * delayed allocation blocks. This number gets
4893 * adjusted later. We return if we haven't
4894 * allocated blocks already inside this loop.
4896 if ((error = XFS_TRANS_RESERVE_QUOTA_NBLKS(
4897 mp, NULL, ip, (long)alen, 0,
4898 rt ? XFS_QMOPT_RES_RTBLKS :
4899 XFS_QMOPT_RES_REGBLKS))) {
4902 ASSERT(cur == NULL);
4909 * Split changing sb for alen and indlen since
4910 * they could be coming from different places.
4912 indlen = (xfs_extlen_t)
4913 xfs_bmap_worst_indlen(ip, alen);
4917 error = xfs_mod_incore_sb(mp,
4919 -((int64_t)extsz), (flags &
4920 XFS_BMAPI_RSVBLOCKS));
4922 error = xfs_mod_incore_sb(mp,
4924 -((int64_t)alen), (flags &
4925 XFS_BMAPI_RSVBLOCKS));
4928 error = xfs_mod_incore_sb(mp,
4930 -((int64_t)indlen), (flags &
4931 XFS_BMAPI_RSVBLOCKS));
4933 xfs_mod_incore_sb(mp,
4935 (int64_t)extsz, (flags &
4936 XFS_BMAPI_RSVBLOCKS));
4938 xfs_mod_incore_sb(mp,
4940 (int64_t)alen, (flags &
4941 XFS_BMAPI_RSVBLOCKS));
4945 if (XFS_IS_QUOTA_ON(mp))
4946 /* unreserve the blocks now */
4948 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
4951 XFS_QMOPT_RES_RTBLKS :
4952 XFS_QMOPT_RES_REGBLKS);
4956 ip->i_delayed_blks += alen;
4957 abno = NULLSTARTBLOCK(indlen);
4960 * If first time, allocate and fill in
4961 * once-only bma fields.
4963 if (bma.ip == NULL) {
4971 /* Indicate if this is the first user data
4972 * in the file, or just any user data.
4974 if (!(flags & XFS_BMAPI_METADATA)) {
4975 bma.userdata = (aoff == 0) ?
4976 XFS_ALLOC_INITIAL_USER_DATA :
4980 * Fill in changeable bma fields.
4983 bma.firstblock = *firstblock;
4986 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4987 bma.wasdel = wasdelay;
4988 bma.minlen = minlen;
4989 bma.low = flist->xbf_low;
4990 bma.minleft = minleft;
4992 * Only want to do the alignment at the
4993 * eof if it is userdata and allocation length
4994 * is larger than a stripe unit.
4996 if (mp->m_dalign && alen >= mp->m_dalign &&
4997 (!(flags & XFS_BMAPI_METADATA)) &&
4998 (whichfork == XFS_DATA_FORK)) {
4999 if ((error = xfs_bmap_isaeof(ip, aoff,
5000 whichfork, &bma.aeof)))
5007 if ((error = xfs_bmap_alloc(&bma)))
5010 * Copy out result fields.
5013 if ((flist->xbf_low = bma.low))
5017 ASSERT(*firstblock == NULLFSBLOCK ||
5018 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5019 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
5021 XFS_FSB_TO_AGNO(mp, *firstblock) <
5022 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
5023 *firstblock = bma.firstblock;
5025 cur->bc_private.b.firstblock =
5027 if (abno == NULLFSBLOCK)
5029 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5030 cur = xfs_bmbt_init_cursor(mp, tp,
5032 cur->bc_private.b.firstblock =
5034 cur->bc_private.b.flist = flist;
5037 * Bump the number of extents we've allocated
5043 cur->bc_private.b.flags =
5044 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
5045 got.br_startoff = aoff;
5046 got.br_startblock = abno;
5047 got.br_blockcount = alen;
5048 got.br_state = XFS_EXT_NORM; /* assume normal */
5050 * Determine state of extent, and the filesystem.
5051 * A wasdelay extent has been initialized, so
5052 * shouldn't be flagged as unwritten.
5054 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5055 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
5056 got.br_state = XFS_EXT_UNWRITTEN;
5058 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
5059 firstblock, flist, &tmp_logflags, delta,
5060 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
5061 logflags |= tmp_logflags;
5064 lastx = ifp->if_lastex;
5065 ep = xfs_iext_get_ext(ifp, lastx);
5066 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5067 xfs_bmbt_get_all(ep, &got);
5068 ASSERT(got.br_startoff <= aoff);
5069 ASSERT(got.br_startoff + got.br_blockcount >=
5072 if (flags & XFS_BMAPI_DELAY) {
5073 ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
5074 ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
5076 ASSERT(got.br_state == XFS_EXT_NORM ||
5077 got.br_state == XFS_EXT_UNWRITTEN);
5080 * Fall down into the found allocated space case.
5082 } else if (inhole) {
5084 * Reading in a hole.
5086 mval->br_startoff = bno;
5087 mval->br_startblock = HOLESTARTBLOCK;
5088 mval->br_blockcount =
5089 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
5090 mval->br_state = XFS_EXT_NORM;
5091 bno += mval->br_blockcount;
5092 len -= mval->br_blockcount;
5098 * Then deal with the allocated space we found.
5101 if (!(flags & XFS_BMAPI_ENTIRE) &&
5102 (got.br_startoff + got.br_blockcount > obno)) {
5105 ASSERT((bno >= obno) || (n == 0));
5107 mval->br_startoff = bno;
5108 if (ISNULLSTARTBLOCK(got.br_startblock)) {
5109 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5110 mval->br_startblock = DELAYSTARTBLOCK;
5112 mval->br_startblock =
5114 (bno - got.br_startoff);
5116 * Return the minimum of what we got and what we
5117 * asked for for the length. We can use the len
5118 * variable here because it is modified below
5119 * and we could have been there before coming
5120 * here if the first part of the allocation
5121 * didn't overlap what was asked for.
5123 mval->br_blockcount =
5124 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
5125 (bno - got.br_startoff));
5126 mval->br_state = got.br_state;
5127 ASSERT(mval->br_blockcount <= len);
5130 if (ISNULLSTARTBLOCK(mval->br_startblock)) {
5131 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5132 mval->br_startblock = DELAYSTARTBLOCK;
5137 * Check if writing previously allocated but
5138 * unwritten extents.
5140 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
5141 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
5143 * Modify (by adding) the state flag, if writing.
5145 ASSERT(mval->br_blockcount <= len);
5146 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5147 cur = xfs_bmbt_init_cursor(mp,
5149 cur->bc_private.b.firstblock =
5151 cur->bc_private.b.flist = flist;
5153 mval->br_state = XFS_EXT_NORM;
5154 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
5155 firstblock, flist, &tmp_logflags, delta,
5156 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
5157 logflags |= tmp_logflags;
5160 lastx = ifp->if_lastex;
5161 ep = xfs_iext_get_ext(ifp, lastx);
5162 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5163 xfs_bmbt_get_all(ep, &got);
5165 * We may have combined previously unwritten
5166 * space with written space, so generate
5169 if (mval->br_blockcount < len)
5173 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5174 ((mval->br_startoff + mval->br_blockcount) <= end));
5175 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5176 (mval->br_blockcount <= len) ||
5177 (mval->br_startoff < obno));
5178 bno = mval->br_startoff + mval->br_blockcount;
5180 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
5181 ASSERT(mval->br_startblock == mval[-1].br_startblock);
5182 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
5183 ASSERT(mval->br_state == mval[-1].br_state);
5184 mval[-1].br_blockcount = mval->br_blockcount;
5185 mval[-1].br_state = mval->br_state;
5186 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
5187 mval[-1].br_startblock != DELAYSTARTBLOCK &&
5188 mval[-1].br_startblock != HOLESTARTBLOCK &&
5189 mval->br_startblock ==
5190 mval[-1].br_startblock + mval[-1].br_blockcount &&
5191 ((flags & XFS_BMAPI_IGSTATE) ||
5192 mval[-1].br_state == mval->br_state)) {
5193 ASSERT(mval->br_startoff ==
5194 mval[-1].br_startoff + mval[-1].br_blockcount);
5195 mval[-1].br_blockcount += mval->br_blockcount;
5197 mval->br_startblock == DELAYSTARTBLOCK &&
5198 mval[-1].br_startblock == DELAYSTARTBLOCK &&
5199 mval->br_startoff ==
5200 mval[-1].br_startoff + mval[-1].br_blockcount) {
5201 mval[-1].br_blockcount += mval->br_blockcount;
5202 mval[-1].br_state = mval->br_state;
5203 } else if (!((n == 0) &&
5204 ((mval->br_startoff + mval->br_blockcount) <=
5210 * If we're done, stop now. Stop when we've allocated
5211 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5212 * the transaction may get too big.
5214 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5217 * Else go on to the next record.
5219 ep = xfs_iext_get_ext(ifp, ++lastx);
5221 if (lastx >= nextents)
5224 xfs_bmbt_get_all(ep, &got);
5226 ifp->if_lastex = lastx;
5229 * Transform from btree to extents, give it cur.
5231 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5232 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5234 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5235 &tmp_logflags, whichfork);
5236 logflags |= tmp_logflags;
5240 ASSERT(ifp->if_ext_max ==
5241 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5242 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5243 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5245 if (delta && delta->xed_startoff != NULLFILEOFF) {
5246 /* A change was actually made.
5247 * Note that delta->xed_blockount is an offset at this
5248 * point and needs to be converted to a block count.
5250 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5251 delta->xed_blockcount -= delta->xed_startoff;
5255 * Log everything. Do this after conversion, there's no point in
5256 * logging the extent records if we've converted to btree format.
5258 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5259 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5260 logflags &= ~XFS_ILOG_FEXT(whichfork);
5261 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5262 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5263 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5265 * Log whatever the flags say, even if error. Otherwise we might miss
5266 * detecting a case where the data is changed, there's an error,
5267 * and it's not logged so we don't shutdown when we should.
5271 xfs_trans_log_inode(tp, ip, logflags);
5275 ASSERT(*firstblock == NULLFSBLOCK ||
5276 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5278 cur->bc_private.b.firstblock) ||
5280 XFS_FSB_TO_AGNO(mp, *firstblock) <
5282 cur->bc_private.b.firstblock)));
5283 *firstblock = cur->bc_private.b.firstblock;
5285 xfs_btree_del_cursor(cur,
5286 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5289 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5295 * Map file blocks to filesystem blocks, simple version.
5296 * One block (extent) only, read-only.
5297 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5298 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5299 * was set and all the others were clear.
5303 xfs_trans_t *tp, /* transaction pointer */
5304 xfs_inode_t *ip, /* incore inode */
5305 int whichfork, /* data or attr fork */
5306 xfs_fsblock_t *fsb, /* output: mapped block */
5307 xfs_fileoff_t bno) /* starting file offs. mapped */
5309 int eof; /* we've hit the end of extents */
5310 int error; /* error return */
5311 xfs_bmbt_irec_t got; /* current file extent record */
5312 xfs_ifork_t *ifp; /* inode fork pointer */
5313 xfs_extnum_t lastx; /* last useful extent number */
5314 xfs_bmbt_irec_t prev; /* previous file extent record */
5316 ifp = XFS_IFORK_PTR(ip, whichfork);
5318 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5319 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5320 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5322 return XFS_ERROR(EFSCORRUPTED);
5324 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5325 return XFS_ERROR(EIO);
5326 XFS_STATS_INC(xs_blk_mapr);
5327 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5328 (error = xfs_iread_extents(tp, ip, whichfork)))
5330 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5333 * Reading past eof, act as though there's a hole
5336 if (eof || got.br_startoff > bno) {
5340 ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
5341 ASSERT(bno < got.br_startoff + got.br_blockcount);
5342 *fsb = got.br_startblock + (bno - got.br_startoff);
5343 ifp->if_lastex = lastx;
5348 * Unmap (remove) blocks from a file.
5349 * If nexts is nonzero then the number of extents to remove is limited to
5350 * that value. If not all extents in the block range can be removed then
5355 xfs_trans_t *tp, /* transaction pointer */
5356 struct xfs_inode *ip, /* incore inode */
5357 xfs_fileoff_t bno, /* starting offset to unmap */
5358 xfs_filblks_t len, /* length to unmap in file */
5359 int flags, /* misc flags */
5360 xfs_extnum_t nexts, /* number of extents max */
5361 xfs_fsblock_t *firstblock, /* first allocated block
5362 controls a.g. for allocs */
5363 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5364 xfs_extdelta_t *delta, /* o: change made to incore
5366 int *done) /* set if not done yet */
5368 xfs_btree_cur_t *cur; /* bmap btree cursor */
5369 xfs_bmbt_irec_t del; /* extent being deleted */
5370 int eof; /* is deleting at eof */
5371 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
5372 int error; /* error return value */
5373 xfs_extnum_t extno; /* extent number in list */
5374 xfs_bmbt_irec_t got; /* current extent record */
5375 xfs_ifork_t *ifp; /* inode fork pointer */
5376 int isrt; /* freeing in rt area */
5377 xfs_extnum_t lastx; /* last extent index used */
5378 int logflags; /* transaction logging flags */
5379 xfs_extlen_t mod; /* rt extent offset */
5380 xfs_mount_t *mp; /* mount structure */
5381 xfs_extnum_t nextents; /* number of file extents */
5382 xfs_bmbt_irec_t prev; /* previous extent record */
5383 xfs_fileoff_t start; /* first file offset deleted */
5384 int tmp_logflags; /* partial logging flags */
5385 int wasdel; /* was a delayed alloc extent */
5386 int whichfork; /* data or attribute fork */
5387 int rsvd; /* OK to allocate reserved blocks */
5390 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
5391 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5392 XFS_ATTR_FORK : XFS_DATA_FORK;
5393 ifp = XFS_IFORK_PTR(ip, whichfork);
5395 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5396 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5397 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5399 return XFS_ERROR(EFSCORRUPTED);
5402 if (XFS_FORCED_SHUTDOWN(mp))
5403 return XFS_ERROR(EIO);
5404 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5407 ASSERT(ifp->if_ext_max ==
5408 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5409 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5410 (error = xfs_iread_extents(tp, ip, whichfork)))
5412 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5413 if (nextents == 0) {
5417 XFS_STATS_INC(xs_blk_unmap);
5418 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5420 bno = start + len - 1;
5421 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5424 delta->xed_startoff = NULLFILEOFF;
5425 delta->xed_blockcount = 0;
5428 * Check to see if the given block number is past the end of the
5429 * file, back up to the last block if so...
5432 ep = xfs_iext_get_ext(ifp, --lastx);
5433 xfs_bmbt_get_all(ep, &got);
5434 bno = got.br_startoff + got.br_blockcount - 1;
5437 if (ifp->if_flags & XFS_IFBROOT) {
5438 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5439 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5440 cur->bc_private.b.firstblock = *firstblock;
5441 cur->bc_private.b.flist = flist;
5442 cur->bc_private.b.flags = 0;
5446 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5447 (nexts == 0 || extno < nexts)) {
5449 * Is the found extent after a hole in which bno lives?
5450 * Just back up to the previous extent, if so.
5452 if (got.br_startoff > bno) {
5455 ep = xfs_iext_get_ext(ifp, lastx);
5456 xfs_bmbt_get_all(ep, &got);
5459 * Is the last block of this extent before the range
5460 * we're supposed to delete? If so, we're done.
5462 bno = XFS_FILEOFF_MIN(bno,
5463 got.br_startoff + got.br_blockcount - 1);
5467 * Then deal with the (possibly delayed) allocated space
5472 wasdel = ISNULLSTARTBLOCK(del.br_startblock);
5473 if (got.br_startoff < start) {
5474 del.br_startoff = start;
5475 del.br_blockcount -= start - got.br_startoff;
5477 del.br_startblock += start - got.br_startoff;
5479 if (del.br_startoff + del.br_blockcount > bno + 1)
5480 del.br_blockcount = bno + 1 - del.br_startoff;
5481 sum = del.br_startblock + del.br_blockcount;
5483 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5485 * Realtime extent not lined up at the end.
5486 * The extent could have been split into written
5487 * and unwritten pieces, or we could just be
5488 * unmapping part of it. But we can't really
5489 * get rid of part of a realtime extent.
5491 if (del.br_state == XFS_EXT_UNWRITTEN ||
5492 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5494 * This piece is unwritten, or we're not
5495 * using unwritten extents. Skip over it.
5498 bno -= mod > del.br_blockcount ?
5499 del.br_blockcount : mod;
5500 if (bno < got.br_startoff) {
5502 xfs_bmbt_get_all(xfs_iext_get_ext(
5508 * It's written, turn it unwritten.
5509 * This is better than zeroing it.
5511 ASSERT(del.br_state == XFS_EXT_NORM);
5512 ASSERT(xfs_trans_get_block_res(tp) > 0);
5514 * If this spans a realtime extent boundary,
5515 * chop it back to the start of the one we end at.
5517 if (del.br_blockcount > mod) {
5518 del.br_startoff += del.br_blockcount - mod;
5519 del.br_startblock += del.br_blockcount - mod;
5520 del.br_blockcount = mod;
5522 del.br_state = XFS_EXT_UNWRITTEN;
5523 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
5524 firstblock, flist, &logflags, delta,
5530 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5532 * Realtime extent is lined up at the end but not
5533 * at the front. We'll get rid of full extents if
5536 mod = mp->m_sb.sb_rextsize - mod;
5537 if (del.br_blockcount > mod) {
5538 del.br_blockcount -= mod;
5539 del.br_startoff += mod;
5540 del.br_startblock += mod;
5541 } else if ((del.br_startoff == start &&
5542 (del.br_state == XFS_EXT_UNWRITTEN ||
5543 xfs_trans_get_block_res(tp) == 0)) ||
5544 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5546 * Can't make it unwritten. There isn't
5547 * a full extent here so just skip it.
5549 ASSERT(bno >= del.br_blockcount);
5550 bno -= del.br_blockcount;
5551 if (bno < got.br_startoff) {
5553 xfs_bmbt_get_all(--ep, &got);
5556 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5558 * This one is already unwritten.
5559 * It must have a written left neighbor.
5560 * Unwrite the killed part of that one and
5564 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5566 ASSERT(prev.br_state == XFS_EXT_NORM);
5567 ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
5568 ASSERT(del.br_startblock ==
5569 prev.br_startblock + prev.br_blockcount);
5570 if (prev.br_startoff < start) {
5571 mod = start - prev.br_startoff;
5572 prev.br_blockcount -= mod;
5573 prev.br_startblock += mod;
5574 prev.br_startoff = start;
5576 prev.br_state = XFS_EXT_UNWRITTEN;
5577 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5578 &prev, firstblock, flist, &logflags,
5579 delta, XFS_DATA_FORK, 0);
5584 ASSERT(del.br_state == XFS_EXT_NORM);
5585 del.br_state = XFS_EXT_UNWRITTEN;
5586 error = xfs_bmap_add_extent(ip, lastx, &cur,
5587 &del, firstblock, flist, &logflags,
5588 delta, XFS_DATA_FORK, 0);
5595 ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
5596 /* Update realtime/data freespace, unreserve quota */
5598 xfs_filblks_t rtexts;
5600 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5601 do_div(rtexts, mp->m_sb.sb_rextsize);
5602 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5603 (int64_t)rtexts, rsvd);
5604 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5605 NULL, ip, -((long)del.br_blockcount), 0,
5606 XFS_QMOPT_RES_RTBLKS);
5608 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
5609 (int64_t)del.br_blockcount, rsvd);
5610 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5611 NULL, ip, -((long)del.br_blockcount), 0,
5612 XFS_QMOPT_RES_REGBLKS);
5614 ip->i_delayed_blks -= del.br_blockcount;
5616 cur->bc_private.b.flags |=
5617 XFS_BTCUR_BPRV_WASDEL;
5619 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5621 * If it's the case where the directory code is running
5622 * with no block reservation, and the deleted block is in
5623 * the middle of its extent, and the resulting insert
5624 * of an extent would cause transformation to btree format,
5625 * then reject it. The calling code will then swap
5626 * blocks around instead.
5627 * We have to do this now, rather than waiting for the
5628 * conversion to btree format, since the transaction
5631 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5632 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5633 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5634 del.br_startoff > got.br_startoff &&
5635 del.br_startoff + del.br_blockcount <
5636 got.br_startoff + got.br_blockcount) {
5637 error = XFS_ERROR(ENOSPC);
5640 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
5641 &tmp_logflags, delta, whichfork, rsvd);
5642 logflags |= tmp_logflags;
5645 bno = del.br_startoff - 1;
5647 lastx = ifp->if_lastex;
5649 * If not done go on to the next (previous) record.
5650 * Reset ep in case the extents array was re-alloced.
5652 ep = xfs_iext_get_ext(ifp, lastx);
5653 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5654 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5655 xfs_bmbt_get_startoff(ep) > bno) {
5657 ep = xfs_iext_get_ext(ifp, lastx);
5660 xfs_bmbt_get_all(ep, &got);
5664 ifp->if_lastex = lastx;
5665 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5666 ASSERT(ifp->if_ext_max ==
5667 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5669 * Convert to a btree if necessary.
5671 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5672 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5673 ASSERT(cur == NULL);
5674 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5675 &cur, 0, &tmp_logflags, whichfork);
5676 logflags |= tmp_logflags;
5681 * transform from btree to extents, give it cur
5683 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5684 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5685 ASSERT(cur != NULL);
5686 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5688 logflags |= tmp_logflags;
5693 * transform from extents to local?
5695 ASSERT(ifp->if_ext_max ==
5696 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5698 if (delta && delta->xed_startoff != NULLFILEOFF) {
5699 /* A change was actually made.
5700 * Note that delta->xed_blockount is an offset at this
5701 * point and needs to be converted to a block count.
5703 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5704 delta->xed_blockcount -= delta->xed_startoff;
5708 * Log everything. Do this after conversion, there's no point in
5709 * logging the extent records if we've converted to btree format.
5711 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5712 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5713 logflags &= ~XFS_ILOG_FEXT(whichfork);
5714 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5715 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5716 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5718 * Log inode even in the error case, if the transaction
5719 * is dirty we'll need to shut down the filesystem.
5722 xfs_trans_log_inode(tp, ip, logflags);
5725 *firstblock = cur->bc_private.b.firstblock;
5726 cur->bc_private.b.allocated = 0;
5728 xfs_btree_del_cursor(cur,
5729 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5735 * returns 1 for success, 0 if we failed to map the extent.
5738 xfs_getbmapx_fix_eof_hole(
5739 xfs_inode_t *ip, /* xfs incore inode pointer */
5740 struct getbmap *out, /* output structure */
5741 int prealloced, /* this is a file with
5742 * preallocated data space */
5743 __int64_t end, /* last block requested */
5744 xfs_fsblock_t startblock)
5747 xfs_mount_t *mp; /* file system mount point */
5749 if (startblock == HOLESTARTBLOCK) {
5751 out->bmv_block = -1;
5752 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5753 fixlen -= out->bmv_offset;
5754 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5755 /* Came to hole at EOF. Trim it. */
5758 out->bmv_length = fixlen;
5761 out->bmv_block = XFS_FSB_TO_DB(ip, startblock);
5768 * Fcntl interface to xfs_bmapi.
5770 int /* error code */
5773 struct getbmap *bmv, /* user bmap structure */
5774 void __user *ap, /* pointer to user's array */
5775 int interface) /* interface flags */
5777 __int64_t bmvend; /* last block requested */
5778 int error; /* return value */
5779 __int64_t fixlen; /* length for -1 case */
5780 int i; /* extent number */
5781 int lock; /* lock state */
5782 xfs_bmbt_irec_t *map; /* buffer for user's data */
5783 xfs_mount_t *mp; /* file system mount point */
5784 int nex; /* # of user extents can do */
5785 int nexleft; /* # of user extents left */
5786 int subnex; /* # of bmapi's can do */
5787 int nmap; /* number of map entries */
5788 struct getbmap out; /* output structure */
5789 int whichfork; /* data or attr fork */
5790 int prealloced; /* this is a file with
5791 * preallocated data space */
5792 int sh_unwritten; /* true, if unwritten */
5793 /* extents listed separately */
5794 int bmapi_flags; /* flags for xfs_bmapi */
5795 __int32_t oflags; /* getbmapx bmv_oflags field */
5799 whichfork = interface & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5800 sh_unwritten = (interface & BMV_IF_PREALLOC) != 0;
5802 /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
5803 * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
5804 * bit is set for the file, generate a read event in order
5805 * that the DMAPI application may do its thing before we return
5806 * the extents. Usually this means restoring user file data to
5807 * regions of the file that look like holes.
5809 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5810 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5811 * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
5812 * could misinterpret holes in a DMAPI file as true holes,
5813 * when in fact they may represent offline user data.
5815 if ((interface & BMV_IF_NO_DMAPI_READ) == 0 &&
5816 DM_EVENT_ENABLED(ip, DM_EVENT_READ) &&
5817 whichfork == XFS_DATA_FORK) {
5818 error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip, 0, 0, 0, NULL);
5820 return XFS_ERROR(error);
5823 if (whichfork == XFS_ATTR_FORK) {
5824 if (XFS_IFORK_Q(ip)) {
5825 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5826 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5827 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5828 return XFS_ERROR(EINVAL);
5829 } else if (unlikely(
5830 ip->i_d.di_aformat != 0 &&
5831 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5832 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5834 return XFS_ERROR(EFSCORRUPTED);
5836 } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5837 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5838 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5839 return XFS_ERROR(EINVAL);
5840 if (whichfork == XFS_DATA_FORK) {
5841 if (xfs_get_extsz_hint(ip) ||
5842 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5844 fixlen = XFS_MAXIOFFSET(mp);
5847 fixlen = ip->i_size;
5854 if (bmv->bmv_length == -1) {
5855 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5856 bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset),
5858 } else if (bmv->bmv_length < 0)
5859 return XFS_ERROR(EINVAL);
5860 if (bmv->bmv_length == 0) {
5861 bmv->bmv_entries = 0;
5864 nex = bmv->bmv_count - 1;
5866 return XFS_ERROR(EINVAL);
5867 bmvend = bmv->bmv_offset + bmv->bmv_length;
5869 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5871 if (whichfork == XFS_DATA_FORK &&
5872 (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size)) {
5873 /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
5874 error = xfs_flush_pages(ip, (xfs_off_t)0,
5877 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5882 ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0);
5884 lock = xfs_ilock_map_shared(ip);
5887 * Don't let nex be bigger than the number of extents
5888 * we can have assuming alternating holes and real extents.
5890 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5891 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5893 bmapi_flags = XFS_BMAPI_AFLAG(whichfork) |
5894 ((sh_unwritten) ? 0 : XFS_BMAPI_IGSTATE);
5897 * Allocate enough space to handle "subnex" maps at a time.
5900 map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP);
5902 bmv->bmv_entries = 0;
5904 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0) {
5906 goto unlock_and_return;
5912 nmap = (nexleft > subnex) ? subnex : nexleft;
5913 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5914 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5915 bmapi_flags, NULL, 0, map, &nmap,
5918 goto unlock_and_return;
5919 ASSERT(nmap <= subnex);
5921 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5923 oflags = (map[i].br_state == XFS_EXT_UNWRITTEN) ?
5924 BMV_OF_PREALLOC : 0;
5925 out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff);
5926 out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5927 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
5928 if (map[i].br_startblock == HOLESTARTBLOCK &&
5929 whichfork == XFS_ATTR_FORK) {
5930 /* came to the end of attribute fork */
5931 goto unlock_and_return;
5933 if (!xfs_getbmapx_fix_eof_hole(ip, &out,
5935 map[i].br_startblock)) {
5936 goto unlock_and_return;
5939 /* return either getbmap/getbmapx structure. */
5940 if (interface & BMV_IF_EXTENDED) {
5941 struct getbmapx outx;
5943 GETBMAP_CONVERT(out,outx);
5944 outx.bmv_oflags = oflags;
5945 outx.bmv_unused1 = outx.bmv_unused2 = 0;
5946 if (copy_to_user(ap, &outx,
5948 error = XFS_ERROR(EFAULT);
5949 goto unlock_and_return;
5952 if (copy_to_user(ap, &out,
5954 error = XFS_ERROR(EFAULT);
5955 goto unlock_and_return;
5959 out.bmv_offset + out.bmv_length;
5960 bmv->bmv_length = MAX((__int64_t)0,
5961 (__int64_t)(bmvend - bmv->bmv_offset));
5963 ap = (interface & BMV_IF_EXTENDED) ?
5965 ((struct getbmapx __user *)ap + 1) :
5967 ((struct getbmap __user *)ap + 1);
5970 } while (nmap && nexleft && bmv->bmv_length);
5973 xfs_iunlock_map_shared(ip, lock);
5974 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5982 * Check the last inode extent to determine whether this allocation will result
5983 * in blocks being allocated at the end of the file. When we allocate new data
5984 * blocks at the end of the file which do not start at the previous data block,
5985 * we will try to align the new blocks at stripe unit boundaries.
5987 STATIC int /* error */
5989 xfs_inode_t *ip, /* incore inode pointer */
5990 xfs_fileoff_t off, /* file offset in fsblocks */
5991 int whichfork, /* data or attribute fork */
5992 char *aeof) /* return value */
5994 int error; /* error return value */
5995 xfs_ifork_t *ifp; /* inode fork pointer */
5996 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
5997 xfs_extnum_t nextents; /* number of file extents */
5998 xfs_bmbt_irec_t s; /* expanded extent record */
6000 ASSERT(whichfork == XFS_DATA_FORK);
6001 ifp = XFS_IFORK_PTR(ip, whichfork);
6002 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6003 (error = xfs_iread_extents(NULL, ip, whichfork)))
6005 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6006 if (nextents == 0) {
6011 * Go to the last extent
6013 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
6014 xfs_bmbt_get_all(lastrec, &s);
6016 * Check we are allocating in the last extent (for delayed allocations)
6017 * or past the last extent for non-delayed allocations.
6019 *aeof = (off >= s.br_startoff &&
6020 off < s.br_startoff + s.br_blockcount &&
6021 ISNULLSTARTBLOCK(s.br_startblock)) ||
6022 off >= s.br_startoff + s.br_blockcount;
6027 * Check if the endoff is outside the last extent. If so the caller will grow
6028 * the allocation to a stripe unit boundary.
6032 xfs_inode_t *ip, /* incore inode pointer */
6033 xfs_fileoff_t endoff, /* file offset in fsblocks */
6034 int whichfork, /* data or attribute fork */
6035 int *eof) /* result value */
6037 xfs_fsblock_t blockcount; /* extent block count */
6038 int error; /* error return value */
6039 xfs_ifork_t *ifp; /* inode fork pointer */
6040 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
6041 xfs_extnum_t nextents; /* number of file extents */
6042 xfs_fileoff_t startoff; /* extent starting file offset */
6044 ASSERT(whichfork == XFS_DATA_FORK);
6045 ifp = XFS_IFORK_PTR(ip, whichfork);
6046 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6047 (error = xfs_iread_extents(NULL, ip, whichfork)))
6049 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6050 if (nextents == 0) {
6055 * Go to the last extent
6057 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
6058 startoff = xfs_bmbt_get_startoff(lastrec);
6059 blockcount = xfs_bmbt_get_blockcount(lastrec);
6060 *eof = endoff >= startoff + blockcount;
6068 xfs_btree_cur_t *cur,
6078 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
6079 bp = cur->bc_bufs[i];
6081 if (XFS_BUF_ADDR(bp) == bno)
6082 break; /* Found it */
6084 if (i == XFS_BTREE_MAXLEVELS)
6087 if (!bp) { /* Chase down all the log items to see if the bp is there */
6088 xfs_log_item_chunk_t *licp;
6092 licp = &tp->t_items;
6093 while (!bp && licp != NULL) {
6094 if (xfs_lic_are_all_free(licp)) {
6095 licp = licp->lic_next;
6098 for (i = 0; i < licp->lic_unused; i++) {
6099 xfs_log_item_desc_t *lidp;
6100 xfs_log_item_t *lip;
6101 xfs_buf_log_item_t *bip;
6104 if (xfs_lic_isfree(licp, i)) {
6108 lidp = xfs_lic_slot(licp, i);
6109 lip = lidp->lid_item;
6110 if (lip->li_type != XFS_LI_BUF)
6113 bip = (xfs_buf_log_item_t *)lip;
6116 if (XFS_BUF_ADDR(lbp) == bno) {
6118 break; /* Found it */
6121 licp = licp->lic_next;
6129 xfs_bmbt_block_t *block,
6135 __be64 *pp, *thispa; /* pointer to block address */
6136 xfs_bmbt_key_t *prevp, *keyp;
6138 ASSERT(be16_to_cpu(block->bb_level) > 0);
6141 for( i = 1; i <= be16_to_cpu(block->bb_numrecs); i++) {
6142 dmxr = mp->m_bmap_dmxr[0];
6145 keyp = XFS_BMAP_BROOT_KEY_ADDR(block, i, sz);
6147 keyp = XFS_BTREE_KEY_ADDR(xfs_bmbt, block, i);
6151 xfs_btree_check_key(XFS_BTNUM_BMAP, prevp, keyp);
6156 * Compare the block numbers to see if there are dups.
6160 pp = XFS_BMAP_BROOT_PTR_ADDR(block, i, sz);
6162 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, i, dmxr);
6164 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
6166 thispa = XFS_BMAP_BROOT_PTR_ADDR(block, j, sz);
6168 thispa = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, j,
6171 if (*thispa == *pp) {
6172 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
6174 (unsigned long long)be64_to_cpu(*thispa));
6175 panic("%s: ptrs are equal in node\n",
6183 * Check that the extents for the inode ip are in the right order in all
6188 xfs_bmap_check_leaf_extents(
6189 xfs_btree_cur_t *cur, /* btree cursor or null */
6190 xfs_inode_t *ip, /* incore inode pointer */
6191 int whichfork) /* data or attr fork */
6193 xfs_bmbt_block_t *block; /* current btree block */
6194 xfs_fsblock_t bno; /* block # of "block" */
6195 xfs_buf_t *bp; /* buffer for "block" */
6196 int error; /* error return value */
6197 xfs_extnum_t i=0, j; /* index into the extents list */
6198 xfs_ifork_t *ifp; /* fork structure */
6199 int level; /* btree level, for checking */
6200 xfs_mount_t *mp; /* file system mount structure */
6201 __be64 *pp; /* pointer to block address */
6202 xfs_bmbt_rec_t *ep; /* pointer to current extent */
6203 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
6204 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
6207 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
6213 ifp = XFS_IFORK_PTR(ip, whichfork);
6214 block = ifp->if_broot;
6216 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6218 level = be16_to_cpu(block->bb_level);
6220 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
6221 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6222 bno = be64_to_cpu(*pp);
6224 ASSERT(bno != NULLDFSBNO);
6225 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6226 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6229 * Go down the tree until leaf level is reached, following the first
6230 * pointer (leftmost) at each level.
6232 while (level-- > 0) {
6233 /* See if buf is in cur first */
6234 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6240 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6241 XFS_BMAP_BTREE_REF)))
6243 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6244 XFS_WANT_CORRUPTED_GOTO(
6245 XFS_BMAP_SANITY_CHECK(mp, block, level),
6251 * Check this block for basic sanity (increasing keys and
6252 * no duplicate blocks).
6255 xfs_check_block(block, mp, 0, 0);
6256 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
6257 bno = be64_to_cpu(*pp);
6258 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
6261 xfs_trans_brelse(NULL, bp);
6266 * Here with bp and block set to the leftmost leaf node in the tree.
6271 * Loop over all leaf nodes checking that all extents are in the right order.
6274 xfs_fsblock_t nextbno;
6275 xfs_extnum_t num_recs;
6278 num_recs = be16_to_cpu(block->bb_numrecs);
6281 * Read-ahead the next leaf block, if any.
6284 nextbno = be64_to_cpu(block->bb_rightsib);
6287 * Check all the extents to make sure they are OK.
6288 * If we had a previous block, the last entry should
6289 * conform with the first entry in this one.
6292 ep = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
6294 xfs_btree_check_rec(XFS_BTNUM_BMAP, &last, ep);
6296 for (j = 1; j < num_recs; j++) {
6297 nextp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, j + 1);
6298 xfs_btree_check_rec(XFS_BTNUM_BMAP, ep, nextp);
6306 xfs_trans_brelse(NULL, bp);
6310 * If we've reached the end, stop.
6312 if (bno == NULLFSBLOCK)
6315 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6321 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6322 XFS_BMAP_BTREE_REF)))
6324 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6328 xfs_trans_brelse(NULL, bp);
6333 cmn_err(CE_WARN, "%s: at error0", __func__);
6335 xfs_trans_brelse(NULL, bp);
6337 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
6339 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
6345 * Count fsblocks of the given fork.
6348 xfs_bmap_count_blocks(
6349 xfs_trans_t *tp, /* transaction pointer */
6350 xfs_inode_t *ip, /* incore inode */
6351 int whichfork, /* data or attr fork */
6352 int *count) /* out: count of blocks */
6354 xfs_bmbt_block_t *block; /* current btree block */
6355 xfs_fsblock_t bno; /* block # of "block" */
6356 xfs_ifork_t *ifp; /* fork structure */
6357 int level; /* btree level, for checking */
6358 xfs_mount_t *mp; /* file system mount structure */
6359 __be64 *pp; /* pointer to block address */
6363 ifp = XFS_IFORK_PTR(ip, whichfork);
6364 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
6365 xfs_bmap_count_leaves(ifp, 0,
6366 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6372 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6374 block = ifp->if_broot;
6375 level = be16_to_cpu(block->bb_level);
6377 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6378 bno = be64_to_cpu(*pp);
6379 ASSERT(bno != NULLDFSBNO);
6380 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6381 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6383 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
6384 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6386 return XFS_ERROR(EFSCORRUPTED);
6393 * Recursively walks each level of a btree
6394 * to count total fsblocks is use.
6396 STATIC int /* error */
6397 xfs_bmap_count_tree(
6398 xfs_mount_t *mp, /* file system mount point */
6399 xfs_trans_t *tp, /* transaction pointer */
6400 xfs_ifork_t *ifp, /* inode fork pointer */
6401 xfs_fsblock_t blockno, /* file system block number */
6402 int levelin, /* level in btree */
6403 int *count) /* Count of blocks */
6406 xfs_buf_t *bp, *nbp;
6407 int level = levelin;
6409 xfs_fsblock_t bno = blockno;
6410 xfs_fsblock_t nextbno;
6411 xfs_bmbt_block_t *block, *nextblock;
6414 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6417 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6420 /* Not at node above leafs, count this level of nodes */
6421 nextbno = be64_to_cpu(block->bb_rightsib);
6422 while (nextbno != NULLFSBLOCK) {
6423 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6424 0, &nbp, XFS_BMAP_BTREE_REF)))
6427 nextblock = XFS_BUF_TO_BMBT_BLOCK(nbp);
6428 nextbno = be64_to_cpu(nextblock->bb_rightsib);
6429 xfs_trans_brelse(tp, nbp);
6432 /* Dive to the next level */
6433 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
6434 bno = be64_to_cpu(*pp);
6435 if (unlikely((error =
6436 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
6437 xfs_trans_brelse(tp, bp);
6438 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6439 XFS_ERRLEVEL_LOW, mp);
6440 return XFS_ERROR(EFSCORRUPTED);
6442 xfs_trans_brelse(tp, bp);
6444 /* count all level 1 nodes and their leaves */
6446 nextbno = be64_to_cpu(block->bb_rightsib);
6447 numrecs = be16_to_cpu(block->bb_numrecs);
6448 xfs_bmap_disk_count_leaves(0, block, numrecs, count);
6449 xfs_trans_brelse(tp, bp);
6450 if (nextbno == NULLFSBLOCK)
6453 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6454 XFS_BMAP_BTREE_REF)))
6457 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6464 * Count leaf blocks given a range of extent records.
6467 xfs_bmap_count_leaves(
6475 for (b = 0; b < numrecs; b++) {
6476 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
6477 *count += xfs_bmbt_get_blockcount(frp);
6482 * Count leaf blocks given a range of extent records originally
6486 xfs_bmap_disk_count_leaves(
6488 xfs_bmbt_block_t *block,
6493 xfs_bmbt_rec_t *frp;
6495 for (b = 1; b <= numrecs; b++) {
6496 frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, idx + b);
6497 *count += xfs_bmbt_disk_get_blockcount(frp);