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_dinode.h"
33 #include "xfs_inode.h"
34 #include "xfs_btree.h"
35 #include "xfs_mount.h"
36 #include "xfs_itable.h"
37 #include "xfs_inode_item.h"
38 #include "xfs_extfree_item.h"
39 #include "xfs_alloc.h"
41 #include "xfs_rtalloc.h"
42 #include "xfs_error.h"
43 #include "xfs_attr_leaf.h"
45 #include "xfs_quota.h"
46 #include "xfs_trans_space.h"
47 #include "xfs_buf_item.h"
48 #include "xfs_filestream.h"
49 #include "xfs_vnodeops.h"
50 #include "xfs_trace.h"
53 kmem_zone_t *xfs_bmap_free_item_zone;
56 * Prototypes for internal bmap routines.
61 xfs_bmap_check_leaf_extents(
62 struct xfs_btree_cur *cur,
66 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
71 * Called from xfs_bmap_add_attrfork to handle extents format files.
73 STATIC int /* error */
74 xfs_bmap_add_attrfork_extents(
75 xfs_trans_t *tp, /* transaction pointer */
76 xfs_inode_t *ip, /* incore inode pointer */
77 xfs_fsblock_t *firstblock, /* first block allocated */
78 xfs_bmap_free_t *flist, /* blocks to free at commit */
79 int *flags); /* inode logging flags */
82 * Called from xfs_bmap_add_attrfork to handle local format files.
84 STATIC int /* error */
85 xfs_bmap_add_attrfork_local(
86 xfs_trans_t *tp, /* transaction pointer */
87 xfs_inode_t *ip, /* incore inode pointer */
88 xfs_fsblock_t *firstblock, /* first block allocated */
89 xfs_bmap_free_t *flist, /* blocks to free at commit */
90 int *flags); /* inode logging flags */
93 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
94 * It figures out where to ask the underlying allocator to put the new extent.
96 STATIC int /* error */
98 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
101 * Transform a btree format file with only one leaf node, where the
102 * extents list will fit in the inode, into an extents format file.
103 * Since the file extents are already in-core, all we have to do is
104 * give up the space for the btree root and pitch the leaf block.
106 STATIC int /* error */
107 xfs_bmap_btree_to_extents(
108 xfs_trans_t *tp, /* transaction pointer */
109 xfs_inode_t *ip, /* incore inode pointer */
110 xfs_btree_cur_t *cur, /* btree cursor */
111 int *logflagsp, /* inode logging flags */
112 int whichfork); /* data or attr fork */
115 * Remove the entry "free" from the free item list. Prev points to the
116 * previous entry, unless "free" is the head of the list.
120 xfs_bmap_free_t *flist, /* free item list header */
121 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
122 xfs_bmap_free_item_t *free); /* list item to be freed */
125 * Convert an extents-format file into a btree-format file.
126 * The new file will have a root block (in the inode) and a single child block.
128 STATIC int /* error */
129 xfs_bmap_extents_to_btree(
130 xfs_trans_t *tp, /* transaction pointer */
131 xfs_inode_t *ip, /* incore inode pointer */
132 xfs_fsblock_t *firstblock, /* first-block-allocated */
133 xfs_bmap_free_t *flist, /* blocks freed in xaction */
134 xfs_btree_cur_t **curp, /* cursor returned to caller */
135 int wasdel, /* converting a delayed alloc */
136 int *logflagsp, /* inode logging flags */
137 int whichfork); /* data or attr fork */
140 * Convert a local file to an extents file.
141 * This code is sort of bogus, since the file data needs to get
142 * logged so it won't be lost. The bmap-level manipulations are ok, though.
144 STATIC int /* error */
145 xfs_bmap_local_to_extents(
146 xfs_trans_t *tp, /* transaction pointer */
147 xfs_inode_t *ip, /* incore inode pointer */
148 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
149 xfs_extlen_t total, /* total blocks needed by transaction */
150 int *logflagsp, /* inode logging flags */
151 int whichfork); /* data or attr fork */
154 * Search the extents list for the inode, for the extent containing bno.
155 * If bno lies in a hole, point to the next entry. If bno lies past eof,
156 * *eofp will be set, and *prevp will contain the last entry (null if none).
157 * Else, *lastxp will be set to the index of the found
158 * entry; *gotp will contain the entry.
160 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
161 xfs_bmap_search_extents(
162 xfs_inode_t *ip, /* incore inode pointer */
163 xfs_fileoff_t bno, /* block number searched for */
164 int whichfork, /* data or attr fork */
165 int *eofp, /* out: end of file found */
166 xfs_extnum_t *lastxp, /* out: last extent index */
167 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
168 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
171 * Compute the worst-case number of indirect blocks that will be used
172 * for ip's delayed extent of length "len".
175 xfs_bmap_worst_indlen(
176 xfs_inode_t *ip, /* incore inode pointer */
177 xfs_filblks_t len); /* delayed extent length */
181 * Perform various validation checks on the values being returned
185 xfs_bmap_validate_ret(
189 xfs_bmbt_irec_t *mval,
193 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
201 xfs_fsblock_t blockno,
206 xfs_bmap_count_leaves(
213 xfs_bmap_disk_count_leaves(
214 struct xfs_mount *mp,
215 struct xfs_btree_block *block,
220 * Bmap internal routines.
223 STATIC int /* error */
225 struct xfs_btree_cur *cur,
229 int *stat) /* success/failure */
231 cur->bc_rec.b.br_startoff = off;
232 cur->bc_rec.b.br_startblock = bno;
233 cur->bc_rec.b.br_blockcount = len;
234 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
237 STATIC int /* error */
239 struct xfs_btree_cur *cur,
243 int *stat) /* success/failure */
245 cur->bc_rec.b.br_startoff = off;
246 cur->bc_rec.b.br_startblock = bno;
247 cur->bc_rec.b.br_blockcount = len;
248 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
252 * Update the record referred to by cur to the value given
253 * by [off, bno, len, state].
254 * This either works (return 0) or gets an EFSCORRUPTED error.
258 struct xfs_btree_cur *cur,
264 union xfs_btree_rec rec;
266 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
267 return xfs_btree_update(cur, &rec);
271 * Called from xfs_bmap_add_attrfork to handle btree format files.
273 STATIC int /* error */
274 xfs_bmap_add_attrfork_btree(
275 xfs_trans_t *tp, /* transaction pointer */
276 xfs_inode_t *ip, /* incore inode pointer */
277 xfs_fsblock_t *firstblock, /* first block allocated */
278 xfs_bmap_free_t *flist, /* blocks to free at commit */
279 int *flags) /* inode logging flags */
281 xfs_btree_cur_t *cur; /* btree cursor */
282 int error; /* error return value */
283 xfs_mount_t *mp; /* file system mount struct */
284 int stat; /* newroot status */
287 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
288 *flags |= XFS_ILOG_DBROOT;
290 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
291 cur->bc_private.b.flist = flist;
292 cur->bc_private.b.firstblock = *firstblock;
293 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
295 /* must be at least one entry */
296 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
297 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
300 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
301 return XFS_ERROR(ENOSPC);
303 *firstblock = cur->bc_private.b.firstblock;
304 cur->bc_private.b.allocated = 0;
305 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
309 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
314 * Called from xfs_bmap_add_attrfork to handle extents format files.
316 STATIC int /* error */
317 xfs_bmap_add_attrfork_extents(
318 xfs_trans_t *tp, /* transaction pointer */
319 xfs_inode_t *ip, /* incore inode pointer */
320 xfs_fsblock_t *firstblock, /* first block allocated */
321 xfs_bmap_free_t *flist, /* blocks to free at commit */
322 int *flags) /* inode logging flags */
324 xfs_btree_cur_t *cur; /* bmap btree cursor */
325 int error; /* error return value */
327 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
330 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
331 flags, XFS_DATA_FORK);
333 cur->bc_private.b.allocated = 0;
334 xfs_btree_del_cursor(cur,
335 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
341 * Called from xfs_bmap_add_attrfork to handle local format files.
343 STATIC int /* error */
344 xfs_bmap_add_attrfork_local(
345 xfs_trans_t *tp, /* transaction pointer */
346 xfs_inode_t *ip, /* incore inode pointer */
347 xfs_fsblock_t *firstblock, /* first block allocated */
348 xfs_bmap_free_t *flist, /* blocks to free at commit */
349 int *flags) /* inode logging flags */
351 xfs_da_args_t dargs; /* args for dir/attr code */
352 int error; /* error return value */
353 xfs_mount_t *mp; /* mount structure pointer */
355 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
357 if (S_ISDIR(ip->i_d.di_mode)) {
359 memset(&dargs, 0, sizeof(dargs));
361 dargs.firstblock = firstblock;
363 dargs.total = mp->m_dirblkfsbs;
364 dargs.whichfork = XFS_DATA_FORK;
366 error = xfs_dir2_sf_to_block(&dargs);
368 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
374 * Convert a delayed allocation to a real allocation.
376 STATIC int /* error */
377 xfs_bmap_add_extent_delay_real(
378 struct xfs_bmalloca *bma)
380 struct xfs_bmbt_irec *new = &bma->got;
381 int diff; /* temp value */
382 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
383 int error; /* error return value */
384 int i; /* temp state */
385 xfs_ifork_t *ifp; /* inode fork pointer */
386 xfs_fileoff_t new_endoff; /* end offset of new entry */
387 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
388 /* left is 0, right is 1, prev is 2 */
389 int rval=0; /* return value (logging flags) */
390 int state = 0;/* state bits, accessed thru macros */
391 xfs_filblks_t da_new; /* new count del alloc blocks used */
392 xfs_filblks_t da_old; /* old count del alloc blocks used */
393 xfs_filblks_t temp=0; /* value for da_new calculations */
394 xfs_filblks_t temp2=0;/* value for da_new calculations */
395 int tmp_rval; /* partial logging flags */
397 ifp = XFS_IFORK_PTR(bma->ip, XFS_DATA_FORK);
399 ASSERT(bma->idx >= 0);
400 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
401 ASSERT(!isnullstartblock(new->br_startblock));
403 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
405 XFS_STATS_INC(xs_add_exlist);
412 * Set up a bunch of variables to make the tests simpler.
414 ep = xfs_iext_get_ext(ifp, bma->idx);
415 xfs_bmbt_get_all(ep, &PREV);
416 new_endoff = new->br_startoff + new->br_blockcount;
417 ASSERT(PREV.br_startoff <= new->br_startoff);
418 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
420 da_old = startblockval(PREV.br_startblock);
424 * Set flags determining what part of the previous delayed allocation
425 * extent is being replaced by a real allocation.
427 if (PREV.br_startoff == new->br_startoff)
428 state |= BMAP_LEFT_FILLING;
429 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
430 state |= BMAP_RIGHT_FILLING;
433 * Check and set flags if this segment has a left neighbor.
434 * Don't set contiguous if the combined extent would be too large.
437 state |= BMAP_LEFT_VALID;
438 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
440 if (isnullstartblock(LEFT.br_startblock))
441 state |= BMAP_LEFT_DELAY;
444 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
445 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
446 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
447 LEFT.br_state == new->br_state &&
448 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
449 state |= BMAP_LEFT_CONTIG;
452 * Check and set flags if this segment has a right neighbor.
453 * Don't set contiguous if the combined extent would be too large.
454 * Also check for all-three-contiguous being too large.
456 if (bma->idx < bma->ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
457 state |= BMAP_RIGHT_VALID;
458 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
460 if (isnullstartblock(RIGHT.br_startblock))
461 state |= BMAP_RIGHT_DELAY;
464 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
465 new_endoff == RIGHT.br_startoff &&
466 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
467 new->br_state == RIGHT.br_state &&
468 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
469 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
470 BMAP_RIGHT_FILLING)) !=
471 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
472 BMAP_RIGHT_FILLING) ||
473 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
475 state |= BMAP_RIGHT_CONTIG;
479 * Switch out based on the FILLING and CONTIG state bits.
481 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
482 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
483 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
484 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
486 * Filling in all of a previously delayed allocation extent.
487 * The left and right neighbors are both contiguous with new.
490 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
491 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
492 LEFT.br_blockcount + PREV.br_blockcount +
493 RIGHT.br_blockcount);
494 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
496 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
497 bma->ip->i_d.di_nextents--;
498 if (bma->cur == NULL)
499 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
501 rval = XFS_ILOG_CORE;
502 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
504 RIGHT.br_blockcount, &i);
507 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
508 error = xfs_btree_delete(bma->cur, &i);
511 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
512 error = xfs_btree_decrement(bma->cur, 0, &i);
515 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
516 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
520 RIGHT.br_blockcount, LEFT.br_state);
526 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
528 * Filling in all of a previously delayed allocation extent.
529 * The left neighbor is contiguous, the right is not.
533 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
534 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
535 LEFT.br_blockcount + PREV.br_blockcount);
536 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
538 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
539 if (bma->cur == NULL)
540 rval = XFS_ILOG_DEXT;
543 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
544 LEFT.br_startblock, LEFT.br_blockcount,
548 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
549 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
552 PREV.br_blockcount, LEFT.br_state);
558 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
560 * Filling in all of a previously delayed allocation extent.
561 * The right neighbor is contiguous, the left is not.
563 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
564 xfs_bmbt_set_startblock(ep, new->br_startblock);
565 xfs_bmbt_set_blockcount(ep,
566 PREV.br_blockcount + RIGHT.br_blockcount);
567 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
569 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
570 if (bma->cur == NULL)
571 rval = XFS_ILOG_DEXT;
574 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
576 RIGHT.br_blockcount, &i);
579 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
580 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
583 RIGHT.br_blockcount, PREV.br_state);
589 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
591 * Filling in all of a previously delayed allocation extent.
592 * Neither the left nor right neighbors are contiguous with
595 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
596 xfs_bmbt_set_startblock(ep, new->br_startblock);
597 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
599 bma->ip->i_d.di_nextents++;
600 if (bma->cur == NULL)
601 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
603 rval = XFS_ILOG_CORE;
604 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
605 new->br_startblock, new->br_blockcount,
609 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
610 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
611 error = xfs_btree_insert(bma->cur, &i);
614 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
618 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
620 * Filling in the first part of a previous delayed allocation.
621 * The left neighbor is contiguous.
623 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
624 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
625 LEFT.br_blockcount + new->br_blockcount);
626 xfs_bmbt_set_startoff(ep,
627 PREV.br_startoff + new->br_blockcount);
628 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
630 temp = PREV.br_blockcount - new->br_blockcount;
631 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
632 xfs_bmbt_set_blockcount(ep, temp);
633 if (bma->cur == NULL)
634 rval = XFS_ILOG_DEXT;
637 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
638 LEFT.br_startblock, LEFT.br_blockcount,
642 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
643 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
651 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
652 startblockval(PREV.br_startblock));
653 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
654 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
659 case BMAP_LEFT_FILLING:
661 * Filling in the first part of a previous delayed allocation.
662 * The left neighbor is not contiguous.
664 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
665 xfs_bmbt_set_startoff(ep, new_endoff);
666 temp = PREV.br_blockcount - new->br_blockcount;
667 xfs_bmbt_set_blockcount(ep, temp);
668 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
669 bma->ip->i_d.di_nextents++;
670 if (bma->cur == NULL)
671 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
673 rval = XFS_ILOG_CORE;
674 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
675 new->br_startblock, new->br_blockcount,
679 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
680 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
681 error = xfs_btree_insert(bma->cur, &i);
684 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
686 if (bma->ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
687 bma->ip->i_d.di_nextents > bma->ip->i_df.if_ext_max) {
688 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
689 bma->firstblock, bma->flist,
690 &bma->cur, 1, &tmp_rval, XFS_DATA_FORK);
695 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
696 startblockval(PREV.br_startblock) -
697 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
698 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
699 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
700 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
703 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
705 * Filling in the last part of a previous delayed allocation.
706 * The right neighbor is contiguous with the new allocation.
708 temp = PREV.br_blockcount - new->br_blockcount;
709 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
710 xfs_bmbt_set_blockcount(ep, temp);
711 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
712 new->br_startoff, new->br_startblock,
713 new->br_blockcount + RIGHT.br_blockcount,
715 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
716 if (bma->cur == NULL)
717 rval = XFS_ILOG_DEXT;
720 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
722 RIGHT.br_blockcount, &i);
725 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
726 error = xfs_bmbt_update(bma->cur, new->br_startoff,
735 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
736 startblockval(PREV.br_startblock));
737 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
738 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
739 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
744 case BMAP_RIGHT_FILLING:
746 * Filling in the last part of a previous delayed allocation.
747 * The right neighbor is not contiguous.
749 temp = PREV.br_blockcount - new->br_blockcount;
750 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
751 xfs_bmbt_set_blockcount(ep, temp);
752 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
753 bma->ip->i_d.di_nextents++;
754 if (bma->cur == NULL)
755 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
757 rval = XFS_ILOG_CORE;
758 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
759 new->br_startblock, new->br_blockcount,
763 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
764 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
765 error = xfs_btree_insert(bma->cur, &i);
768 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
770 if (bma->ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
771 bma->ip->i_d.di_nextents > bma->ip->i_df.if_ext_max) {
772 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
773 bma->firstblock, bma->flist, &bma->cur, 1,
774 &tmp_rval, XFS_DATA_FORK);
779 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
780 startblockval(PREV.br_startblock) -
781 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
782 ep = xfs_iext_get_ext(ifp, bma->idx);
783 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
784 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
791 * Filling in the middle part of a previous delayed allocation.
792 * Contiguity is impossible here.
793 * This case is avoided almost all the time.
795 * We start with a delayed allocation:
797 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
800 * and we are allocating:
801 * +rrrrrrrrrrrrrrrrr+
804 * and we set it up for insertion as:
805 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
807 * PREV @ idx LEFT RIGHT
808 * inserted at idx + 1
810 temp = new->br_startoff - PREV.br_startoff;
811 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
812 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
813 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
815 RIGHT.br_state = PREV.br_state;
816 RIGHT.br_startblock = nullstartblock(
817 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
818 RIGHT.br_startoff = new_endoff;
819 RIGHT.br_blockcount = temp2;
820 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
821 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
822 bma->ip->i_d.di_nextents++;
823 if (bma->cur == NULL)
824 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
826 rval = XFS_ILOG_CORE;
827 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
828 new->br_startblock, new->br_blockcount,
832 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
833 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
834 error = xfs_btree_insert(bma->cur, &i);
837 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
839 if (bma->ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
840 bma->ip->i_d.di_nextents > bma->ip->i_df.if_ext_max) {
841 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
842 bma->firstblock, bma->flist, &bma->cur,
843 1, &tmp_rval, XFS_DATA_FORK);
848 temp = xfs_bmap_worst_indlen(bma->ip, temp);
849 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
850 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
851 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
853 error = xfs_icsb_modify_counters(bma->ip->i_mount,
855 -((int64_t)diff), 0);
861 ep = xfs_iext_get_ext(ifp, bma->idx);
862 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
863 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
864 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
865 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
866 nullstartblock((int)temp2));
867 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
870 da_new = temp + temp2;
873 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
874 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
875 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
876 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
877 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
878 case BMAP_LEFT_CONTIG:
879 case BMAP_RIGHT_CONTIG:
881 * These cases are all impossible.
886 /* convert to a btree if necessary */
887 if (XFS_IFORK_FORMAT(bma->ip, XFS_DATA_FORK) == XFS_DINODE_FMT_EXTENTS &&
888 XFS_IFORK_NEXTENTS(bma->ip, XFS_DATA_FORK) > ifp->if_ext_max) {
889 int tmp_logflags; /* partial log flag return val */
891 ASSERT(bma->cur == NULL);
892 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
893 bma->firstblock, bma->flist, &bma->cur,
894 da_old > 0, &tmp_logflags, XFS_DATA_FORK);
895 bma->logflags |= tmp_logflags;
900 /* adjust for changes in reserved delayed indirect blocks */
901 if (da_old || da_new) {
904 temp += bma->cur->bc_private.b.allocated;
905 ASSERT(temp <= da_old);
907 xfs_icsb_modify_counters(bma->ip->i_mount,
909 (int64_t)(da_old - temp), 0);
912 /* clear out the allocated field, done with it now in any case. */
914 bma->cur->bc_private.b.allocated = 0;
916 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, XFS_DATA_FORK);
918 bma->logflags |= rval;
926 * Convert an unwritten allocation to a real allocation or vice versa.
928 STATIC int /* error */
929 xfs_bmap_add_extent_unwritten_real(
930 struct xfs_trans *tp,
931 xfs_inode_t *ip, /* incore inode pointer */
932 xfs_extnum_t *idx, /* extent number to update/insert */
933 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
934 xfs_bmbt_irec_t *new, /* new data to add to file extents */
935 xfs_fsblock_t *first, /* pointer to firstblock variable */
936 xfs_bmap_free_t *flist, /* list of extents to be freed */
937 int *logflagsp) /* inode logging flags */
939 xfs_btree_cur_t *cur; /* btree cursor */
940 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
941 int error; /* error return value */
942 int i; /* temp state */
943 xfs_ifork_t *ifp; /* inode fork pointer */
944 xfs_fileoff_t new_endoff; /* end offset of new entry */
945 xfs_exntst_t newext; /* new extent state */
946 xfs_exntst_t oldext; /* old extent state */
947 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
948 /* left is 0, right is 1, prev is 2 */
949 int rval=0; /* return value (logging flags) */
950 int state = 0;/* state bits, accessed thru macros */
955 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
958 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
959 ASSERT(!isnullstartblock(new->br_startblock));
961 XFS_STATS_INC(xs_add_exlist);
968 * Set up a bunch of variables to make the tests simpler.
971 ep = xfs_iext_get_ext(ifp, *idx);
972 xfs_bmbt_get_all(ep, &PREV);
973 newext = new->br_state;
974 oldext = (newext == XFS_EXT_UNWRITTEN) ?
975 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
976 ASSERT(PREV.br_state == oldext);
977 new_endoff = new->br_startoff + new->br_blockcount;
978 ASSERT(PREV.br_startoff <= new->br_startoff);
979 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
982 * Set flags determining what part of the previous oldext allocation
983 * extent is being replaced by a newext allocation.
985 if (PREV.br_startoff == new->br_startoff)
986 state |= BMAP_LEFT_FILLING;
987 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
988 state |= BMAP_RIGHT_FILLING;
991 * Check and set flags if this segment has a left neighbor.
992 * Don't set contiguous if the combined extent would be too large.
995 state |= BMAP_LEFT_VALID;
996 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
998 if (isnullstartblock(LEFT.br_startblock))
999 state |= BMAP_LEFT_DELAY;
1002 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1003 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1004 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1005 LEFT.br_state == newext &&
1006 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1007 state |= BMAP_LEFT_CONTIG;
1010 * Check and set flags if this segment has a right neighbor.
1011 * Don't set contiguous if the combined extent would be too large.
1012 * Also check for all-three-contiguous being too large.
1014 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1015 state |= BMAP_RIGHT_VALID;
1016 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
1017 if (isnullstartblock(RIGHT.br_startblock))
1018 state |= BMAP_RIGHT_DELAY;
1021 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1022 new_endoff == RIGHT.br_startoff &&
1023 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1024 newext == RIGHT.br_state &&
1025 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1026 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1027 BMAP_RIGHT_FILLING)) !=
1028 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1029 BMAP_RIGHT_FILLING) ||
1030 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1032 state |= BMAP_RIGHT_CONTIG;
1035 * Switch out based on the FILLING and CONTIG state bits.
1037 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1038 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1039 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1040 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1042 * Setting all of a previous oldext extent to newext.
1043 * The left and right neighbors are both contiguous with new.
1047 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1048 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1049 LEFT.br_blockcount + PREV.br_blockcount +
1050 RIGHT.br_blockcount);
1051 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1053 xfs_iext_remove(ip, *idx + 1, 2, state);
1054 ip->i_d.di_nextents -= 2;
1056 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1058 rval = XFS_ILOG_CORE;
1059 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1060 RIGHT.br_startblock,
1061 RIGHT.br_blockcount, &i)))
1063 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1064 if ((error = xfs_btree_delete(cur, &i)))
1066 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1067 if ((error = xfs_btree_decrement(cur, 0, &i)))
1069 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1070 if ((error = xfs_btree_delete(cur, &i)))
1072 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1073 if ((error = xfs_btree_decrement(cur, 0, &i)))
1075 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1076 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1078 LEFT.br_blockcount + PREV.br_blockcount +
1079 RIGHT.br_blockcount, LEFT.br_state)))
1084 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1086 * Setting all of a previous oldext extent to newext.
1087 * The left neighbor is contiguous, the right is not.
1091 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1092 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1093 LEFT.br_blockcount + PREV.br_blockcount);
1094 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1096 xfs_iext_remove(ip, *idx + 1, 1, state);
1097 ip->i_d.di_nextents--;
1099 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1101 rval = XFS_ILOG_CORE;
1102 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1103 PREV.br_startblock, PREV.br_blockcount,
1106 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1107 if ((error = xfs_btree_delete(cur, &i)))
1109 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1110 if ((error = xfs_btree_decrement(cur, 0, &i)))
1112 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1113 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1115 LEFT.br_blockcount + PREV.br_blockcount,
1121 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1123 * Setting all of a previous oldext extent to newext.
1124 * The right neighbor is contiguous, the left is not.
1126 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1127 xfs_bmbt_set_blockcount(ep,
1128 PREV.br_blockcount + RIGHT.br_blockcount);
1129 xfs_bmbt_set_state(ep, newext);
1130 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1131 xfs_iext_remove(ip, *idx + 1, 1, state);
1132 ip->i_d.di_nextents--;
1134 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1136 rval = XFS_ILOG_CORE;
1137 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1138 RIGHT.br_startblock,
1139 RIGHT.br_blockcount, &i)))
1141 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1142 if ((error = xfs_btree_delete(cur, &i)))
1144 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1145 if ((error = xfs_btree_decrement(cur, 0, &i)))
1147 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1148 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1150 new->br_blockcount + RIGHT.br_blockcount,
1156 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1158 * Setting all of a previous oldext extent to newext.
1159 * Neither the left nor right neighbors are contiguous with
1162 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1163 xfs_bmbt_set_state(ep, newext);
1164 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1167 rval = XFS_ILOG_DEXT;
1170 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1171 new->br_startblock, new->br_blockcount,
1174 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1175 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1176 new->br_startblock, new->br_blockcount,
1182 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1184 * Setting the first part of a previous oldext extent to newext.
1185 * The left neighbor is contiguous.
1187 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
1188 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
1189 LEFT.br_blockcount + new->br_blockcount);
1190 xfs_bmbt_set_startoff(ep,
1191 PREV.br_startoff + new->br_blockcount);
1192 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
1194 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1195 xfs_bmbt_set_startblock(ep,
1196 new->br_startblock + new->br_blockcount);
1197 xfs_bmbt_set_blockcount(ep,
1198 PREV.br_blockcount - new->br_blockcount);
1199 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1204 rval = XFS_ILOG_DEXT;
1207 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1208 PREV.br_startblock, PREV.br_blockcount,
1211 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1212 if ((error = xfs_bmbt_update(cur,
1213 PREV.br_startoff + new->br_blockcount,
1214 PREV.br_startblock + new->br_blockcount,
1215 PREV.br_blockcount - new->br_blockcount,
1218 if ((error = xfs_btree_decrement(cur, 0, &i)))
1220 error = xfs_bmbt_update(cur, LEFT.br_startoff,
1222 LEFT.br_blockcount + new->br_blockcount,
1229 case BMAP_LEFT_FILLING:
1231 * Setting the first part of a previous oldext extent to newext.
1232 * The left neighbor is not contiguous.
1234 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1235 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1236 xfs_bmbt_set_startoff(ep, new_endoff);
1237 xfs_bmbt_set_blockcount(ep,
1238 PREV.br_blockcount - new->br_blockcount);
1239 xfs_bmbt_set_startblock(ep,
1240 new->br_startblock + new->br_blockcount);
1241 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1243 xfs_iext_insert(ip, *idx, 1, new, state);
1244 ip->i_d.di_nextents++;
1246 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1248 rval = XFS_ILOG_CORE;
1249 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1250 PREV.br_startblock, PREV.br_blockcount,
1253 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1254 if ((error = xfs_bmbt_update(cur,
1255 PREV.br_startoff + new->br_blockcount,
1256 PREV.br_startblock + new->br_blockcount,
1257 PREV.br_blockcount - new->br_blockcount,
1260 cur->bc_rec.b = *new;
1261 if ((error = xfs_btree_insert(cur, &i)))
1263 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1267 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1269 * Setting the last part of a previous oldext extent to newext.
1270 * The right neighbor is contiguous with the new allocation.
1272 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1273 xfs_bmbt_set_blockcount(ep,
1274 PREV.br_blockcount - new->br_blockcount);
1275 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1279 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1280 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
1281 new->br_startoff, new->br_startblock,
1282 new->br_blockcount + RIGHT.br_blockcount, newext);
1283 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1286 rval = XFS_ILOG_DEXT;
1289 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1291 PREV.br_blockcount, &i)))
1293 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1294 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1296 PREV.br_blockcount - new->br_blockcount,
1299 if ((error = xfs_btree_increment(cur, 0, &i)))
1301 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1303 new->br_blockcount + RIGHT.br_blockcount,
1309 case BMAP_RIGHT_FILLING:
1311 * Setting the last part of a previous oldext extent to newext.
1312 * The right neighbor is not contiguous.
1314 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1315 xfs_bmbt_set_blockcount(ep,
1316 PREV.br_blockcount - new->br_blockcount);
1317 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1320 xfs_iext_insert(ip, *idx, 1, new, state);
1322 ip->i_d.di_nextents++;
1324 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1326 rval = XFS_ILOG_CORE;
1327 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1328 PREV.br_startblock, PREV.br_blockcount,
1331 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1332 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1334 PREV.br_blockcount - new->br_blockcount,
1337 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1338 new->br_startblock, new->br_blockcount,
1341 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1342 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1343 if ((error = xfs_btree_insert(cur, &i)))
1345 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1351 * Setting the middle part of a previous oldext extent to
1352 * newext. Contiguity is impossible here.
1353 * One extent becomes three extents.
1355 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1356 xfs_bmbt_set_blockcount(ep,
1357 new->br_startoff - PREV.br_startoff);
1358 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1361 r[1].br_startoff = new_endoff;
1362 r[1].br_blockcount =
1363 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1364 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1365 r[1].br_state = oldext;
1368 xfs_iext_insert(ip, *idx, 2, &r[0], state);
1370 ip->i_d.di_nextents += 2;
1372 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1374 rval = XFS_ILOG_CORE;
1375 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1376 PREV.br_startblock, PREV.br_blockcount,
1379 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1380 /* new right extent - oldext */
1381 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1382 r[1].br_startblock, r[1].br_blockcount,
1385 /* new left extent - oldext */
1386 cur->bc_rec.b = PREV;
1387 cur->bc_rec.b.br_blockcount =
1388 new->br_startoff - PREV.br_startoff;
1389 if ((error = xfs_btree_insert(cur, &i)))
1391 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1393 * Reset the cursor to the position of the new extent
1394 * we are about to insert as we can't trust it after
1395 * the previous insert.
1397 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1398 new->br_startblock, new->br_blockcount,
1401 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1402 /* new middle extent - newext */
1403 cur->bc_rec.b.br_state = new->br_state;
1404 if ((error = xfs_btree_insert(cur, &i)))
1406 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1410 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1411 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1412 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1413 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1414 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1415 case BMAP_LEFT_CONTIG:
1416 case BMAP_RIGHT_CONTIG:
1418 * These cases are all impossible.
1423 /* convert to a btree if necessary */
1424 if (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) == XFS_DINODE_FMT_EXTENTS &&
1425 XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) > ifp->if_ext_max) {
1426 int tmp_logflags; /* partial log flag return val */
1428 ASSERT(cur == NULL);
1429 error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur,
1430 0, &tmp_logflags, XFS_DATA_FORK);
1431 *logflagsp |= tmp_logflags;
1436 /* clear out the allocated field, done with it now in any case. */
1438 cur->bc_private.b.allocated = 0;
1442 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
1452 * Convert a hole to a delayed allocation.
1455 xfs_bmap_add_extent_hole_delay(
1456 xfs_inode_t *ip, /* incore inode pointer */
1457 xfs_extnum_t *idx, /* extent number to update/insert */
1458 xfs_bmbt_irec_t *new) /* new data to add to file extents */
1460 xfs_ifork_t *ifp; /* inode fork pointer */
1461 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1462 xfs_filblks_t newlen=0; /* new indirect size */
1463 xfs_filblks_t oldlen=0; /* old indirect size */
1464 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1465 int state; /* state bits, accessed thru macros */
1466 xfs_filblks_t temp=0; /* temp for indirect calculations */
1468 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1470 ASSERT(isnullstartblock(new->br_startblock));
1473 * Check and set flags if this segment has a left neighbor
1476 state |= BMAP_LEFT_VALID;
1477 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
1479 if (isnullstartblock(left.br_startblock))
1480 state |= BMAP_LEFT_DELAY;
1484 * Check and set flags if the current (right) segment exists.
1485 * If it doesn't exist, we're converting the hole at end-of-file.
1487 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1488 state |= BMAP_RIGHT_VALID;
1489 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
1491 if (isnullstartblock(right.br_startblock))
1492 state |= BMAP_RIGHT_DELAY;
1496 * Set contiguity flags on the left and right neighbors.
1497 * Don't let extents get too large, even if the pieces are contiguous.
1499 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1500 left.br_startoff + left.br_blockcount == new->br_startoff &&
1501 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1502 state |= BMAP_LEFT_CONTIG;
1504 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1505 new->br_startoff + new->br_blockcount == right.br_startoff &&
1506 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1507 (!(state & BMAP_LEFT_CONTIG) ||
1508 (left.br_blockcount + new->br_blockcount +
1509 right.br_blockcount <= MAXEXTLEN)))
1510 state |= BMAP_RIGHT_CONTIG;
1513 * Switch out based on the contiguity flags.
1515 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1516 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1518 * New allocation is contiguous with delayed allocations
1519 * on the left and on the right.
1520 * Merge all three into a single extent record.
1523 temp = left.br_blockcount + new->br_blockcount +
1524 right.br_blockcount;
1526 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1527 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
1528 oldlen = startblockval(left.br_startblock) +
1529 startblockval(new->br_startblock) +
1530 startblockval(right.br_startblock);
1531 newlen = xfs_bmap_worst_indlen(ip, temp);
1532 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
1533 nullstartblock((int)newlen));
1534 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1536 xfs_iext_remove(ip, *idx + 1, 1, state);
1539 case BMAP_LEFT_CONTIG:
1541 * New allocation is contiguous with a delayed allocation
1543 * Merge the new allocation with the left neighbor.
1546 temp = left.br_blockcount + new->br_blockcount;
1548 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1549 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
1550 oldlen = startblockval(left.br_startblock) +
1551 startblockval(new->br_startblock);
1552 newlen = xfs_bmap_worst_indlen(ip, temp);
1553 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
1554 nullstartblock((int)newlen));
1555 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1558 case BMAP_RIGHT_CONTIG:
1560 * New allocation is contiguous with a delayed allocation
1562 * Merge the new allocation with the right neighbor.
1564 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1565 temp = new->br_blockcount + right.br_blockcount;
1566 oldlen = startblockval(new->br_startblock) +
1567 startblockval(right.br_startblock);
1568 newlen = xfs_bmap_worst_indlen(ip, temp);
1569 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
1571 nullstartblock((int)newlen), temp, right.br_state);
1572 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1577 * New allocation is not contiguous with another
1578 * delayed allocation.
1579 * Insert a new entry.
1581 oldlen = newlen = 0;
1582 xfs_iext_insert(ip, *idx, 1, new, state);
1585 if (oldlen != newlen) {
1586 ASSERT(oldlen > newlen);
1587 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
1588 (int64_t)(oldlen - newlen), 0);
1590 * Nothing to do for disk quota accounting here.
1596 * Convert a hole to a real allocation.
1598 STATIC int /* error */
1599 xfs_bmap_add_extent_hole_real(
1600 struct xfs_bmalloca *bma,
1603 struct xfs_bmbt_irec *new = &bma->got;
1604 int error; /* error return value */
1605 int i; /* temp state */
1606 xfs_ifork_t *ifp; /* inode fork pointer */
1607 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1608 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1609 int rval=0; /* return value (logging flags) */
1610 int state; /* state bits, accessed thru macros */
1612 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
1614 ASSERT(bma->idx >= 0);
1615 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
1616 ASSERT(!isnullstartblock(new->br_startblock));
1618 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
1620 XFS_STATS_INC(xs_add_exlist);
1623 if (whichfork == XFS_ATTR_FORK)
1624 state |= BMAP_ATTRFORK;
1627 * Check and set flags if this segment has a left neighbor.
1630 state |= BMAP_LEFT_VALID;
1631 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
1632 if (isnullstartblock(left.br_startblock))
1633 state |= BMAP_LEFT_DELAY;
1637 * Check and set flags if this segment has a current value.
1638 * Not true if we're inserting into the "hole" at eof.
1640 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1641 state |= BMAP_RIGHT_VALID;
1642 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
1643 if (isnullstartblock(right.br_startblock))
1644 state |= BMAP_RIGHT_DELAY;
1648 * We're inserting a real allocation between "left" and "right".
1649 * Set the contiguity flags. Don't let extents get too large.
1651 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1652 left.br_startoff + left.br_blockcount == new->br_startoff &&
1653 left.br_startblock + left.br_blockcount == new->br_startblock &&
1654 left.br_state == new->br_state &&
1655 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1656 state |= BMAP_LEFT_CONTIG;
1658 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1659 new->br_startoff + new->br_blockcount == right.br_startoff &&
1660 new->br_startblock + new->br_blockcount == right.br_startblock &&
1661 new->br_state == right.br_state &&
1662 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1663 (!(state & BMAP_LEFT_CONTIG) ||
1664 left.br_blockcount + new->br_blockcount +
1665 right.br_blockcount <= MAXEXTLEN))
1666 state |= BMAP_RIGHT_CONTIG;
1670 * Select which case we're in here, and implement it.
1672 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1673 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1675 * New allocation is contiguous with real allocations on the
1676 * left and on the right.
1677 * Merge all three into a single extent record.
1680 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1681 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1682 left.br_blockcount + new->br_blockcount +
1683 right.br_blockcount);
1684 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1686 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1688 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
1689 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
1690 if (bma->cur == NULL) {
1691 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1693 rval = XFS_ILOG_CORE;
1694 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
1695 right.br_startblock, right.br_blockcount,
1699 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1700 error = xfs_btree_delete(bma->cur, &i);
1703 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1704 error = xfs_btree_decrement(bma->cur, 0, &i);
1707 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1708 error = xfs_bmbt_update(bma->cur, left.br_startoff,
1710 left.br_blockcount +
1711 new->br_blockcount +
1712 right.br_blockcount,
1719 case BMAP_LEFT_CONTIG:
1721 * New allocation is contiguous with a real allocation
1723 * Merge the new allocation with the left neighbor.
1726 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1727 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1728 left.br_blockcount + new->br_blockcount);
1729 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1731 if (bma->cur == NULL) {
1732 rval = xfs_ilog_fext(whichfork);
1735 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
1736 left.br_startblock, left.br_blockcount,
1740 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1741 error = xfs_bmbt_update(bma->cur, left.br_startoff,
1743 left.br_blockcount +
1751 case BMAP_RIGHT_CONTIG:
1753 * New allocation is contiguous with a real allocation
1755 * Merge the new allocation with the right neighbor.
1757 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1758 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
1759 new->br_startoff, new->br_startblock,
1760 new->br_blockcount + right.br_blockcount,
1762 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1764 if (bma->cur == NULL) {
1765 rval = xfs_ilog_fext(whichfork);
1768 error = xfs_bmbt_lookup_eq(bma->cur,
1770 right.br_startblock,
1771 right.br_blockcount, &i);
1774 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1775 error = xfs_bmbt_update(bma->cur, new->br_startoff,
1777 new->br_blockcount +
1778 right.br_blockcount,
1787 * New allocation is not contiguous with another
1789 * Insert a new entry.
1791 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
1792 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
1793 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
1794 if (bma->cur == NULL) {
1795 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1797 rval = XFS_ILOG_CORE;
1798 error = xfs_bmbt_lookup_eq(bma->cur,
1801 new->br_blockcount, &i);
1804 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1805 bma->cur->bc_rec.b.br_state = new->br_state;
1806 error = xfs_btree_insert(bma->cur, &i);
1809 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1814 /* convert to a btree if necessary */
1815 if (XFS_IFORK_FORMAT(bma->ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
1816 XFS_IFORK_NEXTENTS(bma->ip, whichfork) > ifp->if_ext_max) {
1817 int tmp_logflags; /* partial log flag return val */
1819 ASSERT(bma->cur == NULL);
1820 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1821 bma->firstblock, bma->flist, &bma->cur,
1822 0, &tmp_logflags, whichfork);
1823 bma->logflags |= tmp_logflags;
1828 /* clear out the allocated field, done with it now in any case. */
1830 bma->cur->bc_private.b.allocated = 0;
1832 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
1834 bma->logflags |= rval;
1839 * Adjust the size of the new extent based on di_extsize and rt extsize.
1842 xfs_bmap_extsize_align(
1844 xfs_bmbt_irec_t *gotp, /* next extent pointer */
1845 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
1846 xfs_extlen_t extsz, /* align to this extent size */
1847 int rt, /* is this a realtime inode? */
1848 int eof, /* is extent at end-of-file? */
1849 int delay, /* creating delalloc extent? */
1850 int convert, /* overwriting unwritten extent? */
1851 xfs_fileoff_t *offp, /* in/out: aligned offset */
1852 xfs_extlen_t *lenp) /* in/out: aligned length */
1854 xfs_fileoff_t orig_off; /* original offset */
1855 xfs_extlen_t orig_alen; /* original length */
1856 xfs_fileoff_t orig_end; /* original off+len */
1857 xfs_fileoff_t nexto; /* next file offset */
1858 xfs_fileoff_t prevo; /* previous file offset */
1859 xfs_fileoff_t align_off; /* temp for offset */
1860 xfs_extlen_t align_alen; /* temp for length */
1861 xfs_extlen_t temp; /* temp for calculations */
1866 orig_off = align_off = *offp;
1867 orig_alen = align_alen = *lenp;
1868 orig_end = orig_off + orig_alen;
1871 * If this request overlaps an existing extent, then don't
1872 * attempt to perform any additional alignment.
1874 if (!delay && !eof &&
1875 (orig_off >= gotp->br_startoff) &&
1876 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
1881 * If the file offset is unaligned vs. the extent size
1882 * we need to align it. This will be possible unless
1883 * the file was previously written with a kernel that didn't
1884 * perform this alignment, or if a truncate shot us in the
1887 temp = do_mod(orig_off, extsz);
1893 * Same adjustment for the end of the requested area.
1895 if ((temp = (align_alen % extsz))) {
1896 align_alen += extsz - temp;
1899 * If the previous block overlaps with this proposed allocation
1900 * then move the start forward without adjusting the length.
1902 if (prevp->br_startoff != NULLFILEOFF) {
1903 if (prevp->br_startblock == HOLESTARTBLOCK)
1904 prevo = prevp->br_startoff;
1906 prevo = prevp->br_startoff + prevp->br_blockcount;
1909 if (align_off != orig_off && align_off < prevo)
1912 * If the next block overlaps with this proposed allocation
1913 * then move the start back without adjusting the length,
1914 * but not before offset 0.
1915 * This may of course make the start overlap previous block,
1916 * and if we hit the offset 0 limit then the next block
1917 * can still overlap too.
1919 if (!eof && gotp->br_startoff != NULLFILEOFF) {
1920 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
1921 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
1922 nexto = gotp->br_startoff + gotp->br_blockcount;
1924 nexto = gotp->br_startoff;
1926 nexto = NULLFILEOFF;
1928 align_off + align_alen != orig_end &&
1929 align_off + align_alen > nexto)
1930 align_off = nexto > align_alen ? nexto - align_alen : 0;
1932 * If we're now overlapping the next or previous extent that
1933 * means we can't fit an extsz piece in this hole. Just move
1934 * the start forward to the first valid spot and set
1935 * the length so we hit the end.
1937 if (align_off != orig_off && align_off < prevo)
1939 if (align_off + align_alen != orig_end &&
1940 align_off + align_alen > nexto &&
1941 nexto != NULLFILEOFF) {
1942 ASSERT(nexto > prevo);
1943 align_alen = nexto - align_off;
1947 * If realtime, and the result isn't a multiple of the realtime
1948 * extent size we need to remove blocks until it is.
1950 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
1952 * We're not covering the original request, or
1953 * we won't be able to once we fix the length.
1955 if (orig_off < align_off ||
1956 orig_end > align_off + align_alen ||
1957 align_alen - temp < orig_alen)
1958 return XFS_ERROR(EINVAL);
1960 * Try to fix it by moving the start up.
1962 if (align_off + temp <= orig_off) {
1967 * Try to fix it by moving the end in.
1969 else if (align_off + align_alen - temp >= orig_end)
1972 * Set the start to the minimum then trim the length.
1975 align_alen -= orig_off - align_off;
1976 align_off = orig_off;
1977 align_alen -= align_alen % mp->m_sb.sb_rextsize;
1980 * Result doesn't cover the request, fail it.
1982 if (orig_off < align_off || orig_end > align_off + align_alen)
1983 return XFS_ERROR(EINVAL);
1985 ASSERT(orig_off >= align_off);
1986 ASSERT(orig_end <= align_off + align_alen);
1990 if (!eof && gotp->br_startoff != NULLFILEOFF)
1991 ASSERT(align_off + align_alen <= gotp->br_startoff);
1992 if (prevp->br_startoff != NULLFILEOFF)
1993 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2001 #define XFS_ALLOC_GAP_UNITS 4
2005 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2007 xfs_fsblock_t adjust; /* adjustment to block numbers */
2008 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2009 xfs_mount_t *mp; /* mount point structure */
2010 int nullfb; /* true if ap->firstblock isn't set */
2011 int rt; /* true if inode is realtime */
2013 #define ISVALID(x,y) \
2015 (x) < mp->m_sb.sb_rblocks : \
2016 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2017 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2018 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2020 mp = ap->ip->i_mount;
2021 nullfb = *ap->firstblock == NULLFSBLOCK;
2022 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2023 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
2025 * If allocating at eof, and there's a previous real block,
2026 * try to use its last block as our starting point.
2028 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
2029 !isnullstartblock(ap->prev.br_startblock) &&
2030 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
2031 ap->prev.br_startblock)) {
2032 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
2034 * Adjust for the gap between prevp and us.
2036 adjust = ap->offset -
2037 (ap->prev.br_startoff + ap->prev.br_blockcount);
2039 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
2040 ap->blkno += adjust;
2043 * If not at eof, then compare the two neighbor blocks.
2044 * Figure out whether either one gives us a good starting point,
2045 * and pick the better one.
2047 else if (!ap->eof) {
2048 xfs_fsblock_t gotbno; /* right side block number */
2049 xfs_fsblock_t gotdiff=0; /* right side difference */
2050 xfs_fsblock_t prevbno; /* left side block number */
2051 xfs_fsblock_t prevdiff=0; /* left side difference */
2054 * If there's a previous (left) block, select a requested
2055 * start block based on it.
2057 if (ap->prev.br_startoff != NULLFILEOFF &&
2058 !isnullstartblock(ap->prev.br_startblock) &&
2059 (prevbno = ap->prev.br_startblock +
2060 ap->prev.br_blockcount) &&
2061 ISVALID(prevbno, ap->prev.br_startblock)) {
2063 * Calculate gap to end of previous block.
2065 adjust = prevdiff = ap->offset -
2066 (ap->prev.br_startoff +
2067 ap->prev.br_blockcount);
2069 * Figure the startblock based on the previous block's
2070 * end and the gap size.
2072 * If the gap is large relative to the piece we're
2073 * allocating, or using it gives us an invalid block
2074 * number, then just use the end of the previous block.
2076 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
2077 ISVALID(prevbno + prevdiff,
2078 ap->prev.br_startblock))
2083 * If the firstblock forbids it, can't use it,
2086 if (!rt && !nullfb &&
2087 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2088 prevbno = NULLFSBLOCK;
2091 * No previous block or can't follow it, just default.
2094 prevbno = NULLFSBLOCK;
2096 * If there's a following (right) block, select a requested
2097 * start block based on it.
2099 if (!isnullstartblock(ap->got.br_startblock)) {
2101 * Calculate gap to start of next block.
2103 adjust = gotdiff = ap->got.br_startoff - ap->offset;
2105 * Figure the startblock based on the next block's
2106 * start and the gap size.
2108 gotbno = ap->got.br_startblock;
2111 * If the gap is large relative to the piece we're
2112 * allocating, or using it gives us an invalid block
2113 * number, then just use the start of the next block
2114 * offset by our length.
2116 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
2117 ISVALID(gotbno - gotdiff, gotbno))
2119 else if (ISVALID(gotbno - ap->length, gotbno)) {
2120 gotbno -= ap->length;
2121 gotdiff += adjust - ap->length;
2125 * If the firstblock forbids it, can't use it,
2128 if (!rt && !nullfb &&
2129 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2130 gotbno = NULLFSBLOCK;
2133 * No next block, just default.
2136 gotbno = NULLFSBLOCK;
2138 * If both valid, pick the better one, else the only good
2139 * one, else ap->blkno is already set (to 0 or the inode block).
2141 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2142 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
2143 else if (prevbno != NULLFSBLOCK)
2144 ap->blkno = prevbno;
2145 else if (gotbno != NULLFSBLOCK)
2153 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2155 xfs_alloctype_t atype = 0; /* type for allocation routines */
2156 int error; /* error return value */
2157 xfs_mount_t *mp; /* mount point structure */
2158 xfs_extlen_t prod = 0; /* product factor for allocators */
2159 xfs_extlen_t ralen = 0; /* realtime allocation length */
2160 xfs_extlen_t align; /* minimum allocation alignment */
2163 mp = ap->ip->i_mount;
2164 align = xfs_get_extsz_hint(ap->ip);
2165 prod = align / mp->m_sb.sb_rextsize;
2166 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
2167 align, 1, ap->eof, 0,
2168 ap->conv, &ap->offset, &ap->length);
2172 ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
2175 * If the offset & length are not perfectly aligned
2176 * then kill prod, it will just get us in trouble.
2178 if (do_mod(ap->offset, align) || ap->length % align)
2181 * Set ralen to be the actual requested length in rtextents.
2183 ralen = ap->length / mp->m_sb.sb_rextsize;
2185 * If the old value was close enough to MAXEXTLEN that
2186 * we rounded up to it, cut it back so it's valid again.
2187 * Note that if it's a really large request (bigger than
2188 * MAXEXTLEN), we don't hear about that number, and can't
2189 * adjust the starting point to match it.
2191 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2192 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2195 * Lock out other modifications to the RT bitmap inode.
2197 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
2198 xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
2201 * If it's an allocation to an empty file at offset 0,
2202 * pick an extent that will space things out in the rt area.
2204 if (ap->eof && ap->offset == 0) {
2205 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2207 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2210 ap->blkno = rtx * mp->m_sb.sb_rextsize;
2215 xfs_bmap_adjacent(ap);
2218 * Realtime allocation, done through xfs_rtallocate_extent.
2220 atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2221 do_div(ap->blkno, mp->m_sb.sb_rextsize);
2224 if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
2225 &ralen, atype, ap->wasdel, prod, &rtb)))
2227 if (rtb == NULLFSBLOCK && prod > 1 &&
2228 (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1,
2229 ap->length, &ralen, atype,
2230 ap->wasdel, 1, &rtb)))
2233 if (ap->blkno != NULLFSBLOCK) {
2234 ap->blkno *= mp->m_sb.sb_rextsize;
2235 ralen *= mp->m_sb.sb_rextsize;
2237 ap->ip->i_d.di_nblocks += ralen;
2238 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2240 ap->ip->i_delayed_blks -= ralen;
2242 * Adjust the disk quota also. This was reserved
2245 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2246 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2247 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2255 xfs_bmap_btalloc_nullfb(
2256 struct xfs_bmalloca *ap,
2257 struct xfs_alloc_arg *args,
2260 struct xfs_mount *mp = ap->ip->i_mount;
2261 struct xfs_perag *pag;
2262 xfs_agnumber_t ag, startag;
2266 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2267 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2269 args->type = XFS_ALLOCTYPE_START_BNO;
2270 args->total = ap->total;
2273 * Search for an allocation group with a single extent large enough
2274 * for the request. If one isn't found, then adjust the minimum
2275 * allocation size to the largest space found.
2277 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
2278 if (startag == NULLAGNUMBER)
2281 pag = xfs_perag_get(mp, ag);
2282 while (*blen < args->maxlen) {
2283 if (!pag->pagf_init) {
2284 error = xfs_alloc_pagf_init(mp, args->tp, ag,
2285 XFS_ALLOC_FLAG_TRYLOCK);
2293 * See xfs_alloc_fix_freelist...
2295 if (pag->pagf_init) {
2296 xfs_extlen_t longest;
2297 longest = xfs_alloc_longest_free_extent(mp, pag);
2298 if (*blen < longest)
2303 if (xfs_inode_is_filestream(ap->ip)) {
2304 if (*blen >= args->maxlen)
2309 * If startag is an invalid AG, we've
2310 * come here once before and
2311 * xfs_filestream_new_ag picked the
2312 * best currently available.
2314 * Don't continue looping, since we
2315 * could loop forever.
2317 if (startag == NULLAGNUMBER)
2320 error = xfs_filestream_new_ag(ap, &ag);
2325 /* loop again to set 'blen'*/
2326 startag = NULLAGNUMBER;
2327 pag = xfs_perag_get(mp, ag);
2331 if (++ag == mp->m_sb.sb_agcount)
2336 pag = xfs_perag_get(mp, ag);
2341 * Since the above loop did a BUF_TRYLOCK, it is
2342 * possible that there is space for this request.
2344 if (notinit || *blen < ap->minlen)
2345 args->minlen = ap->minlen;
2347 * If the best seen length is less than the request
2348 * length, use the best as the minimum.
2350 else if (*blen < args->maxlen)
2351 args->minlen = *blen;
2353 * Otherwise we've seen an extent as big as maxlen,
2354 * use that as the minimum.
2357 args->minlen = args->maxlen;
2360 * set the failure fallback case to look in the selected
2361 * AG as the stream may have moved.
2363 if (xfs_inode_is_filestream(ap->ip))
2364 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2371 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2373 xfs_mount_t *mp; /* mount point structure */
2374 xfs_alloctype_t atype = 0; /* type for allocation routines */
2375 xfs_extlen_t align; /* minimum allocation alignment */
2376 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2378 xfs_alloc_arg_t args;
2380 xfs_extlen_t nextminlen = 0;
2381 int nullfb; /* true if ap->firstblock isn't set */
2386 mp = ap->ip->i_mount;
2387 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
2388 if (unlikely(align)) {
2389 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
2390 align, 0, ap->eof, 0, ap->conv,
2391 &ap->offset, &ap->length);
2395 nullfb = *ap->firstblock == NULLFSBLOCK;
2396 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
2398 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2399 ag = xfs_filestream_lookup_ag(ap->ip);
2400 ag = (ag != NULLAGNUMBER) ? ag : 0;
2401 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
2403 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2406 ap->blkno = *ap->firstblock;
2408 xfs_bmap_adjacent(ap);
2411 * If allowed, use ap->blkno; otherwise must use firstblock since
2412 * it's in the right allocation group.
2414 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
2417 ap->blkno = *ap->firstblock;
2419 * Normal allocation, done through xfs_alloc_vextent.
2421 tryagain = isaligned = 0;
2424 args.fsbno = ap->blkno;
2426 /* Trim the allocation back to the maximum an AG can fit. */
2427 args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
2428 args.firstblock = *ap->firstblock;
2431 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
2434 } else if (ap->flist->xbf_low) {
2435 if (xfs_inode_is_filestream(ap->ip))
2436 args.type = XFS_ALLOCTYPE_FIRST_AG;
2438 args.type = XFS_ALLOCTYPE_START_BNO;
2439 args.total = args.minlen = ap->minlen;
2441 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2442 args.total = ap->total;
2443 args.minlen = ap->minlen;
2445 /* apply extent size hints if obtained earlier */
2446 if (unlikely(align)) {
2448 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
2449 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2450 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
2454 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
2455 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
2456 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2459 * If we are not low on available data blocks, and the
2460 * underlying logical volume manager is a stripe, and
2461 * the file offset is zero then try to allocate data
2462 * blocks on stripe unit boundary.
2463 * NOTE: ap->aeof is only set if the allocation length
2464 * is >= the stripe unit and the allocation offset is
2465 * at the end of file.
2467 if (!ap->flist->xbf_low && ap->aeof) {
2469 args.alignment = mp->m_dalign;
2473 * Adjust for alignment
2475 if (blen > args.alignment && blen <= args.maxlen)
2476 args.minlen = blen - args.alignment;
2477 args.minalignslop = 0;
2480 * First try an exact bno allocation.
2481 * If it fails then do a near or start bno
2482 * allocation with alignment turned on.
2486 args.type = XFS_ALLOCTYPE_THIS_BNO;
2489 * Compute the minlen+alignment for the
2490 * next case. Set slop so that the value
2491 * of minlen+alignment+slop doesn't go up
2492 * between the calls.
2494 if (blen > mp->m_dalign && blen <= args.maxlen)
2495 nextminlen = blen - mp->m_dalign;
2497 nextminlen = args.minlen;
2498 if (nextminlen + mp->m_dalign > args.minlen + 1)
2500 nextminlen + mp->m_dalign -
2503 args.minalignslop = 0;
2507 args.minalignslop = 0;
2509 args.minleft = ap->minleft;
2510 args.wasdel = ap->wasdel;
2512 args.userdata = ap->userdata;
2513 if ((error = xfs_alloc_vextent(&args)))
2515 if (tryagain && args.fsbno == NULLFSBLOCK) {
2517 * Exact allocation failed. Now try with alignment
2521 args.fsbno = ap->blkno;
2522 args.alignment = mp->m_dalign;
2523 args.minlen = nextminlen;
2524 args.minalignslop = 0;
2526 if ((error = xfs_alloc_vextent(&args)))
2529 if (isaligned && args.fsbno == NULLFSBLOCK) {
2531 * allocation failed, so turn off alignment and
2535 args.fsbno = ap->blkno;
2537 if ((error = xfs_alloc_vextent(&args)))
2540 if (args.fsbno == NULLFSBLOCK && nullfb &&
2541 args.minlen > ap->minlen) {
2542 args.minlen = ap->minlen;
2543 args.type = XFS_ALLOCTYPE_START_BNO;
2544 args.fsbno = ap->blkno;
2545 if ((error = xfs_alloc_vextent(&args)))
2548 if (args.fsbno == NULLFSBLOCK && nullfb) {
2550 args.type = XFS_ALLOCTYPE_FIRST_AG;
2551 args.total = ap->minlen;
2553 if ((error = xfs_alloc_vextent(&args)))
2555 ap->flist->xbf_low = 1;
2557 if (args.fsbno != NULLFSBLOCK) {
2559 * check the allocation happened at the same or higher AG than
2560 * the first block that was allocated.
2562 ASSERT(*ap->firstblock == NULLFSBLOCK ||
2563 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
2564 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
2565 (ap->flist->xbf_low &&
2566 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
2567 XFS_FSB_TO_AGNO(mp, args.fsbno)));
2569 ap->blkno = args.fsbno;
2570 if (*ap->firstblock == NULLFSBLOCK)
2571 *ap->firstblock = args.fsbno;
2572 ASSERT(nullfb || fb_agno == args.agno ||
2573 (ap->flist->xbf_low && fb_agno < args.agno));
2574 ap->length = args.len;
2575 ap->ip->i_d.di_nblocks += args.len;
2576 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2578 ap->ip->i_delayed_blks -= args.len;
2580 * Adjust the disk quota also. This was reserved
2583 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2584 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2585 XFS_TRANS_DQ_BCOUNT,
2588 ap->blkno = NULLFSBLOCK;
2595 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2596 * It figures out where to ask the underlying allocator to put the new extent.
2600 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2602 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
2603 return xfs_bmap_rtalloc(ap);
2604 return xfs_bmap_btalloc(ap);
2608 * Transform a btree format file with only one leaf node, where the
2609 * extents list will fit in the inode, into an extents format file.
2610 * Since the file extents are already in-core, all we have to do is
2611 * give up the space for the btree root and pitch the leaf block.
2613 STATIC int /* error */
2614 xfs_bmap_btree_to_extents(
2615 xfs_trans_t *tp, /* transaction pointer */
2616 xfs_inode_t *ip, /* incore inode pointer */
2617 xfs_btree_cur_t *cur, /* btree cursor */
2618 int *logflagsp, /* inode logging flags */
2619 int whichfork) /* data or attr fork */
2622 struct xfs_btree_block *cblock;/* child btree block */
2623 xfs_fsblock_t cbno; /* child block number */
2624 xfs_buf_t *cbp; /* child block's buffer */
2625 int error; /* error return value */
2626 xfs_ifork_t *ifp; /* inode fork data */
2627 xfs_mount_t *mp; /* mount point structure */
2628 __be64 *pp; /* ptr to block address */
2629 struct xfs_btree_block *rblock;/* root btree block */
2632 ifp = XFS_IFORK_PTR(ip, whichfork);
2633 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2634 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2635 rblock = ifp->if_broot;
2636 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2637 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
2638 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2639 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
2640 cbno = be64_to_cpu(*pp);
2643 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
2646 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2647 XFS_BMAP_BTREE_REF)))
2649 cblock = XFS_BUF_TO_BLOCK(cbp);
2650 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
2652 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2653 ip->i_d.di_nblocks--;
2654 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
2655 xfs_trans_binval(tp, cbp);
2656 if (cur->bc_bufs[0] == cbp)
2657 cur->bc_bufs[0] = NULL;
2658 xfs_iroot_realloc(ip, -1, whichfork);
2659 ASSERT(ifp->if_broot == NULL);
2660 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2661 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
2662 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2667 * Called by xfs_bmapi to update file extent records and the btree
2668 * after removing space (or undoing a delayed allocation).
2670 STATIC int /* error */
2671 xfs_bmap_del_extent(
2672 xfs_inode_t *ip, /* incore inode pointer */
2673 xfs_trans_t *tp, /* current transaction pointer */
2674 xfs_extnum_t *idx, /* extent number to update/delete */
2675 xfs_bmap_free_t *flist, /* list of extents to be freed */
2676 xfs_btree_cur_t *cur, /* if null, not a btree */
2677 xfs_bmbt_irec_t *del, /* data to remove from extents */
2678 int *logflagsp, /* inode logging flags */
2679 int whichfork) /* data or attr fork */
2681 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2682 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2683 xfs_fsblock_t del_endblock=0; /* first block past del */
2684 xfs_fileoff_t del_endoff; /* first offset past del */
2685 int delay; /* current block is delayed allocated */
2686 int do_fx; /* free extent at end of routine */
2687 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
2688 int error; /* error return value */
2689 int flags; /* inode logging flags */
2690 xfs_bmbt_irec_t got; /* current extent entry */
2691 xfs_fileoff_t got_endoff; /* first offset past got */
2692 int i; /* temp state */
2693 xfs_ifork_t *ifp; /* inode fork pointer */
2694 xfs_mount_t *mp; /* mount structure */
2695 xfs_filblks_t nblks; /* quota/sb block count */
2696 xfs_bmbt_irec_t new; /* new record to be inserted */
2698 uint qfield; /* quota field to update */
2699 xfs_filblks_t temp; /* for indirect length calculations */
2700 xfs_filblks_t temp2; /* for indirect length calculations */
2703 XFS_STATS_INC(xs_del_exlist);
2705 if (whichfork == XFS_ATTR_FORK)
2706 state |= BMAP_ATTRFORK;
2709 ifp = XFS_IFORK_PTR(ip, whichfork);
2710 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
2711 (uint)sizeof(xfs_bmbt_rec_t)));
2712 ASSERT(del->br_blockcount > 0);
2713 ep = xfs_iext_get_ext(ifp, *idx);
2714 xfs_bmbt_get_all(ep, &got);
2715 ASSERT(got.br_startoff <= del->br_startoff);
2716 del_endoff = del->br_startoff + del->br_blockcount;
2717 got_endoff = got.br_startoff + got.br_blockcount;
2718 ASSERT(got_endoff >= del_endoff);
2719 delay = isnullstartblock(got.br_startblock);
2720 ASSERT(isnullstartblock(del->br_startblock) == delay);
2725 * If deleting a real allocation, must free up the disk space.
2728 flags = XFS_ILOG_CORE;
2730 * Realtime allocation. Free it and record di_nblocks update.
2732 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
2736 ASSERT(do_mod(del->br_blockcount,
2737 mp->m_sb.sb_rextsize) == 0);
2738 ASSERT(do_mod(del->br_startblock,
2739 mp->m_sb.sb_rextsize) == 0);
2740 bno = del->br_startblock;
2741 len = del->br_blockcount;
2742 do_div(bno, mp->m_sb.sb_rextsize);
2743 do_div(len, mp->m_sb.sb_rextsize);
2744 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
2748 nblks = len * mp->m_sb.sb_rextsize;
2749 qfield = XFS_TRANS_DQ_RTBCOUNT;
2752 * Ordinary allocation.
2756 nblks = del->br_blockcount;
2757 qfield = XFS_TRANS_DQ_BCOUNT;
2760 * Set up del_endblock and cur for later.
2762 del_endblock = del->br_startblock + del->br_blockcount;
2764 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2765 got.br_startblock, got.br_blockcount,
2768 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2770 da_old = da_new = 0;
2772 da_old = startblockval(got.br_startblock);
2778 * Set flag value to use in switch statement.
2779 * Left-contig is 2, right-contig is 1.
2781 switch (((got.br_startoff == del->br_startoff) << 1) |
2782 (got_endoff == del_endoff)) {
2785 * Matches the whole extent. Delete the entry.
2787 xfs_iext_remove(ip, *idx, 1,
2788 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
2793 XFS_IFORK_NEXT_SET(ip, whichfork,
2794 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2795 flags |= XFS_ILOG_CORE;
2797 flags |= xfs_ilog_fext(whichfork);
2800 if ((error = xfs_btree_delete(cur, &i)))
2802 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2807 * Deleting the first part of the extent.
2809 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2810 xfs_bmbt_set_startoff(ep, del_endoff);
2811 temp = got.br_blockcount - del->br_blockcount;
2812 xfs_bmbt_set_blockcount(ep, temp);
2814 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2816 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2817 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2821 xfs_bmbt_set_startblock(ep, del_endblock);
2822 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2824 flags |= xfs_ilog_fext(whichfork);
2827 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2828 got.br_blockcount - del->br_blockcount,
2835 * Deleting the last part of the extent.
2837 temp = got.br_blockcount - del->br_blockcount;
2838 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2839 xfs_bmbt_set_blockcount(ep, temp);
2841 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2843 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2844 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2848 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2850 flags |= xfs_ilog_fext(whichfork);
2853 if ((error = xfs_bmbt_update(cur, got.br_startoff,
2855 got.br_blockcount - del->br_blockcount,
2862 * Deleting the middle of the extent.
2864 temp = del->br_startoff - got.br_startoff;
2865 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2866 xfs_bmbt_set_blockcount(ep, temp);
2867 new.br_startoff = del_endoff;
2868 temp2 = got_endoff - del_endoff;
2869 new.br_blockcount = temp2;
2870 new.br_state = got.br_state;
2872 new.br_startblock = del_endblock;
2873 flags |= XFS_ILOG_CORE;
2875 if ((error = xfs_bmbt_update(cur,
2877 got.br_startblock, temp,
2880 if ((error = xfs_btree_increment(cur, 0, &i)))
2882 cur->bc_rec.b = new;
2883 error = xfs_btree_insert(cur, &i);
2884 if (error && error != ENOSPC)
2887 * If get no-space back from btree insert,
2888 * it tried a split, and we have a zero
2889 * block reservation.
2890 * Fix up our state and return the error.
2892 if (error == ENOSPC) {
2894 * Reset the cursor, don't trust
2895 * it after any insert operation.
2897 if ((error = xfs_bmbt_lookup_eq(cur,
2902 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2904 * Update the btree record back
2905 * to the original value.
2907 if ((error = xfs_bmbt_update(cur,
2914 * Reset the extent record back
2915 * to the original value.
2917 xfs_bmbt_set_blockcount(ep,
2920 error = XFS_ERROR(ENOSPC);
2923 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2925 flags |= xfs_ilog_fext(whichfork);
2926 XFS_IFORK_NEXT_SET(ip, whichfork,
2927 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2929 ASSERT(whichfork == XFS_DATA_FORK);
2930 temp = xfs_bmap_worst_indlen(ip, temp);
2931 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2932 temp2 = xfs_bmap_worst_indlen(ip, temp2);
2933 new.br_startblock = nullstartblock((int)temp2);
2934 da_new = temp + temp2;
2935 while (da_new > da_old) {
2939 xfs_bmbt_set_startblock(ep,
2940 nullstartblock((int)temp));
2942 if (da_new == da_old)
2948 nullstartblock((int)temp2);
2952 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2953 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
2958 * If we need to, add to list of extents to delete.
2961 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
2964 * Adjust inode # blocks in the file.
2967 ip->i_d.di_nblocks -= nblks;
2969 * Adjust quota data.
2972 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
2975 * Account for change in delayed indirect blocks.
2976 * Nothing to do for disk quota accounting here.
2978 ASSERT(da_old >= da_new);
2979 if (da_old > da_new) {
2980 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
2981 (int64_t)(da_old - da_new), 0);
2989 * Remove the entry "free" from the free item list. Prev points to the
2990 * previous entry, unless "free" is the head of the list.
2994 xfs_bmap_free_t *flist, /* free item list header */
2995 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
2996 xfs_bmap_free_item_t *free) /* list item to be freed */
2999 prev->xbfi_next = free->xbfi_next;
3001 flist->xbf_first = free->xbfi_next;
3003 kmem_zone_free(xfs_bmap_free_item_zone, free);
3007 * Convert an extents-format file into a btree-format file.
3008 * The new file will have a root block (in the inode) and a single child block.
3010 STATIC int /* error */
3011 xfs_bmap_extents_to_btree(
3012 xfs_trans_t *tp, /* transaction pointer */
3013 xfs_inode_t *ip, /* incore inode pointer */
3014 xfs_fsblock_t *firstblock, /* first-block-allocated */
3015 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3016 xfs_btree_cur_t **curp, /* cursor returned to caller */
3017 int wasdel, /* converting a delayed alloc */
3018 int *logflagsp, /* inode logging flags */
3019 int whichfork) /* data or attr fork */
3021 struct xfs_btree_block *ablock; /* allocated (child) bt block */
3022 xfs_buf_t *abp; /* buffer for ablock */
3023 xfs_alloc_arg_t args; /* allocation arguments */
3024 xfs_bmbt_rec_t *arp; /* child record pointer */
3025 struct xfs_btree_block *block; /* btree root block */
3026 xfs_btree_cur_t *cur; /* bmap btree cursor */
3027 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3028 int error; /* error return value */
3029 xfs_extnum_t i, cnt; /* extent record index */
3030 xfs_ifork_t *ifp; /* inode fork pointer */
3031 xfs_bmbt_key_t *kp; /* root block key pointer */
3032 xfs_mount_t *mp; /* mount structure */
3033 xfs_extnum_t nextents; /* number of file extents */
3034 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3036 ifp = XFS_IFORK_PTR(ip, whichfork);
3037 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3038 ASSERT(ifp->if_ext_max ==
3039 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3041 * Make space in the inode incore.
3043 xfs_iroot_realloc(ip, 1, whichfork);
3044 ifp->if_flags |= XFS_IFBROOT;
3049 block = ifp->if_broot;
3050 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3051 block->bb_level = cpu_to_be16(1);
3052 block->bb_numrecs = cpu_to_be16(1);
3053 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3054 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3057 * Need a cursor. Can't allocate until bb_level is filled in.
3060 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
3061 cur->bc_private.b.firstblock = *firstblock;
3062 cur->bc_private.b.flist = flist;
3063 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3065 * Convert to a btree with two levels, one record in root.
3067 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3070 args.firstblock = *firstblock;
3071 if (*firstblock == NULLFSBLOCK) {
3072 args.type = XFS_ALLOCTYPE_START_BNO;
3073 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3074 } else if (flist->xbf_low) {
3075 args.type = XFS_ALLOCTYPE_START_BNO;
3076 args.fsbno = *firstblock;
3078 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3079 args.fsbno = *firstblock;
3081 args.minlen = args.maxlen = args.prod = 1;
3082 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3083 args.minalignslop = 0;
3084 args.wasdel = wasdel;
3086 if ((error = xfs_alloc_vextent(&args))) {
3087 xfs_iroot_realloc(ip, -1, whichfork);
3088 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3092 * Allocation can't fail, the space was reserved.
3094 ASSERT(args.fsbno != NULLFSBLOCK);
3095 ASSERT(*firstblock == NULLFSBLOCK ||
3096 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3098 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3099 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3100 cur->bc_private.b.allocated++;
3101 ip->i_d.di_nblocks++;
3102 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3103 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3105 * Fill in the child block.
3107 ablock = XFS_BUF_TO_BLOCK(abp);
3108 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3109 ablock->bb_level = 0;
3110 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3111 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3112 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3113 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3114 for (cnt = i = 0; i < nextents; i++) {
3115 ep = xfs_iext_get_ext(ifp, i);
3116 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
3117 arp->l0 = cpu_to_be64(ep->l0);
3118 arp->l1 = cpu_to_be64(ep->l1);
3122 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3123 xfs_btree_set_numrecs(ablock, cnt);
3126 * Fill in the root key and pointer.
3128 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3129 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3130 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
3131 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3132 be16_to_cpu(block->bb_level)));
3133 *pp = cpu_to_be64(args.fsbno);
3136 * Do all this logging at the end so that
3137 * the root is at the right level.
3139 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3140 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3141 ASSERT(*curp == NULL);
3143 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
3148 * Calculate the default attribute fork offset for newly created inodes.
3151 xfs_default_attroffset(
3152 struct xfs_inode *ip)
3154 struct xfs_mount *mp = ip->i_mount;
3157 if (mp->m_sb.sb_inodesize == 256) {
3158 offset = XFS_LITINO(mp) -
3159 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3161 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3164 ASSERT(offset < XFS_LITINO(mp));
3169 * Helper routine to reset inode di_forkoff field when switching
3170 * attribute fork from local to extent format - we reset it where
3171 * possible to make space available for inline data fork extents.
3174 xfs_bmap_forkoff_reset(
3179 if (whichfork == XFS_ATTR_FORK &&
3180 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3181 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3182 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3183 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3185 if (dfl_forkoff > ip->i_d.di_forkoff) {
3186 ip->i_d.di_forkoff = dfl_forkoff;
3187 ip->i_df.if_ext_max =
3188 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3189 ip->i_afp->if_ext_max =
3190 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3196 * Convert a local file to an extents file.
3197 * This code is out of bounds for data forks of regular files,
3198 * since the file data needs to get logged so things will stay consistent.
3199 * (The bmap-level manipulations are ok, though).
3201 STATIC int /* error */
3202 xfs_bmap_local_to_extents(
3203 xfs_trans_t *tp, /* transaction pointer */
3204 xfs_inode_t *ip, /* incore inode pointer */
3205 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3206 xfs_extlen_t total, /* total blocks needed by transaction */
3207 int *logflagsp, /* inode logging flags */
3208 int whichfork) /* data or attr fork */
3210 int error; /* error return value */
3211 int flags; /* logging flags returned */
3212 xfs_ifork_t *ifp; /* inode fork pointer */
3215 * We don't want to deal with the case of keeping inode data inline yet.
3216 * So sending the data fork of a regular inode is invalid.
3218 ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK));
3219 ifp = XFS_IFORK_PTR(ip, whichfork);
3220 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3223 if (ifp->if_bytes) {
3224 xfs_alloc_arg_t args; /* allocation arguments */
3225 xfs_buf_t *bp; /* buffer for extent block */
3226 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
3229 args.mp = ip->i_mount;
3230 args.firstblock = *firstblock;
3231 ASSERT((ifp->if_flags &
3232 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
3234 * Allocate a block. We know we need only one, since the
3235 * file currently fits in an inode.
3237 if (*firstblock == NULLFSBLOCK) {
3238 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3239 args.type = XFS_ALLOCTYPE_START_BNO;
3241 args.fsbno = *firstblock;
3242 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3245 args.mod = args.minleft = args.alignment = args.wasdel =
3246 args.isfl = args.minalignslop = 0;
3247 args.minlen = args.maxlen = args.prod = 1;
3248 if ((error = xfs_alloc_vextent(&args)))
3251 * Can't fail, the space was reserved.
3253 ASSERT(args.fsbno != NULLFSBLOCK);
3254 ASSERT(args.len == 1);
3255 *firstblock = args.fsbno;
3256 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3257 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
3258 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3259 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3260 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3261 xfs_iext_add(ifp, 0, 1);
3262 ep = xfs_iext_get_ext(ifp, 0);
3263 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3264 trace_xfs_bmap_post_update(ip, 0,
3265 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3267 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3268 ip->i_d.di_nblocks = 1;
3269 xfs_trans_mod_dquot_byino(tp, ip,
3270 XFS_TRANS_DQ_BCOUNT, 1L);
3271 flags |= xfs_ilog_fext(whichfork);
3273 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3274 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3276 ifp->if_flags &= ~XFS_IFINLINE;
3277 ifp->if_flags |= XFS_IFEXTENTS;
3278 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3279 flags |= XFS_ILOG_CORE;
3286 * Search the extent records for the entry containing block bno.
3287 * If bno lies in a hole, point to the next entry. If bno lies
3288 * past eof, *eofp will be set, and *prevp will contain the last
3289 * entry (null if none). Else, *lastxp will be set to the index
3290 * of the found entry; *gotp will contain the entry.
3292 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3293 xfs_bmap_search_multi_extents(
3294 xfs_ifork_t *ifp, /* inode fork pointer */
3295 xfs_fileoff_t bno, /* block number searched for */
3296 int *eofp, /* out: end of file found */
3297 xfs_extnum_t *lastxp, /* out: last extent index */
3298 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3299 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3301 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3302 xfs_extnum_t lastx; /* last extent index */
3305 * Initialize the extent entry structure to catch access to
3306 * uninitialized br_startblock field.
3308 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3309 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3310 gotp->br_state = XFS_EXT_INVALID;
3312 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3314 gotp->br_startblock = 0xffffa5a5;
3316 prevp->br_startoff = NULLFILEOFF;
3318 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3320 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3322 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3323 xfs_bmbt_get_all(ep, gotp);
3337 * Search the extents list for the inode, for the extent containing bno.
3338 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3339 * *eofp will be set, and *prevp will contain the last entry (null if none).
3340 * Else, *lastxp will be set to the index of the found
3341 * entry; *gotp will contain the entry.
3343 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3344 xfs_bmap_search_extents(
3345 xfs_inode_t *ip, /* incore inode pointer */
3346 xfs_fileoff_t bno, /* block number searched for */
3347 int fork, /* data or attr fork */
3348 int *eofp, /* out: end of file found */
3349 xfs_extnum_t *lastxp, /* out: last extent index */
3350 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3351 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3353 xfs_ifork_t *ifp; /* inode fork pointer */
3354 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3356 XFS_STATS_INC(xs_look_exlist);
3357 ifp = XFS_IFORK_PTR(ip, fork);
3359 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3361 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3362 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3363 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
3364 "Access to block zero in inode %llu "
3365 "start_block: %llx start_off: %llx "
3366 "blkcnt: %llx extent-state: %x lastx: %x\n",
3367 (unsigned long long)ip->i_ino,
3368 (unsigned long long)gotp->br_startblock,
3369 (unsigned long long)gotp->br_startoff,
3370 (unsigned long long)gotp->br_blockcount,
3371 gotp->br_state, *lastxp);
3372 *lastxp = NULLEXTNUM;
3380 * Compute the worst-case number of indirect blocks that will be used
3381 * for ip's delayed extent of length "len".
3383 STATIC xfs_filblks_t
3384 xfs_bmap_worst_indlen(
3385 xfs_inode_t *ip, /* incore inode pointer */
3386 xfs_filblks_t len) /* delayed extent length */
3388 int level; /* btree level number */
3389 int maxrecs; /* maximum record count at this level */
3390 xfs_mount_t *mp; /* mount structure */
3391 xfs_filblks_t rval; /* return value */
3394 maxrecs = mp->m_bmap_dmxr[0];
3395 for (level = 0, rval = 0;
3396 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3399 do_div(len, maxrecs);
3402 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3405 maxrecs = mp->m_bmap_dmxr[1];
3411 * Convert inode from non-attributed to attributed.
3412 * Must not be in a transaction, ip must not be locked.
3414 int /* error code */
3415 xfs_bmap_add_attrfork(
3416 xfs_inode_t *ip, /* incore inode pointer */
3417 int size, /* space new attribute needs */
3418 int rsvd) /* xact may use reserved blks */
3420 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
3421 xfs_bmap_free_t flist; /* freed extent records */
3422 xfs_mount_t *mp; /* mount structure */
3423 xfs_trans_t *tp; /* transaction pointer */
3424 int blks; /* space reservation */
3425 int version = 1; /* superblock attr version */
3426 int committed; /* xaction was committed */
3427 int logflags; /* logging flags */
3428 int error; /* error return value */
3430 ASSERT(XFS_IFORK_Q(ip) == 0);
3431 ASSERT(ip->i_df.if_ext_max ==
3432 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3435 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3436 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3437 blks = XFS_ADDAFORK_SPACE_RES(mp);
3439 tp->t_flags |= XFS_TRANS_RESERVE;
3440 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3441 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3443 xfs_ilock(ip, XFS_ILOCK_EXCL);
3444 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
3445 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3446 XFS_QMOPT_RES_REGBLKS);
3448 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3449 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3452 if (XFS_IFORK_Q(ip))
3454 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3456 * For inodes coming from pre-6.2 filesystems.
3458 ASSERT(ip->i_d.di_aformat == 0);
3459 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3461 ASSERT(ip->i_d.di_anextents == 0);
3463 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3464 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3466 switch (ip->i_d.di_format) {
3467 case XFS_DINODE_FMT_DEV:
3468 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3470 case XFS_DINODE_FMT_UUID:
3471 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3473 case XFS_DINODE_FMT_LOCAL:
3474 case XFS_DINODE_FMT_EXTENTS:
3475 case XFS_DINODE_FMT_BTREE:
3476 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3477 if (!ip->i_d.di_forkoff)
3478 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
3479 else if (mp->m_flags & XFS_MOUNT_ATTR2)
3484 error = XFS_ERROR(EINVAL);
3487 ip->i_df.if_ext_max =
3488 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3489 ASSERT(ip->i_afp == NULL);
3490 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3491 ip->i_afp->if_ext_max =
3492 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3493 ip->i_afp->if_flags = XFS_IFEXTENTS;
3495 xfs_bmap_init(&flist, &firstblock);
3496 switch (ip->i_d.di_format) {
3497 case XFS_DINODE_FMT_LOCAL:
3498 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3501 case XFS_DINODE_FMT_EXTENTS:
3502 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3505 case XFS_DINODE_FMT_BTREE:
3506 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3514 xfs_trans_log_inode(tp, ip, logflags);
3517 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3518 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
3519 __int64_t sbfields = 0;
3521 spin_lock(&mp->m_sb_lock);
3522 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3523 xfs_sb_version_addattr(&mp->m_sb);
3524 sbfields |= XFS_SB_VERSIONNUM;
3526 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3527 xfs_sb_version_addattr2(&mp->m_sb);
3528 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
3531 spin_unlock(&mp->m_sb_lock);
3532 xfs_mod_sb(tp, sbfields);
3534 spin_unlock(&mp->m_sb_lock);
3536 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
3538 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3539 ASSERT(ip->i_df.if_ext_max ==
3540 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3543 xfs_bmap_cancel(&flist);
3545 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3547 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3548 ASSERT(ip->i_df.if_ext_max ==
3549 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3554 * Add the extent to the list of extents to be free at transaction end.
3555 * The list is maintained sorted (by block number).
3560 xfs_fsblock_t bno, /* fs block number of extent */
3561 xfs_filblks_t len, /* length of extent */
3562 xfs_bmap_free_t *flist, /* list of extents */
3563 xfs_mount_t *mp) /* mount point structure */
3565 xfs_bmap_free_item_t *cur; /* current (next) element */
3566 xfs_bmap_free_item_t *new; /* new element */
3567 xfs_bmap_free_item_t *prev; /* previous element */
3569 xfs_agnumber_t agno;
3570 xfs_agblock_t agbno;
3572 ASSERT(bno != NULLFSBLOCK);
3574 ASSERT(len <= MAXEXTLEN);
3575 ASSERT(!isnullstartblock(bno));
3576 agno = XFS_FSB_TO_AGNO(mp, bno);
3577 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3578 ASSERT(agno < mp->m_sb.sb_agcount);
3579 ASSERT(agbno < mp->m_sb.sb_agblocks);
3580 ASSERT(len < mp->m_sb.sb_agblocks);
3581 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3583 ASSERT(xfs_bmap_free_item_zone != NULL);
3584 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3585 new->xbfi_startblock = bno;
3586 new->xbfi_blockcount = (xfs_extlen_t)len;
3587 for (prev = NULL, cur = flist->xbf_first;
3589 prev = cur, cur = cur->xbfi_next) {
3590 if (cur->xbfi_startblock >= bno)
3594 prev->xbfi_next = new;
3596 flist->xbf_first = new;
3597 new->xbfi_next = cur;
3602 * Compute and fill in the value of the maximum depth of a bmap btree
3603 * in this filesystem. Done once, during mount.
3606 xfs_bmap_compute_maxlevels(
3607 xfs_mount_t *mp, /* file system mount structure */
3608 int whichfork) /* data or attr fork */
3610 int level; /* btree level */
3611 uint maxblocks; /* max blocks at this level */
3612 uint maxleafents; /* max leaf entries possible */
3613 int maxrootrecs; /* max records in root block */
3614 int minleafrecs; /* min records in leaf block */
3615 int minnoderecs; /* min records in node block */
3616 int sz; /* root block size */
3619 * The maximum number of extents in a file, hence the maximum
3620 * number of leaf entries, is controlled by the type of di_nextents
3621 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3622 * (a signed 16-bit number, xfs_aextnum_t).
3624 * Note that we can no longer assume that if we are in ATTR1 that
3625 * the fork offset of all the inodes will be
3626 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3627 * with ATTR2 and then mounted back with ATTR1, keeping the
3628 * di_forkoff's fixed but probably at various positions. Therefore,
3629 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3630 * of a minimum size available.
3632 if (whichfork == XFS_DATA_FORK) {
3633 maxleafents = MAXEXTNUM;
3634 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
3636 maxleafents = MAXAEXTNUM;
3637 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
3639 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
3640 minleafrecs = mp->m_bmap_dmnr[0];
3641 minnoderecs = mp->m_bmap_dmnr[1];
3642 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3643 for (level = 1; maxblocks > 1; level++) {
3644 if (maxblocks <= maxrootrecs)
3647 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3649 mp->m_bm_maxlevels[whichfork] = level;
3653 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3654 * caller. Frees all the extents that need freeing, which must be done
3655 * last due to locking considerations. We never free any extents in
3656 * the first transaction.
3658 * Return 1 if the given transaction was committed and a new one
3659 * started, and 0 otherwise in the committed parameter.
3663 xfs_trans_t **tp, /* transaction pointer addr */
3664 xfs_bmap_free_t *flist, /* i/o: list extents to free */
3665 int *committed) /* xact committed or not */
3667 xfs_efd_log_item_t *efd; /* extent free data */
3668 xfs_efi_log_item_t *efi; /* extent free intention */
3669 int error; /* error return value */
3670 xfs_bmap_free_item_t *free; /* free extent item */
3671 unsigned int logres; /* new log reservation */
3672 unsigned int logcount; /* new log count */
3673 xfs_mount_t *mp; /* filesystem mount structure */
3674 xfs_bmap_free_item_t *next; /* next item on free list */
3675 xfs_trans_t *ntp; /* new transaction pointer */
3677 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3678 if (flist->xbf_count == 0) {
3683 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3684 for (free = flist->xbf_first; free; free = free->xbfi_next)
3685 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3686 free->xbfi_blockcount);
3687 logres = ntp->t_log_res;
3688 logcount = ntp->t_log_count;
3689 ntp = xfs_trans_dup(*tp);
3690 error = xfs_trans_commit(*tp, 0);
3694 * We have a new transaction, so we should return committed=1,
3695 * even though we're returning an error.
3701 * transaction commit worked ok so we can drop the extra ticket
3702 * reference that we gained in xfs_trans_dup()
3704 xfs_log_ticket_put(ntp->t_ticket);
3706 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
3709 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
3710 for (free = flist->xbf_first; free != NULL; free = next) {
3711 next = free->xbfi_next;
3712 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
3713 free->xbfi_blockcount))) {
3715 * The bmap free list will be cleaned up at a
3716 * higher level. The EFI will be canceled when
3717 * this transaction is aborted.
3718 * Need to force shutdown here to make sure it
3719 * happens, since this transaction may not be
3723 if (!XFS_FORCED_SHUTDOWN(mp))
3724 xfs_force_shutdown(mp,
3725 (error == EFSCORRUPTED) ?
3726 SHUTDOWN_CORRUPT_INCORE :
3727 SHUTDOWN_META_IO_ERROR);
3730 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
3731 free->xbfi_blockcount);
3732 xfs_bmap_del_free(flist, NULL, free);
3738 * Free up any items left in the list.
3742 xfs_bmap_free_t *flist) /* list of bmap_free_items */
3744 xfs_bmap_free_item_t *free; /* free list item */
3745 xfs_bmap_free_item_t *next;
3747 if (flist->xbf_count == 0)
3749 ASSERT(flist->xbf_first != NULL);
3750 for (free = flist->xbf_first; free; free = next) {
3751 next = free->xbfi_next;
3752 xfs_bmap_del_free(flist, NULL, free);
3754 ASSERT(flist->xbf_count == 0);
3758 * Returns the file-relative block number of the first unused block(s)
3759 * in the file with at least "len" logically contiguous blocks free.
3760 * This is the lowest-address hole if the file has holes, else the first block
3761 * past the end of file.
3762 * Return 0 if the file is currently local (in-inode).
3765 xfs_bmap_first_unused(
3766 xfs_trans_t *tp, /* transaction pointer */
3767 xfs_inode_t *ip, /* incore inode */
3768 xfs_extlen_t len, /* size of hole to find */
3769 xfs_fileoff_t *first_unused, /* unused block */
3770 int whichfork) /* data or attr fork */
3772 int error; /* error return value */
3773 int idx; /* extent record index */
3774 xfs_ifork_t *ifp; /* inode fork pointer */
3775 xfs_fileoff_t lastaddr; /* last block number seen */
3776 xfs_fileoff_t lowest; /* lowest useful block */
3777 xfs_fileoff_t max; /* starting useful block */
3778 xfs_fileoff_t off; /* offset for this block */
3779 xfs_extnum_t nextents; /* number of extent entries */
3781 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
3782 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
3783 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3784 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3788 ifp = XFS_IFORK_PTR(ip, whichfork);
3789 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3790 (error = xfs_iread_extents(tp, ip, whichfork)))
3792 lowest = *first_unused;
3793 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3794 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
3795 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
3796 off = xfs_bmbt_get_startoff(ep);
3798 * See if the hole before this extent will work.
3800 if (off >= lowest + len && off - max >= len) {
3801 *first_unused = max;
3804 lastaddr = off + xfs_bmbt_get_blockcount(ep);
3805 max = XFS_FILEOFF_MAX(lastaddr, lowest);
3807 *first_unused = max;
3812 * Returns the file-relative block number of the last block + 1 before
3813 * last_block (input value) in the file.
3814 * This is not based on i_size, it is based on the extent records.
3815 * Returns 0 for local files, as they do not have extent records.
3818 xfs_bmap_last_before(
3819 xfs_trans_t *tp, /* transaction pointer */
3820 xfs_inode_t *ip, /* incore inode */
3821 xfs_fileoff_t *last_block, /* last block */
3822 int whichfork) /* data or attr fork */
3824 xfs_fileoff_t bno; /* input file offset */
3825 int eof; /* hit end of file */
3826 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
3827 int error; /* error return value */
3828 xfs_bmbt_irec_t got; /* current extent value */
3829 xfs_ifork_t *ifp; /* inode fork pointer */
3830 xfs_extnum_t lastx; /* last extent used */
3831 xfs_bmbt_irec_t prev; /* previous extent value */
3833 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3834 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3835 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3836 return XFS_ERROR(EIO);
3837 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3841 ifp = XFS_IFORK_PTR(ip, whichfork);
3842 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3843 (error = xfs_iread_extents(tp, ip, whichfork)))
3845 bno = *last_block - 1;
3846 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
3848 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
3849 if (prev.br_startoff == NULLFILEOFF)
3852 *last_block = prev.br_startoff + prev.br_blockcount;
3855 * Otherwise *last_block is already the right answer.
3861 xfs_bmap_last_extent(
3862 struct xfs_trans *tp,
3863 struct xfs_inode *ip,
3865 struct xfs_bmbt_irec *rec,
3868 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3872 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3873 error = xfs_iread_extents(tp, ip, whichfork);
3878 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
3879 if (nextents == 0) {
3884 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
3890 * Check the last inode extent to determine whether this allocation will result
3891 * in blocks being allocated at the end of the file. When we allocate new data
3892 * blocks at the end of the file which do not start at the previous data block,
3893 * we will try to align the new blocks at stripe unit boundaries.
3895 * Returns 0 in bma->aeof if the file (fork) is empty as any new write will be
3896 * at, or past the EOF.
3900 struct xfs_bmalloca *bma,
3903 struct xfs_bmbt_irec rec;
3908 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
3910 if (error || is_empty)
3914 * Check if we are allocation or past the last extent, or at least into
3915 * the last delayed allocated extent.
3917 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
3918 (bma->offset >= rec.br_startoff &&
3919 isnullstartblock(rec.br_startblock));
3924 * Check if the endoff is outside the last extent. If so the caller will grow
3925 * the allocation to a stripe unit boundary. All offsets are considered outside
3926 * the end of file for an empty fork, so 1 is returned in *eof in that case.
3930 struct xfs_inode *ip,
3931 xfs_fileoff_t endoff,
3935 struct xfs_bmbt_irec rec;
3938 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
3942 *eof = endoff >= rec.br_startoff + rec.br_blockcount;
3947 * Returns the file-relative block number of the first block past eof in
3948 * the file. This is not based on i_size, it is based on the extent records.
3949 * Returns 0 for local files, as they do not have extent records.
3952 xfs_bmap_last_offset(
3953 struct xfs_trans *tp,
3954 struct xfs_inode *ip,
3955 xfs_fileoff_t *last_block,
3958 struct xfs_bmbt_irec rec;
3964 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
3967 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3968 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
3969 return XFS_ERROR(EIO);
3971 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
3972 if (error || is_empty)
3975 *last_block = rec.br_startoff + rec.br_blockcount;
3980 * Returns whether the selected fork of the inode has exactly one
3981 * block or not. For the data fork we check this matches di_size,
3982 * implying the file's range is 0..bsize-1.
3984 int /* 1=>1 block, 0=>otherwise */
3986 xfs_inode_t *ip, /* incore inode */
3987 int whichfork) /* data or attr fork */
3989 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
3990 xfs_ifork_t *ifp; /* inode fork pointer */
3991 int rval; /* return value */
3992 xfs_bmbt_irec_t s; /* internal version of extent */
3995 if (whichfork == XFS_DATA_FORK) {
3996 return S_ISREG(ip->i_d.di_mode) ?
3997 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
3998 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4001 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4003 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4005 ifp = XFS_IFORK_PTR(ip, whichfork);
4006 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4007 ep = xfs_iext_get_ext(ifp, 0);
4008 xfs_bmbt_get_all(ep, &s);
4009 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4010 if (rval && whichfork == XFS_DATA_FORK)
4011 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
4016 xfs_bmap_sanity_check(
4017 struct xfs_mount *mp,
4021 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4023 if (block->bb_magic != cpu_to_be32(XFS_BMAP_MAGIC) ||
4024 be16_to_cpu(block->bb_level) != level ||
4025 be16_to_cpu(block->bb_numrecs) == 0 ||
4026 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4032 * Read in the extents to if_extents.
4033 * All inode fields are set up by caller, we just traverse the btree
4034 * and copy the records in. If the file system cannot contain unwritten
4035 * extents, the records are checked for no "state" flags.
4038 xfs_bmap_read_extents(
4039 xfs_trans_t *tp, /* transaction pointer */
4040 xfs_inode_t *ip, /* incore inode */
4041 int whichfork) /* data or attr fork */
4043 struct xfs_btree_block *block; /* current btree block */
4044 xfs_fsblock_t bno; /* block # of "block" */
4045 xfs_buf_t *bp; /* buffer for "block" */
4046 int error; /* error return value */
4047 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4048 xfs_extnum_t i, j; /* index into the extents list */
4049 xfs_ifork_t *ifp; /* fork structure */
4050 int level; /* btree level, for checking */
4051 xfs_mount_t *mp; /* file system mount structure */
4052 __be64 *pp; /* pointer to block address */
4054 xfs_extnum_t room; /* number of entries there's room for */
4058 ifp = XFS_IFORK_PTR(ip, whichfork);
4059 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4060 XFS_EXTFMT_INODE(ip);
4061 block = ifp->if_broot;
4063 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4065 level = be16_to_cpu(block->bb_level);
4067 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
4068 bno = be64_to_cpu(*pp);
4069 ASSERT(bno != NULLDFSBNO);
4070 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4071 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
4073 * Go down the tree until leaf level is reached, following the first
4074 * pointer (leftmost) at each level.
4076 while (level-- > 0) {
4077 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4078 XFS_BMAP_BTREE_REF)))
4080 block = XFS_BUF_TO_BLOCK(bp);
4081 XFS_WANT_CORRUPTED_GOTO(
4082 xfs_bmap_sanity_check(mp, bp, level),
4086 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
4087 bno = be64_to_cpu(*pp);
4088 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
4089 xfs_trans_brelse(tp, bp);
4092 * Here with bp and block set to the leftmost leaf node in the tree.
4094 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4097 * Loop over all leaf nodes. Copy information to the extent records.
4100 xfs_bmbt_rec_t *frp;
4101 xfs_fsblock_t nextbno;
4102 xfs_extnum_t num_recs;
4105 num_recs = xfs_btree_get_numrecs(block);
4106 if (unlikely(i + num_recs > room)) {
4107 ASSERT(i + num_recs <= room);
4108 xfs_warn(ip->i_mount,
4109 "corrupt dinode %Lu, (btree extents).",
4110 (unsigned long long) ip->i_ino);
4111 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
4112 XFS_ERRLEVEL_LOW, ip->i_mount, block);
4115 XFS_WANT_CORRUPTED_GOTO(
4116 xfs_bmap_sanity_check(mp, bp, 0),
4119 * Read-ahead the next leaf block, if any.
4121 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
4122 if (nextbno != NULLFSBLOCK)
4123 xfs_btree_reada_bufl(mp, nextbno, 1);
4125 * Copy records into the extent records.
4127 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
4129 for (j = 0; j < num_recs; j++, i++, frp++) {
4130 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
4131 trp->l0 = be64_to_cpu(frp->l0);
4132 trp->l1 = be64_to_cpu(frp->l1);
4134 if (exntf == XFS_EXTFMT_NOSTATE) {
4136 * Check all attribute bmap btree records and
4137 * any "older" data bmap btree records for a
4138 * set bit in the "extent flag" position.
4140 if (unlikely(xfs_check_nostate_extents(ifp,
4141 start, num_recs))) {
4142 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4148 xfs_trans_brelse(tp, bp);
4151 * If we've reached the end, stop.
4153 if (bno == NULLFSBLOCK)
4155 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4156 XFS_BMAP_BTREE_REF)))
4158 block = XFS_BUF_TO_BLOCK(bp);
4160 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4161 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4162 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
4165 xfs_trans_brelse(tp, bp);
4166 return XFS_ERROR(EFSCORRUPTED);
4171 * Add bmap trace insert entries for all the contents of the extent records.
4174 xfs_bmap_trace_exlist(
4175 xfs_inode_t *ip, /* incore inode pointer */
4176 xfs_extnum_t cnt, /* count of entries in the list */
4177 int whichfork, /* data or attr fork */
4178 unsigned long caller_ip)
4180 xfs_extnum_t idx; /* extent record index */
4181 xfs_ifork_t *ifp; /* inode fork pointer */
4184 if (whichfork == XFS_ATTR_FORK)
4185 state |= BMAP_ATTRFORK;
4187 ifp = XFS_IFORK_PTR(ip, whichfork);
4188 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4189 for (idx = 0; idx < cnt; idx++)
4190 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
4194 * Validate that the bmbt_irecs being returned from bmapi are valid
4195 * given the callers original parameters. Specifically check the
4196 * ranges of the returned irecs to ensure that they only extent beyond
4197 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4200 xfs_bmap_validate_ret(
4204 xfs_bmbt_irec_t *mval,
4208 int i; /* index to map values */
4210 ASSERT(ret_nmap <= nmap);
4212 for (i = 0; i < ret_nmap; i++) {
4213 ASSERT(mval[i].br_blockcount > 0);
4214 if (!(flags & XFS_BMAPI_ENTIRE)) {
4215 ASSERT(mval[i].br_startoff >= bno);
4216 ASSERT(mval[i].br_blockcount <= len);
4217 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4220 ASSERT(mval[i].br_startoff < bno + len);
4221 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4225 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4226 mval[i].br_startoff);
4227 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4228 mval[i].br_startblock != HOLESTARTBLOCK);
4229 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4230 mval[i].br_state == XFS_EXT_UNWRITTEN);
4237 * Trim the returned map to the required bounds
4241 struct xfs_bmbt_irec *mval,
4242 struct xfs_bmbt_irec *got,
4250 if ((flags & XFS_BMAPI_ENTIRE) ||
4251 got->br_startoff + got->br_blockcount <= obno) {
4253 if (isnullstartblock(got->br_startblock))
4254 mval->br_startblock = DELAYSTARTBLOCK;
4260 ASSERT((*bno >= obno) || (n == 0));
4262 mval->br_startoff = *bno;
4263 if (isnullstartblock(got->br_startblock))
4264 mval->br_startblock = DELAYSTARTBLOCK;
4266 mval->br_startblock = got->br_startblock +
4267 (*bno - got->br_startoff);
4269 * Return the minimum of what we got and what we asked for for
4270 * the length. We can use the len variable here because it is
4271 * modified below and we could have been there before coming
4272 * here if the first part of the allocation didn't overlap what
4275 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
4276 got->br_blockcount - (*bno - got->br_startoff));
4277 mval->br_state = got->br_state;
4278 ASSERT(mval->br_blockcount <= len);
4283 * Update and validate the extent map to return
4286 xfs_bmapi_update_map(
4287 struct xfs_bmbt_irec **map,
4295 xfs_bmbt_irec_t *mval = *map;
4297 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4298 ((mval->br_startoff + mval->br_blockcount) <= end));
4299 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4300 (mval->br_startoff < obno));
4302 *bno = mval->br_startoff + mval->br_blockcount;
4304 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4305 /* update previous map with new information */
4306 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4307 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4308 ASSERT(mval->br_state == mval[-1].br_state);
4309 mval[-1].br_blockcount = mval->br_blockcount;
4310 mval[-1].br_state = mval->br_state;
4311 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4312 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4313 mval[-1].br_startblock != HOLESTARTBLOCK &&
4314 mval->br_startblock == mval[-1].br_startblock +
4315 mval[-1].br_blockcount &&
4316 ((flags & XFS_BMAPI_IGSTATE) ||
4317 mval[-1].br_state == mval->br_state)) {
4318 ASSERT(mval->br_startoff ==
4319 mval[-1].br_startoff + mval[-1].br_blockcount);
4320 mval[-1].br_blockcount += mval->br_blockcount;
4321 } else if (*n > 0 &&
4322 mval->br_startblock == DELAYSTARTBLOCK &&
4323 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4324 mval->br_startoff ==
4325 mval[-1].br_startoff + mval[-1].br_blockcount) {
4326 mval[-1].br_blockcount += mval->br_blockcount;
4327 mval[-1].br_state = mval->br_state;
4328 } else if (!((*n == 0) &&
4329 ((mval->br_startoff + mval->br_blockcount) <=
4338 * Map file blocks to filesystem blocks without allocation.
4342 struct xfs_inode *ip,
4345 struct xfs_bmbt_irec *mval,
4349 struct xfs_mount *mp = ip->i_mount;
4350 struct xfs_ifork *ifp;
4351 struct xfs_bmbt_irec got;
4352 struct xfs_bmbt_irec prev;
4359 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4360 XFS_ATTR_FORK : XFS_DATA_FORK;
4363 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4364 XFS_BMAPI_IGSTATE)));
4366 if (unlikely(XFS_TEST_ERROR(
4367 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4368 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4369 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4370 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
4371 return XFS_ERROR(EFSCORRUPTED);
4374 if (XFS_FORCED_SHUTDOWN(mp))
4375 return XFS_ERROR(EIO);
4377 XFS_STATS_INC(xs_blk_mapr);
4379 ifp = XFS_IFORK_PTR(ip, whichfork);
4380 ASSERT(ifp->if_ext_max ==
4381 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4383 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4384 error = xfs_iread_extents(NULL, ip, whichfork);
4389 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4393 while (bno < end && n < *nmap) {
4394 /* Reading past eof, act as though there's a hole up to end. */
4396 got.br_startoff = end;
4397 if (got.br_startoff > bno) {
4398 /* Reading in a hole. */
4399 mval->br_startoff = bno;
4400 mval->br_startblock = HOLESTARTBLOCK;
4401 mval->br_blockcount =
4402 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4403 mval->br_state = XFS_EXT_NORM;
4404 bno += mval->br_blockcount;
4405 len -= mval->br_blockcount;
4411 /* set up the extent map to return. */
4412 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4413 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4415 /* If we're done, stop now. */
4416 if (bno >= end || n >= *nmap)
4419 /* Else go on to the next record. */
4420 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4421 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4430 xfs_bmapi_reserve_delalloc(
4431 struct xfs_inode *ip,
4434 struct xfs_bmbt_irec *got,
4435 struct xfs_bmbt_irec *prev,
4436 xfs_extnum_t *lastx,
4439 struct xfs_mount *mp = ip->i_mount;
4440 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4442 xfs_extlen_t indlen;
4443 char rt = XFS_IS_REALTIME_INODE(ip);
4447 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4449 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4451 /* Figure out the extent size, adjust alen */
4452 extsz = xfs_get_extsz_hint(ip);
4455 * Make sure we don't exceed a single extent length when we
4456 * align the extent by reducing length we are going to
4457 * allocate by the maximum amount extent size aligment may
4460 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1));
4461 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4462 1, 0, &aoff, &alen);
4467 extsz = alen / mp->m_sb.sb_rextsize;
4470 * Make a transaction-less quota reservation for delayed allocation
4471 * blocks. This number gets adjusted later. We return if we haven't
4472 * allocated blocks already inside this loop.
4474 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4475 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4480 * Split changing sb for alen and indlen since they could be coming
4481 * from different places.
4483 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4487 error = xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
4488 -((int64_t)extsz), 0);
4490 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4491 -((int64_t)alen), 0);
4495 goto out_unreserve_quota;
4497 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4498 -((int64_t)indlen), 0);
4500 goto out_unreserve_blocks;
4503 ip->i_delayed_blks += alen;
4505 got->br_startoff = aoff;
4506 got->br_startblock = nullstartblock(indlen);
4507 got->br_blockcount = alen;
4508 got->br_state = XFS_EXT_NORM;
4509 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
4512 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4513 * might have merged it into one of the neighbouring ones.
4515 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4517 ASSERT(got->br_startoff <= aoff);
4518 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4519 ASSERT(isnullstartblock(got->br_startblock));
4520 ASSERT(got->br_state == XFS_EXT_NORM);
4523 out_unreserve_blocks:
4525 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, extsz, 0);
4527 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, alen, 0);
4528 out_unreserve_quota:
4529 if (XFS_IS_QUOTA_ON(mp))
4530 xfs_trans_unreserve_quota_nblks(NULL, ip, alen, 0, rt ?
4531 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4536 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4540 struct xfs_inode *ip, /* incore inode */
4541 xfs_fileoff_t bno, /* starting file offs. mapped */
4542 xfs_filblks_t len, /* length to map in file */
4543 struct xfs_bmbt_irec *mval, /* output: map values */
4544 int *nmap, /* i/o: mval size/count */
4545 int flags) /* XFS_BMAPI_... */
4547 struct xfs_mount *mp = ip->i_mount;
4548 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4549 struct xfs_bmbt_irec got; /* current file extent record */
4550 struct xfs_bmbt_irec prev; /* previous file extent record */
4551 xfs_fileoff_t obno; /* old block number (offset) */
4552 xfs_fileoff_t end; /* end of mapped file region */
4553 xfs_extnum_t lastx; /* last useful extent number */
4554 int eof; /* we've hit the end of extents */
4555 int n = 0; /* current extent index */
4559 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4560 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
4562 if (unlikely(XFS_TEST_ERROR(
4563 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4564 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4565 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4566 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
4567 return XFS_ERROR(EFSCORRUPTED);
4570 if (XFS_FORCED_SHUTDOWN(mp))
4571 return XFS_ERROR(EIO);
4573 XFS_STATS_INC(xs_blk_mapw);
4575 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4576 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4581 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4585 while (bno < end && n < *nmap) {
4586 if (eof || got.br_startoff > bno) {
4587 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4588 &prev, &lastx, eof);
4598 /* set up the extent map to return. */
4599 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4600 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4602 /* If we're done, stop now. */
4603 if (bno >= end || n >= *nmap)
4606 /* Else go on to the next record. */
4608 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4609 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4621 struct xfs_bmalloca *bma,
4624 struct xfs_mount *mp = bma->ip->i_mount;
4625 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4626 XFS_ATTR_FORK : XFS_DATA_FORK;
4627 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4628 int tmp_logflags = 0;
4632 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(bma->ip);
4635 * For the wasdelay case, we could also just allocate the stuff asked
4636 * for in this bmap call but that wouldn't be as good.
4639 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4640 bma->offset = bma->got.br_startoff;
4641 if (bma->idx != NULLEXTNUM && bma->idx) {
4642 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
4646 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
4648 bma->length = XFS_FILBLKS_MIN(bma->length,
4649 bma->got.br_startoff - bma->offset);
4653 * Indicate if this is the first user data in the file, or just any
4656 if (!(flags & XFS_BMAPI_METADATA)) {
4657 bma->userdata = (bma->offset == 0) ?
4658 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
4661 bma->minlen = (flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
4664 * Only want to do the alignment at the eof if it is userdata and
4665 * allocation length is larger than a stripe unit.
4667 if (mp->m_dalign && bma->length >= mp->m_dalign &&
4668 !(flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
4669 error = xfs_bmap_isaeof(bma, whichfork);
4674 error = xfs_bmap_alloc(bma);
4678 if (bma->flist->xbf_low)
4681 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4682 if (bma->blkno == NULLFSBLOCK)
4684 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4685 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4686 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4687 bma->cur->bc_private.b.flist = bma->flist;
4690 * Bump the number of extents we've allocated
4696 bma->cur->bc_private.b.flags =
4697 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4699 bma->got.br_startoff = bma->offset;
4700 bma->got.br_startblock = bma->blkno;
4701 bma->got.br_blockcount = bma->length;
4702 bma->got.br_state = XFS_EXT_NORM;
4705 * A wasdelay extent has been initialized, so shouldn't be flagged
4708 if (!bma->wasdel && (flags & XFS_BMAPI_PREALLOC) &&
4709 xfs_sb_version_hasextflgbit(&mp->m_sb))
4710 bma->got.br_state = XFS_EXT_UNWRITTEN;
4713 error = xfs_bmap_add_extent_delay_real(bma);
4715 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
4717 bma->logflags |= tmp_logflags;
4722 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4723 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4724 * the neighbouring ones.
4726 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
4728 ASSERT(bma->got.br_startoff <= bma->offset);
4729 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4730 bma->offset + bma->length);
4731 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4732 bma->got.br_state == XFS_EXT_UNWRITTEN);
4737 xfs_bmapi_convert_unwritten(
4738 struct xfs_bmalloca *bma,
4739 struct xfs_bmbt_irec *mval,
4743 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4744 XFS_ATTR_FORK : XFS_DATA_FORK;
4745 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4746 int tmp_logflags = 0;
4749 /* check if we need to do unwritten->real conversion */
4750 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4751 (flags & XFS_BMAPI_PREALLOC))
4754 /* check if we need to do real->unwritten conversion */
4755 if (mval->br_state == XFS_EXT_NORM &&
4756 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4757 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4761 * Modify (by adding) the state flag, if writing.
4763 ASSERT(mval->br_blockcount <= len);
4764 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4765 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4766 bma->ip, whichfork);
4767 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4768 bma->cur->bc_private.b.flist = bma->flist;
4770 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4771 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4773 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
4774 &bma->cur, mval, bma->firstblock, bma->flist,
4776 bma->logflags |= tmp_logflags;
4781 * Update our extent pointer, given that
4782 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4783 * of the neighbouring ones.
4785 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
4788 * We may have combined previously unwritten space with written space,
4789 * so generate another request.
4791 if (mval->br_blockcount < len)
4797 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4798 * extent state if necessary. Details behaviour is controlled by the flags
4799 * parameter. Only allocates blocks from a single allocation group, to avoid
4802 * The returned value in "firstblock" from the first call in a transaction
4803 * must be remembered and presented to subsequent calls in "firstblock".
4804 * An upper bound for the number of blocks to be allocated is supplied to
4805 * the first call in "total"; if no allocation group has that many free
4806 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4810 struct xfs_trans *tp, /* transaction pointer */
4811 struct xfs_inode *ip, /* incore inode */
4812 xfs_fileoff_t bno, /* starting file offs. mapped */
4813 xfs_filblks_t len, /* length to map in file */
4814 int flags, /* XFS_BMAPI_... */
4815 xfs_fsblock_t *firstblock, /* first allocated block
4816 controls a.g. for allocs */
4817 xfs_extlen_t total, /* total blocks needed */
4818 struct xfs_bmbt_irec *mval, /* output: map values */
4819 int *nmap, /* i/o: mval size/count */
4820 struct xfs_bmap_free *flist) /* i/o: list extents to free */
4822 struct xfs_mount *mp = ip->i_mount;
4823 struct xfs_ifork *ifp;
4824 struct xfs_bmalloca bma = { 0 }; /* args for xfs_bmap_alloc */
4825 xfs_fileoff_t end; /* end of mapped file region */
4826 int eof; /* after the end of extents */
4827 int error; /* error return */
4828 int n; /* current extent index */
4829 xfs_fileoff_t obno; /* old block number (offset) */
4830 int whichfork; /* data or attr fork */
4831 char inhole; /* current location is hole in file */
4832 char wasdelay; /* old extent was delayed */
4835 xfs_fileoff_t orig_bno; /* original block number value */
4836 int orig_flags; /* original flags arg value */
4837 xfs_filblks_t orig_len; /* original value of len arg */
4838 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4839 int orig_nmap; /* original value of *nmap */
4849 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4850 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4853 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4854 XFS_ATTR_FORK : XFS_DATA_FORK;
4856 if (unlikely(XFS_TEST_ERROR(
4857 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4858 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4859 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4860 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4861 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
4862 return XFS_ERROR(EFSCORRUPTED);
4865 if (XFS_FORCED_SHUTDOWN(mp))
4866 return XFS_ERROR(EIO);
4868 ifp = XFS_IFORK_PTR(ip, whichfork);
4869 ASSERT(ifp->if_ext_max ==
4870 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4872 XFS_STATS_INC(xs_blk_mapw);
4874 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4875 error = xfs_bmap_local_to_extents(tp, ip, firstblock, total,
4876 &bma.logflags, whichfork);
4881 if (*firstblock == NULLFSBLOCK) {
4882 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4883 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4890 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4891 error = xfs_iread_extents(tp, ip, whichfork);
4896 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
4907 bma.firstblock = firstblock;
4909 while (bno < end && n < *nmap) {
4910 inhole = eof || bma.got.br_startoff > bno;
4911 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
4914 * First, deal with the hole before the allocated space
4915 * that we found, if any.
4917 if (inhole || wasdelay) {
4919 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4920 bma.wasdel = wasdelay;
4924 error = xfs_bmapi_allocate(&bma, flags);
4927 if (bma.blkno == NULLFSBLOCK)
4931 /* Deal with the allocated space we found. */
4932 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4935 /* Execute unwritten extent conversion if necessary */
4936 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
4937 if (error == EAGAIN)
4942 /* update the extent map to return */
4943 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4946 * If we're done, stop now. Stop when we've allocated
4947 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4948 * the transaction may get too big.
4950 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4953 /* Else go on to the next record. */
4955 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
4956 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
4964 * Transform from btree to extents, give it cur.
4966 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
4967 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
4968 int tmp_logflags = 0;
4971 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
4972 &tmp_logflags, whichfork);
4973 bma.logflags |= tmp_logflags;
4977 ASSERT(ifp->if_ext_max ==
4978 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4979 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4980 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
4984 * Log everything. Do this after conversion, there's no point in
4985 * logging the extent records if we've converted to btree format.
4987 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
4988 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4989 bma.logflags &= ~xfs_ilog_fext(whichfork);
4990 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
4991 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
4992 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
4994 * Log whatever the flags say, even if error. Otherwise we might miss
4995 * detecting a case where the data is changed, there's an error,
4996 * and it's not logged so we don't shutdown when we should.
4999 xfs_trans_log_inode(tp, ip, bma.logflags);
5003 ASSERT(*firstblock == NULLFSBLOCK ||
5004 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5006 bma.cur->bc_private.b.firstblock) ||
5008 XFS_FSB_TO_AGNO(mp, *firstblock) <
5010 bma.cur->bc_private.b.firstblock)));
5011 *firstblock = bma.cur->bc_private.b.firstblock;
5013 xfs_btree_del_cursor(bma.cur,
5014 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5017 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5023 * Unmap (remove) blocks from a file.
5024 * If nexts is nonzero then the number of extents to remove is limited to
5025 * that value. If not all extents in the block range can be removed then
5030 xfs_trans_t *tp, /* transaction pointer */
5031 struct xfs_inode *ip, /* incore inode */
5032 xfs_fileoff_t bno, /* starting offset to unmap */
5033 xfs_filblks_t len, /* length to unmap in file */
5034 int flags, /* misc flags */
5035 xfs_extnum_t nexts, /* number of extents max */
5036 xfs_fsblock_t *firstblock, /* first allocated block
5037 controls a.g. for allocs */
5038 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5039 int *done) /* set if not done yet */
5041 xfs_btree_cur_t *cur; /* bmap btree cursor */
5042 xfs_bmbt_irec_t del; /* extent being deleted */
5043 int eof; /* is deleting at eof */
5044 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
5045 int error; /* error return value */
5046 xfs_extnum_t extno; /* extent number in list */
5047 xfs_bmbt_irec_t got; /* current extent record */
5048 xfs_ifork_t *ifp; /* inode fork pointer */
5049 int isrt; /* freeing in rt area */
5050 xfs_extnum_t lastx; /* last extent index used */
5051 int logflags; /* transaction logging flags */
5052 xfs_extlen_t mod; /* rt extent offset */
5053 xfs_mount_t *mp; /* mount structure */
5054 xfs_extnum_t nextents; /* number of file extents */
5055 xfs_bmbt_irec_t prev; /* previous extent record */
5056 xfs_fileoff_t start; /* first file offset deleted */
5057 int tmp_logflags; /* partial logging flags */
5058 int wasdel; /* was a delayed alloc extent */
5059 int whichfork; /* data or attribute fork */
5062 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5064 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5065 XFS_ATTR_FORK : XFS_DATA_FORK;
5066 ifp = XFS_IFORK_PTR(ip, whichfork);
5068 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5069 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5070 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5072 return XFS_ERROR(EFSCORRUPTED);
5075 if (XFS_FORCED_SHUTDOWN(mp))
5076 return XFS_ERROR(EIO);
5080 ASSERT(ifp->if_ext_max ==
5081 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5082 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5083 (error = xfs_iread_extents(tp, ip, whichfork)))
5085 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5086 if (nextents == 0) {
5090 XFS_STATS_INC(xs_blk_unmap);
5091 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5093 bno = start + len - 1;
5094 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5098 * Check to see if the given block number is past the end of the
5099 * file, back up to the last block if so...
5102 ep = xfs_iext_get_ext(ifp, --lastx);
5103 xfs_bmbt_get_all(ep, &got);
5104 bno = got.br_startoff + got.br_blockcount - 1;
5107 if (ifp->if_flags & XFS_IFBROOT) {
5108 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5109 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5110 cur->bc_private.b.firstblock = *firstblock;
5111 cur->bc_private.b.flist = flist;
5112 cur->bc_private.b.flags = 0;
5116 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5117 (nexts == 0 || extno < nexts)) {
5119 * Is the found extent after a hole in which bno lives?
5120 * Just back up to the previous extent, if so.
5122 if (got.br_startoff > bno) {
5125 ep = xfs_iext_get_ext(ifp, lastx);
5126 xfs_bmbt_get_all(ep, &got);
5129 * Is the last block of this extent before the range
5130 * we're supposed to delete? If so, we're done.
5132 bno = XFS_FILEOFF_MIN(bno,
5133 got.br_startoff + got.br_blockcount - 1);
5137 * Then deal with the (possibly delayed) allocated space
5142 wasdel = isnullstartblock(del.br_startblock);
5143 if (got.br_startoff < start) {
5144 del.br_startoff = start;
5145 del.br_blockcount -= start - got.br_startoff;
5147 del.br_startblock += start - got.br_startoff;
5149 if (del.br_startoff + del.br_blockcount > bno + 1)
5150 del.br_blockcount = bno + 1 - del.br_startoff;
5151 sum = del.br_startblock + del.br_blockcount;
5153 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5155 * Realtime extent not lined up at the end.
5156 * The extent could have been split into written
5157 * and unwritten pieces, or we could just be
5158 * unmapping part of it. But we can't really
5159 * get rid of part of a realtime extent.
5161 if (del.br_state == XFS_EXT_UNWRITTEN ||
5162 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5164 * This piece is unwritten, or we're not
5165 * using unwritten extents. Skip over it.
5168 bno -= mod > del.br_blockcount ?
5169 del.br_blockcount : mod;
5170 if (bno < got.br_startoff) {
5172 xfs_bmbt_get_all(xfs_iext_get_ext(
5178 * It's written, turn it unwritten.
5179 * This is better than zeroing it.
5181 ASSERT(del.br_state == XFS_EXT_NORM);
5182 ASSERT(xfs_trans_get_block_res(tp) > 0);
5184 * If this spans a realtime extent boundary,
5185 * chop it back to the start of the one we end at.
5187 if (del.br_blockcount > mod) {
5188 del.br_startoff += del.br_blockcount - mod;
5189 del.br_startblock += del.br_blockcount - mod;
5190 del.br_blockcount = mod;
5192 del.br_state = XFS_EXT_UNWRITTEN;
5193 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5194 &lastx, &cur, &del, firstblock, flist,
5200 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5202 * Realtime extent is lined up at the end but not
5203 * at the front. We'll get rid of full extents if
5206 mod = mp->m_sb.sb_rextsize - mod;
5207 if (del.br_blockcount > mod) {
5208 del.br_blockcount -= mod;
5209 del.br_startoff += mod;
5210 del.br_startblock += mod;
5211 } else if ((del.br_startoff == start &&
5212 (del.br_state == XFS_EXT_UNWRITTEN ||
5213 xfs_trans_get_block_res(tp) == 0)) ||
5214 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5216 * Can't make it unwritten. There isn't
5217 * a full extent here so just skip it.
5219 ASSERT(bno >= del.br_blockcount);
5220 bno -= del.br_blockcount;
5221 if (got.br_startoff > bno) {
5223 ep = xfs_iext_get_ext(ifp,
5225 xfs_bmbt_get_all(ep, &got);
5229 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5231 * This one is already unwritten.
5232 * It must have a written left neighbor.
5233 * Unwrite the killed part of that one and
5237 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5239 ASSERT(prev.br_state == XFS_EXT_NORM);
5240 ASSERT(!isnullstartblock(prev.br_startblock));
5241 ASSERT(del.br_startblock ==
5242 prev.br_startblock + prev.br_blockcount);
5243 if (prev.br_startoff < start) {
5244 mod = start - prev.br_startoff;
5245 prev.br_blockcount -= mod;
5246 prev.br_startblock += mod;
5247 prev.br_startoff = start;
5249 prev.br_state = XFS_EXT_UNWRITTEN;
5251 error = xfs_bmap_add_extent_unwritten_real(tp,
5252 ip, &lastx, &cur, &prev,
5253 firstblock, flist, &logflags);
5258 ASSERT(del.br_state == XFS_EXT_NORM);
5259 del.br_state = XFS_EXT_UNWRITTEN;
5260 error = xfs_bmap_add_extent_unwritten_real(tp,
5261 ip, &lastx, &cur, &del,
5262 firstblock, flist, &logflags);
5269 ASSERT(startblockval(del.br_startblock) > 0);
5270 /* Update realtime/data freespace, unreserve quota */
5272 xfs_filblks_t rtexts;
5274 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5275 do_div(rtexts, mp->m_sb.sb_rextsize);
5276 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5277 (int64_t)rtexts, 0);
5278 (void)xfs_trans_reserve_quota_nblks(NULL,
5279 ip, -((long)del.br_blockcount), 0,
5280 XFS_QMOPT_RES_RTBLKS);
5282 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
5283 (int64_t)del.br_blockcount, 0);
5284 (void)xfs_trans_reserve_quota_nblks(NULL,
5285 ip, -((long)del.br_blockcount), 0,
5286 XFS_QMOPT_RES_REGBLKS);
5288 ip->i_delayed_blks -= del.br_blockcount;
5290 cur->bc_private.b.flags |=
5291 XFS_BTCUR_BPRV_WASDEL;
5293 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5295 * If it's the case where the directory code is running
5296 * with no block reservation, and the deleted block is in
5297 * the middle of its extent, and the resulting insert
5298 * of an extent would cause transformation to btree format,
5299 * then reject it. The calling code will then swap
5300 * blocks around instead.
5301 * We have to do this now, rather than waiting for the
5302 * conversion to btree format, since the transaction
5305 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5306 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5307 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5308 del.br_startoff > got.br_startoff &&
5309 del.br_startoff + del.br_blockcount <
5310 got.br_startoff + got.br_blockcount) {
5311 error = XFS_ERROR(ENOSPC);
5314 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
5315 &tmp_logflags, whichfork);
5316 logflags |= tmp_logflags;
5319 bno = del.br_startoff - 1;
5322 * If not done go on to the next (previous) record.
5324 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5326 ep = xfs_iext_get_ext(ifp, lastx);
5327 if (xfs_bmbt_get_startoff(ep) > bno) {
5329 ep = xfs_iext_get_ext(ifp,
5332 xfs_bmbt_get_all(ep, &got);
5337 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5338 ASSERT(ifp->if_ext_max ==
5339 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5341 * Convert to a btree if necessary.
5343 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5344 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5345 ASSERT(cur == NULL);
5346 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5347 &cur, 0, &tmp_logflags, whichfork);
5348 logflags |= tmp_logflags;
5353 * transform from btree to extents, give it cur
5355 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5356 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5357 ASSERT(cur != NULL);
5358 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5360 logflags |= tmp_logflags;
5365 * transform from extents to local?
5367 ASSERT(ifp->if_ext_max ==
5368 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5372 * Log everything. Do this after conversion, there's no point in
5373 * logging the extent records if we've converted to btree format.
5375 if ((logflags & xfs_ilog_fext(whichfork)) &&
5376 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5377 logflags &= ~xfs_ilog_fext(whichfork);
5378 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5379 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5380 logflags &= ~xfs_ilog_fbroot(whichfork);
5382 * Log inode even in the error case, if the transaction
5383 * is dirty we'll need to shut down the filesystem.
5386 xfs_trans_log_inode(tp, ip, logflags);
5389 *firstblock = cur->bc_private.b.firstblock;
5390 cur->bc_private.b.allocated = 0;
5392 xfs_btree_del_cursor(cur,
5393 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5399 * returns 1 for success, 0 if we failed to map the extent.
5402 xfs_getbmapx_fix_eof_hole(
5403 xfs_inode_t *ip, /* xfs incore inode pointer */
5404 struct getbmapx *out, /* output structure */
5405 int prealloced, /* this is a file with
5406 * preallocated data space */
5407 __int64_t end, /* last block requested */
5408 xfs_fsblock_t startblock)
5411 xfs_mount_t *mp; /* file system mount point */
5412 xfs_ifork_t *ifp; /* inode fork pointer */
5413 xfs_extnum_t lastx; /* last extent pointer */
5414 xfs_fileoff_t fileblock;
5416 if (startblock == HOLESTARTBLOCK) {
5418 out->bmv_block = -1;
5419 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5420 fixlen -= out->bmv_offset;
5421 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5422 /* Came to hole at EOF. Trim it. */
5425 out->bmv_length = fixlen;
5428 if (startblock == DELAYSTARTBLOCK)
5429 out->bmv_block = -2;
5431 out->bmv_block = xfs_fsb_to_db(ip, startblock);
5432 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5433 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5434 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5435 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5436 out->bmv_oflags |= BMV_OF_LAST;
5443 * Get inode's extents as described in bmv, and format for output.
5444 * Calls formatter to fill the user's buffer until all extents
5445 * are mapped, until the passed-in bmv->bmv_count slots have
5446 * been filled, or until the formatter short-circuits the loop,
5447 * if it is tracking filled-in extents on its own.
5449 int /* error code */
5452 struct getbmapx *bmv, /* user bmap structure */
5453 xfs_bmap_format_t formatter, /* format to user */
5454 void *arg) /* formatter arg */
5456 __int64_t bmvend; /* last block requested */
5457 int error = 0; /* return value */
5458 __int64_t fixlen; /* length for -1 case */
5459 int i; /* extent number */
5460 int lock; /* lock state */
5461 xfs_bmbt_irec_t *map; /* buffer for user's data */
5462 xfs_mount_t *mp; /* file system mount point */
5463 int nex; /* # of user extents can do */
5464 int nexleft; /* # of user extents left */
5465 int subnex; /* # of bmapi's can do */
5466 int nmap; /* number of map entries */
5467 struct getbmapx *out; /* output structure */
5468 int whichfork; /* data or attr fork */
5469 int prealloced; /* this is a file with
5470 * preallocated data space */
5471 int iflags; /* interface flags */
5472 int bmapi_flags; /* flags for xfs_bmapi */
5476 iflags = bmv->bmv_iflags;
5477 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5479 if (whichfork == XFS_ATTR_FORK) {
5480 if (XFS_IFORK_Q(ip)) {
5481 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5482 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5483 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5484 return XFS_ERROR(EINVAL);
5485 } else if (unlikely(
5486 ip->i_d.di_aformat != 0 &&
5487 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5488 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5490 return XFS_ERROR(EFSCORRUPTED);
5496 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5497 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5498 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5499 return XFS_ERROR(EINVAL);
5501 if (xfs_get_extsz_hint(ip) ||
5502 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5504 fixlen = XFS_MAXIOFFSET(mp);
5507 fixlen = ip->i_size;
5511 if (bmv->bmv_length == -1) {
5512 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5514 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5515 } else if (bmv->bmv_length == 0) {
5516 bmv->bmv_entries = 0;
5518 } else if (bmv->bmv_length < 0) {
5519 return XFS_ERROR(EINVAL);
5522 nex = bmv->bmv_count - 1;
5524 return XFS_ERROR(EINVAL);
5525 bmvend = bmv->bmv_offset + bmv->bmv_length;
5528 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5529 return XFS_ERROR(ENOMEM);
5530 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5532 return XFS_ERROR(ENOMEM);
5534 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5535 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5536 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5537 error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5539 goto out_unlock_iolock;
5542 * even after flushing the inode, there can still be delalloc
5543 * blocks on the inode beyond EOF due to speculative
5544 * preallocation. These are not removed until the release
5545 * function is called or the inode is inactivated. Hence we
5546 * cannot assert here that ip->i_delayed_blks == 0.
5550 lock = xfs_ilock_map_shared(ip);
5553 * Don't let nex be bigger than the number of extents
5554 * we can have assuming alternating holes and real extents.
5556 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5557 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5559 bmapi_flags = xfs_bmapi_aflag(whichfork);
5560 if (!(iflags & BMV_IF_PREALLOC))
5561 bmapi_flags |= XFS_BMAPI_IGSTATE;
5564 * Allocate enough space to handle "subnex" maps at a time.
5568 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
5570 goto out_unlock_ilock;
5572 bmv->bmv_entries = 0;
5574 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5575 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5583 nmap = (nexleft > subnex) ? subnex : nexleft;
5584 error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5585 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5586 map, &nmap, bmapi_flags);
5589 ASSERT(nmap <= subnex);
5591 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5592 out[cur_ext].bmv_oflags = 0;
5593 if (map[i].br_state == XFS_EXT_UNWRITTEN)
5594 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
5595 else if (map[i].br_startblock == DELAYSTARTBLOCK)
5596 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5597 out[cur_ext].bmv_offset =
5598 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5599 out[cur_ext].bmv_length =
5600 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5601 out[cur_ext].bmv_unused1 = 0;
5602 out[cur_ext].bmv_unused2 = 0;
5603 ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
5604 (map[i].br_startblock != DELAYSTARTBLOCK));
5605 if (map[i].br_startblock == HOLESTARTBLOCK &&
5606 whichfork == XFS_ATTR_FORK) {
5607 /* came to the end of attribute fork */
5608 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
5612 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5614 map[i].br_startblock))
5618 out[cur_ext].bmv_offset +
5619 out[cur_ext].bmv_length;
5621 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
5624 * In case we don't want to return the hole,
5625 * don't increase cur_ext so that we can reuse
5626 * it in the next loop.
5628 if ((iflags & BMV_IF_NO_HOLES) &&
5629 map[i].br_startblock == HOLESTARTBLOCK) {
5630 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
5638 } while (nmap && nexleft && bmv->bmv_length);
5643 xfs_iunlock_map_shared(ip, lock);
5645 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5647 for (i = 0; i < cur_ext; i++) {
5648 int full = 0; /* user array is full */
5650 /* format results & advance arg */
5651 error = formatter(&arg, &out[i], &full);
5661 STATIC struct xfs_buf *
5663 struct xfs_btree_cur *cur,
5666 struct xfs_log_item_desc *lidp;
5672 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5673 if (!cur->bc_bufs[i])
5675 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
5676 return cur->bc_bufs[i];
5679 /* Chase down all the log items to see if the bp is there */
5680 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
5681 struct xfs_buf_log_item *bip;
5682 bip = (struct xfs_buf_log_item *)lidp->lid_item;
5683 if (bip->bli_item.li_type == XFS_LI_BUF &&
5684 XFS_BUF_ADDR(bip->bli_buf) == bno)
5685 return bip->bli_buf;
5693 struct xfs_btree_block *block,
5699 __be64 *pp, *thispa; /* pointer to block address */
5700 xfs_bmbt_key_t *prevp, *keyp;
5702 ASSERT(be16_to_cpu(block->bb_level) > 0);
5705 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
5706 dmxr = mp->m_bmap_dmxr[0];
5707 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
5710 ASSERT(be64_to_cpu(prevp->br_startoff) <
5711 be64_to_cpu(keyp->br_startoff));
5716 * Compare the block numbers to see if there are dups.
5719 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
5721 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5723 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
5725 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
5727 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
5728 if (*thispa == *pp) {
5729 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
5731 (unsigned long long)be64_to_cpu(*thispa));
5732 panic("%s: ptrs are equal in node\n",
5740 * Check that the extents for the inode ip are in the right order in all
5745 xfs_bmap_check_leaf_extents(
5746 xfs_btree_cur_t *cur, /* btree cursor or null */
5747 xfs_inode_t *ip, /* incore inode pointer */
5748 int whichfork) /* data or attr fork */
5750 struct xfs_btree_block *block; /* current btree block */
5751 xfs_fsblock_t bno; /* block # of "block" */
5752 xfs_buf_t *bp; /* buffer for "block" */
5753 int error; /* error return value */
5754 xfs_extnum_t i=0, j; /* index into the extents list */
5755 xfs_ifork_t *ifp; /* fork structure */
5756 int level; /* btree level, for checking */
5757 xfs_mount_t *mp; /* file system mount structure */
5758 __be64 *pp; /* pointer to block address */
5759 xfs_bmbt_rec_t *ep; /* pointer to current extent */
5760 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
5761 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
5764 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5770 ifp = XFS_IFORK_PTR(ip, whichfork);
5771 block = ifp->if_broot;
5773 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5775 level = be16_to_cpu(block->bb_level);
5777 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
5778 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
5779 bno = be64_to_cpu(*pp);
5781 ASSERT(bno != NULLDFSBNO);
5782 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5783 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5786 * Go down the tree until leaf level is reached, following the first
5787 * pointer (leftmost) at each level.
5789 while (level-- > 0) {
5790 /* See if buf is in cur first */
5791 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5797 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5798 XFS_BMAP_BTREE_REF)))
5800 block = XFS_BUF_TO_BLOCK(bp);
5801 XFS_WANT_CORRUPTED_GOTO(
5802 xfs_bmap_sanity_check(mp, bp, level),
5808 * Check this block for basic sanity (increasing keys and
5809 * no duplicate blocks).
5812 xfs_check_block(block, mp, 0, 0);
5813 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
5814 bno = be64_to_cpu(*pp);
5815 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
5818 xfs_trans_brelse(NULL, bp);
5823 * Here with bp and block set to the leftmost leaf node in the tree.
5828 * Loop over all leaf nodes checking that all extents are in the right order.
5831 xfs_fsblock_t nextbno;
5832 xfs_extnum_t num_recs;
5835 num_recs = xfs_btree_get_numrecs(block);
5838 * Read-ahead the next leaf block, if any.
5841 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
5844 * Check all the extents to make sure they are OK.
5845 * If we had a previous block, the last entry should
5846 * conform with the first entry in this one.
5849 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
5851 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
5852 xfs_bmbt_disk_get_blockcount(&last) <=
5853 xfs_bmbt_disk_get_startoff(ep));
5855 for (j = 1; j < num_recs; j++) {
5856 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
5857 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
5858 xfs_bmbt_disk_get_blockcount(ep) <=
5859 xfs_bmbt_disk_get_startoff(nextp));
5867 xfs_trans_brelse(NULL, bp);
5871 * If we've reached the end, stop.
5873 if (bno == NULLFSBLOCK)
5876 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5882 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5883 XFS_BMAP_BTREE_REF)))
5885 block = XFS_BUF_TO_BLOCK(bp);
5889 xfs_trans_brelse(NULL, bp);
5894 xfs_warn(mp, "%s: at error0", __func__);
5896 xfs_trans_brelse(NULL, bp);
5898 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
5900 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
5906 * Count fsblocks of the given fork.
5909 xfs_bmap_count_blocks(
5910 xfs_trans_t *tp, /* transaction pointer */
5911 xfs_inode_t *ip, /* incore inode */
5912 int whichfork, /* data or attr fork */
5913 int *count) /* out: count of blocks */
5915 struct xfs_btree_block *block; /* current btree block */
5916 xfs_fsblock_t bno; /* block # of "block" */
5917 xfs_ifork_t *ifp; /* fork structure */
5918 int level; /* btree level, for checking */
5919 xfs_mount_t *mp; /* file system mount structure */
5920 __be64 *pp; /* pointer to block address */
5924 ifp = XFS_IFORK_PTR(ip, whichfork);
5925 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
5926 xfs_bmap_count_leaves(ifp, 0,
5927 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
5933 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5935 block = ifp->if_broot;
5936 level = be16_to_cpu(block->bb_level);
5938 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
5939 bno = be64_to_cpu(*pp);
5940 ASSERT(bno != NULLDFSBNO);
5941 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5942 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5944 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
5945 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
5947 return XFS_ERROR(EFSCORRUPTED);
5954 * Recursively walks each level of a btree
5955 * to count total fsblocks is use.
5957 STATIC int /* error */
5958 xfs_bmap_count_tree(
5959 xfs_mount_t *mp, /* file system mount point */
5960 xfs_trans_t *tp, /* transaction pointer */
5961 xfs_ifork_t *ifp, /* inode fork pointer */
5962 xfs_fsblock_t blockno, /* file system block number */
5963 int levelin, /* level in btree */
5964 int *count) /* Count of blocks */
5967 xfs_buf_t *bp, *nbp;
5968 int level = levelin;
5970 xfs_fsblock_t bno = blockno;
5971 xfs_fsblock_t nextbno;
5972 struct xfs_btree_block *block, *nextblock;
5975 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
5978 block = XFS_BUF_TO_BLOCK(bp);
5981 /* Not at node above leaves, count this level of nodes */
5982 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
5983 while (nextbno != NULLFSBLOCK) {
5984 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
5985 0, &nbp, XFS_BMAP_BTREE_REF)))
5988 nextblock = XFS_BUF_TO_BLOCK(nbp);
5989 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
5990 xfs_trans_brelse(tp, nbp);
5993 /* Dive to the next level */
5994 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
5995 bno = be64_to_cpu(*pp);
5996 if (unlikely((error =
5997 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
5998 xfs_trans_brelse(tp, bp);
5999 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6000 XFS_ERRLEVEL_LOW, mp);
6001 return XFS_ERROR(EFSCORRUPTED);
6003 xfs_trans_brelse(tp, bp);
6005 /* count all level 1 nodes and their leaves */
6007 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6008 numrecs = be16_to_cpu(block->bb_numrecs);
6009 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
6010 xfs_trans_brelse(tp, bp);
6011 if (nextbno == NULLFSBLOCK)
6014 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6015 XFS_BMAP_BTREE_REF)))
6018 block = XFS_BUF_TO_BLOCK(bp);
6025 * Count leaf blocks given a range of extent records.
6028 xfs_bmap_count_leaves(
6036 for (b = 0; b < numrecs; b++) {
6037 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
6038 *count += xfs_bmbt_get_blockcount(frp);
6043 * Count leaf blocks given a range of extent records originally
6047 xfs_bmap_disk_count_leaves(
6048 struct xfs_mount *mp,
6049 struct xfs_btree_block *block,
6054 xfs_bmbt_rec_t *frp;
6056 for (b = 1; b <= numrecs; b++) {
6057 frp = XFS_BMBT_REC_ADDR(mp, block, b);
6058 *count += xfs_bmbt_disk_get_blockcount(frp);
6063 * dead simple method of punching delalyed allocation blocks from a range in
6064 * the inode. Walks a block at a time so will be slow, but is only executed in
6065 * rare error cases so the overhead is not critical. This will alays punch out
6066 * both the start and end blocks, even if the ranges only partially overlap
6067 * them, so it is up to the caller to ensure that partial blocks are not
6071 xfs_bmap_punch_delalloc_range(
6072 struct xfs_inode *ip,
6073 xfs_fileoff_t start_fsb,
6074 xfs_fileoff_t length)
6076 xfs_fileoff_t remaining = length;
6079 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6083 xfs_bmbt_irec_t imap;
6085 xfs_fsblock_t firstblock;
6086 xfs_bmap_free_t flist;
6089 * Map the range first and check that it is a delalloc extent
6090 * before trying to unmap the range. Otherwise we will be
6091 * trying to remove a real extent (which requires a
6092 * transaction) or a hole, which is probably a bad idea...
6094 error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
6098 /* something screwed, just bail */
6099 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
6100 xfs_alert(ip->i_mount,
6101 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6102 ip->i_ino, start_fsb);
6110 if (imap.br_startblock != DELAYSTARTBLOCK) {
6111 /* been converted, ignore */
6114 WARN_ON(imap.br_blockcount == 0);
6117 * Note: while we initialise the firstblock/flist pair, they
6118 * should never be used because blocks should never be
6119 * allocated or freed for a delalloc extent and hence we need
6120 * don't cancel or finish them after the xfs_bunmapi() call.
6122 xfs_bmap_init(&flist, &firstblock);
6123 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6128 ASSERT(!flist.xbf_count && !flist.xbf_first);
6132 } while(remaining > 0);