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_dinode.h"
34 #include "xfs_inode.h"
35 #include "xfs_btree.h"
36 #include "xfs_mount.h"
37 #include "xfs_itable.h"
38 #include "xfs_dir2_data.h"
39 #include "xfs_dir2_leaf.h"
40 #include "xfs_dir2_block.h"
41 #include "xfs_inode_item.h"
42 #include "xfs_extfree_item.h"
43 #include "xfs_alloc.h"
45 #include "xfs_rtalloc.h"
46 #include "xfs_error.h"
47 #include "xfs_attr_leaf.h"
49 #include "xfs_quota.h"
50 #include "xfs_trans_space.h"
51 #include "xfs_buf_item.h"
52 #include "xfs_filestream.h"
53 #include "xfs_vnodeops.h"
54 #include "xfs_trace.h"
59 xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
62 kmem_zone_t *xfs_bmap_free_item_zone;
65 * Prototypes for internal bmap routines.
70 * Called from xfs_bmap_add_attrfork to handle extents format files.
72 STATIC int /* error */
73 xfs_bmap_add_attrfork_extents(
74 xfs_trans_t *tp, /* transaction pointer */
75 xfs_inode_t *ip, /* incore inode pointer */
76 xfs_fsblock_t *firstblock, /* first block allocated */
77 xfs_bmap_free_t *flist, /* blocks to free at commit */
78 int *flags); /* inode logging flags */
81 * Called from xfs_bmap_add_attrfork to handle local format files.
83 STATIC int /* error */
84 xfs_bmap_add_attrfork_local(
85 xfs_trans_t *tp, /* transaction pointer */
86 xfs_inode_t *ip, /* incore inode pointer */
87 xfs_fsblock_t *firstblock, /* first block allocated */
88 xfs_bmap_free_t *flist, /* blocks to free at commit */
89 int *flags); /* inode logging flags */
92 * Called by xfs_bmapi to update file extent records and the btree
93 * after allocating space (or doing a delayed allocation).
95 STATIC int /* error */
97 xfs_inode_t *ip, /* incore inode pointer */
98 xfs_extnum_t idx, /* extent number to update/insert */
99 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
100 xfs_bmbt_irec_t *new, /* new data to add to file extents */
101 xfs_fsblock_t *first, /* pointer to firstblock variable */
102 xfs_bmap_free_t *flist, /* list of extents to be freed */
103 int *logflagsp, /* inode logging flags */
104 int whichfork, /* data or attr fork */
105 int rsvd); /* OK to allocate reserved blocks */
108 * Called by xfs_bmap_add_extent to handle cases converting a delayed
109 * allocation to a real allocation.
111 STATIC int /* error */
112 xfs_bmap_add_extent_delay_real(
113 xfs_inode_t *ip, /* incore inode pointer */
114 xfs_extnum_t idx, /* extent number to update/insert */
115 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
116 xfs_bmbt_irec_t *new, /* new data to add to file extents */
117 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
118 xfs_fsblock_t *first, /* pointer to firstblock variable */
119 xfs_bmap_free_t *flist, /* list of extents to be freed */
120 int *logflagsp, /* inode logging flags */
121 int rsvd); /* OK to allocate reserved blocks */
124 * Called by xfs_bmap_add_extent to handle cases converting a hole
125 * to a delayed allocation.
127 STATIC int /* error */
128 xfs_bmap_add_extent_hole_delay(
129 xfs_inode_t *ip, /* incore inode pointer */
130 xfs_extnum_t idx, /* extent number to update/insert */
131 xfs_bmbt_irec_t *new, /* new data to add to file extents */
132 int *logflagsp,/* inode logging flags */
133 int rsvd); /* OK to allocate reserved blocks */
136 * Called by xfs_bmap_add_extent to handle cases converting a hole
137 * to a real allocation.
139 STATIC int /* error */
140 xfs_bmap_add_extent_hole_real(
141 xfs_inode_t *ip, /* incore inode pointer */
142 xfs_extnum_t idx, /* extent number to update/insert */
143 xfs_btree_cur_t *cur, /* if null, not a btree */
144 xfs_bmbt_irec_t *new, /* new data to add to file extents */
145 int *logflagsp, /* inode logging flags */
146 int whichfork); /* data or attr fork */
149 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
150 * allocation to a real allocation or vice versa.
152 STATIC int /* error */
153 xfs_bmap_add_extent_unwritten_real(
154 xfs_inode_t *ip, /* incore inode pointer */
155 xfs_extnum_t idx, /* extent number to update/insert */
156 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
157 xfs_bmbt_irec_t *new, /* new data to add to file extents */
158 int *logflagsp); /* inode logging flags */
161 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
162 * It figures out where to ask the underlying allocator to put the new extent.
164 STATIC int /* error */
166 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
169 * Transform a btree format file with only one leaf node, where the
170 * extents list will fit in the inode, into an extents format file.
171 * Since the file extents are already in-core, all we have to do is
172 * give up the space for the btree root and pitch the leaf block.
174 STATIC int /* error */
175 xfs_bmap_btree_to_extents(
176 xfs_trans_t *tp, /* transaction pointer */
177 xfs_inode_t *ip, /* incore inode pointer */
178 xfs_btree_cur_t *cur, /* btree cursor */
179 int *logflagsp, /* inode logging flags */
180 int whichfork); /* data or attr fork */
183 * Called by xfs_bmapi to update file extent records and the btree
184 * after removing space (or undoing a delayed allocation).
186 STATIC int /* error */
188 xfs_inode_t *ip, /* incore inode pointer */
189 xfs_trans_t *tp, /* current trans pointer */
190 xfs_extnum_t idx, /* extent number to update/insert */
191 xfs_bmap_free_t *flist, /* list of extents to be freed */
192 xfs_btree_cur_t *cur, /* if null, not a btree */
193 xfs_bmbt_irec_t *new, /* new data to add to file extents */
194 int *logflagsp,/* inode logging flags */
195 int whichfork, /* data or attr fork */
196 int rsvd); /* OK to allocate reserved blocks */
199 * Remove the entry "free" from the free item list. Prev points to the
200 * previous entry, unless "free" is the head of the list.
204 xfs_bmap_free_t *flist, /* free item list header */
205 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
206 xfs_bmap_free_item_t *free); /* list item to be freed */
209 * Convert an extents-format file into a btree-format file.
210 * The new file will have a root block (in the inode) and a single child block.
212 STATIC int /* error */
213 xfs_bmap_extents_to_btree(
214 xfs_trans_t *tp, /* transaction pointer */
215 xfs_inode_t *ip, /* incore inode pointer */
216 xfs_fsblock_t *firstblock, /* first-block-allocated */
217 xfs_bmap_free_t *flist, /* blocks freed in xaction */
218 xfs_btree_cur_t **curp, /* cursor returned to caller */
219 int wasdel, /* converting a delayed alloc */
220 int *logflagsp, /* inode logging flags */
221 int whichfork); /* data or attr fork */
224 * Convert a local file to an extents file.
225 * This code is sort of bogus, since the file data needs to get
226 * logged so it won't be lost. The bmap-level manipulations are ok, though.
228 STATIC int /* error */
229 xfs_bmap_local_to_extents(
230 xfs_trans_t *tp, /* transaction pointer */
231 xfs_inode_t *ip, /* incore inode pointer */
232 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
233 xfs_extlen_t total, /* total blocks needed by transaction */
234 int *logflagsp, /* inode logging flags */
235 int whichfork); /* data or attr fork */
238 * Search the extents list for the inode, for the extent containing bno.
239 * If bno lies in a hole, point to the next entry. If bno lies past eof,
240 * *eofp will be set, and *prevp will contain the last entry (null if none).
241 * Else, *lastxp will be set to the index of the found
242 * entry; *gotp will contain the entry.
244 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
245 xfs_bmap_search_extents(
246 xfs_inode_t *ip, /* incore inode pointer */
247 xfs_fileoff_t bno, /* block number searched for */
248 int whichfork, /* data or attr fork */
249 int *eofp, /* out: end of file found */
250 xfs_extnum_t *lastxp, /* out: last extent index */
251 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
252 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
255 * Check the last inode extent to determine whether this allocation will result
256 * in blocks being allocated at the end of the file. When we allocate new data
257 * blocks at the end of the file which do not start at the previous data block,
258 * we will try to align the new blocks at stripe unit boundaries.
260 STATIC int /* error */
262 xfs_inode_t *ip, /* incore inode pointer */
263 xfs_fileoff_t off, /* file offset in fsblocks */
264 int whichfork, /* data or attribute fork */
265 char *aeof); /* return value */
268 * Compute the worst-case number of indirect blocks that will be used
269 * for ip's delayed extent of length "len".
272 xfs_bmap_worst_indlen(
273 xfs_inode_t *ip, /* incore inode pointer */
274 xfs_filblks_t len); /* delayed extent length */
278 * Perform various validation checks on the values being returned
282 xfs_bmap_validate_ret(
286 xfs_bmbt_irec_t *mval,
290 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
298 xfs_fsblock_t blockno,
303 xfs_bmap_count_leaves(
310 xfs_bmap_disk_count_leaves(
311 struct xfs_mount *mp,
312 struct xfs_btree_block *block,
317 * Bmap internal routines.
320 STATIC int /* error */
322 struct xfs_btree_cur *cur,
326 int *stat) /* success/failure */
328 cur->bc_rec.b.br_startoff = off;
329 cur->bc_rec.b.br_startblock = bno;
330 cur->bc_rec.b.br_blockcount = len;
331 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
334 STATIC int /* error */
336 struct xfs_btree_cur *cur,
340 int *stat) /* success/failure */
342 cur->bc_rec.b.br_startoff = off;
343 cur->bc_rec.b.br_startblock = bno;
344 cur->bc_rec.b.br_blockcount = len;
345 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
349 * Update the record referred to by cur to the value given
350 * by [off, bno, len, state].
351 * This either works (return 0) or gets an EFSCORRUPTED error.
355 struct xfs_btree_cur *cur,
361 union xfs_btree_rec rec;
363 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
364 return xfs_btree_update(cur, &rec);
368 * Called from xfs_bmap_add_attrfork to handle btree format files.
370 STATIC int /* error */
371 xfs_bmap_add_attrfork_btree(
372 xfs_trans_t *tp, /* transaction pointer */
373 xfs_inode_t *ip, /* incore inode pointer */
374 xfs_fsblock_t *firstblock, /* first block allocated */
375 xfs_bmap_free_t *flist, /* blocks to free at commit */
376 int *flags) /* inode logging flags */
378 xfs_btree_cur_t *cur; /* btree cursor */
379 int error; /* error return value */
380 xfs_mount_t *mp; /* file system mount struct */
381 int stat; /* newroot status */
384 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
385 *flags |= XFS_ILOG_DBROOT;
387 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
388 cur->bc_private.b.flist = flist;
389 cur->bc_private.b.firstblock = *firstblock;
390 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
392 /* must be at least one entry */
393 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
394 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
397 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
398 return XFS_ERROR(ENOSPC);
400 *firstblock = cur->bc_private.b.firstblock;
401 cur->bc_private.b.allocated = 0;
402 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
406 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
411 * Called from xfs_bmap_add_attrfork to handle extents format files.
413 STATIC int /* error */
414 xfs_bmap_add_attrfork_extents(
415 xfs_trans_t *tp, /* transaction pointer */
416 xfs_inode_t *ip, /* incore inode pointer */
417 xfs_fsblock_t *firstblock, /* first block allocated */
418 xfs_bmap_free_t *flist, /* blocks to free at commit */
419 int *flags) /* inode logging flags */
421 xfs_btree_cur_t *cur; /* bmap btree cursor */
422 int error; /* error return value */
424 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
427 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
428 flags, XFS_DATA_FORK);
430 cur->bc_private.b.allocated = 0;
431 xfs_btree_del_cursor(cur,
432 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
438 * Called from xfs_bmap_add_attrfork to handle local format files.
440 STATIC int /* error */
441 xfs_bmap_add_attrfork_local(
442 xfs_trans_t *tp, /* transaction pointer */
443 xfs_inode_t *ip, /* incore inode pointer */
444 xfs_fsblock_t *firstblock, /* first block allocated */
445 xfs_bmap_free_t *flist, /* blocks to free at commit */
446 int *flags) /* inode logging flags */
448 xfs_da_args_t dargs; /* args for dir/attr code */
449 int error; /* error return value */
450 xfs_mount_t *mp; /* mount structure pointer */
452 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
454 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
456 memset(&dargs, 0, sizeof(dargs));
458 dargs.firstblock = firstblock;
460 dargs.total = mp->m_dirblkfsbs;
461 dargs.whichfork = XFS_DATA_FORK;
463 error = xfs_dir2_sf_to_block(&dargs);
465 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
471 * Called by xfs_bmapi to update file extent records and the btree
472 * after allocating space (or doing a delayed allocation).
474 STATIC int /* error */
476 xfs_inode_t *ip, /* incore inode pointer */
477 xfs_extnum_t idx, /* extent number to update/insert */
478 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
479 xfs_bmbt_irec_t *new, /* new data to add to file extents */
480 xfs_fsblock_t *first, /* pointer to firstblock variable */
481 xfs_bmap_free_t *flist, /* list of extents to be freed */
482 int *logflagsp, /* inode logging flags */
483 int whichfork, /* data or attr fork */
484 int rsvd) /* OK to use reserved data blocks */
486 xfs_btree_cur_t *cur; /* btree cursor or null */
487 xfs_filblks_t da_new; /* new count del alloc blocks used */
488 xfs_filblks_t da_old; /* old count del alloc blocks used */
489 int error; /* error return value */
490 xfs_ifork_t *ifp; /* inode fork ptr */
491 int logflags; /* returned value */
492 xfs_extnum_t nextents; /* number of extents in file now */
494 XFS_STATS_INC(xs_add_exlist);
496 ifp = XFS_IFORK_PTR(ip, whichfork);
497 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
498 ASSERT(idx <= nextents);
502 * This is the first extent added to a new/empty file.
503 * Special case this one, so other routines get to assume there are
504 * already extents in the list.
507 xfs_iext_insert(ip, 0, 1, new,
508 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
512 if (!isnullstartblock(new->br_startblock)) {
513 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
514 logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
519 * Any kind of new delayed allocation goes here.
521 else if (isnullstartblock(new->br_startblock)) {
523 ASSERT((cur->bc_private.b.flags &
524 XFS_BTCUR_BPRV_WASDEL) == 0);
525 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
530 * Real allocation off the end of the file.
532 else if (idx == nextents) {
534 ASSERT((cur->bc_private.b.flags &
535 XFS_BTCUR_BPRV_WASDEL) == 0);
536 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
537 &logflags, whichfork)))
540 xfs_bmbt_irec_t prev; /* old extent at offset idx */
543 * Get the record referred to by idx.
545 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
547 * If it's a real allocation record, and the new allocation ends
548 * after the start of the referred to record, then we're filling
549 * in a delayed or unwritten allocation with a real one, or
550 * converting real back to unwritten.
552 if (!isnullstartblock(new->br_startblock) &&
553 new->br_startoff + new->br_blockcount > prev.br_startoff) {
554 if (prev.br_state != XFS_EXT_UNWRITTEN &&
555 isnullstartblock(prev.br_startblock)) {
556 da_old = startblockval(prev.br_startblock);
558 ASSERT(cur->bc_private.b.flags &
559 XFS_BTCUR_BPRV_WASDEL);
560 if ((error = xfs_bmap_add_extent_delay_real(ip,
561 idx, &cur, new, &da_new, first, flist,
564 } else if (new->br_state == XFS_EXT_NORM) {
565 ASSERT(new->br_state == XFS_EXT_NORM);
566 if ((error = xfs_bmap_add_extent_unwritten_real(
567 ip, idx, &cur, new, &logflags)))
570 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
571 if ((error = xfs_bmap_add_extent_unwritten_real(
572 ip, idx, &cur, new, &logflags)))
575 ASSERT(*curp == cur || *curp == NULL);
578 * Otherwise we're filling in a hole with an allocation.
582 ASSERT((cur->bc_private.b.flags &
583 XFS_BTCUR_BPRV_WASDEL) == 0);
584 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
585 new, &logflags, whichfork)))
590 ASSERT(*curp == cur || *curp == NULL);
592 * Convert to a btree if necessary.
594 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
595 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
596 int tmp_logflags; /* partial log flag return val */
599 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
600 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
601 logflags |= tmp_logflags;
606 * Adjust for changes in reserved delayed indirect blocks.
607 * Nothing to do for disk quotas here.
609 if (da_old || da_new) {
614 nblks += cur->bc_private.b.allocated;
615 ASSERT(nblks <= da_old);
617 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
618 (int64_t)(da_old - nblks), rsvd);
621 * Clear out the allocated field, done with it now in any case.
624 cur->bc_private.b.allocated = 0;
630 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
632 *logflagsp = logflags;
637 * Called by xfs_bmap_add_extent to handle cases converting a delayed
638 * allocation to a real allocation.
640 STATIC int /* error */
641 xfs_bmap_add_extent_delay_real(
642 xfs_inode_t *ip, /* incore inode pointer */
643 xfs_extnum_t idx, /* extent number to update/insert */
644 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
645 xfs_bmbt_irec_t *new, /* new data to add to file extents */
646 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
647 xfs_fsblock_t *first, /* pointer to firstblock variable */
648 xfs_bmap_free_t *flist, /* list of extents to be freed */
649 int *logflagsp, /* inode logging flags */
650 int rsvd) /* OK to use reserved data block allocation */
652 xfs_btree_cur_t *cur; /* btree cursor */
653 int diff; /* temp value */
654 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
655 int error; /* error return value */
656 int i; /* temp state */
657 xfs_ifork_t *ifp; /* inode fork pointer */
658 xfs_fileoff_t new_endoff; /* end offset of new entry */
659 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
660 /* left is 0, right is 1, prev is 2 */
661 int rval=0; /* return value (logging flags) */
662 int state = 0;/* state bits, accessed thru macros */
663 xfs_filblks_t temp=0; /* value for dnew calculations */
664 xfs_filblks_t temp2=0;/* value for dnew calculations */
665 int tmp_rval; /* partial logging flags */
672 * Set up a bunch of variables to make the tests simpler.
675 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
676 ep = xfs_iext_get_ext(ifp, idx);
677 xfs_bmbt_get_all(ep, &PREV);
678 new_endoff = new->br_startoff + new->br_blockcount;
679 ASSERT(PREV.br_startoff <= new->br_startoff);
680 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
683 * Set flags determining what part of the previous delayed allocation
684 * extent is being replaced by a real allocation.
686 if (PREV.br_startoff == new->br_startoff)
687 state |= BMAP_LEFT_FILLING;
688 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
689 state |= BMAP_RIGHT_FILLING;
692 * Check and set flags if this segment has a left neighbor.
693 * Don't set contiguous if the combined extent would be too large.
696 state |= BMAP_LEFT_VALID;
697 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
699 if (isnullstartblock(LEFT.br_startblock))
700 state |= BMAP_LEFT_DELAY;
703 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
704 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
705 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
706 LEFT.br_state == new->br_state &&
707 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
708 state |= BMAP_LEFT_CONTIG;
711 * Check and set flags if this segment has a right neighbor.
712 * Don't set contiguous if the combined extent would be too large.
713 * Also check for all-three-contiguous being too large.
715 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
716 state |= BMAP_RIGHT_VALID;
717 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
719 if (isnullstartblock(RIGHT.br_startblock))
720 state |= BMAP_RIGHT_DELAY;
723 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
724 new_endoff == RIGHT.br_startoff &&
725 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
726 new->br_state == RIGHT.br_state &&
727 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
728 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
729 BMAP_RIGHT_FILLING)) !=
730 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
731 BMAP_RIGHT_FILLING) ||
732 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
734 state |= BMAP_RIGHT_CONTIG;
738 * Switch out based on the FILLING and CONTIG state bits.
740 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
741 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
742 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
743 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
745 * Filling in all of a previously delayed allocation extent.
746 * The left and right neighbors are both contiguous with new.
748 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
749 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
750 LEFT.br_blockcount + PREV.br_blockcount +
751 RIGHT.br_blockcount);
752 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
754 xfs_iext_remove(ip, idx, 2, state);
755 ip->i_df.if_lastex = idx - 1;
756 ip->i_d.di_nextents--;
758 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
760 rval = XFS_ILOG_CORE;
761 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
763 RIGHT.br_blockcount, &i)))
765 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
766 if ((error = xfs_btree_delete(cur, &i)))
768 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
769 if ((error = xfs_btree_decrement(cur, 0, &i)))
771 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
772 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
776 RIGHT.br_blockcount, LEFT.br_state)))
782 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
784 * Filling in all of a previously delayed allocation extent.
785 * The left neighbor is contiguous, the right is not.
787 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
788 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
789 LEFT.br_blockcount + PREV.br_blockcount);
790 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
792 ip->i_df.if_lastex = idx - 1;
793 xfs_iext_remove(ip, idx, 1, state);
795 rval = XFS_ILOG_DEXT;
798 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
799 LEFT.br_startblock, LEFT.br_blockcount,
802 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
803 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
806 PREV.br_blockcount, LEFT.br_state)))
812 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
814 * Filling in all of a previously delayed allocation extent.
815 * The right neighbor is contiguous, the left is not.
817 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
818 xfs_bmbt_set_startblock(ep, new->br_startblock);
819 xfs_bmbt_set_blockcount(ep,
820 PREV.br_blockcount + RIGHT.br_blockcount);
821 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
823 ip->i_df.if_lastex = idx;
824 xfs_iext_remove(ip, idx + 1, 1, state);
826 rval = XFS_ILOG_DEXT;
829 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
831 RIGHT.br_blockcount, &i)))
833 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
834 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
837 RIGHT.br_blockcount, PREV.br_state)))
843 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
845 * Filling in all of a previously delayed allocation extent.
846 * Neither the left nor right neighbors are contiguous with
849 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
850 xfs_bmbt_set_startblock(ep, new->br_startblock);
851 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
853 ip->i_df.if_lastex = idx;
854 ip->i_d.di_nextents++;
856 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
858 rval = XFS_ILOG_CORE;
859 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
860 new->br_startblock, new->br_blockcount,
863 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
864 cur->bc_rec.b.br_state = XFS_EXT_NORM;
865 if ((error = xfs_btree_insert(cur, &i)))
867 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
872 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
874 * Filling in the first part of a previous delayed allocation.
875 * The left neighbor is contiguous.
877 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
878 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
879 LEFT.br_blockcount + new->br_blockcount);
880 xfs_bmbt_set_startoff(ep,
881 PREV.br_startoff + new->br_blockcount);
882 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
884 temp = PREV.br_blockcount - new->br_blockcount;
885 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
886 xfs_bmbt_set_blockcount(ep, temp);
887 ip->i_df.if_lastex = idx - 1;
889 rval = XFS_ILOG_DEXT;
892 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
893 LEFT.br_startblock, LEFT.br_blockcount,
896 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
897 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
904 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
905 startblockval(PREV.br_startblock));
906 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
907 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
911 case BMAP_LEFT_FILLING:
913 * Filling in the first part of a previous delayed allocation.
914 * The left neighbor is not contiguous.
916 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
917 xfs_bmbt_set_startoff(ep, new_endoff);
918 temp = PREV.br_blockcount - new->br_blockcount;
919 xfs_bmbt_set_blockcount(ep, temp);
920 xfs_iext_insert(ip, idx, 1, new, state);
921 ip->i_df.if_lastex = idx;
922 ip->i_d.di_nextents++;
924 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
926 rval = XFS_ILOG_CORE;
927 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
928 new->br_startblock, new->br_blockcount,
931 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
932 cur->bc_rec.b.br_state = XFS_EXT_NORM;
933 if ((error = xfs_btree_insert(cur, &i)))
935 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
937 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
938 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
939 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
940 first, flist, &cur, 1, &tmp_rval,
946 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
947 startblockval(PREV.br_startblock) -
948 (cur ? cur->bc_private.b.allocated : 0));
949 ep = xfs_iext_get_ext(ifp, idx + 1);
950 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
951 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
955 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
957 * Filling in the last part of a previous delayed allocation.
958 * The right neighbor is contiguous with the new allocation.
960 temp = PREV.br_blockcount - new->br_blockcount;
961 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
962 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
963 xfs_bmbt_set_blockcount(ep, temp);
964 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
965 new->br_startoff, new->br_startblock,
966 new->br_blockcount + RIGHT.br_blockcount,
968 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
969 ip->i_df.if_lastex = idx + 1;
971 rval = XFS_ILOG_DEXT;
974 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
976 RIGHT.br_blockcount, &i)))
978 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
979 if ((error = xfs_bmbt_update(cur, new->br_startoff,
986 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
987 startblockval(PREV.br_startblock));
988 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
989 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
993 case BMAP_RIGHT_FILLING:
995 * Filling in the last part of a previous delayed allocation.
996 * The right neighbor is not contiguous.
998 temp = PREV.br_blockcount - new->br_blockcount;
999 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1000 xfs_bmbt_set_blockcount(ep, temp);
1001 xfs_iext_insert(ip, idx + 1, 1, new, state);
1002 ip->i_df.if_lastex = idx + 1;
1003 ip->i_d.di_nextents++;
1005 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1007 rval = XFS_ILOG_CORE;
1008 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1009 new->br_startblock, new->br_blockcount,
1012 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1013 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1014 if ((error = xfs_btree_insert(cur, &i)))
1016 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1018 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1019 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1020 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1021 first, flist, &cur, 1, &tmp_rval,
1027 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1028 startblockval(PREV.br_startblock) -
1029 (cur ? cur->bc_private.b.allocated : 0));
1030 ep = xfs_iext_get_ext(ifp, idx);
1031 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1032 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1038 * Filling in the middle part of a previous delayed allocation.
1039 * Contiguity is impossible here.
1040 * This case is avoided almost all the time.
1042 temp = new->br_startoff - PREV.br_startoff;
1043 trace_xfs_bmap_pre_update(ip, idx, 0, _THIS_IP_);
1044 xfs_bmbt_set_blockcount(ep, temp);
1046 r[1].br_state = PREV.br_state;
1047 r[1].br_startblock = 0;
1048 r[1].br_startoff = new_endoff;
1049 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1050 r[1].br_blockcount = temp2;
1051 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
1052 ip->i_df.if_lastex = idx + 1;
1053 ip->i_d.di_nextents++;
1055 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1057 rval = XFS_ILOG_CORE;
1058 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1059 new->br_startblock, new->br_blockcount,
1062 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1063 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1064 if ((error = xfs_btree_insert(cur, &i)))
1066 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1068 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1069 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1070 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1071 first, flist, &cur, 1, &tmp_rval,
1077 temp = xfs_bmap_worst_indlen(ip, temp);
1078 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1079 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
1080 (cur ? cur->bc_private.b.allocated : 0));
1082 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
1083 -((int64_t)diff), rsvd)) {
1085 * Ick gross gag me with a spoon.
1087 ASSERT(0); /* want to see if this ever happens! */
1093 !xfs_icsb_modify_counters(ip->i_mount,
1095 -((int64_t)diff), rsvd))
1102 !xfs_icsb_modify_counters(ip->i_mount,
1104 -((int64_t)diff), rsvd))
1109 ep = xfs_iext_get_ext(ifp, idx);
1110 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1111 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1112 trace_xfs_bmap_pre_update(ip, idx + 2, state, _THIS_IP_);
1113 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1114 nullstartblock((int)temp2));
1115 trace_xfs_bmap_post_update(ip, idx + 2, state, _THIS_IP_);
1116 *dnew = temp + temp2;
1119 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1120 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1121 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1122 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1123 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1124 case BMAP_LEFT_CONTIG:
1125 case BMAP_RIGHT_CONTIG:
1127 * These cases are all impossible.
1141 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1142 * allocation to a real allocation or vice versa.
1144 STATIC int /* error */
1145 xfs_bmap_add_extent_unwritten_real(
1146 xfs_inode_t *ip, /* incore inode pointer */
1147 xfs_extnum_t idx, /* extent number to update/insert */
1148 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
1149 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1150 int *logflagsp) /* inode logging flags */
1152 xfs_btree_cur_t *cur; /* btree cursor */
1153 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1154 int error; /* error return value */
1155 int i; /* temp state */
1156 xfs_ifork_t *ifp; /* inode fork pointer */
1157 xfs_fileoff_t new_endoff; /* end offset of new entry */
1158 xfs_exntst_t newext; /* new extent state */
1159 xfs_exntst_t oldext; /* old extent state */
1160 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1161 /* left is 0, right is 1, prev is 2 */
1162 int rval=0; /* return value (logging flags) */
1163 int state = 0;/* state bits, accessed thru macros */
1169 * Set up a bunch of variables to make the tests simpler.
1173 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1174 ep = xfs_iext_get_ext(ifp, idx);
1175 xfs_bmbt_get_all(ep, &PREV);
1176 newext = new->br_state;
1177 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1178 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1179 ASSERT(PREV.br_state == oldext);
1180 new_endoff = new->br_startoff + new->br_blockcount;
1181 ASSERT(PREV.br_startoff <= new->br_startoff);
1182 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1185 * Set flags determining what part of the previous oldext allocation
1186 * extent is being replaced by a newext allocation.
1188 if (PREV.br_startoff == new->br_startoff)
1189 state |= BMAP_LEFT_FILLING;
1190 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1191 state |= BMAP_RIGHT_FILLING;
1194 * Check and set flags if this segment has a left neighbor.
1195 * Don't set contiguous if the combined extent would be too large.
1198 state |= BMAP_LEFT_VALID;
1199 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
1201 if (isnullstartblock(LEFT.br_startblock))
1202 state |= BMAP_LEFT_DELAY;
1205 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1206 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1207 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1208 LEFT.br_state == newext &&
1209 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1210 state |= BMAP_LEFT_CONTIG;
1213 * Check and set flags if this segment has a right neighbor.
1214 * Don't set contiguous if the combined extent would be too large.
1215 * Also check for all-three-contiguous being too large.
1217 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1218 state |= BMAP_RIGHT_VALID;
1219 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
1220 if (isnullstartblock(RIGHT.br_startblock))
1221 state |= BMAP_RIGHT_DELAY;
1224 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1225 new_endoff == RIGHT.br_startoff &&
1226 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1227 newext == RIGHT.br_state &&
1228 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1229 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1230 BMAP_RIGHT_FILLING)) !=
1231 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1232 BMAP_RIGHT_FILLING) ||
1233 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1235 state |= BMAP_RIGHT_CONTIG;
1238 * Switch out based on the FILLING and CONTIG state bits.
1240 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1241 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1242 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1243 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1245 * Setting all of a previous oldext extent to newext.
1246 * The left and right neighbors are both contiguous with new.
1248 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1249 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1250 LEFT.br_blockcount + PREV.br_blockcount +
1251 RIGHT.br_blockcount);
1252 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1254 xfs_iext_remove(ip, idx, 2, state);
1255 ip->i_df.if_lastex = idx - 1;
1256 ip->i_d.di_nextents -= 2;
1258 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1260 rval = XFS_ILOG_CORE;
1261 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1262 RIGHT.br_startblock,
1263 RIGHT.br_blockcount, &i)))
1265 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1266 if ((error = xfs_btree_delete(cur, &i)))
1268 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1269 if ((error = xfs_btree_decrement(cur, 0, &i)))
1271 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1272 if ((error = xfs_btree_delete(cur, &i)))
1274 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1275 if ((error = xfs_btree_decrement(cur, 0, &i)))
1277 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1278 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1280 LEFT.br_blockcount + PREV.br_blockcount +
1281 RIGHT.br_blockcount, LEFT.br_state)))
1286 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1288 * Setting all of a previous oldext extent to newext.
1289 * The left neighbor is contiguous, the right is not.
1291 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1292 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1293 LEFT.br_blockcount + PREV.br_blockcount);
1294 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1296 ip->i_df.if_lastex = idx - 1;
1297 xfs_iext_remove(ip, idx, 1, state);
1298 ip->i_d.di_nextents--;
1300 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1302 rval = XFS_ILOG_CORE;
1303 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1304 PREV.br_startblock, PREV.br_blockcount,
1307 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1308 if ((error = xfs_btree_delete(cur, &i)))
1310 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1311 if ((error = xfs_btree_decrement(cur, 0, &i)))
1313 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1314 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1316 LEFT.br_blockcount + PREV.br_blockcount,
1322 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1324 * Setting all of a previous oldext extent to newext.
1325 * The right neighbor is contiguous, the left is not.
1327 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1328 xfs_bmbt_set_blockcount(ep,
1329 PREV.br_blockcount + RIGHT.br_blockcount);
1330 xfs_bmbt_set_state(ep, newext);
1331 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1332 ip->i_df.if_lastex = idx;
1333 xfs_iext_remove(ip, idx + 1, 1, state);
1334 ip->i_d.di_nextents--;
1336 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1338 rval = XFS_ILOG_CORE;
1339 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1340 RIGHT.br_startblock,
1341 RIGHT.br_blockcount, &i)))
1343 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1344 if ((error = xfs_btree_delete(cur, &i)))
1346 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1347 if ((error = xfs_btree_decrement(cur, 0, &i)))
1349 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1350 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1352 new->br_blockcount + RIGHT.br_blockcount,
1358 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1360 * Setting all of a previous oldext extent to newext.
1361 * Neither the left nor right neighbors are contiguous with
1364 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1365 xfs_bmbt_set_state(ep, newext);
1366 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1368 ip->i_df.if_lastex = idx;
1370 rval = XFS_ILOG_DEXT;
1373 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1374 new->br_startblock, new->br_blockcount,
1377 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1378 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1379 new->br_startblock, new->br_blockcount,
1385 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1387 * Setting the first part of a previous oldext extent to newext.
1388 * The left neighbor is contiguous.
1390 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1391 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1392 LEFT.br_blockcount + new->br_blockcount);
1393 xfs_bmbt_set_startoff(ep,
1394 PREV.br_startoff + new->br_blockcount);
1395 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1397 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1398 xfs_bmbt_set_startblock(ep,
1399 new->br_startblock + new->br_blockcount);
1400 xfs_bmbt_set_blockcount(ep,
1401 PREV.br_blockcount - new->br_blockcount);
1402 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1404 ip->i_df.if_lastex = idx - 1;
1406 rval = XFS_ILOG_DEXT;
1409 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1410 PREV.br_startblock, PREV.br_blockcount,
1413 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1414 if ((error = xfs_bmbt_update(cur,
1415 PREV.br_startoff + new->br_blockcount,
1416 PREV.br_startblock + new->br_blockcount,
1417 PREV.br_blockcount - new->br_blockcount,
1420 if ((error = xfs_btree_decrement(cur, 0, &i)))
1422 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1424 LEFT.br_blockcount + new->br_blockcount,
1430 case BMAP_LEFT_FILLING:
1432 * Setting the first part of a previous oldext extent to newext.
1433 * The left neighbor is not contiguous.
1435 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1436 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1437 xfs_bmbt_set_startoff(ep, new_endoff);
1438 xfs_bmbt_set_blockcount(ep,
1439 PREV.br_blockcount - new->br_blockcount);
1440 xfs_bmbt_set_startblock(ep,
1441 new->br_startblock + new->br_blockcount);
1442 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1444 xfs_iext_insert(ip, idx, 1, new, state);
1445 ip->i_df.if_lastex = idx;
1446 ip->i_d.di_nextents++;
1448 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1450 rval = XFS_ILOG_CORE;
1451 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1452 PREV.br_startblock, PREV.br_blockcount,
1455 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1456 if ((error = xfs_bmbt_update(cur,
1457 PREV.br_startoff + new->br_blockcount,
1458 PREV.br_startblock + new->br_blockcount,
1459 PREV.br_blockcount - new->br_blockcount,
1462 cur->bc_rec.b = *new;
1463 if ((error = xfs_btree_insert(cur, &i)))
1465 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1469 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1471 * Setting the last part of a previous oldext extent to newext.
1472 * The right neighbor is contiguous with the new allocation.
1474 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1475 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
1476 xfs_bmbt_set_blockcount(ep,
1477 PREV.br_blockcount - new->br_blockcount);
1478 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1479 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1480 new->br_startoff, new->br_startblock,
1481 new->br_blockcount + RIGHT.br_blockcount, newext);
1482 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
1484 ip->i_df.if_lastex = idx + 1;
1486 rval = XFS_ILOG_DEXT;
1489 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1491 PREV.br_blockcount, &i)))
1493 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1494 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1496 PREV.br_blockcount - new->br_blockcount,
1499 if ((error = xfs_btree_increment(cur, 0, &i)))
1501 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1503 new->br_blockcount + RIGHT.br_blockcount,
1509 case BMAP_RIGHT_FILLING:
1511 * Setting the last part of a previous oldext extent to newext.
1512 * The right neighbor is not contiguous.
1514 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1515 xfs_bmbt_set_blockcount(ep,
1516 PREV.br_blockcount - new->br_blockcount);
1517 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1519 xfs_iext_insert(ip, idx + 1, 1, new, state);
1520 ip->i_df.if_lastex = idx + 1;
1521 ip->i_d.di_nextents++;
1523 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1525 rval = XFS_ILOG_CORE;
1526 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1527 PREV.br_startblock, PREV.br_blockcount,
1530 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1531 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1533 PREV.br_blockcount - new->br_blockcount,
1536 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1537 new->br_startblock, new->br_blockcount,
1540 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1541 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1542 if ((error = xfs_btree_insert(cur, &i)))
1544 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1550 * Setting the middle part of a previous oldext extent to
1551 * newext. Contiguity is impossible here.
1552 * One extent becomes three extents.
1554 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1555 xfs_bmbt_set_blockcount(ep,
1556 new->br_startoff - PREV.br_startoff);
1557 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1560 r[1].br_startoff = new_endoff;
1561 r[1].br_blockcount =
1562 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1563 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1564 r[1].br_state = oldext;
1565 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
1566 ip->i_df.if_lastex = idx + 1;
1567 ip->i_d.di_nextents += 2;
1569 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1571 rval = XFS_ILOG_CORE;
1572 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1573 PREV.br_startblock, PREV.br_blockcount,
1576 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1577 /* new right extent - oldext */
1578 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1579 r[1].br_startblock, r[1].br_blockcount,
1582 /* new left extent - oldext */
1583 cur->bc_rec.b = PREV;
1584 cur->bc_rec.b.br_blockcount =
1585 new->br_startoff - PREV.br_startoff;
1586 if ((error = xfs_btree_insert(cur, &i)))
1588 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1590 * Reset the cursor to the position of the new extent
1591 * we are about to insert as we can't trust it after
1592 * the previous insert.
1594 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1595 new->br_startblock, new->br_blockcount,
1598 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1599 /* new middle extent - newext */
1600 cur->bc_rec.b.br_state = new->br_state;
1601 if ((error = xfs_btree_insert(cur, &i)))
1603 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1607 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1608 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1609 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1610 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1611 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1612 case BMAP_LEFT_CONTIG:
1613 case BMAP_RIGHT_CONTIG:
1615 * These cases are all impossible.
1629 * Called by xfs_bmap_add_extent to handle cases converting a hole
1630 * to a delayed allocation.
1633 STATIC int /* error */
1634 xfs_bmap_add_extent_hole_delay(
1635 xfs_inode_t *ip, /* incore inode pointer */
1636 xfs_extnum_t idx, /* extent number to update/insert */
1637 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1638 int *logflagsp, /* inode logging flags */
1639 int rsvd) /* OK to allocate reserved blocks */
1641 xfs_bmbt_rec_host_t *ep; /* extent record for idx */
1642 xfs_ifork_t *ifp; /* inode fork pointer */
1643 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1644 xfs_filblks_t newlen=0; /* new indirect size */
1645 xfs_filblks_t oldlen=0; /* old indirect size */
1646 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1647 int state; /* state bits, accessed thru macros */
1648 xfs_filblks_t temp=0; /* temp for indirect calculations */
1650 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1651 ep = xfs_iext_get_ext(ifp, idx);
1653 ASSERT(isnullstartblock(new->br_startblock));
1656 * Check and set flags if this segment has a left neighbor
1659 state |= BMAP_LEFT_VALID;
1660 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1662 if (isnullstartblock(left.br_startblock))
1663 state |= BMAP_LEFT_DELAY;
1667 * Check and set flags if the current (right) segment exists.
1668 * If it doesn't exist, we're converting the hole at end-of-file.
1670 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1671 state |= BMAP_RIGHT_VALID;
1672 xfs_bmbt_get_all(ep, &right);
1674 if (isnullstartblock(right.br_startblock))
1675 state |= BMAP_RIGHT_DELAY;
1679 * Set contiguity flags on the left and right neighbors.
1680 * Don't let extents get too large, even if the pieces are contiguous.
1682 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1683 left.br_startoff + left.br_blockcount == new->br_startoff &&
1684 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1685 state |= BMAP_LEFT_CONTIG;
1687 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1688 new->br_startoff + new->br_blockcount == right.br_startoff &&
1689 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1690 (!(state & BMAP_LEFT_CONTIG) ||
1691 (left.br_blockcount + new->br_blockcount +
1692 right.br_blockcount <= MAXEXTLEN)))
1693 state |= BMAP_RIGHT_CONTIG;
1696 * Switch out based on the contiguity flags.
1698 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1699 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1701 * New allocation is contiguous with delayed allocations
1702 * on the left and on the right.
1703 * Merge all three into a single extent record.
1705 temp = left.br_blockcount + new->br_blockcount +
1706 right.br_blockcount;
1708 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1709 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1710 oldlen = startblockval(left.br_startblock) +
1711 startblockval(new->br_startblock) +
1712 startblockval(right.br_startblock);
1713 newlen = xfs_bmap_worst_indlen(ip, temp);
1714 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1715 nullstartblock((int)newlen));
1716 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1718 xfs_iext_remove(ip, idx, 1, state);
1719 ip->i_df.if_lastex = idx - 1;
1722 case BMAP_LEFT_CONTIG:
1724 * New allocation is contiguous with a delayed allocation
1726 * Merge the new allocation with the left neighbor.
1728 temp = left.br_blockcount + new->br_blockcount;
1729 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1730 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1731 oldlen = startblockval(left.br_startblock) +
1732 startblockval(new->br_startblock);
1733 newlen = xfs_bmap_worst_indlen(ip, temp);
1734 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1735 nullstartblock((int)newlen));
1736 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1738 ip->i_df.if_lastex = idx - 1;
1741 case BMAP_RIGHT_CONTIG:
1743 * New allocation is contiguous with a delayed allocation
1745 * Merge the new allocation with the right neighbor.
1747 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1748 temp = new->br_blockcount + right.br_blockcount;
1749 oldlen = startblockval(new->br_startblock) +
1750 startblockval(right.br_startblock);
1751 newlen = xfs_bmap_worst_indlen(ip, temp);
1752 xfs_bmbt_set_allf(ep, new->br_startoff,
1753 nullstartblock((int)newlen), temp, right.br_state);
1754 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1756 ip->i_df.if_lastex = idx;
1761 * New allocation is not contiguous with another
1762 * delayed allocation.
1763 * Insert a new entry.
1765 oldlen = newlen = 0;
1766 xfs_iext_insert(ip, idx, 1, new, state);
1767 ip->i_df.if_lastex = idx;
1770 if (oldlen != newlen) {
1771 ASSERT(oldlen > newlen);
1772 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
1773 (int64_t)(oldlen - newlen), rsvd);
1775 * Nothing to do for disk quota accounting here.
1783 * Called by xfs_bmap_add_extent to handle cases converting a hole
1784 * to a real allocation.
1786 STATIC int /* error */
1787 xfs_bmap_add_extent_hole_real(
1788 xfs_inode_t *ip, /* incore inode pointer */
1789 xfs_extnum_t idx, /* extent number to update/insert */
1790 xfs_btree_cur_t *cur, /* if null, not a btree */
1791 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1792 int *logflagsp, /* inode logging flags */
1793 int whichfork) /* data or attr fork */
1795 xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */
1796 int error; /* error return value */
1797 int i; /* temp state */
1798 xfs_ifork_t *ifp; /* inode fork pointer */
1799 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1800 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1801 int rval=0; /* return value (logging flags) */
1802 int state; /* state bits, accessed thru macros */
1804 ifp = XFS_IFORK_PTR(ip, whichfork);
1805 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
1806 ep = xfs_iext_get_ext(ifp, idx);
1809 if (whichfork == XFS_ATTR_FORK)
1810 state |= BMAP_ATTRFORK;
1813 * Check and set flags if this segment has a left neighbor.
1816 state |= BMAP_LEFT_VALID;
1817 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1818 if (isnullstartblock(left.br_startblock))
1819 state |= BMAP_LEFT_DELAY;
1823 * Check and set flags if this segment has a current value.
1824 * Not true if we're inserting into the "hole" at eof.
1826 if (idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1827 state |= BMAP_RIGHT_VALID;
1828 xfs_bmbt_get_all(ep, &right);
1829 if (isnullstartblock(right.br_startblock))
1830 state |= BMAP_RIGHT_DELAY;
1834 * We're inserting a real allocation between "left" and "right".
1835 * Set the contiguity flags. Don't let extents get too large.
1837 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1838 left.br_startoff + left.br_blockcount == new->br_startoff &&
1839 left.br_startblock + left.br_blockcount == new->br_startblock &&
1840 left.br_state == new->br_state &&
1841 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1842 state |= BMAP_LEFT_CONTIG;
1844 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1845 new->br_startoff + new->br_blockcount == right.br_startoff &&
1846 new->br_startblock + new->br_blockcount == right.br_startblock &&
1847 new->br_state == right.br_state &&
1848 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1849 (!(state & BMAP_LEFT_CONTIG) ||
1850 left.br_blockcount + new->br_blockcount +
1851 right.br_blockcount <= MAXEXTLEN))
1852 state |= BMAP_RIGHT_CONTIG;
1856 * Select which case we're in here, and implement it.
1858 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1859 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1861 * New allocation is contiguous with real allocations on the
1862 * left and on the right.
1863 * Merge all three into a single extent record.
1865 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1866 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1867 left.br_blockcount + new->br_blockcount +
1868 right.br_blockcount);
1869 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1871 xfs_iext_remove(ip, idx, 1, state);
1872 ifp->if_lastex = idx - 1;
1873 XFS_IFORK_NEXT_SET(ip, whichfork,
1874 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
1876 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1878 rval = XFS_ILOG_CORE;
1879 if ((error = xfs_bmbt_lookup_eq(cur,
1881 right.br_startblock,
1882 right.br_blockcount, &i)))
1884 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1885 if ((error = xfs_btree_delete(cur, &i)))
1887 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1888 if ((error = xfs_btree_decrement(cur, 0, &i)))
1890 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1891 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1893 left.br_blockcount +
1894 new->br_blockcount +
1895 right.br_blockcount,
1901 case BMAP_LEFT_CONTIG:
1903 * New allocation is contiguous with a real allocation
1905 * Merge the new allocation with the left neighbor.
1907 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1908 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1909 left.br_blockcount + new->br_blockcount);
1910 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1912 ifp->if_lastex = idx - 1;
1914 rval = xfs_ilog_fext(whichfork);
1917 if ((error = xfs_bmbt_lookup_eq(cur,
1920 left.br_blockcount, &i)))
1922 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1923 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1925 left.br_blockcount +
1932 case BMAP_RIGHT_CONTIG:
1934 * New allocation is contiguous with a real allocation
1936 * Merge the new allocation with the right neighbor.
1938 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1939 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
1940 new->br_blockcount + right.br_blockcount,
1942 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1944 ifp->if_lastex = idx;
1946 rval = xfs_ilog_fext(whichfork);
1949 if ((error = xfs_bmbt_lookup_eq(cur,
1951 right.br_startblock,
1952 right.br_blockcount, &i)))
1954 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1955 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1957 new->br_blockcount +
1958 right.br_blockcount,
1966 * New allocation is not contiguous with another
1968 * Insert a new entry.
1970 xfs_iext_insert(ip, idx, 1, new, state);
1971 ifp->if_lastex = idx;
1972 XFS_IFORK_NEXT_SET(ip, whichfork,
1973 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
1975 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1977 rval = XFS_ILOG_CORE;
1978 if ((error = xfs_bmbt_lookup_eq(cur,
1981 new->br_blockcount, &i)))
1983 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1984 cur->bc_rec.b.br_state = new->br_state;
1985 if ((error = xfs_btree_insert(cur, &i)))
1987 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1997 * Adjust the size of the new extent based on di_extsize and rt extsize.
2000 xfs_bmap_extsize_align(
2002 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2003 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2004 xfs_extlen_t extsz, /* align to this extent size */
2005 int rt, /* is this a realtime inode? */
2006 int eof, /* is extent at end-of-file? */
2007 int delay, /* creating delalloc extent? */
2008 int convert, /* overwriting unwritten extent? */
2009 xfs_fileoff_t *offp, /* in/out: aligned offset */
2010 xfs_extlen_t *lenp) /* in/out: aligned length */
2012 xfs_fileoff_t orig_off; /* original offset */
2013 xfs_extlen_t orig_alen; /* original length */
2014 xfs_fileoff_t orig_end; /* original off+len */
2015 xfs_fileoff_t nexto; /* next file offset */
2016 xfs_fileoff_t prevo; /* previous file offset */
2017 xfs_fileoff_t align_off; /* temp for offset */
2018 xfs_extlen_t align_alen; /* temp for length */
2019 xfs_extlen_t temp; /* temp for calculations */
2024 orig_off = align_off = *offp;
2025 orig_alen = align_alen = *lenp;
2026 orig_end = orig_off + orig_alen;
2029 * If this request overlaps an existing extent, then don't
2030 * attempt to perform any additional alignment.
2032 if (!delay && !eof &&
2033 (orig_off >= gotp->br_startoff) &&
2034 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2039 * If the file offset is unaligned vs. the extent size
2040 * we need to align it. This will be possible unless
2041 * the file was previously written with a kernel that didn't
2042 * perform this alignment, or if a truncate shot us in the
2045 temp = do_mod(orig_off, extsz);
2051 * Same adjustment for the end of the requested area.
2053 if ((temp = (align_alen % extsz))) {
2054 align_alen += extsz - temp;
2057 * If the previous block overlaps with this proposed allocation
2058 * then move the start forward without adjusting the length.
2060 if (prevp->br_startoff != NULLFILEOFF) {
2061 if (prevp->br_startblock == HOLESTARTBLOCK)
2062 prevo = prevp->br_startoff;
2064 prevo = prevp->br_startoff + prevp->br_blockcount;
2067 if (align_off != orig_off && align_off < prevo)
2070 * If the next block overlaps with this proposed allocation
2071 * then move the start back without adjusting the length,
2072 * but not before offset 0.
2073 * This may of course make the start overlap previous block,
2074 * and if we hit the offset 0 limit then the next block
2075 * can still overlap too.
2077 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2078 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2079 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2080 nexto = gotp->br_startoff + gotp->br_blockcount;
2082 nexto = gotp->br_startoff;
2084 nexto = NULLFILEOFF;
2086 align_off + align_alen != orig_end &&
2087 align_off + align_alen > nexto)
2088 align_off = nexto > align_alen ? nexto - align_alen : 0;
2090 * If we're now overlapping the next or previous extent that
2091 * means we can't fit an extsz piece in this hole. Just move
2092 * the start forward to the first valid spot and set
2093 * the length so we hit the end.
2095 if (align_off != orig_off && align_off < prevo)
2097 if (align_off + align_alen != orig_end &&
2098 align_off + align_alen > nexto &&
2099 nexto != NULLFILEOFF) {
2100 ASSERT(nexto > prevo);
2101 align_alen = nexto - align_off;
2105 * If realtime, and the result isn't a multiple of the realtime
2106 * extent size we need to remove blocks until it is.
2108 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2110 * We're not covering the original request, or
2111 * we won't be able to once we fix the length.
2113 if (orig_off < align_off ||
2114 orig_end > align_off + align_alen ||
2115 align_alen - temp < orig_alen)
2116 return XFS_ERROR(EINVAL);
2118 * Try to fix it by moving the start up.
2120 if (align_off + temp <= orig_off) {
2125 * Try to fix it by moving the end in.
2127 else if (align_off + align_alen - temp >= orig_end)
2130 * Set the start to the minimum then trim the length.
2133 align_alen -= orig_off - align_off;
2134 align_off = orig_off;
2135 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2138 * Result doesn't cover the request, fail it.
2140 if (orig_off < align_off || orig_end > align_off + align_alen)
2141 return XFS_ERROR(EINVAL);
2143 ASSERT(orig_off >= align_off);
2144 ASSERT(orig_end <= align_off + align_alen);
2148 if (!eof && gotp->br_startoff != NULLFILEOFF)
2149 ASSERT(align_off + align_alen <= gotp->br_startoff);
2150 if (prevp->br_startoff != NULLFILEOFF)
2151 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2159 #define XFS_ALLOC_GAP_UNITS 4
2163 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2165 xfs_fsblock_t adjust; /* adjustment to block numbers */
2166 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2167 xfs_mount_t *mp; /* mount point structure */
2168 int nullfb; /* true if ap->firstblock isn't set */
2169 int rt; /* true if inode is realtime */
2171 #define ISVALID(x,y) \
2173 (x) < mp->m_sb.sb_rblocks : \
2174 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2175 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2176 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2178 mp = ap->ip->i_mount;
2179 nullfb = ap->firstblock == NULLFSBLOCK;
2180 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2181 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2183 * If allocating at eof, and there's a previous real block,
2184 * try to use its last block as our starting point.
2186 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2187 !isnullstartblock(ap->prevp->br_startblock) &&
2188 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2189 ap->prevp->br_startblock)) {
2190 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2192 * Adjust for the gap between prevp and us.
2195 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2197 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2201 * If not at eof, then compare the two neighbor blocks.
2202 * Figure out whether either one gives us a good starting point,
2203 * and pick the better one.
2205 else if (!ap->eof) {
2206 xfs_fsblock_t gotbno; /* right side block number */
2207 xfs_fsblock_t gotdiff=0; /* right side difference */
2208 xfs_fsblock_t prevbno; /* left side block number */
2209 xfs_fsblock_t prevdiff=0; /* left side difference */
2212 * If there's a previous (left) block, select a requested
2213 * start block based on it.
2215 if (ap->prevp->br_startoff != NULLFILEOFF &&
2216 !isnullstartblock(ap->prevp->br_startblock) &&
2217 (prevbno = ap->prevp->br_startblock +
2218 ap->prevp->br_blockcount) &&
2219 ISVALID(prevbno, ap->prevp->br_startblock)) {
2221 * Calculate gap to end of previous block.
2223 adjust = prevdiff = ap->off -
2224 (ap->prevp->br_startoff +
2225 ap->prevp->br_blockcount);
2227 * Figure the startblock based on the previous block's
2228 * end and the gap size.
2230 * If the gap is large relative to the piece we're
2231 * allocating, or using it gives us an invalid block
2232 * number, then just use the end of the previous block.
2234 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2235 ISVALID(prevbno + prevdiff,
2236 ap->prevp->br_startblock))
2241 * If the firstblock forbids it, can't use it,
2244 if (!rt && !nullfb &&
2245 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2246 prevbno = NULLFSBLOCK;
2249 * No previous block or can't follow it, just default.
2252 prevbno = NULLFSBLOCK;
2254 * If there's a following (right) block, select a requested
2255 * start block based on it.
2257 if (!isnullstartblock(ap->gotp->br_startblock)) {
2259 * Calculate gap to start of next block.
2261 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2263 * Figure the startblock based on the next block's
2264 * start and the gap size.
2266 gotbno = ap->gotp->br_startblock;
2269 * If the gap is large relative to the piece we're
2270 * allocating, or using it gives us an invalid block
2271 * number, then just use the start of the next block
2272 * offset by our length.
2274 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2275 ISVALID(gotbno - gotdiff, gotbno))
2277 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2279 gotdiff += adjust - ap->alen;
2283 * If the firstblock forbids it, can't use it,
2286 if (!rt && !nullfb &&
2287 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2288 gotbno = NULLFSBLOCK;
2291 * No next block, just default.
2294 gotbno = NULLFSBLOCK;
2296 * If both valid, pick the better one, else the only good
2297 * one, else ap->rval is already set (to 0 or the inode block).
2299 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2300 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2301 else if (prevbno != NULLFSBLOCK)
2303 else if (gotbno != NULLFSBLOCK)
2311 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2313 xfs_alloctype_t atype = 0; /* type for allocation routines */
2314 int error; /* error return value */
2315 xfs_mount_t *mp; /* mount point structure */
2316 xfs_extlen_t prod = 0; /* product factor for allocators */
2317 xfs_extlen_t ralen = 0; /* realtime allocation length */
2318 xfs_extlen_t align; /* minimum allocation alignment */
2321 mp = ap->ip->i_mount;
2322 align = xfs_get_extsz_hint(ap->ip);
2323 prod = align / mp->m_sb.sb_rextsize;
2324 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2325 align, 1, ap->eof, 0,
2326 ap->conv, &ap->off, &ap->alen);
2330 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2333 * If the offset & length are not perfectly aligned
2334 * then kill prod, it will just get us in trouble.
2336 if (do_mod(ap->off, align) || ap->alen % align)
2339 * Set ralen to be the actual requested length in rtextents.
2341 ralen = ap->alen / mp->m_sb.sb_rextsize;
2343 * If the old value was close enough to MAXEXTLEN that
2344 * we rounded up to it, cut it back so it's valid again.
2345 * Note that if it's a really large request (bigger than
2346 * MAXEXTLEN), we don't hear about that number, and can't
2347 * adjust the starting point to match it.
2349 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2350 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2352 * If it's an allocation to an empty file at offset 0,
2353 * pick an extent that will space things out in the rt area.
2355 if (ap->eof && ap->off == 0) {
2356 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2358 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2361 ap->rval = rtx * mp->m_sb.sb_rextsize;
2366 xfs_bmap_adjacent(ap);
2369 * Realtime allocation, done through xfs_rtallocate_extent.
2371 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2372 do_div(ap->rval, mp->m_sb.sb_rextsize);
2375 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2376 &ralen, atype, ap->wasdel, prod, &rtb)))
2378 if (rtb == NULLFSBLOCK && prod > 1 &&
2379 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2380 ap->alen, &ralen, atype,
2381 ap->wasdel, 1, &rtb)))
2384 if (ap->rval != NULLFSBLOCK) {
2385 ap->rval *= mp->m_sb.sb_rextsize;
2386 ralen *= mp->m_sb.sb_rextsize;
2388 ap->ip->i_d.di_nblocks += ralen;
2389 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2391 ap->ip->i_delayed_blks -= ralen;
2393 * Adjust the disk quota also. This was reserved
2396 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2397 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2398 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2406 xfs_bmap_btalloc_nullfb(
2407 struct xfs_bmalloca *ap,
2408 struct xfs_alloc_arg *args,
2411 struct xfs_mount *mp = ap->ip->i_mount;
2412 struct xfs_perag *pag;
2413 xfs_agnumber_t ag, startag;
2417 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2418 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2420 args->type = XFS_ALLOCTYPE_START_BNO;
2421 args->total = ap->total;
2424 * Search for an allocation group with a single extent large enough
2425 * for the request. If one isn't found, then adjust the minimum
2426 * allocation size to the largest space found.
2428 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
2429 if (startag == NULLAGNUMBER)
2432 pag = xfs_perag_get(mp, ag);
2433 while (*blen < args->maxlen) {
2434 if (!pag->pagf_init) {
2435 error = xfs_alloc_pagf_init(mp, args->tp, ag,
2436 XFS_ALLOC_FLAG_TRYLOCK);
2444 * See xfs_alloc_fix_freelist...
2446 if (pag->pagf_init) {
2447 xfs_extlen_t longest;
2448 longest = xfs_alloc_longest_free_extent(mp, pag);
2449 if (*blen < longest)
2454 if (xfs_inode_is_filestream(ap->ip)) {
2455 if (*blen >= args->maxlen)
2460 * If startag is an invalid AG, we've
2461 * come here once before and
2462 * xfs_filestream_new_ag picked the
2463 * best currently available.
2465 * Don't continue looping, since we
2466 * could loop forever.
2468 if (startag == NULLAGNUMBER)
2471 error = xfs_filestream_new_ag(ap, &ag);
2476 /* loop again to set 'blen'*/
2477 startag = NULLAGNUMBER;
2478 pag = xfs_perag_get(mp, ag);
2482 if (++ag == mp->m_sb.sb_agcount)
2487 pag = xfs_perag_get(mp, ag);
2492 * Since the above loop did a BUF_TRYLOCK, it is
2493 * possible that there is space for this request.
2495 if (notinit || *blen < ap->minlen)
2496 args->minlen = ap->minlen;
2498 * If the best seen length is less than the request
2499 * length, use the best as the minimum.
2501 else if (*blen < args->maxlen)
2502 args->minlen = *blen;
2504 * Otherwise we've seen an extent as big as maxlen,
2505 * use that as the minimum.
2508 args->minlen = args->maxlen;
2511 * set the failure fallback case to look in the selected
2512 * AG as the stream may have moved.
2514 if (xfs_inode_is_filestream(ap->ip))
2515 ap->rval = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2522 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2524 xfs_mount_t *mp; /* mount point structure */
2525 xfs_alloctype_t atype = 0; /* type for allocation routines */
2526 xfs_extlen_t align; /* minimum allocation alignment */
2527 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2529 xfs_alloc_arg_t args;
2531 xfs_extlen_t nextminlen = 0;
2532 int nullfb; /* true if ap->firstblock isn't set */
2537 mp = ap->ip->i_mount;
2538 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
2539 if (unlikely(align)) {
2540 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2541 align, 0, ap->eof, 0, ap->conv,
2542 &ap->off, &ap->alen);
2546 nullfb = ap->firstblock == NULLFSBLOCK;
2547 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2549 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2550 ag = xfs_filestream_lookup_ag(ap->ip);
2551 ag = (ag != NULLAGNUMBER) ? ag : 0;
2552 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2554 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2557 ap->rval = ap->firstblock;
2559 xfs_bmap_adjacent(ap);
2562 * If allowed, use ap->rval; otherwise must use firstblock since
2563 * it's in the right allocation group.
2565 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2568 ap->rval = ap->firstblock;
2570 * Normal allocation, done through xfs_alloc_vextent.
2572 tryagain = isaligned = 0;
2575 args.fsbno = ap->rval;
2577 /* Trim the allocation back to the maximum an AG can fit. */
2578 args.maxlen = MIN(ap->alen, XFS_ALLOC_AG_MAX_USABLE(mp));
2579 args.firstblock = ap->firstblock;
2582 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
2585 } else if (ap->low) {
2586 if (xfs_inode_is_filestream(ap->ip))
2587 args.type = XFS_ALLOCTYPE_FIRST_AG;
2589 args.type = XFS_ALLOCTYPE_START_BNO;
2590 args.total = args.minlen = ap->minlen;
2592 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2593 args.total = ap->total;
2594 args.minlen = ap->minlen;
2596 /* apply extent size hints if obtained earlier */
2597 if (unlikely(align)) {
2599 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2600 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2601 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
2605 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
2606 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2607 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2610 * If we are not low on available data blocks, and the
2611 * underlying logical volume manager is a stripe, and
2612 * the file offset is zero then try to allocate data
2613 * blocks on stripe unit boundary.
2614 * NOTE: ap->aeof is only set if the allocation length
2615 * is >= the stripe unit and the allocation offset is
2616 * at the end of file.
2618 if (!ap->low && ap->aeof) {
2620 args.alignment = mp->m_dalign;
2624 * Adjust for alignment
2626 if (blen > args.alignment && blen <= args.maxlen)
2627 args.minlen = blen - args.alignment;
2628 args.minalignslop = 0;
2631 * First try an exact bno allocation.
2632 * If it fails then do a near or start bno
2633 * allocation with alignment turned on.
2637 args.type = XFS_ALLOCTYPE_THIS_BNO;
2640 * Compute the minlen+alignment for the
2641 * next case. Set slop so that the value
2642 * of minlen+alignment+slop doesn't go up
2643 * between the calls.
2645 if (blen > mp->m_dalign && blen <= args.maxlen)
2646 nextminlen = blen - mp->m_dalign;
2648 nextminlen = args.minlen;
2649 if (nextminlen + mp->m_dalign > args.minlen + 1)
2651 nextminlen + mp->m_dalign -
2654 args.minalignslop = 0;
2658 args.minalignslop = 0;
2660 args.minleft = ap->minleft;
2661 args.wasdel = ap->wasdel;
2663 args.userdata = ap->userdata;
2664 if ((error = xfs_alloc_vextent(&args)))
2666 if (tryagain && args.fsbno == NULLFSBLOCK) {
2668 * Exact allocation failed. Now try with alignment
2672 args.fsbno = ap->rval;
2673 args.alignment = mp->m_dalign;
2674 args.minlen = nextminlen;
2675 args.minalignslop = 0;
2677 if ((error = xfs_alloc_vextent(&args)))
2680 if (isaligned && args.fsbno == NULLFSBLOCK) {
2682 * allocation failed, so turn off alignment and
2686 args.fsbno = ap->rval;
2688 if ((error = xfs_alloc_vextent(&args)))
2691 if (args.fsbno == NULLFSBLOCK && nullfb &&
2692 args.minlen > ap->minlen) {
2693 args.minlen = ap->minlen;
2694 args.type = XFS_ALLOCTYPE_START_BNO;
2695 args.fsbno = ap->rval;
2696 if ((error = xfs_alloc_vextent(&args)))
2699 if (args.fsbno == NULLFSBLOCK && nullfb) {
2701 args.type = XFS_ALLOCTYPE_FIRST_AG;
2702 args.total = ap->minlen;
2704 if ((error = xfs_alloc_vextent(&args)))
2708 if (args.fsbno != NULLFSBLOCK) {
2709 ap->firstblock = ap->rval = args.fsbno;
2710 ASSERT(nullfb || fb_agno == args.agno ||
2711 (ap->low && fb_agno < args.agno));
2712 ap->alen = args.len;
2713 ap->ip->i_d.di_nblocks += args.len;
2714 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2716 ap->ip->i_delayed_blks -= args.len;
2718 * Adjust the disk quota also. This was reserved
2721 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2722 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2723 XFS_TRANS_DQ_BCOUNT,
2726 ap->rval = NULLFSBLOCK;
2733 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2734 * It figures out where to ask the underlying allocator to put the new extent.
2738 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2740 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
2741 return xfs_bmap_rtalloc(ap);
2742 return xfs_bmap_btalloc(ap);
2746 * Transform a btree format file with only one leaf node, where the
2747 * extents list will fit in the inode, into an extents format file.
2748 * Since the file extents are already in-core, all we have to do is
2749 * give up the space for the btree root and pitch the leaf block.
2751 STATIC int /* error */
2752 xfs_bmap_btree_to_extents(
2753 xfs_trans_t *tp, /* transaction pointer */
2754 xfs_inode_t *ip, /* incore inode pointer */
2755 xfs_btree_cur_t *cur, /* btree cursor */
2756 int *logflagsp, /* inode logging flags */
2757 int whichfork) /* data or attr fork */
2760 struct xfs_btree_block *cblock;/* child btree block */
2761 xfs_fsblock_t cbno; /* child block number */
2762 xfs_buf_t *cbp; /* child block's buffer */
2763 int error; /* error return value */
2764 xfs_ifork_t *ifp; /* inode fork data */
2765 xfs_mount_t *mp; /* mount point structure */
2766 __be64 *pp; /* ptr to block address */
2767 struct xfs_btree_block *rblock;/* root btree block */
2770 ifp = XFS_IFORK_PTR(ip, whichfork);
2771 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2772 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2773 rblock = ifp->if_broot;
2774 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2775 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
2776 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2777 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
2778 cbno = be64_to_cpu(*pp);
2781 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
2784 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2785 XFS_BMAP_BTREE_REF)))
2787 cblock = XFS_BUF_TO_BLOCK(cbp);
2788 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
2790 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2791 ip->i_d.di_nblocks--;
2792 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
2793 xfs_trans_binval(tp, cbp);
2794 if (cur->bc_bufs[0] == cbp)
2795 cur->bc_bufs[0] = NULL;
2796 xfs_iroot_realloc(ip, -1, whichfork);
2797 ASSERT(ifp->if_broot == NULL);
2798 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2799 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
2800 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2805 * Called by xfs_bmapi to update file extent records and the btree
2806 * after removing space (or undoing a delayed allocation).
2808 STATIC int /* error */
2809 xfs_bmap_del_extent(
2810 xfs_inode_t *ip, /* incore inode pointer */
2811 xfs_trans_t *tp, /* current transaction pointer */
2812 xfs_extnum_t idx, /* extent number to update/delete */
2813 xfs_bmap_free_t *flist, /* list of extents to be freed */
2814 xfs_btree_cur_t *cur, /* if null, not a btree */
2815 xfs_bmbt_irec_t *del, /* data to remove from extents */
2816 int *logflagsp, /* inode logging flags */
2817 int whichfork, /* data or attr fork */
2818 int rsvd) /* OK to allocate reserved blocks */
2820 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2821 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2822 xfs_fsblock_t del_endblock=0; /* first block past del */
2823 xfs_fileoff_t del_endoff; /* first offset past del */
2824 int delay; /* current block is delayed allocated */
2825 int do_fx; /* free extent at end of routine */
2826 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
2827 int error; /* error return value */
2828 int flags; /* inode logging flags */
2829 xfs_bmbt_irec_t got; /* current extent entry */
2830 xfs_fileoff_t got_endoff; /* first offset past got */
2831 int i; /* temp state */
2832 xfs_ifork_t *ifp; /* inode fork pointer */
2833 xfs_mount_t *mp; /* mount structure */
2834 xfs_filblks_t nblks; /* quota/sb block count */
2835 xfs_bmbt_irec_t new; /* new record to be inserted */
2837 uint qfield; /* quota field to update */
2838 xfs_filblks_t temp; /* for indirect length calculations */
2839 xfs_filblks_t temp2; /* for indirect length calculations */
2842 XFS_STATS_INC(xs_del_exlist);
2844 if (whichfork == XFS_ATTR_FORK)
2845 state |= BMAP_ATTRFORK;
2848 ifp = XFS_IFORK_PTR(ip, whichfork);
2849 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
2850 (uint)sizeof(xfs_bmbt_rec_t)));
2851 ASSERT(del->br_blockcount > 0);
2852 ep = xfs_iext_get_ext(ifp, idx);
2853 xfs_bmbt_get_all(ep, &got);
2854 ASSERT(got.br_startoff <= del->br_startoff);
2855 del_endoff = del->br_startoff + del->br_blockcount;
2856 got_endoff = got.br_startoff + got.br_blockcount;
2857 ASSERT(got_endoff >= del_endoff);
2858 delay = isnullstartblock(got.br_startblock);
2859 ASSERT(isnullstartblock(del->br_startblock) == delay);
2864 * If deleting a real allocation, must free up the disk space.
2867 flags = XFS_ILOG_CORE;
2869 * Realtime allocation. Free it and record di_nblocks update.
2871 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
2875 ASSERT(do_mod(del->br_blockcount,
2876 mp->m_sb.sb_rextsize) == 0);
2877 ASSERT(do_mod(del->br_startblock,
2878 mp->m_sb.sb_rextsize) == 0);
2879 bno = del->br_startblock;
2880 len = del->br_blockcount;
2881 do_div(bno, mp->m_sb.sb_rextsize);
2882 do_div(len, mp->m_sb.sb_rextsize);
2883 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
2884 (xfs_extlen_t)len)))
2887 nblks = len * mp->m_sb.sb_rextsize;
2888 qfield = XFS_TRANS_DQ_RTBCOUNT;
2891 * Ordinary allocation.
2895 nblks = del->br_blockcount;
2896 qfield = XFS_TRANS_DQ_BCOUNT;
2899 * Set up del_endblock and cur for later.
2901 del_endblock = del->br_startblock + del->br_blockcount;
2903 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2904 got.br_startblock, got.br_blockcount,
2907 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2909 da_old = da_new = 0;
2911 da_old = startblockval(got.br_startblock);
2917 * Set flag value to use in switch statement.
2918 * Left-contig is 2, right-contig is 1.
2920 switch (((got.br_startoff == del->br_startoff) << 1) |
2921 (got_endoff == del_endoff)) {
2924 * Matches the whole extent. Delete the entry.
2926 xfs_iext_remove(ip, idx, 1,
2927 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
2928 ifp->if_lastex = idx;
2931 XFS_IFORK_NEXT_SET(ip, whichfork,
2932 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2933 flags |= XFS_ILOG_CORE;
2935 flags |= xfs_ilog_fext(whichfork);
2938 if ((error = xfs_btree_delete(cur, &i)))
2940 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2945 * Deleting the first part of the extent.
2947 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
2948 xfs_bmbt_set_startoff(ep, del_endoff);
2949 temp = got.br_blockcount - del->br_blockcount;
2950 xfs_bmbt_set_blockcount(ep, temp);
2951 ifp->if_lastex = idx;
2953 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2955 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2956 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
2960 xfs_bmbt_set_startblock(ep, del_endblock);
2961 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
2963 flags |= xfs_ilog_fext(whichfork);
2966 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2967 got.br_blockcount - del->br_blockcount,
2974 * Deleting the last part of the extent.
2976 temp = got.br_blockcount - del->br_blockcount;
2977 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
2978 xfs_bmbt_set_blockcount(ep, temp);
2979 ifp->if_lastex = idx;
2981 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2983 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2984 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
2988 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
2990 flags |= xfs_ilog_fext(whichfork);
2993 if ((error = xfs_bmbt_update(cur, got.br_startoff,
2995 got.br_blockcount - del->br_blockcount,
3002 * Deleting the middle of the extent.
3004 temp = del->br_startoff - got.br_startoff;
3005 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
3006 xfs_bmbt_set_blockcount(ep, temp);
3007 new.br_startoff = del_endoff;
3008 temp2 = got_endoff - del_endoff;
3009 new.br_blockcount = temp2;
3010 new.br_state = got.br_state;
3012 new.br_startblock = del_endblock;
3013 flags |= XFS_ILOG_CORE;
3015 if ((error = xfs_bmbt_update(cur,
3017 got.br_startblock, temp,
3020 if ((error = xfs_btree_increment(cur, 0, &i)))
3022 cur->bc_rec.b = new;
3023 error = xfs_btree_insert(cur, &i);
3024 if (error && error != ENOSPC)
3027 * If get no-space back from btree insert,
3028 * it tried a split, and we have a zero
3029 * block reservation.
3030 * Fix up our state and return the error.
3032 if (error == ENOSPC) {
3034 * Reset the cursor, don't trust
3035 * it after any insert operation.
3037 if ((error = xfs_bmbt_lookup_eq(cur,
3042 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3044 * Update the btree record back
3045 * to the original value.
3047 if ((error = xfs_bmbt_update(cur,
3054 * Reset the extent record back
3055 * to the original value.
3057 xfs_bmbt_set_blockcount(ep,
3060 error = XFS_ERROR(ENOSPC);
3063 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3065 flags |= xfs_ilog_fext(whichfork);
3066 XFS_IFORK_NEXT_SET(ip, whichfork,
3067 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3069 ASSERT(whichfork == XFS_DATA_FORK);
3070 temp = xfs_bmap_worst_indlen(ip, temp);
3071 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
3072 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3073 new.br_startblock = nullstartblock((int)temp2);
3074 da_new = temp + temp2;
3075 while (da_new > da_old) {
3079 xfs_bmbt_set_startblock(ep,
3080 nullstartblock((int)temp));
3082 if (da_new == da_old)
3088 nullstartblock((int)temp2);
3092 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
3093 xfs_iext_insert(ip, idx + 1, 1, &new, state);
3094 ifp->if_lastex = idx + 1;
3098 * If we need to, add to list of extents to delete.
3101 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3104 * Adjust inode # blocks in the file.
3107 ip->i_d.di_nblocks -= nblks;
3109 * Adjust quota data.
3112 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
3115 * Account for change in delayed indirect blocks.
3116 * Nothing to do for disk quota accounting here.
3118 ASSERT(da_old >= da_new);
3119 if (da_old > da_new) {
3120 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
3121 (int64_t)(da_old - da_new), rsvd);
3129 * Remove the entry "free" from the free item list. Prev points to the
3130 * previous entry, unless "free" is the head of the list.
3134 xfs_bmap_free_t *flist, /* free item list header */
3135 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3136 xfs_bmap_free_item_t *free) /* list item to be freed */
3139 prev->xbfi_next = free->xbfi_next;
3141 flist->xbf_first = free->xbfi_next;
3143 kmem_zone_free(xfs_bmap_free_item_zone, free);
3147 * Convert an extents-format file into a btree-format file.
3148 * The new file will have a root block (in the inode) and a single child block.
3150 STATIC int /* error */
3151 xfs_bmap_extents_to_btree(
3152 xfs_trans_t *tp, /* transaction pointer */
3153 xfs_inode_t *ip, /* incore inode pointer */
3154 xfs_fsblock_t *firstblock, /* first-block-allocated */
3155 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3156 xfs_btree_cur_t **curp, /* cursor returned to caller */
3157 int wasdel, /* converting a delayed alloc */
3158 int *logflagsp, /* inode logging flags */
3159 int whichfork) /* data or attr fork */
3161 struct xfs_btree_block *ablock; /* allocated (child) bt block */
3162 xfs_buf_t *abp; /* buffer for ablock */
3163 xfs_alloc_arg_t args; /* allocation arguments */
3164 xfs_bmbt_rec_t *arp; /* child record pointer */
3165 struct xfs_btree_block *block; /* btree root block */
3166 xfs_btree_cur_t *cur; /* bmap btree cursor */
3167 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3168 int error; /* error return value */
3169 xfs_extnum_t i, cnt; /* extent record index */
3170 xfs_ifork_t *ifp; /* inode fork pointer */
3171 xfs_bmbt_key_t *kp; /* root block key pointer */
3172 xfs_mount_t *mp; /* mount structure */
3173 xfs_extnum_t nextents; /* number of file extents */
3174 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3176 ifp = XFS_IFORK_PTR(ip, whichfork);
3177 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3178 ASSERT(ifp->if_ext_max ==
3179 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3181 * Make space in the inode incore.
3183 xfs_iroot_realloc(ip, 1, whichfork);
3184 ifp->if_flags |= XFS_IFBROOT;
3189 block = ifp->if_broot;
3190 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3191 block->bb_level = cpu_to_be16(1);
3192 block->bb_numrecs = cpu_to_be16(1);
3193 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3194 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3197 * Need a cursor. Can't allocate until bb_level is filled in.
3200 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
3201 cur->bc_private.b.firstblock = *firstblock;
3202 cur->bc_private.b.flist = flist;
3203 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3205 * Convert to a btree with two levels, one record in root.
3207 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3210 args.firstblock = *firstblock;
3211 if (*firstblock == NULLFSBLOCK) {
3212 args.type = XFS_ALLOCTYPE_START_BNO;
3213 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3214 } else if (flist->xbf_low) {
3215 args.type = XFS_ALLOCTYPE_START_BNO;
3216 args.fsbno = *firstblock;
3218 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3219 args.fsbno = *firstblock;
3221 args.minlen = args.maxlen = args.prod = 1;
3222 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3223 args.minalignslop = 0;
3224 args.wasdel = wasdel;
3226 if ((error = xfs_alloc_vextent(&args))) {
3227 xfs_iroot_realloc(ip, -1, whichfork);
3228 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3232 * Allocation can't fail, the space was reserved.
3234 ASSERT(args.fsbno != NULLFSBLOCK);
3235 ASSERT(*firstblock == NULLFSBLOCK ||
3236 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3238 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3239 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3240 cur->bc_private.b.allocated++;
3241 ip->i_d.di_nblocks++;
3242 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3243 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3245 * Fill in the child block.
3247 ablock = XFS_BUF_TO_BLOCK(abp);
3248 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3249 ablock->bb_level = 0;
3250 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3251 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3252 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3253 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3254 for (cnt = i = 0; i < nextents; i++) {
3255 ep = xfs_iext_get_ext(ifp, i);
3256 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
3257 arp->l0 = cpu_to_be64(ep->l0);
3258 arp->l1 = cpu_to_be64(ep->l1);
3262 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3263 xfs_btree_set_numrecs(ablock, cnt);
3266 * Fill in the root key and pointer.
3268 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3269 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3270 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
3271 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3272 be16_to_cpu(block->bb_level)));
3273 *pp = cpu_to_be64(args.fsbno);
3276 * Do all this logging at the end so that
3277 * the root is at the right level.
3279 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3280 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3281 ASSERT(*curp == NULL);
3283 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
3288 * Calculate the default attribute fork offset for newly created inodes.
3291 xfs_default_attroffset(
3292 struct xfs_inode *ip)
3294 struct xfs_mount *mp = ip->i_mount;
3297 if (mp->m_sb.sb_inodesize == 256) {
3298 offset = XFS_LITINO(mp) -
3299 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3301 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3304 ASSERT(offset < XFS_LITINO(mp));
3309 * Helper routine to reset inode di_forkoff field when switching
3310 * attribute fork from local to extent format - we reset it where
3311 * possible to make space available for inline data fork extents.
3314 xfs_bmap_forkoff_reset(
3319 if (whichfork == XFS_ATTR_FORK &&
3320 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3321 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3322 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3323 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3325 if (dfl_forkoff > ip->i_d.di_forkoff) {
3326 ip->i_d.di_forkoff = dfl_forkoff;
3327 ip->i_df.if_ext_max =
3328 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3329 ip->i_afp->if_ext_max =
3330 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3336 * Convert a local file to an extents file.
3337 * This code is out of bounds for data forks of regular files,
3338 * since the file data needs to get logged so things will stay consistent.
3339 * (The bmap-level manipulations are ok, though).
3341 STATIC int /* error */
3342 xfs_bmap_local_to_extents(
3343 xfs_trans_t *tp, /* transaction pointer */
3344 xfs_inode_t *ip, /* incore inode pointer */
3345 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3346 xfs_extlen_t total, /* total blocks needed by transaction */
3347 int *logflagsp, /* inode logging flags */
3348 int whichfork) /* data or attr fork */
3350 int error; /* error return value */
3351 int flags; /* logging flags returned */
3352 xfs_ifork_t *ifp; /* inode fork pointer */
3355 * We don't want to deal with the case of keeping inode data inline yet.
3356 * So sending the data fork of a regular inode is invalid.
3358 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3359 whichfork == XFS_DATA_FORK));
3360 ifp = XFS_IFORK_PTR(ip, whichfork);
3361 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3364 if (ifp->if_bytes) {
3365 xfs_alloc_arg_t args; /* allocation arguments */
3366 xfs_buf_t *bp; /* buffer for extent block */
3367 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
3370 args.mp = ip->i_mount;
3371 args.firstblock = *firstblock;
3372 ASSERT((ifp->if_flags &
3373 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
3375 * Allocate a block. We know we need only one, since the
3376 * file currently fits in an inode.
3378 if (*firstblock == NULLFSBLOCK) {
3379 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3380 args.type = XFS_ALLOCTYPE_START_BNO;
3382 args.fsbno = *firstblock;
3383 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3386 args.mod = args.minleft = args.alignment = args.wasdel =
3387 args.isfl = args.minalignslop = 0;
3388 args.minlen = args.maxlen = args.prod = 1;
3389 if ((error = xfs_alloc_vextent(&args)))
3392 * Can't fail, the space was reserved.
3394 ASSERT(args.fsbno != NULLFSBLOCK);
3395 ASSERT(args.len == 1);
3396 *firstblock = args.fsbno;
3397 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3398 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3400 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3401 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3402 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3403 xfs_iext_add(ifp, 0, 1);
3404 ep = xfs_iext_get_ext(ifp, 0);
3405 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3406 trace_xfs_bmap_post_update(ip, 0,
3407 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3409 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3410 ip->i_d.di_nblocks = 1;
3411 xfs_trans_mod_dquot_byino(tp, ip,
3412 XFS_TRANS_DQ_BCOUNT, 1L);
3413 flags |= xfs_ilog_fext(whichfork);
3415 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3416 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3418 ifp->if_flags &= ~XFS_IFINLINE;
3419 ifp->if_flags |= XFS_IFEXTENTS;
3420 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3421 flags |= XFS_ILOG_CORE;
3428 * Search the extent records for the entry containing block bno.
3429 * If bno lies in a hole, point to the next entry. If bno lies
3430 * past eof, *eofp will be set, and *prevp will contain the last
3431 * entry (null if none). Else, *lastxp will be set to the index
3432 * of the found entry; *gotp will contain the entry.
3434 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3435 xfs_bmap_search_multi_extents(
3436 xfs_ifork_t *ifp, /* inode fork pointer */
3437 xfs_fileoff_t bno, /* block number searched for */
3438 int *eofp, /* out: end of file found */
3439 xfs_extnum_t *lastxp, /* out: last extent index */
3440 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3441 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3443 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3444 xfs_extnum_t lastx; /* last extent index */
3447 * Initialize the extent entry structure to catch access to
3448 * uninitialized br_startblock field.
3450 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3451 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3452 gotp->br_state = XFS_EXT_INVALID;
3454 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3456 gotp->br_startblock = 0xffffa5a5;
3458 prevp->br_startoff = NULLFILEOFF;
3460 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3462 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3464 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3465 xfs_bmbt_get_all(ep, gotp);
3479 * Search the extents list for the inode, for the extent containing bno.
3480 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3481 * *eofp will be set, and *prevp will contain the last entry (null if none).
3482 * Else, *lastxp will be set to the index of the found
3483 * entry; *gotp will contain the entry.
3485 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3486 xfs_bmap_search_extents(
3487 xfs_inode_t *ip, /* incore inode pointer */
3488 xfs_fileoff_t bno, /* block number searched for */
3489 int fork, /* data or attr fork */
3490 int *eofp, /* out: end of file found */
3491 xfs_extnum_t *lastxp, /* out: last extent index */
3492 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3493 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3495 xfs_ifork_t *ifp; /* inode fork pointer */
3496 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3498 XFS_STATS_INC(xs_look_exlist);
3499 ifp = XFS_IFORK_PTR(ip, fork);
3501 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3503 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3504 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3505 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3506 "Access to block zero in inode %llu "
3507 "start_block: %llx start_off: %llx "
3508 "blkcnt: %llx extent-state: %x lastx: %x\n",
3509 (unsigned long long)ip->i_ino,
3510 (unsigned long long)gotp->br_startblock,
3511 (unsigned long long)gotp->br_startoff,
3512 (unsigned long long)gotp->br_blockcount,
3513 gotp->br_state, *lastxp);
3514 *lastxp = NULLEXTNUM;
3522 * Compute the worst-case number of indirect blocks that will be used
3523 * for ip's delayed extent of length "len".
3525 STATIC xfs_filblks_t
3526 xfs_bmap_worst_indlen(
3527 xfs_inode_t *ip, /* incore inode pointer */
3528 xfs_filblks_t len) /* delayed extent length */
3530 int level; /* btree level number */
3531 int maxrecs; /* maximum record count at this level */
3532 xfs_mount_t *mp; /* mount structure */
3533 xfs_filblks_t rval; /* return value */
3536 maxrecs = mp->m_bmap_dmxr[0];
3537 for (level = 0, rval = 0;
3538 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3541 do_div(len, maxrecs);
3544 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3547 maxrecs = mp->m_bmap_dmxr[1];
3553 * Convert inode from non-attributed to attributed.
3554 * Must not be in a transaction, ip must not be locked.
3556 int /* error code */
3557 xfs_bmap_add_attrfork(
3558 xfs_inode_t *ip, /* incore inode pointer */
3559 int size, /* space new attribute needs */
3560 int rsvd) /* xact may use reserved blks */
3562 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
3563 xfs_bmap_free_t flist; /* freed extent records */
3564 xfs_mount_t *mp; /* mount structure */
3565 xfs_trans_t *tp; /* transaction pointer */
3566 int blks; /* space reservation */
3567 int version = 1; /* superblock attr version */
3568 int committed; /* xaction was committed */
3569 int logflags; /* logging flags */
3570 int error; /* error return value */
3572 ASSERT(XFS_IFORK_Q(ip) == 0);
3573 ASSERT(ip->i_df.if_ext_max ==
3574 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3577 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3578 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3579 blks = XFS_ADDAFORK_SPACE_RES(mp);
3581 tp->t_flags |= XFS_TRANS_RESERVE;
3582 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3583 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3585 xfs_ilock(ip, XFS_ILOCK_EXCL);
3586 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
3587 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3588 XFS_QMOPT_RES_REGBLKS);
3590 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3591 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3594 if (XFS_IFORK_Q(ip))
3596 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3598 * For inodes coming from pre-6.2 filesystems.
3600 ASSERT(ip->i_d.di_aformat == 0);
3601 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3603 ASSERT(ip->i_d.di_anextents == 0);
3605 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
3606 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3608 switch (ip->i_d.di_format) {
3609 case XFS_DINODE_FMT_DEV:
3610 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3612 case XFS_DINODE_FMT_UUID:
3613 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3615 case XFS_DINODE_FMT_LOCAL:
3616 case XFS_DINODE_FMT_EXTENTS:
3617 case XFS_DINODE_FMT_BTREE:
3618 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3619 if (!ip->i_d.di_forkoff)
3620 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
3621 else if (mp->m_flags & XFS_MOUNT_ATTR2)
3626 error = XFS_ERROR(EINVAL);
3629 ip->i_df.if_ext_max =
3630 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3631 ASSERT(ip->i_afp == NULL);
3632 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3633 ip->i_afp->if_ext_max =
3634 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3635 ip->i_afp->if_flags = XFS_IFEXTENTS;
3637 xfs_bmap_init(&flist, &firstblock);
3638 switch (ip->i_d.di_format) {
3639 case XFS_DINODE_FMT_LOCAL:
3640 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3643 case XFS_DINODE_FMT_EXTENTS:
3644 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3647 case XFS_DINODE_FMT_BTREE:
3648 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3656 xfs_trans_log_inode(tp, ip, logflags);
3659 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3660 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
3661 __int64_t sbfields = 0;
3663 spin_lock(&mp->m_sb_lock);
3664 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3665 xfs_sb_version_addattr(&mp->m_sb);
3666 sbfields |= XFS_SB_VERSIONNUM;
3668 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3669 xfs_sb_version_addattr2(&mp->m_sb);
3670 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
3673 spin_unlock(&mp->m_sb_lock);
3674 xfs_mod_sb(tp, sbfields);
3676 spin_unlock(&mp->m_sb_lock);
3678 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
3680 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3681 ASSERT(ip->i_df.if_ext_max ==
3682 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3685 xfs_bmap_cancel(&flist);
3687 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3689 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3690 ASSERT(ip->i_df.if_ext_max ==
3691 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3696 * Add the extent to the list of extents to be free at transaction end.
3697 * The list is maintained sorted (by block number).
3702 xfs_fsblock_t bno, /* fs block number of extent */
3703 xfs_filblks_t len, /* length of extent */
3704 xfs_bmap_free_t *flist, /* list of extents */
3705 xfs_mount_t *mp) /* mount point structure */
3707 xfs_bmap_free_item_t *cur; /* current (next) element */
3708 xfs_bmap_free_item_t *new; /* new element */
3709 xfs_bmap_free_item_t *prev; /* previous element */
3711 xfs_agnumber_t agno;
3712 xfs_agblock_t agbno;
3714 ASSERT(bno != NULLFSBLOCK);
3716 ASSERT(len <= MAXEXTLEN);
3717 ASSERT(!isnullstartblock(bno));
3718 agno = XFS_FSB_TO_AGNO(mp, bno);
3719 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3720 ASSERT(agno < mp->m_sb.sb_agcount);
3721 ASSERT(agbno < mp->m_sb.sb_agblocks);
3722 ASSERT(len < mp->m_sb.sb_agblocks);
3723 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3725 ASSERT(xfs_bmap_free_item_zone != NULL);
3726 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3727 new->xbfi_startblock = bno;
3728 new->xbfi_blockcount = (xfs_extlen_t)len;
3729 for (prev = NULL, cur = flist->xbf_first;
3731 prev = cur, cur = cur->xbfi_next) {
3732 if (cur->xbfi_startblock >= bno)
3736 prev->xbfi_next = new;
3738 flist->xbf_first = new;
3739 new->xbfi_next = cur;
3744 * Compute and fill in the value of the maximum depth of a bmap btree
3745 * in this filesystem. Done once, during mount.
3748 xfs_bmap_compute_maxlevels(
3749 xfs_mount_t *mp, /* file system mount structure */
3750 int whichfork) /* data or attr fork */
3752 int level; /* btree level */
3753 uint maxblocks; /* max blocks at this level */
3754 uint maxleafents; /* max leaf entries possible */
3755 int maxrootrecs; /* max records in root block */
3756 int minleafrecs; /* min records in leaf block */
3757 int minnoderecs; /* min records in node block */
3758 int sz; /* root block size */
3761 * The maximum number of extents in a file, hence the maximum
3762 * number of leaf entries, is controlled by the type of di_nextents
3763 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3764 * (a signed 16-bit number, xfs_aextnum_t).
3766 * Note that we can no longer assume that if we are in ATTR1 that
3767 * the fork offset of all the inodes will be
3768 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3769 * with ATTR2 and then mounted back with ATTR1, keeping the
3770 * di_forkoff's fixed but probably at various positions. Therefore,
3771 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3772 * of a minimum size available.
3774 if (whichfork == XFS_DATA_FORK) {
3775 maxleafents = MAXEXTNUM;
3776 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
3778 maxleafents = MAXAEXTNUM;
3779 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
3781 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
3782 minleafrecs = mp->m_bmap_dmnr[0];
3783 minnoderecs = mp->m_bmap_dmnr[1];
3784 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3785 for (level = 1; maxblocks > 1; level++) {
3786 if (maxblocks <= maxrootrecs)
3789 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3791 mp->m_bm_maxlevels[whichfork] = level;
3795 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3796 * caller. Frees all the extents that need freeing, which must be done
3797 * last due to locking considerations. We never free any extents in
3798 * the first transaction. This is to allow the caller to make the first
3799 * transaction a synchronous one so that the pointers to the data being
3800 * broken in this transaction will be permanent before the data is actually
3801 * freed. This is necessary to prevent blocks from being reallocated
3802 * and written to before the free and reallocation are actually permanent.
3803 * We do not just make the first transaction synchronous here, because
3804 * there are more efficient ways to gain the same protection in some cases
3805 * (see the file truncation code).
3807 * Return 1 if the given transaction was committed and a new one
3808 * started, and 0 otherwise in the committed parameter.
3813 xfs_trans_t **tp, /* transaction pointer addr */
3814 xfs_bmap_free_t *flist, /* i/o: list extents to free */
3815 int *committed) /* xact committed or not */
3817 xfs_efd_log_item_t *efd; /* extent free data */
3818 xfs_efi_log_item_t *efi; /* extent free intention */
3819 int error; /* error return value */
3820 xfs_bmap_free_item_t *free; /* free extent item */
3821 unsigned int logres; /* new log reservation */
3822 unsigned int logcount; /* new log count */
3823 xfs_mount_t *mp; /* filesystem mount structure */
3824 xfs_bmap_free_item_t *next; /* next item on free list */
3825 xfs_trans_t *ntp; /* new transaction pointer */
3827 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3828 if (flist->xbf_count == 0) {
3833 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3834 for (free = flist->xbf_first; free; free = free->xbfi_next)
3835 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3836 free->xbfi_blockcount);
3837 logres = ntp->t_log_res;
3838 logcount = ntp->t_log_count;
3839 ntp = xfs_trans_dup(*tp);
3840 error = xfs_trans_commit(*tp, 0);
3844 * We have a new transaction, so we should return committed=1,
3845 * even though we're returning an error.
3851 * transaction commit worked ok so we can drop the extra ticket
3852 * reference that we gained in xfs_trans_dup()
3854 xfs_log_ticket_put(ntp->t_ticket);
3856 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
3859 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
3860 for (free = flist->xbf_first; free != NULL; free = next) {
3861 next = free->xbfi_next;
3862 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
3863 free->xbfi_blockcount))) {
3865 * The bmap free list will be cleaned up at a
3866 * higher level. The EFI will be canceled when
3867 * this transaction is aborted.
3868 * Need to force shutdown here to make sure it
3869 * happens, since this transaction may not be
3873 if (!XFS_FORCED_SHUTDOWN(mp))
3874 xfs_force_shutdown(mp,
3875 (error == EFSCORRUPTED) ?
3876 SHUTDOWN_CORRUPT_INCORE :
3877 SHUTDOWN_META_IO_ERROR);
3880 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
3881 free->xbfi_blockcount);
3882 xfs_bmap_del_free(flist, NULL, free);
3888 * Free up any items left in the list.
3892 xfs_bmap_free_t *flist) /* list of bmap_free_items */
3894 xfs_bmap_free_item_t *free; /* free list item */
3895 xfs_bmap_free_item_t *next;
3897 if (flist->xbf_count == 0)
3899 ASSERT(flist->xbf_first != NULL);
3900 for (free = flist->xbf_first; free; free = next) {
3901 next = free->xbfi_next;
3902 xfs_bmap_del_free(flist, NULL, free);
3904 ASSERT(flist->xbf_count == 0);
3908 * Returns the file-relative block number of the first unused block(s)
3909 * in the file with at least "len" logically contiguous blocks free.
3910 * This is the lowest-address hole if the file has holes, else the first block
3911 * past the end of file.
3912 * Return 0 if the file is currently local (in-inode).
3915 xfs_bmap_first_unused(
3916 xfs_trans_t *tp, /* transaction pointer */
3917 xfs_inode_t *ip, /* incore inode */
3918 xfs_extlen_t len, /* size of hole to find */
3919 xfs_fileoff_t *first_unused, /* unused block */
3920 int whichfork) /* data or attr fork */
3922 int error; /* error return value */
3923 int idx; /* extent record index */
3924 xfs_ifork_t *ifp; /* inode fork pointer */
3925 xfs_fileoff_t lastaddr; /* last block number seen */
3926 xfs_fileoff_t lowest; /* lowest useful block */
3927 xfs_fileoff_t max; /* starting useful block */
3928 xfs_fileoff_t off; /* offset for this block */
3929 xfs_extnum_t nextents; /* number of extent entries */
3931 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
3932 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
3933 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3934 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3938 ifp = XFS_IFORK_PTR(ip, whichfork);
3939 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3940 (error = xfs_iread_extents(tp, ip, whichfork)))
3942 lowest = *first_unused;
3943 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3944 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
3945 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
3946 off = xfs_bmbt_get_startoff(ep);
3948 * See if the hole before this extent will work.
3950 if (off >= lowest + len && off - max >= len) {
3951 *first_unused = max;
3954 lastaddr = off + xfs_bmbt_get_blockcount(ep);
3955 max = XFS_FILEOFF_MAX(lastaddr, lowest);
3957 *first_unused = max;
3962 * Returns the file-relative block number of the last block + 1 before
3963 * last_block (input value) in the file.
3964 * This is not based on i_size, it is based on the extent records.
3965 * Returns 0 for local files, as they do not have extent records.
3968 xfs_bmap_last_before(
3969 xfs_trans_t *tp, /* transaction pointer */
3970 xfs_inode_t *ip, /* incore inode */
3971 xfs_fileoff_t *last_block, /* last block */
3972 int whichfork) /* data or attr fork */
3974 xfs_fileoff_t bno; /* input file offset */
3975 int eof; /* hit end of file */
3976 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
3977 int error; /* error return value */
3978 xfs_bmbt_irec_t got; /* current extent value */
3979 xfs_ifork_t *ifp; /* inode fork pointer */
3980 xfs_extnum_t lastx; /* last extent used */
3981 xfs_bmbt_irec_t prev; /* previous extent value */
3983 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3984 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3985 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3986 return XFS_ERROR(EIO);
3987 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3991 ifp = XFS_IFORK_PTR(ip, whichfork);
3992 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3993 (error = xfs_iread_extents(tp, ip, whichfork)))
3995 bno = *last_block - 1;
3996 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
3998 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
3999 if (prev.br_startoff == NULLFILEOFF)
4002 *last_block = prev.br_startoff + prev.br_blockcount;
4005 * Otherwise *last_block is already the right answer.
4011 * Returns the file-relative block number of the first block past eof in
4012 * the file. This is not based on i_size, it is based on the extent records.
4013 * Returns 0 for local files, as they do not have extent records.
4016 xfs_bmap_last_offset(
4017 xfs_trans_t *tp, /* transaction pointer */
4018 xfs_inode_t *ip, /* incore inode */
4019 xfs_fileoff_t *last_block, /* last block */
4020 int whichfork) /* data or attr fork */
4022 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
4023 int error; /* error return value */
4024 xfs_ifork_t *ifp; /* inode fork pointer */
4025 xfs_extnum_t nextents; /* number of extent entries */
4027 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4028 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4029 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4030 return XFS_ERROR(EIO);
4031 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4035 ifp = XFS_IFORK_PTR(ip, whichfork);
4036 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4037 (error = xfs_iread_extents(tp, ip, whichfork)))
4039 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4044 ep = xfs_iext_get_ext(ifp, nextents - 1);
4045 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4050 * Returns whether the selected fork of the inode has exactly one
4051 * block or not. For the data fork we check this matches di_size,
4052 * implying the file's range is 0..bsize-1.
4054 int /* 1=>1 block, 0=>otherwise */
4056 xfs_inode_t *ip, /* incore inode */
4057 int whichfork) /* data or attr fork */
4059 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
4060 xfs_ifork_t *ifp; /* inode fork pointer */
4061 int rval; /* return value */
4062 xfs_bmbt_irec_t s; /* internal version of extent */
4065 if (whichfork == XFS_DATA_FORK) {
4066 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4067 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4068 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4071 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4073 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4075 ifp = XFS_IFORK_PTR(ip, whichfork);
4076 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4077 ep = xfs_iext_get_ext(ifp, 0);
4078 xfs_bmbt_get_all(ep, &s);
4079 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4080 if (rval && whichfork == XFS_DATA_FORK)
4081 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
4086 xfs_bmap_sanity_check(
4087 struct xfs_mount *mp,
4091 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4093 if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC ||
4094 be16_to_cpu(block->bb_level) != level ||
4095 be16_to_cpu(block->bb_numrecs) == 0 ||
4096 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4102 * Read in the extents to if_extents.
4103 * All inode fields are set up by caller, we just traverse the btree
4104 * and copy the records in. If the file system cannot contain unwritten
4105 * extents, the records are checked for no "state" flags.
4108 xfs_bmap_read_extents(
4109 xfs_trans_t *tp, /* transaction pointer */
4110 xfs_inode_t *ip, /* incore inode */
4111 int whichfork) /* data or attr fork */
4113 struct xfs_btree_block *block; /* current btree block */
4114 xfs_fsblock_t bno; /* block # of "block" */
4115 xfs_buf_t *bp; /* buffer for "block" */
4116 int error; /* error return value */
4117 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4118 xfs_extnum_t i, j; /* index into the extents list */
4119 xfs_ifork_t *ifp; /* fork structure */
4120 int level; /* btree level, for checking */
4121 xfs_mount_t *mp; /* file system mount structure */
4122 __be64 *pp; /* pointer to block address */
4124 xfs_extnum_t room; /* number of entries there's room for */
4128 ifp = XFS_IFORK_PTR(ip, whichfork);
4129 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4130 XFS_EXTFMT_INODE(ip);
4131 block = ifp->if_broot;
4133 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4135 level = be16_to_cpu(block->bb_level);
4137 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
4138 bno = be64_to_cpu(*pp);
4139 ASSERT(bno != NULLDFSBNO);
4140 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4141 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
4143 * Go down the tree until leaf level is reached, following the first
4144 * pointer (leftmost) at each level.
4146 while (level-- > 0) {
4147 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4148 XFS_BMAP_BTREE_REF)))
4150 block = XFS_BUF_TO_BLOCK(bp);
4151 XFS_WANT_CORRUPTED_GOTO(
4152 xfs_bmap_sanity_check(mp, bp, level),
4156 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
4157 bno = be64_to_cpu(*pp);
4158 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
4159 xfs_trans_brelse(tp, bp);
4162 * Here with bp and block set to the leftmost leaf node in the tree.
4164 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4167 * Loop over all leaf nodes. Copy information to the extent records.
4170 xfs_bmbt_rec_t *frp;
4171 xfs_fsblock_t nextbno;
4172 xfs_extnum_t num_recs;
4176 num_recs = xfs_btree_get_numrecs(block);
4177 if (unlikely(i + num_recs > room)) {
4178 ASSERT(i + num_recs <= room);
4179 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4180 "corrupt dinode %Lu, (btree extents).",
4181 (unsigned long long) ip->i_ino);
4182 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4187 XFS_WANT_CORRUPTED_GOTO(
4188 xfs_bmap_sanity_check(mp, bp, 0),
4191 * Read-ahead the next leaf block, if any.
4193 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
4194 if (nextbno != NULLFSBLOCK)
4195 xfs_btree_reada_bufl(mp, nextbno, 1);
4197 * Copy records into the extent records.
4199 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
4201 for (j = 0; j < num_recs; j++, i++, frp++) {
4202 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
4203 trp->l0 = be64_to_cpu(frp->l0);
4204 trp->l1 = be64_to_cpu(frp->l1);
4206 if (exntf == XFS_EXTFMT_NOSTATE) {
4208 * Check all attribute bmap btree records and
4209 * any "older" data bmap btree records for a
4210 * set bit in the "extent flag" position.
4212 if (unlikely(xfs_check_nostate_extents(ifp,
4213 start, num_recs))) {
4214 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4220 xfs_trans_brelse(tp, bp);
4223 * If we've reached the end, stop.
4225 if (bno == NULLFSBLOCK)
4227 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4228 XFS_BMAP_BTREE_REF)))
4230 block = XFS_BUF_TO_BLOCK(bp);
4232 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4233 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4234 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
4237 xfs_trans_brelse(tp, bp);
4238 return XFS_ERROR(EFSCORRUPTED);
4243 * Add bmap trace insert entries for all the contents of the extent records.
4246 xfs_bmap_trace_exlist(
4247 xfs_inode_t *ip, /* incore inode pointer */
4248 xfs_extnum_t cnt, /* count of entries in the list */
4249 int whichfork, /* data or attr fork */
4250 unsigned long caller_ip)
4252 xfs_extnum_t idx; /* extent record index */
4253 xfs_ifork_t *ifp; /* inode fork pointer */
4256 if (whichfork == XFS_ATTR_FORK)
4257 state |= BMAP_ATTRFORK;
4259 ifp = XFS_IFORK_PTR(ip, whichfork);
4260 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4261 for (idx = 0; idx < cnt; idx++)
4262 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
4266 * Validate that the bmbt_irecs being returned from bmapi are valid
4267 * given the callers original parameters. Specifically check the
4268 * ranges of the returned irecs to ensure that they only extent beyond
4269 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4272 xfs_bmap_validate_ret(
4276 xfs_bmbt_irec_t *mval,
4280 int i; /* index to map values */
4282 ASSERT(ret_nmap <= nmap);
4284 for (i = 0; i < ret_nmap; i++) {
4285 ASSERT(mval[i].br_blockcount > 0);
4286 if (!(flags & XFS_BMAPI_ENTIRE)) {
4287 ASSERT(mval[i].br_startoff >= bno);
4288 ASSERT(mval[i].br_blockcount <= len);
4289 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4292 ASSERT(mval[i].br_startoff < bno + len);
4293 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4297 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4298 mval[i].br_startoff);
4299 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4300 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4301 mval[i].br_startblock != HOLESTARTBLOCK);
4302 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4303 mval[i].br_state == XFS_EXT_UNWRITTEN);
4310 * Map file blocks to filesystem blocks.
4311 * File range is given by the bno/len pair.
4312 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4313 * into a hole or past eof.
4314 * Only allocates blocks from a single allocation group,
4315 * to avoid locking problems.
4316 * The returned value in "firstblock" from the first call in a transaction
4317 * must be remembered and presented to subsequent calls in "firstblock".
4318 * An upper bound for the number of blocks to be allocated is supplied to
4319 * the first call in "total"; if no allocation group has that many free
4320 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4324 xfs_trans_t *tp, /* transaction pointer */
4325 xfs_inode_t *ip, /* incore inode */
4326 xfs_fileoff_t bno, /* starting file offs. mapped */
4327 xfs_filblks_t len, /* length to map in file */
4328 int flags, /* XFS_BMAPI_... */
4329 xfs_fsblock_t *firstblock, /* first allocated block
4330 controls a.g. for allocs */
4331 xfs_extlen_t total, /* total blocks needed */
4332 xfs_bmbt_irec_t *mval, /* output: map values */
4333 int *nmap, /* i/o: mval size/count */
4334 xfs_bmap_free_t *flist) /* i/o: list extents to free */
4336 xfs_fsblock_t abno; /* allocated block number */
4337 xfs_extlen_t alen; /* allocated extent length */
4338 xfs_fileoff_t aoff; /* allocated file offset */
4339 xfs_bmalloca_t bma = { 0 }; /* args for xfs_bmap_alloc */
4340 xfs_btree_cur_t *cur; /* bmap btree cursor */
4341 xfs_fileoff_t end; /* end of mapped file region */
4342 int eof; /* we've hit the end of extents */
4343 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
4344 int error; /* error return */
4345 xfs_bmbt_irec_t got; /* current file extent record */
4346 xfs_ifork_t *ifp; /* inode fork pointer */
4347 xfs_extlen_t indlen; /* indirect blocks length */
4348 xfs_extnum_t lastx; /* last useful extent number */
4349 int logflags; /* flags for transaction logging */
4350 xfs_extlen_t minleft; /* min blocks left after allocation */
4351 xfs_extlen_t minlen; /* min allocation size */
4352 xfs_mount_t *mp; /* xfs mount structure */
4353 int n; /* current extent index */
4354 int nallocs; /* number of extents alloc'd */
4355 xfs_extnum_t nextents; /* number of extents in file */
4356 xfs_fileoff_t obno; /* old block number (offset) */
4357 xfs_bmbt_irec_t prev; /* previous file extent record */
4358 int tmp_logflags; /* temp flags holder */
4359 int whichfork; /* data or attr fork */
4360 char inhole; /* current location is hole in file */
4361 char wasdelay; /* old extent was delayed */
4362 char wr; /* this is a write request */
4363 char rt; /* this is a realtime file */
4365 xfs_fileoff_t orig_bno; /* original block number value */
4366 int orig_flags; /* original flags arg value */
4367 xfs_filblks_t orig_len; /* original value of len arg */
4368 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4369 int orig_nmap; /* original value of *nmap */
4378 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4379 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4380 XFS_ATTR_FORK : XFS_DATA_FORK;
4382 if (unlikely(XFS_TEST_ERROR(
4383 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4384 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4385 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4386 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4387 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4388 return XFS_ERROR(EFSCORRUPTED);
4390 if (XFS_FORCED_SHUTDOWN(mp))
4391 return XFS_ERROR(EIO);
4392 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4393 ifp = XFS_IFORK_PTR(ip, whichfork);
4394 ASSERT(ifp->if_ext_max ==
4395 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4396 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4397 XFS_STATS_INC(xs_blk_mapw);
4399 XFS_STATS_INC(xs_blk_mapr);
4401 * IGSTATE flag is used to combine extents which
4402 * differ only due to the state of the extents.
4403 * This technique is used from xfs_getbmap()
4404 * when the caller does not wish to see the
4405 * separation (which is the default).
4407 * This technique is also used when writing a
4408 * buffer which has been partially written,
4409 * (usually by being flushed during a chunkread),
4410 * to ensure one write takes place. This also
4411 * prevents a change in the xfs inode extents at
4412 * this time, intentionally. This change occurs
4413 * on completion of the write operation, in
4414 * xfs_strat_comp(), where the xfs_bmapi() call
4415 * is transactioned, and the extents combined.
4417 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4418 wr = 0; /* no allocations are allowed */
4419 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
4423 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4425 if ((error = xfs_bmap_local_to_extents(tp, ip,
4426 firstblock, total, &logflags, whichfork)))
4429 if (wr && *firstblock == NULLFSBLOCK) {
4430 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4431 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4436 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4437 (error = xfs_iread_extents(tp, ip, whichfork)))
4439 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4441 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4447 while (bno < end && n < *nmap) {
4449 * Reading past eof, act as though there's a hole
4453 got.br_startoff = end;
4454 inhole = eof || got.br_startoff > bno;
4455 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
4456 isnullstartblock(got.br_startblock);
4458 * First, deal with the hole before the allocated space
4459 * that we found, if any.
4461 if (wr && (inhole || wasdelay)) {
4463 * For the wasdelay case, we could also just
4464 * allocate the stuff asked for in this bmap call
4465 * but that wouldn't be as good.
4468 alen = (xfs_extlen_t)got.br_blockcount;
4469 aoff = got.br_startoff;
4470 if (lastx != NULLEXTNUM && lastx) {
4471 ep = xfs_iext_get_ext(ifp, lastx - 1);
4472 xfs_bmbt_get_all(ep, &prev);
4475 alen = (xfs_extlen_t)
4476 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4478 alen = (xfs_extlen_t)
4479 XFS_FILBLKS_MIN(alen,
4480 got.br_startoff - bno);
4483 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4484 if (flags & XFS_BMAPI_DELAY) {
4487 /* Figure out the extent size, adjust alen */
4488 extsz = xfs_get_extsz_hint(ip);
4490 error = xfs_bmap_extsize_align(mp,
4493 flags&XFS_BMAPI_DELAY,
4494 flags&XFS_BMAPI_CONVERT,
4500 extsz = alen / mp->m_sb.sb_rextsize;
4503 * Make a transaction-less quota reservation for
4504 * delayed allocation blocks. This number gets
4505 * adjusted later. We return if we haven't
4506 * allocated blocks already inside this loop.
4508 error = xfs_trans_reserve_quota_nblks(
4509 NULL, ip, (long)alen, 0,
4510 rt ? XFS_QMOPT_RES_RTBLKS :
4511 XFS_QMOPT_RES_REGBLKS);
4515 ASSERT(cur == NULL);
4522 * Split changing sb for alen and indlen since
4523 * they could be coming from different places.
4525 indlen = (xfs_extlen_t)
4526 xfs_bmap_worst_indlen(ip, alen);
4530 error = xfs_mod_incore_sb(mp,
4532 -((int64_t)extsz), (flags &
4533 XFS_BMAPI_RSVBLOCKS));
4535 error = xfs_icsb_modify_counters(mp,
4537 -((int64_t)alen), (flags &
4538 XFS_BMAPI_RSVBLOCKS));
4541 error = xfs_icsb_modify_counters(mp,
4543 -((int64_t)indlen), (flags &
4544 XFS_BMAPI_RSVBLOCKS));
4546 xfs_mod_incore_sb(mp,
4548 (int64_t)extsz, (flags &
4549 XFS_BMAPI_RSVBLOCKS));
4551 xfs_icsb_modify_counters(mp,
4553 (int64_t)alen, (flags &
4554 XFS_BMAPI_RSVBLOCKS));
4558 if (XFS_IS_QUOTA_ON(mp))
4559 /* unreserve the blocks now */
4561 xfs_trans_unreserve_quota_nblks(
4564 XFS_QMOPT_RES_RTBLKS :
4565 XFS_QMOPT_RES_REGBLKS);
4569 ip->i_delayed_blks += alen;
4570 abno = nullstartblock(indlen);
4573 * If first time, allocate and fill in
4574 * once-only bma fields.
4576 if (bma.ip == NULL) {
4584 /* Indicate if this is the first user data
4585 * in the file, or just any user data.
4587 if (!(flags & XFS_BMAPI_METADATA)) {
4588 bma.userdata = (aoff == 0) ?
4589 XFS_ALLOC_INITIAL_USER_DATA :
4593 * Fill in changeable bma fields.
4596 bma.firstblock = *firstblock;
4599 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4600 bma.wasdel = wasdelay;
4601 bma.minlen = minlen;
4602 bma.low = flist->xbf_low;
4603 bma.minleft = minleft;
4605 * Only want to do the alignment at the
4606 * eof if it is userdata and allocation length
4607 * is larger than a stripe unit.
4609 if (mp->m_dalign && alen >= mp->m_dalign &&
4610 (!(flags & XFS_BMAPI_METADATA)) &&
4611 (whichfork == XFS_DATA_FORK)) {
4612 if ((error = xfs_bmap_isaeof(ip, aoff,
4613 whichfork, &bma.aeof)))
4620 if ((error = xfs_bmap_alloc(&bma)))
4623 * Copy out result fields.
4626 if ((flist->xbf_low = bma.low))
4630 ASSERT(*firstblock == NULLFSBLOCK ||
4631 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4632 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
4634 XFS_FSB_TO_AGNO(mp, *firstblock) <
4635 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
4636 *firstblock = bma.firstblock;
4638 cur->bc_private.b.firstblock =
4640 if (abno == NULLFSBLOCK)
4642 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
4643 cur = xfs_bmbt_init_cursor(mp, tp,
4645 cur->bc_private.b.firstblock =
4647 cur->bc_private.b.flist = flist;
4650 * Bump the number of extents we've allocated
4656 cur->bc_private.b.flags =
4657 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
4658 got.br_startoff = aoff;
4659 got.br_startblock = abno;
4660 got.br_blockcount = alen;
4661 got.br_state = XFS_EXT_NORM; /* assume normal */
4663 * Determine state of extent, and the filesystem.
4664 * A wasdelay extent has been initialized, so
4665 * shouldn't be flagged as unwritten.
4667 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
4668 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
4669 got.br_state = XFS_EXT_UNWRITTEN;
4671 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
4672 firstblock, flist, &tmp_logflags,
4673 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
4674 logflags |= tmp_logflags;
4677 lastx = ifp->if_lastex;
4678 ep = xfs_iext_get_ext(ifp, lastx);
4679 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4680 xfs_bmbt_get_all(ep, &got);
4681 ASSERT(got.br_startoff <= aoff);
4682 ASSERT(got.br_startoff + got.br_blockcount >=
4685 if (flags & XFS_BMAPI_DELAY) {
4686 ASSERT(isnullstartblock(got.br_startblock));
4687 ASSERT(startblockval(got.br_startblock) > 0);
4689 ASSERT(got.br_state == XFS_EXT_NORM ||
4690 got.br_state == XFS_EXT_UNWRITTEN);
4693 * Fall down into the found allocated space case.
4695 } else if (inhole) {
4697 * Reading in a hole.
4699 mval->br_startoff = bno;
4700 mval->br_startblock = HOLESTARTBLOCK;
4701 mval->br_blockcount =
4702 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4703 mval->br_state = XFS_EXT_NORM;
4704 bno += mval->br_blockcount;
4705 len -= mval->br_blockcount;
4711 * Then deal with the allocated space we found.
4714 if (!(flags & XFS_BMAPI_ENTIRE) &&
4715 (got.br_startoff + got.br_blockcount > obno)) {
4718 ASSERT((bno >= obno) || (n == 0));
4720 mval->br_startoff = bno;
4721 if (isnullstartblock(got.br_startblock)) {
4722 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
4723 mval->br_startblock = DELAYSTARTBLOCK;
4725 mval->br_startblock =
4727 (bno - got.br_startoff);
4729 * Return the minimum of what we got and what we
4730 * asked for for the length. We can use the len
4731 * variable here because it is modified below
4732 * and we could have been there before coming
4733 * here if the first part of the allocation
4734 * didn't overlap what was asked for.
4736 mval->br_blockcount =
4737 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
4738 (bno - got.br_startoff));
4739 mval->br_state = got.br_state;
4740 ASSERT(mval->br_blockcount <= len);
4743 if (isnullstartblock(mval->br_startblock)) {
4744 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
4745 mval->br_startblock = DELAYSTARTBLOCK;
4750 * Check if writing previously allocated but
4751 * unwritten extents.
4754 ((mval->br_state == XFS_EXT_UNWRITTEN &&
4755 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) ||
4756 (mval->br_state == XFS_EXT_NORM &&
4757 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT)) ==
4758 (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT))))) {
4760 * Modify (by adding) the state flag, if writing.
4762 ASSERT(mval->br_blockcount <= len);
4763 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
4764 cur = xfs_bmbt_init_cursor(mp,
4766 cur->bc_private.b.firstblock =
4768 cur->bc_private.b.flist = flist;
4770 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4772 : XFS_EXT_UNWRITTEN;
4773 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
4774 firstblock, flist, &tmp_logflags,
4775 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
4776 logflags |= tmp_logflags;
4779 lastx = ifp->if_lastex;
4780 ep = xfs_iext_get_ext(ifp, lastx);
4781 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4782 xfs_bmbt_get_all(ep, &got);
4784 * We may have combined previously unwritten
4785 * space with written space, so generate
4788 if (mval->br_blockcount < len)
4792 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4793 ((mval->br_startoff + mval->br_blockcount) <= end));
4794 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4795 (mval->br_blockcount <= len) ||
4796 (mval->br_startoff < obno));
4797 bno = mval->br_startoff + mval->br_blockcount;
4799 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4800 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4801 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4802 ASSERT(mval->br_state == mval[-1].br_state);
4803 mval[-1].br_blockcount = mval->br_blockcount;
4804 mval[-1].br_state = mval->br_state;
4805 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4806 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4807 mval[-1].br_startblock != HOLESTARTBLOCK &&
4808 mval->br_startblock ==
4809 mval[-1].br_startblock + mval[-1].br_blockcount &&
4810 ((flags & XFS_BMAPI_IGSTATE) ||
4811 mval[-1].br_state == mval->br_state)) {
4812 ASSERT(mval->br_startoff ==
4813 mval[-1].br_startoff + mval[-1].br_blockcount);
4814 mval[-1].br_blockcount += mval->br_blockcount;
4816 mval->br_startblock == DELAYSTARTBLOCK &&
4817 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4818 mval->br_startoff ==
4819 mval[-1].br_startoff + mval[-1].br_blockcount) {
4820 mval[-1].br_blockcount += mval->br_blockcount;
4821 mval[-1].br_state = mval->br_state;
4822 } else if (!((n == 0) &&
4823 ((mval->br_startoff + mval->br_blockcount) <=
4829 * If we're done, stop now. Stop when we've allocated
4830 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4831 * the transaction may get too big.
4833 if (bno >= end || n >= *nmap || nallocs >= *nmap)
4836 * Else go on to the next record.
4838 ep = xfs_iext_get_ext(ifp, ++lastx);
4840 if (lastx >= nextents)
4843 xfs_bmbt_get_all(ep, &got);
4845 ifp->if_lastex = lastx;
4848 * Transform from btree to extents, give it cur.
4850 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
4851 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
4853 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4854 &tmp_logflags, whichfork);
4855 logflags |= tmp_logflags;
4859 ASSERT(ifp->if_ext_max ==
4860 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4861 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4862 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
4866 * Log everything. Do this after conversion, there's no point in
4867 * logging the extent records if we've converted to btree format.
4869 if ((logflags & xfs_ilog_fext(whichfork)) &&
4870 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4871 logflags &= ~xfs_ilog_fext(whichfork);
4872 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
4873 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
4874 logflags &= ~xfs_ilog_fbroot(whichfork);
4876 * Log whatever the flags say, even if error. Otherwise we might miss
4877 * detecting a case where the data is changed, there's an error,
4878 * and it's not logged so we don't shutdown when we should.
4882 xfs_trans_log_inode(tp, ip, logflags);
4886 ASSERT(*firstblock == NULLFSBLOCK ||
4887 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4889 cur->bc_private.b.firstblock) ||
4891 XFS_FSB_TO_AGNO(mp, *firstblock) <
4893 cur->bc_private.b.firstblock)));
4894 *firstblock = cur->bc_private.b.firstblock;
4896 xfs_btree_del_cursor(cur,
4897 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4900 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4906 * Map file blocks to filesystem blocks, simple version.
4907 * One block (extent) only, read-only.
4908 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
4909 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
4910 * was set and all the others were clear.
4914 xfs_trans_t *tp, /* transaction pointer */
4915 xfs_inode_t *ip, /* incore inode */
4916 int whichfork, /* data or attr fork */
4917 xfs_fsblock_t *fsb, /* output: mapped block */
4918 xfs_fileoff_t bno) /* starting file offs. mapped */
4920 int eof; /* we've hit the end of extents */
4921 int error; /* error return */
4922 xfs_bmbt_irec_t got; /* current file extent record */
4923 xfs_ifork_t *ifp; /* inode fork pointer */
4924 xfs_extnum_t lastx; /* last useful extent number */
4925 xfs_bmbt_irec_t prev; /* previous file extent record */
4927 ifp = XFS_IFORK_PTR(ip, whichfork);
4929 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4930 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
4931 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
4933 return XFS_ERROR(EFSCORRUPTED);
4935 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
4936 return XFS_ERROR(EIO);
4937 XFS_STATS_INC(xs_blk_mapr);
4938 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4939 (error = xfs_iread_extents(tp, ip, whichfork)))
4941 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4944 * Reading past eof, act as though there's a hole
4947 if (eof || got.br_startoff > bno) {
4951 ASSERT(!isnullstartblock(got.br_startblock));
4952 ASSERT(bno < got.br_startoff + got.br_blockcount);
4953 *fsb = got.br_startblock + (bno - got.br_startoff);
4954 ifp->if_lastex = lastx;
4959 * Unmap (remove) blocks from a file.
4960 * If nexts is nonzero then the number of extents to remove is limited to
4961 * that value. If not all extents in the block range can be removed then
4966 xfs_trans_t *tp, /* transaction pointer */
4967 struct xfs_inode *ip, /* incore inode */
4968 xfs_fileoff_t bno, /* starting offset to unmap */
4969 xfs_filblks_t len, /* length to unmap in file */
4970 int flags, /* misc flags */
4971 xfs_extnum_t nexts, /* number of extents max */
4972 xfs_fsblock_t *firstblock, /* first allocated block
4973 controls a.g. for allocs */
4974 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4975 int *done) /* set if not done yet */
4977 xfs_btree_cur_t *cur; /* bmap btree cursor */
4978 xfs_bmbt_irec_t del; /* extent being deleted */
4979 int eof; /* is deleting at eof */
4980 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
4981 int error; /* error return value */
4982 xfs_extnum_t extno; /* extent number in list */
4983 xfs_bmbt_irec_t got; /* current extent record */
4984 xfs_ifork_t *ifp; /* inode fork pointer */
4985 int isrt; /* freeing in rt area */
4986 xfs_extnum_t lastx; /* last extent index used */
4987 int logflags; /* transaction logging flags */
4988 xfs_extlen_t mod; /* rt extent offset */
4989 xfs_mount_t *mp; /* mount structure */
4990 xfs_extnum_t nextents; /* number of file extents */
4991 xfs_bmbt_irec_t prev; /* previous extent record */
4992 xfs_fileoff_t start; /* first file offset deleted */
4993 int tmp_logflags; /* partial logging flags */
4994 int wasdel; /* was a delayed alloc extent */
4995 int whichfork; /* data or attribute fork */
4996 int rsvd; /* OK to allocate reserved blocks */
4999 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5001 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5002 XFS_ATTR_FORK : XFS_DATA_FORK;
5003 ifp = XFS_IFORK_PTR(ip, whichfork);
5005 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5006 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5007 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5009 return XFS_ERROR(EFSCORRUPTED);
5012 if (XFS_FORCED_SHUTDOWN(mp))
5013 return XFS_ERROR(EIO);
5014 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5017 ASSERT(ifp->if_ext_max ==
5018 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5019 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5020 (error = xfs_iread_extents(tp, ip, whichfork)))
5022 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5023 if (nextents == 0) {
5027 XFS_STATS_INC(xs_blk_unmap);
5028 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5030 bno = start + len - 1;
5031 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5035 * Check to see if the given block number is past the end of the
5036 * file, back up to the last block if so...
5039 ep = xfs_iext_get_ext(ifp, --lastx);
5040 xfs_bmbt_get_all(ep, &got);
5041 bno = got.br_startoff + got.br_blockcount - 1;
5044 if (ifp->if_flags & XFS_IFBROOT) {
5045 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5046 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5047 cur->bc_private.b.firstblock = *firstblock;
5048 cur->bc_private.b.flist = flist;
5049 cur->bc_private.b.flags = 0;
5053 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5054 (nexts == 0 || extno < nexts)) {
5056 * Is the found extent after a hole in which bno lives?
5057 * Just back up to the previous extent, if so.
5059 if (got.br_startoff > bno) {
5062 ep = xfs_iext_get_ext(ifp, lastx);
5063 xfs_bmbt_get_all(ep, &got);
5066 * Is the last block of this extent before the range
5067 * we're supposed to delete? If so, we're done.
5069 bno = XFS_FILEOFF_MIN(bno,
5070 got.br_startoff + got.br_blockcount - 1);
5074 * Then deal with the (possibly delayed) allocated space
5079 wasdel = isnullstartblock(del.br_startblock);
5080 if (got.br_startoff < start) {
5081 del.br_startoff = start;
5082 del.br_blockcount -= start - got.br_startoff;
5084 del.br_startblock += start - got.br_startoff;
5086 if (del.br_startoff + del.br_blockcount > bno + 1)
5087 del.br_blockcount = bno + 1 - del.br_startoff;
5088 sum = del.br_startblock + del.br_blockcount;
5090 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5092 * Realtime extent not lined up at the end.
5093 * The extent could have been split into written
5094 * and unwritten pieces, or we could just be
5095 * unmapping part of it. But we can't really
5096 * get rid of part of a realtime extent.
5098 if (del.br_state == XFS_EXT_UNWRITTEN ||
5099 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5101 * This piece is unwritten, or we're not
5102 * using unwritten extents. Skip over it.
5105 bno -= mod > del.br_blockcount ?
5106 del.br_blockcount : mod;
5107 if (bno < got.br_startoff) {
5109 xfs_bmbt_get_all(xfs_iext_get_ext(
5115 * It's written, turn it unwritten.
5116 * This is better than zeroing it.
5118 ASSERT(del.br_state == XFS_EXT_NORM);
5119 ASSERT(xfs_trans_get_block_res(tp) > 0);
5121 * If this spans a realtime extent boundary,
5122 * chop it back to the start of the one we end at.
5124 if (del.br_blockcount > mod) {
5125 del.br_startoff += del.br_blockcount - mod;
5126 del.br_startblock += del.br_blockcount - mod;
5127 del.br_blockcount = mod;
5129 del.br_state = XFS_EXT_UNWRITTEN;
5130 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
5131 firstblock, flist, &logflags,
5137 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5139 * Realtime extent is lined up at the end but not
5140 * at the front. We'll get rid of full extents if
5143 mod = mp->m_sb.sb_rextsize - mod;
5144 if (del.br_blockcount > mod) {
5145 del.br_blockcount -= mod;
5146 del.br_startoff += mod;
5147 del.br_startblock += mod;
5148 } else if ((del.br_startoff == start &&
5149 (del.br_state == XFS_EXT_UNWRITTEN ||
5150 xfs_trans_get_block_res(tp) == 0)) ||
5151 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5153 * Can't make it unwritten. There isn't
5154 * a full extent here so just skip it.
5156 ASSERT(bno >= del.br_blockcount);
5157 bno -= del.br_blockcount;
5158 if (bno < got.br_startoff) {
5160 xfs_bmbt_get_all(--ep, &got);
5163 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5165 * This one is already unwritten.
5166 * It must have a written left neighbor.
5167 * Unwrite the killed part of that one and
5171 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5173 ASSERT(prev.br_state == XFS_EXT_NORM);
5174 ASSERT(!isnullstartblock(prev.br_startblock));
5175 ASSERT(del.br_startblock ==
5176 prev.br_startblock + prev.br_blockcount);
5177 if (prev.br_startoff < start) {
5178 mod = start - prev.br_startoff;
5179 prev.br_blockcount -= mod;
5180 prev.br_startblock += mod;
5181 prev.br_startoff = start;
5183 prev.br_state = XFS_EXT_UNWRITTEN;
5184 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5185 &prev, firstblock, flist, &logflags,
5191 ASSERT(del.br_state == XFS_EXT_NORM);
5192 del.br_state = XFS_EXT_UNWRITTEN;
5193 error = xfs_bmap_add_extent(ip, lastx, &cur,
5194 &del, firstblock, flist, &logflags,
5202 ASSERT(startblockval(del.br_startblock) > 0);
5203 /* Update realtime/data freespace, unreserve quota */
5205 xfs_filblks_t rtexts;
5207 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5208 do_div(rtexts, mp->m_sb.sb_rextsize);
5209 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5210 (int64_t)rtexts, rsvd);
5211 (void)xfs_trans_reserve_quota_nblks(NULL,
5212 ip, -((long)del.br_blockcount), 0,
5213 XFS_QMOPT_RES_RTBLKS);
5215 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
5216 (int64_t)del.br_blockcount, rsvd);
5217 (void)xfs_trans_reserve_quota_nblks(NULL,
5218 ip, -((long)del.br_blockcount), 0,
5219 XFS_QMOPT_RES_REGBLKS);
5221 ip->i_delayed_blks -= del.br_blockcount;
5223 cur->bc_private.b.flags |=
5224 XFS_BTCUR_BPRV_WASDEL;
5226 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5228 * If it's the case where the directory code is running
5229 * with no block reservation, and the deleted block is in
5230 * the middle of its extent, and the resulting insert
5231 * of an extent would cause transformation to btree format,
5232 * then reject it. The calling code will then swap
5233 * blocks around instead.
5234 * We have to do this now, rather than waiting for the
5235 * conversion to btree format, since the transaction
5238 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5239 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5240 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5241 del.br_startoff > got.br_startoff &&
5242 del.br_startoff + del.br_blockcount <
5243 got.br_startoff + got.br_blockcount) {
5244 error = XFS_ERROR(ENOSPC);
5247 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
5248 &tmp_logflags, whichfork, rsvd);
5249 logflags |= tmp_logflags;
5252 bno = del.br_startoff - 1;
5254 lastx = ifp->if_lastex;
5256 * If not done go on to the next (previous) record.
5257 * Reset ep in case the extents array was re-alloced.
5259 ep = xfs_iext_get_ext(ifp, lastx);
5260 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5261 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5262 xfs_bmbt_get_startoff(ep) > bno) {
5264 ep = xfs_iext_get_ext(ifp, lastx);
5267 xfs_bmbt_get_all(ep, &got);
5271 ifp->if_lastex = lastx;
5272 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5273 ASSERT(ifp->if_ext_max ==
5274 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5276 * Convert to a btree if necessary.
5278 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5279 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5280 ASSERT(cur == NULL);
5281 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5282 &cur, 0, &tmp_logflags, whichfork);
5283 logflags |= tmp_logflags;
5288 * transform from btree to extents, give it cur
5290 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5291 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5292 ASSERT(cur != NULL);
5293 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5295 logflags |= tmp_logflags;
5300 * transform from extents to local?
5302 ASSERT(ifp->if_ext_max ==
5303 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5307 * Log everything. Do this after conversion, there's no point in
5308 * logging the extent records if we've converted to btree format.
5310 if ((logflags & xfs_ilog_fext(whichfork)) &&
5311 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5312 logflags &= ~xfs_ilog_fext(whichfork);
5313 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5314 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5315 logflags &= ~xfs_ilog_fbroot(whichfork);
5317 * Log inode even in the error case, if the transaction
5318 * is dirty we'll need to shut down the filesystem.
5321 xfs_trans_log_inode(tp, ip, logflags);
5324 *firstblock = cur->bc_private.b.firstblock;
5325 cur->bc_private.b.allocated = 0;
5327 xfs_btree_del_cursor(cur,
5328 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5334 * returns 1 for success, 0 if we failed to map the extent.
5337 xfs_getbmapx_fix_eof_hole(
5338 xfs_inode_t *ip, /* xfs incore inode pointer */
5339 struct getbmapx *out, /* output structure */
5340 int prealloced, /* this is a file with
5341 * preallocated data space */
5342 __int64_t end, /* last block requested */
5343 xfs_fsblock_t startblock)
5346 xfs_mount_t *mp; /* file system mount point */
5347 xfs_ifork_t *ifp; /* inode fork pointer */
5348 xfs_extnum_t lastx; /* last extent pointer */
5349 xfs_fileoff_t fileblock;
5351 if (startblock == HOLESTARTBLOCK) {
5353 out->bmv_block = -1;
5354 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5355 fixlen -= out->bmv_offset;
5356 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5357 /* Came to hole at EOF. Trim it. */
5360 out->bmv_length = fixlen;
5363 if (startblock == DELAYSTARTBLOCK)
5364 out->bmv_block = -2;
5366 out->bmv_block = xfs_fsb_to_db(ip, startblock);
5367 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5368 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5369 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5370 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5371 out->bmv_oflags |= BMV_OF_LAST;
5378 * Get inode's extents as described in bmv, and format for output.
5379 * Calls formatter to fill the user's buffer until all extents
5380 * are mapped, until the passed-in bmv->bmv_count slots have
5381 * been filled, or until the formatter short-circuits the loop,
5382 * if it is tracking filled-in extents on its own.
5384 int /* error code */
5387 struct getbmapx *bmv, /* user bmap structure */
5388 xfs_bmap_format_t formatter, /* format to user */
5389 void *arg) /* formatter arg */
5391 __int64_t bmvend; /* last block requested */
5392 int error = 0; /* return value */
5393 __int64_t fixlen; /* length for -1 case */
5394 int i; /* extent number */
5395 int lock; /* lock state */
5396 xfs_bmbt_irec_t *map; /* buffer for user's data */
5397 xfs_mount_t *mp; /* file system mount point */
5398 int nex; /* # of user extents can do */
5399 int nexleft; /* # of user extents left */
5400 int subnex; /* # of bmapi's can do */
5401 int nmap; /* number of map entries */
5402 struct getbmapx *out; /* output structure */
5403 int whichfork; /* data or attr fork */
5404 int prealloced; /* this is a file with
5405 * preallocated data space */
5406 int iflags; /* interface flags */
5407 int bmapi_flags; /* flags for xfs_bmapi */
5411 iflags = bmv->bmv_iflags;
5412 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5414 if (whichfork == XFS_ATTR_FORK) {
5415 if (XFS_IFORK_Q(ip)) {
5416 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5417 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5418 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5419 return XFS_ERROR(EINVAL);
5420 } else if (unlikely(
5421 ip->i_d.di_aformat != 0 &&
5422 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5423 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5425 return XFS_ERROR(EFSCORRUPTED);
5431 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5432 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5433 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5434 return XFS_ERROR(EINVAL);
5436 if (xfs_get_extsz_hint(ip) ||
5437 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5439 fixlen = XFS_MAXIOFFSET(mp);
5442 fixlen = ip->i_size;
5446 if (bmv->bmv_length == -1) {
5447 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5449 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5450 } else if (bmv->bmv_length == 0) {
5451 bmv->bmv_entries = 0;
5453 } else if (bmv->bmv_length < 0) {
5454 return XFS_ERROR(EINVAL);
5457 nex = bmv->bmv_count - 1;
5459 return XFS_ERROR(EINVAL);
5460 bmvend = bmv->bmv_offset + bmv->bmv_length;
5463 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5464 return XFS_ERROR(ENOMEM);
5465 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5467 return XFS_ERROR(ENOMEM);
5469 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5470 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5471 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5472 error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5474 goto out_unlock_iolock;
5477 * even after flushing the inode, there can still be delalloc
5478 * blocks on the inode beyond EOF due to speculative
5479 * preallocation. These are not removed until the release
5480 * function is called or the inode is inactivated. Hence we
5481 * cannot assert here that ip->i_delayed_blks == 0.
5485 lock = xfs_ilock_map_shared(ip);
5488 * Don't let nex be bigger than the number of extents
5489 * we can have assuming alternating holes and real extents.
5491 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5492 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5494 bmapi_flags = xfs_bmapi_aflag(whichfork);
5495 if (!(iflags & BMV_IF_PREALLOC))
5496 bmapi_flags |= XFS_BMAPI_IGSTATE;
5499 * Allocate enough space to handle "subnex" maps at a time.
5503 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
5505 goto out_unlock_ilock;
5507 bmv->bmv_entries = 0;
5509 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5510 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5518 nmap = (nexleft > subnex) ? subnex : nexleft;
5519 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5520 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5521 bmapi_flags, NULL, 0, map, &nmap,
5525 ASSERT(nmap <= subnex);
5527 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5528 out[cur_ext].bmv_oflags = 0;
5529 if (map[i].br_state == XFS_EXT_UNWRITTEN)
5530 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
5531 else if (map[i].br_startblock == DELAYSTARTBLOCK)
5532 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5533 out[cur_ext].bmv_offset =
5534 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5535 out[cur_ext].bmv_length =
5536 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5537 out[cur_ext].bmv_unused1 = 0;
5538 out[cur_ext].bmv_unused2 = 0;
5539 ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
5540 (map[i].br_startblock != DELAYSTARTBLOCK));
5541 if (map[i].br_startblock == HOLESTARTBLOCK &&
5542 whichfork == XFS_ATTR_FORK) {
5543 /* came to the end of attribute fork */
5544 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
5548 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5550 map[i].br_startblock))
5554 out[cur_ext].bmv_offset +
5555 out[cur_ext].bmv_length;
5557 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
5560 * In case we don't want to return the hole,
5561 * don't increase cur_ext so that we can reuse
5562 * it in the next loop.
5564 if ((iflags & BMV_IF_NO_HOLES) &&
5565 map[i].br_startblock == HOLESTARTBLOCK) {
5566 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
5574 } while (nmap && nexleft && bmv->bmv_length);
5579 xfs_iunlock_map_shared(ip, lock);
5581 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5583 for (i = 0; i < cur_ext; i++) {
5584 int full = 0; /* user array is full */
5586 /* format results & advance arg */
5587 error = formatter(&arg, &out[i], &full);
5597 * Check the last inode extent to determine whether this allocation will result
5598 * in blocks being allocated at the end of the file. When we allocate new data
5599 * blocks at the end of the file which do not start at the previous data block,
5600 * we will try to align the new blocks at stripe unit boundaries.
5602 STATIC int /* error */
5604 xfs_inode_t *ip, /* incore inode pointer */
5605 xfs_fileoff_t off, /* file offset in fsblocks */
5606 int whichfork, /* data or attribute fork */
5607 char *aeof) /* return value */
5609 int error; /* error return value */
5610 xfs_ifork_t *ifp; /* inode fork pointer */
5611 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
5612 xfs_extnum_t nextents; /* number of file extents */
5613 xfs_bmbt_irec_t s; /* expanded extent record */
5615 ASSERT(whichfork == XFS_DATA_FORK);
5616 ifp = XFS_IFORK_PTR(ip, whichfork);
5617 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5618 (error = xfs_iread_extents(NULL, ip, whichfork)))
5620 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5621 if (nextents == 0) {
5626 * Go to the last extent
5628 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
5629 xfs_bmbt_get_all(lastrec, &s);
5631 * Check we are allocating in the last extent (for delayed allocations)
5632 * or past the last extent for non-delayed allocations.
5634 *aeof = (off >= s.br_startoff &&
5635 off < s.br_startoff + s.br_blockcount &&
5636 isnullstartblock(s.br_startblock)) ||
5637 off >= s.br_startoff + s.br_blockcount;
5642 * Check if the endoff is outside the last extent. If so the caller will grow
5643 * the allocation to a stripe unit boundary.
5647 xfs_inode_t *ip, /* incore inode pointer */
5648 xfs_fileoff_t endoff, /* file offset in fsblocks */
5649 int whichfork, /* data or attribute fork */
5650 int *eof) /* result value */
5652 xfs_fsblock_t blockcount; /* extent block count */
5653 int error; /* error return value */
5654 xfs_ifork_t *ifp; /* inode fork pointer */
5655 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
5656 xfs_extnum_t nextents; /* number of file extents */
5657 xfs_fileoff_t startoff; /* extent starting file offset */
5659 ASSERT(whichfork == XFS_DATA_FORK);
5660 ifp = XFS_IFORK_PTR(ip, whichfork);
5661 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5662 (error = xfs_iread_extents(NULL, ip, whichfork)))
5664 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5665 if (nextents == 0) {
5670 * Go to the last extent
5672 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
5673 startoff = xfs_bmbt_get_startoff(lastrec);
5674 blockcount = xfs_bmbt_get_blockcount(lastrec);
5675 *eof = endoff >= startoff + blockcount;
5680 STATIC struct xfs_buf *
5682 struct xfs_btree_cur *cur,
5685 struct xfs_log_item_desc *lidp;
5691 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5692 if (!cur->bc_bufs[i])
5694 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
5695 return cur->bc_bufs[i];
5698 /* Chase down all the log items to see if the bp is there */
5699 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
5700 struct xfs_buf_log_item *bip;
5701 bip = (struct xfs_buf_log_item *)lidp->lid_item;
5702 if (bip->bli_item.li_type == XFS_LI_BUF &&
5703 XFS_BUF_ADDR(bip->bli_buf) == bno)
5704 return bip->bli_buf;
5712 struct xfs_btree_block *block,
5718 __be64 *pp, *thispa; /* pointer to block address */
5719 xfs_bmbt_key_t *prevp, *keyp;
5721 ASSERT(be16_to_cpu(block->bb_level) > 0);
5724 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
5725 dmxr = mp->m_bmap_dmxr[0];
5726 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
5729 ASSERT(be64_to_cpu(prevp->br_startoff) <
5730 be64_to_cpu(keyp->br_startoff));
5735 * Compare the block numbers to see if there are dups.
5738 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
5740 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5742 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
5744 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
5746 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
5747 if (*thispa == *pp) {
5748 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
5750 (unsigned long long)be64_to_cpu(*thispa));
5751 panic("%s: ptrs are equal in node\n",
5759 * Check that the extents for the inode ip are in the right order in all
5764 xfs_bmap_check_leaf_extents(
5765 xfs_btree_cur_t *cur, /* btree cursor or null */
5766 xfs_inode_t *ip, /* incore inode pointer */
5767 int whichfork) /* data or attr fork */
5769 struct xfs_btree_block *block; /* current btree block */
5770 xfs_fsblock_t bno; /* block # of "block" */
5771 xfs_buf_t *bp; /* buffer for "block" */
5772 int error; /* error return value */
5773 xfs_extnum_t i=0, j; /* index into the extents list */
5774 xfs_ifork_t *ifp; /* fork structure */
5775 int level; /* btree level, for checking */
5776 xfs_mount_t *mp; /* file system mount structure */
5777 __be64 *pp; /* pointer to block address */
5778 xfs_bmbt_rec_t *ep; /* pointer to current extent */
5779 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
5780 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
5783 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5789 ifp = XFS_IFORK_PTR(ip, whichfork);
5790 block = ifp->if_broot;
5792 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5794 level = be16_to_cpu(block->bb_level);
5796 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
5797 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
5798 bno = be64_to_cpu(*pp);
5800 ASSERT(bno != NULLDFSBNO);
5801 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5802 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5805 * Go down the tree until leaf level is reached, following the first
5806 * pointer (leftmost) at each level.
5808 while (level-- > 0) {
5809 /* See if buf is in cur first */
5810 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5816 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5817 XFS_BMAP_BTREE_REF)))
5819 block = XFS_BUF_TO_BLOCK(bp);
5820 XFS_WANT_CORRUPTED_GOTO(
5821 xfs_bmap_sanity_check(mp, bp, level),
5827 * Check this block for basic sanity (increasing keys and
5828 * no duplicate blocks).
5831 xfs_check_block(block, mp, 0, 0);
5832 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
5833 bno = be64_to_cpu(*pp);
5834 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
5837 xfs_trans_brelse(NULL, bp);
5842 * Here with bp and block set to the leftmost leaf node in the tree.
5847 * Loop over all leaf nodes checking that all extents are in the right order.
5850 xfs_fsblock_t nextbno;
5851 xfs_extnum_t num_recs;
5854 num_recs = xfs_btree_get_numrecs(block);
5857 * Read-ahead the next leaf block, if any.
5860 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
5863 * Check all the extents to make sure they are OK.
5864 * If we had a previous block, the last entry should
5865 * conform with the first entry in this one.
5868 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
5870 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
5871 xfs_bmbt_disk_get_blockcount(&last) <=
5872 xfs_bmbt_disk_get_startoff(ep));
5874 for (j = 1; j < num_recs; j++) {
5875 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
5876 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
5877 xfs_bmbt_disk_get_blockcount(ep) <=
5878 xfs_bmbt_disk_get_startoff(nextp));
5886 xfs_trans_brelse(NULL, bp);
5890 * If we've reached the end, stop.
5892 if (bno == NULLFSBLOCK)
5895 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5901 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5902 XFS_BMAP_BTREE_REF)))
5904 block = XFS_BUF_TO_BLOCK(bp);
5908 xfs_trans_brelse(NULL, bp);
5913 cmn_err(CE_WARN, "%s: at error0", __func__);
5915 xfs_trans_brelse(NULL, bp);
5917 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
5919 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
5925 * Count fsblocks of the given fork.
5928 xfs_bmap_count_blocks(
5929 xfs_trans_t *tp, /* transaction pointer */
5930 xfs_inode_t *ip, /* incore inode */
5931 int whichfork, /* data or attr fork */
5932 int *count) /* out: count of blocks */
5934 struct xfs_btree_block *block; /* current btree block */
5935 xfs_fsblock_t bno; /* block # of "block" */
5936 xfs_ifork_t *ifp; /* fork structure */
5937 int level; /* btree level, for checking */
5938 xfs_mount_t *mp; /* file system mount structure */
5939 __be64 *pp; /* pointer to block address */
5943 ifp = XFS_IFORK_PTR(ip, whichfork);
5944 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
5945 xfs_bmap_count_leaves(ifp, 0,
5946 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
5952 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5954 block = ifp->if_broot;
5955 level = be16_to_cpu(block->bb_level);
5957 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
5958 bno = be64_to_cpu(*pp);
5959 ASSERT(bno != NULLDFSBNO);
5960 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5961 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5963 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
5964 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
5966 return XFS_ERROR(EFSCORRUPTED);
5973 * Recursively walks each level of a btree
5974 * to count total fsblocks is use.
5976 STATIC int /* error */
5977 xfs_bmap_count_tree(
5978 xfs_mount_t *mp, /* file system mount point */
5979 xfs_trans_t *tp, /* transaction pointer */
5980 xfs_ifork_t *ifp, /* inode fork pointer */
5981 xfs_fsblock_t blockno, /* file system block number */
5982 int levelin, /* level in btree */
5983 int *count) /* Count of blocks */
5986 xfs_buf_t *bp, *nbp;
5987 int level = levelin;
5989 xfs_fsblock_t bno = blockno;
5990 xfs_fsblock_t nextbno;
5991 struct xfs_btree_block *block, *nextblock;
5994 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
5997 block = XFS_BUF_TO_BLOCK(bp);
6000 /* Not at node above leaves, count this level of nodes */
6001 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6002 while (nextbno != NULLFSBLOCK) {
6003 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6004 0, &nbp, XFS_BMAP_BTREE_REF)))
6007 nextblock = XFS_BUF_TO_BLOCK(nbp);
6008 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
6009 xfs_trans_brelse(tp, nbp);
6012 /* Dive to the next level */
6013 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
6014 bno = be64_to_cpu(*pp);
6015 if (unlikely((error =
6016 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
6017 xfs_trans_brelse(tp, bp);
6018 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6019 XFS_ERRLEVEL_LOW, mp);
6020 return XFS_ERROR(EFSCORRUPTED);
6022 xfs_trans_brelse(tp, bp);
6024 /* count all level 1 nodes and their leaves */
6026 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6027 numrecs = be16_to_cpu(block->bb_numrecs);
6028 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
6029 xfs_trans_brelse(tp, bp);
6030 if (nextbno == NULLFSBLOCK)
6033 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6034 XFS_BMAP_BTREE_REF)))
6037 block = XFS_BUF_TO_BLOCK(bp);
6044 * Count leaf blocks given a range of extent records.
6047 xfs_bmap_count_leaves(
6055 for (b = 0; b < numrecs; b++) {
6056 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
6057 *count += xfs_bmbt_get_blockcount(frp);
6062 * Count leaf blocks given a range of extent records originally
6066 xfs_bmap_disk_count_leaves(
6067 struct xfs_mount *mp,
6068 struct xfs_btree_block *block,
6073 xfs_bmbt_rec_t *frp;
6075 for (b = 1; b <= numrecs; b++) {
6076 frp = XFS_BMBT_REC_ADDR(mp, block, b);
6077 *count += xfs_bmbt_disk_get_blockcount(frp);
6082 * dead simple method of punching delalyed allocation blocks from a range in
6083 * the inode. Walks a block at a time so will be slow, but is only executed in
6084 * rare error cases so the overhead is not critical. This will alays punch out
6085 * both the start and end blocks, even if the ranges only partially overlap
6086 * them, so it is up to the caller to ensure that partial blocks are not
6090 xfs_bmap_punch_delalloc_range(
6091 struct xfs_inode *ip,
6092 xfs_fileoff_t start_fsb,
6093 xfs_fileoff_t length)
6095 xfs_fileoff_t remaining = length;
6098 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6102 xfs_bmbt_irec_t imap;
6104 xfs_fsblock_t firstblock;
6105 xfs_bmap_free_t flist;
6108 * Map the range first and check that it is a delalloc extent
6109 * before trying to unmap the range. Otherwise we will be
6110 * trying to remove a real extent (which requires a
6111 * transaction) or a hole, which is probably a bad idea...
6113 error = xfs_bmapi(NULL, ip, start_fsb, 1,
6114 XFS_BMAPI_ENTIRE, NULL, 0, &imap,
6118 /* something screwed, just bail */
6119 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
6120 xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
6121 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6122 ip->i_ino, start_fsb);
6130 if (imap.br_startblock != DELAYSTARTBLOCK) {
6131 /* been converted, ignore */
6134 WARN_ON(imap.br_blockcount == 0);
6137 * Note: while we initialise the firstblock/flist pair, they
6138 * should never be used because blocks should never be
6139 * allocated or freed for a delalloc extent and hence we need
6140 * don't cancel or finish them after the xfs_bunmapi() call.
6142 xfs_bmap_init(&flist, &firstblock);
6143 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6148 ASSERT(!flist.xbf_count && !flist.xbf_first);
6152 } while(remaining > 0);