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"
44 #include "xfs_quota.h"
45 #include "xfs_trans_space.h"
46 #include "xfs_buf_item.h"
47 #include "xfs_filestream.h"
48 #include "xfs_vnodeops.h"
49 #include "xfs_trace.h"
52 kmem_zone_t *xfs_bmap_free_item_zone;
55 * Prototypes for internal bmap routines.
60 xfs_bmap_check_leaf_extents(
61 struct xfs_btree_cur *cur,
65 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
70 * Called from xfs_bmap_add_attrfork to handle extents format files.
72 STATIC int /* error */
73 xfs_bmap_add_attrfork_extents(
74 xfs_trans_t *tp, /* transaction pointer */
75 xfs_inode_t *ip, /* incore inode pointer */
76 xfs_fsblock_t *firstblock, /* first block allocated */
77 xfs_bmap_free_t *flist, /* blocks to free at commit */
78 int *flags); /* inode logging flags */
81 * Called from xfs_bmap_add_attrfork to handle local format files.
83 STATIC int /* error */
84 xfs_bmap_add_attrfork_local(
85 xfs_trans_t *tp, /* transaction pointer */
86 xfs_inode_t *ip, /* incore inode pointer */
87 xfs_fsblock_t *firstblock, /* first block allocated */
88 xfs_bmap_free_t *flist, /* blocks to free at commit */
89 int *flags); /* inode logging flags */
92 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
93 * It figures out where to ask the underlying allocator to put the new extent.
95 STATIC int /* error */
97 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
100 * Transform a btree format file with only one leaf node, where the
101 * extents list will fit in the inode, into an extents format file.
102 * Since the file extents are already in-core, all we have to do is
103 * give up the space for the btree root and pitch the leaf block.
105 STATIC int /* error */
106 xfs_bmap_btree_to_extents(
107 xfs_trans_t *tp, /* transaction pointer */
108 xfs_inode_t *ip, /* incore inode pointer */
109 xfs_btree_cur_t *cur, /* btree cursor */
110 int *logflagsp, /* inode logging flags */
111 int whichfork); /* data or attr fork */
114 * Remove the entry "free" from the free item list. Prev points to the
115 * previous entry, unless "free" is the head of the list.
119 xfs_bmap_free_t *flist, /* free item list header */
120 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
121 xfs_bmap_free_item_t *free); /* list item to be freed */
124 * Convert an extents-format file into a btree-format file.
125 * The new file will have a root block (in the inode) and a single child block.
127 STATIC int /* error */
128 xfs_bmap_extents_to_btree(
129 xfs_trans_t *tp, /* transaction pointer */
130 xfs_inode_t *ip, /* incore inode pointer */
131 xfs_fsblock_t *firstblock, /* first-block-allocated */
132 xfs_bmap_free_t *flist, /* blocks freed in xaction */
133 xfs_btree_cur_t **curp, /* cursor returned to caller */
134 int wasdel, /* converting a delayed alloc */
135 int *logflagsp, /* inode logging flags */
136 int whichfork); /* data or attr fork */
139 * Convert a local file to an extents file.
140 * This code is sort of bogus, since the file data needs to get
141 * logged so it won't be lost. The bmap-level manipulations are ok, though.
143 STATIC int /* error */
144 xfs_bmap_local_to_extents(
145 xfs_trans_t *tp, /* transaction pointer */
146 xfs_inode_t *ip, /* incore inode pointer */
147 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
148 xfs_extlen_t total, /* total blocks needed by transaction */
149 int *logflagsp, /* inode logging flags */
150 int whichfork, /* data or attr fork */
151 void (*init_fn)(struct xfs_buf *bp,
152 struct xfs_inode *ip,
153 struct xfs_ifork *ifp));
156 * Search the extents list for the inode, for the extent containing bno.
157 * If bno lies in a hole, point to the next entry. If bno lies past eof,
158 * *eofp will be set, and *prevp will contain the last entry (null if none).
159 * Else, *lastxp will be set to the index of the found
160 * entry; *gotp will contain the entry.
162 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
163 xfs_bmap_search_extents(
164 xfs_inode_t *ip, /* incore inode pointer */
165 xfs_fileoff_t bno, /* block number searched for */
166 int whichfork, /* data or attr fork */
167 int *eofp, /* out: end of file found */
168 xfs_extnum_t *lastxp, /* out: last extent index */
169 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
170 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
173 * Compute the worst-case number of indirect blocks that will be used
174 * for ip's delayed extent of length "len".
177 xfs_bmap_worst_indlen(
178 xfs_inode_t *ip, /* incore inode pointer */
179 xfs_filblks_t len); /* delayed extent length */
183 * Perform various validation checks on the values being returned
187 xfs_bmap_validate_ret(
191 xfs_bmbt_irec_t *mval,
195 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
203 xfs_fsblock_t blockno,
208 xfs_bmap_count_leaves(
215 xfs_bmap_disk_count_leaves(
216 struct xfs_mount *mp,
217 struct xfs_btree_block *block,
222 * Bmap internal routines.
225 STATIC int /* error */
227 struct xfs_btree_cur *cur,
231 int *stat) /* success/failure */
233 cur->bc_rec.b.br_startoff = off;
234 cur->bc_rec.b.br_startblock = bno;
235 cur->bc_rec.b.br_blockcount = len;
236 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
239 STATIC int /* error */
241 struct xfs_btree_cur *cur,
245 int *stat) /* success/failure */
247 cur->bc_rec.b.br_startoff = off;
248 cur->bc_rec.b.br_startblock = bno;
249 cur->bc_rec.b.br_blockcount = len;
250 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
254 * Check if the inode needs to be converted to btree format.
256 static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
258 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
259 XFS_IFORK_NEXTENTS(ip, whichfork) >
260 XFS_IFORK_MAXEXT(ip, whichfork);
264 * Check if the inode should be converted to extent format.
266 static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
268 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
269 XFS_IFORK_NEXTENTS(ip, whichfork) <=
270 XFS_IFORK_MAXEXT(ip, whichfork);
274 * Update the record referred to by cur to the value given
275 * by [off, bno, len, state].
276 * This either works (return 0) or gets an EFSCORRUPTED error.
280 struct xfs_btree_cur *cur,
286 union xfs_btree_rec rec;
288 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
289 return xfs_btree_update(cur, &rec);
293 * Called from xfs_bmap_add_attrfork to handle btree format files.
295 STATIC int /* error */
296 xfs_bmap_add_attrfork_btree(
297 xfs_trans_t *tp, /* transaction pointer */
298 xfs_inode_t *ip, /* incore inode pointer */
299 xfs_fsblock_t *firstblock, /* first block allocated */
300 xfs_bmap_free_t *flist, /* blocks to free at commit */
301 int *flags) /* inode logging flags */
303 xfs_btree_cur_t *cur; /* btree cursor */
304 int error; /* error return value */
305 xfs_mount_t *mp; /* file system mount struct */
306 int stat; /* newroot status */
309 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
310 *flags |= XFS_ILOG_DBROOT;
312 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
313 cur->bc_private.b.flist = flist;
314 cur->bc_private.b.firstblock = *firstblock;
315 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
317 /* must be at least one entry */
318 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
319 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
322 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
323 return XFS_ERROR(ENOSPC);
325 *firstblock = cur->bc_private.b.firstblock;
326 cur->bc_private.b.allocated = 0;
327 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
331 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
336 * Called from xfs_bmap_add_attrfork to handle extents format files.
338 STATIC int /* error */
339 xfs_bmap_add_attrfork_extents(
340 xfs_trans_t *tp, /* transaction pointer */
341 xfs_inode_t *ip, /* incore inode pointer */
342 xfs_fsblock_t *firstblock, /* first block allocated */
343 xfs_bmap_free_t *flist, /* blocks to free at commit */
344 int *flags) /* inode logging flags */
346 xfs_btree_cur_t *cur; /* bmap btree cursor */
347 int error; /* error return value */
349 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
352 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
353 flags, XFS_DATA_FORK);
355 cur->bc_private.b.allocated = 0;
356 xfs_btree_del_cursor(cur,
357 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
363 * Block initialisation functions for local to extent format conversion.
364 * As these get more complex, they will be moved to the relevant files,
365 * but for now they are too simple to worry about.
368 xfs_bmap_local_to_extents_init_fn(
370 struct xfs_inode *ip,
371 struct xfs_ifork *ifp)
373 bp->b_ops = &xfs_bmbt_buf_ops;
374 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
378 xfs_symlink_local_to_remote(
380 struct xfs_inode *ip,
381 struct xfs_ifork *ifp)
383 /* remote symlink blocks are not verifiable until CRCs come along */
385 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
389 * Called from xfs_bmap_add_attrfork to handle local format files. Each
390 * different data fork content type needs a different callout to do the
391 * conversion. Some are basic and only require special block initialisation
392 * callouts for the data formating, others (directories) are so specialised they
393 * handle everything themselves.
395 * XXX (dgc): investigate whether directory conversion can use the generic
396 * formatting callout. It should be possible - it's just a very complex
397 * formatter. it would also require passing the transaction through to the init
400 STATIC int /* error */
401 xfs_bmap_add_attrfork_local(
402 xfs_trans_t *tp, /* transaction pointer */
403 xfs_inode_t *ip, /* incore inode pointer */
404 xfs_fsblock_t *firstblock, /* first block allocated */
405 xfs_bmap_free_t *flist, /* blocks to free at commit */
406 int *flags) /* inode logging flags */
408 xfs_da_args_t dargs; /* args for dir/attr code */
410 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
413 if (S_ISDIR(ip->i_d.di_mode)) {
414 memset(&dargs, 0, sizeof(dargs));
416 dargs.firstblock = firstblock;
418 dargs.total = ip->i_mount->m_dirblkfsbs;
419 dargs.whichfork = XFS_DATA_FORK;
421 return xfs_dir2_sf_to_block(&dargs);
424 if (S_ISLNK(ip->i_d.di_mode))
425 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
426 flags, XFS_DATA_FORK,
427 xfs_symlink_local_to_remote);
429 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
431 xfs_bmap_local_to_extents_init_fn);
435 * Convert a delayed allocation to a real allocation.
437 STATIC int /* error */
438 xfs_bmap_add_extent_delay_real(
439 struct xfs_bmalloca *bma)
441 struct xfs_bmbt_irec *new = &bma->got;
442 int diff; /* temp value */
443 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
444 int error; /* error return value */
445 int i; /* temp state */
446 xfs_ifork_t *ifp; /* inode fork pointer */
447 xfs_fileoff_t new_endoff; /* end offset of new entry */
448 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
449 /* left is 0, right is 1, prev is 2 */
450 int rval=0; /* return value (logging flags) */
451 int state = 0;/* state bits, accessed thru macros */
452 xfs_filblks_t da_new; /* new count del alloc blocks used */
453 xfs_filblks_t da_old; /* old count del alloc blocks used */
454 xfs_filblks_t temp=0; /* value for da_new calculations */
455 xfs_filblks_t temp2=0;/* value for da_new calculations */
456 int tmp_rval; /* partial logging flags */
458 ifp = XFS_IFORK_PTR(bma->ip, XFS_DATA_FORK);
460 ASSERT(bma->idx >= 0);
461 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
462 ASSERT(!isnullstartblock(new->br_startblock));
464 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
466 XFS_STATS_INC(xs_add_exlist);
473 * Set up a bunch of variables to make the tests simpler.
475 ep = xfs_iext_get_ext(ifp, bma->idx);
476 xfs_bmbt_get_all(ep, &PREV);
477 new_endoff = new->br_startoff + new->br_blockcount;
478 ASSERT(PREV.br_startoff <= new->br_startoff);
479 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
481 da_old = startblockval(PREV.br_startblock);
485 * Set flags determining what part of the previous delayed allocation
486 * extent is being replaced by a real allocation.
488 if (PREV.br_startoff == new->br_startoff)
489 state |= BMAP_LEFT_FILLING;
490 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
491 state |= BMAP_RIGHT_FILLING;
494 * Check and set flags if this segment has a left neighbor.
495 * Don't set contiguous if the combined extent would be too large.
498 state |= BMAP_LEFT_VALID;
499 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
501 if (isnullstartblock(LEFT.br_startblock))
502 state |= BMAP_LEFT_DELAY;
505 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
506 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
507 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
508 LEFT.br_state == new->br_state &&
509 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
510 state |= BMAP_LEFT_CONTIG;
513 * Check and set flags if this segment has a right neighbor.
514 * Don't set contiguous if the combined extent would be too large.
515 * Also check for all-three-contiguous being too large.
517 if (bma->idx < bma->ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
518 state |= BMAP_RIGHT_VALID;
519 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
521 if (isnullstartblock(RIGHT.br_startblock))
522 state |= BMAP_RIGHT_DELAY;
525 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
526 new_endoff == RIGHT.br_startoff &&
527 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
528 new->br_state == RIGHT.br_state &&
529 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
530 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
531 BMAP_RIGHT_FILLING)) !=
532 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
533 BMAP_RIGHT_FILLING) ||
534 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
536 state |= BMAP_RIGHT_CONTIG;
540 * Switch out based on the FILLING and CONTIG state bits.
542 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
543 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
544 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
545 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
547 * Filling in all of a previously delayed allocation extent.
548 * The left and right neighbors are both contiguous with new.
551 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
552 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
553 LEFT.br_blockcount + PREV.br_blockcount +
554 RIGHT.br_blockcount);
555 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
557 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
558 bma->ip->i_d.di_nextents--;
559 if (bma->cur == NULL)
560 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
562 rval = XFS_ILOG_CORE;
563 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
565 RIGHT.br_blockcount, &i);
568 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
569 error = xfs_btree_delete(bma->cur, &i);
572 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
573 error = xfs_btree_decrement(bma->cur, 0, &i);
576 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
577 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
581 RIGHT.br_blockcount, LEFT.br_state);
587 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
589 * Filling in all of a previously delayed allocation extent.
590 * The left neighbor is contiguous, the right is not.
594 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
595 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
596 LEFT.br_blockcount + PREV.br_blockcount);
597 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
599 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
600 if (bma->cur == NULL)
601 rval = XFS_ILOG_DEXT;
604 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
605 LEFT.br_startblock, LEFT.br_blockcount,
609 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
610 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
613 PREV.br_blockcount, LEFT.br_state);
619 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
621 * Filling in all of a previously delayed allocation extent.
622 * The right neighbor is contiguous, the left is not.
624 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
625 xfs_bmbt_set_startblock(ep, new->br_startblock);
626 xfs_bmbt_set_blockcount(ep,
627 PREV.br_blockcount + RIGHT.br_blockcount);
628 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
630 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
631 if (bma->cur == NULL)
632 rval = XFS_ILOG_DEXT;
635 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
637 RIGHT.br_blockcount, &i);
640 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
641 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
644 RIGHT.br_blockcount, PREV.br_state);
650 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
652 * Filling in all of a previously delayed allocation extent.
653 * Neither the left nor right neighbors are contiguous with
656 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
657 xfs_bmbt_set_startblock(ep, new->br_startblock);
658 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
660 bma->ip->i_d.di_nextents++;
661 if (bma->cur == NULL)
662 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
664 rval = XFS_ILOG_CORE;
665 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
666 new->br_startblock, new->br_blockcount,
670 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
671 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
672 error = xfs_btree_insert(bma->cur, &i);
675 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
679 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
681 * Filling in the first part of a previous delayed allocation.
682 * The left neighbor is contiguous.
684 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
685 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
686 LEFT.br_blockcount + new->br_blockcount);
687 xfs_bmbt_set_startoff(ep,
688 PREV.br_startoff + new->br_blockcount);
689 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
691 temp = PREV.br_blockcount - new->br_blockcount;
692 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
693 xfs_bmbt_set_blockcount(ep, temp);
694 if (bma->cur == NULL)
695 rval = XFS_ILOG_DEXT;
698 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
699 LEFT.br_startblock, LEFT.br_blockcount,
703 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
704 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
712 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
713 startblockval(PREV.br_startblock));
714 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
715 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
720 case BMAP_LEFT_FILLING:
722 * Filling in the first part of a previous delayed allocation.
723 * The left neighbor is not contiguous.
725 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
726 xfs_bmbt_set_startoff(ep, new_endoff);
727 temp = PREV.br_blockcount - new->br_blockcount;
728 xfs_bmbt_set_blockcount(ep, temp);
729 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
730 bma->ip->i_d.di_nextents++;
731 if (bma->cur == NULL)
732 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
734 rval = XFS_ILOG_CORE;
735 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
736 new->br_startblock, new->br_blockcount,
740 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
741 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
742 error = xfs_btree_insert(bma->cur, &i);
745 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
748 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
749 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
750 bma->firstblock, bma->flist,
751 &bma->cur, 1, &tmp_rval, XFS_DATA_FORK);
756 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
757 startblockval(PREV.br_startblock) -
758 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
759 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
760 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
761 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
764 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
766 * Filling in the last part of a previous delayed allocation.
767 * The right neighbor is contiguous with the new allocation.
769 temp = PREV.br_blockcount - new->br_blockcount;
770 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
771 xfs_bmbt_set_blockcount(ep, temp);
772 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
773 new->br_startoff, new->br_startblock,
774 new->br_blockcount + RIGHT.br_blockcount,
776 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
777 if (bma->cur == NULL)
778 rval = XFS_ILOG_DEXT;
781 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
783 RIGHT.br_blockcount, &i);
786 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
787 error = xfs_bmbt_update(bma->cur, new->br_startoff,
796 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
797 startblockval(PREV.br_startblock));
798 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
799 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
800 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
805 case BMAP_RIGHT_FILLING:
807 * Filling in the last part of a previous delayed allocation.
808 * The right neighbor is not contiguous.
810 temp = PREV.br_blockcount - new->br_blockcount;
811 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
812 xfs_bmbt_set_blockcount(ep, temp);
813 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
814 bma->ip->i_d.di_nextents++;
815 if (bma->cur == NULL)
816 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
818 rval = XFS_ILOG_CORE;
819 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
820 new->br_startblock, new->br_blockcount,
824 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
825 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
826 error = xfs_btree_insert(bma->cur, &i);
829 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
832 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
833 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
834 bma->firstblock, bma->flist, &bma->cur, 1,
835 &tmp_rval, XFS_DATA_FORK);
840 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
841 startblockval(PREV.br_startblock) -
842 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
843 ep = xfs_iext_get_ext(ifp, bma->idx);
844 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
845 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
852 * Filling in the middle part of a previous delayed allocation.
853 * Contiguity is impossible here.
854 * This case is avoided almost all the time.
856 * We start with a delayed allocation:
858 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
861 * and we are allocating:
862 * +rrrrrrrrrrrrrrrrr+
865 * and we set it up for insertion as:
866 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
868 * PREV @ idx LEFT RIGHT
869 * inserted at idx + 1
871 temp = new->br_startoff - PREV.br_startoff;
872 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
873 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
874 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
876 RIGHT.br_state = PREV.br_state;
877 RIGHT.br_startblock = nullstartblock(
878 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
879 RIGHT.br_startoff = new_endoff;
880 RIGHT.br_blockcount = temp2;
881 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
882 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
883 bma->ip->i_d.di_nextents++;
884 if (bma->cur == NULL)
885 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
887 rval = XFS_ILOG_CORE;
888 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
889 new->br_startblock, new->br_blockcount,
893 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
894 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
895 error = xfs_btree_insert(bma->cur, &i);
898 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
901 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
902 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
903 bma->firstblock, bma->flist, &bma->cur,
904 1, &tmp_rval, XFS_DATA_FORK);
909 temp = xfs_bmap_worst_indlen(bma->ip, temp);
910 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
911 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
912 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
914 error = xfs_icsb_modify_counters(bma->ip->i_mount,
916 -((int64_t)diff), 0);
922 ep = xfs_iext_get_ext(ifp, bma->idx);
923 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
924 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
925 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
926 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
927 nullstartblock((int)temp2));
928 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
931 da_new = temp + temp2;
934 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
935 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
936 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
937 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
938 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
939 case BMAP_LEFT_CONTIG:
940 case BMAP_RIGHT_CONTIG:
942 * These cases are all impossible.
947 /* convert to a btree if necessary */
948 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
949 int tmp_logflags; /* partial log flag return val */
951 ASSERT(bma->cur == NULL);
952 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
953 bma->firstblock, bma->flist, &bma->cur,
954 da_old > 0, &tmp_logflags, XFS_DATA_FORK);
955 bma->logflags |= tmp_logflags;
960 /* adjust for changes in reserved delayed indirect blocks */
961 if (da_old || da_new) {
964 temp += bma->cur->bc_private.b.allocated;
965 ASSERT(temp <= da_old);
967 xfs_icsb_modify_counters(bma->ip->i_mount,
969 (int64_t)(da_old - temp), 0);
972 /* clear out the allocated field, done with it now in any case. */
974 bma->cur->bc_private.b.allocated = 0;
976 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, XFS_DATA_FORK);
978 bma->logflags |= rval;
986 * Convert an unwritten allocation to a real allocation or vice versa.
988 STATIC int /* error */
989 xfs_bmap_add_extent_unwritten_real(
990 struct xfs_trans *tp,
991 xfs_inode_t *ip, /* incore inode pointer */
992 xfs_extnum_t *idx, /* extent number to update/insert */
993 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
994 xfs_bmbt_irec_t *new, /* new data to add to file extents */
995 xfs_fsblock_t *first, /* pointer to firstblock variable */
996 xfs_bmap_free_t *flist, /* list of extents to be freed */
997 int *logflagsp) /* inode logging flags */
999 xfs_btree_cur_t *cur; /* btree cursor */
1000 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1001 int error; /* error return value */
1002 int i; /* temp state */
1003 xfs_ifork_t *ifp; /* inode fork pointer */
1004 xfs_fileoff_t new_endoff; /* end offset of new entry */
1005 xfs_exntst_t newext; /* new extent state */
1006 xfs_exntst_t oldext; /* old extent state */
1007 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1008 /* left is 0, right is 1, prev is 2 */
1009 int rval=0; /* return value (logging flags) */
1010 int state = 0;/* state bits, accessed thru macros */
1015 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1018 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
1019 ASSERT(!isnullstartblock(new->br_startblock));
1021 XFS_STATS_INC(xs_add_exlist);
1028 * Set up a bunch of variables to make the tests simpler.
1031 ep = xfs_iext_get_ext(ifp, *idx);
1032 xfs_bmbt_get_all(ep, &PREV);
1033 newext = new->br_state;
1034 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1035 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1036 ASSERT(PREV.br_state == oldext);
1037 new_endoff = new->br_startoff + new->br_blockcount;
1038 ASSERT(PREV.br_startoff <= new->br_startoff);
1039 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1042 * Set flags determining what part of the previous oldext allocation
1043 * extent is being replaced by a newext allocation.
1045 if (PREV.br_startoff == new->br_startoff)
1046 state |= BMAP_LEFT_FILLING;
1047 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1048 state |= BMAP_RIGHT_FILLING;
1051 * Check and set flags if this segment has a left neighbor.
1052 * Don't set contiguous if the combined extent would be too large.
1055 state |= BMAP_LEFT_VALID;
1056 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
1058 if (isnullstartblock(LEFT.br_startblock))
1059 state |= BMAP_LEFT_DELAY;
1062 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1063 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1064 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1065 LEFT.br_state == newext &&
1066 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1067 state |= BMAP_LEFT_CONTIG;
1070 * Check and set flags if this segment has a right neighbor.
1071 * Don't set contiguous if the combined extent would be too large.
1072 * Also check for all-three-contiguous being too large.
1074 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1075 state |= BMAP_RIGHT_VALID;
1076 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
1077 if (isnullstartblock(RIGHT.br_startblock))
1078 state |= BMAP_RIGHT_DELAY;
1081 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1082 new_endoff == RIGHT.br_startoff &&
1083 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1084 newext == RIGHT.br_state &&
1085 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1086 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1087 BMAP_RIGHT_FILLING)) !=
1088 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1089 BMAP_RIGHT_FILLING) ||
1090 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1092 state |= BMAP_RIGHT_CONTIG;
1095 * Switch out based on the FILLING and CONTIG state bits.
1097 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1098 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1099 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1100 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1102 * Setting all of a previous oldext extent to newext.
1103 * The left and right neighbors are both contiguous with new.
1107 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1108 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1109 LEFT.br_blockcount + PREV.br_blockcount +
1110 RIGHT.br_blockcount);
1111 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1113 xfs_iext_remove(ip, *idx + 1, 2, state);
1114 ip->i_d.di_nextents -= 2;
1116 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1118 rval = XFS_ILOG_CORE;
1119 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1120 RIGHT.br_startblock,
1121 RIGHT.br_blockcount, &i)))
1123 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1124 if ((error = xfs_btree_delete(cur, &i)))
1126 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1127 if ((error = xfs_btree_decrement(cur, 0, &i)))
1129 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1130 if ((error = xfs_btree_delete(cur, &i)))
1132 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1133 if ((error = xfs_btree_decrement(cur, 0, &i)))
1135 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1136 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1138 LEFT.br_blockcount + PREV.br_blockcount +
1139 RIGHT.br_blockcount, LEFT.br_state)))
1144 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1146 * Setting all of a previous oldext extent to newext.
1147 * The left neighbor is contiguous, the right is not.
1151 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1152 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1153 LEFT.br_blockcount + PREV.br_blockcount);
1154 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1156 xfs_iext_remove(ip, *idx + 1, 1, state);
1157 ip->i_d.di_nextents--;
1159 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1161 rval = XFS_ILOG_CORE;
1162 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1163 PREV.br_startblock, PREV.br_blockcount,
1166 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1167 if ((error = xfs_btree_delete(cur, &i)))
1169 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1170 if ((error = xfs_btree_decrement(cur, 0, &i)))
1172 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1173 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1175 LEFT.br_blockcount + PREV.br_blockcount,
1181 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1183 * Setting all of a previous oldext extent to newext.
1184 * The right neighbor is contiguous, the left is not.
1186 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1187 xfs_bmbt_set_blockcount(ep,
1188 PREV.br_blockcount + RIGHT.br_blockcount);
1189 xfs_bmbt_set_state(ep, newext);
1190 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1191 xfs_iext_remove(ip, *idx + 1, 1, state);
1192 ip->i_d.di_nextents--;
1194 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1196 rval = XFS_ILOG_CORE;
1197 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1198 RIGHT.br_startblock,
1199 RIGHT.br_blockcount, &i)))
1201 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1202 if ((error = xfs_btree_delete(cur, &i)))
1204 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1205 if ((error = xfs_btree_decrement(cur, 0, &i)))
1207 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1208 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1210 new->br_blockcount + RIGHT.br_blockcount,
1216 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1218 * Setting all of a previous oldext extent to newext.
1219 * Neither the left nor right neighbors are contiguous with
1222 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1223 xfs_bmbt_set_state(ep, newext);
1224 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1227 rval = XFS_ILOG_DEXT;
1230 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1231 new->br_startblock, new->br_blockcount,
1234 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1235 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1236 new->br_startblock, new->br_blockcount,
1242 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1244 * Setting the first part of a previous oldext extent to newext.
1245 * The left neighbor is contiguous.
1247 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
1248 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
1249 LEFT.br_blockcount + new->br_blockcount);
1250 xfs_bmbt_set_startoff(ep,
1251 PREV.br_startoff + new->br_blockcount);
1252 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
1254 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1255 xfs_bmbt_set_startblock(ep,
1256 new->br_startblock + new->br_blockcount);
1257 xfs_bmbt_set_blockcount(ep,
1258 PREV.br_blockcount - new->br_blockcount);
1259 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1264 rval = XFS_ILOG_DEXT;
1267 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1268 PREV.br_startblock, PREV.br_blockcount,
1271 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1272 if ((error = xfs_bmbt_update(cur,
1273 PREV.br_startoff + new->br_blockcount,
1274 PREV.br_startblock + new->br_blockcount,
1275 PREV.br_blockcount - new->br_blockcount,
1278 if ((error = xfs_btree_decrement(cur, 0, &i)))
1280 error = xfs_bmbt_update(cur, LEFT.br_startoff,
1282 LEFT.br_blockcount + new->br_blockcount,
1289 case BMAP_LEFT_FILLING:
1291 * Setting the first part of a previous oldext extent to newext.
1292 * The left neighbor is not contiguous.
1294 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1295 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1296 xfs_bmbt_set_startoff(ep, new_endoff);
1297 xfs_bmbt_set_blockcount(ep,
1298 PREV.br_blockcount - new->br_blockcount);
1299 xfs_bmbt_set_startblock(ep,
1300 new->br_startblock + new->br_blockcount);
1301 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1303 xfs_iext_insert(ip, *idx, 1, new, state);
1304 ip->i_d.di_nextents++;
1306 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1308 rval = XFS_ILOG_CORE;
1309 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1310 PREV.br_startblock, PREV.br_blockcount,
1313 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1314 if ((error = xfs_bmbt_update(cur,
1315 PREV.br_startoff + new->br_blockcount,
1316 PREV.br_startblock + new->br_blockcount,
1317 PREV.br_blockcount - new->br_blockcount,
1320 cur->bc_rec.b = *new;
1321 if ((error = xfs_btree_insert(cur, &i)))
1323 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1327 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1329 * Setting the last part of a previous oldext extent to newext.
1330 * The right neighbor is contiguous with the new allocation.
1332 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1333 xfs_bmbt_set_blockcount(ep,
1334 PREV.br_blockcount - new->br_blockcount);
1335 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1339 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1340 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
1341 new->br_startoff, new->br_startblock,
1342 new->br_blockcount + RIGHT.br_blockcount, newext);
1343 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1346 rval = XFS_ILOG_DEXT;
1349 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1351 PREV.br_blockcount, &i)))
1353 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1354 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1356 PREV.br_blockcount - new->br_blockcount,
1359 if ((error = xfs_btree_increment(cur, 0, &i)))
1361 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1363 new->br_blockcount + RIGHT.br_blockcount,
1369 case BMAP_RIGHT_FILLING:
1371 * Setting the last part of a previous oldext extent to newext.
1372 * The right neighbor is not contiguous.
1374 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1375 xfs_bmbt_set_blockcount(ep,
1376 PREV.br_blockcount - new->br_blockcount);
1377 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1380 xfs_iext_insert(ip, *idx, 1, new, state);
1382 ip->i_d.di_nextents++;
1384 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1386 rval = XFS_ILOG_CORE;
1387 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1388 PREV.br_startblock, PREV.br_blockcount,
1391 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1392 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1394 PREV.br_blockcount - new->br_blockcount,
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 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1403 if ((error = xfs_btree_insert(cur, &i)))
1405 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1411 * Setting the middle part of a previous oldext extent to
1412 * newext. Contiguity is impossible here.
1413 * One extent becomes three extents.
1415 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1416 xfs_bmbt_set_blockcount(ep,
1417 new->br_startoff - PREV.br_startoff);
1418 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1421 r[1].br_startoff = new_endoff;
1422 r[1].br_blockcount =
1423 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1424 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1425 r[1].br_state = oldext;
1428 xfs_iext_insert(ip, *idx, 2, &r[0], state);
1430 ip->i_d.di_nextents += 2;
1432 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1434 rval = XFS_ILOG_CORE;
1435 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1436 PREV.br_startblock, PREV.br_blockcount,
1439 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1440 /* new right extent - oldext */
1441 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1442 r[1].br_startblock, r[1].br_blockcount,
1445 /* new left extent - oldext */
1446 cur->bc_rec.b = PREV;
1447 cur->bc_rec.b.br_blockcount =
1448 new->br_startoff - PREV.br_startoff;
1449 if ((error = xfs_btree_insert(cur, &i)))
1451 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1453 * Reset the cursor to the position of the new extent
1454 * we are about to insert as we can't trust it after
1455 * the previous insert.
1457 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1458 new->br_startblock, new->br_blockcount,
1461 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1462 /* new middle extent - newext */
1463 cur->bc_rec.b.br_state = new->br_state;
1464 if ((error = xfs_btree_insert(cur, &i)))
1466 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1470 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1471 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1472 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1473 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1474 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1475 case BMAP_LEFT_CONTIG:
1476 case BMAP_RIGHT_CONTIG:
1478 * These cases are all impossible.
1483 /* convert to a btree if necessary */
1484 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
1485 int tmp_logflags; /* partial log flag return val */
1487 ASSERT(cur == NULL);
1488 error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur,
1489 0, &tmp_logflags, XFS_DATA_FORK);
1490 *logflagsp |= tmp_logflags;
1495 /* clear out the allocated field, done with it now in any case. */
1497 cur->bc_private.b.allocated = 0;
1501 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
1511 * Convert a hole to a delayed allocation.
1514 xfs_bmap_add_extent_hole_delay(
1515 xfs_inode_t *ip, /* incore inode pointer */
1516 xfs_extnum_t *idx, /* extent number to update/insert */
1517 xfs_bmbt_irec_t *new) /* new data to add to file extents */
1519 xfs_ifork_t *ifp; /* inode fork pointer */
1520 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1521 xfs_filblks_t newlen=0; /* new indirect size */
1522 xfs_filblks_t oldlen=0; /* old indirect size */
1523 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1524 int state; /* state bits, accessed thru macros */
1525 xfs_filblks_t temp=0; /* temp for indirect calculations */
1527 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1529 ASSERT(isnullstartblock(new->br_startblock));
1532 * Check and set flags if this segment has a left neighbor
1535 state |= BMAP_LEFT_VALID;
1536 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
1538 if (isnullstartblock(left.br_startblock))
1539 state |= BMAP_LEFT_DELAY;
1543 * Check and set flags if the current (right) segment exists.
1544 * If it doesn't exist, we're converting the hole at end-of-file.
1546 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1547 state |= BMAP_RIGHT_VALID;
1548 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
1550 if (isnullstartblock(right.br_startblock))
1551 state |= BMAP_RIGHT_DELAY;
1555 * Set contiguity flags on the left and right neighbors.
1556 * Don't let extents get too large, even if the pieces are contiguous.
1558 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1559 left.br_startoff + left.br_blockcount == new->br_startoff &&
1560 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1561 state |= BMAP_LEFT_CONTIG;
1563 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1564 new->br_startoff + new->br_blockcount == right.br_startoff &&
1565 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1566 (!(state & BMAP_LEFT_CONTIG) ||
1567 (left.br_blockcount + new->br_blockcount +
1568 right.br_blockcount <= MAXEXTLEN)))
1569 state |= BMAP_RIGHT_CONTIG;
1572 * Switch out based on the contiguity flags.
1574 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1575 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1577 * New allocation is contiguous with delayed allocations
1578 * on the left and on the right.
1579 * Merge all three into a single extent record.
1582 temp = left.br_blockcount + new->br_blockcount +
1583 right.br_blockcount;
1585 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1586 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
1587 oldlen = startblockval(left.br_startblock) +
1588 startblockval(new->br_startblock) +
1589 startblockval(right.br_startblock);
1590 newlen = xfs_bmap_worst_indlen(ip, temp);
1591 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
1592 nullstartblock((int)newlen));
1593 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1595 xfs_iext_remove(ip, *idx + 1, 1, state);
1598 case BMAP_LEFT_CONTIG:
1600 * New allocation is contiguous with a delayed allocation
1602 * Merge the new allocation with the left neighbor.
1605 temp = left.br_blockcount + new->br_blockcount;
1607 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1608 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
1609 oldlen = startblockval(left.br_startblock) +
1610 startblockval(new->br_startblock);
1611 newlen = xfs_bmap_worst_indlen(ip, temp);
1612 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
1613 nullstartblock((int)newlen));
1614 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1617 case BMAP_RIGHT_CONTIG:
1619 * New allocation is contiguous with a delayed allocation
1621 * Merge the new allocation with the right neighbor.
1623 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1624 temp = new->br_blockcount + right.br_blockcount;
1625 oldlen = startblockval(new->br_startblock) +
1626 startblockval(right.br_startblock);
1627 newlen = xfs_bmap_worst_indlen(ip, temp);
1628 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
1630 nullstartblock((int)newlen), temp, right.br_state);
1631 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1636 * New allocation is not contiguous with another
1637 * delayed allocation.
1638 * Insert a new entry.
1640 oldlen = newlen = 0;
1641 xfs_iext_insert(ip, *idx, 1, new, state);
1644 if (oldlen != newlen) {
1645 ASSERT(oldlen > newlen);
1646 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
1647 (int64_t)(oldlen - newlen), 0);
1649 * Nothing to do for disk quota accounting here.
1655 * Convert a hole to a real allocation.
1657 STATIC int /* error */
1658 xfs_bmap_add_extent_hole_real(
1659 struct xfs_bmalloca *bma,
1662 struct xfs_bmbt_irec *new = &bma->got;
1663 int error; /* error return value */
1664 int i; /* temp state */
1665 xfs_ifork_t *ifp; /* inode fork pointer */
1666 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1667 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1668 int rval=0; /* return value (logging flags) */
1669 int state; /* state bits, accessed thru macros */
1671 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
1673 ASSERT(bma->idx >= 0);
1674 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
1675 ASSERT(!isnullstartblock(new->br_startblock));
1677 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
1679 XFS_STATS_INC(xs_add_exlist);
1682 if (whichfork == XFS_ATTR_FORK)
1683 state |= BMAP_ATTRFORK;
1686 * Check and set flags if this segment has a left neighbor.
1689 state |= BMAP_LEFT_VALID;
1690 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
1691 if (isnullstartblock(left.br_startblock))
1692 state |= BMAP_LEFT_DELAY;
1696 * Check and set flags if this segment has a current value.
1697 * Not true if we're inserting into the "hole" at eof.
1699 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1700 state |= BMAP_RIGHT_VALID;
1701 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
1702 if (isnullstartblock(right.br_startblock))
1703 state |= BMAP_RIGHT_DELAY;
1707 * We're inserting a real allocation between "left" and "right".
1708 * Set the contiguity flags. Don't let extents get too large.
1710 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1711 left.br_startoff + left.br_blockcount == new->br_startoff &&
1712 left.br_startblock + left.br_blockcount == new->br_startblock &&
1713 left.br_state == new->br_state &&
1714 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1715 state |= BMAP_LEFT_CONTIG;
1717 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1718 new->br_startoff + new->br_blockcount == right.br_startoff &&
1719 new->br_startblock + new->br_blockcount == right.br_startblock &&
1720 new->br_state == right.br_state &&
1721 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1722 (!(state & BMAP_LEFT_CONTIG) ||
1723 left.br_blockcount + new->br_blockcount +
1724 right.br_blockcount <= MAXEXTLEN))
1725 state |= BMAP_RIGHT_CONTIG;
1729 * Select which case we're in here, and implement it.
1731 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1732 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1734 * New allocation is contiguous with real allocations on the
1735 * left and on the right.
1736 * Merge all three into a single extent record.
1739 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1740 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1741 left.br_blockcount + new->br_blockcount +
1742 right.br_blockcount);
1743 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1745 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1747 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
1748 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
1749 if (bma->cur == NULL) {
1750 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1752 rval = XFS_ILOG_CORE;
1753 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
1754 right.br_startblock, right.br_blockcount,
1758 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1759 error = xfs_btree_delete(bma->cur, &i);
1762 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1763 error = xfs_btree_decrement(bma->cur, 0, &i);
1766 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1767 error = xfs_bmbt_update(bma->cur, left.br_startoff,
1769 left.br_blockcount +
1770 new->br_blockcount +
1771 right.br_blockcount,
1778 case BMAP_LEFT_CONTIG:
1780 * New allocation is contiguous with a real allocation
1782 * Merge the new allocation with the left neighbor.
1785 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1786 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1787 left.br_blockcount + new->br_blockcount);
1788 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1790 if (bma->cur == NULL) {
1791 rval = xfs_ilog_fext(whichfork);
1794 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
1795 left.br_startblock, left.br_blockcount,
1799 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1800 error = xfs_bmbt_update(bma->cur, left.br_startoff,
1802 left.br_blockcount +
1810 case BMAP_RIGHT_CONTIG:
1812 * New allocation is contiguous with a real allocation
1814 * Merge the new allocation with the right neighbor.
1816 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1817 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
1818 new->br_startoff, new->br_startblock,
1819 new->br_blockcount + right.br_blockcount,
1821 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1823 if (bma->cur == NULL) {
1824 rval = xfs_ilog_fext(whichfork);
1827 error = xfs_bmbt_lookup_eq(bma->cur,
1829 right.br_startblock,
1830 right.br_blockcount, &i);
1833 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1834 error = xfs_bmbt_update(bma->cur, new->br_startoff,
1836 new->br_blockcount +
1837 right.br_blockcount,
1846 * New allocation is not contiguous with another
1848 * Insert a new entry.
1850 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
1851 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
1852 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
1853 if (bma->cur == NULL) {
1854 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1856 rval = XFS_ILOG_CORE;
1857 error = xfs_bmbt_lookup_eq(bma->cur,
1860 new->br_blockcount, &i);
1863 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1864 bma->cur->bc_rec.b.br_state = new->br_state;
1865 error = xfs_btree_insert(bma->cur, &i);
1868 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1873 /* convert to a btree if necessary */
1874 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1875 int tmp_logflags; /* partial log flag return val */
1877 ASSERT(bma->cur == NULL);
1878 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1879 bma->firstblock, bma->flist, &bma->cur,
1880 0, &tmp_logflags, whichfork);
1881 bma->logflags |= tmp_logflags;
1886 /* clear out the allocated field, done with it now in any case. */
1888 bma->cur->bc_private.b.allocated = 0;
1890 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
1892 bma->logflags |= rval;
1897 * Adjust the size of the new extent based on di_extsize and rt extsize.
1900 xfs_bmap_extsize_align(
1902 xfs_bmbt_irec_t *gotp, /* next extent pointer */
1903 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
1904 xfs_extlen_t extsz, /* align to this extent size */
1905 int rt, /* is this a realtime inode? */
1906 int eof, /* is extent at end-of-file? */
1907 int delay, /* creating delalloc extent? */
1908 int convert, /* overwriting unwritten extent? */
1909 xfs_fileoff_t *offp, /* in/out: aligned offset */
1910 xfs_extlen_t *lenp) /* in/out: aligned length */
1912 xfs_fileoff_t orig_off; /* original offset */
1913 xfs_extlen_t orig_alen; /* original length */
1914 xfs_fileoff_t orig_end; /* original off+len */
1915 xfs_fileoff_t nexto; /* next file offset */
1916 xfs_fileoff_t prevo; /* previous file offset */
1917 xfs_fileoff_t align_off; /* temp for offset */
1918 xfs_extlen_t align_alen; /* temp for length */
1919 xfs_extlen_t temp; /* temp for calculations */
1924 orig_off = align_off = *offp;
1925 orig_alen = align_alen = *lenp;
1926 orig_end = orig_off + orig_alen;
1929 * If this request overlaps an existing extent, then don't
1930 * attempt to perform any additional alignment.
1932 if (!delay && !eof &&
1933 (orig_off >= gotp->br_startoff) &&
1934 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
1939 * If the file offset is unaligned vs. the extent size
1940 * we need to align it. This will be possible unless
1941 * the file was previously written with a kernel that didn't
1942 * perform this alignment, or if a truncate shot us in the
1945 temp = do_mod(orig_off, extsz);
1951 * Same adjustment for the end of the requested area.
1953 if ((temp = (align_alen % extsz))) {
1954 align_alen += extsz - temp;
1957 * If the previous block overlaps with this proposed allocation
1958 * then move the start forward without adjusting the length.
1960 if (prevp->br_startoff != NULLFILEOFF) {
1961 if (prevp->br_startblock == HOLESTARTBLOCK)
1962 prevo = prevp->br_startoff;
1964 prevo = prevp->br_startoff + prevp->br_blockcount;
1967 if (align_off != orig_off && align_off < prevo)
1970 * If the next block overlaps with this proposed allocation
1971 * then move the start back without adjusting the length,
1972 * but not before offset 0.
1973 * This may of course make the start overlap previous block,
1974 * and if we hit the offset 0 limit then the next block
1975 * can still overlap too.
1977 if (!eof && gotp->br_startoff != NULLFILEOFF) {
1978 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
1979 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
1980 nexto = gotp->br_startoff + gotp->br_blockcount;
1982 nexto = gotp->br_startoff;
1984 nexto = NULLFILEOFF;
1986 align_off + align_alen != orig_end &&
1987 align_off + align_alen > nexto)
1988 align_off = nexto > align_alen ? nexto - align_alen : 0;
1990 * If we're now overlapping the next or previous extent that
1991 * means we can't fit an extsz piece in this hole. Just move
1992 * the start forward to the first valid spot and set
1993 * the length so we hit the end.
1995 if (align_off != orig_off && align_off < prevo)
1997 if (align_off + align_alen != orig_end &&
1998 align_off + align_alen > nexto &&
1999 nexto != NULLFILEOFF) {
2000 ASSERT(nexto > prevo);
2001 align_alen = nexto - align_off;
2005 * If realtime, and the result isn't a multiple of the realtime
2006 * extent size we need to remove blocks until it is.
2008 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2010 * We're not covering the original request, or
2011 * we won't be able to once we fix the length.
2013 if (orig_off < align_off ||
2014 orig_end > align_off + align_alen ||
2015 align_alen - temp < orig_alen)
2016 return XFS_ERROR(EINVAL);
2018 * Try to fix it by moving the start up.
2020 if (align_off + temp <= orig_off) {
2025 * Try to fix it by moving the end in.
2027 else if (align_off + align_alen - temp >= orig_end)
2030 * Set the start to the minimum then trim the length.
2033 align_alen -= orig_off - align_off;
2034 align_off = orig_off;
2035 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2038 * Result doesn't cover the request, fail it.
2040 if (orig_off < align_off || orig_end > align_off + align_alen)
2041 return XFS_ERROR(EINVAL);
2043 ASSERT(orig_off >= align_off);
2044 ASSERT(orig_end <= align_off + align_alen);
2048 if (!eof && gotp->br_startoff != NULLFILEOFF)
2049 ASSERT(align_off + align_alen <= gotp->br_startoff);
2050 if (prevp->br_startoff != NULLFILEOFF)
2051 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2059 #define XFS_ALLOC_GAP_UNITS 4
2063 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2065 xfs_fsblock_t adjust; /* adjustment to block numbers */
2066 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2067 xfs_mount_t *mp; /* mount point structure */
2068 int nullfb; /* true if ap->firstblock isn't set */
2069 int rt; /* true if inode is realtime */
2071 #define ISVALID(x,y) \
2073 (x) < mp->m_sb.sb_rblocks : \
2074 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2075 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2076 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2078 mp = ap->ip->i_mount;
2079 nullfb = *ap->firstblock == NULLFSBLOCK;
2080 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2081 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
2083 * If allocating at eof, and there's a previous real block,
2084 * try to use its last block as our starting point.
2086 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
2087 !isnullstartblock(ap->prev.br_startblock) &&
2088 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
2089 ap->prev.br_startblock)) {
2090 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
2092 * Adjust for the gap between prevp and us.
2094 adjust = ap->offset -
2095 (ap->prev.br_startoff + ap->prev.br_blockcount);
2097 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
2098 ap->blkno += adjust;
2101 * If not at eof, then compare the two neighbor blocks.
2102 * Figure out whether either one gives us a good starting point,
2103 * and pick the better one.
2105 else if (!ap->eof) {
2106 xfs_fsblock_t gotbno; /* right side block number */
2107 xfs_fsblock_t gotdiff=0; /* right side difference */
2108 xfs_fsblock_t prevbno; /* left side block number */
2109 xfs_fsblock_t prevdiff=0; /* left side difference */
2112 * If there's a previous (left) block, select a requested
2113 * start block based on it.
2115 if (ap->prev.br_startoff != NULLFILEOFF &&
2116 !isnullstartblock(ap->prev.br_startblock) &&
2117 (prevbno = ap->prev.br_startblock +
2118 ap->prev.br_blockcount) &&
2119 ISVALID(prevbno, ap->prev.br_startblock)) {
2121 * Calculate gap to end of previous block.
2123 adjust = prevdiff = ap->offset -
2124 (ap->prev.br_startoff +
2125 ap->prev.br_blockcount);
2127 * Figure the startblock based on the previous block's
2128 * end and the gap size.
2130 * If the gap is large relative to the piece we're
2131 * allocating, or using it gives us an invalid block
2132 * number, then just use the end of the previous block.
2134 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
2135 ISVALID(prevbno + prevdiff,
2136 ap->prev.br_startblock))
2141 * If the firstblock forbids it, can't use it,
2144 if (!rt && !nullfb &&
2145 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2146 prevbno = NULLFSBLOCK;
2149 * No previous block or can't follow it, just default.
2152 prevbno = NULLFSBLOCK;
2154 * If there's a following (right) block, select a requested
2155 * start block based on it.
2157 if (!isnullstartblock(ap->got.br_startblock)) {
2159 * Calculate gap to start of next block.
2161 adjust = gotdiff = ap->got.br_startoff - ap->offset;
2163 * Figure the startblock based on the next block's
2164 * start and the gap size.
2166 gotbno = ap->got.br_startblock;
2169 * If the gap is large relative to the piece we're
2170 * allocating, or using it gives us an invalid block
2171 * number, then just use the start of the next block
2172 * offset by our length.
2174 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
2175 ISVALID(gotbno - gotdiff, gotbno))
2177 else if (ISVALID(gotbno - ap->length, gotbno)) {
2178 gotbno -= ap->length;
2179 gotdiff += adjust - ap->length;
2183 * If the firstblock forbids it, can't use it,
2186 if (!rt && !nullfb &&
2187 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2188 gotbno = NULLFSBLOCK;
2191 * No next block, just default.
2194 gotbno = NULLFSBLOCK;
2196 * If both valid, pick the better one, else the only good
2197 * one, else ap->blkno is already set (to 0 or the inode block).
2199 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2200 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
2201 else if (prevbno != NULLFSBLOCK)
2202 ap->blkno = prevbno;
2203 else if (gotbno != NULLFSBLOCK)
2211 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2213 xfs_alloctype_t atype = 0; /* type for allocation routines */
2214 int error; /* error return value */
2215 xfs_mount_t *mp; /* mount point structure */
2216 xfs_extlen_t prod = 0; /* product factor for allocators */
2217 xfs_extlen_t ralen = 0; /* realtime allocation length */
2218 xfs_extlen_t align; /* minimum allocation alignment */
2221 mp = ap->ip->i_mount;
2222 align = xfs_get_extsz_hint(ap->ip);
2223 prod = align / mp->m_sb.sb_rextsize;
2224 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
2225 align, 1, ap->eof, 0,
2226 ap->conv, &ap->offset, &ap->length);
2230 ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
2233 * If the offset & length are not perfectly aligned
2234 * then kill prod, it will just get us in trouble.
2236 if (do_mod(ap->offset, align) || ap->length % align)
2239 * Set ralen to be the actual requested length in rtextents.
2241 ralen = ap->length / mp->m_sb.sb_rextsize;
2243 * If the old value was close enough to MAXEXTLEN that
2244 * we rounded up to it, cut it back so it's valid again.
2245 * Note that if it's a really large request (bigger than
2246 * MAXEXTLEN), we don't hear about that number, and can't
2247 * adjust the starting point to match it.
2249 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2250 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2253 * Lock out other modifications to the RT bitmap inode.
2255 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
2256 xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
2259 * If it's an allocation to an empty file at offset 0,
2260 * pick an extent that will space things out in the rt area.
2262 if (ap->eof && ap->offset == 0) {
2263 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2265 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2268 ap->blkno = rtx * mp->m_sb.sb_rextsize;
2273 xfs_bmap_adjacent(ap);
2276 * Realtime allocation, done through xfs_rtallocate_extent.
2278 atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2279 do_div(ap->blkno, mp->m_sb.sb_rextsize);
2282 if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
2283 &ralen, atype, ap->wasdel, prod, &rtb)))
2285 if (rtb == NULLFSBLOCK && prod > 1 &&
2286 (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1,
2287 ap->length, &ralen, atype,
2288 ap->wasdel, 1, &rtb)))
2291 if (ap->blkno != NULLFSBLOCK) {
2292 ap->blkno *= mp->m_sb.sb_rextsize;
2293 ralen *= mp->m_sb.sb_rextsize;
2295 ap->ip->i_d.di_nblocks += ralen;
2296 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2298 ap->ip->i_delayed_blks -= ralen;
2300 * Adjust the disk quota also. This was reserved
2303 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2304 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2305 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2313 xfs_bmap_btalloc_nullfb(
2314 struct xfs_bmalloca *ap,
2315 struct xfs_alloc_arg *args,
2318 struct xfs_mount *mp = ap->ip->i_mount;
2319 struct xfs_perag *pag;
2320 xfs_agnumber_t ag, startag;
2324 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2325 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2327 args->type = XFS_ALLOCTYPE_START_BNO;
2328 args->total = ap->total;
2331 * Search for an allocation group with a single extent large enough
2332 * for the request. If one isn't found, then adjust the minimum
2333 * allocation size to the largest space found.
2335 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
2336 if (startag == NULLAGNUMBER)
2339 pag = xfs_perag_get(mp, ag);
2340 while (*blen < args->maxlen) {
2341 if (!pag->pagf_init) {
2342 error = xfs_alloc_pagf_init(mp, args->tp, ag,
2343 XFS_ALLOC_FLAG_TRYLOCK);
2351 * See xfs_alloc_fix_freelist...
2353 if (pag->pagf_init) {
2354 xfs_extlen_t longest;
2355 longest = xfs_alloc_longest_free_extent(mp, pag);
2356 if (*blen < longest)
2361 if (xfs_inode_is_filestream(ap->ip)) {
2362 if (*blen >= args->maxlen)
2367 * If startag is an invalid AG, we've
2368 * come here once before and
2369 * xfs_filestream_new_ag picked the
2370 * best currently available.
2372 * Don't continue looping, since we
2373 * could loop forever.
2375 if (startag == NULLAGNUMBER)
2378 error = xfs_filestream_new_ag(ap, &ag);
2383 /* loop again to set 'blen'*/
2384 startag = NULLAGNUMBER;
2385 pag = xfs_perag_get(mp, ag);
2389 if (++ag == mp->m_sb.sb_agcount)
2394 pag = xfs_perag_get(mp, ag);
2399 * Since the above loop did a BUF_TRYLOCK, it is
2400 * possible that there is space for this request.
2402 if (notinit || *blen < ap->minlen)
2403 args->minlen = ap->minlen;
2405 * If the best seen length is less than the request
2406 * length, use the best as the minimum.
2408 else if (*blen < args->maxlen)
2409 args->minlen = *blen;
2411 * Otherwise we've seen an extent as big as maxlen,
2412 * use that as the minimum.
2415 args->minlen = args->maxlen;
2418 * set the failure fallback case to look in the selected
2419 * AG as the stream may have moved.
2421 if (xfs_inode_is_filestream(ap->ip))
2422 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2429 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2431 xfs_mount_t *mp; /* mount point structure */
2432 xfs_alloctype_t atype = 0; /* type for allocation routines */
2433 xfs_extlen_t align; /* minimum allocation alignment */
2434 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2436 xfs_alloc_arg_t args;
2438 xfs_extlen_t nextminlen = 0;
2439 int nullfb; /* true if ap->firstblock isn't set */
2446 mp = ap->ip->i_mount;
2447 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
2448 if (unlikely(align)) {
2449 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
2450 align, 0, ap->eof, 0, ap->conv,
2451 &ap->offset, &ap->length);
2455 nullfb = *ap->firstblock == NULLFSBLOCK;
2456 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
2458 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2459 ag = xfs_filestream_lookup_ag(ap->ip);
2460 ag = (ag != NULLAGNUMBER) ? ag : 0;
2461 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
2463 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2466 ap->blkno = *ap->firstblock;
2468 xfs_bmap_adjacent(ap);
2471 * If allowed, use ap->blkno; otherwise must use firstblock since
2472 * it's in the right allocation group.
2474 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
2477 ap->blkno = *ap->firstblock;
2479 * Normal allocation, done through xfs_alloc_vextent.
2481 tryagain = isaligned = 0;
2482 memset(&args, 0, sizeof(args));
2485 args.fsbno = ap->blkno;
2487 /* Trim the allocation back to the maximum an AG can fit. */
2488 args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
2489 args.firstblock = *ap->firstblock;
2492 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
2495 } else if (ap->flist->xbf_low) {
2496 if (xfs_inode_is_filestream(ap->ip))
2497 args.type = XFS_ALLOCTYPE_FIRST_AG;
2499 args.type = XFS_ALLOCTYPE_START_BNO;
2500 args.total = args.minlen = ap->minlen;
2502 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2503 args.total = ap->total;
2504 args.minlen = ap->minlen;
2506 /* apply extent size hints if obtained earlier */
2507 if (unlikely(align)) {
2509 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
2510 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2511 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
2515 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
2516 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
2517 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2520 * If we are not low on available data blocks, and the
2521 * underlying logical volume manager is a stripe, and
2522 * the file offset is zero then try to allocate data
2523 * blocks on stripe unit boundary.
2524 * NOTE: ap->aeof is only set if the allocation length
2525 * is >= the stripe unit and the allocation offset is
2526 * at the end of file.
2528 if (!ap->flist->xbf_low && ap->aeof) {
2530 args.alignment = mp->m_dalign;
2534 * Adjust for alignment
2536 if (blen > args.alignment && blen <= args.maxlen)
2537 args.minlen = blen - args.alignment;
2538 args.minalignslop = 0;
2541 * First try an exact bno allocation.
2542 * If it fails then do a near or start bno
2543 * allocation with alignment turned on.
2547 args.type = XFS_ALLOCTYPE_THIS_BNO;
2550 * Compute the minlen+alignment for the
2551 * next case. Set slop so that the value
2552 * of minlen+alignment+slop doesn't go up
2553 * between the calls.
2555 if (blen > mp->m_dalign && blen <= args.maxlen)
2556 nextminlen = blen - mp->m_dalign;
2558 nextminlen = args.minlen;
2559 if (nextminlen + mp->m_dalign > args.minlen + 1)
2561 nextminlen + mp->m_dalign -
2564 args.minalignslop = 0;
2568 args.minalignslop = 0;
2570 args.minleft = ap->minleft;
2571 args.wasdel = ap->wasdel;
2573 args.userdata = ap->userdata;
2574 if ((error = xfs_alloc_vextent(&args)))
2576 if (tryagain && args.fsbno == NULLFSBLOCK) {
2578 * Exact allocation failed. Now try with alignment
2582 args.fsbno = ap->blkno;
2583 args.alignment = mp->m_dalign;
2584 args.minlen = nextminlen;
2585 args.minalignslop = 0;
2587 if ((error = xfs_alloc_vextent(&args)))
2590 if (isaligned && args.fsbno == NULLFSBLOCK) {
2592 * allocation failed, so turn off alignment and
2596 args.fsbno = ap->blkno;
2598 if ((error = xfs_alloc_vextent(&args)))
2601 if (args.fsbno == NULLFSBLOCK && nullfb &&
2602 args.minlen > ap->minlen) {
2603 args.minlen = ap->minlen;
2604 args.type = XFS_ALLOCTYPE_START_BNO;
2605 args.fsbno = ap->blkno;
2606 if ((error = xfs_alloc_vextent(&args)))
2609 if (args.fsbno == NULLFSBLOCK && nullfb) {
2611 args.type = XFS_ALLOCTYPE_FIRST_AG;
2612 args.total = ap->minlen;
2614 if ((error = xfs_alloc_vextent(&args)))
2616 ap->flist->xbf_low = 1;
2618 if (args.fsbno != NULLFSBLOCK) {
2620 * check the allocation happened at the same or higher AG than
2621 * the first block that was allocated.
2623 ASSERT(*ap->firstblock == NULLFSBLOCK ||
2624 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
2625 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
2626 (ap->flist->xbf_low &&
2627 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
2628 XFS_FSB_TO_AGNO(mp, args.fsbno)));
2630 ap->blkno = args.fsbno;
2631 if (*ap->firstblock == NULLFSBLOCK)
2632 *ap->firstblock = args.fsbno;
2633 ASSERT(nullfb || fb_agno == args.agno ||
2634 (ap->flist->xbf_low && fb_agno < args.agno));
2635 ap->length = args.len;
2636 ap->ip->i_d.di_nblocks += args.len;
2637 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2639 ap->ip->i_delayed_blks -= args.len;
2641 * Adjust the disk quota also. This was reserved
2644 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2645 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2646 XFS_TRANS_DQ_BCOUNT,
2649 ap->blkno = NULLFSBLOCK;
2656 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2657 * It figures out where to ask the underlying allocator to put the new extent.
2661 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2663 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
2664 return xfs_bmap_rtalloc(ap);
2665 return xfs_bmap_btalloc(ap);
2669 * Transform a btree format file with only one leaf node, where the
2670 * extents list will fit in the inode, into an extents format file.
2671 * Since the file extents are already in-core, all we have to do is
2672 * give up the space for the btree root and pitch the leaf block.
2674 STATIC int /* error */
2675 xfs_bmap_btree_to_extents(
2676 xfs_trans_t *tp, /* transaction pointer */
2677 xfs_inode_t *ip, /* incore inode pointer */
2678 xfs_btree_cur_t *cur, /* btree cursor */
2679 int *logflagsp, /* inode logging flags */
2680 int whichfork) /* data or attr fork */
2683 struct xfs_btree_block *cblock;/* child btree block */
2684 xfs_fsblock_t cbno; /* child block number */
2685 xfs_buf_t *cbp; /* child block's buffer */
2686 int error; /* error return value */
2687 xfs_ifork_t *ifp; /* inode fork data */
2688 xfs_mount_t *mp; /* mount point structure */
2689 __be64 *pp; /* ptr to block address */
2690 struct xfs_btree_block *rblock;/* root btree block */
2693 ifp = XFS_IFORK_PTR(ip, whichfork);
2694 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2695 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2696 rblock = ifp->if_broot;
2697 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2698 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
2699 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2700 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
2701 cbno = be64_to_cpu(*pp);
2704 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
2707 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
2711 cblock = XFS_BUF_TO_BLOCK(cbp);
2712 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
2714 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2715 ip->i_d.di_nblocks--;
2716 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
2717 xfs_trans_binval(tp, cbp);
2718 if (cur->bc_bufs[0] == cbp)
2719 cur->bc_bufs[0] = NULL;
2720 xfs_iroot_realloc(ip, -1, whichfork);
2721 ASSERT(ifp->if_broot == NULL);
2722 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2723 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
2724 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2729 * Called by xfs_bmapi to update file extent records and the btree
2730 * after removing space (or undoing a delayed allocation).
2732 STATIC int /* error */
2733 xfs_bmap_del_extent(
2734 xfs_inode_t *ip, /* incore inode pointer */
2735 xfs_trans_t *tp, /* current transaction pointer */
2736 xfs_extnum_t *idx, /* extent number to update/delete */
2737 xfs_bmap_free_t *flist, /* list of extents to be freed */
2738 xfs_btree_cur_t *cur, /* if null, not a btree */
2739 xfs_bmbt_irec_t *del, /* data to remove from extents */
2740 int *logflagsp, /* inode logging flags */
2741 int whichfork) /* data or attr fork */
2743 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2744 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2745 xfs_fsblock_t del_endblock=0; /* first block past del */
2746 xfs_fileoff_t del_endoff; /* first offset past del */
2747 int delay; /* current block is delayed allocated */
2748 int do_fx; /* free extent at end of routine */
2749 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
2750 int error; /* error return value */
2751 int flags; /* inode logging flags */
2752 xfs_bmbt_irec_t got; /* current extent entry */
2753 xfs_fileoff_t got_endoff; /* first offset past got */
2754 int i; /* temp state */
2755 xfs_ifork_t *ifp; /* inode fork pointer */
2756 xfs_mount_t *mp; /* mount structure */
2757 xfs_filblks_t nblks; /* quota/sb block count */
2758 xfs_bmbt_irec_t new; /* new record to be inserted */
2760 uint qfield; /* quota field to update */
2761 xfs_filblks_t temp; /* for indirect length calculations */
2762 xfs_filblks_t temp2; /* for indirect length calculations */
2765 XFS_STATS_INC(xs_del_exlist);
2767 if (whichfork == XFS_ATTR_FORK)
2768 state |= BMAP_ATTRFORK;
2771 ifp = XFS_IFORK_PTR(ip, whichfork);
2772 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
2773 (uint)sizeof(xfs_bmbt_rec_t)));
2774 ASSERT(del->br_blockcount > 0);
2775 ep = xfs_iext_get_ext(ifp, *idx);
2776 xfs_bmbt_get_all(ep, &got);
2777 ASSERT(got.br_startoff <= del->br_startoff);
2778 del_endoff = del->br_startoff + del->br_blockcount;
2779 got_endoff = got.br_startoff + got.br_blockcount;
2780 ASSERT(got_endoff >= del_endoff);
2781 delay = isnullstartblock(got.br_startblock);
2782 ASSERT(isnullstartblock(del->br_startblock) == delay);
2787 * If deleting a real allocation, must free up the disk space.
2790 flags = XFS_ILOG_CORE;
2792 * Realtime allocation. Free it and record di_nblocks update.
2794 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
2798 ASSERT(do_mod(del->br_blockcount,
2799 mp->m_sb.sb_rextsize) == 0);
2800 ASSERT(do_mod(del->br_startblock,
2801 mp->m_sb.sb_rextsize) == 0);
2802 bno = del->br_startblock;
2803 len = del->br_blockcount;
2804 do_div(bno, mp->m_sb.sb_rextsize);
2805 do_div(len, mp->m_sb.sb_rextsize);
2806 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
2810 nblks = len * mp->m_sb.sb_rextsize;
2811 qfield = XFS_TRANS_DQ_RTBCOUNT;
2814 * Ordinary allocation.
2818 nblks = del->br_blockcount;
2819 qfield = XFS_TRANS_DQ_BCOUNT;
2822 * Set up del_endblock and cur for later.
2824 del_endblock = del->br_startblock + del->br_blockcount;
2826 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2827 got.br_startblock, got.br_blockcount,
2830 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2832 da_old = da_new = 0;
2834 da_old = startblockval(got.br_startblock);
2840 * Set flag value to use in switch statement.
2841 * Left-contig is 2, right-contig is 1.
2843 switch (((got.br_startoff == del->br_startoff) << 1) |
2844 (got_endoff == del_endoff)) {
2847 * Matches the whole extent. Delete the entry.
2849 xfs_iext_remove(ip, *idx, 1,
2850 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
2855 XFS_IFORK_NEXT_SET(ip, whichfork,
2856 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2857 flags |= XFS_ILOG_CORE;
2859 flags |= xfs_ilog_fext(whichfork);
2862 if ((error = xfs_btree_delete(cur, &i)))
2864 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2869 * Deleting the first part of the extent.
2871 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2872 xfs_bmbt_set_startoff(ep, del_endoff);
2873 temp = got.br_blockcount - del->br_blockcount;
2874 xfs_bmbt_set_blockcount(ep, temp);
2876 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2878 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2879 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2883 xfs_bmbt_set_startblock(ep, del_endblock);
2884 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2886 flags |= xfs_ilog_fext(whichfork);
2889 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2890 got.br_blockcount - del->br_blockcount,
2897 * Deleting the last part of the extent.
2899 temp = got.br_blockcount - del->br_blockcount;
2900 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2901 xfs_bmbt_set_blockcount(ep, temp);
2903 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2905 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2906 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2910 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2912 flags |= xfs_ilog_fext(whichfork);
2915 if ((error = xfs_bmbt_update(cur, got.br_startoff,
2917 got.br_blockcount - del->br_blockcount,
2924 * Deleting the middle of the extent.
2926 temp = del->br_startoff - got.br_startoff;
2927 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2928 xfs_bmbt_set_blockcount(ep, temp);
2929 new.br_startoff = del_endoff;
2930 temp2 = got_endoff - del_endoff;
2931 new.br_blockcount = temp2;
2932 new.br_state = got.br_state;
2934 new.br_startblock = del_endblock;
2935 flags |= XFS_ILOG_CORE;
2937 if ((error = xfs_bmbt_update(cur,
2939 got.br_startblock, temp,
2942 if ((error = xfs_btree_increment(cur, 0, &i)))
2944 cur->bc_rec.b = new;
2945 error = xfs_btree_insert(cur, &i);
2946 if (error && error != ENOSPC)
2949 * If get no-space back from btree insert,
2950 * it tried a split, and we have a zero
2951 * block reservation.
2952 * Fix up our state and return the error.
2954 if (error == ENOSPC) {
2956 * Reset the cursor, don't trust
2957 * it after any insert operation.
2959 if ((error = xfs_bmbt_lookup_eq(cur,
2964 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2966 * Update the btree record back
2967 * to the original value.
2969 if ((error = xfs_bmbt_update(cur,
2976 * Reset the extent record back
2977 * to the original value.
2979 xfs_bmbt_set_blockcount(ep,
2982 error = XFS_ERROR(ENOSPC);
2985 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2987 flags |= xfs_ilog_fext(whichfork);
2988 XFS_IFORK_NEXT_SET(ip, whichfork,
2989 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2991 ASSERT(whichfork == XFS_DATA_FORK);
2992 temp = xfs_bmap_worst_indlen(ip, temp);
2993 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2994 temp2 = xfs_bmap_worst_indlen(ip, temp2);
2995 new.br_startblock = nullstartblock((int)temp2);
2996 da_new = temp + temp2;
2997 while (da_new > da_old) {
3001 xfs_bmbt_set_startblock(ep,
3002 nullstartblock((int)temp));
3004 if (da_new == da_old)
3010 nullstartblock((int)temp2);
3014 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
3015 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
3020 * If we need to, add to list of extents to delete.
3023 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3026 * Adjust inode # blocks in the file.
3029 ip->i_d.di_nblocks -= nblks;
3031 * Adjust quota data.
3034 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
3037 * Account for change in delayed indirect blocks.
3038 * Nothing to do for disk quota accounting here.
3040 ASSERT(da_old >= da_new);
3041 if (da_old > da_new) {
3042 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
3043 (int64_t)(da_old - da_new), 0);
3051 * Remove the entry "free" from the free item list. Prev points to the
3052 * previous entry, unless "free" is the head of the list.
3056 xfs_bmap_free_t *flist, /* free item list header */
3057 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3058 xfs_bmap_free_item_t *free) /* list item to be freed */
3061 prev->xbfi_next = free->xbfi_next;
3063 flist->xbf_first = free->xbfi_next;
3065 kmem_zone_free(xfs_bmap_free_item_zone, free);
3069 * Convert an extents-format file into a btree-format file.
3070 * The new file will have a root block (in the inode) and a single child block.
3072 STATIC int /* error */
3073 xfs_bmap_extents_to_btree(
3074 xfs_trans_t *tp, /* transaction pointer */
3075 xfs_inode_t *ip, /* incore inode pointer */
3076 xfs_fsblock_t *firstblock, /* first-block-allocated */
3077 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3078 xfs_btree_cur_t **curp, /* cursor returned to caller */
3079 int wasdel, /* converting a delayed alloc */
3080 int *logflagsp, /* inode logging flags */
3081 int whichfork) /* data or attr fork */
3083 struct xfs_btree_block *ablock; /* allocated (child) bt block */
3084 xfs_buf_t *abp; /* buffer for ablock */
3085 xfs_alloc_arg_t args; /* allocation arguments */
3086 xfs_bmbt_rec_t *arp; /* child record pointer */
3087 struct xfs_btree_block *block; /* btree root block */
3088 xfs_btree_cur_t *cur; /* bmap btree cursor */
3089 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3090 int error; /* error return value */
3091 xfs_extnum_t i, cnt; /* extent record index */
3092 xfs_ifork_t *ifp; /* inode fork pointer */
3093 xfs_bmbt_key_t *kp; /* root block key pointer */
3094 xfs_mount_t *mp; /* mount structure */
3095 xfs_extnum_t nextents; /* number of file extents */
3096 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3098 ifp = XFS_IFORK_PTR(ip, whichfork);
3099 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3102 * Make space in the inode incore.
3104 xfs_iroot_realloc(ip, 1, whichfork);
3105 ifp->if_flags |= XFS_IFBROOT;
3110 block = ifp->if_broot;
3111 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3112 block->bb_level = cpu_to_be16(1);
3113 block->bb_numrecs = cpu_to_be16(1);
3114 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3115 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3118 * Need a cursor. Can't allocate until bb_level is filled in.
3121 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
3122 cur->bc_private.b.firstblock = *firstblock;
3123 cur->bc_private.b.flist = flist;
3124 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3126 * Convert to a btree with two levels, one record in root.
3128 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3129 memset(&args, 0, sizeof(args));
3132 args.firstblock = *firstblock;
3133 if (*firstblock == NULLFSBLOCK) {
3134 args.type = XFS_ALLOCTYPE_START_BNO;
3135 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3136 } else if (flist->xbf_low) {
3137 args.type = XFS_ALLOCTYPE_START_BNO;
3138 args.fsbno = *firstblock;
3140 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3141 args.fsbno = *firstblock;
3143 args.minlen = args.maxlen = args.prod = 1;
3144 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3145 args.minalignslop = 0;
3146 args.wasdel = wasdel;
3148 if ((error = xfs_alloc_vextent(&args))) {
3149 xfs_iroot_realloc(ip, -1, whichfork);
3150 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3154 * Allocation can't fail, the space was reserved.
3156 ASSERT(args.fsbno != NULLFSBLOCK);
3157 ASSERT(*firstblock == NULLFSBLOCK ||
3158 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3160 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3161 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3162 cur->bc_private.b.allocated++;
3163 ip->i_d.di_nblocks++;
3164 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3165 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3167 * Fill in the child block.
3169 abp->b_ops = &xfs_bmbt_buf_ops;
3170 ablock = XFS_BUF_TO_BLOCK(abp);
3171 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3172 ablock->bb_level = 0;
3173 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3174 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3175 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3176 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3177 for (cnt = i = 0; i < nextents; i++) {
3178 ep = xfs_iext_get_ext(ifp, i);
3179 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
3180 arp->l0 = cpu_to_be64(ep->l0);
3181 arp->l1 = cpu_to_be64(ep->l1);
3185 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3186 xfs_btree_set_numrecs(ablock, cnt);
3189 * Fill in the root key and pointer.
3191 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3192 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3193 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
3194 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3195 be16_to_cpu(block->bb_level)));
3196 *pp = cpu_to_be64(args.fsbno);
3199 * Do all this logging at the end so that
3200 * the root is at the right level.
3202 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3203 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3204 ASSERT(*curp == NULL);
3206 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
3211 * Calculate the default attribute fork offset for newly created inodes.
3214 xfs_default_attroffset(
3215 struct xfs_inode *ip)
3217 struct xfs_mount *mp = ip->i_mount;
3220 if (mp->m_sb.sb_inodesize == 256) {
3221 offset = XFS_LITINO(mp) -
3222 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3224 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3227 ASSERT(offset < XFS_LITINO(mp));
3232 * Helper routine to reset inode di_forkoff field when switching
3233 * attribute fork from local to extent format - we reset it where
3234 * possible to make space available for inline data fork extents.
3237 xfs_bmap_forkoff_reset(
3242 if (whichfork == XFS_ATTR_FORK &&
3243 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3244 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3245 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3246 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3248 if (dfl_forkoff > ip->i_d.di_forkoff)
3249 ip->i_d.di_forkoff = dfl_forkoff;
3254 * Convert a local file to an extents file.
3255 * This code is out of bounds for data forks of regular files,
3256 * since the file data needs to get logged so things will stay consistent.
3257 * (The bmap-level manipulations are ok, though).
3259 STATIC int /* error */
3260 xfs_bmap_local_to_extents(
3261 xfs_trans_t *tp, /* transaction pointer */
3262 xfs_inode_t *ip, /* incore inode pointer */
3263 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3264 xfs_extlen_t total, /* total blocks needed by transaction */
3265 int *logflagsp, /* inode logging flags */
3267 void (*init_fn)(struct xfs_buf *bp,
3268 struct xfs_inode *ip,
3269 struct xfs_ifork *ifp))
3271 int error; /* error return value */
3272 int flags; /* logging flags returned */
3273 xfs_ifork_t *ifp; /* inode fork pointer */
3276 * We don't want to deal with the case of keeping inode data inline yet.
3277 * So sending the data fork of a regular inode is invalid.
3279 ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK));
3280 ifp = XFS_IFORK_PTR(ip, whichfork);
3281 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3284 if (ifp->if_bytes) {
3285 xfs_alloc_arg_t args; /* allocation arguments */
3286 xfs_buf_t *bp; /* buffer for extent block */
3287 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
3289 ASSERT((ifp->if_flags &
3290 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
3291 memset(&args, 0, sizeof(args));
3293 args.mp = ip->i_mount;
3294 args.firstblock = *firstblock;
3296 * Allocate a block. We know we need only one, since the
3297 * file currently fits in an inode.
3299 if (*firstblock == NULLFSBLOCK) {
3300 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3301 args.type = XFS_ALLOCTYPE_START_BNO;
3303 args.fsbno = *firstblock;
3304 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3307 args.mod = args.minleft = args.alignment = args.wasdel =
3308 args.isfl = args.minalignslop = 0;
3309 args.minlen = args.maxlen = args.prod = 1;
3310 error = xfs_alloc_vextent(&args);
3314 /* Can't fail, the space was reserved. */
3315 ASSERT(args.fsbno != NULLFSBLOCK);
3316 ASSERT(args.len == 1);
3317 *firstblock = args.fsbno;
3318 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3320 /* initialise the block and copy the data */
3321 init_fn(bp, ip, ifp);
3323 /* account for the change in fork size and log everything */
3324 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3325 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3326 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3327 xfs_iext_add(ifp, 0, 1);
3328 ep = xfs_iext_get_ext(ifp, 0);
3329 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3330 trace_xfs_bmap_post_update(ip, 0,
3331 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3333 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3334 ip->i_d.di_nblocks = 1;
3335 xfs_trans_mod_dquot_byino(tp, ip,
3336 XFS_TRANS_DQ_BCOUNT, 1L);
3337 flags |= xfs_ilog_fext(whichfork);
3339 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3340 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3342 ifp->if_flags &= ~XFS_IFINLINE;
3343 ifp->if_flags |= XFS_IFEXTENTS;
3344 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3345 flags |= XFS_ILOG_CORE;
3352 * Search the extent records for the entry containing block bno.
3353 * If bno lies in a hole, point to the next entry. If bno lies
3354 * past eof, *eofp will be set, and *prevp will contain the last
3355 * entry (null if none). Else, *lastxp will be set to the index
3356 * of the found entry; *gotp will contain the entry.
3358 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3359 xfs_bmap_search_multi_extents(
3360 xfs_ifork_t *ifp, /* inode fork pointer */
3361 xfs_fileoff_t bno, /* block number searched for */
3362 int *eofp, /* out: end of file found */
3363 xfs_extnum_t *lastxp, /* out: last extent index */
3364 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3365 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3367 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3368 xfs_extnum_t lastx; /* last extent index */
3371 * Initialize the extent entry structure to catch access to
3372 * uninitialized br_startblock field.
3374 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3375 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3376 gotp->br_state = XFS_EXT_INVALID;
3378 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3380 gotp->br_startblock = 0xffffa5a5;
3382 prevp->br_startoff = NULLFILEOFF;
3384 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3386 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3388 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3389 xfs_bmbt_get_all(ep, gotp);
3403 * Search the extents list for the inode, for the extent containing bno.
3404 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3405 * *eofp will be set, and *prevp will contain the last entry (null if none).
3406 * Else, *lastxp will be set to the index of the found
3407 * entry; *gotp will contain the entry.
3409 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3410 xfs_bmap_search_extents(
3411 xfs_inode_t *ip, /* incore inode pointer */
3412 xfs_fileoff_t bno, /* block number searched for */
3413 int fork, /* data or attr fork */
3414 int *eofp, /* out: end of file found */
3415 xfs_extnum_t *lastxp, /* out: last extent index */
3416 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3417 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3419 xfs_ifork_t *ifp; /* inode fork pointer */
3420 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3422 XFS_STATS_INC(xs_look_exlist);
3423 ifp = XFS_IFORK_PTR(ip, fork);
3425 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3427 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3428 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3429 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
3430 "Access to block zero in inode %llu "
3431 "start_block: %llx start_off: %llx "
3432 "blkcnt: %llx extent-state: %x lastx: %x\n",
3433 (unsigned long long)ip->i_ino,
3434 (unsigned long long)gotp->br_startblock,
3435 (unsigned long long)gotp->br_startoff,
3436 (unsigned long long)gotp->br_blockcount,
3437 gotp->br_state, *lastxp);
3438 *lastxp = NULLEXTNUM;
3446 * Compute the worst-case number of indirect blocks that will be used
3447 * for ip's delayed extent of length "len".
3449 STATIC xfs_filblks_t
3450 xfs_bmap_worst_indlen(
3451 xfs_inode_t *ip, /* incore inode pointer */
3452 xfs_filblks_t len) /* delayed extent length */
3454 int level; /* btree level number */
3455 int maxrecs; /* maximum record count at this level */
3456 xfs_mount_t *mp; /* mount structure */
3457 xfs_filblks_t rval; /* return value */
3460 maxrecs = mp->m_bmap_dmxr[0];
3461 for (level = 0, rval = 0;
3462 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3465 do_div(len, maxrecs);
3468 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3471 maxrecs = mp->m_bmap_dmxr[1];
3477 * Convert inode from non-attributed to attributed.
3478 * Must not be in a transaction, ip must not be locked.
3480 int /* error code */
3481 xfs_bmap_add_attrfork(
3482 xfs_inode_t *ip, /* incore inode pointer */
3483 int size, /* space new attribute needs */
3484 int rsvd) /* xact may use reserved blks */
3486 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
3487 xfs_bmap_free_t flist; /* freed extent records */
3488 xfs_mount_t *mp; /* mount structure */
3489 xfs_trans_t *tp; /* transaction pointer */
3490 int blks; /* space reservation */
3491 int version = 1; /* superblock attr version */
3492 int committed; /* xaction was committed */
3493 int logflags; /* logging flags */
3494 int error; /* error return value */
3496 ASSERT(XFS_IFORK_Q(ip) == 0);
3499 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3500 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3501 blks = XFS_ADDAFORK_SPACE_RES(mp);
3503 tp->t_flags |= XFS_TRANS_RESERVE;
3504 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3505 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3507 xfs_ilock(ip, XFS_ILOCK_EXCL);
3508 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
3509 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3510 XFS_QMOPT_RES_REGBLKS);
3512 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3513 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3516 if (XFS_IFORK_Q(ip))
3518 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3520 * For inodes coming from pre-6.2 filesystems.
3522 ASSERT(ip->i_d.di_aformat == 0);
3523 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3525 ASSERT(ip->i_d.di_anextents == 0);
3527 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3528 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3530 switch (ip->i_d.di_format) {
3531 case XFS_DINODE_FMT_DEV:
3532 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3534 case XFS_DINODE_FMT_UUID:
3535 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3537 case XFS_DINODE_FMT_LOCAL:
3538 case XFS_DINODE_FMT_EXTENTS:
3539 case XFS_DINODE_FMT_BTREE:
3540 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3541 if (!ip->i_d.di_forkoff)
3542 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
3543 else if (mp->m_flags & XFS_MOUNT_ATTR2)
3548 error = XFS_ERROR(EINVAL);
3552 ASSERT(ip->i_afp == NULL);
3553 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3554 ip->i_afp->if_flags = XFS_IFEXTENTS;
3556 xfs_bmap_init(&flist, &firstblock);
3557 switch (ip->i_d.di_format) {
3558 case XFS_DINODE_FMT_LOCAL:
3559 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3562 case XFS_DINODE_FMT_EXTENTS:
3563 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3566 case XFS_DINODE_FMT_BTREE:
3567 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3575 xfs_trans_log_inode(tp, ip, logflags);
3578 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3579 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
3580 __int64_t sbfields = 0;
3582 spin_lock(&mp->m_sb_lock);
3583 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3584 xfs_sb_version_addattr(&mp->m_sb);
3585 sbfields |= XFS_SB_VERSIONNUM;
3587 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3588 xfs_sb_version_addattr2(&mp->m_sb);
3589 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
3592 spin_unlock(&mp->m_sb_lock);
3593 xfs_mod_sb(tp, sbfields);
3595 spin_unlock(&mp->m_sb_lock);
3598 error = xfs_bmap_finish(&tp, &flist, &committed);
3601 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3603 xfs_bmap_cancel(&flist);
3605 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3607 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3612 * Add the extent to the list of extents to be free at transaction end.
3613 * The list is maintained sorted (by block number).
3618 xfs_fsblock_t bno, /* fs block number of extent */
3619 xfs_filblks_t len, /* length of extent */
3620 xfs_bmap_free_t *flist, /* list of extents */
3621 xfs_mount_t *mp) /* mount point structure */
3623 xfs_bmap_free_item_t *cur; /* current (next) element */
3624 xfs_bmap_free_item_t *new; /* new element */
3625 xfs_bmap_free_item_t *prev; /* previous element */
3627 xfs_agnumber_t agno;
3628 xfs_agblock_t agbno;
3630 ASSERT(bno != NULLFSBLOCK);
3632 ASSERT(len <= MAXEXTLEN);
3633 ASSERT(!isnullstartblock(bno));
3634 agno = XFS_FSB_TO_AGNO(mp, bno);
3635 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3636 ASSERT(agno < mp->m_sb.sb_agcount);
3637 ASSERT(agbno < mp->m_sb.sb_agblocks);
3638 ASSERT(len < mp->m_sb.sb_agblocks);
3639 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3641 ASSERT(xfs_bmap_free_item_zone != NULL);
3642 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3643 new->xbfi_startblock = bno;
3644 new->xbfi_blockcount = (xfs_extlen_t)len;
3645 for (prev = NULL, cur = flist->xbf_first;
3647 prev = cur, cur = cur->xbfi_next) {
3648 if (cur->xbfi_startblock >= bno)
3652 prev->xbfi_next = new;
3654 flist->xbf_first = new;
3655 new->xbfi_next = cur;
3660 * Compute and fill in the value of the maximum depth of a bmap btree
3661 * in this filesystem. Done once, during mount.
3664 xfs_bmap_compute_maxlevels(
3665 xfs_mount_t *mp, /* file system mount structure */
3666 int whichfork) /* data or attr fork */
3668 int level; /* btree level */
3669 uint maxblocks; /* max blocks at this level */
3670 uint maxleafents; /* max leaf entries possible */
3671 int maxrootrecs; /* max records in root block */
3672 int minleafrecs; /* min records in leaf block */
3673 int minnoderecs; /* min records in node block */
3674 int sz; /* root block size */
3677 * The maximum number of extents in a file, hence the maximum
3678 * number of leaf entries, is controlled by the type of di_nextents
3679 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3680 * (a signed 16-bit number, xfs_aextnum_t).
3682 * Note that we can no longer assume that if we are in ATTR1 that
3683 * the fork offset of all the inodes will be
3684 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3685 * with ATTR2 and then mounted back with ATTR1, keeping the
3686 * di_forkoff's fixed but probably at various positions. Therefore,
3687 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3688 * of a minimum size available.
3690 if (whichfork == XFS_DATA_FORK) {
3691 maxleafents = MAXEXTNUM;
3692 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
3694 maxleafents = MAXAEXTNUM;
3695 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
3697 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
3698 minleafrecs = mp->m_bmap_dmnr[0];
3699 minnoderecs = mp->m_bmap_dmnr[1];
3700 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3701 for (level = 1; maxblocks > 1; level++) {
3702 if (maxblocks <= maxrootrecs)
3705 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3707 mp->m_bm_maxlevels[whichfork] = level;
3711 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3712 * caller. Frees all the extents that need freeing, which must be done
3713 * last due to locking considerations. We never free any extents in
3714 * the first transaction.
3716 * Return 1 if the given transaction was committed and a new one
3717 * started, and 0 otherwise in the committed parameter.
3721 xfs_trans_t **tp, /* transaction pointer addr */
3722 xfs_bmap_free_t *flist, /* i/o: list extents to free */
3723 int *committed) /* xact committed or not */
3725 xfs_efd_log_item_t *efd; /* extent free data */
3726 xfs_efi_log_item_t *efi; /* extent free intention */
3727 int error; /* error return value */
3728 xfs_bmap_free_item_t *free; /* free extent item */
3729 unsigned int logres; /* new log reservation */
3730 unsigned int logcount; /* new log count */
3731 xfs_mount_t *mp; /* filesystem mount structure */
3732 xfs_bmap_free_item_t *next; /* next item on free list */
3733 xfs_trans_t *ntp; /* new transaction pointer */
3735 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3736 if (flist->xbf_count == 0) {
3741 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3742 for (free = flist->xbf_first; free; free = free->xbfi_next)
3743 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3744 free->xbfi_blockcount);
3745 logres = ntp->t_log_res;
3746 logcount = ntp->t_log_count;
3747 ntp = xfs_trans_dup(*tp);
3748 error = xfs_trans_commit(*tp, 0);
3752 * We have a new transaction, so we should return committed=1,
3753 * even though we're returning an error.
3759 * transaction commit worked ok so we can drop the extra ticket
3760 * reference that we gained in xfs_trans_dup()
3762 xfs_log_ticket_put(ntp->t_ticket);
3764 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
3767 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
3768 for (free = flist->xbf_first; free != NULL; free = next) {
3769 next = free->xbfi_next;
3770 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
3771 free->xbfi_blockcount))) {
3773 * The bmap free list will be cleaned up at a
3774 * higher level. The EFI will be canceled when
3775 * this transaction is aborted.
3776 * Need to force shutdown here to make sure it
3777 * happens, since this transaction may not be
3781 if (!XFS_FORCED_SHUTDOWN(mp))
3782 xfs_force_shutdown(mp,
3783 (error == EFSCORRUPTED) ?
3784 SHUTDOWN_CORRUPT_INCORE :
3785 SHUTDOWN_META_IO_ERROR);
3788 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
3789 free->xbfi_blockcount);
3790 xfs_bmap_del_free(flist, NULL, free);
3796 * Free up any items left in the list.
3800 xfs_bmap_free_t *flist) /* list of bmap_free_items */
3802 xfs_bmap_free_item_t *free; /* free list item */
3803 xfs_bmap_free_item_t *next;
3805 if (flist->xbf_count == 0)
3807 ASSERT(flist->xbf_first != NULL);
3808 for (free = flist->xbf_first; free; free = next) {
3809 next = free->xbfi_next;
3810 xfs_bmap_del_free(flist, NULL, free);
3812 ASSERT(flist->xbf_count == 0);
3816 * Returns the file-relative block number of the first unused block(s)
3817 * in the file with at least "len" logically contiguous blocks free.
3818 * This is the lowest-address hole if the file has holes, else the first block
3819 * past the end of file.
3820 * Return 0 if the file is currently local (in-inode).
3823 xfs_bmap_first_unused(
3824 xfs_trans_t *tp, /* transaction pointer */
3825 xfs_inode_t *ip, /* incore inode */
3826 xfs_extlen_t len, /* size of hole to find */
3827 xfs_fileoff_t *first_unused, /* unused block */
3828 int whichfork) /* data or attr fork */
3830 int error; /* error return value */
3831 int idx; /* extent record index */
3832 xfs_ifork_t *ifp; /* inode fork pointer */
3833 xfs_fileoff_t lastaddr; /* last block number seen */
3834 xfs_fileoff_t lowest; /* lowest useful block */
3835 xfs_fileoff_t max; /* starting useful block */
3836 xfs_fileoff_t off; /* offset for this block */
3837 xfs_extnum_t nextents; /* number of extent entries */
3839 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
3840 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
3841 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3842 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3846 ifp = XFS_IFORK_PTR(ip, whichfork);
3847 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3848 (error = xfs_iread_extents(tp, ip, whichfork)))
3850 lowest = *first_unused;
3851 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3852 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
3853 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
3854 off = xfs_bmbt_get_startoff(ep);
3856 * See if the hole before this extent will work.
3858 if (off >= lowest + len && off - max >= len) {
3859 *first_unused = max;
3862 lastaddr = off + xfs_bmbt_get_blockcount(ep);
3863 max = XFS_FILEOFF_MAX(lastaddr, lowest);
3865 *first_unused = max;
3870 * Returns the file-relative block number of the last block + 1 before
3871 * last_block (input value) in the file.
3872 * This is not based on i_size, it is based on the extent records.
3873 * Returns 0 for local files, as they do not have extent records.
3876 xfs_bmap_last_before(
3877 xfs_trans_t *tp, /* transaction pointer */
3878 xfs_inode_t *ip, /* incore inode */
3879 xfs_fileoff_t *last_block, /* last block */
3880 int whichfork) /* data or attr fork */
3882 xfs_fileoff_t bno; /* input file offset */
3883 int eof; /* hit end of file */
3884 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
3885 int error; /* error return value */
3886 xfs_bmbt_irec_t got; /* current extent value */
3887 xfs_ifork_t *ifp; /* inode fork pointer */
3888 xfs_extnum_t lastx; /* last extent used */
3889 xfs_bmbt_irec_t prev; /* previous extent value */
3891 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3892 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3893 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3894 return XFS_ERROR(EIO);
3895 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3899 ifp = XFS_IFORK_PTR(ip, whichfork);
3900 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3901 (error = xfs_iread_extents(tp, ip, whichfork)))
3903 bno = *last_block - 1;
3904 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
3906 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
3907 if (prev.br_startoff == NULLFILEOFF)
3910 *last_block = prev.br_startoff + prev.br_blockcount;
3913 * Otherwise *last_block is already the right answer.
3919 xfs_bmap_last_extent(
3920 struct xfs_trans *tp,
3921 struct xfs_inode *ip,
3923 struct xfs_bmbt_irec *rec,
3926 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3930 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3931 error = xfs_iread_extents(tp, ip, whichfork);
3936 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
3937 if (nextents == 0) {
3942 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
3948 * Check the last inode extent to determine whether this allocation will result
3949 * in blocks being allocated at the end of the file. When we allocate new data
3950 * blocks at the end of the file which do not start at the previous data block,
3951 * we will try to align the new blocks at stripe unit boundaries.
3953 * Returns 0 in bma->aeof if the file (fork) is empty as any new write will be
3954 * at, or past the EOF.
3958 struct xfs_bmalloca *bma,
3961 struct xfs_bmbt_irec rec;
3966 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
3968 if (error || is_empty)
3972 * Check if we are allocation or past the last extent, or at least into
3973 * the last delayed allocated extent.
3975 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
3976 (bma->offset >= rec.br_startoff &&
3977 isnullstartblock(rec.br_startblock));
3982 * Check if the endoff is outside the last extent. If so the caller will grow
3983 * the allocation to a stripe unit boundary. All offsets are considered outside
3984 * the end of file for an empty fork, so 1 is returned in *eof in that case.
3988 struct xfs_inode *ip,
3989 xfs_fileoff_t endoff,
3993 struct xfs_bmbt_irec rec;
3996 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
4000 *eof = endoff >= rec.br_startoff + rec.br_blockcount;
4005 * Returns the file-relative block number of the first block past eof in
4006 * the file. This is not based on i_size, it is based on the extent records.
4007 * Returns 0 for local files, as they do not have extent records.
4010 xfs_bmap_last_offset(
4011 struct xfs_trans *tp,
4012 struct xfs_inode *ip,
4013 xfs_fileoff_t *last_block,
4016 struct xfs_bmbt_irec rec;
4022 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
4025 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4026 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4027 return XFS_ERROR(EIO);
4029 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
4030 if (error || is_empty)
4033 *last_block = rec.br_startoff + rec.br_blockcount;
4038 * Returns whether the selected fork of the inode has exactly one
4039 * block or not. For the data fork we check this matches di_size,
4040 * implying the file's range is 0..bsize-1.
4042 int /* 1=>1 block, 0=>otherwise */
4044 xfs_inode_t *ip, /* incore inode */
4045 int whichfork) /* data or attr fork */
4047 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
4048 xfs_ifork_t *ifp; /* inode fork pointer */
4049 int rval; /* return value */
4050 xfs_bmbt_irec_t s; /* internal version of extent */
4053 if (whichfork == XFS_DATA_FORK)
4054 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
4056 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4058 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4060 ifp = XFS_IFORK_PTR(ip, whichfork);
4061 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4062 ep = xfs_iext_get_ext(ifp, 0);
4063 xfs_bmbt_get_all(ep, &s);
4064 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4065 if (rval && whichfork == XFS_DATA_FORK)
4066 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
4071 xfs_bmap_sanity_check(
4072 struct xfs_mount *mp,
4076 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4078 if (block->bb_magic != cpu_to_be32(XFS_BMAP_MAGIC) ||
4079 be16_to_cpu(block->bb_level) != level ||
4080 be16_to_cpu(block->bb_numrecs) == 0 ||
4081 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4087 * Read in the extents to if_extents.
4088 * All inode fields are set up by caller, we just traverse the btree
4089 * and copy the records in. If the file system cannot contain unwritten
4090 * extents, the records are checked for no "state" flags.
4093 xfs_bmap_read_extents(
4094 xfs_trans_t *tp, /* transaction pointer */
4095 xfs_inode_t *ip, /* incore inode */
4096 int whichfork) /* data or attr fork */
4098 struct xfs_btree_block *block; /* current btree block */
4099 xfs_fsblock_t bno; /* block # of "block" */
4100 xfs_buf_t *bp; /* buffer for "block" */
4101 int error; /* error return value */
4102 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4103 xfs_extnum_t i, j; /* index into the extents list */
4104 xfs_ifork_t *ifp; /* fork structure */
4105 int level; /* btree level, for checking */
4106 xfs_mount_t *mp; /* file system mount structure */
4107 __be64 *pp; /* pointer to block address */
4109 xfs_extnum_t room; /* number of entries there's room for */
4113 ifp = XFS_IFORK_PTR(ip, whichfork);
4114 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4115 XFS_EXTFMT_INODE(ip);
4116 block = ifp->if_broot;
4118 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4120 level = be16_to_cpu(block->bb_level);
4122 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
4123 bno = be64_to_cpu(*pp);
4124 ASSERT(bno != NULLDFSBNO);
4125 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4126 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
4128 * Go down the tree until leaf level is reached, following the first
4129 * pointer (leftmost) at each level.
4131 while (level-- > 0) {
4132 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4133 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
4136 block = XFS_BUF_TO_BLOCK(bp);
4137 XFS_WANT_CORRUPTED_GOTO(
4138 xfs_bmap_sanity_check(mp, bp, level),
4142 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
4143 bno = be64_to_cpu(*pp);
4144 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
4145 xfs_trans_brelse(tp, bp);
4148 * Here with bp and block set to the leftmost leaf node in the tree.
4150 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4153 * Loop over all leaf nodes. Copy information to the extent records.
4156 xfs_bmbt_rec_t *frp;
4157 xfs_fsblock_t nextbno;
4158 xfs_extnum_t num_recs;
4161 num_recs = xfs_btree_get_numrecs(block);
4162 if (unlikely(i + num_recs > room)) {
4163 ASSERT(i + num_recs <= room);
4164 xfs_warn(ip->i_mount,
4165 "corrupt dinode %Lu, (btree extents).",
4166 (unsigned long long) ip->i_ino);
4167 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
4168 XFS_ERRLEVEL_LOW, ip->i_mount, block);
4171 XFS_WANT_CORRUPTED_GOTO(
4172 xfs_bmap_sanity_check(mp, bp, 0),
4175 * Read-ahead the next leaf block, if any.
4177 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
4178 if (nextbno != NULLFSBLOCK)
4179 xfs_btree_reada_bufl(mp, nextbno, 1,
4182 * Copy records into the extent records.
4184 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
4186 for (j = 0; j < num_recs; j++, i++, frp++) {
4187 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
4188 trp->l0 = be64_to_cpu(frp->l0);
4189 trp->l1 = be64_to_cpu(frp->l1);
4191 if (exntf == XFS_EXTFMT_NOSTATE) {
4193 * Check all attribute bmap btree records and
4194 * any "older" data bmap btree records for a
4195 * set bit in the "extent flag" position.
4197 if (unlikely(xfs_check_nostate_extents(ifp,
4198 start, num_recs))) {
4199 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4205 xfs_trans_brelse(tp, bp);
4208 * If we've reached the end, stop.
4210 if (bno == NULLFSBLOCK)
4212 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4213 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
4216 block = XFS_BUF_TO_BLOCK(bp);
4218 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4219 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4220 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
4223 xfs_trans_brelse(tp, bp);
4224 return XFS_ERROR(EFSCORRUPTED);
4229 * Add bmap trace insert entries for all the contents of the extent records.
4232 xfs_bmap_trace_exlist(
4233 xfs_inode_t *ip, /* incore inode pointer */
4234 xfs_extnum_t cnt, /* count of entries in the list */
4235 int whichfork, /* data or attr fork */
4236 unsigned long caller_ip)
4238 xfs_extnum_t idx; /* extent record index */
4239 xfs_ifork_t *ifp; /* inode fork pointer */
4242 if (whichfork == XFS_ATTR_FORK)
4243 state |= BMAP_ATTRFORK;
4245 ifp = XFS_IFORK_PTR(ip, whichfork);
4246 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4247 for (idx = 0; idx < cnt; idx++)
4248 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
4252 * Validate that the bmbt_irecs being returned from bmapi are valid
4253 * given the callers original parameters. Specifically check the
4254 * ranges of the returned irecs to ensure that they only extent beyond
4255 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4258 xfs_bmap_validate_ret(
4262 xfs_bmbt_irec_t *mval,
4266 int i; /* index to map values */
4268 ASSERT(ret_nmap <= nmap);
4270 for (i = 0; i < ret_nmap; i++) {
4271 ASSERT(mval[i].br_blockcount > 0);
4272 if (!(flags & XFS_BMAPI_ENTIRE)) {
4273 ASSERT(mval[i].br_startoff >= bno);
4274 ASSERT(mval[i].br_blockcount <= len);
4275 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4278 ASSERT(mval[i].br_startoff < bno + len);
4279 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4283 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4284 mval[i].br_startoff);
4285 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4286 mval[i].br_startblock != HOLESTARTBLOCK);
4287 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4288 mval[i].br_state == XFS_EXT_UNWRITTEN);
4295 * Trim the returned map to the required bounds
4299 struct xfs_bmbt_irec *mval,
4300 struct xfs_bmbt_irec *got,
4308 if ((flags & XFS_BMAPI_ENTIRE) ||
4309 got->br_startoff + got->br_blockcount <= obno) {
4311 if (isnullstartblock(got->br_startblock))
4312 mval->br_startblock = DELAYSTARTBLOCK;
4318 ASSERT((*bno >= obno) || (n == 0));
4320 mval->br_startoff = *bno;
4321 if (isnullstartblock(got->br_startblock))
4322 mval->br_startblock = DELAYSTARTBLOCK;
4324 mval->br_startblock = got->br_startblock +
4325 (*bno - got->br_startoff);
4327 * Return the minimum of what we got and what we asked for for
4328 * the length. We can use the len variable here because it is
4329 * modified below and we could have been there before coming
4330 * here if the first part of the allocation didn't overlap what
4333 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
4334 got->br_blockcount - (*bno - got->br_startoff));
4335 mval->br_state = got->br_state;
4336 ASSERT(mval->br_blockcount <= len);
4341 * Update and validate the extent map to return
4344 xfs_bmapi_update_map(
4345 struct xfs_bmbt_irec **map,
4353 xfs_bmbt_irec_t *mval = *map;
4355 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4356 ((mval->br_startoff + mval->br_blockcount) <= end));
4357 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4358 (mval->br_startoff < obno));
4360 *bno = mval->br_startoff + mval->br_blockcount;
4362 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4363 /* update previous map with new information */
4364 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4365 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4366 ASSERT(mval->br_state == mval[-1].br_state);
4367 mval[-1].br_blockcount = mval->br_blockcount;
4368 mval[-1].br_state = mval->br_state;
4369 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4370 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4371 mval[-1].br_startblock != HOLESTARTBLOCK &&
4372 mval->br_startblock == mval[-1].br_startblock +
4373 mval[-1].br_blockcount &&
4374 ((flags & XFS_BMAPI_IGSTATE) ||
4375 mval[-1].br_state == mval->br_state)) {
4376 ASSERT(mval->br_startoff ==
4377 mval[-1].br_startoff + mval[-1].br_blockcount);
4378 mval[-1].br_blockcount += mval->br_blockcount;
4379 } else if (*n > 0 &&
4380 mval->br_startblock == DELAYSTARTBLOCK &&
4381 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4382 mval->br_startoff ==
4383 mval[-1].br_startoff + mval[-1].br_blockcount) {
4384 mval[-1].br_blockcount += mval->br_blockcount;
4385 mval[-1].br_state = mval->br_state;
4386 } else if (!((*n == 0) &&
4387 ((mval->br_startoff + mval->br_blockcount) <=
4396 * Map file blocks to filesystem blocks without allocation.
4400 struct xfs_inode *ip,
4403 struct xfs_bmbt_irec *mval,
4407 struct xfs_mount *mp = ip->i_mount;
4408 struct xfs_ifork *ifp;
4409 struct xfs_bmbt_irec got;
4410 struct xfs_bmbt_irec prev;
4417 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4418 XFS_ATTR_FORK : XFS_DATA_FORK;
4421 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4422 XFS_BMAPI_IGSTATE)));
4424 if (unlikely(XFS_TEST_ERROR(
4425 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4426 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4427 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4428 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
4429 return XFS_ERROR(EFSCORRUPTED);
4432 if (XFS_FORCED_SHUTDOWN(mp))
4433 return XFS_ERROR(EIO);
4435 XFS_STATS_INC(xs_blk_mapr);
4437 ifp = XFS_IFORK_PTR(ip, whichfork);
4439 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4440 error = xfs_iread_extents(NULL, ip, whichfork);
4445 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4449 while (bno < end && n < *nmap) {
4450 /* Reading past eof, act as though there's a hole up to end. */
4452 got.br_startoff = end;
4453 if (got.br_startoff > bno) {
4454 /* Reading in a hole. */
4455 mval->br_startoff = bno;
4456 mval->br_startblock = HOLESTARTBLOCK;
4457 mval->br_blockcount =
4458 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4459 mval->br_state = XFS_EXT_NORM;
4460 bno += mval->br_blockcount;
4461 len -= mval->br_blockcount;
4467 /* set up the extent map to return. */
4468 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4469 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4471 /* If we're done, stop now. */
4472 if (bno >= end || n >= *nmap)
4475 /* Else go on to the next record. */
4476 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4477 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4486 xfs_bmapi_reserve_delalloc(
4487 struct xfs_inode *ip,
4490 struct xfs_bmbt_irec *got,
4491 struct xfs_bmbt_irec *prev,
4492 xfs_extnum_t *lastx,
4495 struct xfs_mount *mp = ip->i_mount;
4496 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4498 xfs_extlen_t indlen;
4499 char rt = XFS_IS_REALTIME_INODE(ip);
4503 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4505 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4507 /* Figure out the extent size, adjust alen */
4508 extsz = xfs_get_extsz_hint(ip);
4511 * Make sure we don't exceed a single extent length when we
4512 * align the extent by reducing length we are going to
4513 * allocate by the maximum amount extent size aligment may
4516 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1));
4517 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4518 1, 0, &aoff, &alen);
4523 extsz = alen / mp->m_sb.sb_rextsize;
4526 * Make a transaction-less quota reservation for delayed allocation
4527 * blocks. This number gets adjusted later. We return if we haven't
4528 * allocated blocks already inside this loop.
4530 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4531 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4536 * Split changing sb for alen and indlen since they could be coming
4537 * from different places.
4539 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4543 error = xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
4544 -((int64_t)extsz), 0);
4546 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4547 -((int64_t)alen), 0);
4551 goto out_unreserve_quota;
4553 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4554 -((int64_t)indlen), 0);
4556 goto out_unreserve_blocks;
4559 ip->i_delayed_blks += alen;
4561 got->br_startoff = aoff;
4562 got->br_startblock = nullstartblock(indlen);
4563 got->br_blockcount = alen;
4564 got->br_state = XFS_EXT_NORM;
4565 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
4568 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4569 * might have merged it into one of the neighbouring ones.
4571 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4573 ASSERT(got->br_startoff <= aoff);
4574 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4575 ASSERT(isnullstartblock(got->br_startblock));
4576 ASSERT(got->br_state == XFS_EXT_NORM);
4579 out_unreserve_blocks:
4581 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, extsz, 0);
4583 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, alen, 0);
4584 out_unreserve_quota:
4585 if (XFS_IS_QUOTA_ON(mp))
4586 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
4587 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4592 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4596 struct xfs_inode *ip, /* incore inode */
4597 xfs_fileoff_t bno, /* starting file offs. mapped */
4598 xfs_filblks_t len, /* length to map in file */
4599 struct xfs_bmbt_irec *mval, /* output: map values */
4600 int *nmap, /* i/o: mval size/count */
4601 int flags) /* XFS_BMAPI_... */
4603 struct xfs_mount *mp = ip->i_mount;
4604 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4605 struct xfs_bmbt_irec got; /* current file extent record */
4606 struct xfs_bmbt_irec prev; /* previous file extent record */
4607 xfs_fileoff_t obno; /* old block number (offset) */
4608 xfs_fileoff_t end; /* end of mapped file region */
4609 xfs_extnum_t lastx; /* last useful extent number */
4610 int eof; /* we've hit the end of extents */
4611 int n = 0; /* current extent index */
4615 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4616 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
4618 if (unlikely(XFS_TEST_ERROR(
4619 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4620 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4621 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4622 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
4623 return XFS_ERROR(EFSCORRUPTED);
4626 if (XFS_FORCED_SHUTDOWN(mp))
4627 return XFS_ERROR(EIO);
4629 XFS_STATS_INC(xs_blk_mapw);
4631 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4632 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4637 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4641 while (bno < end && n < *nmap) {
4642 if (eof || got.br_startoff > bno) {
4643 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4644 &prev, &lastx, eof);
4654 /* set up the extent map to return. */
4655 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4656 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4658 /* If we're done, stop now. */
4659 if (bno >= end || n >= *nmap)
4662 /* Else go on to the next record. */
4664 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4665 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4676 __xfs_bmapi_allocate(
4677 struct xfs_bmalloca *bma)
4679 struct xfs_mount *mp = bma->ip->i_mount;
4680 int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ?
4681 XFS_ATTR_FORK : XFS_DATA_FORK;
4682 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4683 int tmp_logflags = 0;
4687 ASSERT(bma->length > 0);
4689 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(bma->ip);
4692 * For the wasdelay case, we could also just allocate the stuff asked
4693 * for in this bmap call but that wouldn't be as good.
4696 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4697 bma->offset = bma->got.br_startoff;
4698 if (bma->idx != NULLEXTNUM && bma->idx) {
4699 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
4703 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
4705 bma->length = XFS_FILBLKS_MIN(bma->length,
4706 bma->got.br_startoff - bma->offset);
4710 * Indicate if this is the first user data in the file, or just any
4713 if (!(bma->flags & XFS_BMAPI_METADATA)) {
4714 bma->userdata = (bma->offset == 0) ?
4715 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
4718 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
4721 * Only want to do the alignment at the eof if it is userdata and
4722 * allocation length is larger than a stripe unit.
4724 if (mp->m_dalign && bma->length >= mp->m_dalign &&
4725 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
4726 error = xfs_bmap_isaeof(bma, whichfork);
4731 error = xfs_bmap_alloc(bma);
4735 if (bma->flist->xbf_low)
4738 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4739 if (bma->blkno == NULLFSBLOCK)
4741 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4742 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4743 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4744 bma->cur->bc_private.b.flist = bma->flist;
4747 * Bump the number of extents we've allocated
4753 bma->cur->bc_private.b.flags =
4754 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4756 bma->got.br_startoff = bma->offset;
4757 bma->got.br_startblock = bma->blkno;
4758 bma->got.br_blockcount = bma->length;
4759 bma->got.br_state = XFS_EXT_NORM;
4762 * A wasdelay extent has been initialized, so shouldn't be flagged
4765 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
4766 xfs_sb_version_hasextflgbit(&mp->m_sb))
4767 bma->got.br_state = XFS_EXT_UNWRITTEN;
4770 error = xfs_bmap_add_extent_delay_real(bma);
4772 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
4774 bma->logflags |= tmp_logflags;
4779 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4780 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4781 * the neighbouring ones.
4783 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
4785 ASSERT(bma->got.br_startoff <= bma->offset);
4786 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4787 bma->offset + bma->length);
4788 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4789 bma->got.br_state == XFS_EXT_UNWRITTEN);
4794 xfs_bmapi_allocate_worker(
4795 struct work_struct *work)
4797 struct xfs_bmalloca *args = container_of(work,
4798 struct xfs_bmalloca, work);
4799 unsigned long pflags;
4801 /* we are in a transaction context here */
4802 current_set_flags_nested(&pflags, PF_FSTRANS);
4804 args->result = __xfs_bmapi_allocate(args);
4805 complete(args->done);
4807 current_restore_flags_nested(&pflags, PF_FSTRANS);
4811 * Some allocation requests often come in with little stack to work on. Push
4812 * them off to a worker thread so there is lots of stack to use. Otherwise just
4813 * call directly to avoid the context switch overhead here.
4817 struct xfs_bmalloca *args)
4819 DECLARE_COMPLETION_ONSTACK(done);
4821 if (!args->stack_switch)
4822 return __xfs_bmapi_allocate(args);
4826 INIT_WORK_ONSTACK(&args->work, xfs_bmapi_allocate_worker);
4827 queue_work(xfs_alloc_wq, &args->work);
4828 wait_for_completion(&done);
4829 return args->result;
4833 xfs_bmapi_convert_unwritten(
4834 struct xfs_bmalloca *bma,
4835 struct xfs_bmbt_irec *mval,
4839 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4840 XFS_ATTR_FORK : XFS_DATA_FORK;
4841 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4842 int tmp_logflags = 0;
4845 /* check if we need to do unwritten->real conversion */
4846 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4847 (flags & XFS_BMAPI_PREALLOC))
4850 /* check if we need to do real->unwritten conversion */
4851 if (mval->br_state == XFS_EXT_NORM &&
4852 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4853 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4857 * Modify (by adding) the state flag, if writing.
4859 ASSERT(mval->br_blockcount <= len);
4860 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4861 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4862 bma->ip, whichfork);
4863 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4864 bma->cur->bc_private.b.flist = bma->flist;
4866 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4867 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4869 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
4870 &bma->cur, mval, bma->firstblock, bma->flist,
4872 bma->logflags |= tmp_logflags;
4877 * Update our extent pointer, given that
4878 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4879 * of the neighbouring ones.
4881 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
4884 * We may have combined previously unwritten space with written space,
4885 * so generate another request.
4887 if (mval->br_blockcount < len)
4893 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4894 * extent state if necessary. Details behaviour is controlled by the flags
4895 * parameter. Only allocates blocks from a single allocation group, to avoid
4898 * The returned value in "firstblock" from the first call in a transaction
4899 * must be remembered and presented to subsequent calls in "firstblock".
4900 * An upper bound for the number of blocks to be allocated is supplied to
4901 * the first call in "total"; if no allocation group has that many free
4902 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4906 struct xfs_trans *tp, /* transaction pointer */
4907 struct xfs_inode *ip, /* incore inode */
4908 xfs_fileoff_t bno, /* starting file offs. mapped */
4909 xfs_filblks_t len, /* length to map in file */
4910 int flags, /* XFS_BMAPI_... */
4911 xfs_fsblock_t *firstblock, /* first allocated block
4912 controls a.g. for allocs */
4913 xfs_extlen_t total, /* total blocks needed */
4914 struct xfs_bmbt_irec *mval, /* output: map values */
4915 int *nmap, /* i/o: mval size/count */
4916 struct xfs_bmap_free *flist) /* i/o: list extents to free */
4918 struct xfs_mount *mp = ip->i_mount;
4919 struct xfs_ifork *ifp;
4920 struct xfs_bmalloca bma = { 0 }; /* args for xfs_bmap_alloc */
4921 xfs_fileoff_t end; /* end of mapped file region */
4922 int eof; /* after the end of extents */
4923 int error; /* error return */
4924 int n; /* current extent index */
4925 xfs_fileoff_t obno; /* old block number (offset) */
4926 int whichfork; /* data or attr fork */
4927 char inhole; /* current location is hole in file */
4928 char wasdelay; /* old extent was delayed */
4931 xfs_fileoff_t orig_bno; /* original block number value */
4932 int orig_flags; /* original flags arg value */
4933 xfs_filblks_t orig_len; /* original value of len arg */
4934 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4935 int orig_nmap; /* original value of *nmap */
4945 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4946 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4950 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4951 XFS_ATTR_FORK : XFS_DATA_FORK;
4953 if (unlikely(XFS_TEST_ERROR(
4954 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4955 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4956 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4957 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4958 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
4959 return XFS_ERROR(EFSCORRUPTED);
4962 if (XFS_FORCED_SHUTDOWN(mp))
4963 return XFS_ERROR(EIO);
4965 ifp = XFS_IFORK_PTR(ip, whichfork);
4967 XFS_STATS_INC(xs_blk_mapw);
4969 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4971 * XXX (dgc): This assumes we are only called for inodes that
4972 * contain content neutral data in local format. Anything that
4973 * contains caller-specific data in local format that needs
4974 * transformation to move to a block format needs to do the
4975 * conversion to extent format itself.
4977 * Directory data forks and attribute forks handle this
4978 * themselves, but with the addition of metadata verifiers every
4979 * data fork in local format now contains caller specific data
4980 * and as such conversion through this function is likely to be
4983 * The only likely user of this branch is for remote symlinks,
4984 * but we cannot overwrite the data fork contents of the symlink
4985 * (EEXIST occurs higher up the stack) and so it will never go
4986 * from local format to extent format here. Hence I don't think
4987 * this branch is ever executed intentionally and we should
4988 * consider removing it and asserting that xfs_bmapi_write()
4989 * cannot be called directly on local format forks. i.e. callers
4990 * are completely responsible for local to extent format
4991 * conversion, not xfs_bmapi_write().
4993 error = xfs_bmap_local_to_extents(tp, ip, firstblock, total,
4994 &bma.logflags, whichfork,
4995 xfs_bmap_local_to_extents_init_fn);
5000 if (*firstblock == NULLFSBLOCK) {
5001 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
5002 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
5009 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5010 error = xfs_iread_extents(tp, ip, whichfork);
5015 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
5026 bma.firstblock = firstblock;
5028 if (flags & XFS_BMAPI_STACK_SWITCH)
5029 bma.stack_switch = 1;
5031 while (bno < end && n < *nmap) {
5032 inhole = eof || bma.got.br_startoff > bno;
5033 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
5036 * First, deal with the hole before the allocated space
5037 * that we found, if any.
5039 if (inhole || wasdelay) {
5041 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
5042 bma.wasdel = wasdelay;
5047 * There's a 32/64 bit type mismatch between the
5048 * allocation length request (which can be 64 bits in
5049 * length) and the bma length request, which is
5050 * xfs_extlen_t and therefore 32 bits. Hence we have to
5051 * check for 32-bit overflows and handle them here.
5053 if (len > (xfs_filblks_t)MAXEXTLEN)
5054 bma.length = MAXEXTLEN;
5059 ASSERT(bma.length > 0);
5060 error = xfs_bmapi_allocate(&bma);
5063 if (bma.blkno == NULLFSBLOCK)
5067 /* Deal with the allocated space we found. */
5068 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
5071 /* Execute unwritten extent conversion if necessary */
5072 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
5073 if (error == EAGAIN)
5078 /* update the extent map to return */
5079 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
5082 * If we're done, stop now. Stop when we've allocated
5083 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5084 * the transaction may get too big.
5086 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
5089 /* Else go on to the next record. */
5091 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
5092 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
5100 * Transform from btree to extents, give it cur.
5102 if (xfs_bmap_wants_extents(ip, whichfork)) {
5103 int tmp_logflags = 0;
5106 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
5107 &tmp_logflags, whichfork);
5108 bma.logflags |= tmp_logflags;
5113 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5114 XFS_IFORK_NEXTENTS(ip, whichfork) >
5115 XFS_IFORK_MAXEXT(ip, whichfork));
5119 * Log everything. Do this after conversion, there's no point in
5120 * logging the extent records if we've converted to btree format.
5122 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
5123 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5124 bma.logflags &= ~xfs_ilog_fext(whichfork);
5125 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
5126 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5127 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
5129 * Log whatever the flags say, even if error. Otherwise we might miss
5130 * detecting a case where the data is changed, there's an error,
5131 * and it's not logged so we don't shutdown when we should.
5134 xfs_trans_log_inode(tp, ip, bma.logflags);
5138 ASSERT(*firstblock == NULLFSBLOCK ||
5139 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5141 bma.cur->bc_private.b.firstblock) ||
5143 XFS_FSB_TO_AGNO(mp, *firstblock) <
5145 bma.cur->bc_private.b.firstblock)));
5146 *firstblock = bma.cur->bc_private.b.firstblock;
5148 xfs_btree_del_cursor(bma.cur,
5149 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5152 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5158 * Unmap (remove) blocks from a file.
5159 * If nexts is nonzero then the number of extents to remove is limited to
5160 * that value. If not all extents in the block range can be removed then
5165 xfs_trans_t *tp, /* transaction pointer */
5166 struct xfs_inode *ip, /* incore inode */
5167 xfs_fileoff_t bno, /* starting offset to unmap */
5168 xfs_filblks_t len, /* length to unmap in file */
5169 int flags, /* misc flags */
5170 xfs_extnum_t nexts, /* number of extents max */
5171 xfs_fsblock_t *firstblock, /* first allocated block
5172 controls a.g. for allocs */
5173 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5174 int *done) /* set if not done yet */
5176 xfs_btree_cur_t *cur; /* bmap btree cursor */
5177 xfs_bmbt_irec_t del; /* extent being deleted */
5178 int eof; /* is deleting at eof */
5179 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
5180 int error; /* error return value */
5181 xfs_extnum_t extno; /* extent number in list */
5182 xfs_bmbt_irec_t got; /* current extent record */
5183 xfs_ifork_t *ifp; /* inode fork pointer */
5184 int isrt; /* freeing in rt area */
5185 xfs_extnum_t lastx; /* last extent index used */
5186 int logflags; /* transaction logging flags */
5187 xfs_extlen_t mod; /* rt extent offset */
5188 xfs_mount_t *mp; /* mount structure */
5189 xfs_extnum_t nextents; /* number of file extents */
5190 xfs_bmbt_irec_t prev; /* previous extent record */
5191 xfs_fileoff_t start; /* first file offset deleted */
5192 int tmp_logflags; /* partial logging flags */
5193 int wasdel; /* was a delayed alloc extent */
5194 int whichfork; /* data or attribute fork */
5197 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5199 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5200 XFS_ATTR_FORK : XFS_DATA_FORK;
5201 ifp = XFS_IFORK_PTR(ip, whichfork);
5203 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5204 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5205 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5207 return XFS_ERROR(EFSCORRUPTED);
5210 if (XFS_FORCED_SHUTDOWN(mp))
5211 return XFS_ERROR(EIO);
5216 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5217 (error = xfs_iread_extents(tp, ip, whichfork)))
5219 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5220 if (nextents == 0) {
5224 XFS_STATS_INC(xs_blk_unmap);
5225 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5227 bno = start + len - 1;
5228 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5232 * Check to see if the given block number is past the end of the
5233 * file, back up to the last block if so...
5236 ep = xfs_iext_get_ext(ifp, --lastx);
5237 xfs_bmbt_get_all(ep, &got);
5238 bno = got.br_startoff + got.br_blockcount - 1;
5241 if (ifp->if_flags & XFS_IFBROOT) {
5242 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5243 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5244 cur->bc_private.b.firstblock = *firstblock;
5245 cur->bc_private.b.flist = flist;
5246 cur->bc_private.b.flags = 0;
5252 * Synchronize by locking the bitmap inode.
5254 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
5255 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5259 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5260 (nexts == 0 || extno < nexts)) {
5262 * Is the found extent after a hole in which bno lives?
5263 * Just back up to the previous extent, if so.
5265 if (got.br_startoff > bno) {
5268 ep = xfs_iext_get_ext(ifp, lastx);
5269 xfs_bmbt_get_all(ep, &got);
5272 * Is the last block of this extent before the range
5273 * we're supposed to delete? If so, we're done.
5275 bno = XFS_FILEOFF_MIN(bno,
5276 got.br_startoff + got.br_blockcount - 1);
5280 * Then deal with the (possibly delayed) allocated space
5285 wasdel = isnullstartblock(del.br_startblock);
5286 if (got.br_startoff < start) {
5287 del.br_startoff = start;
5288 del.br_blockcount -= start - got.br_startoff;
5290 del.br_startblock += start - got.br_startoff;
5292 if (del.br_startoff + del.br_blockcount > bno + 1)
5293 del.br_blockcount = bno + 1 - del.br_startoff;
5294 sum = del.br_startblock + del.br_blockcount;
5296 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5298 * Realtime extent not lined up at the end.
5299 * The extent could have been split into written
5300 * and unwritten pieces, or we could just be
5301 * unmapping part of it. But we can't really
5302 * get rid of part of a realtime extent.
5304 if (del.br_state == XFS_EXT_UNWRITTEN ||
5305 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5307 * This piece is unwritten, or we're not
5308 * using unwritten extents. Skip over it.
5311 bno -= mod > del.br_blockcount ?
5312 del.br_blockcount : mod;
5313 if (bno < got.br_startoff) {
5315 xfs_bmbt_get_all(xfs_iext_get_ext(
5321 * It's written, turn it unwritten.
5322 * This is better than zeroing it.
5324 ASSERT(del.br_state == XFS_EXT_NORM);
5325 ASSERT(xfs_trans_get_block_res(tp) > 0);
5327 * If this spans a realtime extent boundary,
5328 * chop it back to the start of the one we end at.
5330 if (del.br_blockcount > mod) {
5331 del.br_startoff += del.br_blockcount - mod;
5332 del.br_startblock += del.br_blockcount - mod;
5333 del.br_blockcount = mod;
5335 del.br_state = XFS_EXT_UNWRITTEN;
5336 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5337 &lastx, &cur, &del, firstblock, flist,
5343 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5345 * Realtime extent is lined up at the end but not
5346 * at the front. We'll get rid of full extents if
5349 mod = mp->m_sb.sb_rextsize - mod;
5350 if (del.br_blockcount > mod) {
5351 del.br_blockcount -= mod;
5352 del.br_startoff += mod;
5353 del.br_startblock += mod;
5354 } else if ((del.br_startoff == start &&
5355 (del.br_state == XFS_EXT_UNWRITTEN ||
5356 xfs_trans_get_block_res(tp) == 0)) ||
5357 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5359 * Can't make it unwritten. There isn't
5360 * a full extent here so just skip it.
5362 ASSERT(bno >= del.br_blockcount);
5363 bno -= del.br_blockcount;
5364 if (got.br_startoff > bno) {
5366 ep = xfs_iext_get_ext(ifp,
5368 xfs_bmbt_get_all(ep, &got);
5372 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5374 * This one is already unwritten.
5375 * It must have a written left neighbor.
5376 * Unwrite the killed part of that one and
5380 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5382 ASSERT(prev.br_state == XFS_EXT_NORM);
5383 ASSERT(!isnullstartblock(prev.br_startblock));
5384 ASSERT(del.br_startblock ==
5385 prev.br_startblock + prev.br_blockcount);
5386 if (prev.br_startoff < start) {
5387 mod = start - prev.br_startoff;
5388 prev.br_blockcount -= mod;
5389 prev.br_startblock += mod;
5390 prev.br_startoff = start;
5392 prev.br_state = XFS_EXT_UNWRITTEN;
5394 error = xfs_bmap_add_extent_unwritten_real(tp,
5395 ip, &lastx, &cur, &prev,
5396 firstblock, flist, &logflags);
5401 ASSERT(del.br_state == XFS_EXT_NORM);
5402 del.br_state = XFS_EXT_UNWRITTEN;
5403 error = xfs_bmap_add_extent_unwritten_real(tp,
5404 ip, &lastx, &cur, &del,
5405 firstblock, flist, &logflags);
5412 ASSERT(startblockval(del.br_startblock) > 0);
5413 /* Update realtime/data freespace, unreserve quota */
5415 xfs_filblks_t rtexts;
5417 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5418 do_div(rtexts, mp->m_sb.sb_rextsize);
5419 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5420 (int64_t)rtexts, 0);
5421 (void)xfs_trans_reserve_quota_nblks(NULL,
5422 ip, -((long)del.br_blockcount), 0,
5423 XFS_QMOPT_RES_RTBLKS);
5425 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
5426 (int64_t)del.br_blockcount, 0);
5427 (void)xfs_trans_reserve_quota_nblks(NULL,
5428 ip, -((long)del.br_blockcount), 0,
5429 XFS_QMOPT_RES_REGBLKS);
5431 ip->i_delayed_blks -= del.br_blockcount;
5433 cur->bc_private.b.flags |=
5434 XFS_BTCUR_BPRV_WASDEL;
5436 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5438 * If it's the case where the directory code is running
5439 * with no block reservation, and the deleted block is in
5440 * the middle of its extent, and the resulting insert
5441 * of an extent would cause transformation to btree format,
5442 * then reject it. The calling code will then swap
5443 * blocks around instead.
5444 * We have to do this now, rather than waiting for the
5445 * conversion to btree format, since the transaction
5448 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5449 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5450 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5451 XFS_IFORK_MAXEXT(ip, whichfork) &&
5452 del.br_startoff > got.br_startoff &&
5453 del.br_startoff + del.br_blockcount <
5454 got.br_startoff + got.br_blockcount) {
5455 error = XFS_ERROR(ENOSPC);
5458 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
5459 &tmp_logflags, whichfork);
5460 logflags |= tmp_logflags;
5463 bno = del.br_startoff - 1;
5466 * If not done go on to the next (previous) record.
5468 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5470 ep = xfs_iext_get_ext(ifp, lastx);
5471 if (xfs_bmbt_get_startoff(ep) > bno) {
5473 ep = xfs_iext_get_ext(ifp,
5476 xfs_bmbt_get_all(ep, &got);
5481 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5484 * Convert to a btree if necessary.
5486 if (xfs_bmap_needs_btree(ip, whichfork)) {
5487 ASSERT(cur == NULL);
5488 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5489 &cur, 0, &tmp_logflags, whichfork);
5490 logflags |= tmp_logflags;
5495 * transform from btree to extents, give it cur
5497 else if (xfs_bmap_wants_extents(ip, whichfork)) {
5498 ASSERT(cur != NULL);
5499 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5501 logflags |= tmp_logflags;
5506 * transform from extents to local?
5511 * Log everything. Do this after conversion, there's no point in
5512 * logging the extent records if we've converted to btree format.
5514 if ((logflags & xfs_ilog_fext(whichfork)) &&
5515 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5516 logflags &= ~xfs_ilog_fext(whichfork);
5517 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5518 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5519 logflags &= ~xfs_ilog_fbroot(whichfork);
5521 * Log inode even in the error case, if the transaction
5522 * is dirty we'll need to shut down the filesystem.
5525 xfs_trans_log_inode(tp, ip, logflags);
5528 *firstblock = cur->bc_private.b.firstblock;
5529 cur->bc_private.b.allocated = 0;
5531 xfs_btree_del_cursor(cur,
5532 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5538 * returns 1 for success, 0 if we failed to map the extent.
5541 xfs_getbmapx_fix_eof_hole(
5542 xfs_inode_t *ip, /* xfs incore inode pointer */
5543 struct getbmapx *out, /* output structure */
5544 int prealloced, /* this is a file with
5545 * preallocated data space */
5546 __int64_t end, /* last block requested */
5547 xfs_fsblock_t startblock)
5550 xfs_mount_t *mp; /* file system mount point */
5551 xfs_ifork_t *ifp; /* inode fork pointer */
5552 xfs_extnum_t lastx; /* last extent pointer */
5553 xfs_fileoff_t fileblock;
5555 if (startblock == HOLESTARTBLOCK) {
5557 out->bmv_block = -1;
5558 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
5559 fixlen -= out->bmv_offset;
5560 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5561 /* Came to hole at EOF. Trim it. */
5564 out->bmv_length = fixlen;
5567 if (startblock == DELAYSTARTBLOCK)
5568 out->bmv_block = -2;
5570 out->bmv_block = xfs_fsb_to_db(ip, startblock);
5571 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5572 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5573 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5574 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5575 out->bmv_oflags |= BMV_OF_LAST;
5582 * Get inode's extents as described in bmv, and format for output.
5583 * Calls formatter to fill the user's buffer until all extents
5584 * are mapped, until the passed-in bmv->bmv_count slots have
5585 * been filled, or until the formatter short-circuits the loop,
5586 * if it is tracking filled-in extents on its own.
5588 int /* error code */
5591 struct getbmapx *bmv, /* user bmap structure */
5592 xfs_bmap_format_t formatter, /* format to user */
5593 void *arg) /* formatter arg */
5595 __int64_t bmvend; /* last block requested */
5596 int error = 0; /* return value */
5597 __int64_t fixlen; /* length for -1 case */
5598 int i; /* extent number */
5599 int lock; /* lock state */
5600 xfs_bmbt_irec_t *map; /* buffer for user's data */
5601 xfs_mount_t *mp; /* file system mount point */
5602 int nex; /* # of user extents can do */
5603 int nexleft; /* # of user extents left */
5604 int subnex; /* # of bmapi's can do */
5605 int nmap; /* number of map entries */
5606 struct getbmapx *out; /* output structure */
5607 int whichfork; /* data or attr fork */
5608 int prealloced; /* this is a file with
5609 * preallocated data space */
5610 int iflags; /* interface flags */
5611 int bmapi_flags; /* flags for xfs_bmapi */
5615 iflags = bmv->bmv_iflags;
5616 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5618 if (whichfork == XFS_ATTR_FORK) {
5619 if (XFS_IFORK_Q(ip)) {
5620 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5621 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5622 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5623 return XFS_ERROR(EINVAL);
5624 } else if (unlikely(
5625 ip->i_d.di_aformat != 0 &&
5626 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5627 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5629 return XFS_ERROR(EFSCORRUPTED);
5635 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5636 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5637 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5638 return XFS_ERROR(EINVAL);
5640 if (xfs_get_extsz_hint(ip) ||
5641 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5643 fixlen = mp->m_super->s_maxbytes;
5646 fixlen = XFS_ISIZE(ip);
5650 if (bmv->bmv_length == -1) {
5651 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5653 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5654 } else if (bmv->bmv_length == 0) {
5655 bmv->bmv_entries = 0;
5657 } else if (bmv->bmv_length < 0) {
5658 return XFS_ERROR(EINVAL);
5661 nex = bmv->bmv_count - 1;
5663 return XFS_ERROR(EINVAL);
5664 bmvend = bmv->bmv_offset + bmv->bmv_length;
5667 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5668 return XFS_ERROR(ENOMEM);
5669 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5671 out = kmem_zalloc_large(bmv->bmv_count *
5672 sizeof(struct getbmapx));
5674 return XFS_ERROR(ENOMEM);
5677 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5678 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5679 if (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size) {
5680 error = -filemap_write_and_wait(VFS_I(ip)->i_mapping);
5682 goto out_unlock_iolock;
5685 * even after flushing the inode, there can still be delalloc
5686 * blocks on the inode beyond EOF due to speculative
5687 * preallocation. These are not removed until the release
5688 * function is called or the inode is inactivated. Hence we
5689 * cannot assert here that ip->i_delayed_blks == 0.
5693 lock = xfs_ilock_map_shared(ip);
5696 * Don't let nex be bigger than the number of extents
5697 * we can have assuming alternating holes and real extents.
5699 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5700 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5702 bmapi_flags = xfs_bmapi_aflag(whichfork);
5703 if (!(iflags & BMV_IF_PREALLOC))
5704 bmapi_flags |= XFS_BMAPI_IGSTATE;
5707 * Allocate enough space to handle "subnex" maps at a time.
5711 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
5713 goto out_unlock_ilock;
5715 bmv->bmv_entries = 0;
5717 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5718 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5726 nmap = (nexleft > subnex) ? subnex : nexleft;
5727 error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5728 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5729 map, &nmap, bmapi_flags);
5732 ASSERT(nmap <= subnex);
5734 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5735 out[cur_ext].bmv_oflags = 0;
5736 if (map[i].br_state == XFS_EXT_UNWRITTEN)
5737 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
5738 else if (map[i].br_startblock == DELAYSTARTBLOCK)
5739 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5740 out[cur_ext].bmv_offset =
5741 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5742 out[cur_ext].bmv_length =
5743 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5744 out[cur_ext].bmv_unused1 = 0;
5745 out[cur_ext].bmv_unused2 = 0;
5748 * delayed allocation extents that start beyond EOF can
5749 * occur due to speculative EOF allocation when the
5750 * delalloc extent is larger than the largest freespace
5751 * extent at conversion time. These extents cannot be
5752 * converted by data writeback, so can exist here even
5753 * if we are not supposed to be finding delalloc
5756 if (map[i].br_startblock == DELAYSTARTBLOCK &&
5757 map[i].br_startoff <= XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
5758 ASSERT((iflags & BMV_IF_DELALLOC) != 0);
5760 if (map[i].br_startblock == HOLESTARTBLOCK &&
5761 whichfork == XFS_ATTR_FORK) {
5762 /* came to the end of attribute fork */
5763 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
5767 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5769 map[i].br_startblock))
5773 out[cur_ext].bmv_offset +
5774 out[cur_ext].bmv_length;
5776 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
5779 * In case we don't want to return the hole,
5780 * don't increase cur_ext so that we can reuse
5781 * it in the next loop.
5783 if ((iflags & BMV_IF_NO_HOLES) &&
5784 map[i].br_startblock == HOLESTARTBLOCK) {
5785 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
5793 } while (nmap && nexleft && bmv->bmv_length);
5798 xfs_iunlock_map_shared(ip, lock);
5800 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5802 for (i = 0; i < cur_ext; i++) {
5803 int full = 0; /* user array is full */
5805 /* format results & advance arg */
5806 error = formatter(&arg, &out[i], &full);
5811 if (is_vmalloc_addr(out))
5812 kmem_free_large(out);
5819 STATIC struct xfs_buf *
5821 struct xfs_btree_cur *cur,
5824 struct xfs_log_item_desc *lidp;
5830 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5831 if (!cur->bc_bufs[i])
5833 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
5834 return cur->bc_bufs[i];
5837 /* Chase down all the log items to see if the bp is there */
5838 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
5839 struct xfs_buf_log_item *bip;
5840 bip = (struct xfs_buf_log_item *)lidp->lid_item;
5841 if (bip->bli_item.li_type == XFS_LI_BUF &&
5842 XFS_BUF_ADDR(bip->bli_buf) == bno)
5843 return bip->bli_buf;
5851 struct xfs_btree_block *block,
5857 __be64 *pp, *thispa; /* pointer to block address */
5858 xfs_bmbt_key_t *prevp, *keyp;
5860 ASSERT(be16_to_cpu(block->bb_level) > 0);
5863 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
5864 dmxr = mp->m_bmap_dmxr[0];
5865 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
5868 ASSERT(be64_to_cpu(prevp->br_startoff) <
5869 be64_to_cpu(keyp->br_startoff));
5874 * Compare the block numbers to see if there are dups.
5877 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
5879 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5881 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
5883 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
5885 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
5886 if (*thispa == *pp) {
5887 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
5889 (unsigned long long)be64_to_cpu(*thispa));
5890 panic("%s: ptrs are equal in node\n",
5898 * Check that the extents for the inode ip are in the right order in all
5903 xfs_bmap_check_leaf_extents(
5904 xfs_btree_cur_t *cur, /* btree cursor or null */
5905 xfs_inode_t *ip, /* incore inode pointer */
5906 int whichfork) /* data or attr fork */
5908 struct xfs_btree_block *block; /* current btree block */
5909 xfs_fsblock_t bno; /* block # of "block" */
5910 xfs_buf_t *bp; /* buffer for "block" */
5911 int error; /* error return value */
5912 xfs_extnum_t i=0, j; /* index into the extents list */
5913 xfs_ifork_t *ifp; /* fork structure */
5914 int level; /* btree level, for checking */
5915 xfs_mount_t *mp; /* file system mount structure */
5916 __be64 *pp; /* pointer to block address */
5917 xfs_bmbt_rec_t *ep; /* pointer to current extent */
5918 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
5919 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
5922 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5928 ifp = XFS_IFORK_PTR(ip, whichfork);
5929 block = ifp->if_broot;
5931 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5933 level = be16_to_cpu(block->bb_level);
5935 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
5936 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
5937 bno = be64_to_cpu(*pp);
5939 ASSERT(bno != NULLDFSBNO);
5940 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5941 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5944 * Go down the tree until leaf level is reached, following the first
5945 * pointer (leftmost) at each level.
5947 while (level-- > 0) {
5948 /* See if buf is in cur first */
5950 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5953 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5959 block = XFS_BUF_TO_BLOCK(bp);
5960 XFS_WANT_CORRUPTED_GOTO(
5961 xfs_bmap_sanity_check(mp, bp, level),
5967 * Check this block for basic sanity (increasing keys and
5968 * no duplicate blocks).
5971 xfs_check_block(block, mp, 0, 0);
5972 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
5973 bno = be64_to_cpu(*pp);
5974 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
5977 xfs_trans_brelse(NULL, bp);
5982 * Here with bp and block set to the leftmost leaf node in the tree.
5987 * Loop over all leaf nodes checking that all extents are in the right order.
5990 xfs_fsblock_t nextbno;
5991 xfs_extnum_t num_recs;
5994 num_recs = xfs_btree_get_numrecs(block);
5997 * Read-ahead the next leaf block, if any.
6000 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6003 * Check all the extents to make sure they are OK.
6004 * If we had a previous block, the last entry should
6005 * conform with the first entry in this one.
6008 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
6010 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
6011 xfs_bmbt_disk_get_blockcount(&last) <=
6012 xfs_bmbt_disk_get_startoff(ep));
6014 for (j = 1; j < num_recs; j++) {
6015 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
6016 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
6017 xfs_bmbt_disk_get_blockcount(ep) <=
6018 xfs_bmbt_disk_get_startoff(nextp));
6026 xfs_trans_brelse(NULL, bp);
6030 * If we've reached the end, stop.
6032 if (bno == NULLFSBLOCK)
6036 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6039 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6045 block = XFS_BUF_TO_BLOCK(bp);
6049 xfs_trans_brelse(NULL, bp);
6054 xfs_warn(mp, "%s: at error0", __func__);
6056 xfs_trans_brelse(NULL, bp);
6058 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
6060 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
6066 * Count fsblocks of the given fork.
6069 xfs_bmap_count_blocks(
6070 xfs_trans_t *tp, /* transaction pointer */
6071 xfs_inode_t *ip, /* incore inode */
6072 int whichfork, /* data or attr fork */
6073 int *count) /* out: count of blocks */
6075 struct xfs_btree_block *block; /* current btree block */
6076 xfs_fsblock_t bno; /* block # of "block" */
6077 xfs_ifork_t *ifp; /* fork structure */
6078 int level; /* btree level, for checking */
6079 xfs_mount_t *mp; /* file system mount structure */
6080 __be64 *pp; /* pointer to block address */
6084 ifp = XFS_IFORK_PTR(ip, whichfork);
6085 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
6086 xfs_bmap_count_leaves(ifp, 0,
6087 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6093 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6095 block = ifp->if_broot;
6096 level = be16_to_cpu(block->bb_level);
6098 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
6099 bno = be64_to_cpu(*pp);
6100 ASSERT(bno != NULLDFSBNO);
6101 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6102 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6104 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
6105 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6107 return XFS_ERROR(EFSCORRUPTED);
6114 * Recursively walks each level of a btree
6115 * to count total fsblocks is use.
6117 STATIC int /* error */
6118 xfs_bmap_count_tree(
6119 xfs_mount_t *mp, /* file system mount point */
6120 xfs_trans_t *tp, /* transaction pointer */
6121 xfs_ifork_t *ifp, /* inode fork pointer */
6122 xfs_fsblock_t blockno, /* file system block number */
6123 int levelin, /* level in btree */
6124 int *count) /* Count of blocks */
6127 xfs_buf_t *bp, *nbp;
6128 int level = levelin;
6130 xfs_fsblock_t bno = blockno;
6131 xfs_fsblock_t nextbno;
6132 struct xfs_btree_block *block, *nextblock;
6135 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF,
6140 block = XFS_BUF_TO_BLOCK(bp);
6143 /* Not at node above leaves, count this level of nodes */
6144 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6145 while (nextbno != NULLFSBLOCK) {
6146 error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp,
6152 nextblock = XFS_BUF_TO_BLOCK(nbp);
6153 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
6154 xfs_trans_brelse(tp, nbp);
6157 /* Dive to the next level */
6158 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
6159 bno = be64_to_cpu(*pp);
6160 if (unlikely((error =
6161 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
6162 xfs_trans_brelse(tp, bp);
6163 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6164 XFS_ERRLEVEL_LOW, mp);
6165 return XFS_ERROR(EFSCORRUPTED);
6167 xfs_trans_brelse(tp, bp);
6169 /* count all level 1 nodes and their leaves */
6171 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6172 numrecs = be16_to_cpu(block->bb_numrecs);
6173 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
6174 xfs_trans_brelse(tp, bp);
6175 if (nextbno == NULLFSBLOCK)
6178 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6184 block = XFS_BUF_TO_BLOCK(bp);
6191 * Count leaf blocks given a range of extent records.
6194 xfs_bmap_count_leaves(
6202 for (b = 0; b < numrecs; b++) {
6203 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
6204 *count += xfs_bmbt_get_blockcount(frp);
6209 * Count leaf blocks given a range of extent records originally
6213 xfs_bmap_disk_count_leaves(
6214 struct xfs_mount *mp,
6215 struct xfs_btree_block *block,
6220 xfs_bmbt_rec_t *frp;
6222 for (b = 1; b <= numrecs; b++) {
6223 frp = XFS_BMBT_REC_ADDR(mp, block, b);
6224 *count += xfs_bmbt_disk_get_blockcount(frp);
6229 * dead simple method of punching delalyed allocation blocks from a range in
6230 * the inode. Walks a block at a time so will be slow, but is only executed in
6231 * rare error cases so the overhead is not critical. This will alays punch out
6232 * both the start and end blocks, even if the ranges only partially overlap
6233 * them, so it is up to the caller to ensure that partial blocks are not
6237 xfs_bmap_punch_delalloc_range(
6238 struct xfs_inode *ip,
6239 xfs_fileoff_t start_fsb,
6240 xfs_fileoff_t length)
6242 xfs_fileoff_t remaining = length;
6245 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6249 xfs_bmbt_irec_t imap;
6251 xfs_fsblock_t firstblock;
6252 xfs_bmap_free_t flist;
6255 * Map the range first and check that it is a delalloc extent
6256 * before trying to unmap the range. Otherwise we will be
6257 * trying to remove a real extent (which requires a
6258 * transaction) or a hole, which is probably a bad idea...
6260 error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
6264 /* something screwed, just bail */
6265 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
6266 xfs_alert(ip->i_mount,
6267 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6268 ip->i_ino, start_fsb);
6276 if (imap.br_startblock != DELAYSTARTBLOCK) {
6277 /* been converted, ignore */
6280 WARN_ON(imap.br_blockcount == 0);
6283 * Note: while we initialise the firstblock/flist pair, they
6284 * should never be used because blocks should never be
6285 * allocated or freed for a delalloc extent and hence we need
6286 * don't cancel or finish them after the xfs_bunmapi() call.
6288 xfs_bmap_init(&flist, &firstblock);
6289 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6294 ASSERT(!flist.xbf_count && !flist.xbf_first);
6298 } while(remaining > 0);
6304 * Convert the given file system block to a disk block. We have to treat it
6305 * differently based on whether the file is a real time file or not, because the
6309 xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
6311 return (XFS_IS_REALTIME_INODE(ip) ? \
6312 (xfs_daddr_t)XFS_FSB_TO_BB((ip)->i_mount, (fsb)) : \
6313 XFS_FSB_TO_DADDR((ip)->i_mount, (fsb)));