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.
405 STATIC int /* error */
407 struct xfs_btree_cur *cur,
411 int *stat) /* success/failure */
413 cur->bc_rec.b.br_startoff = off;
414 cur->bc_rec.b.br_startblock = bno;
415 cur->bc_rec.b.br_blockcount = len;
416 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
419 STATIC int /* error */
421 struct xfs_btree_cur *cur,
425 int *stat) /* success/failure */
427 cur->bc_rec.b.br_startoff = off;
428 cur->bc_rec.b.br_startblock = bno;
429 cur->bc_rec.b.br_blockcount = len;
430 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
435 * Called from xfs_bmap_add_attrfork to handle btree format files.
437 STATIC int /* error */
438 xfs_bmap_add_attrfork_btree(
439 xfs_trans_t *tp, /* transaction pointer */
440 xfs_inode_t *ip, /* incore inode pointer */
441 xfs_fsblock_t *firstblock, /* first block allocated */
442 xfs_bmap_free_t *flist, /* blocks to free at commit */
443 int *flags) /* inode logging flags */
445 xfs_btree_cur_t *cur; /* btree cursor */
446 int error; /* error return value */
447 xfs_mount_t *mp; /* file system mount struct */
448 int stat; /* newroot status */
451 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
452 *flags |= XFS_ILOG_DBROOT;
454 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
455 cur->bc_private.b.flist = flist;
456 cur->bc_private.b.firstblock = *firstblock;
457 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
459 /* must be at least one entry */
460 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
461 if ((error = xfs_bmbt_newroot(cur, flags, &stat)))
464 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
465 return XFS_ERROR(ENOSPC);
467 *firstblock = cur->bc_private.b.firstblock;
468 cur->bc_private.b.allocated = 0;
469 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
473 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
478 * Called from xfs_bmap_add_attrfork to handle extents format files.
480 STATIC int /* error */
481 xfs_bmap_add_attrfork_extents(
482 xfs_trans_t *tp, /* transaction pointer */
483 xfs_inode_t *ip, /* incore inode pointer */
484 xfs_fsblock_t *firstblock, /* first block allocated */
485 xfs_bmap_free_t *flist, /* blocks to free at commit */
486 int *flags) /* inode logging flags */
488 xfs_btree_cur_t *cur; /* bmap btree cursor */
489 int error; /* error return value */
491 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
494 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
495 flags, XFS_DATA_FORK);
497 cur->bc_private.b.allocated = 0;
498 xfs_btree_del_cursor(cur,
499 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
505 * Called from xfs_bmap_add_attrfork to handle local format files.
507 STATIC int /* error */
508 xfs_bmap_add_attrfork_local(
509 xfs_trans_t *tp, /* transaction pointer */
510 xfs_inode_t *ip, /* incore inode pointer */
511 xfs_fsblock_t *firstblock, /* first block allocated */
512 xfs_bmap_free_t *flist, /* blocks to free at commit */
513 int *flags) /* inode logging flags */
515 xfs_da_args_t dargs; /* args for dir/attr code */
516 int error; /* error return value */
517 xfs_mount_t *mp; /* mount structure pointer */
519 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
521 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
523 memset(&dargs, 0, sizeof(dargs));
525 dargs.firstblock = firstblock;
527 dargs.total = mp->m_dirblkfsbs;
528 dargs.whichfork = XFS_DATA_FORK;
530 error = xfs_dir2_sf_to_block(&dargs);
532 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
538 * Called by xfs_bmapi to update file extent records and the btree
539 * after allocating space (or doing a delayed allocation).
541 STATIC int /* error */
543 xfs_inode_t *ip, /* incore inode pointer */
544 xfs_extnum_t idx, /* extent number to update/insert */
545 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
546 xfs_bmbt_irec_t *new, /* new data to add to file extents */
547 xfs_fsblock_t *first, /* pointer to firstblock variable */
548 xfs_bmap_free_t *flist, /* list of extents to be freed */
549 int *logflagsp, /* inode logging flags */
550 xfs_extdelta_t *delta, /* Change made to incore extents */
551 int whichfork, /* data or attr fork */
552 int rsvd) /* OK to use reserved data blocks */
554 xfs_btree_cur_t *cur; /* btree cursor or null */
555 xfs_filblks_t da_new; /* new count del alloc blocks used */
556 xfs_filblks_t da_old; /* old count del alloc blocks used */
557 int error; /* error return value */
558 xfs_ifork_t *ifp; /* inode fork ptr */
559 int logflags; /* returned value */
560 xfs_extnum_t nextents; /* number of extents in file now */
562 XFS_STATS_INC(xs_add_exlist);
564 ifp = XFS_IFORK_PTR(ip, whichfork);
565 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
566 ASSERT(idx <= nextents);
570 * This is the first extent added to a new/empty file.
571 * Special case this one, so other routines get to assume there are
572 * already extents in the list.
575 XFS_BMAP_TRACE_INSERT("insert empty", ip, 0, 1, new, NULL,
577 xfs_iext_insert(ifp, 0, 1, new);
580 if (!ISNULLSTARTBLOCK(new->br_startblock)) {
581 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
582 logflags = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
585 /* DELTA: single new extent */
587 if (delta->xed_startoff > new->br_startoff)
588 delta->xed_startoff = new->br_startoff;
589 if (delta->xed_blockcount <
590 new->br_startoff + new->br_blockcount)
591 delta->xed_blockcount = new->br_startoff +
596 * Any kind of new delayed allocation goes here.
598 else if (ISNULLSTARTBLOCK(new->br_startblock)) {
600 ASSERT((cur->bc_private.b.flags &
601 XFS_BTCUR_BPRV_WASDEL) == 0);
602 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
603 &logflags, delta, rsvd)))
607 * Real allocation off the end of the file.
609 else if (idx == nextents) {
611 ASSERT((cur->bc_private.b.flags &
612 XFS_BTCUR_BPRV_WASDEL) == 0);
613 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
614 &logflags, delta, whichfork)))
617 xfs_bmbt_irec_t prev; /* old extent at offset idx */
620 * Get the record referred to by idx.
622 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
624 * If it's a real allocation record, and the new allocation ends
625 * after the start of the referred to record, then we're filling
626 * in a delayed or unwritten allocation with a real one, or
627 * converting real back to unwritten.
629 if (!ISNULLSTARTBLOCK(new->br_startblock) &&
630 new->br_startoff + new->br_blockcount > prev.br_startoff) {
631 if (prev.br_state != XFS_EXT_UNWRITTEN &&
632 ISNULLSTARTBLOCK(prev.br_startblock)) {
633 da_old = STARTBLOCKVAL(prev.br_startblock);
635 ASSERT(cur->bc_private.b.flags &
636 XFS_BTCUR_BPRV_WASDEL);
637 if ((error = xfs_bmap_add_extent_delay_real(ip,
638 idx, &cur, new, &da_new, first, flist,
639 &logflags, delta, rsvd)))
641 } else if (new->br_state == XFS_EXT_NORM) {
642 ASSERT(new->br_state == XFS_EXT_NORM);
643 if ((error = xfs_bmap_add_extent_unwritten_real(
644 ip, idx, &cur, new, &logflags, delta)))
647 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
648 if ((error = xfs_bmap_add_extent_unwritten_real(
649 ip, idx, &cur, new, &logflags, delta)))
652 ASSERT(*curp == cur || *curp == NULL);
655 * Otherwise we're filling in a hole with an allocation.
659 ASSERT((cur->bc_private.b.flags &
660 XFS_BTCUR_BPRV_WASDEL) == 0);
661 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
662 new, &logflags, delta, whichfork)))
667 ASSERT(*curp == cur || *curp == NULL);
669 * Convert to a btree if necessary.
671 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
672 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
673 int tmp_logflags; /* partial log flag return val */
676 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
677 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
678 logflags |= tmp_logflags;
683 * Adjust for changes in reserved delayed indirect blocks.
684 * Nothing to do for disk quotas here.
686 if (da_old || da_new) {
691 nblks += cur->bc_private.b.allocated;
692 ASSERT(nblks <= da_old);
694 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
695 (int64_t)(da_old - nblks), rsvd);
698 * Clear out the allocated field, done with it now in any case.
701 cur->bc_private.b.allocated = 0;
707 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
709 *logflagsp = logflags;
714 * Called by xfs_bmap_add_extent to handle cases converting a delayed
715 * allocation to a real allocation.
717 STATIC int /* error */
718 xfs_bmap_add_extent_delay_real(
719 xfs_inode_t *ip, /* incore inode pointer */
720 xfs_extnum_t idx, /* extent number to update/insert */
721 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
722 xfs_bmbt_irec_t *new, /* new data to add to file extents */
723 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
724 xfs_fsblock_t *first, /* pointer to firstblock variable */
725 xfs_bmap_free_t *flist, /* list of extents to be freed */
726 int *logflagsp, /* inode logging flags */
727 xfs_extdelta_t *delta, /* Change made to incore extents */
728 int rsvd) /* OK to use reserved data block allocation */
730 xfs_btree_cur_t *cur; /* btree cursor */
731 int diff; /* temp value */
732 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
733 int error; /* error return value */
734 int i; /* temp state */
735 xfs_ifork_t *ifp; /* inode fork pointer */
736 xfs_fileoff_t new_endoff; /* end offset of new entry */
737 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
738 /* left is 0, right is 1, prev is 2 */
739 int rval=0; /* return value (logging flags) */
740 int state = 0;/* state bits, accessed thru macros */
741 xfs_filblks_t temp=0; /* value for dnew calculations */
742 xfs_filblks_t temp2=0;/* value for dnew calculations */
743 int tmp_rval; /* partial logging flags */
744 enum { /* bit number definitions for state */
745 LEFT_CONTIG, RIGHT_CONTIG,
746 LEFT_FILLING, RIGHT_FILLING,
747 LEFT_DELAY, RIGHT_DELAY,
748 LEFT_VALID, RIGHT_VALID
754 #define MASK(b) (1 << (b))
755 #define MASK2(a,b) (MASK(a) | MASK(b))
756 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
757 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
758 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
759 #define STATE_TEST(b) (state & MASK(b))
760 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
761 ((state &= ~MASK(b)), 0))
762 #define SWITCH_STATE \
763 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
766 * Set up a bunch of variables to make the tests simpler.
769 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
770 ep = xfs_iext_get_ext(ifp, idx);
771 xfs_bmbt_get_all(ep, &PREV);
772 new_endoff = new->br_startoff + new->br_blockcount;
773 ASSERT(PREV.br_startoff <= new->br_startoff);
774 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
776 * Set flags determining what part of the previous delayed allocation
777 * extent is being replaced by a real allocation.
779 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
780 STATE_SET(RIGHT_FILLING,
781 PREV.br_startoff + PREV.br_blockcount == new_endoff);
783 * Check and set flags if this segment has a left neighbor.
784 * Don't set contiguous if the combined extent would be too large.
786 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
787 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
788 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
790 STATE_SET(LEFT_CONTIG,
791 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
792 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
793 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
794 LEFT.br_state == new->br_state &&
795 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
797 * Check and set flags if this segment has a right neighbor.
798 * Don't set contiguous if the combined extent would be too large.
799 * Also check for all-three-contiguous being too large.
801 if (STATE_SET_TEST(RIGHT_VALID,
803 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
804 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
805 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
807 STATE_SET(RIGHT_CONTIG,
808 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
809 new_endoff == RIGHT.br_startoff &&
810 new->br_startblock + new->br_blockcount ==
811 RIGHT.br_startblock &&
812 new->br_state == RIGHT.br_state &&
813 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
814 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
815 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
816 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
820 * Switch out based on the FILLING and CONTIG state bits.
822 switch (SWITCH_STATE) {
824 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
826 * Filling in all of a previously delayed allocation extent.
827 * The left and right neighbors are both contiguous with new.
829 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
831 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
832 LEFT.br_blockcount + PREV.br_blockcount +
833 RIGHT.br_blockcount);
834 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
836 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
837 xfs_iext_remove(ifp, idx, 2);
838 ip->i_df.if_lastex = idx - 1;
839 ip->i_d.di_nextents--;
841 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
843 rval = XFS_ILOG_CORE;
844 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
846 RIGHT.br_blockcount, &i)))
848 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
849 if ((error = xfs_bmbt_delete(cur, &i)))
851 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
852 if ((error = xfs_btree_decrement(cur, 0, &i)))
854 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
855 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
859 RIGHT.br_blockcount, LEFT.br_state)))
863 /* DELTA: Three in-core extents are replaced by one. */
864 temp = LEFT.br_startoff;
865 temp2 = LEFT.br_blockcount +
870 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
872 * Filling in all of a previously delayed allocation extent.
873 * The left neighbor is contiguous, the right is not.
875 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
877 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
878 LEFT.br_blockcount + PREV.br_blockcount);
879 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
881 ip->i_df.if_lastex = idx - 1;
882 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
883 xfs_iext_remove(ifp, idx, 1);
885 rval = XFS_ILOG_DEXT;
888 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
889 LEFT.br_startblock, LEFT.br_blockcount,
892 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
893 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
896 PREV.br_blockcount, LEFT.br_state)))
900 /* DELTA: Two in-core extents are replaced by one. */
901 temp = LEFT.br_startoff;
902 temp2 = LEFT.br_blockcount +
906 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
908 * Filling in all of a previously delayed allocation extent.
909 * The right neighbor is contiguous, the left is not.
911 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
912 xfs_bmbt_set_startblock(ep, new->br_startblock);
913 xfs_bmbt_set_blockcount(ep,
914 PREV.br_blockcount + RIGHT.br_blockcount);
915 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
916 ip->i_df.if_lastex = idx;
917 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
918 xfs_iext_remove(ifp, idx + 1, 1);
920 rval = XFS_ILOG_DEXT;
923 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
925 RIGHT.br_blockcount, &i)))
927 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
928 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
931 RIGHT.br_blockcount, PREV.br_state)))
935 /* DELTA: Two in-core extents are replaced by one. */
936 temp = PREV.br_startoff;
937 temp2 = PREV.br_blockcount +
941 case MASK2(LEFT_FILLING, RIGHT_FILLING):
943 * Filling in all of a previously delayed allocation extent.
944 * Neither the left nor right neighbors are contiguous with
947 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
948 xfs_bmbt_set_startblock(ep, new->br_startblock);
949 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
950 ip->i_df.if_lastex = idx;
951 ip->i_d.di_nextents++;
953 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
955 rval = XFS_ILOG_CORE;
956 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
957 new->br_startblock, new->br_blockcount,
960 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
961 cur->bc_rec.b.br_state = XFS_EXT_NORM;
962 if ((error = xfs_bmbt_insert(cur, &i)))
964 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
967 /* DELTA: The in-core extent described by new changed type. */
968 temp = new->br_startoff;
969 temp2 = new->br_blockcount;
972 case MASK2(LEFT_FILLING, LEFT_CONTIG):
974 * Filling in the first part of a previous delayed allocation.
975 * The left neighbor is contiguous.
977 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
978 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
979 LEFT.br_blockcount + new->br_blockcount);
980 xfs_bmbt_set_startoff(ep,
981 PREV.br_startoff + new->br_blockcount);
982 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
983 temp = PREV.br_blockcount - new->br_blockcount;
984 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
985 xfs_bmbt_set_blockcount(ep, temp);
986 ip->i_df.if_lastex = idx - 1;
988 rval = XFS_ILOG_DEXT;
991 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
992 LEFT.br_startblock, LEFT.br_blockcount,
995 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
996 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1003 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1004 STARTBLOCKVAL(PREV.br_startblock));
1005 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1006 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
1008 /* DELTA: The boundary between two in-core extents moved. */
1009 temp = LEFT.br_startoff;
1010 temp2 = LEFT.br_blockcount +
1014 case MASK(LEFT_FILLING):
1016 * Filling in the first part of a previous delayed allocation.
1017 * The left neighbor is not contiguous.
1019 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
1020 xfs_bmbt_set_startoff(ep, new_endoff);
1021 temp = PREV.br_blockcount - new->br_blockcount;
1022 xfs_bmbt_set_blockcount(ep, temp);
1023 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
1025 xfs_iext_insert(ifp, idx, 1, new);
1026 ip->i_df.if_lastex = idx;
1027 ip->i_d.di_nextents++;
1029 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1031 rval = XFS_ILOG_CORE;
1032 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1033 new->br_startblock, new->br_blockcount,
1036 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1037 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1038 if ((error = xfs_bmbt_insert(cur, &i)))
1040 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1042 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1043 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1044 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1045 first, flist, &cur, 1, &tmp_rval,
1051 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1052 STARTBLOCKVAL(PREV.br_startblock) -
1053 (cur ? cur->bc_private.b.allocated : 0));
1054 ep = xfs_iext_get_ext(ifp, idx + 1);
1055 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1056 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx + 1, XFS_DATA_FORK);
1058 /* DELTA: One in-core extent is split in two. */
1059 temp = PREV.br_startoff;
1060 temp2 = PREV.br_blockcount;
1063 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1065 * Filling in the last part of a previous delayed allocation.
1066 * The right neighbor is contiguous with the new allocation.
1068 temp = PREV.br_blockcount - new->br_blockcount;
1069 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
1070 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
1071 xfs_bmbt_set_blockcount(ep, temp);
1072 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1073 new->br_startoff, new->br_startblock,
1074 new->br_blockcount + RIGHT.br_blockcount,
1076 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
1077 ip->i_df.if_lastex = idx + 1;
1079 rval = XFS_ILOG_DEXT;
1082 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1083 RIGHT.br_startblock,
1084 RIGHT.br_blockcount, &i)))
1086 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1087 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1089 new->br_blockcount +
1090 RIGHT.br_blockcount,
1094 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1095 STARTBLOCKVAL(PREV.br_startblock));
1096 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1097 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
1099 /* DELTA: The boundary between two in-core extents moved. */
1100 temp = PREV.br_startoff;
1101 temp2 = PREV.br_blockcount +
1102 RIGHT.br_blockcount;
1105 case MASK(RIGHT_FILLING):
1107 * Filling in the last part of a previous delayed allocation.
1108 * The right neighbor is not contiguous.
1110 temp = PREV.br_blockcount - new->br_blockcount;
1111 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1112 xfs_bmbt_set_blockcount(ep, temp);
1113 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1115 xfs_iext_insert(ifp, idx + 1, 1, new);
1116 ip->i_df.if_lastex = idx + 1;
1117 ip->i_d.di_nextents++;
1119 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1121 rval = XFS_ILOG_CORE;
1122 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1123 new->br_startblock, new->br_blockcount,
1126 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1127 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1128 if ((error = xfs_bmbt_insert(cur, &i)))
1130 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1132 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1133 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1134 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1135 first, flist, &cur, 1, &tmp_rval,
1141 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1142 STARTBLOCKVAL(PREV.br_startblock) -
1143 (cur ? cur->bc_private.b.allocated : 0));
1144 ep = xfs_iext_get_ext(ifp, idx);
1145 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1146 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1148 /* DELTA: One in-core extent is split in two. */
1149 temp = PREV.br_startoff;
1150 temp2 = PREV.br_blockcount;
1155 * Filling in the middle part of a previous delayed allocation.
1156 * Contiguity is impossible here.
1157 * This case is avoided almost all the time.
1159 temp = new->br_startoff - PREV.br_startoff;
1160 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
1161 xfs_bmbt_set_blockcount(ep, temp);
1163 r[1].br_state = PREV.br_state;
1164 r[1].br_startblock = 0;
1165 r[1].br_startoff = new_endoff;
1166 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1167 r[1].br_blockcount = temp2;
1168 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
1170 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
1171 ip->i_df.if_lastex = idx + 1;
1172 ip->i_d.di_nextents++;
1174 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1176 rval = XFS_ILOG_CORE;
1177 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1178 new->br_startblock, new->br_blockcount,
1181 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1182 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1183 if ((error = xfs_bmbt_insert(cur, &i)))
1185 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1187 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1188 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1189 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1190 first, flist, &cur, 1, &tmp_rval,
1196 temp = xfs_bmap_worst_indlen(ip, temp);
1197 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1198 diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) -
1199 (cur ? cur->bc_private.b.allocated : 0));
1201 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
1203 * Ick gross gag me with a spoon.
1205 ASSERT(0); /* want to see if this ever happens! */
1211 !xfs_mod_incore_sb(ip->i_mount,
1212 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1219 !xfs_mod_incore_sb(ip->i_mount,
1220 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1225 ep = xfs_iext_get_ext(ifp, idx);
1226 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1227 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
1228 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
1229 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1230 NULLSTARTBLOCK((int)temp2));
1231 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
1232 *dnew = temp + temp2;
1233 /* DELTA: One in-core extent is split in three. */
1234 temp = PREV.br_startoff;
1235 temp2 = PREV.br_blockcount;
1238 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1239 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1240 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1241 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1242 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1243 case MASK(LEFT_CONTIG):
1244 case MASK(RIGHT_CONTIG):
1246 * These cases are all impossible.
1253 if (delta->xed_startoff > temp)
1254 delta->xed_startoff = temp;
1255 if (delta->xed_blockcount < temp2)
1256 delta->xed_blockcount = temp2;
1270 #undef STATE_SET_TEST
1275 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1276 * allocation to a real allocation or vice versa.
1278 STATIC int /* error */
1279 xfs_bmap_add_extent_unwritten_real(
1280 xfs_inode_t *ip, /* incore inode pointer */
1281 xfs_extnum_t idx, /* extent number to update/insert */
1282 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
1283 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1284 int *logflagsp, /* inode logging flags */
1285 xfs_extdelta_t *delta) /* Change made to incore extents */
1287 xfs_btree_cur_t *cur; /* btree cursor */
1288 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1289 int error; /* error return value */
1290 int i; /* temp state */
1291 xfs_ifork_t *ifp; /* inode fork pointer */
1292 xfs_fileoff_t new_endoff; /* end offset of new entry */
1293 xfs_exntst_t newext; /* new extent state */
1294 xfs_exntst_t oldext; /* old extent state */
1295 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1296 /* left is 0, right is 1, prev is 2 */
1297 int rval=0; /* return value (logging flags) */
1298 int state = 0;/* state bits, accessed thru macros */
1299 xfs_filblks_t temp=0;
1300 xfs_filblks_t temp2=0;
1301 enum { /* bit number definitions for state */
1302 LEFT_CONTIG, RIGHT_CONTIG,
1303 LEFT_FILLING, RIGHT_FILLING,
1304 LEFT_DELAY, RIGHT_DELAY,
1305 LEFT_VALID, RIGHT_VALID
1311 #define MASK(b) (1 << (b))
1312 #define MASK2(a,b) (MASK(a) | MASK(b))
1313 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
1314 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
1315 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1316 #define STATE_TEST(b) (state & MASK(b))
1317 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1318 ((state &= ~MASK(b)), 0))
1319 #define SWITCH_STATE \
1320 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
1323 * Set up a bunch of variables to make the tests simpler.
1327 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1328 ep = xfs_iext_get_ext(ifp, idx);
1329 xfs_bmbt_get_all(ep, &PREV);
1330 newext = new->br_state;
1331 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1332 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1333 ASSERT(PREV.br_state == oldext);
1334 new_endoff = new->br_startoff + new->br_blockcount;
1335 ASSERT(PREV.br_startoff <= new->br_startoff);
1336 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1338 * Set flags determining what part of the previous oldext allocation
1339 * extent is being replaced by a newext allocation.
1341 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
1342 STATE_SET(RIGHT_FILLING,
1343 PREV.br_startoff + PREV.br_blockcount == new_endoff);
1345 * Check and set flags if this segment has a left neighbor.
1346 * Don't set contiguous if the combined extent would be too large.
1348 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1349 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
1350 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
1352 STATE_SET(LEFT_CONTIG,
1353 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
1354 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1355 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1356 LEFT.br_state == newext &&
1357 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1359 * Check and set flags if this segment has a right neighbor.
1360 * Don't set contiguous if the combined extent would be too large.
1361 * Also check for all-three-contiguous being too large.
1363 if (STATE_SET_TEST(RIGHT_VALID,
1365 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
1366 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
1367 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
1369 STATE_SET(RIGHT_CONTIG,
1370 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
1371 new_endoff == RIGHT.br_startoff &&
1372 new->br_startblock + new->br_blockcount ==
1373 RIGHT.br_startblock &&
1374 newext == RIGHT.br_state &&
1375 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1376 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
1377 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
1378 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1381 * Switch out based on the FILLING and CONTIG state bits.
1383 switch (SWITCH_STATE) {
1385 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1387 * Setting all of a previous oldext extent to newext.
1388 * The left and right neighbors are both contiguous with new.
1390 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
1392 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1393 LEFT.br_blockcount + PREV.br_blockcount +
1394 RIGHT.br_blockcount);
1395 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
1397 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
1398 xfs_iext_remove(ifp, idx, 2);
1399 ip->i_df.if_lastex = idx - 1;
1400 ip->i_d.di_nextents -= 2;
1402 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1404 rval = XFS_ILOG_CORE;
1405 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1406 RIGHT.br_startblock,
1407 RIGHT.br_blockcount, &i)))
1409 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1410 if ((error = xfs_bmbt_delete(cur, &i)))
1412 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1413 if ((error = xfs_btree_decrement(cur, 0, &i)))
1415 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1416 if ((error = xfs_bmbt_delete(cur, &i)))
1418 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1419 if ((error = xfs_btree_decrement(cur, 0, &i)))
1421 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1422 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1424 LEFT.br_blockcount + PREV.br_blockcount +
1425 RIGHT.br_blockcount, LEFT.br_state)))
1428 /* DELTA: Three in-core extents are replaced by one. */
1429 temp = LEFT.br_startoff;
1430 temp2 = LEFT.br_blockcount +
1431 PREV.br_blockcount +
1432 RIGHT.br_blockcount;
1435 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
1437 * Setting all of a previous oldext extent to newext.
1438 * The left neighbor is contiguous, the right is not.
1440 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
1442 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1443 LEFT.br_blockcount + PREV.br_blockcount);
1444 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
1446 ip->i_df.if_lastex = idx - 1;
1447 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
1448 xfs_iext_remove(ifp, idx, 1);
1449 ip->i_d.di_nextents--;
1451 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1453 rval = XFS_ILOG_CORE;
1454 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1455 PREV.br_startblock, PREV.br_blockcount,
1458 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1459 if ((error = xfs_bmbt_delete(cur, &i)))
1461 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1462 if ((error = xfs_btree_decrement(cur, 0, &i)))
1464 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1465 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1467 LEFT.br_blockcount + PREV.br_blockcount,
1471 /* DELTA: Two in-core extents are replaced by one. */
1472 temp = LEFT.br_startoff;
1473 temp2 = LEFT.br_blockcount +
1477 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
1479 * Setting all of a previous oldext extent to newext.
1480 * The right neighbor is contiguous, the left is not.
1482 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx,
1484 xfs_bmbt_set_blockcount(ep,
1485 PREV.br_blockcount + RIGHT.br_blockcount);
1486 xfs_bmbt_set_state(ep, newext);
1487 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx,
1489 ip->i_df.if_lastex = idx;
1490 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
1491 xfs_iext_remove(ifp, idx + 1, 1);
1492 ip->i_d.di_nextents--;
1494 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1496 rval = XFS_ILOG_CORE;
1497 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1498 RIGHT.br_startblock,
1499 RIGHT.br_blockcount, &i)))
1501 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1502 if ((error = xfs_bmbt_delete(cur, &i)))
1504 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1505 if ((error = xfs_btree_decrement(cur, 0, &i)))
1507 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1508 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1510 new->br_blockcount + RIGHT.br_blockcount,
1514 /* DELTA: Two in-core extents are replaced by one. */
1515 temp = PREV.br_startoff;
1516 temp2 = PREV.br_blockcount +
1517 RIGHT.br_blockcount;
1520 case MASK2(LEFT_FILLING, RIGHT_FILLING):
1522 * Setting all of a previous oldext extent to newext.
1523 * Neither the left nor right neighbors are contiguous with
1526 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx,
1528 xfs_bmbt_set_state(ep, newext);
1529 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx,
1531 ip->i_df.if_lastex = idx;
1533 rval = XFS_ILOG_DEXT;
1536 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1537 new->br_startblock, new->br_blockcount,
1540 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1541 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1542 new->br_startblock, new->br_blockcount,
1546 /* DELTA: The in-core extent described by new changed type. */
1547 temp = new->br_startoff;
1548 temp2 = new->br_blockcount;
1551 case MASK2(LEFT_FILLING, LEFT_CONTIG):
1553 * Setting the first part of a previous oldext extent to newext.
1554 * The left neighbor is contiguous.
1556 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1,
1558 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1559 LEFT.br_blockcount + new->br_blockcount);
1560 xfs_bmbt_set_startoff(ep,
1561 PREV.br_startoff + new->br_blockcount);
1562 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1,
1564 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx,
1566 xfs_bmbt_set_startblock(ep,
1567 new->br_startblock + new->br_blockcount);
1568 xfs_bmbt_set_blockcount(ep,
1569 PREV.br_blockcount - new->br_blockcount);
1570 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx,
1572 ip->i_df.if_lastex = idx - 1;
1574 rval = XFS_ILOG_DEXT;
1577 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1578 PREV.br_startblock, PREV.br_blockcount,
1581 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1582 if ((error = xfs_bmbt_update(cur,
1583 PREV.br_startoff + new->br_blockcount,
1584 PREV.br_startblock + new->br_blockcount,
1585 PREV.br_blockcount - new->br_blockcount,
1588 if ((error = xfs_btree_decrement(cur, 0, &i)))
1590 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1592 LEFT.br_blockcount + new->br_blockcount,
1596 /* DELTA: The boundary between two in-core extents moved. */
1597 temp = LEFT.br_startoff;
1598 temp2 = LEFT.br_blockcount +
1602 case MASK(LEFT_FILLING):
1604 * Setting the first part of a previous oldext extent to newext.
1605 * The left neighbor is not contiguous.
1607 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
1608 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1609 xfs_bmbt_set_startoff(ep, new_endoff);
1610 xfs_bmbt_set_blockcount(ep,
1611 PREV.br_blockcount - new->br_blockcount);
1612 xfs_bmbt_set_startblock(ep,
1613 new->br_startblock + new->br_blockcount);
1614 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx, XFS_DATA_FORK);
1615 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
1617 xfs_iext_insert(ifp, idx, 1, new);
1618 ip->i_df.if_lastex = idx;
1619 ip->i_d.di_nextents++;
1621 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1623 rval = XFS_ILOG_CORE;
1624 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1625 PREV.br_startblock, PREV.br_blockcount,
1628 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1629 if ((error = xfs_bmbt_update(cur,
1630 PREV.br_startoff + new->br_blockcount,
1631 PREV.br_startblock + new->br_blockcount,
1632 PREV.br_blockcount - new->br_blockcount,
1635 cur->bc_rec.b = *new;
1636 if ((error = xfs_bmbt_insert(cur, &i)))
1638 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1640 /* DELTA: One in-core extent is split in two. */
1641 temp = PREV.br_startoff;
1642 temp2 = PREV.br_blockcount;
1645 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1647 * Setting the last part of a previous oldext extent to newext.
1648 * The right neighbor is contiguous with the new allocation.
1650 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx,
1652 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1,
1654 xfs_bmbt_set_blockcount(ep,
1655 PREV.br_blockcount - new->br_blockcount);
1656 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx,
1658 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1659 new->br_startoff, new->br_startblock,
1660 new->br_blockcount + RIGHT.br_blockcount, newext);
1661 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1,
1663 ip->i_df.if_lastex = idx + 1;
1665 rval = XFS_ILOG_DEXT;
1668 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1670 PREV.br_blockcount, &i)))
1672 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1673 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1675 PREV.br_blockcount - new->br_blockcount,
1678 if ((error = xfs_btree_increment(cur, 0, &i)))
1680 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1682 new->br_blockcount + RIGHT.br_blockcount,
1686 /* DELTA: The boundary between two in-core extents moved. */
1687 temp = PREV.br_startoff;
1688 temp2 = PREV.br_blockcount +
1689 RIGHT.br_blockcount;
1692 case MASK(RIGHT_FILLING):
1694 * Setting the last part of a previous oldext extent to newext.
1695 * The right neighbor is not contiguous.
1697 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1698 xfs_bmbt_set_blockcount(ep,
1699 PREV.br_blockcount - new->br_blockcount);
1700 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1701 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1703 xfs_iext_insert(ifp, idx + 1, 1, new);
1704 ip->i_df.if_lastex = idx + 1;
1705 ip->i_d.di_nextents++;
1707 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1709 rval = XFS_ILOG_CORE;
1710 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1711 PREV.br_startblock, PREV.br_blockcount,
1714 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1715 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1717 PREV.br_blockcount - new->br_blockcount,
1720 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1721 new->br_startblock, new->br_blockcount,
1724 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1725 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1726 if ((error = xfs_bmbt_insert(cur, &i)))
1728 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1730 /* DELTA: One in-core extent is split in two. */
1731 temp = PREV.br_startoff;
1732 temp2 = PREV.br_blockcount;
1737 * Setting the middle part of a previous oldext extent to
1738 * newext. Contiguity is impossible here.
1739 * One extent becomes three extents.
1741 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
1742 xfs_bmbt_set_blockcount(ep,
1743 new->br_startoff - PREV.br_startoff);
1744 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
1746 r[1].br_startoff = new_endoff;
1747 r[1].br_blockcount =
1748 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1749 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1750 r[1].br_state = oldext;
1751 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
1753 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
1754 ip->i_df.if_lastex = idx + 1;
1755 ip->i_d.di_nextents += 2;
1757 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1759 rval = XFS_ILOG_CORE;
1760 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1761 PREV.br_startblock, PREV.br_blockcount,
1764 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1765 /* new right extent - oldext */
1766 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1767 r[1].br_startblock, r[1].br_blockcount,
1770 /* new left extent - oldext */
1771 cur->bc_rec.b = PREV;
1772 cur->bc_rec.b.br_blockcount =
1773 new->br_startoff - PREV.br_startoff;
1774 if ((error = xfs_bmbt_insert(cur, &i)))
1776 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1778 * Reset the cursor to the position of the new extent
1779 * we are about to insert as we can't trust it after
1780 * the previous insert.
1782 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1783 new->br_startblock, new->br_blockcount,
1786 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1787 /* new middle extent - newext */
1788 cur->bc_rec.b.br_state = new->br_state;
1789 if ((error = xfs_bmbt_insert(cur, &i)))
1791 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1793 /* DELTA: One in-core extent is split in three. */
1794 temp = PREV.br_startoff;
1795 temp2 = PREV.br_blockcount;
1798 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1799 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1800 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1801 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1802 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1803 case MASK(LEFT_CONTIG):
1804 case MASK(RIGHT_CONTIG):
1806 * These cases are all impossible.
1813 if (delta->xed_startoff > temp)
1814 delta->xed_startoff = temp;
1815 if (delta->xed_blockcount < temp2)
1816 delta->xed_blockcount = temp2;
1830 #undef STATE_SET_TEST
1835 * Called by xfs_bmap_add_extent to handle cases converting a hole
1836 * to a delayed allocation.
1839 STATIC int /* error */
1840 xfs_bmap_add_extent_hole_delay(
1841 xfs_inode_t *ip, /* incore inode pointer */
1842 xfs_extnum_t idx, /* extent number to update/insert */
1843 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1844 int *logflagsp, /* inode logging flags */
1845 xfs_extdelta_t *delta, /* Change made to incore extents */
1846 int rsvd) /* OK to allocate reserved blocks */
1848 xfs_bmbt_rec_host_t *ep; /* extent record for idx */
1849 xfs_ifork_t *ifp; /* inode fork pointer */
1850 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1851 xfs_filblks_t newlen=0; /* new indirect size */
1852 xfs_filblks_t oldlen=0; /* old indirect size */
1853 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1854 int state; /* state bits, accessed thru macros */
1855 xfs_filblks_t temp=0; /* temp for indirect calculations */
1856 xfs_filblks_t temp2=0;
1857 enum { /* bit number definitions for state */
1858 LEFT_CONTIG, RIGHT_CONTIG,
1859 LEFT_DELAY, RIGHT_DELAY,
1860 LEFT_VALID, RIGHT_VALID
1863 #define MASK(b) (1 << (b))
1864 #define MASK2(a,b) (MASK(a) | MASK(b))
1865 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1866 #define STATE_TEST(b) (state & MASK(b))
1867 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1868 ((state &= ~MASK(b)), 0))
1869 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1871 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1872 ep = xfs_iext_get_ext(ifp, idx);
1874 ASSERT(ISNULLSTARTBLOCK(new->br_startblock));
1876 * Check and set flags if this segment has a left neighbor
1878 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1879 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1880 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1883 * Check and set flags if the current (right) segment exists.
1884 * If it doesn't exist, we're converting the hole at end-of-file.
1886 if (STATE_SET_TEST(RIGHT_VALID,
1888 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1889 xfs_bmbt_get_all(ep, &right);
1890 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1893 * Set contiguity flags on the left and right neighbors.
1894 * Don't let extents get too large, even if the pieces are contiguous.
1896 STATE_SET(LEFT_CONTIG,
1897 STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) &&
1898 left.br_startoff + left.br_blockcount == new->br_startoff &&
1899 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1900 STATE_SET(RIGHT_CONTIG,
1901 STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) &&
1902 new->br_startoff + new->br_blockcount == right.br_startoff &&
1903 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1904 (!STATE_TEST(LEFT_CONTIG) ||
1905 (left.br_blockcount + new->br_blockcount +
1906 right.br_blockcount <= MAXEXTLEN)));
1908 * Switch out based on the contiguity flags.
1910 switch (SWITCH_STATE) {
1912 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1914 * New allocation is contiguous with delayed allocations
1915 * on the left and on the right.
1916 * Merge all three into a single extent record.
1918 temp = left.br_blockcount + new->br_blockcount +
1919 right.br_blockcount;
1920 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
1922 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1923 oldlen = STARTBLOCKVAL(left.br_startblock) +
1924 STARTBLOCKVAL(new->br_startblock) +
1925 STARTBLOCKVAL(right.br_startblock);
1926 newlen = xfs_bmap_worst_indlen(ip, temp);
1927 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1928 NULLSTARTBLOCK((int)newlen));
1929 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
1931 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, XFS_DATA_FORK);
1932 xfs_iext_remove(ifp, idx, 1);
1933 ip->i_df.if_lastex = idx - 1;
1934 /* DELTA: Two in-core extents were replaced by one. */
1936 temp = left.br_startoff;
1939 case MASK(LEFT_CONTIG):
1941 * New allocation is contiguous with a delayed allocation
1943 * Merge the new allocation with the left neighbor.
1945 temp = left.br_blockcount + new->br_blockcount;
1946 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1,
1948 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1949 oldlen = STARTBLOCKVAL(left.br_startblock) +
1950 STARTBLOCKVAL(new->br_startblock);
1951 newlen = xfs_bmap_worst_indlen(ip, temp);
1952 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1953 NULLSTARTBLOCK((int)newlen));
1954 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1,
1956 ip->i_df.if_lastex = idx - 1;
1957 /* DELTA: One in-core extent grew into a hole. */
1959 temp = left.br_startoff;
1962 case MASK(RIGHT_CONTIG):
1964 * New allocation is contiguous with a delayed allocation
1966 * Merge the new allocation with the right neighbor.
1968 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, XFS_DATA_FORK);
1969 temp = new->br_blockcount + right.br_blockcount;
1970 oldlen = STARTBLOCKVAL(new->br_startblock) +
1971 STARTBLOCKVAL(right.br_startblock);
1972 newlen = xfs_bmap_worst_indlen(ip, temp);
1973 xfs_bmbt_set_allf(ep, new->br_startoff,
1974 NULLSTARTBLOCK((int)newlen), temp, right.br_state);
1975 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, XFS_DATA_FORK);
1976 ip->i_df.if_lastex = idx;
1977 /* DELTA: One in-core extent grew into a hole. */
1979 temp = new->br_startoff;
1984 * New allocation is not contiguous with another
1985 * delayed allocation.
1986 * Insert a new entry.
1988 oldlen = newlen = 0;
1989 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL,
1991 xfs_iext_insert(ifp, idx, 1, new);
1992 ip->i_df.if_lastex = idx;
1993 /* DELTA: A new in-core extent was added in a hole. */
1994 temp2 = new->br_blockcount;
1995 temp = new->br_startoff;
1998 if (oldlen != newlen) {
1999 ASSERT(oldlen > newlen);
2000 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
2001 (int64_t)(oldlen - newlen), rsvd);
2003 * Nothing to do for disk quota accounting here.
2008 if (delta->xed_startoff > temp)
2009 delta->xed_startoff = temp;
2010 if (delta->xed_blockcount < temp2)
2011 delta->xed_blockcount = temp2;
2019 #undef STATE_SET_TEST
2024 * Called by xfs_bmap_add_extent to handle cases converting a hole
2025 * to a real allocation.
2027 STATIC int /* error */
2028 xfs_bmap_add_extent_hole_real(
2029 xfs_inode_t *ip, /* incore inode pointer */
2030 xfs_extnum_t idx, /* extent number to update/insert */
2031 xfs_btree_cur_t *cur, /* if null, not a btree */
2032 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2033 int *logflagsp, /* inode logging flags */
2034 xfs_extdelta_t *delta, /* Change made to incore extents */
2035 int whichfork) /* data or attr fork */
2037 xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */
2038 int error; /* error return value */
2039 int i; /* temp state */
2040 xfs_ifork_t *ifp; /* inode fork pointer */
2041 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2042 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2043 int rval=0; /* return value (logging flags) */
2044 int state; /* state bits, accessed thru macros */
2045 xfs_filblks_t temp=0;
2046 xfs_filblks_t temp2=0;
2047 enum { /* bit number definitions for state */
2048 LEFT_CONTIG, RIGHT_CONTIG,
2049 LEFT_DELAY, RIGHT_DELAY,
2050 LEFT_VALID, RIGHT_VALID
2053 #define MASK(b) (1 << (b))
2054 #define MASK2(a,b) (MASK(a) | MASK(b))
2055 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
2056 #define STATE_TEST(b) (state & MASK(b))
2057 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
2058 ((state &= ~MASK(b)), 0))
2059 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
2061 ifp = XFS_IFORK_PTR(ip, whichfork);
2062 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
2063 ep = xfs_iext_get_ext(ifp, idx);
2066 * Check and set flags if this segment has a left neighbor.
2068 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
2069 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
2070 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
2073 * Check and set flags if this segment has a current value.
2074 * Not true if we're inserting into the "hole" at eof.
2076 if (STATE_SET_TEST(RIGHT_VALID,
2078 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
2079 xfs_bmbt_get_all(ep, &right);
2080 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
2083 * We're inserting a real allocation between "left" and "right".
2084 * Set the contiguity flags. Don't let extents get too large.
2086 STATE_SET(LEFT_CONTIG,
2087 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
2088 left.br_startoff + left.br_blockcount == new->br_startoff &&
2089 left.br_startblock + left.br_blockcount == new->br_startblock &&
2090 left.br_state == new->br_state &&
2091 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
2092 STATE_SET(RIGHT_CONTIG,
2093 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
2094 new->br_startoff + new->br_blockcount == right.br_startoff &&
2095 new->br_startblock + new->br_blockcount ==
2096 right.br_startblock &&
2097 new->br_state == right.br_state &&
2098 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2099 (!STATE_TEST(LEFT_CONTIG) ||
2100 left.br_blockcount + new->br_blockcount +
2101 right.br_blockcount <= MAXEXTLEN));
2105 * Select which case we're in here, and implement it.
2107 switch (SWITCH_STATE) {
2109 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
2111 * New allocation is contiguous with real allocations on the
2112 * left and on the right.
2113 * Merge all three into a single extent record.
2115 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
2117 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2118 left.br_blockcount + new->br_blockcount +
2119 right.br_blockcount);
2120 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
2122 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, whichfork);
2123 xfs_iext_remove(ifp, idx, 1);
2124 ifp->if_lastex = idx - 1;
2125 XFS_IFORK_NEXT_SET(ip, whichfork,
2126 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2128 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2130 rval = XFS_ILOG_CORE;
2131 if ((error = xfs_bmbt_lookup_eq(cur,
2133 right.br_startblock,
2134 right.br_blockcount, &i)))
2136 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2137 if ((error = xfs_bmbt_delete(cur, &i)))
2139 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2140 if ((error = xfs_btree_decrement(cur, 0, &i)))
2142 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2143 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2145 left.br_blockcount +
2146 new->br_blockcount +
2147 right.br_blockcount,
2151 /* DELTA: Two in-core extents were replaced by one. */
2152 temp = left.br_startoff;
2153 temp2 = left.br_blockcount +
2154 new->br_blockcount +
2155 right.br_blockcount;
2158 case MASK(LEFT_CONTIG):
2160 * New allocation is contiguous with a real allocation
2162 * Merge the new allocation with the left neighbor.
2164 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1, whichfork);
2165 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2166 left.br_blockcount + new->br_blockcount);
2167 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1, whichfork);
2168 ifp->if_lastex = idx - 1;
2170 rval = XFS_ILOG_FEXT(whichfork);
2173 if ((error = xfs_bmbt_lookup_eq(cur,
2176 left.br_blockcount, &i)))
2178 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2179 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2181 left.br_blockcount +
2186 /* DELTA: One in-core extent grew. */
2187 temp = left.br_startoff;
2188 temp2 = left.br_blockcount +
2192 case MASK(RIGHT_CONTIG):
2194 * New allocation is contiguous with a real allocation
2196 * Merge the new allocation with the right neighbor.
2198 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, whichfork);
2199 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2200 new->br_blockcount + right.br_blockcount,
2202 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, whichfork);
2203 ifp->if_lastex = idx;
2205 rval = XFS_ILOG_FEXT(whichfork);
2208 if ((error = xfs_bmbt_lookup_eq(cur,
2210 right.br_startblock,
2211 right.br_blockcount, &i)))
2213 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2214 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2216 new->br_blockcount +
2217 right.br_blockcount,
2221 /* DELTA: One in-core extent grew. */
2222 temp = new->br_startoff;
2223 temp2 = new->br_blockcount +
2224 right.br_blockcount;
2229 * New allocation is not contiguous with another
2231 * Insert a new entry.
2233 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL, whichfork);
2234 xfs_iext_insert(ifp, idx, 1, new);
2235 ifp->if_lastex = idx;
2236 XFS_IFORK_NEXT_SET(ip, whichfork,
2237 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2239 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2241 rval = XFS_ILOG_CORE;
2242 if ((error = xfs_bmbt_lookup_eq(cur,
2245 new->br_blockcount, &i)))
2247 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
2248 cur->bc_rec.b.br_state = new->br_state;
2249 if ((error = xfs_bmbt_insert(cur, &i)))
2251 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2253 /* DELTA: A new extent was added in a hole. */
2254 temp = new->br_startoff;
2255 temp2 = new->br_blockcount;
2260 if (delta->xed_startoff > temp)
2261 delta->xed_startoff = temp;
2262 if (delta->xed_blockcount < temp2)
2263 delta->xed_blockcount = temp2;
2272 #undef STATE_SET_TEST
2277 * Adjust the size of the new extent based on di_extsize and rt extsize.
2280 xfs_bmap_extsize_align(
2282 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2283 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2284 xfs_extlen_t extsz, /* align to this extent size */
2285 int rt, /* is this a realtime inode? */
2286 int eof, /* is extent at end-of-file? */
2287 int delay, /* creating delalloc extent? */
2288 int convert, /* overwriting unwritten extent? */
2289 xfs_fileoff_t *offp, /* in/out: aligned offset */
2290 xfs_extlen_t *lenp) /* in/out: aligned length */
2292 xfs_fileoff_t orig_off; /* original offset */
2293 xfs_extlen_t orig_alen; /* original length */
2294 xfs_fileoff_t orig_end; /* original off+len */
2295 xfs_fileoff_t nexto; /* next file offset */
2296 xfs_fileoff_t prevo; /* previous file offset */
2297 xfs_fileoff_t align_off; /* temp for offset */
2298 xfs_extlen_t align_alen; /* temp for length */
2299 xfs_extlen_t temp; /* temp for calculations */
2304 orig_off = align_off = *offp;
2305 orig_alen = align_alen = *lenp;
2306 orig_end = orig_off + orig_alen;
2309 * If this request overlaps an existing extent, then don't
2310 * attempt to perform any additional alignment.
2312 if (!delay && !eof &&
2313 (orig_off >= gotp->br_startoff) &&
2314 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2319 * If the file offset is unaligned vs. the extent size
2320 * we need to align it. This will be possible unless
2321 * the file was previously written with a kernel that didn't
2322 * perform this alignment, or if a truncate shot us in the
2325 temp = do_mod(orig_off, extsz);
2331 * Same adjustment for the end of the requested area.
2333 if ((temp = (align_alen % extsz))) {
2334 align_alen += extsz - temp;
2337 * If the previous block overlaps with this proposed allocation
2338 * then move the start forward without adjusting the length.
2340 if (prevp->br_startoff != NULLFILEOFF) {
2341 if (prevp->br_startblock == HOLESTARTBLOCK)
2342 prevo = prevp->br_startoff;
2344 prevo = prevp->br_startoff + prevp->br_blockcount;
2347 if (align_off != orig_off && align_off < prevo)
2350 * If the next block overlaps with this proposed allocation
2351 * then move the start back without adjusting the length,
2352 * but not before offset 0.
2353 * This may of course make the start overlap previous block,
2354 * and if we hit the offset 0 limit then the next block
2355 * can still overlap too.
2357 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2358 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2359 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2360 nexto = gotp->br_startoff + gotp->br_blockcount;
2362 nexto = gotp->br_startoff;
2364 nexto = NULLFILEOFF;
2366 align_off + align_alen != orig_end &&
2367 align_off + align_alen > nexto)
2368 align_off = nexto > align_alen ? nexto - align_alen : 0;
2370 * If we're now overlapping the next or previous extent that
2371 * means we can't fit an extsz piece in this hole. Just move
2372 * the start forward to the first valid spot and set
2373 * the length so we hit the end.
2375 if (align_off != orig_off && align_off < prevo)
2377 if (align_off + align_alen != orig_end &&
2378 align_off + align_alen > nexto &&
2379 nexto != NULLFILEOFF) {
2380 ASSERT(nexto > prevo);
2381 align_alen = nexto - align_off;
2385 * If realtime, and the result isn't a multiple of the realtime
2386 * extent size we need to remove blocks until it is.
2388 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2390 * We're not covering the original request, or
2391 * we won't be able to once we fix the length.
2393 if (orig_off < align_off ||
2394 orig_end > align_off + align_alen ||
2395 align_alen - temp < orig_alen)
2396 return XFS_ERROR(EINVAL);
2398 * Try to fix it by moving the start up.
2400 if (align_off + temp <= orig_off) {
2405 * Try to fix it by moving the end in.
2407 else if (align_off + align_alen - temp >= orig_end)
2410 * Set the start to the minimum then trim the length.
2413 align_alen -= orig_off - align_off;
2414 align_off = orig_off;
2415 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2418 * Result doesn't cover the request, fail it.
2420 if (orig_off < align_off || orig_end > align_off + align_alen)
2421 return XFS_ERROR(EINVAL);
2423 ASSERT(orig_off >= align_off);
2424 ASSERT(orig_end <= align_off + align_alen);
2428 if (!eof && gotp->br_startoff != NULLFILEOFF)
2429 ASSERT(align_off + align_alen <= gotp->br_startoff);
2430 if (prevp->br_startoff != NULLFILEOFF)
2431 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2439 #define XFS_ALLOC_GAP_UNITS 4
2443 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2445 xfs_fsblock_t adjust; /* adjustment to block numbers */
2446 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2447 xfs_mount_t *mp; /* mount point structure */
2448 int nullfb; /* true if ap->firstblock isn't set */
2449 int rt; /* true if inode is realtime */
2451 #define ISVALID(x,y) \
2453 (x) < mp->m_sb.sb_rblocks : \
2454 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2455 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2456 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2458 mp = ap->ip->i_mount;
2459 nullfb = ap->firstblock == NULLFSBLOCK;
2460 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2461 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2463 * If allocating at eof, and there's a previous real block,
2464 * try to use it's last block as our starting point.
2466 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2467 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2468 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2469 ap->prevp->br_startblock)) {
2470 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2472 * Adjust for the gap between prevp and us.
2475 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2477 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2481 * If not at eof, then compare the two neighbor blocks.
2482 * Figure out whether either one gives us a good starting point,
2483 * and pick the better one.
2485 else if (!ap->eof) {
2486 xfs_fsblock_t gotbno; /* right side block number */
2487 xfs_fsblock_t gotdiff=0; /* right side difference */
2488 xfs_fsblock_t prevbno; /* left side block number */
2489 xfs_fsblock_t prevdiff=0; /* left side difference */
2492 * If there's a previous (left) block, select a requested
2493 * start block based on it.
2495 if (ap->prevp->br_startoff != NULLFILEOFF &&
2496 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2497 (prevbno = ap->prevp->br_startblock +
2498 ap->prevp->br_blockcount) &&
2499 ISVALID(prevbno, ap->prevp->br_startblock)) {
2501 * Calculate gap to end of previous block.
2503 adjust = prevdiff = ap->off -
2504 (ap->prevp->br_startoff +
2505 ap->prevp->br_blockcount);
2507 * Figure the startblock based on the previous block's
2508 * end and the gap size.
2510 * If the gap is large relative to the piece we're
2511 * allocating, or using it gives us an invalid block
2512 * number, then just use the end of the previous block.
2514 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2515 ISVALID(prevbno + prevdiff,
2516 ap->prevp->br_startblock))
2521 * If the firstblock forbids it, can't use it,
2524 if (!rt && !nullfb &&
2525 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2526 prevbno = NULLFSBLOCK;
2529 * No previous block or can't follow it, just default.
2532 prevbno = NULLFSBLOCK;
2534 * If there's a following (right) block, select a requested
2535 * start block based on it.
2537 if (!ISNULLSTARTBLOCK(ap->gotp->br_startblock)) {
2539 * Calculate gap to start of next block.
2541 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2543 * Figure the startblock based on the next block's
2544 * start and the gap size.
2546 gotbno = ap->gotp->br_startblock;
2549 * If the gap is large relative to the piece we're
2550 * allocating, or using it gives us an invalid block
2551 * number, then just use the start of the next block
2552 * offset by our length.
2554 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2555 ISVALID(gotbno - gotdiff, gotbno))
2557 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2559 gotdiff += adjust - ap->alen;
2563 * If the firstblock forbids it, can't use it,
2566 if (!rt && !nullfb &&
2567 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2568 gotbno = NULLFSBLOCK;
2571 * No next block, just default.
2574 gotbno = NULLFSBLOCK;
2576 * If both valid, pick the better one, else the only good
2577 * one, else ap->rval is already set (to 0 or the inode block).
2579 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2580 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2581 else if (prevbno != NULLFSBLOCK)
2583 else if (gotbno != NULLFSBLOCK)
2591 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2593 xfs_alloctype_t atype = 0; /* type for allocation routines */
2594 int error; /* error return value */
2595 xfs_mount_t *mp; /* mount point structure */
2596 xfs_extlen_t prod = 0; /* product factor for allocators */
2597 xfs_extlen_t ralen = 0; /* realtime allocation length */
2598 xfs_extlen_t align; /* minimum allocation alignment */
2601 mp = ap->ip->i_mount;
2602 align = xfs_get_extsz_hint(ap->ip);
2603 prod = align / mp->m_sb.sb_rextsize;
2604 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2605 align, 1, ap->eof, 0,
2606 ap->conv, &ap->off, &ap->alen);
2610 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2613 * If the offset & length are not perfectly aligned
2614 * then kill prod, it will just get us in trouble.
2616 if (do_mod(ap->off, align) || ap->alen % align)
2619 * Set ralen to be the actual requested length in rtextents.
2621 ralen = ap->alen / mp->m_sb.sb_rextsize;
2623 * If the old value was close enough to MAXEXTLEN that
2624 * we rounded up to it, cut it back so it's valid again.
2625 * Note that if it's a really large request (bigger than
2626 * MAXEXTLEN), we don't hear about that number, and can't
2627 * adjust the starting point to match it.
2629 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2630 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2632 * If it's an allocation to an empty file at offset 0,
2633 * pick an extent that will space things out in the rt area.
2635 if (ap->eof && ap->off == 0) {
2636 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2638 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2641 ap->rval = rtx * mp->m_sb.sb_rextsize;
2646 xfs_bmap_adjacent(ap);
2649 * Realtime allocation, done through xfs_rtallocate_extent.
2651 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2652 do_div(ap->rval, mp->m_sb.sb_rextsize);
2655 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2656 &ralen, atype, ap->wasdel, prod, &rtb)))
2658 if (rtb == NULLFSBLOCK && prod > 1 &&
2659 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2660 ap->alen, &ralen, atype,
2661 ap->wasdel, 1, &rtb)))
2664 if (ap->rval != NULLFSBLOCK) {
2665 ap->rval *= mp->m_sb.sb_rextsize;
2666 ralen *= mp->m_sb.sb_rextsize;
2668 ap->ip->i_d.di_nblocks += ralen;
2669 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2671 ap->ip->i_delayed_blks -= ralen;
2673 * Adjust the disk quota also. This was reserved
2676 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2677 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2678 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2687 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2689 xfs_mount_t *mp; /* mount point structure */
2690 xfs_alloctype_t atype = 0; /* type for allocation routines */
2691 xfs_extlen_t align; /* minimum allocation alignment */
2693 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2694 xfs_agnumber_t startag;
2695 xfs_alloc_arg_t args;
2698 xfs_extlen_t longest;
2700 xfs_extlen_t nextminlen = 0;
2702 int nullfb; /* true if ap->firstblock isn't set */
2708 mp = ap->ip->i_mount;
2709 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
2710 if (unlikely(align)) {
2711 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2712 align, 0, ap->eof, 0, ap->conv,
2713 &ap->off, &ap->alen);
2717 nullfb = ap->firstblock == NULLFSBLOCK;
2718 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2720 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2721 ag = xfs_filestream_lookup_ag(ap->ip);
2722 ag = (ag != NULLAGNUMBER) ? ag : 0;
2723 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2725 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2728 ap->rval = ap->firstblock;
2730 xfs_bmap_adjacent(ap);
2733 * If allowed, use ap->rval; otherwise must use firstblock since
2734 * it's in the right allocation group.
2736 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2739 ap->rval = ap->firstblock;
2741 * Normal allocation, done through xfs_alloc_vextent.
2743 tryagain = isaligned = 0;
2746 args.fsbno = ap->rval;
2747 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
2748 args.firstblock = ap->firstblock;
2751 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2752 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2754 args.type = XFS_ALLOCTYPE_START_BNO;
2755 args.total = ap->total;
2758 * Search for an allocation group with a single extent
2759 * large enough for the request.
2761 * If one isn't found, then adjust the minimum allocation
2762 * size to the largest space found.
2764 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
2765 if (startag == NULLAGNUMBER)
2768 down_read(&mp->m_peraglock);
2769 while (blen < ap->alen) {
2770 pag = &mp->m_perag[ag];
2771 if (!pag->pagf_init &&
2772 (error = xfs_alloc_pagf_init(mp, args.tp,
2773 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2774 up_read(&mp->m_peraglock);
2778 * See xfs_alloc_fix_freelist...
2780 if (pag->pagf_init) {
2781 need = XFS_MIN_FREELIST_PAG(pag, mp);
2782 delta = need > pag->pagf_flcount ?
2783 need - pag->pagf_flcount : 0;
2784 longest = (pag->pagf_longest > delta) ?
2785 (pag->pagf_longest - delta) :
2786 (pag->pagf_flcount > 0 ||
2787 pag->pagf_longest > 0);
2793 if (xfs_inode_is_filestream(ap->ip)) {
2794 if (blen >= ap->alen)
2799 * If startag is an invalid AG, we've
2800 * come here once before and
2801 * xfs_filestream_new_ag picked the
2802 * best currently available.
2804 * Don't continue looping, since we
2805 * could loop forever.
2807 if (startag == NULLAGNUMBER)
2810 error = xfs_filestream_new_ag(ap, &ag);
2812 up_read(&mp->m_peraglock);
2816 /* loop again to set 'blen'*/
2817 startag = NULLAGNUMBER;
2821 if (++ag == mp->m_sb.sb_agcount)
2826 up_read(&mp->m_peraglock);
2828 * Since the above loop did a BUF_TRYLOCK, it is
2829 * possible that there is space for this request.
2831 if (notinit || blen < ap->minlen)
2832 args.minlen = ap->minlen;
2834 * If the best seen length is less than the request
2835 * length, use the best as the minimum.
2837 else if (blen < ap->alen)
2840 * Otherwise we've seen an extent as big as alen,
2841 * use that as the minimum.
2844 args.minlen = ap->alen;
2847 * set the failure fallback case to look in the selected
2848 * AG as the stream may have moved.
2850 if (xfs_inode_is_filestream(ap->ip))
2851 ap->rval = args.fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2852 } else if (ap->low) {
2853 if (xfs_inode_is_filestream(ap->ip))
2854 args.type = XFS_ALLOCTYPE_FIRST_AG;
2856 args.type = XFS_ALLOCTYPE_START_BNO;
2857 args.total = args.minlen = ap->minlen;
2859 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2860 args.total = ap->total;
2861 args.minlen = ap->minlen;
2863 /* apply extent size hints if obtained earlier */
2864 if (unlikely(align)) {
2866 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2867 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2868 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
2872 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
2873 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2874 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2877 * If we are not low on available data blocks, and the
2878 * underlying logical volume manager is a stripe, and
2879 * the file offset is zero then try to allocate data
2880 * blocks on stripe unit boundary.
2881 * NOTE: ap->aeof is only set if the allocation length
2882 * is >= the stripe unit and the allocation offset is
2883 * at the end of file.
2885 if (!ap->low && ap->aeof) {
2887 args.alignment = mp->m_dalign;
2891 * Adjust for alignment
2893 if (blen > args.alignment && blen <= ap->alen)
2894 args.minlen = blen - args.alignment;
2895 args.minalignslop = 0;
2898 * First try an exact bno allocation.
2899 * If it fails then do a near or start bno
2900 * allocation with alignment turned on.
2904 args.type = XFS_ALLOCTYPE_THIS_BNO;
2907 * Compute the minlen+alignment for the
2908 * next case. Set slop so that the value
2909 * of minlen+alignment+slop doesn't go up
2910 * between the calls.
2912 if (blen > mp->m_dalign && blen <= ap->alen)
2913 nextminlen = blen - mp->m_dalign;
2915 nextminlen = args.minlen;
2916 if (nextminlen + mp->m_dalign > args.minlen + 1)
2918 nextminlen + mp->m_dalign -
2921 args.minalignslop = 0;
2925 args.minalignslop = 0;
2927 args.minleft = ap->minleft;
2928 args.wasdel = ap->wasdel;
2930 args.userdata = ap->userdata;
2931 if ((error = xfs_alloc_vextent(&args)))
2933 if (tryagain && args.fsbno == NULLFSBLOCK) {
2935 * Exact allocation failed. Now try with alignment
2939 args.fsbno = ap->rval;
2940 args.alignment = mp->m_dalign;
2941 args.minlen = nextminlen;
2942 args.minalignslop = 0;
2944 if ((error = xfs_alloc_vextent(&args)))
2947 if (isaligned && args.fsbno == NULLFSBLOCK) {
2949 * allocation failed, so turn off alignment and
2953 args.fsbno = ap->rval;
2955 if ((error = xfs_alloc_vextent(&args)))
2958 if (args.fsbno == NULLFSBLOCK && nullfb &&
2959 args.minlen > ap->minlen) {
2960 args.minlen = ap->minlen;
2961 args.type = XFS_ALLOCTYPE_START_BNO;
2962 args.fsbno = ap->rval;
2963 if ((error = xfs_alloc_vextent(&args)))
2966 if (args.fsbno == NULLFSBLOCK && nullfb) {
2968 args.type = XFS_ALLOCTYPE_FIRST_AG;
2969 args.total = ap->minlen;
2971 if ((error = xfs_alloc_vextent(&args)))
2975 if (args.fsbno != NULLFSBLOCK) {
2976 ap->firstblock = ap->rval = args.fsbno;
2977 ASSERT(nullfb || fb_agno == args.agno ||
2978 (ap->low && fb_agno < args.agno));
2979 ap->alen = args.len;
2980 ap->ip->i_d.di_nblocks += args.len;
2981 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2983 ap->ip->i_delayed_blks -= args.len;
2985 * Adjust the disk quota also. This was reserved
2988 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2989 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2990 XFS_TRANS_DQ_BCOUNT,
2993 ap->rval = NULLFSBLOCK;
3000 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3001 * It figures out where to ask the underlying allocator to put the new extent.
3005 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
3007 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
3008 return xfs_bmap_rtalloc(ap);
3009 return xfs_bmap_btalloc(ap);
3013 * Transform a btree format file with only one leaf node, where the
3014 * extents list will fit in the inode, into an extents format file.
3015 * Since the file extents are already in-core, all we have to do is
3016 * give up the space for the btree root and pitch the leaf block.
3018 STATIC int /* error */
3019 xfs_bmap_btree_to_extents(
3020 xfs_trans_t *tp, /* transaction pointer */
3021 xfs_inode_t *ip, /* incore inode pointer */
3022 xfs_btree_cur_t *cur, /* btree cursor */
3023 int *logflagsp, /* inode logging flags */
3024 int whichfork) /* data or attr fork */
3027 xfs_bmbt_block_t *cblock;/* child btree block */
3028 xfs_fsblock_t cbno; /* child block number */
3029 xfs_buf_t *cbp; /* child block's buffer */
3030 int error; /* error return value */
3031 xfs_ifork_t *ifp; /* inode fork data */
3032 xfs_mount_t *mp; /* mount point structure */
3033 __be64 *pp; /* ptr to block address */
3034 xfs_bmbt_block_t *rblock;/* root btree block */
3036 ifp = XFS_IFORK_PTR(ip, whichfork);
3037 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3038 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
3039 rblock = ifp->if_broot;
3040 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
3041 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
3042 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
3044 pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
3045 cbno = be64_to_cpu(*pp);
3048 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
3051 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
3052 XFS_BMAP_BTREE_REF)))
3054 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
3055 if ((error = xfs_btree_check_lblock(cur, cblock, 0, cbp)))
3057 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
3058 ip->i_d.di_nblocks--;
3059 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
3060 xfs_trans_binval(tp, cbp);
3061 if (cur->bc_bufs[0] == cbp)
3062 cur->bc_bufs[0] = NULL;
3063 xfs_iroot_realloc(ip, -1, whichfork);
3064 ASSERT(ifp->if_broot == NULL);
3065 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
3066 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3067 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
3072 * Called by xfs_bmapi to update file extent records and the btree
3073 * after removing space (or undoing a delayed allocation).
3075 STATIC int /* error */
3076 xfs_bmap_del_extent(
3077 xfs_inode_t *ip, /* incore inode pointer */
3078 xfs_trans_t *tp, /* current transaction pointer */
3079 xfs_extnum_t idx, /* extent number to update/delete */
3080 xfs_bmap_free_t *flist, /* list of extents to be freed */
3081 xfs_btree_cur_t *cur, /* if null, not a btree */
3082 xfs_bmbt_irec_t *del, /* data to remove from extents */
3083 int *logflagsp, /* inode logging flags */
3084 xfs_extdelta_t *delta, /* Change made to incore extents */
3085 int whichfork, /* data or attr fork */
3086 int rsvd) /* OK to allocate reserved blocks */
3088 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
3089 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
3090 xfs_fsblock_t del_endblock=0; /* first block past del */
3091 xfs_fileoff_t del_endoff; /* first offset past del */
3092 int delay; /* current block is delayed allocated */
3093 int do_fx; /* free extent at end of routine */
3094 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
3095 int error; /* error return value */
3096 int flags; /* inode logging flags */
3097 xfs_bmbt_irec_t got; /* current extent entry */
3098 xfs_fileoff_t got_endoff; /* first offset past got */
3099 int i; /* temp state */
3100 xfs_ifork_t *ifp; /* inode fork pointer */
3101 xfs_mount_t *mp; /* mount structure */
3102 xfs_filblks_t nblks; /* quota/sb block count */
3103 xfs_bmbt_irec_t new; /* new record to be inserted */
3105 uint qfield; /* quota field to update */
3106 xfs_filblks_t temp; /* for indirect length calculations */
3107 xfs_filblks_t temp2; /* for indirect length calculations */
3109 XFS_STATS_INC(xs_del_exlist);
3111 ifp = XFS_IFORK_PTR(ip, whichfork);
3112 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
3113 (uint)sizeof(xfs_bmbt_rec_t)));
3114 ASSERT(del->br_blockcount > 0);
3115 ep = xfs_iext_get_ext(ifp, idx);
3116 xfs_bmbt_get_all(ep, &got);
3117 ASSERT(got.br_startoff <= del->br_startoff);
3118 del_endoff = del->br_startoff + del->br_blockcount;
3119 got_endoff = got.br_startoff + got.br_blockcount;
3120 ASSERT(got_endoff >= del_endoff);
3121 delay = ISNULLSTARTBLOCK(got.br_startblock);
3122 ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
3127 * If deleting a real allocation, must free up the disk space.
3130 flags = XFS_ILOG_CORE;
3132 * Realtime allocation. Free it and record di_nblocks update.
3134 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
3138 ASSERT(do_mod(del->br_blockcount,
3139 mp->m_sb.sb_rextsize) == 0);
3140 ASSERT(do_mod(del->br_startblock,
3141 mp->m_sb.sb_rextsize) == 0);
3142 bno = del->br_startblock;
3143 len = del->br_blockcount;
3144 do_div(bno, mp->m_sb.sb_rextsize);
3145 do_div(len, mp->m_sb.sb_rextsize);
3146 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
3147 (xfs_extlen_t)len)))
3150 nblks = len * mp->m_sb.sb_rextsize;
3151 qfield = XFS_TRANS_DQ_RTBCOUNT;
3154 * Ordinary allocation.
3158 nblks = del->br_blockcount;
3159 qfield = XFS_TRANS_DQ_BCOUNT;
3162 * Set up del_endblock and cur for later.
3164 del_endblock = del->br_startblock + del->br_blockcount;
3166 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
3167 got.br_startblock, got.br_blockcount,
3170 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3172 da_old = da_new = 0;
3174 da_old = STARTBLOCKVAL(got.br_startblock);
3180 * Set flag value to use in switch statement.
3181 * Left-contig is 2, right-contig is 1.
3183 switch (((got.br_startoff == del->br_startoff) << 1) |
3184 (got_endoff == del_endoff)) {
3187 * Matches the whole extent. Delete the entry.
3189 XFS_BMAP_TRACE_DELETE("3", ip, idx, 1, whichfork);
3190 xfs_iext_remove(ifp, idx, 1);
3191 ifp->if_lastex = idx;
3194 XFS_IFORK_NEXT_SET(ip, whichfork,
3195 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
3196 flags |= XFS_ILOG_CORE;
3198 flags |= XFS_ILOG_FEXT(whichfork);
3201 if ((error = xfs_bmbt_delete(cur, &i)))
3203 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3208 * Deleting the first part of the extent.
3210 XFS_BMAP_TRACE_PRE_UPDATE("2", ip, idx, whichfork);
3211 xfs_bmbt_set_startoff(ep, del_endoff);
3212 temp = got.br_blockcount - del->br_blockcount;
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("2", ip, idx,
3224 xfs_bmbt_set_startblock(ep, del_endblock);
3225 XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx, whichfork);
3227 flags |= XFS_ILOG_FEXT(whichfork);
3230 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3231 got.br_blockcount - del->br_blockcount,
3238 * Deleting the last part of the extent.
3240 temp = got.br_blockcount - del->br_blockcount;
3241 XFS_BMAP_TRACE_PRE_UPDATE("1", ip, idx, whichfork);
3242 xfs_bmbt_set_blockcount(ep, temp);
3243 ifp->if_lastex = idx;
3245 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3247 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3248 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx,
3253 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx, whichfork);
3255 flags |= XFS_ILOG_FEXT(whichfork);
3258 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3260 got.br_blockcount - del->br_blockcount,
3267 * Deleting the middle of the extent.
3269 temp = del->br_startoff - got.br_startoff;
3270 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, whichfork);
3271 xfs_bmbt_set_blockcount(ep, temp);
3272 new.br_startoff = del_endoff;
3273 temp2 = got_endoff - del_endoff;
3274 new.br_blockcount = temp2;
3275 new.br_state = got.br_state;
3277 new.br_startblock = del_endblock;
3278 flags |= XFS_ILOG_CORE;
3280 if ((error = xfs_bmbt_update(cur,
3282 got.br_startblock, temp,
3285 if ((error = xfs_btree_increment(cur, 0, &i)))
3287 cur->bc_rec.b = new;
3288 error = xfs_bmbt_insert(cur, &i);
3289 if (error && error != ENOSPC)
3292 * If get no-space back from btree insert,
3293 * it tried a split, and we have a zero
3294 * block reservation.
3295 * Fix up our state and return the error.
3297 if (error == ENOSPC) {
3299 * Reset the cursor, don't trust
3300 * it after any insert operation.
3302 if ((error = xfs_bmbt_lookup_eq(cur,
3307 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3309 * Update the btree record back
3310 * to the original value.
3312 if ((error = xfs_bmbt_update(cur,
3319 * Reset the extent record back
3320 * to the original value.
3322 xfs_bmbt_set_blockcount(ep,
3325 error = XFS_ERROR(ENOSPC);
3328 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3330 flags |= XFS_ILOG_FEXT(whichfork);
3331 XFS_IFORK_NEXT_SET(ip, whichfork,
3332 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3334 ASSERT(whichfork == XFS_DATA_FORK);
3335 temp = xfs_bmap_worst_indlen(ip, temp);
3336 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3337 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3338 new.br_startblock = NULLSTARTBLOCK((int)temp2);
3339 da_new = temp + temp2;
3340 while (da_new > da_old) {
3344 xfs_bmbt_set_startblock(ep,
3345 NULLSTARTBLOCK((int)temp));
3347 if (da_new == da_old)
3353 NULLSTARTBLOCK((int)temp2);
3357 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, whichfork);
3358 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 1, &new, NULL,
3360 xfs_iext_insert(ifp, idx + 1, 1, &new);
3361 ifp->if_lastex = idx + 1;
3365 * If we need to, add to list of extents to delete.
3368 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3371 * Adjust inode # blocks in the file.
3374 ip->i_d.di_nblocks -= nblks;
3376 * Adjust quota data.
3379 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks);
3382 * Account for change in delayed indirect blocks.
3383 * Nothing to do for disk quota accounting here.
3385 ASSERT(da_old >= da_new);
3386 if (da_old > da_new)
3387 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
3390 /* DELTA: report the original extent. */
3391 if (delta->xed_startoff > got.br_startoff)
3392 delta->xed_startoff = got.br_startoff;
3393 if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
3394 delta->xed_blockcount = got.br_startoff +
3403 * Remove the entry "free" from the free item list. Prev points to the
3404 * previous entry, unless "free" is the head of the list.
3408 xfs_bmap_free_t *flist, /* free item list header */
3409 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3410 xfs_bmap_free_item_t *free) /* list item to be freed */
3413 prev->xbfi_next = free->xbfi_next;
3415 flist->xbf_first = free->xbfi_next;
3417 kmem_zone_free(xfs_bmap_free_item_zone, free);
3421 * Convert an extents-format file into a btree-format file.
3422 * The new file will have a root block (in the inode) and a single child block.
3424 STATIC int /* error */
3425 xfs_bmap_extents_to_btree(
3426 xfs_trans_t *tp, /* transaction pointer */
3427 xfs_inode_t *ip, /* incore inode pointer */
3428 xfs_fsblock_t *firstblock, /* first-block-allocated */
3429 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3430 xfs_btree_cur_t **curp, /* cursor returned to caller */
3431 int wasdel, /* converting a delayed alloc */
3432 int *logflagsp, /* inode logging flags */
3433 int whichfork) /* data or attr fork */
3435 xfs_bmbt_block_t *ablock; /* allocated (child) bt block */
3436 xfs_buf_t *abp; /* buffer for ablock */
3437 xfs_alloc_arg_t args; /* allocation arguments */
3438 xfs_bmbt_rec_t *arp; /* child record pointer */
3439 xfs_bmbt_block_t *block; /* btree root block */
3440 xfs_btree_cur_t *cur; /* bmap btree cursor */
3441 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3442 int error; /* error return value */
3443 xfs_extnum_t i, cnt; /* extent record index */
3444 xfs_ifork_t *ifp; /* inode fork pointer */
3445 xfs_bmbt_key_t *kp; /* root block key pointer */
3446 xfs_mount_t *mp; /* mount structure */
3447 xfs_extnum_t nextents; /* number of file extents */
3448 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3450 ifp = XFS_IFORK_PTR(ip, whichfork);
3451 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3452 ASSERT(ifp->if_ext_max ==
3453 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3455 * Make space in the inode incore.
3457 xfs_iroot_realloc(ip, 1, whichfork);
3458 ifp->if_flags |= XFS_IFBROOT;
3462 block = ifp->if_broot;
3463 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3464 block->bb_level = cpu_to_be16(1);
3465 block->bb_numrecs = cpu_to_be16(1);
3466 block->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3467 block->bb_rightsib = cpu_to_be64(NULLDFSBNO);
3469 * Need a cursor. Can't allocate until bb_level is filled in.
3472 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
3473 cur->bc_private.b.firstblock = *firstblock;
3474 cur->bc_private.b.flist = flist;
3475 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3477 * Convert to a btree with two levels, one record in root.
3479 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3482 args.firstblock = *firstblock;
3483 if (*firstblock == NULLFSBLOCK) {
3484 args.type = XFS_ALLOCTYPE_START_BNO;
3485 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3486 } else if (flist->xbf_low) {
3487 args.type = XFS_ALLOCTYPE_START_BNO;
3488 args.fsbno = *firstblock;
3490 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3491 args.fsbno = *firstblock;
3493 args.minlen = args.maxlen = args.prod = 1;
3494 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3495 args.minalignslop = 0;
3496 args.wasdel = wasdel;
3498 if ((error = xfs_alloc_vextent(&args))) {
3499 xfs_iroot_realloc(ip, -1, whichfork);
3500 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3504 * Allocation can't fail, the space was reserved.
3506 ASSERT(args.fsbno != NULLFSBLOCK);
3507 ASSERT(*firstblock == NULLFSBLOCK ||
3508 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3510 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3511 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3512 cur->bc_private.b.allocated++;
3513 ip->i_d.di_nblocks++;
3514 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3515 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3517 * Fill in the child block.
3519 ablock = XFS_BUF_TO_BMBT_BLOCK(abp);
3520 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3521 ablock->bb_level = 0;
3522 ablock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3523 ablock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
3524 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3525 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3526 for (cnt = i = 0; i < nextents; i++) {
3527 ep = xfs_iext_get_ext(ifp, i);
3528 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
3529 arp->l0 = cpu_to_be64(ep->l0);
3530 arp->l1 = cpu_to_be64(ep->l1);
3534 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3535 ablock->bb_numrecs = cpu_to_be16(cnt);
3537 * Fill in the root key and pointer.
3539 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
3540 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3541 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
3542 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
3543 *pp = cpu_to_be64(args.fsbno);
3545 * Do all this logging at the end so that
3546 * the root is at the right level.
3548 xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS);
3549 xfs_bmbt_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3550 ASSERT(*curp == NULL);
3552 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
3557 * Helper routine to reset inode di_forkoff field when switching
3558 * attribute fork from local to extent format - we reset it where
3559 * possible to make space available for inline data fork extents.
3562 xfs_bmap_forkoff_reset(
3567 if (whichfork == XFS_ATTR_FORK &&
3568 (ip->i_d.di_format != XFS_DINODE_FMT_DEV) &&
3569 (ip->i_d.di_format != XFS_DINODE_FMT_UUID) &&
3570 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
3571 ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) {
3572 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
3573 ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) /
3574 (uint)sizeof(xfs_bmbt_rec_t);
3575 ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) /
3576 (uint)sizeof(xfs_bmbt_rec_t);
3581 * Convert a local file to an extents file.
3582 * This code is out of bounds for data forks of regular files,
3583 * since the file data needs to get logged so things will stay consistent.
3584 * (The bmap-level manipulations are ok, though).
3586 STATIC int /* error */
3587 xfs_bmap_local_to_extents(
3588 xfs_trans_t *tp, /* transaction pointer */
3589 xfs_inode_t *ip, /* incore inode pointer */
3590 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3591 xfs_extlen_t total, /* total blocks needed by transaction */
3592 int *logflagsp, /* inode logging flags */
3593 int whichfork) /* data or attr fork */
3595 int error; /* error return value */
3596 int flags; /* logging flags returned */
3597 xfs_ifork_t *ifp; /* inode fork pointer */
3600 * We don't want to deal with the case of keeping inode data inline yet.
3601 * So sending the data fork of a regular inode is invalid.
3603 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3604 whichfork == XFS_DATA_FORK));
3605 ifp = XFS_IFORK_PTR(ip, whichfork);
3606 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3609 if (ifp->if_bytes) {
3610 xfs_alloc_arg_t args; /* allocation arguments */
3611 xfs_buf_t *bp; /* buffer for extent block */
3612 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
3615 args.mp = ip->i_mount;
3616 args.firstblock = *firstblock;
3617 ASSERT((ifp->if_flags &
3618 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
3620 * Allocate a block. We know we need only one, since the
3621 * file currently fits in an inode.
3623 if (*firstblock == NULLFSBLOCK) {
3624 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3625 args.type = XFS_ALLOCTYPE_START_BNO;
3627 args.fsbno = *firstblock;
3628 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3631 args.mod = args.minleft = args.alignment = args.wasdel =
3632 args.isfl = args.minalignslop = 0;
3633 args.minlen = args.maxlen = args.prod = 1;
3634 if ((error = xfs_alloc_vextent(&args)))
3637 * Can't fail, the space was reserved.
3639 ASSERT(args.fsbno != NULLFSBLOCK);
3640 ASSERT(args.len == 1);
3641 *firstblock = args.fsbno;
3642 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3643 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3645 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3646 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3647 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3648 xfs_iext_add(ifp, 0, 1);
3649 ep = xfs_iext_get_ext(ifp, 0);
3650 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3651 XFS_BMAP_TRACE_POST_UPDATE("new", ip, 0, whichfork);
3652 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3653 ip->i_d.di_nblocks = 1;
3654 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
3655 XFS_TRANS_DQ_BCOUNT, 1L);
3656 flags |= XFS_ILOG_FEXT(whichfork);
3658 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3659 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3661 ifp->if_flags &= ~XFS_IFINLINE;
3662 ifp->if_flags |= XFS_IFEXTENTS;
3663 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3664 flags |= XFS_ILOG_CORE;
3671 * Search the extent records for the entry containing block bno.
3672 * If bno lies in a hole, point to the next entry. If bno lies
3673 * past eof, *eofp will be set, and *prevp will contain the last
3674 * entry (null if none). Else, *lastxp will be set to the index
3675 * of the found entry; *gotp will contain the entry.
3677 xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3678 xfs_bmap_search_multi_extents(
3679 xfs_ifork_t *ifp, /* inode fork pointer */
3680 xfs_fileoff_t bno, /* block number searched for */
3681 int *eofp, /* out: end of file found */
3682 xfs_extnum_t *lastxp, /* out: last extent index */
3683 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3684 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3686 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3687 xfs_extnum_t lastx; /* last extent index */
3690 * Initialize the extent entry structure to catch access to
3691 * uninitialized br_startblock field.
3693 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3694 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3695 gotp->br_state = XFS_EXT_INVALID;
3697 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3699 gotp->br_startblock = 0xffffa5a5;
3701 prevp->br_startoff = NULLFILEOFF;
3703 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3705 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3707 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3708 xfs_bmbt_get_all(ep, gotp);
3722 * Search the extents list for the inode, for the extent containing bno.
3723 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3724 * *eofp will be set, and *prevp will contain the last entry (null if none).
3725 * Else, *lastxp will be set to the index of the found
3726 * entry; *gotp will contain the entry.
3728 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3729 xfs_bmap_search_extents(
3730 xfs_inode_t *ip, /* incore inode pointer */
3731 xfs_fileoff_t bno, /* block number searched for */
3732 int fork, /* data or attr fork */
3733 int *eofp, /* out: end of file found */
3734 xfs_extnum_t *lastxp, /* out: last extent index */
3735 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3736 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3738 xfs_ifork_t *ifp; /* inode fork pointer */
3739 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3741 XFS_STATS_INC(xs_look_exlist);
3742 ifp = XFS_IFORK_PTR(ip, fork);
3744 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3746 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3747 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3748 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3749 "Access to block zero in inode %llu "
3750 "start_block: %llx start_off: %llx "
3751 "blkcnt: %llx extent-state: %x lastx: %x\n",
3752 (unsigned long long)ip->i_ino,
3753 (unsigned long long)gotp->br_startblock,
3754 (unsigned long long)gotp->br_startoff,
3755 (unsigned long long)gotp->br_blockcount,
3756 gotp->br_state, *lastxp);
3757 *lastxp = NULLEXTNUM;
3765 #ifdef XFS_BMAP_TRACE
3766 ktrace_t *xfs_bmap_trace_buf;
3769 * Add a bmap trace buffer entry. Base routine for the others.
3772 xfs_bmap_trace_addentry(
3773 int opcode, /* operation */
3774 const char *fname, /* function name */
3775 char *desc, /* operation description */
3776 xfs_inode_t *ip, /* incore inode pointer */
3777 xfs_extnum_t idx, /* index of entry(ies) */
3778 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
3779 xfs_bmbt_rec_host_t *r1, /* first record */
3780 xfs_bmbt_rec_host_t *r2, /* second record or null */
3781 int whichfork) /* data or attr fork */
3783 xfs_bmbt_rec_host_t tr2;
3785 ASSERT(cnt == 1 || cnt == 2);
3790 memset(&tr2, 0, sizeof(tr2));
3793 ktrace_enter(xfs_bmap_trace_buf,
3794 (void *)(__psint_t)(opcode | (whichfork << 16)),
3795 (void *)fname, (void *)desc, (void *)ip,
3796 (void *)(__psint_t)idx,
3797 (void *)(__psint_t)cnt,
3798 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3799 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3800 (void *)(__psunsigned_t)(r1->l0 >> 32),
3801 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3802 (void *)(__psunsigned_t)(r1->l1 >> 32),
3803 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3804 (void *)(__psunsigned_t)(r2->l0 >> 32),
3805 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3806 (void *)(__psunsigned_t)(r2->l1 >> 32),
3807 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3809 ASSERT(ip->i_xtrace);
3810 ktrace_enter(ip->i_xtrace,
3811 (void *)(__psint_t)(opcode | (whichfork << 16)),
3812 (void *)fname, (void *)desc, (void *)ip,
3813 (void *)(__psint_t)idx,
3814 (void *)(__psint_t)cnt,
3815 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3816 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3817 (void *)(__psunsigned_t)(r1->l0 >> 32),
3818 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3819 (void *)(__psunsigned_t)(r1->l1 >> 32),
3820 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3821 (void *)(__psunsigned_t)(r2->l0 >> 32),
3822 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3823 (void *)(__psunsigned_t)(r2->l1 >> 32),
3824 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3829 * Add bmap trace entry prior to a call to xfs_iext_remove.
3832 xfs_bmap_trace_delete(
3833 const char *fname, /* function name */
3834 char *desc, /* operation description */
3835 xfs_inode_t *ip, /* incore inode pointer */
3836 xfs_extnum_t idx, /* index of entry(entries) deleted */
3837 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
3838 int whichfork) /* data or attr fork */
3840 xfs_ifork_t *ifp; /* inode fork pointer */
3842 ifp = XFS_IFORK_PTR(ip, whichfork);
3843 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
3844 cnt, xfs_iext_get_ext(ifp, idx),
3845 cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL,
3850 * Add bmap trace entry prior to a call to xfs_iext_insert, or
3851 * reading in the extents list from the disk (in the btree).
3854 xfs_bmap_trace_insert(
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(entries) inserted */
3859 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
3860 xfs_bmbt_irec_t *r1, /* inserted record 1 */
3861 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
3862 int whichfork) /* data or attr fork */
3864 xfs_bmbt_rec_host_t tr1; /* compressed record 1 */
3865 xfs_bmbt_rec_host_t tr2; /* compressed record 2 if needed */
3867 xfs_bmbt_set_all(&tr1, r1);
3870 xfs_bmbt_set_all(&tr2, r2);
3875 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
3876 cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
3880 * Add bmap trace entry after updating an extent record in place.
3883 xfs_bmap_trace_post_update(
3884 const char *fname, /* function name */
3885 char *desc, /* operation description */
3886 xfs_inode_t *ip, /* incore inode pointer */
3887 xfs_extnum_t idx, /* index of entry updated */
3888 int whichfork) /* data or attr fork */
3890 xfs_ifork_t *ifp; /* inode fork pointer */
3892 ifp = XFS_IFORK_PTR(ip, whichfork);
3893 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
3894 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3898 * Add bmap trace entry prior to updating an extent record in place.
3901 xfs_bmap_trace_pre_update(
3902 const char *fname, /* function name */
3903 char *desc, /* operation description */
3904 xfs_inode_t *ip, /* incore inode pointer */
3905 xfs_extnum_t idx, /* index of entry to be updated */
3906 int whichfork) /* data or attr fork */
3908 xfs_ifork_t *ifp; /* inode fork pointer */
3910 ifp = XFS_IFORK_PTR(ip, whichfork);
3911 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
3912 xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3914 #endif /* XFS_BMAP_TRACE */
3917 * Compute the worst-case number of indirect blocks that will be used
3918 * for ip's delayed extent of length "len".
3920 STATIC xfs_filblks_t
3921 xfs_bmap_worst_indlen(
3922 xfs_inode_t *ip, /* incore inode pointer */
3923 xfs_filblks_t len) /* delayed extent length */
3925 int level; /* btree level number */
3926 int maxrecs; /* maximum record count at this level */
3927 xfs_mount_t *mp; /* mount structure */
3928 xfs_filblks_t rval; /* return value */
3931 maxrecs = mp->m_bmap_dmxr[0];
3932 for (level = 0, rval = 0;
3933 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3936 do_div(len, maxrecs);
3939 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3942 maxrecs = mp->m_bmap_dmxr[1];
3947 #if defined(XFS_RW_TRACE)
3956 if (ip->i_rwtrace == NULL)
3958 ktrace_enter(ip->i_rwtrace,
3959 (void *)(__psint_t)XFS_BUNMAP,
3961 (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
3962 (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
3963 (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
3964 (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
3965 (void *)(__psint_t)len,
3966 (void *)(__psint_t)flags,
3967 (void *)(unsigned long)current_cpu(),
3979 * Convert inode from non-attributed to attributed.
3980 * Must not be in a transaction, ip must not be locked.
3982 int /* error code */
3983 xfs_bmap_add_attrfork(
3984 xfs_inode_t *ip, /* incore inode pointer */
3985 int size, /* space new attribute needs */
3986 int rsvd) /* xact may use reserved blks */
3988 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
3989 xfs_bmap_free_t flist; /* freed extent records */
3990 xfs_mount_t *mp; /* mount structure */
3991 xfs_trans_t *tp; /* transaction pointer */
3992 int blks; /* space reservation */
3993 int version = 1; /* superblock attr version */
3994 int committed; /* xaction was committed */
3995 int logflags; /* logging flags */
3996 int error; /* error return value */
3998 ASSERT(XFS_IFORK_Q(ip) == 0);
3999 ASSERT(ip->i_df.if_ext_max ==
4000 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4003 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
4004 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
4005 blks = XFS_ADDAFORK_SPACE_RES(mp);
4007 tp->t_flags |= XFS_TRANS_RESERVE;
4008 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
4009 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
4011 xfs_ilock(ip, XFS_ILOCK_EXCL);
4012 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ?
4013 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
4014 XFS_QMOPT_RES_REGBLKS);
4016 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4017 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
4020 if (XFS_IFORK_Q(ip))
4022 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
4024 * For inodes coming from pre-6.2 filesystems.
4026 ASSERT(ip->i_d.di_aformat == 0);
4027 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
4029 ASSERT(ip->i_d.di_anextents == 0);
4031 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4032 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4033 switch (ip->i_d.di_format) {
4034 case XFS_DINODE_FMT_DEV:
4035 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
4037 case XFS_DINODE_FMT_UUID:
4038 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
4040 case XFS_DINODE_FMT_LOCAL:
4041 case XFS_DINODE_FMT_EXTENTS:
4042 case XFS_DINODE_FMT_BTREE:
4043 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
4044 if (!ip->i_d.di_forkoff)
4045 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
4046 else if (mp->m_flags & XFS_MOUNT_ATTR2)
4051 error = XFS_ERROR(EINVAL);
4054 ip->i_df.if_ext_max =
4055 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4056 ASSERT(ip->i_afp == NULL);
4057 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
4058 ip->i_afp->if_ext_max =
4059 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4060 ip->i_afp->if_flags = XFS_IFEXTENTS;
4062 XFS_BMAP_INIT(&flist, &firstblock);
4063 switch (ip->i_d.di_format) {
4064 case XFS_DINODE_FMT_LOCAL:
4065 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
4068 case XFS_DINODE_FMT_EXTENTS:
4069 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
4072 case XFS_DINODE_FMT_BTREE:
4073 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
4081 xfs_trans_log_inode(tp, ip, logflags);
4084 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
4085 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
4086 __int64_t sbfields = 0;
4088 spin_lock(&mp->m_sb_lock);
4089 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
4090 xfs_sb_version_addattr(&mp->m_sb);
4091 sbfields |= XFS_SB_VERSIONNUM;
4093 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
4094 xfs_sb_version_addattr2(&mp->m_sb);
4095 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
4098 spin_unlock(&mp->m_sb_lock);
4099 xfs_mod_sb(tp, sbfields);
4101 spin_unlock(&mp->m_sb_lock);
4103 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
4105 error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES);
4106 ASSERT(ip->i_df.if_ext_max ==
4107 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4110 xfs_bmap_cancel(&flist);
4112 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4114 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
4115 ASSERT(ip->i_df.if_ext_max ==
4116 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4121 * Add the extent to the list of extents to be free at transaction end.
4122 * The list is maintained sorted (by block number).
4127 xfs_fsblock_t bno, /* fs block number of extent */
4128 xfs_filblks_t len, /* length of extent */
4129 xfs_bmap_free_t *flist, /* list of extents */
4130 xfs_mount_t *mp) /* mount point structure */
4132 xfs_bmap_free_item_t *cur; /* current (next) element */
4133 xfs_bmap_free_item_t *new; /* new element */
4134 xfs_bmap_free_item_t *prev; /* previous element */
4136 xfs_agnumber_t agno;
4137 xfs_agblock_t agbno;
4139 ASSERT(bno != NULLFSBLOCK);
4141 ASSERT(len <= MAXEXTLEN);
4142 ASSERT(!ISNULLSTARTBLOCK(bno));
4143 agno = XFS_FSB_TO_AGNO(mp, bno);
4144 agbno = XFS_FSB_TO_AGBNO(mp, bno);
4145 ASSERT(agno < mp->m_sb.sb_agcount);
4146 ASSERT(agbno < mp->m_sb.sb_agblocks);
4147 ASSERT(len < mp->m_sb.sb_agblocks);
4148 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
4150 ASSERT(xfs_bmap_free_item_zone != NULL);
4151 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
4152 new->xbfi_startblock = bno;
4153 new->xbfi_blockcount = (xfs_extlen_t)len;
4154 for (prev = NULL, cur = flist->xbf_first;
4156 prev = cur, cur = cur->xbfi_next) {
4157 if (cur->xbfi_startblock >= bno)
4161 prev->xbfi_next = new;
4163 flist->xbf_first = new;
4164 new->xbfi_next = cur;
4169 * Compute and fill in the value of the maximum depth of a bmap btree
4170 * in this filesystem. Done once, during mount.
4173 xfs_bmap_compute_maxlevels(
4174 xfs_mount_t *mp, /* file system mount structure */
4175 int whichfork) /* data or attr fork */
4177 int level; /* btree level */
4178 uint maxblocks; /* max blocks at this level */
4179 uint maxleafents; /* max leaf entries possible */
4180 int maxrootrecs; /* max records in root block */
4181 int minleafrecs; /* min records in leaf block */
4182 int minnoderecs; /* min records in node block */
4183 int sz; /* root block size */
4186 * The maximum number of extents in a file, hence the maximum
4187 * number of leaf entries, is controlled by the type of di_nextents
4188 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
4189 * (a signed 16-bit number, xfs_aextnum_t).
4191 * Note that we can no longer assume that if we are in ATTR1 that
4192 * the fork offset of all the inodes will be (m_attroffset >> 3)
4193 * because we could have mounted with ATTR2 and then mounted back
4194 * with ATTR1, keeping the di_forkoff's fixed but probably at
4195 * various positions. Therefore, for both ATTR1 and ATTR2
4196 * we have to assume the worst case scenario of a minimum size
4199 if (whichfork == XFS_DATA_FORK) {
4200 maxleafents = MAXEXTNUM;
4201 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
4203 maxleafents = MAXAEXTNUM;
4204 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
4206 maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
4207 minleafrecs = mp->m_bmap_dmnr[0];
4208 minnoderecs = mp->m_bmap_dmnr[1];
4209 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
4210 for (level = 1; maxblocks > 1; level++) {
4211 if (maxblocks <= maxrootrecs)
4214 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
4216 mp->m_bm_maxlevels[whichfork] = level;
4220 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
4221 * caller. Frees all the extents that need freeing, which must be done
4222 * last due to locking considerations. We never free any extents in
4223 * the first transaction. This is to allow the caller to make the first
4224 * transaction a synchronous one so that the pointers to the data being
4225 * broken in this transaction will be permanent before the data is actually
4226 * freed. This is necessary to prevent blocks from being reallocated
4227 * and written to before the free and reallocation are actually permanent.
4228 * We do not just make the first transaction synchronous here, because
4229 * there are more efficient ways to gain the same protection in some cases
4230 * (see the file truncation code).
4232 * Return 1 if the given transaction was committed and a new one
4233 * started, and 0 otherwise in the committed parameter.
4238 xfs_trans_t **tp, /* transaction pointer addr */
4239 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4240 int *committed) /* xact committed or not */
4242 xfs_efd_log_item_t *efd; /* extent free data */
4243 xfs_efi_log_item_t *efi; /* extent free intention */
4244 int error; /* error return value */
4245 xfs_bmap_free_item_t *free; /* free extent item */
4246 unsigned int logres; /* new log reservation */
4247 unsigned int logcount; /* new log count */
4248 xfs_mount_t *mp; /* filesystem mount structure */
4249 xfs_bmap_free_item_t *next; /* next item on free list */
4250 xfs_trans_t *ntp; /* new transaction pointer */
4252 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
4253 if (flist->xbf_count == 0) {
4258 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
4259 for (free = flist->xbf_first; free; free = free->xbfi_next)
4260 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
4261 free->xbfi_blockcount);
4262 logres = ntp->t_log_res;
4263 logcount = ntp->t_log_count;
4264 ntp = xfs_trans_dup(*tp);
4265 error = xfs_trans_commit(*tp, 0);
4269 * We have a new transaction, so we should return committed=1,
4270 * even though we're returning an error.
4275 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
4278 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
4279 for (free = flist->xbf_first; free != NULL; free = next) {
4280 next = free->xbfi_next;
4281 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4282 free->xbfi_blockcount))) {
4284 * The bmap free list will be cleaned up at a
4285 * higher level. The EFI will be canceled when
4286 * this transaction is aborted.
4287 * Need to force shutdown here to make sure it
4288 * happens, since this transaction may not be
4292 if (!XFS_FORCED_SHUTDOWN(mp))
4293 xfs_force_shutdown(mp,
4294 (error == EFSCORRUPTED) ?
4295 SHUTDOWN_CORRUPT_INCORE :
4296 SHUTDOWN_META_IO_ERROR);
4299 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4300 free->xbfi_blockcount);
4301 xfs_bmap_del_free(flist, NULL, free);
4307 * Free up any items left in the list.
4311 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4313 xfs_bmap_free_item_t *free; /* free list item */
4314 xfs_bmap_free_item_t *next;
4316 if (flist->xbf_count == 0)
4318 ASSERT(flist->xbf_first != NULL);
4319 for (free = flist->xbf_first; free; free = next) {
4320 next = free->xbfi_next;
4321 xfs_bmap_del_free(flist, NULL, free);
4323 ASSERT(flist->xbf_count == 0);
4327 * Returns the file-relative block number of the first unused block(s)
4328 * in the file with at least "len" logically contiguous blocks free.
4329 * This is the lowest-address hole if the file has holes, else the first block
4330 * past the end of file.
4331 * Return 0 if the file is currently local (in-inode).
4334 xfs_bmap_first_unused(
4335 xfs_trans_t *tp, /* transaction pointer */
4336 xfs_inode_t *ip, /* incore inode */
4337 xfs_extlen_t len, /* size of hole to find */
4338 xfs_fileoff_t *first_unused, /* unused block */
4339 int whichfork) /* data or attr fork */
4341 int error; /* error return value */
4342 int idx; /* extent record index */
4343 xfs_ifork_t *ifp; /* inode fork pointer */
4344 xfs_fileoff_t lastaddr; /* last block number seen */
4345 xfs_fileoff_t lowest; /* lowest useful block */
4346 xfs_fileoff_t max; /* starting useful block */
4347 xfs_fileoff_t off; /* offset for this block */
4348 xfs_extnum_t nextents; /* number of extent entries */
4350 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4351 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4352 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4353 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4357 ifp = XFS_IFORK_PTR(ip, whichfork);
4358 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4359 (error = xfs_iread_extents(tp, ip, whichfork)))
4361 lowest = *first_unused;
4362 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4363 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
4364 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
4365 off = xfs_bmbt_get_startoff(ep);
4367 * See if the hole before this extent will work.
4369 if (off >= lowest + len && off - max >= len) {
4370 *first_unused = max;
4373 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4374 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4376 *first_unused = max;
4381 * Returns the file-relative block number of the last block + 1 before
4382 * last_block (input value) in the file.
4383 * This is not based on i_size, it is based on the extent records.
4384 * Returns 0 for local files, as they do not have extent records.
4387 xfs_bmap_last_before(
4388 xfs_trans_t *tp, /* transaction pointer */
4389 xfs_inode_t *ip, /* incore inode */
4390 xfs_fileoff_t *last_block, /* last block */
4391 int whichfork) /* data or attr fork */
4393 xfs_fileoff_t bno; /* input file offset */
4394 int eof; /* hit end of file */
4395 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
4396 int error; /* error return value */
4397 xfs_bmbt_irec_t got; /* current extent value */
4398 xfs_ifork_t *ifp; /* inode fork pointer */
4399 xfs_extnum_t lastx; /* last extent used */
4400 xfs_bmbt_irec_t prev; /* previous extent value */
4402 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4403 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4404 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4405 return XFS_ERROR(EIO);
4406 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4410 ifp = XFS_IFORK_PTR(ip, whichfork);
4411 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4412 (error = xfs_iread_extents(tp, ip, whichfork)))
4414 bno = *last_block - 1;
4415 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4417 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4418 if (prev.br_startoff == NULLFILEOFF)
4421 *last_block = prev.br_startoff + prev.br_blockcount;
4424 * Otherwise *last_block is already the right answer.
4430 * Returns the file-relative block number of the first block past eof in
4431 * the file. This is not based on i_size, it is based on the extent records.
4432 * Returns 0 for local files, as they do not have extent records.
4435 xfs_bmap_last_offset(
4436 xfs_trans_t *tp, /* transaction pointer */
4437 xfs_inode_t *ip, /* incore inode */
4438 xfs_fileoff_t *last_block, /* last block */
4439 int whichfork) /* data or attr fork */
4441 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
4442 int error; /* error return value */
4443 xfs_ifork_t *ifp; /* inode fork pointer */
4444 xfs_extnum_t nextents; /* number of extent entries */
4446 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4447 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4448 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4449 return XFS_ERROR(EIO);
4450 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4454 ifp = XFS_IFORK_PTR(ip, whichfork);
4455 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4456 (error = xfs_iread_extents(tp, ip, whichfork)))
4458 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4463 ep = xfs_iext_get_ext(ifp, nextents - 1);
4464 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4469 * Returns whether the selected fork of the inode has exactly one
4470 * block or not. For the data fork we check this matches di_size,
4471 * implying the file's range is 0..bsize-1.
4473 int /* 1=>1 block, 0=>otherwise */
4475 xfs_inode_t *ip, /* incore inode */
4476 int whichfork) /* data or attr fork */
4478 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
4479 xfs_ifork_t *ifp; /* inode fork pointer */
4480 int rval; /* return value */
4481 xfs_bmbt_irec_t s; /* internal version of extent */
4484 if (whichfork == XFS_DATA_FORK) {
4485 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4486 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4487 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4490 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4492 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4494 ifp = XFS_IFORK_PTR(ip, whichfork);
4495 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4496 ep = xfs_iext_get_ext(ifp, 0);
4497 xfs_bmbt_get_all(ep, &s);
4498 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4499 if (rval && whichfork == XFS_DATA_FORK)
4500 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
4505 * Read in the extents to if_extents.
4506 * All inode fields are set up by caller, we just traverse the btree
4507 * and copy the records in. If the file system cannot contain unwritten
4508 * extents, the records are checked for no "state" flags.
4511 xfs_bmap_read_extents(
4512 xfs_trans_t *tp, /* transaction pointer */
4513 xfs_inode_t *ip, /* incore inode */
4514 int whichfork) /* data or attr fork */
4516 xfs_bmbt_block_t *block; /* current btree block */
4517 xfs_fsblock_t bno; /* block # of "block" */
4518 xfs_buf_t *bp; /* buffer for "block" */
4519 int error; /* error return value */
4520 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4521 xfs_extnum_t i, j; /* index into the extents list */
4522 xfs_ifork_t *ifp; /* fork structure */
4523 int level; /* btree level, for checking */
4524 xfs_mount_t *mp; /* file system mount structure */
4525 __be64 *pp; /* pointer to block address */
4527 xfs_extnum_t room; /* number of entries there's room for */
4531 ifp = XFS_IFORK_PTR(ip, whichfork);
4532 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4533 XFS_EXTFMT_INODE(ip);
4534 block = ifp->if_broot;
4536 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4538 level = be16_to_cpu(block->bb_level);
4540 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
4541 bno = be64_to_cpu(*pp);
4542 ASSERT(bno != NULLDFSBNO);
4543 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4544 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
4546 * Go down the tree until leaf level is reached, following the first
4547 * pointer (leftmost) at each level.
4549 while (level-- > 0) {
4550 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4551 XFS_BMAP_BTREE_REF)))
4553 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4554 XFS_WANT_CORRUPTED_GOTO(
4555 XFS_BMAP_SANITY_CHECK(mp, block, level),
4559 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
4560 bno = be64_to_cpu(*pp);
4561 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
4562 xfs_trans_brelse(tp, bp);
4565 * Here with bp and block set to the leftmost leaf node in the tree.
4567 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4570 * Loop over all leaf nodes. Copy information to the extent records.
4573 xfs_bmbt_rec_t *frp;
4574 xfs_fsblock_t nextbno;
4575 xfs_extnum_t num_recs;
4579 num_recs = be16_to_cpu(block->bb_numrecs);
4580 if (unlikely(i + num_recs > room)) {
4581 ASSERT(i + num_recs <= room);
4582 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4583 "corrupt dinode %Lu, (btree extents).",
4584 (unsigned long long) ip->i_ino);
4585 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4590 XFS_WANT_CORRUPTED_GOTO(
4591 XFS_BMAP_SANITY_CHECK(mp, block, 0),
4594 * Read-ahead the next leaf block, if any.
4596 nextbno = be64_to_cpu(block->bb_rightsib);
4597 if (nextbno != NULLFSBLOCK)
4598 xfs_btree_reada_bufl(mp, nextbno, 1);
4600 * Copy records into the extent records.
4602 frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
4604 for (j = 0; j < num_recs; j++, i++, frp++) {
4605 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
4606 trp->l0 = be64_to_cpu(frp->l0);
4607 trp->l1 = be64_to_cpu(frp->l1);
4609 if (exntf == XFS_EXTFMT_NOSTATE) {
4611 * Check all attribute bmap btree records and
4612 * any "older" data bmap btree records for a
4613 * set bit in the "extent flag" position.
4615 if (unlikely(xfs_check_nostate_extents(ifp,
4616 start, num_recs))) {
4617 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4623 xfs_trans_brelse(tp, bp);
4626 * If we've reached the end, stop.
4628 if (bno == NULLFSBLOCK)
4630 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4631 XFS_BMAP_BTREE_REF)))
4633 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4635 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4636 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4637 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
4640 xfs_trans_brelse(tp, bp);
4641 return XFS_ERROR(EFSCORRUPTED);
4644 #ifdef XFS_BMAP_TRACE
4646 * Add bmap trace insert entries for all the contents of the extent records.
4649 xfs_bmap_trace_exlist(
4650 const char *fname, /* function name */
4651 xfs_inode_t *ip, /* incore inode pointer */
4652 xfs_extnum_t cnt, /* count of entries in the list */
4653 int whichfork) /* data or attr fork */
4655 xfs_bmbt_rec_host_t *ep; /* current extent record */
4656 xfs_extnum_t idx; /* extent record index */
4657 xfs_ifork_t *ifp; /* inode fork pointer */
4658 xfs_bmbt_irec_t s; /* file extent record */
4660 ifp = XFS_IFORK_PTR(ip, whichfork);
4661 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4662 for (idx = 0; idx < cnt; idx++) {
4663 ep = xfs_iext_get_ext(ifp, idx);
4664 xfs_bmbt_get_all(ep, &s);
4665 XFS_BMAP_TRACE_INSERT("exlist", ip, idx, 1, &s, NULL,
4673 * Validate that the bmbt_irecs being returned from bmapi are valid
4674 * given the callers original parameters. Specifically check the
4675 * ranges of the returned irecs to ensure that they only extent beyond
4676 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4679 xfs_bmap_validate_ret(
4683 xfs_bmbt_irec_t *mval,
4687 int i; /* index to map values */
4689 ASSERT(ret_nmap <= nmap);
4691 for (i = 0; i < ret_nmap; i++) {
4692 ASSERT(mval[i].br_blockcount > 0);
4693 if (!(flags & XFS_BMAPI_ENTIRE)) {
4694 ASSERT(mval[i].br_startoff >= bno);
4695 ASSERT(mval[i].br_blockcount <= len);
4696 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4699 ASSERT(mval[i].br_startoff < bno + len);
4700 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4704 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4705 mval[i].br_startoff);
4706 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4707 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4708 mval[i].br_startblock != HOLESTARTBLOCK);
4709 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4710 mval[i].br_state == XFS_EXT_UNWRITTEN);
4717 * Map file blocks to filesystem blocks.
4718 * File range is given by the bno/len pair.
4719 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4720 * into a hole or past eof.
4721 * Only allocates blocks from a single allocation group,
4722 * to avoid locking problems.
4723 * The returned value in "firstblock" from the first call in a transaction
4724 * must be remembered and presented to subsequent calls in "firstblock".
4725 * An upper bound for the number of blocks to be allocated is supplied to
4726 * the first call in "total"; if no allocation group has that many free
4727 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4731 xfs_trans_t *tp, /* transaction pointer */
4732 xfs_inode_t *ip, /* incore inode */
4733 xfs_fileoff_t bno, /* starting file offs. mapped */
4734 xfs_filblks_t len, /* length to map in file */
4735 int flags, /* XFS_BMAPI_... */
4736 xfs_fsblock_t *firstblock, /* first allocated block
4737 controls a.g. for allocs */
4738 xfs_extlen_t total, /* total blocks needed */
4739 xfs_bmbt_irec_t *mval, /* output: map values */
4740 int *nmap, /* i/o: mval size/count */
4741 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4742 xfs_extdelta_t *delta) /* o: change made to incore extents */
4744 xfs_fsblock_t abno; /* allocated block number */
4745 xfs_extlen_t alen; /* allocated extent length */
4746 xfs_fileoff_t aoff; /* allocated file offset */
4747 xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
4748 xfs_btree_cur_t *cur; /* bmap btree cursor */
4749 xfs_fileoff_t end; /* end of mapped file region */
4750 int eof; /* we've hit the end of extents */
4751 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
4752 int error; /* error return */
4753 xfs_bmbt_irec_t got; /* current file extent record */
4754 xfs_ifork_t *ifp; /* inode fork pointer */
4755 xfs_extlen_t indlen; /* indirect blocks length */
4756 xfs_extnum_t lastx; /* last useful extent number */
4757 int logflags; /* flags for transaction logging */
4758 xfs_extlen_t minleft; /* min blocks left after allocation */
4759 xfs_extlen_t minlen; /* min allocation size */
4760 xfs_mount_t *mp; /* xfs mount structure */
4761 int n; /* current extent index */
4762 int nallocs; /* number of extents alloc\'d */
4763 xfs_extnum_t nextents; /* number of extents in file */
4764 xfs_fileoff_t obno; /* old block number (offset) */
4765 xfs_bmbt_irec_t prev; /* previous file extent record */
4766 int tmp_logflags; /* temp flags holder */
4767 int whichfork; /* data or attr fork */
4768 char inhole; /* current location is hole in file */
4769 char wasdelay; /* old extent was delayed */
4770 char wr; /* this is a write request */
4771 char rt; /* this is a realtime file */
4773 xfs_fileoff_t orig_bno; /* original block number value */
4774 int orig_flags; /* original flags arg value */
4775 xfs_filblks_t orig_len; /* original value of len arg */
4776 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4777 int orig_nmap; /* original value of *nmap */
4786 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4787 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4788 XFS_ATTR_FORK : XFS_DATA_FORK;
4790 if (unlikely(XFS_TEST_ERROR(
4791 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4792 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4793 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4794 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4795 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4796 return XFS_ERROR(EFSCORRUPTED);
4798 if (XFS_FORCED_SHUTDOWN(mp))
4799 return XFS_ERROR(EIO);
4800 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4801 ifp = XFS_IFORK_PTR(ip, whichfork);
4802 ASSERT(ifp->if_ext_max ==
4803 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4804 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4805 XFS_STATS_INC(xs_blk_mapw);
4807 XFS_STATS_INC(xs_blk_mapr);
4809 * IGSTATE flag is used to combine extents which
4810 * differ only due to the state of the extents.
4811 * This technique is used from xfs_getbmap()
4812 * when the caller does not wish to see the
4813 * separation (which is the default).
4815 * This technique is also used when writing a
4816 * buffer which has been partially written,
4817 * (usually by being flushed during a chunkread),
4818 * to ensure one write takes place. This also
4819 * prevents a change in the xfs inode extents at
4820 * this time, intentionally. This change occurs
4821 * on completion of the write operation, in
4822 * xfs_strat_comp(), where the xfs_bmapi() call
4823 * is transactioned, and the extents combined.
4825 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4826 wr = 0; /* no allocations are allowed */
4827 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
4831 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4833 if ((error = xfs_bmap_local_to_extents(tp, ip,
4834 firstblock, total, &logflags, whichfork)))
4837 if (wr && *firstblock == NULLFSBLOCK) {
4838 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4839 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4844 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4845 (error = xfs_iread_extents(tp, ip, whichfork)))
4847 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4849 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4855 delta->xed_startoff = NULLFILEOFF;
4856 delta->xed_blockcount = 0;
4858 while (bno < end && n < *nmap) {
4860 * Reading past eof, act as though there's a hole
4864 got.br_startoff = end;
4865 inhole = eof || got.br_startoff > bno;
4866 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
4867 ISNULLSTARTBLOCK(got.br_startblock);
4869 * First, deal with the hole before the allocated space
4870 * that we found, if any.
4872 if (wr && (inhole || wasdelay)) {
4874 * For the wasdelay case, we could also just
4875 * allocate the stuff asked for in this bmap call
4876 * but that wouldn't be as good.
4878 if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
4879 alen = (xfs_extlen_t)got.br_blockcount;
4880 aoff = got.br_startoff;
4881 if (lastx != NULLEXTNUM && lastx) {
4882 ep = xfs_iext_get_ext(ifp, lastx - 1);
4883 xfs_bmbt_get_all(ep, &prev);
4885 } else if (wasdelay) {
4886 alen = (xfs_extlen_t)
4887 XFS_FILBLKS_MIN(len,
4889 got.br_blockcount) - bno);
4892 alen = (xfs_extlen_t)
4893 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4895 alen = (xfs_extlen_t)
4896 XFS_FILBLKS_MIN(alen,
4897 got.br_startoff - bno);
4900 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4901 if (flags & XFS_BMAPI_DELAY) {
4904 /* Figure out the extent size, adjust alen */
4905 extsz = xfs_get_extsz_hint(ip);
4907 error = xfs_bmap_extsize_align(mp,
4910 flags&XFS_BMAPI_DELAY,
4911 flags&XFS_BMAPI_CONVERT,
4917 extsz = alen / mp->m_sb.sb_rextsize;
4920 * Make a transaction-less quota reservation for
4921 * delayed allocation blocks. This number gets
4922 * adjusted later. We return if we haven't
4923 * allocated blocks already inside this loop.
4925 if ((error = XFS_TRANS_RESERVE_QUOTA_NBLKS(
4926 mp, NULL, ip, (long)alen, 0,
4927 rt ? XFS_QMOPT_RES_RTBLKS :
4928 XFS_QMOPT_RES_REGBLKS))) {
4931 ASSERT(cur == NULL);
4938 * Split changing sb for alen and indlen since
4939 * they could be coming from different places.
4941 indlen = (xfs_extlen_t)
4942 xfs_bmap_worst_indlen(ip, alen);
4946 error = xfs_mod_incore_sb(mp,
4948 -((int64_t)extsz), (flags &
4949 XFS_BMAPI_RSVBLOCKS));
4951 error = xfs_mod_incore_sb(mp,
4953 -((int64_t)alen), (flags &
4954 XFS_BMAPI_RSVBLOCKS));
4957 error = xfs_mod_incore_sb(mp,
4959 -((int64_t)indlen), (flags &
4960 XFS_BMAPI_RSVBLOCKS));
4962 xfs_mod_incore_sb(mp,
4964 (int64_t)extsz, (flags &
4965 XFS_BMAPI_RSVBLOCKS));
4967 xfs_mod_incore_sb(mp,
4969 (int64_t)alen, (flags &
4970 XFS_BMAPI_RSVBLOCKS));
4974 if (XFS_IS_QUOTA_ON(mp))
4975 /* unreserve the blocks now */
4977 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
4980 XFS_QMOPT_RES_RTBLKS :
4981 XFS_QMOPT_RES_REGBLKS);
4985 ip->i_delayed_blks += alen;
4986 abno = NULLSTARTBLOCK(indlen);
4989 * If first time, allocate and fill in
4990 * once-only bma fields.
4992 if (bma.ip == NULL) {
5000 /* Indicate if this is the first user data
5001 * in the file, or just any user data.
5003 if (!(flags & XFS_BMAPI_METADATA)) {
5004 bma.userdata = (aoff == 0) ?
5005 XFS_ALLOC_INITIAL_USER_DATA :
5009 * Fill in changeable bma fields.
5012 bma.firstblock = *firstblock;
5015 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
5016 bma.wasdel = wasdelay;
5017 bma.minlen = minlen;
5018 bma.low = flist->xbf_low;
5019 bma.minleft = minleft;
5021 * Only want to do the alignment at the
5022 * eof if it is userdata and allocation length
5023 * is larger than a stripe unit.
5025 if (mp->m_dalign && alen >= mp->m_dalign &&
5026 (!(flags & XFS_BMAPI_METADATA)) &&
5027 (whichfork == XFS_DATA_FORK)) {
5028 if ((error = xfs_bmap_isaeof(ip, aoff,
5029 whichfork, &bma.aeof)))
5036 if ((error = xfs_bmap_alloc(&bma)))
5039 * Copy out result fields.
5042 if ((flist->xbf_low = bma.low))
5046 ASSERT(*firstblock == NULLFSBLOCK ||
5047 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5048 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
5050 XFS_FSB_TO_AGNO(mp, *firstblock) <
5051 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
5052 *firstblock = bma.firstblock;
5054 cur->bc_private.b.firstblock =
5056 if (abno == NULLFSBLOCK)
5058 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5059 cur = xfs_bmbt_init_cursor(mp, tp,
5061 cur->bc_private.b.firstblock =
5063 cur->bc_private.b.flist = flist;
5066 * Bump the number of extents we've allocated
5072 cur->bc_private.b.flags =
5073 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
5074 got.br_startoff = aoff;
5075 got.br_startblock = abno;
5076 got.br_blockcount = alen;
5077 got.br_state = XFS_EXT_NORM; /* assume normal */
5079 * Determine state of extent, and the filesystem.
5080 * A wasdelay extent has been initialized, so
5081 * shouldn't be flagged as unwritten.
5083 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5084 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
5085 got.br_state = XFS_EXT_UNWRITTEN;
5087 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
5088 firstblock, flist, &tmp_logflags, delta,
5089 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
5090 logflags |= tmp_logflags;
5093 lastx = ifp->if_lastex;
5094 ep = xfs_iext_get_ext(ifp, lastx);
5095 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5096 xfs_bmbt_get_all(ep, &got);
5097 ASSERT(got.br_startoff <= aoff);
5098 ASSERT(got.br_startoff + got.br_blockcount >=
5101 if (flags & XFS_BMAPI_DELAY) {
5102 ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
5103 ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
5105 ASSERT(got.br_state == XFS_EXT_NORM ||
5106 got.br_state == XFS_EXT_UNWRITTEN);
5109 * Fall down into the found allocated space case.
5111 } else if (inhole) {
5113 * Reading in a hole.
5115 mval->br_startoff = bno;
5116 mval->br_startblock = HOLESTARTBLOCK;
5117 mval->br_blockcount =
5118 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
5119 mval->br_state = XFS_EXT_NORM;
5120 bno += mval->br_blockcount;
5121 len -= mval->br_blockcount;
5127 * Then deal with the allocated space we found.
5130 if (!(flags & XFS_BMAPI_ENTIRE) &&
5131 (got.br_startoff + got.br_blockcount > obno)) {
5134 ASSERT((bno >= obno) || (n == 0));
5136 mval->br_startoff = bno;
5137 if (ISNULLSTARTBLOCK(got.br_startblock)) {
5138 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5139 mval->br_startblock = DELAYSTARTBLOCK;
5141 mval->br_startblock =
5143 (bno - got.br_startoff);
5145 * Return the minimum of what we got and what we
5146 * asked for for the length. We can use the len
5147 * variable here because it is modified below
5148 * and we could have been there before coming
5149 * here if the first part of the allocation
5150 * didn't overlap what was asked for.
5152 mval->br_blockcount =
5153 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
5154 (bno - got.br_startoff));
5155 mval->br_state = got.br_state;
5156 ASSERT(mval->br_blockcount <= len);
5159 if (ISNULLSTARTBLOCK(mval->br_startblock)) {
5160 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5161 mval->br_startblock = DELAYSTARTBLOCK;
5166 * Check if writing previously allocated but
5167 * unwritten extents.
5169 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
5170 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
5172 * Modify (by adding) the state flag, if writing.
5174 ASSERT(mval->br_blockcount <= len);
5175 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5176 cur = xfs_bmbt_init_cursor(mp,
5178 cur->bc_private.b.firstblock =
5180 cur->bc_private.b.flist = flist;
5182 mval->br_state = XFS_EXT_NORM;
5183 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
5184 firstblock, flist, &tmp_logflags, delta,
5185 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
5186 logflags |= tmp_logflags;
5189 lastx = ifp->if_lastex;
5190 ep = xfs_iext_get_ext(ifp, lastx);
5191 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5192 xfs_bmbt_get_all(ep, &got);
5194 * We may have combined previously unwritten
5195 * space with written space, so generate
5198 if (mval->br_blockcount < len)
5202 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5203 ((mval->br_startoff + mval->br_blockcount) <= end));
5204 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5205 (mval->br_blockcount <= len) ||
5206 (mval->br_startoff < obno));
5207 bno = mval->br_startoff + mval->br_blockcount;
5209 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
5210 ASSERT(mval->br_startblock == mval[-1].br_startblock);
5211 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
5212 ASSERT(mval->br_state == mval[-1].br_state);
5213 mval[-1].br_blockcount = mval->br_blockcount;
5214 mval[-1].br_state = mval->br_state;
5215 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
5216 mval[-1].br_startblock != DELAYSTARTBLOCK &&
5217 mval[-1].br_startblock != HOLESTARTBLOCK &&
5218 mval->br_startblock ==
5219 mval[-1].br_startblock + mval[-1].br_blockcount &&
5220 ((flags & XFS_BMAPI_IGSTATE) ||
5221 mval[-1].br_state == mval->br_state)) {
5222 ASSERT(mval->br_startoff ==
5223 mval[-1].br_startoff + mval[-1].br_blockcount);
5224 mval[-1].br_blockcount += mval->br_blockcount;
5226 mval->br_startblock == DELAYSTARTBLOCK &&
5227 mval[-1].br_startblock == DELAYSTARTBLOCK &&
5228 mval->br_startoff ==
5229 mval[-1].br_startoff + mval[-1].br_blockcount) {
5230 mval[-1].br_blockcount += mval->br_blockcount;
5231 mval[-1].br_state = mval->br_state;
5232 } else if (!((n == 0) &&
5233 ((mval->br_startoff + mval->br_blockcount) <=
5239 * If we're done, stop now. Stop when we've allocated
5240 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5241 * the transaction may get too big.
5243 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5246 * Else go on to the next record.
5248 ep = xfs_iext_get_ext(ifp, ++lastx);
5250 if (lastx >= nextents)
5253 xfs_bmbt_get_all(ep, &got);
5255 ifp->if_lastex = lastx;
5258 * Transform from btree to extents, give it cur.
5260 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5261 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5263 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5264 &tmp_logflags, whichfork);
5265 logflags |= tmp_logflags;
5269 ASSERT(ifp->if_ext_max ==
5270 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5271 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5272 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5274 if (delta && delta->xed_startoff != NULLFILEOFF) {
5275 /* A change was actually made.
5276 * Note that delta->xed_blockount is an offset at this
5277 * point and needs to be converted to a block count.
5279 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5280 delta->xed_blockcount -= delta->xed_startoff;
5284 * Log everything. Do this after conversion, there's no point in
5285 * logging the extent records if we've converted to btree format.
5287 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5288 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5289 logflags &= ~XFS_ILOG_FEXT(whichfork);
5290 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5291 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5292 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5294 * Log whatever the flags say, even if error. Otherwise we might miss
5295 * detecting a case where the data is changed, there's an error,
5296 * and it's not logged so we don't shutdown when we should.
5300 xfs_trans_log_inode(tp, ip, logflags);
5304 ASSERT(*firstblock == NULLFSBLOCK ||
5305 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5307 cur->bc_private.b.firstblock) ||
5309 XFS_FSB_TO_AGNO(mp, *firstblock) <
5311 cur->bc_private.b.firstblock)));
5312 *firstblock = cur->bc_private.b.firstblock;
5314 xfs_btree_del_cursor(cur,
5315 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5318 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5324 * Map file blocks to filesystem blocks, simple version.
5325 * One block (extent) only, read-only.
5326 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5327 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5328 * was set and all the others were clear.
5332 xfs_trans_t *tp, /* transaction pointer */
5333 xfs_inode_t *ip, /* incore inode */
5334 int whichfork, /* data or attr fork */
5335 xfs_fsblock_t *fsb, /* output: mapped block */
5336 xfs_fileoff_t bno) /* starting file offs. mapped */
5338 int eof; /* we've hit the end of extents */
5339 int error; /* error return */
5340 xfs_bmbt_irec_t got; /* current file extent record */
5341 xfs_ifork_t *ifp; /* inode fork pointer */
5342 xfs_extnum_t lastx; /* last useful extent number */
5343 xfs_bmbt_irec_t prev; /* previous file extent record */
5345 ifp = XFS_IFORK_PTR(ip, whichfork);
5347 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5348 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5349 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5351 return XFS_ERROR(EFSCORRUPTED);
5353 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5354 return XFS_ERROR(EIO);
5355 XFS_STATS_INC(xs_blk_mapr);
5356 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5357 (error = xfs_iread_extents(tp, ip, whichfork)))
5359 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5362 * Reading past eof, act as though there's a hole
5365 if (eof || got.br_startoff > bno) {
5369 ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
5370 ASSERT(bno < got.br_startoff + got.br_blockcount);
5371 *fsb = got.br_startblock + (bno - got.br_startoff);
5372 ifp->if_lastex = lastx;
5377 * Unmap (remove) blocks from a file.
5378 * If nexts is nonzero then the number of extents to remove is limited to
5379 * that value. If not all extents in the block range can be removed then
5384 xfs_trans_t *tp, /* transaction pointer */
5385 struct xfs_inode *ip, /* incore inode */
5386 xfs_fileoff_t bno, /* starting offset to unmap */
5387 xfs_filblks_t len, /* length to unmap in file */
5388 int flags, /* misc flags */
5389 xfs_extnum_t nexts, /* number of extents max */
5390 xfs_fsblock_t *firstblock, /* first allocated block
5391 controls a.g. for allocs */
5392 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5393 xfs_extdelta_t *delta, /* o: change made to incore
5395 int *done) /* set if not done yet */
5397 xfs_btree_cur_t *cur; /* bmap btree cursor */
5398 xfs_bmbt_irec_t del; /* extent being deleted */
5399 int eof; /* is deleting at eof */
5400 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
5401 int error; /* error return value */
5402 xfs_extnum_t extno; /* extent number in list */
5403 xfs_bmbt_irec_t got; /* current extent record */
5404 xfs_ifork_t *ifp; /* inode fork pointer */
5405 int isrt; /* freeing in rt area */
5406 xfs_extnum_t lastx; /* last extent index used */
5407 int logflags; /* transaction logging flags */
5408 xfs_extlen_t mod; /* rt extent offset */
5409 xfs_mount_t *mp; /* mount structure */
5410 xfs_extnum_t nextents; /* number of file extents */
5411 xfs_bmbt_irec_t prev; /* previous extent record */
5412 xfs_fileoff_t start; /* first file offset deleted */
5413 int tmp_logflags; /* partial logging flags */
5414 int wasdel; /* was a delayed alloc extent */
5415 int whichfork; /* data or attribute fork */
5416 int rsvd; /* OK to allocate reserved blocks */
5419 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
5420 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5421 XFS_ATTR_FORK : XFS_DATA_FORK;
5422 ifp = XFS_IFORK_PTR(ip, whichfork);
5424 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5425 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5426 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5428 return XFS_ERROR(EFSCORRUPTED);
5431 if (XFS_FORCED_SHUTDOWN(mp))
5432 return XFS_ERROR(EIO);
5433 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5436 ASSERT(ifp->if_ext_max ==
5437 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5438 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5439 (error = xfs_iread_extents(tp, ip, whichfork)))
5441 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5442 if (nextents == 0) {
5446 XFS_STATS_INC(xs_blk_unmap);
5447 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5449 bno = start + len - 1;
5450 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5453 delta->xed_startoff = NULLFILEOFF;
5454 delta->xed_blockcount = 0;
5457 * Check to see if the given block number is past the end of the
5458 * file, back up to the last block if so...
5461 ep = xfs_iext_get_ext(ifp, --lastx);
5462 xfs_bmbt_get_all(ep, &got);
5463 bno = got.br_startoff + got.br_blockcount - 1;
5466 if (ifp->if_flags & XFS_IFBROOT) {
5467 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5468 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5469 cur->bc_private.b.firstblock = *firstblock;
5470 cur->bc_private.b.flist = flist;
5471 cur->bc_private.b.flags = 0;
5475 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5476 (nexts == 0 || extno < nexts)) {
5478 * Is the found extent after a hole in which bno lives?
5479 * Just back up to the previous extent, if so.
5481 if (got.br_startoff > bno) {
5484 ep = xfs_iext_get_ext(ifp, lastx);
5485 xfs_bmbt_get_all(ep, &got);
5488 * Is the last block of this extent before the range
5489 * we're supposed to delete? If so, we're done.
5491 bno = XFS_FILEOFF_MIN(bno,
5492 got.br_startoff + got.br_blockcount - 1);
5496 * Then deal with the (possibly delayed) allocated space
5501 wasdel = ISNULLSTARTBLOCK(del.br_startblock);
5502 if (got.br_startoff < start) {
5503 del.br_startoff = start;
5504 del.br_blockcount -= start - got.br_startoff;
5506 del.br_startblock += start - got.br_startoff;
5508 if (del.br_startoff + del.br_blockcount > bno + 1)
5509 del.br_blockcount = bno + 1 - del.br_startoff;
5510 sum = del.br_startblock + del.br_blockcount;
5512 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5514 * Realtime extent not lined up at the end.
5515 * The extent could have been split into written
5516 * and unwritten pieces, or we could just be
5517 * unmapping part of it. But we can't really
5518 * get rid of part of a realtime extent.
5520 if (del.br_state == XFS_EXT_UNWRITTEN ||
5521 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5523 * This piece is unwritten, or we're not
5524 * using unwritten extents. Skip over it.
5527 bno -= mod > del.br_blockcount ?
5528 del.br_blockcount : mod;
5529 if (bno < got.br_startoff) {
5531 xfs_bmbt_get_all(xfs_iext_get_ext(
5537 * It's written, turn it unwritten.
5538 * This is better than zeroing it.
5540 ASSERT(del.br_state == XFS_EXT_NORM);
5541 ASSERT(xfs_trans_get_block_res(tp) > 0);
5543 * If this spans a realtime extent boundary,
5544 * chop it back to the start of the one we end at.
5546 if (del.br_blockcount > mod) {
5547 del.br_startoff += del.br_blockcount - mod;
5548 del.br_startblock += del.br_blockcount - mod;
5549 del.br_blockcount = mod;
5551 del.br_state = XFS_EXT_UNWRITTEN;
5552 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
5553 firstblock, flist, &logflags, delta,
5559 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5561 * Realtime extent is lined up at the end but not
5562 * at the front. We'll get rid of full extents if
5565 mod = mp->m_sb.sb_rextsize - mod;
5566 if (del.br_blockcount > mod) {
5567 del.br_blockcount -= mod;
5568 del.br_startoff += mod;
5569 del.br_startblock += mod;
5570 } else if ((del.br_startoff == start &&
5571 (del.br_state == XFS_EXT_UNWRITTEN ||
5572 xfs_trans_get_block_res(tp) == 0)) ||
5573 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5575 * Can't make it unwritten. There isn't
5576 * a full extent here so just skip it.
5578 ASSERT(bno >= del.br_blockcount);
5579 bno -= del.br_blockcount;
5580 if (bno < got.br_startoff) {
5582 xfs_bmbt_get_all(--ep, &got);
5585 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5587 * This one is already unwritten.
5588 * It must have a written left neighbor.
5589 * Unwrite the killed part of that one and
5593 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5595 ASSERT(prev.br_state == XFS_EXT_NORM);
5596 ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
5597 ASSERT(del.br_startblock ==
5598 prev.br_startblock + prev.br_blockcount);
5599 if (prev.br_startoff < start) {
5600 mod = start - prev.br_startoff;
5601 prev.br_blockcount -= mod;
5602 prev.br_startblock += mod;
5603 prev.br_startoff = start;
5605 prev.br_state = XFS_EXT_UNWRITTEN;
5606 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5607 &prev, firstblock, flist, &logflags,
5608 delta, XFS_DATA_FORK, 0);
5613 ASSERT(del.br_state == XFS_EXT_NORM);
5614 del.br_state = XFS_EXT_UNWRITTEN;
5615 error = xfs_bmap_add_extent(ip, lastx, &cur,
5616 &del, firstblock, flist, &logflags,
5617 delta, XFS_DATA_FORK, 0);
5624 ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
5625 /* Update realtime/data freespace, unreserve quota */
5627 xfs_filblks_t rtexts;
5629 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5630 do_div(rtexts, mp->m_sb.sb_rextsize);
5631 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5632 (int64_t)rtexts, rsvd);
5633 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5634 NULL, ip, -((long)del.br_blockcount), 0,
5635 XFS_QMOPT_RES_RTBLKS);
5637 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
5638 (int64_t)del.br_blockcount, rsvd);
5639 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5640 NULL, ip, -((long)del.br_blockcount), 0,
5641 XFS_QMOPT_RES_REGBLKS);
5643 ip->i_delayed_blks -= del.br_blockcount;
5645 cur->bc_private.b.flags |=
5646 XFS_BTCUR_BPRV_WASDEL;
5648 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5650 * If it's the case where the directory code is running
5651 * with no block reservation, and the deleted block is in
5652 * the middle of its extent, and the resulting insert
5653 * of an extent would cause transformation to btree format,
5654 * then reject it. The calling code will then swap
5655 * blocks around instead.
5656 * We have to do this now, rather than waiting for the
5657 * conversion to btree format, since the transaction
5660 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5661 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5662 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5663 del.br_startoff > got.br_startoff &&
5664 del.br_startoff + del.br_blockcount <
5665 got.br_startoff + got.br_blockcount) {
5666 error = XFS_ERROR(ENOSPC);
5669 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
5670 &tmp_logflags, delta, whichfork, rsvd);
5671 logflags |= tmp_logflags;
5674 bno = del.br_startoff - 1;
5676 lastx = ifp->if_lastex;
5678 * If not done go on to the next (previous) record.
5679 * Reset ep in case the extents array was re-alloced.
5681 ep = xfs_iext_get_ext(ifp, lastx);
5682 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5683 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5684 xfs_bmbt_get_startoff(ep) > bno) {
5686 ep = xfs_iext_get_ext(ifp, lastx);
5689 xfs_bmbt_get_all(ep, &got);
5693 ifp->if_lastex = lastx;
5694 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5695 ASSERT(ifp->if_ext_max ==
5696 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5698 * Convert to a btree if necessary.
5700 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5701 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5702 ASSERT(cur == NULL);
5703 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5704 &cur, 0, &tmp_logflags, whichfork);
5705 logflags |= tmp_logflags;
5710 * transform from btree to extents, give it cur
5712 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5713 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5714 ASSERT(cur != NULL);
5715 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5717 logflags |= tmp_logflags;
5722 * transform from extents to local?
5724 ASSERT(ifp->if_ext_max ==
5725 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5727 if (delta && delta->xed_startoff != NULLFILEOFF) {
5728 /* A change was actually made.
5729 * Note that delta->xed_blockount is an offset at this
5730 * point and needs to be converted to a block count.
5732 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5733 delta->xed_blockcount -= delta->xed_startoff;
5737 * Log everything. Do this after conversion, there's no point in
5738 * logging the extent records if we've converted to btree format.
5740 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5741 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5742 logflags &= ~XFS_ILOG_FEXT(whichfork);
5743 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5744 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5745 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5747 * Log inode even in the error case, if the transaction
5748 * is dirty we'll need to shut down the filesystem.
5751 xfs_trans_log_inode(tp, ip, logflags);
5754 *firstblock = cur->bc_private.b.firstblock;
5755 cur->bc_private.b.allocated = 0;
5757 xfs_btree_del_cursor(cur,
5758 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5764 * returns 1 for success, 0 if we failed to map the extent.
5767 xfs_getbmapx_fix_eof_hole(
5768 xfs_inode_t *ip, /* xfs incore inode pointer */
5769 struct getbmap *out, /* output structure */
5770 int prealloced, /* this is a file with
5771 * preallocated data space */
5772 __int64_t end, /* last block requested */
5773 xfs_fsblock_t startblock)
5776 xfs_mount_t *mp; /* file system mount point */
5778 if (startblock == HOLESTARTBLOCK) {
5780 out->bmv_block = -1;
5781 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5782 fixlen -= out->bmv_offset;
5783 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5784 /* Came to hole at EOF. Trim it. */
5787 out->bmv_length = fixlen;
5790 out->bmv_block = XFS_FSB_TO_DB(ip, startblock);
5797 * Fcntl interface to xfs_bmapi.
5799 int /* error code */
5802 struct getbmap *bmv, /* user bmap structure */
5803 void __user *ap, /* pointer to user's array */
5804 int interface) /* interface flags */
5806 __int64_t bmvend; /* last block requested */
5807 int error; /* return value */
5808 __int64_t fixlen; /* length for -1 case */
5809 int i; /* extent number */
5810 int lock; /* lock state */
5811 xfs_bmbt_irec_t *map; /* buffer for user's data */
5812 xfs_mount_t *mp; /* file system mount point */
5813 int nex; /* # of user extents can do */
5814 int nexleft; /* # of user extents left */
5815 int subnex; /* # of bmapi's can do */
5816 int nmap; /* number of map entries */
5817 struct getbmap out; /* output structure */
5818 int whichfork; /* data or attr fork */
5819 int prealloced; /* this is a file with
5820 * preallocated data space */
5821 int sh_unwritten; /* true, if unwritten */
5822 /* extents listed separately */
5823 int bmapi_flags; /* flags for xfs_bmapi */
5824 __int32_t oflags; /* getbmapx bmv_oflags field */
5828 whichfork = interface & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5829 sh_unwritten = (interface & BMV_IF_PREALLOC) != 0;
5831 /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
5832 * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
5833 * bit is set for the file, generate a read event in order
5834 * that the DMAPI application may do its thing before we return
5835 * the extents. Usually this means restoring user file data to
5836 * regions of the file that look like holes.
5838 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5839 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5840 * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
5841 * could misinterpret holes in a DMAPI file as true holes,
5842 * when in fact they may represent offline user data.
5844 if ((interface & BMV_IF_NO_DMAPI_READ) == 0 &&
5845 DM_EVENT_ENABLED(ip, DM_EVENT_READ) &&
5846 whichfork == XFS_DATA_FORK) {
5847 error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip, 0, 0, 0, NULL);
5849 return XFS_ERROR(error);
5852 if (whichfork == XFS_ATTR_FORK) {
5853 if (XFS_IFORK_Q(ip)) {
5854 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5855 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5856 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5857 return XFS_ERROR(EINVAL);
5858 } else if (unlikely(
5859 ip->i_d.di_aformat != 0 &&
5860 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5861 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5863 return XFS_ERROR(EFSCORRUPTED);
5865 } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5866 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5867 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5868 return XFS_ERROR(EINVAL);
5869 if (whichfork == XFS_DATA_FORK) {
5870 if (xfs_get_extsz_hint(ip) ||
5871 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5873 fixlen = XFS_MAXIOFFSET(mp);
5876 fixlen = ip->i_size;
5883 if (bmv->bmv_length == -1) {
5884 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5885 bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset),
5887 } else if (bmv->bmv_length < 0)
5888 return XFS_ERROR(EINVAL);
5889 if (bmv->bmv_length == 0) {
5890 bmv->bmv_entries = 0;
5893 nex = bmv->bmv_count - 1;
5895 return XFS_ERROR(EINVAL);
5896 bmvend = bmv->bmv_offset + bmv->bmv_length;
5898 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5900 if (whichfork == XFS_DATA_FORK &&
5901 (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size)) {
5902 /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
5903 error = xfs_flush_pages(ip, (xfs_off_t)0,
5906 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5911 ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0);
5913 lock = xfs_ilock_map_shared(ip);
5916 * Don't let nex be bigger than the number of extents
5917 * we can have assuming alternating holes and real extents.
5919 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5920 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5922 bmapi_flags = XFS_BMAPI_AFLAG(whichfork) |
5923 ((sh_unwritten) ? 0 : XFS_BMAPI_IGSTATE);
5926 * Allocate enough space to handle "subnex" maps at a time.
5929 map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP);
5931 bmv->bmv_entries = 0;
5933 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0) {
5935 goto unlock_and_return;
5941 nmap = (nexleft > subnex) ? subnex : nexleft;
5942 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5943 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5944 bmapi_flags, NULL, 0, map, &nmap,
5947 goto unlock_and_return;
5948 ASSERT(nmap <= subnex);
5950 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5952 oflags = (map[i].br_state == XFS_EXT_UNWRITTEN) ?
5953 BMV_OF_PREALLOC : 0;
5954 out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff);
5955 out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5956 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
5957 if (map[i].br_startblock == HOLESTARTBLOCK &&
5958 whichfork == XFS_ATTR_FORK) {
5959 /* came to the end of attribute fork */
5960 goto unlock_and_return;
5962 if (!xfs_getbmapx_fix_eof_hole(ip, &out,
5964 map[i].br_startblock)) {
5965 goto unlock_and_return;
5968 /* return either getbmap/getbmapx structure. */
5969 if (interface & BMV_IF_EXTENDED) {
5970 struct getbmapx outx;
5972 GETBMAP_CONVERT(out,outx);
5973 outx.bmv_oflags = oflags;
5974 outx.bmv_unused1 = outx.bmv_unused2 = 0;
5975 if (copy_to_user(ap, &outx,
5977 error = XFS_ERROR(EFAULT);
5978 goto unlock_and_return;
5981 if (copy_to_user(ap, &out,
5983 error = XFS_ERROR(EFAULT);
5984 goto unlock_and_return;
5988 out.bmv_offset + out.bmv_length;
5989 bmv->bmv_length = MAX((__int64_t)0,
5990 (__int64_t)(bmvend - bmv->bmv_offset));
5992 ap = (interface & BMV_IF_EXTENDED) ?
5994 ((struct getbmapx __user *)ap + 1) :
5996 ((struct getbmap __user *)ap + 1);
5999 } while (nmap && nexleft && bmv->bmv_length);
6002 xfs_iunlock_map_shared(ip, lock);
6003 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
6011 * Check the last inode extent to determine whether this allocation will result
6012 * in blocks being allocated at the end of the file. When we allocate new data
6013 * blocks at the end of the file which do not start at the previous data block,
6014 * we will try to align the new blocks at stripe unit boundaries.
6016 STATIC int /* error */
6018 xfs_inode_t *ip, /* incore inode pointer */
6019 xfs_fileoff_t off, /* file offset in fsblocks */
6020 int whichfork, /* data or attribute fork */
6021 char *aeof) /* return value */
6023 int error; /* error return value */
6024 xfs_ifork_t *ifp; /* inode fork pointer */
6025 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
6026 xfs_extnum_t nextents; /* number of file extents */
6027 xfs_bmbt_irec_t s; /* expanded extent record */
6029 ASSERT(whichfork == XFS_DATA_FORK);
6030 ifp = XFS_IFORK_PTR(ip, whichfork);
6031 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6032 (error = xfs_iread_extents(NULL, ip, whichfork)))
6034 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6035 if (nextents == 0) {
6040 * Go to the last extent
6042 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
6043 xfs_bmbt_get_all(lastrec, &s);
6045 * Check we are allocating in the last extent (for delayed allocations)
6046 * or past the last extent for non-delayed allocations.
6048 *aeof = (off >= s.br_startoff &&
6049 off < s.br_startoff + s.br_blockcount &&
6050 ISNULLSTARTBLOCK(s.br_startblock)) ||
6051 off >= s.br_startoff + s.br_blockcount;
6056 * Check if the endoff is outside the last extent. If so the caller will grow
6057 * the allocation to a stripe unit boundary.
6061 xfs_inode_t *ip, /* incore inode pointer */
6062 xfs_fileoff_t endoff, /* file offset in fsblocks */
6063 int whichfork, /* data or attribute fork */
6064 int *eof) /* result value */
6066 xfs_fsblock_t blockcount; /* extent block count */
6067 int error; /* error return value */
6068 xfs_ifork_t *ifp; /* inode fork pointer */
6069 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
6070 xfs_extnum_t nextents; /* number of file extents */
6071 xfs_fileoff_t startoff; /* extent starting file offset */
6073 ASSERT(whichfork == XFS_DATA_FORK);
6074 ifp = XFS_IFORK_PTR(ip, whichfork);
6075 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6076 (error = xfs_iread_extents(NULL, ip, whichfork)))
6078 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6079 if (nextents == 0) {
6084 * Go to the last extent
6086 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
6087 startoff = xfs_bmbt_get_startoff(lastrec);
6088 blockcount = xfs_bmbt_get_blockcount(lastrec);
6089 *eof = endoff >= startoff + blockcount;
6097 xfs_btree_cur_t *cur,
6107 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
6108 bp = cur->bc_bufs[i];
6110 if (XFS_BUF_ADDR(bp) == bno)
6111 break; /* Found it */
6113 if (i == XFS_BTREE_MAXLEVELS)
6116 if (!bp) { /* Chase down all the log items to see if the bp is there */
6117 xfs_log_item_chunk_t *licp;
6121 licp = &tp->t_items;
6122 while (!bp && licp != NULL) {
6123 if (xfs_lic_are_all_free(licp)) {
6124 licp = licp->lic_next;
6127 for (i = 0; i < licp->lic_unused; i++) {
6128 xfs_log_item_desc_t *lidp;
6129 xfs_log_item_t *lip;
6130 xfs_buf_log_item_t *bip;
6133 if (xfs_lic_isfree(licp, i)) {
6137 lidp = xfs_lic_slot(licp, i);
6138 lip = lidp->lid_item;
6139 if (lip->li_type != XFS_LI_BUF)
6142 bip = (xfs_buf_log_item_t *)lip;
6145 if (XFS_BUF_ADDR(lbp) == bno) {
6147 break; /* Found it */
6150 licp = licp->lic_next;
6158 xfs_bmbt_block_t *block,
6164 __be64 *pp, *thispa; /* pointer to block address */
6165 xfs_bmbt_key_t *prevp, *keyp;
6167 ASSERT(be16_to_cpu(block->bb_level) > 0);
6170 for( i = 1; i <= be16_to_cpu(block->bb_numrecs); i++) {
6171 dmxr = mp->m_bmap_dmxr[0];
6174 keyp = XFS_BMAP_BROOT_KEY_ADDR(block, i, sz);
6176 keyp = XFS_BTREE_KEY_ADDR(xfs_bmbt, block, i);
6180 xfs_btree_check_key(XFS_BTNUM_BMAP, prevp, keyp);
6185 * Compare the block numbers to see if there are dups.
6189 pp = XFS_BMAP_BROOT_PTR_ADDR(block, i, sz);
6191 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, i, dmxr);
6193 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
6195 thispa = XFS_BMAP_BROOT_PTR_ADDR(block, j, sz);
6197 thispa = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, j,
6200 if (*thispa == *pp) {
6201 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
6203 (unsigned long long)be64_to_cpu(*thispa));
6204 panic("%s: ptrs are equal in node\n",
6212 * Check that the extents for the inode ip are in the right order in all
6217 xfs_bmap_check_leaf_extents(
6218 xfs_btree_cur_t *cur, /* btree cursor or null */
6219 xfs_inode_t *ip, /* incore inode pointer */
6220 int whichfork) /* data or attr fork */
6222 xfs_bmbt_block_t *block; /* current btree block */
6223 xfs_fsblock_t bno; /* block # of "block" */
6224 xfs_buf_t *bp; /* buffer for "block" */
6225 int error; /* error return value */
6226 xfs_extnum_t i=0, j; /* index into the extents list */
6227 xfs_ifork_t *ifp; /* fork structure */
6228 int level; /* btree level, for checking */
6229 xfs_mount_t *mp; /* file system mount structure */
6230 __be64 *pp; /* pointer to block address */
6231 xfs_bmbt_rec_t *ep; /* pointer to current extent */
6232 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
6233 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
6236 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
6242 ifp = XFS_IFORK_PTR(ip, whichfork);
6243 block = ifp->if_broot;
6245 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6247 level = be16_to_cpu(block->bb_level);
6249 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
6250 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6251 bno = be64_to_cpu(*pp);
6253 ASSERT(bno != NULLDFSBNO);
6254 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6255 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6258 * Go down the tree until leaf level is reached, following the first
6259 * pointer (leftmost) at each level.
6261 while (level-- > 0) {
6262 /* See if buf is in cur first */
6263 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6269 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6270 XFS_BMAP_BTREE_REF)))
6272 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6273 XFS_WANT_CORRUPTED_GOTO(
6274 XFS_BMAP_SANITY_CHECK(mp, block, level),
6280 * Check this block for basic sanity (increasing keys and
6281 * no duplicate blocks).
6284 xfs_check_block(block, mp, 0, 0);
6285 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
6286 bno = be64_to_cpu(*pp);
6287 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
6290 xfs_trans_brelse(NULL, bp);
6295 * Here with bp and block set to the leftmost leaf node in the tree.
6300 * Loop over all leaf nodes checking that all extents are in the right order.
6303 xfs_fsblock_t nextbno;
6304 xfs_extnum_t num_recs;
6307 num_recs = be16_to_cpu(block->bb_numrecs);
6310 * Read-ahead the next leaf block, if any.
6313 nextbno = be64_to_cpu(block->bb_rightsib);
6316 * Check all the extents to make sure they are OK.
6317 * If we had a previous block, the last entry should
6318 * conform with the first entry in this one.
6321 ep = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
6323 xfs_btree_check_rec(XFS_BTNUM_BMAP, &last, ep);
6325 for (j = 1; j < num_recs; j++) {
6326 nextp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, j + 1);
6327 xfs_btree_check_rec(XFS_BTNUM_BMAP, ep, nextp);
6335 xfs_trans_brelse(NULL, bp);
6339 * If we've reached the end, stop.
6341 if (bno == NULLFSBLOCK)
6344 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6350 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6351 XFS_BMAP_BTREE_REF)))
6353 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6357 xfs_trans_brelse(NULL, bp);
6362 cmn_err(CE_WARN, "%s: at error0", __func__);
6364 xfs_trans_brelse(NULL, bp);
6366 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
6368 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
6374 * Count fsblocks of the given fork.
6377 xfs_bmap_count_blocks(
6378 xfs_trans_t *tp, /* transaction pointer */
6379 xfs_inode_t *ip, /* incore inode */
6380 int whichfork, /* data or attr fork */
6381 int *count) /* out: count of blocks */
6383 xfs_bmbt_block_t *block; /* current btree block */
6384 xfs_fsblock_t bno; /* block # of "block" */
6385 xfs_ifork_t *ifp; /* fork structure */
6386 int level; /* btree level, for checking */
6387 xfs_mount_t *mp; /* file system mount structure */
6388 __be64 *pp; /* pointer to block address */
6392 ifp = XFS_IFORK_PTR(ip, whichfork);
6393 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
6394 xfs_bmap_count_leaves(ifp, 0,
6395 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6401 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6403 block = ifp->if_broot;
6404 level = be16_to_cpu(block->bb_level);
6406 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6407 bno = be64_to_cpu(*pp);
6408 ASSERT(bno != NULLDFSBNO);
6409 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6410 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6412 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
6413 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6415 return XFS_ERROR(EFSCORRUPTED);
6422 * Recursively walks each level of a btree
6423 * to count total fsblocks is use.
6425 STATIC int /* error */
6426 xfs_bmap_count_tree(
6427 xfs_mount_t *mp, /* file system mount point */
6428 xfs_trans_t *tp, /* transaction pointer */
6429 xfs_ifork_t *ifp, /* inode fork pointer */
6430 xfs_fsblock_t blockno, /* file system block number */
6431 int levelin, /* level in btree */
6432 int *count) /* Count of blocks */
6435 xfs_buf_t *bp, *nbp;
6436 int level = levelin;
6438 xfs_fsblock_t bno = blockno;
6439 xfs_fsblock_t nextbno;
6440 xfs_bmbt_block_t *block, *nextblock;
6443 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6446 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6449 /* Not at node above leafs, count this level of nodes */
6450 nextbno = be64_to_cpu(block->bb_rightsib);
6451 while (nextbno != NULLFSBLOCK) {
6452 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6453 0, &nbp, XFS_BMAP_BTREE_REF)))
6456 nextblock = XFS_BUF_TO_BMBT_BLOCK(nbp);
6457 nextbno = be64_to_cpu(nextblock->bb_rightsib);
6458 xfs_trans_brelse(tp, nbp);
6461 /* Dive to the next level */
6462 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
6463 bno = be64_to_cpu(*pp);
6464 if (unlikely((error =
6465 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
6466 xfs_trans_brelse(tp, bp);
6467 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6468 XFS_ERRLEVEL_LOW, mp);
6469 return XFS_ERROR(EFSCORRUPTED);
6471 xfs_trans_brelse(tp, bp);
6473 /* count all level 1 nodes and their leaves */
6475 nextbno = be64_to_cpu(block->bb_rightsib);
6476 numrecs = be16_to_cpu(block->bb_numrecs);
6477 xfs_bmap_disk_count_leaves(0, block, numrecs, count);
6478 xfs_trans_brelse(tp, bp);
6479 if (nextbno == NULLFSBLOCK)
6482 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6483 XFS_BMAP_BTREE_REF)))
6486 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6493 * Count leaf blocks given a range of extent records.
6496 xfs_bmap_count_leaves(
6504 for (b = 0; b < numrecs; b++) {
6505 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
6506 *count += xfs_bmbt_get_blockcount(frp);
6511 * Count leaf blocks given a range of extent records originally
6515 xfs_bmap_disk_count_leaves(
6517 xfs_bmbt_block_t *block,
6522 xfs_bmbt_rec_t *frp;
6524 for (b = 1; b <= numrecs; b++) {
6525 frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, idx + b);
6526 *count += xfs_bmbt_disk_get_blockcount(frp);