arm64: kgdb: Fix single-step exception handling oops
[linux/fpc-iii.git] / fs / xfs / libxfs / xfs_bmap.c
blob9ca8809ee3d0cf216e3c03d46bd1b3f54e97b039
1 /*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * All Rights Reserved.
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
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_bit.h"
25 #include "xfs_sb.h"
26 #include "xfs_mount.h"
27 #include "xfs_defer.h"
28 #include "xfs_da_format.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_dir2.h"
31 #include "xfs_inode.h"
32 #include "xfs_btree.h"
33 #include "xfs_trans.h"
34 #include "xfs_inode_item.h"
35 #include "xfs_extfree_item.h"
36 #include "xfs_alloc.h"
37 #include "xfs_bmap.h"
38 #include "xfs_bmap_util.h"
39 #include "xfs_bmap_btree.h"
40 #include "xfs_rtalloc.h"
41 #include "xfs_error.h"
42 #include "xfs_quota.h"
43 #include "xfs_trans_space.h"
44 #include "xfs_buf_item.h"
45 #include "xfs_trace.h"
46 #include "xfs_symlink.h"
47 #include "xfs_attr_leaf.h"
48 #include "xfs_filestream.h"
49 #include "xfs_rmap.h"
50 #include "xfs_ag_resv.h"
51 #include "xfs_refcount.h"
52 #include "xfs_rmap_btree.h"
53 #include "xfs_icache.h"
56 kmem_zone_t *xfs_bmap_free_item_zone;
59 * Miscellaneous helper functions
63 * Compute and fill in the value of the maximum depth of a bmap btree
64 * in this filesystem. Done once, during mount.
66 void
67 xfs_bmap_compute_maxlevels(
68 xfs_mount_t *mp, /* file system mount structure */
69 int whichfork) /* data or attr fork */
71 int level; /* btree level */
72 uint maxblocks; /* max blocks at this level */
73 uint maxleafents; /* max leaf entries possible */
74 int maxrootrecs; /* max records in root block */
75 int minleafrecs; /* min records in leaf block */
76 int minnoderecs; /* min records in node block */
77 int sz; /* root block size */
80 * The maximum number of extents in a file, hence the maximum
81 * number of leaf entries, is controlled by the type of di_nextents
82 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
83 * (a signed 16-bit number, xfs_aextnum_t).
85 * Note that we can no longer assume that if we are in ATTR1 that
86 * the fork offset of all the inodes will be
87 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
88 * with ATTR2 and then mounted back with ATTR1, keeping the
89 * di_forkoff's fixed but probably at various positions. Therefore,
90 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
91 * of a minimum size available.
93 if (whichfork == XFS_DATA_FORK) {
94 maxleafents = MAXEXTNUM;
95 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
96 } else {
97 maxleafents = MAXAEXTNUM;
98 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
100 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
101 minleafrecs = mp->m_bmap_dmnr[0];
102 minnoderecs = mp->m_bmap_dmnr[1];
103 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
104 for (level = 1; maxblocks > 1; level++) {
105 if (maxblocks <= maxrootrecs)
106 maxblocks = 1;
107 else
108 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
110 mp->m_bm_maxlevels[whichfork] = level;
113 STATIC int /* error */
114 xfs_bmbt_lookup_eq(
115 struct xfs_btree_cur *cur,
116 xfs_fileoff_t off,
117 xfs_fsblock_t bno,
118 xfs_filblks_t len,
119 int *stat) /* success/failure */
121 cur->bc_rec.b.br_startoff = off;
122 cur->bc_rec.b.br_startblock = bno;
123 cur->bc_rec.b.br_blockcount = len;
124 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
127 STATIC int /* error */
128 xfs_bmbt_lookup_ge(
129 struct xfs_btree_cur *cur,
130 xfs_fileoff_t off,
131 xfs_fsblock_t bno,
132 xfs_filblks_t len,
133 int *stat) /* success/failure */
135 cur->bc_rec.b.br_startoff = off;
136 cur->bc_rec.b.br_startblock = bno;
137 cur->bc_rec.b.br_blockcount = len;
138 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
142 * Check if the inode needs to be converted to btree format.
144 static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
146 return whichfork != XFS_COW_FORK &&
147 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
148 XFS_IFORK_NEXTENTS(ip, whichfork) >
149 XFS_IFORK_MAXEXT(ip, whichfork);
153 * Check if the inode should be converted to extent format.
155 static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
157 return whichfork != XFS_COW_FORK &&
158 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
159 XFS_IFORK_NEXTENTS(ip, whichfork) <=
160 XFS_IFORK_MAXEXT(ip, whichfork);
164 * Update the record referred to by cur to the value given
165 * by [off, bno, len, state].
166 * This either works (return 0) or gets an EFSCORRUPTED error.
168 STATIC int
169 xfs_bmbt_update(
170 struct xfs_btree_cur *cur,
171 xfs_fileoff_t off,
172 xfs_fsblock_t bno,
173 xfs_filblks_t len,
174 xfs_exntst_t state)
176 union xfs_btree_rec rec;
178 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
179 return xfs_btree_update(cur, &rec);
183 * Compute the worst-case number of indirect blocks that will be used
184 * for ip's delayed extent of length "len".
186 STATIC xfs_filblks_t
187 xfs_bmap_worst_indlen(
188 xfs_inode_t *ip, /* incore inode pointer */
189 xfs_filblks_t len) /* delayed extent length */
191 int level; /* btree level number */
192 int maxrecs; /* maximum record count at this level */
193 xfs_mount_t *mp; /* mount structure */
194 xfs_filblks_t rval; /* return value */
195 xfs_filblks_t orig_len;
197 mp = ip->i_mount;
199 /* Calculate the worst-case size of the bmbt. */
200 orig_len = len;
201 maxrecs = mp->m_bmap_dmxr[0];
202 for (level = 0, rval = 0;
203 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
204 level++) {
205 len += maxrecs - 1;
206 do_div(len, maxrecs);
207 rval += len;
208 if (len == 1) {
209 rval += XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
210 level - 1;
211 break;
213 if (level == 0)
214 maxrecs = mp->m_bmap_dmxr[1];
217 /* Calculate the worst-case size of the rmapbt. */
218 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
219 rval += 1 + xfs_rmapbt_calc_size(mp, orig_len) +
220 mp->m_rmap_maxlevels;
222 return rval;
226 * Calculate the default attribute fork offset for newly created inodes.
228 uint
229 xfs_default_attroffset(
230 struct xfs_inode *ip)
232 struct xfs_mount *mp = ip->i_mount;
233 uint offset;
235 if (mp->m_sb.sb_inodesize == 256) {
236 offset = XFS_LITINO(mp, ip->i_d.di_version) -
237 XFS_BMDR_SPACE_CALC(MINABTPTRS);
238 } else {
239 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
242 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
243 return offset;
247 * Helper routine to reset inode di_forkoff field when switching
248 * attribute fork from local to extent format - we reset it where
249 * possible to make space available for inline data fork extents.
251 STATIC void
252 xfs_bmap_forkoff_reset(
253 xfs_inode_t *ip,
254 int whichfork)
256 if (whichfork == XFS_ATTR_FORK &&
257 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
258 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
259 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
260 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
262 if (dfl_forkoff > ip->i_d.di_forkoff)
263 ip->i_d.di_forkoff = dfl_forkoff;
267 #ifdef DEBUG
268 STATIC struct xfs_buf *
269 xfs_bmap_get_bp(
270 struct xfs_btree_cur *cur,
271 xfs_fsblock_t bno)
273 struct xfs_log_item_desc *lidp;
274 int i;
276 if (!cur)
277 return NULL;
279 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
280 if (!cur->bc_bufs[i])
281 break;
282 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
283 return cur->bc_bufs[i];
286 /* Chase down all the log items to see if the bp is there */
287 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
288 struct xfs_buf_log_item *bip;
289 bip = (struct xfs_buf_log_item *)lidp->lid_item;
290 if (bip->bli_item.li_type == XFS_LI_BUF &&
291 XFS_BUF_ADDR(bip->bli_buf) == bno)
292 return bip->bli_buf;
295 return NULL;
298 STATIC void
299 xfs_check_block(
300 struct xfs_btree_block *block,
301 xfs_mount_t *mp,
302 int root,
303 short sz)
305 int i, j, dmxr;
306 __be64 *pp, *thispa; /* pointer to block address */
307 xfs_bmbt_key_t *prevp, *keyp;
309 ASSERT(be16_to_cpu(block->bb_level) > 0);
311 prevp = NULL;
312 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
313 dmxr = mp->m_bmap_dmxr[0];
314 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
316 if (prevp) {
317 ASSERT(be64_to_cpu(prevp->br_startoff) <
318 be64_to_cpu(keyp->br_startoff));
320 prevp = keyp;
323 * Compare the block numbers to see if there are dups.
325 if (root)
326 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
327 else
328 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
330 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
331 if (root)
332 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
333 else
334 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
335 if (*thispa == *pp) {
336 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
337 __func__, j, i,
338 (unsigned long long)be64_to_cpu(*thispa));
339 panic("%s: ptrs are equal in node\n",
340 __func__);
347 * Check that the extents for the inode ip are in the right order in all
348 * btree leaves. THis becomes prohibitively expensive for large extent count
349 * files, so don't bother with inodes that have more than 10,000 extents in
350 * them. The btree record ordering checks will still be done, so for such large
351 * bmapbt constructs that is going to catch most corruptions.
353 STATIC void
354 xfs_bmap_check_leaf_extents(
355 xfs_btree_cur_t *cur, /* btree cursor or null */
356 xfs_inode_t *ip, /* incore inode pointer */
357 int whichfork) /* data or attr fork */
359 struct xfs_btree_block *block; /* current btree block */
360 xfs_fsblock_t bno; /* block # of "block" */
361 xfs_buf_t *bp; /* buffer for "block" */
362 int error; /* error return value */
363 xfs_extnum_t i=0, j; /* index into the extents list */
364 xfs_ifork_t *ifp; /* fork structure */
365 int level; /* btree level, for checking */
366 xfs_mount_t *mp; /* file system mount structure */
367 __be64 *pp; /* pointer to block address */
368 xfs_bmbt_rec_t *ep; /* pointer to current extent */
369 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
370 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
371 int bp_release = 0;
373 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
374 return;
377 /* skip large extent count inodes */
378 if (ip->i_d.di_nextents > 10000)
379 return;
381 bno = NULLFSBLOCK;
382 mp = ip->i_mount;
383 ifp = XFS_IFORK_PTR(ip, whichfork);
384 block = ifp->if_broot;
386 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
388 level = be16_to_cpu(block->bb_level);
389 ASSERT(level > 0);
390 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
391 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
392 bno = be64_to_cpu(*pp);
394 ASSERT(bno != NULLFSBLOCK);
395 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
396 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
399 * Go down the tree until leaf level is reached, following the first
400 * pointer (leftmost) at each level.
402 while (level-- > 0) {
403 /* See if buf is in cur first */
404 bp_release = 0;
405 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
406 if (!bp) {
407 bp_release = 1;
408 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
409 XFS_BMAP_BTREE_REF,
410 &xfs_bmbt_buf_ops);
411 if (error)
412 goto error_norelse;
414 block = XFS_BUF_TO_BLOCK(bp);
415 if (level == 0)
416 break;
419 * Check this block for basic sanity (increasing keys and
420 * no duplicate blocks).
423 xfs_check_block(block, mp, 0, 0);
424 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
425 bno = be64_to_cpu(*pp);
426 XFS_WANT_CORRUPTED_GOTO(mp,
427 XFS_FSB_SANITY_CHECK(mp, bno), error0);
428 if (bp_release) {
429 bp_release = 0;
430 xfs_trans_brelse(NULL, bp);
435 * Here with bp and block set to the leftmost leaf node in the tree.
437 i = 0;
440 * Loop over all leaf nodes checking that all extents are in the right order.
442 for (;;) {
443 xfs_fsblock_t nextbno;
444 xfs_extnum_t num_recs;
447 num_recs = xfs_btree_get_numrecs(block);
450 * Read-ahead the next leaf block, if any.
453 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
456 * Check all the extents to make sure they are OK.
457 * If we had a previous block, the last entry should
458 * conform with the first entry in this one.
461 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
462 if (i) {
463 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
464 xfs_bmbt_disk_get_blockcount(&last) <=
465 xfs_bmbt_disk_get_startoff(ep));
467 for (j = 1; j < num_recs; j++) {
468 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
469 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
470 xfs_bmbt_disk_get_blockcount(ep) <=
471 xfs_bmbt_disk_get_startoff(nextp));
472 ep = nextp;
475 last = *ep;
476 i += num_recs;
477 if (bp_release) {
478 bp_release = 0;
479 xfs_trans_brelse(NULL, bp);
481 bno = nextbno;
483 * If we've reached the end, stop.
485 if (bno == NULLFSBLOCK)
486 break;
488 bp_release = 0;
489 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
490 if (!bp) {
491 bp_release = 1;
492 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
493 XFS_BMAP_BTREE_REF,
494 &xfs_bmbt_buf_ops);
495 if (error)
496 goto error_norelse;
498 block = XFS_BUF_TO_BLOCK(bp);
501 return;
503 error0:
504 xfs_warn(mp, "%s: at error0", __func__);
505 if (bp_release)
506 xfs_trans_brelse(NULL, bp);
507 error_norelse:
508 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
509 __func__, i);
510 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
511 return;
515 * Add bmap trace insert entries for all the contents of the extent records.
517 void
518 xfs_bmap_trace_exlist(
519 xfs_inode_t *ip, /* incore inode pointer */
520 xfs_extnum_t cnt, /* count of entries in the list */
521 int whichfork, /* data or attr or cow fork */
522 unsigned long caller_ip)
524 xfs_extnum_t idx; /* extent record index */
525 xfs_ifork_t *ifp; /* inode fork pointer */
526 int state = 0;
528 if (whichfork == XFS_ATTR_FORK)
529 state |= BMAP_ATTRFORK;
530 else if (whichfork == XFS_COW_FORK)
531 state |= BMAP_COWFORK;
533 ifp = XFS_IFORK_PTR(ip, whichfork);
534 ASSERT(cnt == xfs_iext_count(ifp));
535 for (idx = 0; idx < cnt; idx++)
536 trace_xfs_extlist(ip, idx, state, caller_ip);
540 * Validate that the bmbt_irecs being returned from bmapi are valid
541 * given the caller's original parameters. Specifically check the
542 * ranges of the returned irecs to ensure that they only extend beyond
543 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
545 STATIC void
546 xfs_bmap_validate_ret(
547 xfs_fileoff_t bno,
548 xfs_filblks_t len,
549 int flags,
550 xfs_bmbt_irec_t *mval,
551 int nmap,
552 int ret_nmap)
554 int i; /* index to map values */
556 ASSERT(ret_nmap <= nmap);
558 for (i = 0; i < ret_nmap; i++) {
559 ASSERT(mval[i].br_blockcount > 0);
560 if (!(flags & XFS_BMAPI_ENTIRE)) {
561 ASSERT(mval[i].br_startoff >= bno);
562 ASSERT(mval[i].br_blockcount <= len);
563 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
564 bno + len);
565 } else {
566 ASSERT(mval[i].br_startoff < bno + len);
567 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
568 bno);
570 ASSERT(i == 0 ||
571 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
572 mval[i].br_startoff);
573 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
574 mval[i].br_startblock != HOLESTARTBLOCK);
575 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
576 mval[i].br_state == XFS_EXT_UNWRITTEN);
580 #else
581 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
582 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
583 #endif /* DEBUG */
586 * bmap free list manipulation functions
590 * Add the extent to the list of extents to be free at transaction end.
591 * The list is maintained sorted (by block number).
593 void
594 xfs_bmap_add_free(
595 struct xfs_mount *mp,
596 struct xfs_defer_ops *dfops,
597 xfs_fsblock_t bno,
598 xfs_filblks_t len,
599 struct xfs_owner_info *oinfo)
601 struct xfs_extent_free_item *new; /* new element */
602 #ifdef DEBUG
603 xfs_agnumber_t agno;
604 xfs_agblock_t agbno;
606 ASSERT(bno != NULLFSBLOCK);
607 ASSERT(len > 0);
608 ASSERT(len <= MAXEXTLEN);
609 ASSERT(!isnullstartblock(bno));
610 agno = XFS_FSB_TO_AGNO(mp, bno);
611 agbno = XFS_FSB_TO_AGBNO(mp, bno);
612 ASSERT(agno < mp->m_sb.sb_agcount);
613 ASSERT(agbno < mp->m_sb.sb_agblocks);
614 ASSERT(len < mp->m_sb.sb_agblocks);
615 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
616 #endif
617 ASSERT(xfs_bmap_free_item_zone != NULL);
619 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
620 new->xefi_startblock = bno;
621 new->xefi_blockcount = (xfs_extlen_t)len;
622 if (oinfo)
623 new->xefi_oinfo = *oinfo;
624 else
625 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
626 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
627 XFS_FSB_TO_AGBNO(mp, bno), len);
628 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
632 * Inode fork format manipulation functions
636 * Transform a btree format file with only one leaf node, where the
637 * extents list will fit in the inode, into an extents format file.
638 * Since the file extents are already in-core, all we have to do is
639 * give up the space for the btree root and pitch the leaf block.
641 STATIC int /* error */
642 xfs_bmap_btree_to_extents(
643 xfs_trans_t *tp, /* transaction pointer */
644 xfs_inode_t *ip, /* incore inode pointer */
645 xfs_btree_cur_t *cur, /* btree cursor */
646 int *logflagsp, /* inode logging flags */
647 int whichfork) /* data or attr fork */
649 /* REFERENCED */
650 struct xfs_btree_block *cblock;/* child btree block */
651 xfs_fsblock_t cbno; /* child block number */
652 xfs_buf_t *cbp; /* child block's buffer */
653 int error; /* error return value */
654 xfs_ifork_t *ifp; /* inode fork data */
655 xfs_mount_t *mp; /* mount point structure */
656 __be64 *pp; /* ptr to block address */
657 struct xfs_btree_block *rblock;/* root btree block */
658 struct xfs_owner_info oinfo;
660 mp = ip->i_mount;
661 ifp = XFS_IFORK_PTR(ip, whichfork);
662 ASSERT(whichfork != XFS_COW_FORK);
663 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
664 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
665 rblock = ifp->if_broot;
666 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
667 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
668 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
669 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
670 cbno = be64_to_cpu(*pp);
671 *logflagsp = 0;
672 #ifdef DEBUG
673 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
674 return error;
675 #endif
676 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
677 &xfs_bmbt_buf_ops);
678 if (error)
679 return error;
680 cblock = XFS_BUF_TO_BLOCK(cbp);
681 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
682 return error;
683 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
684 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
685 ip->i_d.di_nblocks--;
686 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
687 xfs_trans_binval(tp, cbp);
688 if (cur->bc_bufs[0] == cbp)
689 cur->bc_bufs[0] = NULL;
690 xfs_iroot_realloc(ip, -1, whichfork);
691 ASSERT(ifp->if_broot == NULL);
692 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
693 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
694 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
695 return 0;
699 * Convert an extents-format file into a btree-format file.
700 * The new file will have a root block (in the inode) and a single child block.
702 STATIC int /* error */
703 xfs_bmap_extents_to_btree(
704 xfs_trans_t *tp, /* transaction pointer */
705 xfs_inode_t *ip, /* incore inode pointer */
706 xfs_fsblock_t *firstblock, /* first-block-allocated */
707 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
708 xfs_btree_cur_t **curp, /* cursor returned to caller */
709 int wasdel, /* converting a delayed alloc */
710 int *logflagsp, /* inode logging flags */
711 int whichfork) /* data or attr fork */
713 struct xfs_btree_block *ablock; /* allocated (child) bt block */
714 xfs_buf_t *abp; /* buffer for ablock */
715 xfs_alloc_arg_t args; /* allocation arguments */
716 xfs_bmbt_rec_t *arp; /* child record pointer */
717 struct xfs_btree_block *block; /* btree root block */
718 xfs_btree_cur_t *cur; /* bmap btree cursor */
719 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
720 int error; /* error return value */
721 xfs_extnum_t i, cnt; /* extent record index */
722 xfs_ifork_t *ifp; /* inode fork pointer */
723 xfs_bmbt_key_t *kp; /* root block key pointer */
724 xfs_mount_t *mp; /* mount structure */
725 xfs_extnum_t nextents; /* number of file extents */
726 xfs_bmbt_ptr_t *pp; /* root block address pointer */
728 mp = ip->i_mount;
729 ASSERT(whichfork != XFS_COW_FORK);
730 ifp = XFS_IFORK_PTR(ip, whichfork);
731 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
734 * Make space in the inode incore.
736 xfs_iroot_realloc(ip, 1, whichfork);
737 ifp->if_flags |= XFS_IFBROOT;
740 * Fill in the root.
742 block = ifp->if_broot;
743 if (xfs_sb_version_hascrc(&mp->m_sb))
744 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
745 XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino,
746 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
747 else
748 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
749 XFS_BMAP_MAGIC, 1, 1, ip->i_ino,
750 XFS_BTREE_LONG_PTRS);
753 * Need a cursor. Can't allocate until bb_level is filled in.
755 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
756 cur->bc_private.b.firstblock = *firstblock;
757 cur->bc_private.b.dfops = dfops;
758 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
760 * Convert to a btree with two levels, one record in root.
762 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
763 memset(&args, 0, sizeof(args));
764 args.tp = tp;
765 args.mp = mp;
766 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
767 args.firstblock = *firstblock;
768 if (*firstblock == NULLFSBLOCK) {
769 args.type = XFS_ALLOCTYPE_START_BNO;
770 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
771 } else if (dfops->dop_low) {
772 args.type = XFS_ALLOCTYPE_START_BNO;
773 try_another_ag:
774 args.fsbno = *firstblock;
775 } else {
776 args.type = XFS_ALLOCTYPE_NEAR_BNO;
777 args.fsbno = *firstblock;
779 args.minlen = args.maxlen = args.prod = 1;
780 args.wasdel = wasdel;
781 *logflagsp = 0;
782 if ((error = xfs_alloc_vextent(&args))) {
783 xfs_iroot_realloc(ip, -1, whichfork);
784 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
785 return error;
789 * During a CoW operation, the allocation and bmbt updates occur in
790 * different transactions. The mapping code tries to put new bmbt
791 * blocks near extents being mapped, but the only way to guarantee this
792 * is if the alloc and the mapping happen in a single transaction that
793 * has a block reservation. That isn't the case here, so if we run out
794 * of space we'll try again with another AG.
796 if (xfs_sb_version_hasreflink(&cur->bc_mp->m_sb) &&
797 args.fsbno == NULLFSBLOCK &&
798 args.type == XFS_ALLOCTYPE_NEAR_BNO) {
799 args.type = XFS_ALLOCTYPE_FIRST_AG;
800 goto try_another_ag;
802 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
803 xfs_iroot_realloc(ip, -1, whichfork);
804 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
805 return -ENOSPC;
808 * Allocation can't fail, the space was reserved.
810 ASSERT(*firstblock == NULLFSBLOCK ||
811 args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock));
812 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
813 cur->bc_private.b.allocated++;
814 ip->i_d.di_nblocks++;
815 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
816 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
818 * Fill in the child block.
820 abp->b_ops = &xfs_bmbt_buf_ops;
821 ablock = XFS_BUF_TO_BLOCK(abp);
822 if (xfs_sb_version_hascrc(&mp->m_sb))
823 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
824 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
825 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
826 else
827 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
828 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
829 XFS_BTREE_LONG_PTRS);
831 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
832 nextents = xfs_iext_count(ifp);
833 for (cnt = i = 0; i < nextents; i++) {
834 ep = xfs_iext_get_ext(ifp, i);
835 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
836 arp->l0 = cpu_to_be64(ep->l0);
837 arp->l1 = cpu_to_be64(ep->l1);
838 arp++; cnt++;
841 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
842 xfs_btree_set_numrecs(ablock, cnt);
845 * Fill in the root key and pointer.
847 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
848 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
849 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
850 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
851 be16_to_cpu(block->bb_level)));
852 *pp = cpu_to_be64(args.fsbno);
855 * Do all this logging at the end so that
856 * the root is at the right level.
858 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
859 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
860 ASSERT(*curp == NULL);
861 *curp = cur;
862 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
863 return 0;
867 * Convert a local file to an extents file.
868 * This code is out of bounds for data forks of regular files,
869 * since the file data needs to get logged so things will stay consistent.
870 * (The bmap-level manipulations are ok, though).
872 void
873 xfs_bmap_local_to_extents_empty(
874 struct xfs_inode *ip,
875 int whichfork)
877 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
879 ASSERT(whichfork != XFS_COW_FORK);
880 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
881 ASSERT(ifp->if_bytes == 0);
882 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
884 xfs_bmap_forkoff_reset(ip, whichfork);
885 ifp->if_flags &= ~XFS_IFINLINE;
886 ifp->if_flags |= XFS_IFEXTENTS;
887 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
891 STATIC int /* error */
892 xfs_bmap_local_to_extents(
893 xfs_trans_t *tp, /* transaction pointer */
894 xfs_inode_t *ip, /* incore inode pointer */
895 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
896 xfs_extlen_t total, /* total blocks needed by transaction */
897 int *logflagsp, /* inode logging flags */
898 int whichfork,
899 void (*init_fn)(struct xfs_trans *tp,
900 struct xfs_buf *bp,
901 struct xfs_inode *ip,
902 struct xfs_ifork *ifp))
904 int error = 0;
905 int flags; /* logging flags returned */
906 xfs_ifork_t *ifp; /* inode fork pointer */
907 xfs_alloc_arg_t args; /* allocation arguments */
908 xfs_buf_t *bp; /* buffer for extent block */
909 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
912 * We don't want to deal with the case of keeping inode data inline yet.
913 * So sending the data fork of a regular inode is invalid.
915 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
916 ifp = XFS_IFORK_PTR(ip, whichfork);
917 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
919 if (!ifp->if_bytes) {
920 xfs_bmap_local_to_extents_empty(ip, whichfork);
921 flags = XFS_ILOG_CORE;
922 goto done;
925 flags = 0;
926 error = 0;
927 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
928 XFS_IFINLINE);
929 memset(&args, 0, sizeof(args));
930 args.tp = tp;
931 args.mp = ip->i_mount;
932 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
933 args.firstblock = *firstblock;
935 * Allocate a block. We know we need only one, since the
936 * file currently fits in an inode.
938 if (*firstblock == NULLFSBLOCK) {
939 try_another_ag:
940 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
941 args.type = XFS_ALLOCTYPE_START_BNO;
942 } else {
943 args.fsbno = *firstblock;
944 args.type = XFS_ALLOCTYPE_NEAR_BNO;
946 args.total = total;
947 args.minlen = args.maxlen = args.prod = 1;
948 error = xfs_alloc_vextent(&args);
949 if (error)
950 goto done;
953 * During a CoW operation, the allocation and bmbt updates occur in
954 * different transactions. The mapping code tries to put new bmbt
955 * blocks near extents being mapped, but the only way to guarantee this
956 * is if the alloc and the mapping happen in a single transaction that
957 * has a block reservation. That isn't the case here, so if we run out
958 * of space we'll try again with another AG.
960 if (xfs_sb_version_hasreflink(&ip->i_mount->m_sb) &&
961 args.fsbno == NULLFSBLOCK &&
962 args.type == XFS_ALLOCTYPE_NEAR_BNO) {
963 goto try_another_ag;
965 /* Can't fail, the space was reserved. */
966 ASSERT(args.fsbno != NULLFSBLOCK);
967 ASSERT(args.len == 1);
968 *firstblock = args.fsbno;
969 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
972 * Initialize the block, copy the data and log the remote buffer.
974 * The callout is responsible for logging because the remote format
975 * might differ from the local format and thus we don't know how much to
976 * log here. Note that init_fn must also set the buffer log item type
977 * correctly.
979 init_fn(tp, bp, ip, ifp);
981 /* account for the change in fork size */
982 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
983 xfs_bmap_local_to_extents_empty(ip, whichfork);
984 flags |= XFS_ILOG_CORE;
986 xfs_iext_add(ifp, 0, 1);
987 ep = xfs_iext_get_ext(ifp, 0);
988 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
989 trace_xfs_bmap_post_update(ip, 0,
990 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
991 _THIS_IP_);
992 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
993 ip->i_d.di_nblocks = 1;
994 xfs_trans_mod_dquot_byino(tp, ip,
995 XFS_TRANS_DQ_BCOUNT, 1L);
996 flags |= xfs_ilog_fext(whichfork);
998 done:
999 *logflagsp = flags;
1000 return error;
1004 * Called from xfs_bmap_add_attrfork to handle btree format files.
1006 STATIC int /* error */
1007 xfs_bmap_add_attrfork_btree(
1008 xfs_trans_t *tp, /* transaction pointer */
1009 xfs_inode_t *ip, /* incore inode pointer */
1010 xfs_fsblock_t *firstblock, /* first block allocated */
1011 struct xfs_defer_ops *dfops, /* blocks to free at commit */
1012 int *flags) /* inode logging flags */
1014 xfs_btree_cur_t *cur; /* btree cursor */
1015 int error; /* error return value */
1016 xfs_mount_t *mp; /* file system mount struct */
1017 int stat; /* newroot status */
1019 mp = ip->i_mount;
1020 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
1021 *flags |= XFS_ILOG_DBROOT;
1022 else {
1023 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
1024 cur->bc_private.b.dfops = dfops;
1025 cur->bc_private.b.firstblock = *firstblock;
1026 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
1027 goto error0;
1028 /* must be at least one entry */
1029 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
1030 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
1031 goto error0;
1032 if (stat == 0) {
1033 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1034 return -ENOSPC;
1036 *firstblock = cur->bc_private.b.firstblock;
1037 cur->bc_private.b.allocated = 0;
1038 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1040 return 0;
1041 error0:
1042 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1043 return error;
1047 * Called from xfs_bmap_add_attrfork to handle extents format files.
1049 STATIC int /* error */
1050 xfs_bmap_add_attrfork_extents(
1051 xfs_trans_t *tp, /* transaction pointer */
1052 xfs_inode_t *ip, /* incore inode pointer */
1053 xfs_fsblock_t *firstblock, /* first block allocated */
1054 struct xfs_defer_ops *dfops, /* blocks to free at commit */
1055 int *flags) /* inode logging flags */
1057 xfs_btree_cur_t *cur; /* bmap btree cursor */
1058 int error; /* error return value */
1060 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1061 return 0;
1062 cur = NULL;
1063 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
1064 flags, XFS_DATA_FORK);
1065 if (cur) {
1066 cur->bc_private.b.allocated = 0;
1067 xfs_btree_del_cursor(cur,
1068 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1070 return error;
1074 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1075 * different data fork content type needs a different callout to do the
1076 * conversion. Some are basic and only require special block initialisation
1077 * callouts for the data formating, others (directories) are so specialised they
1078 * handle everything themselves.
1080 * XXX (dgc): investigate whether directory conversion can use the generic
1081 * formatting callout. It should be possible - it's just a very complex
1082 * formatter.
1084 STATIC int /* error */
1085 xfs_bmap_add_attrfork_local(
1086 xfs_trans_t *tp, /* transaction pointer */
1087 xfs_inode_t *ip, /* incore inode pointer */
1088 xfs_fsblock_t *firstblock, /* first block allocated */
1089 struct xfs_defer_ops *dfops, /* blocks to free at commit */
1090 int *flags) /* inode logging flags */
1092 xfs_da_args_t dargs; /* args for dir/attr code */
1094 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1095 return 0;
1097 if (S_ISDIR(VFS_I(ip)->i_mode)) {
1098 memset(&dargs, 0, sizeof(dargs));
1099 dargs.geo = ip->i_mount->m_dir_geo;
1100 dargs.dp = ip;
1101 dargs.firstblock = firstblock;
1102 dargs.dfops = dfops;
1103 dargs.total = dargs.geo->fsbcount;
1104 dargs.whichfork = XFS_DATA_FORK;
1105 dargs.trans = tp;
1106 return xfs_dir2_sf_to_block(&dargs);
1109 if (S_ISLNK(VFS_I(ip)->i_mode))
1110 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1111 flags, XFS_DATA_FORK,
1112 xfs_symlink_local_to_remote);
1114 /* should only be called for types that support local format data */
1115 ASSERT(0);
1116 return -EFSCORRUPTED;
1120 * Convert inode from non-attributed to attributed.
1121 * Must not be in a transaction, ip must not be locked.
1123 int /* error code */
1124 xfs_bmap_add_attrfork(
1125 xfs_inode_t *ip, /* incore inode pointer */
1126 int size, /* space new attribute needs */
1127 int rsvd) /* xact may use reserved blks */
1129 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
1130 struct xfs_defer_ops dfops; /* freed extent records */
1131 xfs_mount_t *mp; /* mount structure */
1132 xfs_trans_t *tp; /* transaction pointer */
1133 int blks; /* space reservation */
1134 int version = 1; /* superblock attr version */
1135 int logflags; /* logging flags */
1136 int error; /* error return value */
1138 ASSERT(XFS_IFORK_Q(ip) == 0);
1140 mp = ip->i_mount;
1141 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1143 blks = XFS_ADDAFORK_SPACE_RES(mp);
1145 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1146 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1147 if (error)
1148 return error;
1150 xfs_ilock(ip, XFS_ILOCK_EXCL);
1151 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1152 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1153 XFS_QMOPT_RES_REGBLKS);
1154 if (error)
1155 goto trans_cancel;
1156 if (XFS_IFORK_Q(ip))
1157 goto trans_cancel;
1158 if (ip->i_d.di_anextents != 0) {
1159 error = -EFSCORRUPTED;
1160 goto trans_cancel;
1162 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1164 * For inodes coming from pre-6.2 filesystems.
1166 ASSERT(ip->i_d.di_aformat == 0);
1167 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1170 xfs_trans_ijoin(tp, ip, 0);
1171 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1173 switch (ip->i_d.di_format) {
1174 case XFS_DINODE_FMT_DEV:
1175 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1176 break;
1177 case XFS_DINODE_FMT_UUID:
1178 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1179 break;
1180 case XFS_DINODE_FMT_LOCAL:
1181 case XFS_DINODE_FMT_EXTENTS:
1182 case XFS_DINODE_FMT_BTREE:
1183 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1184 if (!ip->i_d.di_forkoff)
1185 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1186 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1187 version = 2;
1188 break;
1189 default:
1190 ASSERT(0);
1191 error = -EINVAL;
1192 goto trans_cancel;
1195 ASSERT(ip->i_afp == NULL);
1196 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1197 ip->i_afp->if_flags = XFS_IFEXTENTS;
1198 logflags = 0;
1199 xfs_defer_init(&dfops, &firstblock);
1200 switch (ip->i_d.di_format) {
1201 case XFS_DINODE_FMT_LOCAL:
1202 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
1203 &logflags);
1204 break;
1205 case XFS_DINODE_FMT_EXTENTS:
1206 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
1207 &dfops, &logflags);
1208 break;
1209 case XFS_DINODE_FMT_BTREE:
1210 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
1211 &logflags);
1212 break;
1213 default:
1214 error = 0;
1215 break;
1217 if (logflags)
1218 xfs_trans_log_inode(tp, ip, logflags);
1219 if (error)
1220 goto bmap_cancel;
1221 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1222 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
1223 bool log_sb = false;
1225 spin_lock(&mp->m_sb_lock);
1226 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1227 xfs_sb_version_addattr(&mp->m_sb);
1228 log_sb = true;
1230 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1231 xfs_sb_version_addattr2(&mp->m_sb);
1232 log_sb = true;
1234 spin_unlock(&mp->m_sb_lock);
1235 if (log_sb)
1236 xfs_log_sb(tp);
1239 error = xfs_defer_finish(&tp, &dfops, NULL);
1240 if (error)
1241 goto bmap_cancel;
1242 error = xfs_trans_commit(tp);
1243 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1244 return error;
1246 bmap_cancel:
1247 xfs_defer_cancel(&dfops);
1248 trans_cancel:
1249 xfs_trans_cancel(tp);
1250 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1251 return error;
1255 * Internal and external extent tree search functions.
1259 * Read in the extents to if_extents.
1260 * All inode fields are set up by caller, we just traverse the btree
1261 * and copy the records in. If the file system cannot contain unwritten
1262 * extents, the records are checked for no "state" flags.
1264 int /* error */
1265 xfs_bmap_read_extents(
1266 xfs_trans_t *tp, /* transaction pointer */
1267 xfs_inode_t *ip, /* incore inode */
1268 int whichfork) /* data or attr fork */
1270 struct xfs_btree_block *block; /* current btree block */
1271 xfs_fsblock_t bno; /* block # of "block" */
1272 xfs_buf_t *bp; /* buffer for "block" */
1273 int error; /* error return value */
1274 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1275 xfs_extnum_t i, j; /* index into the extents list */
1276 xfs_ifork_t *ifp; /* fork structure */
1277 int level; /* btree level, for checking */
1278 xfs_mount_t *mp; /* file system mount structure */
1279 __be64 *pp; /* pointer to block address */
1280 /* REFERENCED */
1281 xfs_extnum_t room; /* number of entries there's room for */
1283 mp = ip->i_mount;
1284 ifp = XFS_IFORK_PTR(ip, whichfork);
1285 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1286 XFS_EXTFMT_INODE(ip);
1287 block = ifp->if_broot;
1289 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1291 level = be16_to_cpu(block->bb_level);
1292 ASSERT(level > 0);
1293 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1294 bno = be64_to_cpu(*pp);
1297 * Go down the tree until leaf level is reached, following the first
1298 * pointer (leftmost) at each level.
1300 while (level-- > 0) {
1301 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1302 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1303 if (error)
1304 return error;
1305 block = XFS_BUF_TO_BLOCK(bp);
1306 if (level == 0)
1307 break;
1308 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1309 bno = be64_to_cpu(*pp);
1310 XFS_WANT_CORRUPTED_GOTO(mp,
1311 XFS_FSB_SANITY_CHECK(mp, bno), error0);
1312 xfs_trans_brelse(tp, bp);
1315 * Here with bp and block set to the leftmost leaf node in the tree.
1317 room = xfs_iext_count(ifp);
1318 i = 0;
1320 * Loop over all leaf nodes. Copy information to the extent records.
1322 for (;;) {
1323 xfs_bmbt_rec_t *frp;
1324 xfs_fsblock_t nextbno;
1325 xfs_extnum_t num_recs;
1326 xfs_extnum_t start;
1328 num_recs = xfs_btree_get_numrecs(block);
1329 if (unlikely(i + num_recs > room)) {
1330 ASSERT(i + num_recs <= room);
1331 xfs_warn(ip->i_mount,
1332 "corrupt dinode %Lu, (btree extents).",
1333 (unsigned long long) ip->i_ino);
1334 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1335 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1336 goto error0;
1339 * Read-ahead the next leaf block, if any.
1341 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1342 if (nextbno != NULLFSBLOCK)
1343 xfs_btree_reada_bufl(mp, nextbno, 1,
1344 &xfs_bmbt_buf_ops);
1346 * Copy records into the extent records.
1348 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1349 start = i;
1350 for (j = 0; j < num_recs; j++, i++, frp++) {
1351 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1352 trp->l0 = be64_to_cpu(frp->l0);
1353 trp->l1 = be64_to_cpu(frp->l1);
1355 if (exntf == XFS_EXTFMT_NOSTATE) {
1357 * Check all attribute bmap btree records and
1358 * any "older" data bmap btree records for a
1359 * set bit in the "extent flag" position.
1361 if (unlikely(xfs_check_nostate_extents(ifp,
1362 start, num_recs))) {
1363 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1364 XFS_ERRLEVEL_LOW,
1365 ip->i_mount);
1366 goto error0;
1369 xfs_trans_brelse(tp, bp);
1370 bno = nextbno;
1372 * If we've reached the end, stop.
1374 if (bno == NULLFSBLOCK)
1375 break;
1376 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1377 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1378 if (error)
1379 return error;
1380 block = XFS_BUF_TO_BLOCK(bp);
1382 if (i != XFS_IFORK_NEXTENTS(ip, whichfork))
1383 return -EFSCORRUPTED;
1384 ASSERT(i == xfs_iext_count(ifp));
1385 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1386 return 0;
1387 error0:
1388 xfs_trans_brelse(tp, bp);
1389 return -EFSCORRUPTED;
1394 * Search the extent records for the entry containing block bno.
1395 * If bno lies in a hole, point to the next entry. If bno lies
1396 * past eof, *eofp will be set, and *prevp will contain the last
1397 * entry (null if none). Else, *lastxp will be set to the index
1398 * of the found entry; *gotp will contain the entry.
1400 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1401 xfs_bmap_search_multi_extents(
1402 xfs_ifork_t *ifp, /* inode fork pointer */
1403 xfs_fileoff_t bno, /* block number searched for */
1404 int *eofp, /* out: end of file found */
1405 xfs_extnum_t *lastxp, /* out: last extent index */
1406 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1407 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1409 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1410 xfs_extnum_t lastx; /* last extent index */
1413 * Initialize the extent entry structure to catch access to
1414 * uninitialized br_startblock field.
1416 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
1417 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
1418 gotp->br_state = XFS_EXT_INVALID;
1419 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
1420 prevp->br_startoff = NULLFILEOFF;
1422 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
1423 if (lastx > 0) {
1424 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
1426 if (lastx < xfs_iext_count(ifp)) {
1427 xfs_bmbt_get_all(ep, gotp);
1428 *eofp = 0;
1429 } else {
1430 if (lastx > 0) {
1431 *gotp = *prevp;
1433 *eofp = 1;
1434 ep = NULL;
1436 *lastxp = lastx;
1437 return ep;
1441 * Search the extents list for the inode, for the extent containing bno.
1442 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1443 * *eofp will be set, and *prevp will contain the last entry (null if none).
1444 * Else, *lastxp will be set to the index of the found
1445 * entry; *gotp will contain the entry.
1447 xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1448 xfs_bmap_search_extents(
1449 xfs_inode_t *ip, /* incore inode pointer */
1450 xfs_fileoff_t bno, /* block number searched for */
1451 int fork, /* data or attr fork */
1452 int *eofp, /* out: end of file found */
1453 xfs_extnum_t *lastxp, /* out: last extent index */
1454 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1455 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1457 xfs_ifork_t *ifp; /* inode fork pointer */
1458 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1460 XFS_STATS_INC(ip->i_mount, xs_look_exlist);
1461 ifp = XFS_IFORK_PTR(ip, fork);
1463 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
1465 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
1466 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
1467 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
1468 "Access to block zero in inode %llu "
1469 "start_block: %llx start_off: %llx "
1470 "blkcnt: %llx extent-state: %x lastx: %x",
1471 (unsigned long long)ip->i_ino,
1472 (unsigned long long)gotp->br_startblock,
1473 (unsigned long long)gotp->br_startoff,
1474 (unsigned long long)gotp->br_blockcount,
1475 gotp->br_state, *lastxp);
1476 *lastxp = NULLEXTNUM;
1477 *eofp = 1;
1478 return NULL;
1480 return ep;
1484 * Returns the file-relative block number of the first unused block(s)
1485 * in the file with at least "len" logically contiguous blocks free.
1486 * This is the lowest-address hole if the file has holes, else the first block
1487 * past the end of file.
1488 * Return 0 if the file is currently local (in-inode).
1490 int /* error */
1491 xfs_bmap_first_unused(
1492 xfs_trans_t *tp, /* transaction pointer */
1493 xfs_inode_t *ip, /* incore inode */
1494 xfs_extlen_t len, /* size of hole to find */
1495 xfs_fileoff_t *first_unused, /* unused block */
1496 int whichfork) /* data or attr fork */
1498 int error; /* error return value */
1499 int idx; /* extent record index */
1500 xfs_ifork_t *ifp; /* inode fork pointer */
1501 xfs_fileoff_t lastaddr; /* last block number seen */
1502 xfs_fileoff_t lowest; /* lowest useful block */
1503 xfs_fileoff_t max; /* starting useful block */
1504 xfs_fileoff_t off; /* offset for this block */
1505 xfs_extnum_t nextents; /* number of extent entries */
1507 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1508 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1509 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1510 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1511 *first_unused = 0;
1512 return 0;
1514 ifp = XFS_IFORK_PTR(ip, whichfork);
1515 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1516 (error = xfs_iread_extents(tp, ip, whichfork)))
1517 return error;
1518 lowest = *first_unused;
1519 nextents = xfs_iext_count(ifp);
1520 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1521 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1522 off = xfs_bmbt_get_startoff(ep);
1524 * See if the hole before this extent will work.
1526 if (off >= lowest + len && off - max >= len) {
1527 *first_unused = max;
1528 return 0;
1530 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1531 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1533 *first_unused = max;
1534 return 0;
1538 * Returns the file-relative block number of the last block - 1 before
1539 * last_block (input value) in the file.
1540 * This is not based on i_size, it is based on the extent records.
1541 * Returns 0 for local files, as they do not have extent records.
1543 int /* error */
1544 xfs_bmap_last_before(
1545 xfs_trans_t *tp, /* transaction pointer */
1546 xfs_inode_t *ip, /* incore inode */
1547 xfs_fileoff_t *last_block, /* last block */
1548 int whichfork) /* data or attr fork */
1550 xfs_fileoff_t bno; /* input file offset */
1551 int eof; /* hit end of file */
1552 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
1553 int error; /* error return value */
1554 xfs_bmbt_irec_t got; /* current extent value */
1555 xfs_ifork_t *ifp; /* inode fork pointer */
1556 xfs_extnum_t lastx; /* last extent used */
1557 xfs_bmbt_irec_t prev; /* previous extent value */
1559 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1560 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
1561 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
1562 return -EIO;
1563 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1564 *last_block = 0;
1565 return 0;
1567 ifp = XFS_IFORK_PTR(ip, whichfork);
1568 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1569 (error = xfs_iread_extents(tp, ip, whichfork)))
1570 return error;
1571 bno = *last_block - 1;
1572 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
1573 &prev);
1574 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
1575 if (prev.br_startoff == NULLFILEOFF)
1576 *last_block = 0;
1577 else
1578 *last_block = prev.br_startoff + prev.br_blockcount;
1581 * Otherwise *last_block is already the right answer.
1583 return 0;
1587 xfs_bmap_last_extent(
1588 struct xfs_trans *tp,
1589 struct xfs_inode *ip,
1590 int whichfork,
1591 struct xfs_bmbt_irec *rec,
1592 int *is_empty)
1594 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1595 int error;
1596 int nextents;
1598 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1599 error = xfs_iread_extents(tp, ip, whichfork);
1600 if (error)
1601 return error;
1604 nextents = xfs_iext_count(ifp);
1605 if (nextents == 0) {
1606 *is_empty = 1;
1607 return 0;
1610 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1611 *is_empty = 0;
1612 return 0;
1616 * Check the last inode extent to determine whether this allocation will result
1617 * in blocks being allocated at the end of the file. When we allocate new data
1618 * blocks at the end of the file which do not start at the previous data block,
1619 * we will try to align the new blocks at stripe unit boundaries.
1621 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1622 * at, or past the EOF.
1624 STATIC int
1625 xfs_bmap_isaeof(
1626 struct xfs_bmalloca *bma,
1627 int whichfork)
1629 struct xfs_bmbt_irec rec;
1630 int is_empty;
1631 int error;
1633 bma->aeof = 0;
1634 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1635 &is_empty);
1636 if (error)
1637 return error;
1639 if (is_empty) {
1640 bma->aeof = 1;
1641 return 0;
1645 * Check if we are allocation or past the last extent, or at least into
1646 * the last delayed allocated extent.
1648 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1649 (bma->offset >= rec.br_startoff &&
1650 isnullstartblock(rec.br_startblock));
1651 return 0;
1655 * Returns the file-relative block number of the first block past eof in
1656 * the file. This is not based on i_size, it is based on the extent records.
1657 * Returns 0 for local files, as they do not have extent records.
1660 xfs_bmap_last_offset(
1661 struct xfs_inode *ip,
1662 xfs_fileoff_t *last_block,
1663 int whichfork)
1665 struct xfs_bmbt_irec rec;
1666 int is_empty;
1667 int error;
1669 *last_block = 0;
1671 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1672 return 0;
1674 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1675 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1676 return -EIO;
1678 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1679 if (error || is_empty)
1680 return error;
1682 *last_block = rec.br_startoff + rec.br_blockcount;
1683 return 0;
1687 * Returns whether the selected fork of the inode has exactly one
1688 * block or not. For the data fork we check this matches di_size,
1689 * implying the file's range is 0..bsize-1.
1691 int /* 1=>1 block, 0=>otherwise */
1692 xfs_bmap_one_block(
1693 xfs_inode_t *ip, /* incore inode */
1694 int whichfork) /* data or attr fork */
1696 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1697 xfs_ifork_t *ifp; /* inode fork pointer */
1698 int rval; /* return value */
1699 xfs_bmbt_irec_t s; /* internal version of extent */
1701 #ifndef DEBUG
1702 if (whichfork == XFS_DATA_FORK)
1703 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1704 #endif /* !DEBUG */
1705 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1706 return 0;
1707 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1708 return 0;
1709 ifp = XFS_IFORK_PTR(ip, whichfork);
1710 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1711 ep = xfs_iext_get_ext(ifp, 0);
1712 xfs_bmbt_get_all(ep, &s);
1713 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1714 if (rval && whichfork == XFS_DATA_FORK)
1715 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1716 return rval;
1720 * Extent tree manipulation functions used during allocation.
1724 * Convert a delayed allocation to a real allocation.
1726 STATIC int /* error */
1727 xfs_bmap_add_extent_delay_real(
1728 struct xfs_bmalloca *bma,
1729 int whichfork)
1731 struct xfs_bmbt_irec *new = &bma->got;
1732 int diff; /* temp value */
1733 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1734 int error; /* error return value */
1735 int i; /* temp state */
1736 xfs_ifork_t *ifp; /* inode fork pointer */
1737 xfs_fileoff_t new_endoff; /* end offset of new entry */
1738 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1739 /* left is 0, right is 1, prev is 2 */
1740 int rval=0; /* return value (logging flags) */
1741 int state = 0;/* state bits, accessed thru macros */
1742 xfs_filblks_t da_new; /* new count del alloc blocks used */
1743 xfs_filblks_t da_old; /* old count del alloc blocks used */
1744 xfs_filblks_t temp=0; /* value for da_new calculations */
1745 xfs_filblks_t temp2=0;/* value for da_new calculations */
1746 int tmp_rval; /* partial logging flags */
1747 struct xfs_mount *mp;
1748 xfs_extnum_t *nextents;
1750 mp = bma->ip->i_mount;
1751 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
1752 ASSERT(whichfork != XFS_ATTR_FORK);
1753 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1754 &bma->ip->i_d.di_nextents);
1756 ASSERT(bma->idx >= 0);
1757 ASSERT(bma->idx <= xfs_iext_count(ifp));
1758 ASSERT(!isnullstartblock(new->br_startblock));
1759 ASSERT(!bma->cur ||
1760 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
1762 XFS_STATS_INC(mp, xs_add_exlist);
1764 #define LEFT r[0]
1765 #define RIGHT r[1]
1766 #define PREV r[2]
1768 if (whichfork == XFS_COW_FORK)
1769 state |= BMAP_COWFORK;
1772 * Set up a bunch of variables to make the tests simpler.
1774 ep = xfs_iext_get_ext(ifp, bma->idx);
1775 xfs_bmbt_get_all(ep, &PREV);
1776 new_endoff = new->br_startoff + new->br_blockcount;
1777 ASSERT(PREV.br_startoff <= new->br_startoff);
1778 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1780 da_old = startblockval(PREV.br_startblock);
1781 da_new = 0;
1784 * Set flags determining what part of the previous delayed allocation
1785 * extent is being replaced by a real allocation.
1787 if (PREV.br_startoff == new->br_startoff)
1788 state |= BMAP_LEFT_FILLING;
1789 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1790 state |= BMAP_RIGHT_FILLING;
1793 * Check and set flags if this segment has a left neighbor.
1794 * Don't set contiguous if the combined extent would be too large.
1796 if (bma->idx > 0) {
1797 state |= BMAP_LEFT_VALID;
1798 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
1800 if (isnullstartblock(LEFT.br_startblock))
1801 state |= BMAP_LEFT_DELAY;
1804 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1805 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1806 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1807 LEFT.br_state == new->br_state &&
1808 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1809 state |= BMAP_LEFT_CONTIG;
1812 * Check and set flags if this segment has a right neighbor.
1813 * Don't set contiguous if the combined extent would be too large.
1814 * Also check for all-three-contiguous being too large.
1816 if (bma->idx < xfs_iext_count(ifp) - 1) {
1817 state |= BMAP_RIGHT_VALID;
1818 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
1820 if (isnullstartblock(RIGHT.br_startblock))
1821 state |= BMAP_RIGHT_DELAY;
1824 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1825 new_endoff == RIGHT.br_startoff &&
1826 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1827 new->br_state == RIGHT.br_state &&
1828 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1829 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1830 BMAP_RIGHT_FILLING)) !=
1831 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1832 BMAP_RIGHT_FILLING) ||
1833 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1834 <= MAXEXTLEN))
1835 state |= BMAP_RIGHT_CONTIG;
1837 error = 0;
1839 * Switch out based on the FILLING and CONTIG state bits.
1841 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1842 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1843 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1844 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1846 * Filling in all of a previously delayed allocation extent.
1847 * The left and right neighbors are both contiguous with new.
1849 bma->idx--;
1850 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1851 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1852 LEFT.br_blockcount + PREV.br_blockcount +
1853 RIGHT.br_blockcount);
1854 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1856 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
1857 (*nextents)--;
1858 if (bma->cur == NULL)
1859 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1860 else {
1861 rval = XFS_ILOG_CORE;
1862 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
1863 RIGHT.br_startblock,
1864 RIGHT.br_blockcount, &i);
1865 if (error)
1866 goto done;
1867 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1868 error = xfs_btree_delete(bma->cur, &i);
1869 if (error)
1870 goto done;
1871 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1872 error = xfs_btree_decrement(bma->cur, 0, &i);
1873 if (error)
1874 goto done;
1875 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1876 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1877 LEFT.br_startblock,
1878 LEFT.br_blockcount +
1879 PREV.br_blockcount +
1880 RIGHT.br_blockcount, LEFT.br_state);
1881 if (error)
1882 goto done;
1884 break;
1886 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1888 * Filling in all of a previously delayed allocation extent.
1889 * The left neighbor is contiguous, the right is not.
1891 bma->idx--;
1893 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1894 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1895 LEFT.br_blockcount + PREV.br_blockcount);
1896 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1898 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1899 if (bma->cur == NULL)
1900 rval = XFS_ILOG_DEXT;
1901 else {
1902 rval = 0;
1903 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
1904 LEFT.br_startblock, LEFT.br_blockcount,
1905 &i);
1906 if (error)
1907 goto done;
1908 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1909 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1910 LEFT.br_startblock,
1911 LEFT.br_blockcount +
1912 PREV.br_blockcount, LEFT.br_state);
1913 if (error)
1914 goto done;
1916 break;
1918 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1920 * Filling in all of a previously delayed allocation extent.
1921 * The right neighbor is contiguous, the left is not.
1923 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1924 xfs_bmbt_set_startblock(ep, new->br_startblock);
1925 xfs_bmbt_set_blockcount(ep,
1926 PREV.br_blockcount + RIGHT.br_blockcount);
1927 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1929 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1930 if (bma->cur == NULL)
1931 rval = XFS_ILOG_DEXT;
1932 else {
1933 rval = 0;
1934 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
1935 RIGHT.br_startblock,
1936 RIGHT.br_blockcount, &i);
1937 if (error)
1938 goto done;
1939 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1940 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
1941 new->br_startblock,
1942 PREV.br_blockcount +
1943 RIGHT.br_blockcount, PREV.br_state);
1944 if (error)
1945 goto done;
1947 break;
1949 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1951 * Filling in all of a previously delayed allocation extent.
1952 * Neither the left nor right neighbors are contiguous with
1953 * the new one.
1955 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1956 xfs_bmbt_set_startblock(ep, new->br_startblock);
1957 xfs_bmbt_set_state(ep, new->br_state);
1958 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1960 (*nextents)++;
1961 if (bma->cur == NULL)
1962 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1963 else {
1964 rval = XFS_ILOG_CORE;
1965 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
1966 new->br_startblock, new->br_blockcount,
1967 &i);
1968 if (error)
1969 goto done;
1970 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1971 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1972 error = xfs_btree_insert(bma->cur, &i);
1973 if (error)
1974 goto done;
1975 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1977 break;
1979 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1981 * Filling in the first part of a previous delayed allocation.
1982 * The left neighbor is contiguous.
1984 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1985 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
1986 LEFT.br_blockcount + new->br_blockcount);
1987 xfs_bmbt_set_startoff(ep,
1988 PREV.br_startoff + new->br_blockcount);
1989 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1991 temp = PREV.br_blockcount - new->br_blockcount;
1992 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1993 xfs_bmbt_set_blockcount(ep, temp);
1994 if (bma->cur == NULL)
1995 rval = XFS_ILOG_DEXT;
1996 else {
1997 rval = 0;
1998 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
1999 LEFT.br_startblock, LEFT.br_blockcount,
2000 &i);
2001 if (error)
2002 goto done;
2003 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2004 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
2005 LEFT.br_startblock,
2006 LEFT.br_blockcount +
2007 new->br_blockcount,
2008 LEFT.br_state);
2009 if (error)
2010 goto done;
2012 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
2013 startblockval(PREV.br_startblock));
2014 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
2015 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2017 bma->idx--;
2018 break;
2020 case BMAP_LEFT_FILLING:
2022 * Filling in the first part of a previous delayed allocation.
2023 * The left neighbor is not contiguous.
2025 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2026 xfs_bmbt_set_startoff(ep, new_endoff);
2027 temp = PREV.br_blockcount - new->br_blockcount;
2028 xfs_bmbt_set_blockcount(ep, temp);
2029 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
2030 (*nextents)++;
2031 if (bma->cur == NULL)
2032 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2033 else {
2034 rval = XFS_ILOG_CORE;
2035 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
2036 new->br_startblock, new->br_blockcount,
2037 &i);
2038 if (error)
2039 goto done;
2040 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2041 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2042 error = xfs_btree_insert(bma->cur, &i);
2043 if (error)
2044 goto done;
2045 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2048 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
2049 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2050 bma->firstblock, bma->dfops,
2051 &bma->cur, 1, &tmp_rval, whichfork);
2052 rval |= tmp_rval;
2053 if (error)
2054 goto done;
2056 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
2057 startblockval(PREV.br_startblock) -
2058 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2059 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
2060 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
2061 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2062 break;
2064 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2066 * Filling in the last part of a previous delayed allocation.
2067 * The right neighbor is contiguous with the new allocation.
2069 temp = PREV.br_blockcount - new->br_blockcount;
2070 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2071 xfs_bmbt_set_blockcount(ep, temp);
2072 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
2073 new->br_startoff, new->br_startblock,
2074 new->br_blockcount + RIGHT.br_blockcount,
2075 RIGHT.br_state);
2076 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2077 if (bma->cur == NULL)
2078 rval = XFS_ILOG_DEXT;
2079 else {
2080 rval = 0;
2081 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
2082 RIGHT.br_startblock,
2083 RIGHT.br_blockcount, &i);
2084 if (error)
2085 goto done;
2086 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2087 error = xfs_bmbt_update(bma->cur, new->br_startoff,
2088 new->br_startblock,
2089 new->br_blockcount +
2090 RIGHT.br_blockcount,
2091 RIGHT.br_state);
2092 if (error)
2093 goto done;
2096 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
2097 startblockval(PREV.br_startblock));
2098 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2099 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
2100 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2102 bma->idx++;
2103 break;
2105 case BMAP_RIGHT_FILLING:
2107 * Filling in the last part of a previous delayed allocation.
2108 * The right neighbor is not contiguous.
2110 temp = PREV.br_blockcount - new->br_blockcount;
2111 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2112 xfs_bmbt_set_blockcount(ep, temp);
2113 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
2114 (*nextents)++;
2115 if (bma->cur == NULL)
2116 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2117 else {
2118 rval = XFS_ILOG_CORE;
2119 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
2120 new->br_startblock, new->br_blockcount,
2121 &i);
2122 if (error)
2123 goto done;
2124 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2125 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2126 error = xfs_btree_insert(bma->cur, &i);
2127 if (error)
2128 goto done;
2129 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2132 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
2133 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2134 bma->firstblock, bma->dfops, &bma->cur, 1,
2135 &tmp_rval, whichfork);
2136 rval |= tmp_rval;
2137 if (error)
2138 goto done;
2140 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
2141 startblockval(PREV.br_startblock) -
2142 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2143 ep = xfs_iext_get_ext(ifp, bma->idx);
2144 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
2145 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2147 bma->idx++;
2148 break;
2150 case 0:
2152 * Filling in the middle part of a previous delayed allocation.
2153 * Contiguity is impossible here.
2154 * This case is avoided almost all the time.
2156 * We start with a delayed allocation:
2158 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2159 * PREV @ idx
2161 * and we are allocating:
2162 * +rrrrrrrrrrrrrrrrr+
2163 * new
2165 * and we set it up for insertion as:
2166 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2167 * new
2168 * PREV @ idx LEFT RIGHT
2169 * inserted at idx + 1
2171 temp = new->br_startoff - PREV.br_startoff;
2172 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
2173 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
2174 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2175 LEFT = *new;
2176 RIGHT.br_state = PREV.br_state;
2177 RIGHT.br_startblock = nullstartblock(
2178 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
2179 RIGHT.br_startoff = new_endoff;
2180 RIGHT.br_blockcount = temp2;
2181 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2182 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
2183 (*nextents)++;
2184 if (bma->cur == NULL)
2185 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2186 else {
2187 rval = XFS_ILOG_CORE;
2188 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
2189 new->br_startblock, new->br_blockcount,
2190 &i);
2191 if (error)
2192 goto done;
2193 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2194 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2195 error = xfs_btree_insert(bma->cur, &i);
2196 if (error)
2197 goto done;
2198 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2201 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
2202 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2203 bma->firstblock, bma->dfops, &bma->cur,
2204 1, &tmp_rval, whichfork);
2205 rval |= tmp_rval;
2206 if (error)
2207 goto done;
2209 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2210 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
2211 diff = (int)(temp + temp2 -
2212 (startblockval(PREV.br_startblock) -
2213 (bma->cur ?
2214 bma->cur->bc_private.b.allocated : 0)));
2215 if (diff > 0) {
2216 error = xfs_mod_fdblocks(bma->ip->i_mount,
2217 -((int64_t)diff), false);
2218 ASSERT(!error);
2219 if (error)
2220 goto done;
2223 ep = xfs_iext_get_ext(ifp, bma->idx);
2224 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2225 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2226 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2227 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
2228 nullstartblock((int)temp2));
2229 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2231 bma->idx++;
2232 da_new = temp + temp2;
2233 break;
2235 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2236 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2237 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2238 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2239 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2240 case BMAP_LEFT_CONTIG:
2241 case BMAP_RIGHT_CONTIG:
2243 * These cases are all impossible.
2245 ASSERT(0);
2248 /* add reverse mapping */
2249 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2250 if (error)
2251 goto done;
2253 /* convert to a btree if necessary */
2254 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
2255 int tmp_logflags; /* partial log flag return val */
2257 ASSERT(bma->cur == NULL);
2258 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2259 bma->firstblock, bma->dfops, &bma->cur,
2260 da_old > 0, &tmp_logflags, whichfork);
2261 bma->logflags |= tmp_logflags;
2262 if (error)
2263 goto done;
2266 /* adjust for changes in reserved delayed indirect blocks */
2267 if (da_old || da_new) {
2268 temp = da_new;
2269 if (bma->cur)
2270 temp += bma->cur->bc_private.b.allocated;
2271 if (temp < da_old)
2272 xfs_mod_fdblocks(bma->ip->i_mount,
2273 (int64_t)(da_old - temp), false);
2276 /* clear out the allocated field, done with it now in any case. */
2277 if (bma->cur)
2278 bma->cur->bc_private.b.allocated = 0;
2280 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
2281 done:
2282 if (whichfork != XFS_COW_FORK)
2283 bma->logflags |= rval;
2284 return error;
2285 #undef LEFT
2286 #undef RIGHT
2287 #undef PREV
2291 * Convert an unwritten allocation to a real allocation or vice versa.
2293 STATIC int /* error */
2294 xfs_bmap_add_extent_unwritten_real(
2295 struct xfs_trans *tp,
2296 xfs_inode_t *ip, /* incore inode pointer */
2297 int whichfork,
2298 xfs_extnum_t *idx, /* extent number to update/insert */
2299 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
2300 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2301 xfs_fsblock_t *first, /* pointer to firstblock variable */
2302 struct xfs_defer_ops *dfops, /* list of extents to be freed */
2303 int *logflagsp) /* inode logging flags */
2305 xfs_btree_cur_t *cur; /* btree cursor */
2306 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
2307 int error; /* error return value */
2308 int i; /* temp state */
2309 xfs_ifork_t *ifp; /* inode fork pointer */
2310 xfs_fileoff_t new_endoff; /* end offset of new entry */
2311 xfs_exntst_t newext; /* new extent state */
2312 xfs_exntst_t oldext; /* old extent state */
2313 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2314 /* left is 0, right is 1, prev is 2 */
2315 int rval=0; /* return value (logging flags) */
2316 int state = 0;/* state bits, accessed thru macros */
2317 struct xfs_mount *mp = ip->i_mount;
2319 *logflagsp = 0;
2321 cur = *curp;
2322 ifp = XFS_IFORK_PTR(ip, whichfork);
2323 if (whichfork == XFS_COW_FORK)
2324 state |= BMAP_COWFORK;
2326 ASSERT(*idx >= 0);
2327 ASSERT(*idx <= xfs_iext_count(ifp));
2328 ASSERT(!isnullstartblock(new->br_startblock));
2330 XFS_STATS_INC(mp, xs_add_exlist);
2332 #define LEFT r[0]
2333 #define RIGHT r[1]
2334 #define PREV r[2]
2337 * Set up a bunch of variables to make the tests simpler.
2339 error = 0;
2340 ep = xfs_iext_get_ext(ifp, *idx);
2341 xfs_bmbt_get_all(ep, &PREV);
2342 newext = new->br_state;
2343 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2344 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2345 ASSERT(PREV.br_state == oldext);
2346 new_endoff = new->br_startoff + new->br_blockcount;
2347 ASSERT(PREV.br_startoff <= new->br_startoff);
2348 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
2351 * Set flags determining what part of the previous oldext allocation
2352 * extent is being replaced by a newext allocation.
2354 if (PREV.br_startoff == new->br_startoff)
2355 state |= BMAP_LEFT_FILLING;
2356 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2357 state |= BMAP_RIGHT_FILLING;
2360 * Check and set flags if this segment has a left neighbor.
2361 * Don't set contiguous if the combined extent would be too large.
2363 if (*idx > 0) {
2364 state |= BMAP_LEFT_VALID;
2365 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
2367 if (isnullstartblock(LEFT.br_startblock))
2368 state |= BMAP_LEFT_DELAY;
2371 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2372 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2373 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2374 LEFT.br_state == newext &&
2375 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2376 state |= BMAP_LEFT_CONTIG;
2379 * Check and set flags if this segment has a right neighbor.
2380 * Don't set contiguous if the combined extent would be too large.
2381 * Also check for all-three-contiguous being too large.
2383 if (*idx < xfs_iext_count(ifp) - 1) {
2384 state |= BMAP_RIGHT_VALID;
2385 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
2386 if (isnullstartblock(RIGHT.br_startblock))
2387 state |= BMAP_RIGHT_DELAY;
2390 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2391 new_endoff == RIGHT.br_startoff &&
2392 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2393 newext == RIGHT.br_state &&
2394 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2395 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2396 BMAP_RIGHT_FILLING)) !=
2397 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2398 BMAP_RIGHT_FILLING) ||
2399 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2400 <= MAXEXTLEN))
2401 state |= BMAP_RIGHT_CONTIG;
2404 * Switch out based on the FILLING and CONTIG state bits.
2406 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2407 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2408 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2409 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2411 * Setting all of a previous oldext extent to newext.
2412 * The left and right neighbors are both contiguous with new.
2414 --*idx;
2416 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2417 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2418 LEFT.br_blockcount + PREV.br_blockcount +
2419 RIGHT.br_blockcount);
2420 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2422 xfs_iext_remove(ip, *idx + 1, 2, state);
2423 XFS_IFORK_NEXT_SET(ip, whichfork,
2424 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
2425 if (cur == NULL)
2426 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2427 else {
2428 rval = XFS_ILOG_CORE;
2429 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2430 RIGHT.br_startblock,
2431 RIGHT.br_blockcount, &i)))
2432 goto done;
2433 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2434 if ((error = xfs_btree_delete(cur, &i)))
2435 goto done;
2436 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2437 if ((error = xfs_btree_decrement(cur, 0, &i)))
2438 goto done;
2439 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2440 if ((error = xfs_btree_delete(cur, &i)))
2441 goto done;
2442 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2443 if ((error = xfs_btree_decrement(cur, 0, &i)))
2444 goto done;
2445 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2446 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2447 LEFT.br_startblock,
2448 LEFT.br_blockcount + PREV.br_blockcount +
2449 RIGHT.br_blockcount, LEFT.br_state)))
2450 goto done;
2452 break;
2454 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2456 * Setting all of a previous oldext extent to newext.
2457 * The left neighbor is contiguous, the right is not.
2459 --*idx;
2461 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2462 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2463 LEFT.br_blockcount + PREV.br_blockcount);
2464 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2466 xfs_iext_remove(ip, *idx + 1, 1, state);
2467 XFS_IFORK_NEXT_SET(ip, whichfork,
2468 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2469 if (cur == NULL)
2470 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2471 else {
2472 rval = XFS_ILOG_CORE;
2473 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2474 PREV.br_startblock, PREV.br_blockcount,
2475 &i)))
2476 goto done;
2477 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2478 if ((error = xfs_btree_delete(cur, &i)))
2479 goto done;
2480 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2481 if ((error = xfs_btree_decrement(cur, 0, &i)))
2482 goto done;
2483 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2484 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2485 LEFT.br_startblock,
2486 LEFT.br_blockcount + PREV.br_blockcount,
2487 LEFT.br_state)))
2488 goto done;
2490 break;
2492 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2494 * Setting all of a previous oldext extent to newext.
2495 * The right neighbor is contiguous, the left is not.
2497 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2498 xfs_bmbt_set_blockcount(ep,
2499 PREV.br_blockcount + RIGHT.br_blockcount);
2500 xfs_bmbt_set_state(ep, newext);
2501 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2502 xfs_iext_remove(ip, *idx + 1, 1, state);
2503 XFS_IFORK_NEXT_SET(ip, whichfork,
2504 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2505 if (cur == NULL)
2506 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2507 else {
2508 rval = XFS_ILOG_CORE;
2509 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2510 RIGHT.br_startblock,
2511 RIGHT.br_blockcount, &i)))
2512 goto done;
2513 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2514 if ((error = xfs_btree_delete(cur, &i)))
2515 goto done;
2516 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2517 if ((error = xfs_btree_decrement(cur, 0, &i)))
2518 goto done;
2519 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2520 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2521 new->br_startblock,
2522 new->br_blockcount + RIGHT.br_blockcount,
2523 newext)))
2524 goto done;
2526 break;
2528 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
2530 * Setting all of a previous oldext extent to newext.
2531 * Neither the left nor right neighbors are contiguous with
2532 * the new one.
2534 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2535 xfs_bmbt_set_state(ep, newext);
2536 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2538 if (cur == NULL)
2539 rval = XFS_ILOG_DEXT;
2540 else {
2541 rval = 0;
2542 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2543 new->br_startblock, new->br_blockcount,
2544 &i)))
2545 goto done;
2546 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2547 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2548 new->br_startblock, new->br_blockcount,
2549 newext)))
2550 goto done;
2552 break;
2554 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2556 * Setting the first part of a previous oldext extent to newext.
2557 * The left neighbor is contiguous.
2559 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2560 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
2561 LEFT.br_blockcount + new->br_blockcount);
2562 xfs_bmbt_set_startoff(ep,
2563 PREV.br_startoff + new->br_blockcount);
2564 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
2566 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2567 xfs_bmbt_set_startblock(ep,
2568 new->br_startblock + new->br_blockcount);
2569 xfs_bmbt_set_blockcount(ep,
2570 PREV.br_blockcount - new->br_blockcount);
2571 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2573 --*idx;
2575 if (cur == NULL)
2576 rval = XFS_ILOG_DEXT;
2577 else {
2578 rval = 0;
2579 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2580 PREV.br_startblock, PREV.br_blockcount,
2581 &i)))
2582 goto done;
2583 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2584 if ((error = xfs_bmbt_update(cur,
2585 PREV.br_startoff + new->br_blockcount,
2586 PREV.br_startblock + new->br_blockcount,
2587 PREV.br_blockcount - new->br_blockcount,
2588 oldext)))
2589 goto done;
2590 if ((error = xfs_btree_decrement(cur, 0, &i)))
2591 goto done;
2592 error = xfs_bmbt_update(cur, LEFT.br_startoff,
2593 LEFT.br_startblock,
2594 LEFT.br_blockcount + new->br_blockcount,
2595 LEFT.br_state);
2596 if (error)
2597 goto done;
2599 break;
2601 case BMAP_LEFT_FILLING:
2603 * Setting the first part of a previous oldext extent to newext.
2604 * The left neighbor is not contiguous.
2606 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2607 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2608 xfs_bmbt_set_startoff(ep, new_endoff);
2609 xfs_bmbt_set_blockcount(ep,
2610 PREV.br_blockcount - new->br_blockcount);
2611 xfs_bmbt_set_startblock(ep,
2612 new->br_startblock + new->br_blockcount);
2613 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2615 xfs_iext_insert(ip, *idx, 1, new, state);
2616 XFS_IFORK_NEXT_SET(ip, whichfork,
2617 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2618 if (cur == NULL)
2619 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2620 else {
2621 rval = XFS_ILOG_CORE;
2622 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2623 PREV.br_startblock, PREV.br_blockcount,
2624 &i)))
2625 goto done;
2626 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2627 if ((error = xfs_bmbt_update(cur,
2628 PREV.br_startoff + new->br_blockcount,
2629 PREV.br_startblock + new->br_blockcount,
2630 PREV.br_blockcount - new->br_blockcount,
2631 oldext)))
2632 goto done;
2633 cur->bc_rec.b = *new;
2634 if ((error = xfs_btree_insert(cur, &i)))
2635 goto done;
2636 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2638 break;
2640 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2642 * Setting the last part of a previous oldext extent to newext.
2643 * The right neighbor is contiguous with the new allocation.
2645 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2646 xfs_bmbt_set_blockcount(ep,
2647 PREV.br_blockcount - new->br_blockcount);
2648 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2650 ++*idx;
2652 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2653 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2654 new->br_startoff, new->br_startblock,
2655 new->br_blockcount + RIGHT.br_blockcount, newext);
2656 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2658 if (cur == NULL)
2659 rval = XFS_ILOG_DEXT;
2660 else {
2661 rval = 0;
2662 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2663 PREV.br_startblock,
2664 PREV.br_blockcount, &i)))
2665 goto done;
2666 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2667 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2668 PREV.br_startblock,
2669 PREV.br_blockcount - new->br_blockcount,
2670 oldext)))
2671 goto done;
2672 if ((error = xfs_btree_increment(cur, 0, &i)))
2673 goto done;
2674 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2675 new->br_startblock,
2676 new->br_blockcount + RIGHT.br_blockcount,
2677 newext)))
2678 goto done;
2680 break;
2682 case BMAP_RIGHT_FILLING:
2684 * Setting the last part of a previous oldext extent to newext.
2685 * The right neighbor is not contiguous.
2687 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2688 xfs_bmbt_set_blockcount(ep,
2689 PREV.br_blockcount - new->br_blockcount);
2690 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2692 ++*idx;
2693 xfs_iext_insert(ip, *idx, 1, new, state);
2695 XFS_IFORK_NEXT_SET(ip, whichfork,
2696 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2697 if (cur == NULL)
2698 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2699 else {
2700 rval = XFS_ILOG_CORE;
2701 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2702 PREV.br_startblock, PREV.br_blockcount,
2703 &i)))
2704 goto done;
2705 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2706 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2707 PREV.br_startblock,
2708 PREV.br_blockcount - new->br_blockcount,
2709 oldext)))
2710 goto done;
2711 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2712 new->br_startblock, new->br_blockcount,
2713 &i)))
2714 goto done;
2715 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2716 cur->bc_rec.b.br_state = new->br_state;
2717 if ((error = xfs_btree_insert(cur, &i)))
2718 goto done;
2719 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2721 break;
2723 case 0:
2725 * Setting the middle part of a previous oldext extent to
2726 * newext. Contiguity is impossible here.
2727 * One extent becomes three extents.
2729 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2730 xfs_bmbt_set_blockcount(ep,
2731 new->br_startoff - PREV.br_startoff);
2732 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2734 r[0] = *new;
2735 r[1].br_startoff = new_endoff;
2736 r[1].br_blockcount =
2737 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2738 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2739 r[1].br_state = oldext;
2741 ++*idx;
2742 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2744 XFS_IFORK_NEXT_SET(ip, whichfork,
2745 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
2746 if (cur == NULL)
2747 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2748 else {
2749 rval = XFS_ILOG_CORE;
2750 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2751 PREV.br_startblock, PREV.br_blockcount,
2752 &i)))
2753 goto done;
2754 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2755 /* new right extent - oldext */
2756 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2757 r[1].br_startblock, r[1].br_blockcount,
2758 r[1].br_state)))
2759 goto done;
2760 /* new left extent - oldext */
2761 cur->bc_rec.b = PREV;
2762 cur->bc_rec.b.br_blockcount =
2763 new->br_startoff - PREV.br_startoff;
2764 if ((error = xfs_btree_insert(cur, &i)))
2765 goto done;
2766 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2768 * Reset the cursor to the position of the new extent
2769 * we are about to insert as we can't trust it after
2770 * the previous insert.
2772 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2773 new->br_startblock, new->br_blockcount,
2774 &i)))
2775 goto done;
2776 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2777 /* new middle extent - newext */
2778 cur->bc_rec.b.br_state = new->br_state;
2779 if ((error = xfs_btree_insert(cur, &i)))
2780 goto done;
2781 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2783 break;
2785 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2786 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2787 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2788 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2789 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2790 case BMAP_LEFT_CONTIG:
2791 case BMAP_RIGHT_CONTIG:
2793 * These cases are all impossible.
2795 ASSERT(0);
2798 /* update reverse mappings */
2799 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
2800 if (error)
2801 goto done;
2803 /* convert to a btree if necessary */
2804 if (xfs_bmap_needs_btree(ip, whichfork)) {
2805 int tmp_logflags; /* partial log flag return val */
2807 ASSERT(cur == NULL);
2808 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
2809 0, &tmp_logflags, whichfork);
2810 *logflagsp |= tmp_logflags;
2811 if (error)
2812 goto done;
2815 /* clear out the allocated field, done with it now in any case. */
2816 if (cur) {
2817 cur->bc_private.b.allocated = 0;
2818 *curp = cur;
2821 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
2822 done:
2823 *logflagsp |= rval;
2824 return error;
2825 #undef LEFT
2826 #undef RIGHT
2827 #undef PREV
2831 * Convert a hole to a delayed allocation.
2833 STATIC void
2834 xfs_bmap_add_extent_hole_delay(
2835 xfs_inode_t *ip, /* incore inode pointer */
2836 int whichfork,
2837 xfs_extnum_t *idx, /* extent number to update/insert */
2838 xfs_bmbt_irec_t *new) /* new data to add to file extents */
2840 xfs_ifork_t *ifp; /* inode fork pointer */
2841 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2842 xfs_filblks_t newlen=0; /* new indirect size */
2843 xfs_filblks_t oldlen=0; /* old indirect size */
2844 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2845 int state; /* state bits, accessed thru macros */
2846 xfs_filblks_t temp=0; /* temp for indirect calculations */
2848 ifp = XFS_IFORK_PTR(ip, whichfork);
2849 state = 0;
2850 if (whichfork == XFS_COW_FORK)
2851 state |= BMAP_COWFORK;
2852 ASSERT(isnullstartblock(new->br_startblock));
2855 * Check and set flags if this segment has a left neighbor
2857 if (*idx > 0) {
2858 state |= BMAP_LEFT_VALID;
2859 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
2861 if (isnullstartblock(left.br_startblock))
2862 state |= BMAP_LEFT_DELAY;
2866 * Check and set flags if the current (right) segment exists.
2867 * If it doesn't exist, we're converting the hole at end-of-file.
2869 if (*idx < xfs_iext_count(ifp)) {
2870 state |= BMAP_RIGHT_VALID;
2871 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
2873 if (isnullstartblock(right.br_startblock))
2874 state |= BMAP_RIGHT_DELAY;
2878 * Set contiguity flags on the left and right neighbors.
2879 * Don't let extents get too large, even if the pieces are contiguous.
2881 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2882 left.br_startoff + left.br_blockcount == new->br_startoff &&
2883 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2884 state |= BMAP_LEFT_CONTIG;
2886 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2887 new->br_startoff + new->br_blockcount == right.br_startoff &&
2888 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2889 (!(state & BMAP_LEFT_CONTIG) ||
2890 (left.br_blockcount + new->br_blockcount +
2891 right.br_blockcount <= MAXEXTLEN)))
2892 state |= BMAP_RIGHT_CONTIG;
2895 * Switch out based on the contiguity flags.
2897 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2898 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2900 * New allocation is contiguous with delayed allocations
2901 * on the left and on the right.
2902 * Merge all three into a single extent record.
2904 --*idx;
2905 temp = left.br_blockcount + new->br_blockcount +
2906 right.br_blockcount;
2908 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2909 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
2910 oldlen = startblockval(left.br_startblock) +
2911 startblockval(new->br_startblock) +
2912 startblockval(right.br_startblock);
2913 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2914 oldlen);
2915 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
2916 nullstartblock((int)newlen));
2917 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2919 xfs_iext_remove(ip, *idx + 1, 1, state);
2920 break;
2922 case BMAP_LEFT_CONTIG:
2924 * New allocation is contiguous with a delayed allocation
2925 * on the left.
2926 * Merge the new allocation with the left neighbor.
2928 --*idx;
2929 temp = left.br_blockcount + new->br_blockcount;
2931 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2932 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
2933 oldlen = startblockval(left.br_startblock) +
2934 startblockval(new->br_startblock);
2935 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2936 oldlen);
2937 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
2938 nullstartblock((int)newlen));
2939 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2940 break;
2942 case BMAP_RIGHT_CONTIG:
2944 * New allocation is contiguous with a delayed allocation
2945 * on the right.
2946 * Merge the new allocation with the right neighbor.
2948 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2949 temp = new->br_blockcount + right.br_blockcount;
2950 oldlen = startblockval(new->br_startblock) +
2951 startblockval(right.br_startblock);
2952 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2953 oldlen);
2954 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2955 new->br_startoff,
2956 nullstartblock((int)newlen), temp, right.br_state);
2957 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2958 break;
2960 case 0:
2962 * New allocation is not contiguous with another
2963 * delayed allocation.
2964 * Insert a new entry.
2966 oldlen = newlen = 0;
2967 xfs_iext_insert(ip, *idx, 1, new, state);
2968 break;
2970 if (oldlen != newlen) {
2971 ASSERT(oldlen > newlen);
2972 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2973 false);
2975 * Nothing to do for disk quota accounting here.
2981 * Convert a hole to a real allocation.
2983 STATIC int /* error */
2984 xfs_bmap_add_extent_hole_real(
2985 struct xfs_bmalloca *bma,
2986 int whichfork)
2988 struct xfs_bmbt_irec *new = &bma->got;
2989 int error; /* error return value */
2990 int i; /* temp state */
2991 xfs_ifork_t *ifp; /* inode fork pointer */
2992 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2993 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2994 int rval=0; /* return value (logging flags) */
2995 int state; /* state bits, accessed thru macros */
2996 struct xfs_mount *mp;
2998 mp = bma->ip->i_mount;
2999 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
3001 ASSERT(bma->idx >= 0);
3002 ASSERT(bma->idx <= xfs_iext_count(ifp));
3003 ASSERT(!isnullstartblock(new->br_startblock));
3004 ASSERT(!bma->cur ||
3005 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
3006 ASSERT(whichfork != XFS_COW_FORK);
3008 XFS_STATS_INC(mp, xs_add_exlist);
3010 state = 0;
3011 if (whichfork == XFS_ATTR_FORK)
3012 state |= BMAP_ATTRFORK;
3015 * Check and set flags if this segment has a left neighbor.
3017 if (bma->idx > 0) {
3018 state |= BMAP_LEFT_VALID;
3019 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
3020 if (isnullstartblock(left.br_startblock))
3021 state |= BMAP_LEFT_DELAY;
3025 * Check and set flags if this segment has a current value.
3026 * Not true if we're inserting into the "hole" at eof.
3028 if (bma->idx < xfs_iext_count(ifp)) {
3029 state |= BMAP_RIGHT_VALID;
3030 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
3031 if (isnullstartblock(right.br_startblock))
3032 state |= BMAP_RIGHT_DELAY;
3036 * We're inserting a real allocation between "left" and "right".
3037 * Set the contiguity flags. Don't let extents get too large.
3039 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
3040 left.br_startoff + left.br_blockcount == new->br_startoff &&
3041 left.br_startblock + left.br_blockcount == new->br_startblock &&
3042 left.br_state == new->br_state &&
3043 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
3044 state |= BMAP_LEFT_CONTIG;
3046 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
3047 new->br_startoff + new->br_blockcount == right.br_startoff &&
3048 new->br_startblock + new->br_blockcount == right.br_startblock &&
3049 new->br_state == right.br_state &&
3050 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
3051 (!(state & BMAP_LEFT_CONTIG) ||
3052 left.br_blockcount + new->br_blockcount +
3053 right.br_blockcount <= MAXEXTLEN))
3054 state |= BMAP_RIGHT_CONTIG;
3056 error = 0;
3058 * Select which case we're in here, and implement it.
3060 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
3061 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
3063 * New allocation is contiguous with real allocations on the
3064 * left and on the right.
3065 * Merge all three into a single extent record.
3067 --bma->idx;
3068 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3069 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
3070 left.br_blockcount + new->br_blockcount +
3071 right.br_blockcount);
3072 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
3074 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
3076 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3077 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
3078 if (bma->cur == NULL) {
3079 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
3080 } else {
3081 rval = XFS_ILOG_CORE;
3082 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
3083 right.br_startblock, right.br_blockcount,
3084 &i);
3085 if (error)
3086 goto done;
3087 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3088 error = xfs_btree_delete(bma->cur, &i);
3089 if (error)
3090 goto done;
3091 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3092 error = xfs_btree_decrement(bma->cur, 0, &i);
3093 if (error)
3094 goto done;
3095 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3096 error = xfs_bmbt_update(bma->cur, left.br_startoff,
3097 left.br_startblock,
3098 left.br_blockcount +
3099 new->br_blockcount +
3100 right.br_blockcount,
3101 left.br_state);
3102 if (error)
3103 goto done;
3105 break;
3107 case BMAP_LEFT_CONTIG:
3109 * New allocation is contiguous with a real allocation
3110 * on the left.
3111 * Merge the new allocation with the left neighbor.
3113 --bma->idx;
3114 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3115 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
3116 left.br_blockcount + new->br_blockcount);
3117 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
3119 if (bma->cur == NULL) {
3120 rval = xfs_ilog_fext(whichfork);
3121 } else {
3122 rval = 0;
3123 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3124 left.br_startblock, left.br_blockcount,
3125 &i);
3126 if (error)
3127 goto done;
3128 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3129 error = xfs_bmbt_update(bma->cur, left.br_startoff,
3130 left.br_startblock,
3131 left.br_blockcount +
3132 new->br_blockcount,
3133 left.br_state);
3134 if (error)
3135 goto done;
3137 break;
3139 case BMAP_RIGHT_CONTIG:
3141 * New allocation is contiguous with a real allocation
3142 * on the right.
3143 * Merge the new allocation with the right neighbor.
3145 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3146 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
3147 new->br_startoff, new->br_startblock,
3148 new->br_blockcount + right.br_blockcount,
3149 right.br_state);
3150 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
3152 if (bma->cur == NULL) {
3153 rval = xfs_ilog_fext(whichfork);
3154 } else {
3155 rval = 0;
3156 error = xfs_bmbt_lookup_eq(bma->cur,
3157 right.br_startoff,
3158 right.br_startblock,
3159 right.br_blockcount, &i);
3160 if (error)
3161 goto done;
3162 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3163 error = xfs_bmbt_update(bma->cur, new->br_startoff,
3164 new->br_startblock,
3165 new->br_blockcount +
3166 right.br_blockcount,
3167 right.br_state);
3168 if (error)
3169 goto done;
3171 break;
3173 case 0:
3175 * New allocation is not contiguous with another
3176 * real allocation.
3177 * Insert a new entry.
3179 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3180 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3181 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3182 if (bma->cur == NULL) {
3183 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
3184 } else {
3185 rval = XFS_ILOG_CORE;
3186 error = xfs_bmbt_lookup_eq(bma->cur,
3187 new->br_startoff,
3188 new->br_startblock,
3189 new->br_blockcount, &i);
3190 if (error)
3191 goto done;
3192 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
3193 bma->cur->bc_rec.b.br_state = new->br_state;
3194 error = xfs_btree_insert(bma->cur, &i);
3195 if (error)
3196 goto done;
3197 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
3199 break;
3202 /* add reverse mapping */
3203 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
3204 if (error)
3205 goto done;
3207 /* convert to a btree if necessary */
3208 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
3209 int tmp_logflags; /* partial log flag return val */
3211 ASSERT(bma->cur == NULL);
3212 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
3213 bma->firstblock, bma->dfops, &bma->cur,
3214 0, &tmp_logflags, whichfork);
3215 bma->logflags |= tmp_logflags;
3216 if (error)
3217 goto done;
3220 /* clear out the allocated field, done with it now in any case. */
3221 if (bma->cur)
3222 bma->cur->bc_private.b.allocated = 0;
3224 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
3225 done:
3226 bma->logflags |= rval;
3227 return error;
3231 * Functions used in the extent read, allocate and remove paths
3235 * Adjust the size of the new extent based on di_extsize and rt extsize.
3238 xfs_bmap_extsize_align(
3239 xfs_mount_t *mp,
3240 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3241 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3242 xfs_extlen_t extsz, /* align to this extent size */
3243 int rt, /* is this a realtime inode? */
3244 int eof, /* is extent at end-of-file? */
3245 int delay, /* creating delalloc extent? */
3246 int convert, /* overwriting unwritten extent? */
3247 xfs_fileoff_t *offp, /* in/out: aligned offset */
3248 xfs_extlen_t *lenp) /* in/out: aligned length */
3250 xfs_fileoff_t orig_off; /* original offset */
3251 xfs_extlen_t orig_alen; /* original length */
3252 xfs_fileoff_t orig_end; /* original off+len */
3253 xfs_fileoff_t nexto; /* next file offset */
3254 xfs_fileoff_t prevo; /* previous file offset */
3255 xfs_fileoff_t align_off; /* temp for offset */
3256 xfs_extlen_t align_alen; /* temp for length */
3257 xfs_extlen_t temp; /* temp for calculations */
3259 if (convert)
3260 return 0;
3262 orig_off = align_off = *offp;
3263 orig_alen = align_alen = *lenp;
3264 orig_end = orig_off + orig_alen;
3267 * If this request overlaps an existing extent, then don't
3268 * attempt to perform any additional alignment.
3270 if (!delay && !eof &&
3271 (orig_off >= gotp->br_startoff) &&
3272 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3273 return 0;
3277 * If the file offset is unaligned vs. the extent size
3278 * we need to align it. This will be possible unless
3279 * the file was previously written with a kernel that didn't
3280 * perform this alignment, or if a truncate shot us in the
3281 * foot.
3283 temp = do_mod(orig_off, extsz);
3284 if (temp) {
3285 align_alen += temp;
3286 align_off -= temp;
3289 /* Same adjustment for the end of the requested area. */
3290 temp = (align_alen % extsz);
3291 if (temp)
3292 align_alen += extsz - temp;
3295 * For large extent hint sizes, the aligned extent might be larger than
3296 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3297 * the length back under MAXEXTLEN. The outer allocation loops handle
3298 * short allocation just fine, so it is safe to do this. We only want to
3299 * do it when we are forced to, though, because it means more allocation
3300 * operations are required.
3302 while (align_alen > MAXEXTLEN)
3303 align_alen -= extsz;
3304 ASSERT(align_alen <= MAXEXTLEN);
3307 * If the previous block overlaps with this proposed allocation
3308 * then move the start forward without adjusting the length.
3310 if (prevp->br_startoff != NULLFILEOFF) {
3311 if (prevp->br_startblock == HOLESTARTBLOCK)
3312 prevo = prevp->br_startoff;
3313 else
3314 prevo = prevp->br_startoff + prevp->br_blockcount;
3315 } else
3316 prevo = 0;
3317 if (align_off != orig_off && align_off < prevo)
3318 align_off = prevo;
3320 * If the next block overlaps with this proposed allocation
3321 * then move the start back without adjusting the length,
3322 * but not before offset 0.
3323 * This may of course make the start overlap previous block,
3324 * and if we hit the offset 0 limit then the next block
3325 * can still overlap too.
3327 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3328 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3329 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3330 nexto = gotp->br_startoff + gotp->br_blockcount;
3331 else
3332 nexto = gotp->br_startoff;
3333 } else
3334 nexto = NULLFILEOFF;
3335 if (!eof &&
3336 align_off + align_alen != orig_end &&
3337 align_off + align_alen > nexto)
3338 align_off = nexto > align_alen ? nexto - align_alen : 0;
3340 * If we're now overlapping the next or previous extent that
3341 * means we can't fit an extsz piece in this hole. Just move
3342 * the start forward to the first valid spot and set
3343 * the length so we hit the end.
3345 if (align_off != orig_off && align_off < prevo)
3346 align_off = prevo;
3347 if (align_off + align_alen != orig_end &&
3348 align_off + align_alen > nexto &&
3349 nexto != NULLFILEOFF) {
3350 ASSERT(nexto > prevo);
3351 align_alen = nexto - align_off;
3355 * If realtime, and the result isn't a multiple of the realtime
3356 * extent size we need to remove blocks until it is.
3358 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3360 * We're not covering the original request, or
3361 * we won't be able to once we fix the length.
3363 if (orig_off < align_off ||
3364 orig_end > align_off + align_alen ||
3365 align_alen - temp < orig_alen)
3366 return -EINVAL;
3368 * Try to fix it by moving the start up.
3370 if (align_off + temp <= orig_off) {
3371 align_alen -= temp;
3372 align_off += temp;
3375 * Try to fix it by moving the end in.
3377 else if (align_off + align_alen - temp >= orig_end)
3378 align_alen -= temp;
3380 * Set the start to the minimum then trim the length.
3382 else {
3383 align_alen -= orig_off - align_off;
3384 align_off = orig_off;
3385 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3388 * Result doesn't cover the request, fail it.
3390 if (orig_off < align_off || orig_end > align_off + align_alen)
3391 return -EINVAL;
3392 } else {
3393 ASSERT(orig_off >= align_off);
3394 /* see MAXEXTLEN handling above */
3395 ASSERT(orig_end <= align_off + align_alen ||
3396 align_alen + extsz > MAXEXTLEN);
3399 #ifdef DEBUG
3400 if (!eof && gotp->br_startoff != NULLFILEOFF)
3401 ASSERT(align_off + align_alen <= gotp->br_startoff);
3402 if (prevp->br_startoff != NULLFILEOFF)
3403 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3404 #endif
3406 *lenp = align_alen;
3407 *offp = align_off;
3408 return 0;
3411 #define XFS_ALLOC_GAP_UNITS 4
3413 void
3414 xfs_bmap_adjacent(
3415 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3417 xfs_fsblock_t adjust; /* adjustment to block numbers */
3418 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3419 xfs_mount_t *mp; /* mount point structure */
3420 int nullfb; /* true if ap->firstblock isn't set */
3421 int rt; /* true if inode is realtime */
3423 #define ISVALID(x,y) \
3424 (rt ? \
3425 (x) < mp->m_sb.sb_rblocks : \
3426 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3427 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3428 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3430 mp = ap->ip->i_mount;
3431 nullfb = *ap->firstblock == NULLFSBLOCK;
3432 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3433 xfs_alloc_is_userdata(ap->datatype);
3434 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
3436 * If allocating at eof, and there's a previous real block,
3437 * try to use its last block as our starting point.
3439 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3440 !isnullstartblock(ap->prev.br_startblock) &&
3441 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3442 ap->prev.br_startblock)) {
3443 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3445 * Adjust for the gap between prevp and us.
3447 adjust = ap->offset -
3448 (ap->prev.br_startoff + ap->prev.br_blockcount);
3449 if (adjust &&
3450 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3451 ap->blkno += adjust;
3454 * If not at eof, then compare the two neighbor blocks.
3455 * Figure out whether either one gives us a good starting point,
3456 * and pick the better one.
3458 else if (!ap->eof) {
3459 xfs_fsblock_t gotbno; /* right side block number */
3460 xfs_fsblock_t gotdiff=0; /* right side difference */
3461 xfs_fsblock_t prevbno; /* left side block number */
3462 xfs_fsblock_t prevdiff=0; /* left side difference */
3465 * If there's a previous (left) block, select a requested
3466 * start block based on it.
3468 if (ap->prev.br_startoff != NULLFILEOFF &&
3469 !isnullstartblock(ap->prev.br_startblock) &&
3470 (prevbno = ap->prev.br_startblock +
3471 ap->prev.br_blockcount) &&
3472 ISVALID(prevbno, ap->prev.br_startblock)) {
3474 * Calculate gap to end of previous block.
3476 adjust = prevdiff = ap->offset -
3477 (ap->prev.br_startoff +
3478 ap->prev.br_blockcount);
3480 * Figure the startblock based on the previous block's
3481 * end and the gap size.
3482 * Heuristic!
3483 * If the gap is large relative to the piece we're
3484 * allocating, or using it gives us an invalid block
3485 * number, then just use the end of the previous block.
3487 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3488 ISVALID(prevbno + prevdiff,
3489 ap->prev.br_startblock))
3490 prevbno += adjust;
3491 else
3492 prevdiff += adjust;
3494 * If the firstblock forbids it, can't use it,
3495 * must use default.
3497 if (!rt && !nullfb &&
3498 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3499 prevbno = NULLFSBLOCK;
3502 * No previous block or can't follow it, just default.
3504 else
3505 prevbno = NULLFSBLOCK;
3507 * If there's a following (right) block, select a requested
3508 * start block based on it.
3510 if (!isnullstartblock(ap->got.br_startblock)) {
3512 * Calculate gap to start of next block.
3514 adjust = gotdiff = ap->got.br_startoff - ap->offset;
3516 * Figure the startblock based on the next block's
3517 * start and the gap size.
3519 gotbno = ap->got.br_startblock;
3521 * Heuristic!
3522 * If the gap is large relative to the piece we're
3523 * allocating, or using it gives us an invalid block
3524 * number, then just use the start of the next block
3525 * offset by our length.
3527 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3528 ISVALID(gotbno - gotdiff, gotbno))
3529 gotbno -= adjust;
3530 else if (ISVALID(gotbno - ap->length, gotbno)) {
3531 gotbno -= ap->length;
3532 gotdiff += adjust - ap->length;
3533 } else
3534 gotdiff += adjust;
3536 * If the firstblock forbids it, can't use it,
3537 * must use default.
3539 if (!rt && !nullfb &&
3540 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3541 gotbno = NULLFSBLOCK;
3544 * No next block, just default.
3546 else
3547 gotbno = NULLFSBLOCK;
3549 * If both valid, pick the better one, else the only good
3550 * one, else ap->blkno is already set (to 0 or the inode block).
3552 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
3553 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
3554 else if (prevbno != NULLFSBLOCK)
3555 ap->blkno = prevbno;
3556 else if (gotbno != NULLFSBLOCK)
3557 ap->blkno = gotbno;
3559 #undef ISVALID
3562 static int
3563 xfs_bmap_longest_free_extent(
3564 struct xfs_trans *tp,
3565 xfs_agnumber_t ag,
3566 xfs_extlen_t *blen,
3567 int *notinit)
3569 struct xfs_mount *mp = tp->t_mountp;
3570 struct xfs_perag *pag;
3571 xfs_extlen_t longest;
3572 int error = 0;
3574 pag = xfs_perag_get(mp, ag);
3575 if (!pag->pagf_init) {
3576 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3577 if (error)
3578 goto out;
3580 if (!pag->pagf_init) {
3581 *notinit = 1;
3582 goto out;
3586 longest = xfs_alloc_longest_free_extent(mp, pag,
3587 xfs_alloc_min_freelist(mp, pag),
3588 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
3589 if (*blen < longest)
3590 *blen = longest;
3592 out:
3593 xfs_perag_put(pag);
3594 return error;
3597 static void
3598 xfs_bmap_select_minlen(
3599 struct xfs_bmalloca *ap,
3600 struct xfs_alloc_arg *args,
3601 xfs_extlen_t *blen,
3602 int notinit)
3604 if (notinit || *blen < ap->minlen) {
3606 * Since we did a BUF_TRYLOCK above, it is possible that
3607 * there is space for this request.
3609 args->minlen = ap->minlen;
3610 } else if (*blen < args->maxlen) {
3612 * If the best seen length is less than the request length,
3613 * use the best as the minimum.
3615 args->minlen = *blen;
3616 } else {
3618 * Otherwise we've seen an extent as big as maxlen, use that
3619 * as the minimum.
3621 args->minlen = args->maxlen;
3625 STATIC int
3626 xfs_bmap_btalloc_nullfb(
3627 struct xfs_bmalloca *ap,
3628 struct xfs_alloc_arg *args,
3629 xfs_extlen_t *blen)
3631 struct xfs_mount *mp = ap->ip->i_mount;
3632 xfs_agnumber_t ag, startag;
3633 int notinit = 0;
3634 int error;
3636 args->type = XFS_ALLOCTYPE_START_BNO;
3637 args->total = ap->total;
3639 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3640 if (startag == NULLAGNUMBER)
3641 startag = ag = 0;
3643 while (*blen < args->maxlen) {
3644 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3645 &notinit);
3646 if (error)
3647 return error;
3649 if (++ag == mp->m_sb.sb_agcount)
3650 ag = 0;
3651 if (ag == startag)
3652 break;
3655 xfs_bmap_select_minlen(ap, args, blen, notinit);
3656 return 0;
3659 STATIC int
3660 xfs_bmap_btalloc_filestreams(
3661 struct xfs_bmalloca *ap,
3662 struct xfs_alloc_arg *args,
3663 xfs_extlen_t *blen)
3665 struct xfs_mount *mp = ap->ip->i_mount;
3666 xfs_agnumber_t ag;
3667 int notinit = 0;
3668 int error;
3670 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3671 args->total = ap->total;
3673 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3674 if (ag == NULLAGNUMBER)
3675 ag = 0;
3677 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3678 if (error)
3679 return error;
3681 if (*blen < args->maxlen) {
3682 error = xfs_filestream_new_ag(ap, &ag);
3683 if (error)
3684 return error;
3686 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3687 &notinit);
3688 if (error)
3689 return error;
3693 xfs_bmap_select_minlen(ap, args, blen, notinit);
3696 * Set the failure fallback case to look in the selected AG as stream
3697 * may have moved.
3699 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
3700 return 0;
3703 STATIC int
3704 xfs_bmap_btalloc(
3705 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3707 xfs_mount_t *mp; /* mount point structure */
3708 xfs_alloctype_t atype = 0; /* type for allocation routines */
3709 xfs_extlen_t align = 0; /* minimum allocation alignment */
3710 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3711 xfs_agnumber_t ag;
3712 xfs_alloc_arg_t args;
3713 xfs_extlen_t blen;
3714 xfs_extlen_t nextminlen = 0;
3715 int nullfb; /* true if ap->firstblock isn't set */
3716 int isaligned;
3717 int tryagain;
3718 int error;
3719 int stripe_align;
3721 ASSERT(ap->length);
3723 mp = ap->ip->i_mount;
3725 /* stripe alignment for allocation is determined by mount parameters */
3726 stripe_align = 0;
3727 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3728 stripe_align = mp->m_swidth;
3729 else if (mp->m_dalign)
3730 stripe_align = mp->m_dalign;
3732 if (ap->flags & XFS_BMAPI_COWFORK)
3733 align = xfs_get_cowextsz_hint(ap->ip);
3734 else if (xfs_alloc_is_userdata(ap->datatype))
3735 align = xfs_get_extsz_hint(ap->ip);
3736 if (align) {
3737 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
3738 align, 0, ap->eof, 0, ap->conv,
3739 &ap->offset, &ap->length);
3740 ASSERT(!error);
3741 ASSERT(ap->length);
3745 nullfb = *ap->firstblock == NULLFSBLOCK;
3746 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
3747 if (nullfb) {
3748 if (xfs_alloc_is_userdata(ap->datatype) &&
3749 xfs_inode_is_filestream(ap->ip)) {
3750 ag = xfs_filestream_lookup_ag(ap->ip);
3751 ag = (ag != NULLAGNUMBER) ? ag : 0;
3752 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
3753 } else {
3754 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
3756 } else
3757 ap->blkno = *ap->firstblock;
3759 xfs_bmap_adjacent(ap);
3762 * If allowed, use ap->blkno; otherwise must use firstblock since
3763 * it's in the right allocation group.
3765 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
3767 else
3768 ap->blkno = *ap->firstblock;
3770 * Normal allocation, done through xfs_alloc_vextent.
3772 tryagain = isaligned = 0;
3773 memset(&args, 0, sizeof(args));
3774 args.tp = ap->tp;
3775 args.mp = mp;
3776 args.fsbno = ap->blkno;
3777 xfs_rmap_skip_owner_update(&args.oinfo);
3779 /* Trim the allocation back to the maximum an AG can fit. */
3780 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
3781 args.firstblock = *ap->firstblock;
3782 blen = 0;
3783 if (nullfb) {
3785 * Search for an allocation group with a single extent large
3786 * enough for the request. If one isn't found, then adjust
3787 * the minimum allocation size to the largest space found.
3789 if (xfs_alloc_is_userdata(ap->datatype) &&
3790 xfs_inode_is_filestream(ap->ip))
3791 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3792 else
3793 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
3794 if (error)
3795 return error;
3796 } else if (ap->dfops->dop_low) {
3797 if (xfs_inode_is_filestream(ap->ip))
3798 args.type = XFS_ALLOCTYPE_FIRST_AG;
3799 else
3800 args.type = XFS_ALLOCTYPE_START_BNO;
3801 args.total = args.minlen = ap->minlen;
3802 } else {
3803 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3804 args.total = ap->total;
3805 args.minlen = ap->minlen;
3807 /* apply extent size hints if obtained earlier */
3808 if (align) {
3809 args.prod = align;
3810 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
3811 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3812 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
3813 args.prod = 1;
3814 args.mod = 0;
3815 } else {
3816 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
3817 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
3818 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3821 * If we are not low on available data blocks, and the
3822 * underlying logical volume manager is a stripe, and
3823 * the file offset is zero then try to allocate data
3824 * blocks on stripe unit boundary.
3825 * NOTE: ap->aeof is only set if the allocation length
3826 * is >= the stripe unit and the allocation offset is
3827 * at the end of file.
3829 if (!ap->dfops->dop_low && ap->aeof) {
3830 if (!ap->offset) {
3831 args.alignment = stripe_align;
3832 atype = args.type;
3833 isaligned = 1;
3835 * Adjust for alignment
3837 if (blen > args.alignment && blen <= args.maxlen)
3838 args.minlen = blen - args.alignment;
3839 args.minalignslop = 0;
3840 } else {
3842 * First try an exact bno allocation.
3843 * If it fails then do a near or start bno
3844 * allocation with alignment turned on.
3846 atype = args.type;
3847 tryagain = 1;
3848 args.type = XFS_ALLOCTYPE_THIS_BNO;
3849 args.alignment = 1;
3851 * Compute the minlen+alignment for the
3852 * next case. Set slop so that the value
3853 * of minlen+alignment+slop doesn't go up
3854 * between the calls.
3856 if (blen > stripe_align && blen <= args.maxlen)
3857 nextminlen = blen - stripe_align;
3858 else
3859 nextminlen = args.minlen;
3860 if (nextminlen + stripe_align > args.minlen + 1)
3861 args.minalignslop =
3862 nextminlen + stripe_align -
3863 args.minlen - 1;
3864 else
3865 args.minalignslop = 0;
3867 } else {
3868 args.alignment = 1;
3869 args.minalignslop = 0;
3871 args.minleft = ap->minleft;
3872 args.wasdel = ap->wasdel;
3873 args.resv = XFS_AG_RESV_NONE;
3874 args.datatype = ap->datatype;
3875 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
3876 args.ip = ap->ip;
3878 error = xfs_alloc_vextent(&args);
3879 if (error)
3880 return error;
3882 if (tryagain && args.fsbno == NULLFSBLOCK) {
3884 * Exact allocation failed. Now try with alignment
3885 * turned on.
3887 args.type = atype;
3888 args.fsbno = ap->blkno;
3889 args.alignment = stripe_align;
3890 args.minlen = nextminlen;
3891 args.minalignslop = 0;
3892 isaligned = 1;
3893 if ((error = xfs_alloc_vextent(&args)))
3894 return error;
3896 if (isaligned && args.fsbno == NULLFSBLOCK) {
3898 * allocation failed, so turn off alignment and
3899 * try again.
3901 args.type = atype;
3902 args.fsbno = ap->blkno;
3903 args.alignment = 0;
3904 if ((error = xfs_alloc_vextent(&args)))
3905 return error;
3907 if (args.fsbno == NULLFSBLOCK && nullfb &&
3908 args.minlen > ap->minlen) {
3909 args.minlen = ap->minlen;
3910 args.type = XFS_ALLOCTYPE_START_BNO;
3911 args.fsbno = ap->blkno;
3912 if ((error = xfs_alloc_vextent(&args)))
3913 return error;
3915 if (args.fsbno == NULLFSBLOCK && nullfb) {
3916 args.fsbno = 0;
3917 args.type = XFS_ALLOCTYPE_FIRST_AG;
3918 args.total = ap->minlen;
3919 if ((error = xfs_alloc_vextent(&args)))
3920 return error;
3921 ap->dfops->dop_low = true;
3923 if (args.fsbno != NULLFSBLOCK) {
3925 * check the allocation happened at the same or higher AG than
3926 * the first block that was allocated.
3928 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3929 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3930 XFS_FSB_TO_AGNO(mp, args.fsbno));
3932 ap->blkno = args.fsbno;
3933 if (*ap->firstblock == NULLFSBLOCK)
3934 *ap->firstblock = args.fsbno;
3935 ASSERT(nullfb || fb_agno <= args.agno);
3936 ap->length = args.len;
3937 if (!(ap->flags & XFS_BMAPI_COWFORK))
3938 ap->ip->i_d.di_nblocks += args.len;
3939 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3940 if (ap->wasdel)
3941 ap->ip->i_delayed_blks -= args.len;
3943 * Adjust the disk quota also. This was reserved
3944 * earlier.
3946 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3947 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3948 XFS_TRANS_DQ_BCOUNT,
3949 (long) args.len);
3950 } else {
3951 ap->blkno = NULLFSBLOCK;
3952 ap->length = 0;
3954 return 0;
3958 * For a remap operation, just "allocate" an extent at the address that the
3959 * caller passed in, and ensure that the AGFL is the right size. The caller
3960 * will then map the "allocated" extent into the file somewhere.
3962 STATIC int
3963 xfs_bmap_remap_alloc(
3964 struct xfs_bmalloca *ap)
3966 struct xfs_trans *tp = ap->tp;
3967 struct xfs_mount *mp = tp->t_mountp;
3968 xfs_fsblock_t bno;
3969 struct xfs_alloc_arg args;
3970 int error;
3973 * validate that the block number is legal - the enables us to detect
3974 * and handle a silent filesystem corruption rather than crashing.
3976 memset(&args, 0, sizeof(struct xfs_alloc_arg));
3977 args.tp = ap->tp;
3978 args.mp = ap->tp->t_mountp;
3979 bno = *ap->firstblock;
3980 args.agno = XFS_FSB_TO_AGNO(mp, bno);
3981 args.agbno = XFS_FSB_TO_AGBNO(mp, bno);
3982 if (args.agno >= mp->m_sb.sb_agcount ||
3983 args.agbno >= mp->m_sb.sb_agblocks)
3984 return -EFSCORRUPTED;
3986 /* "Allocate" the extent from the range we passed in. */
3987 trace_xfs_bmap_remap_alloc(ap->ip, *ap->firstblock, ap->length);
3988 ap->blkno = bno;
3989 ap->ip->i_d.di_nblocks += ap->length;
3990 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3992 /* Fix the freelist, like a real allocator does. */
3993 args.datatype = ap->datatype;
3994 args.pag = xfs_perag_get(args.mp, args.agno);
3995 ASSERT(args.pag);
3998 * The freelist fixing code will decline the allocation if
3999 * the size and shape of the free space doesn't allow for
4000 * allocating the extent and updating all the metadata that
4001 * happens during an allocation. We're remapping, not
4002 * allocating, so skip that check by pretending to be freeing.
4004 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
4005 xfs_perag_put(args.pag);
4006 if (error)
4007 trace_xfs_bmap_remap_alloc_error(ap->ip, error, _RET_IP_);
4008 return error;
4012 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
4013 * It figures out where to ask the underlying allocator to put the new extent.
4015 STATIC int
4016 xfs_bmap_alloc(
4017 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
4019 if (ap->flags & XFS_BMAPI_REMAP)
4020 return xfs_bmap_remap_alloc(ap);
4021 if (XFS_IS_REALTIME_INODE(ap->ip) &&
4022 xfs_alloc_is_userdata(ap->datatype))
4023 return xfs_bmap_rtalloc(ap);
4024 return xfs_bmap_btalloc(ap);
4027 /* Trim extent to fit a logical block range. */
4028 void
4029 xfs_trim_extent(
4030 struct xfs_bmbt_irec *irec,
4031 xfs_fileoff_t bno,
4032 xfs_filblks_t len)
4034 xfs_fileoff_t distance;
4035 xfs_fileoff_t end = bno + len;
4037 if (irec->br_startoff + irec->br_blockcount <= bno ||
4038 irec->br_startoff >= end) {
4039 irec->br_blockcount = 0;
4040 return;
4043 if (irec->br_startoff < bno) {
4044 distance = bno - irec->br_startoff;
4045 if (isnullstartblock(irec->br_startblock))
4046 irec->br_startblock = DELAYSTARTBLOCK;
4047 if (irec->br_startblock != DELAYSTARTBLOCK &&
4048 irec->br_startblock != HOLESTARTBLOCK)
4049 irec->br_startblock += distance;
4050 irec->br_startoff += distance;
4051 irec->br_blockcount -= distance;
4054 if (end < irec->br_startoff + irec->br_blockcount) {
4055 distance = irec->br_startoff + irec->br_blockcount - end;
4056 irec->br_blockcount -= distance;
4060 /* trim extent to within eof */
4061 void
4062 xfs_trim_extent_eof(
4063 struct xfs_bmbt_irec *irec,
4064 struct xfs_inode *ip)
4067 xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
4068 i_size_read(VFS_I(ip))));
4072 * Trim the returned map to the required bounds
4074 STATIC void
4075 xfs_bmapi_trim_map(
4076 struct xfs_bmbt_irec *mval,
4077 struct xfs_bmbt_irec *got,
4078 xfs_fileoff_t *bno,
4079 xfs_filblks_t len,
4080 xfs_fileoff_t obno,
4081 xfs_fileoff_t end,
4082 int n,
4083 int flags)
4085 if ((flags & XFS_BMAPI_ENTIRE) ||
4086 got->br_startoff + got->br_blockcount <= obno) {
4087 *mval = *got;
4088 if (isnullstartblock(got->br_startblock))
4089 mval->br_startblock = DELAYSTARTBLOCK;
4090 return;
4093 if (obno > *bno)
4094 *bno = obno;
4095 ASSERT((*bno >= obno) || (n == 0));
4096 ASSERT(*bno < end);
4097 mval->br_startoff = *bno;
4098 if (isnullstartblock(got->br_startblock))
4099 mval->br_startblock = DELAYSTARTBLOCK;
4100 else
4101 mval->br_startblock = got->br_startblock +
4102 (*bno - got->br_startoff);
4104 * Return the minimum of what we got and what we asked for for
4105 * the length. We can use the len variable here because it is
4106 * modified below and we could have been there before coming
4107 * here if the first part of the allocation didn't overlap what
4108 * was asked for.
4110 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
4111 got->br_blockcount - (*bno - got->br_startoff));
4112 mval->br_state = got->br_state;
4113 ASSERT(mval->br_blockcount <= len);
4114 return;
4118 * Update and validate the extent map to return
4120 STATIC void
4121 xfs_bmapi_update_map(
4122 struct xfs_bmbt_irec **map,
4123 xfs_fileoff_t *bno,
4124 xfs_filblks_t *len,
4125 xfs_fileoff_t obno,
4126 xfs_fileoff_t end,
4127 int *n,
4128 int flags)
4130 xfs_bmbt_irec_t *mval = *map;
4132 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4133 ((mval->br_startoff + mval->br_blockcount) <= end));
4134 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4135 (mval->br_startoff < obno));
4137 *bno = mval->br_startoff + mval->br_blockcount;
4138 *len = end - *bno;
4139 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4140 /* update previous map with new information */
4141 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4142 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4143 ASSERT(mval->br_state == mval[-1].br_state);
4144 mval[-1].br_blockcount = mval->br_blockcount;
4145 mval[-1].br_state = mval->br_state;
4146 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4147 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4148 mval[-1].br_startblock != HOLESTARTBLOCK &&
4149 mval->br_startblock == mval[-1].br_startblock +
4150 mval[-1].br_blockcount &&
4151 ((flags & XFS_BMAPI_IGSTATE) ||
4152 mval[-1].br_state == mval->br_state)) {
4153 ASSERT(mval->br_startoff ==
4154 mval[-1].br_startoff + mval[-1].br_blockcount);
4155 mval[-1].br_blockcount += mval->br_blockcount;
4156 } else if (*n > 0 &&
4157 mval->br_startblock == DELAYSTARTBLOCK &&
4158 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4159 mval->br_startoff ==
4160 mval[-1].br_startoff + mval[-1].br_blockcount) {
4161 mval[-1].br_blockcount += mval->br_blockcount;
4162 mval[-1].br_state = mval->br_state;
4163 } else if (!((*n == 0) &&
4164 ((mval->br_startoff + mval->br_blockcount) <=
4165 obno))) {
4166 mval++;
4167 (*n)++;
4169 *map = mval;
4173 * Map file blocks to filesystem blocks without allocation.
4176 xfs_bmapi_read(
4177 struct xfs_inode *ip,
4178 xfs_fileoff_t bno,
4179 xfs_filblks_t len,
4180 struct xfs_bmbt_irec *mval,
4181 int *nmap,
4182 int flags)
4184 struct xfs_mount *mp = ip->i_mount;
4185 struct xfs_ifork *ifp;
4186 struct xfs_bmbt_irec got;
4187 struct xfs_bmbt_irec prev;
4188 xfs_fileoff_t obno;
4189 xfs_fileoff_t end;
4190 xfs_extnum_t lastx;
4191 int error;
4192 int eof;
4193 int n = 0;
4194 int whichfork = xfs_bmapi_whichfork(flags);
4196 ASSERT(*nmap >= 1);
4197 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4198 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
4199 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
4201 if (unlikely(XFS_TEST_ERROR(
4202 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4203 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4204 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4205 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
4206 return -EFSCORRUPTED;
4209 if (XFS_FORCED_SHUTDOWN(mp))
4210 return -EIO;
4212 XFS_STATS_INC(mp, xs_blk_mapr);
4214 ifp = XFS_IFORK_PTR(ip, whichfork);
4215 if (!ifp) {
4216 /* No CoW fork? Return a hole. */
4217 if (whichfork == XFS_COW_FORK) {
4218 mval->br_startoff = bno;
4219 mval->br_startblock = HOLESTARTBLOCK;
4220 mval->br_blockcount = len;
4221 mval->br_state = XFS_EXT_NORM;
4222 *nmap = 1;
4223 return 0;
4227 * A missing attr ifork implies that the inode says we're in
4228 * extents or btree format but failed to pass the inode fork
4229 * verifier while trying to load it. Treat that as a file
4230 * corruption too.
4232 #ifdef DEBUG
4233 xfs_alert(mp, "%s: inode %llu missing fork %d",
4234 __func__, ip->i_ino, whichfork);
4235 #endif /* DEBUG */
4236 return -EFSCORRUPTED;
4239 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4240 error = xfs_iread_extents(NULL, ip, whichfork);
4241 if (error)
4242 return error;
4245 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4246 end = bno + len;
4247 obno = bno;
4249 while (bno < end && n < *nmap) {
4250 /* Reading past eof, act as though there's a hole up to end. */
4251 if (eof)
4252 got.br_startoff = end;
4253 if (got.br_startoff > bno) {
4254 /* Reading in a hole. */
4255 mval->br_startoff = bno;
4256 mval->br_startblock = HOLESTARTBLOCK;
4257 mval->br_blockcount =
4258 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4259 mval->br_state = XFS_EXT_NORM;
4260 bno += mval->br_blockcount;
4261 len -= mval->br_blockcount;
4262 mval++;
4263 n++;
4264 continue;
4267 /* set up the extent map to return. */
4268 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4269 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4271 /* If we're done, stop now. */
4272 if (bno >= end || n >= *nmap)
4273 break;
4275 /* Else go on to the next record. */
4276 if (++lastx < xfs_iext_count(ifp))
4277 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4278 else
4279 eof = 1;
4281 *nmap = n;
4282 return 0;
4286 * Add a delayed allocation extent to an inode. Blocks are reserved from the
4287 * global pool and the extent inserted into the inode in-core extent tree.
4289 * On entry, got refers to the first extent beyond the offset of the extent to
4290 * allocate or eof is specified if no such extent exists. On return, got refers
4291 * to the extent record that was inserted to the inode fork.
4293 * Note that the allocated extent may have been merged with contiguous extents
4294 * during insertion into the inode fork. Thus, got does not reflect the current
4295 * state of the inode fork on return. If necessary, the caller can use lastx to
4296 * look up the updated record in the inode fork.
4299 xfs_bmapi_reserve_delalloc(
4300 struct xfs_inode *ip,
4301 int whichfork,
4302 xfs_fileoff_t off,
4303 xfs_filblks_t len,
4304 xfs_filblks_t prealloc,
4305 struct xfs_bmbt_irec *got,
4306 xfs_extnum_t *lastx,
4307 int eof)
4309 struct xfs_mount *mp = ip->i_mount;
4310 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4311 xfs_extlen_t alen;
4312 xfs_extlen_t indlen;
4313 char rt = XFS_IS_REALTIME_INODE(ip);
4314 xfs_extlen_t extsz;
4315 int error;
4316 xfs_fileoff_t aoff = off;
4319 * Cap the alloc length. Keep track of prealloc so we know whether to
4320 * tag the inode before we return.
4322 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
4323 if (!eof)
4324 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4325 if (prealloc && alen >= len)
4326 prealloc = alen - len;
4328 /* Figure out the extent size, adjust alen */
4329 if (whichfork == XFS_COW_FORK)
4330 extsz = xfs_get_cowextsz_hint(ip);
4331 else
4332 extsz = xfs_get_extsz_hint(ip);
4333 if (extsz) {
4334 struct xfs_bmbt_irec prev;
4336 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
4337 prev.br_startoff = NULLFILEOFF;
4339 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
4340 1, 0, &aoff, &alen);
4341 ASSERT(!error);
4344 if (rt)
4345 extsz = alen / mp->m_sb.sb_rextsize;
4348 * Make a transaction-less quota reservation for delayed allocation
4349 * blocks. This number gets adjusted later. We return if we haven't
4350 * allocated blocks already inside this loop.
4352 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4353 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4354 if (error)
4355 return error;
4358 * Split changing sb for alen and indlen since they could be coming
4359 * from different places.
4361 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4362 ASSERT(indlen > 0);
4364 if (rt) {
4365 error = xfs_mod_frextents(mp, -((int64_t)extsz));
4366 } else {
4367 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
4370 if (error)
4371 goto out_unreserve_quota;
4373 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
4374 if (error)
4375 goto out_unreserve_blocks;
4378 ip->i_delayed_blks += alen;
4380 got->br_startoff = aoff;
4381 got->br_startblock = nullstartblock(indlen);
4382 got->br_blockcount = alen;
4383 got->br_state = XFS_EXT_NORM;
4385 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
4388 * Tag the inode if blocks were preallocated. Note that COW fork
4389 * preallocation can occur at the start or end of the extent, even when
4390 * prealloc == 0, so we must also check the aligned offset and length.
4392 if (whichfork == XFS_DATA_FORK && prealloc)
4393 xfs_inode_set_eofblocks_tag(ip);
4394 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4395 xfs_inode_set_cowblocks_tag(ip);
4397 return 0;
4399 out_unreserve_blocks:
4400 if (rt)
4401 xfs_mod_frextents(mp, extsz);
4402 else
4403 xfs_mod_fdblocks(mp, alen, false);
4404 out_unreserve_quota:
4405 if (XFS_IS_QUOTA_ON(mp))
4406 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
4407 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4408 return error;
4411 static int
4412 xfs_bmapi_allocate(
4413 struct xfs_bmalloca *bma)
4415 struct xfs_mount *mp = bma->ip->i_mount;
4416 int whichfork = xfs_bmapi_whichfork(bma->flags);
4417 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4418 int tmp_logflags = 0;
4419 int error;
4421 ASSERT(bma->length > 0);
4424 * For the wasdelay case, we could also just allocate the stuff asked
4425 * for in this bmap call but that wouldn't be as good.
4427 if (bma->wasdel) {
4428 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4429 bma->offset = bma->got.br_startoff;
4430 if (bma->idx != NULLEXTNUM && bma->idx) {
4431 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
4432 &bma->prev);
4434 } else {
4435 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
4436 if (!bma->eof)
4437 bma->length = XFS_FILBLKS_MIN(bma->length,
4438 bma->got.br_startoff - bma->offset);
4442 * Set the data type being allocated. For the data fork, the first data
4443 * in the file is treated differently to all other allocations. For the
4444 * attribute fork, we only need to ensure the allocated range is not on
4445 * the busy list.
4447 if (!(bma->flags & XFS_BMAPI_METADATA)) {
4448 bma->datatype = XFS_ALLOC_NOBUSY;
4449 if (whichfork == XFS_DATA_FORK) {
4450 if (bma->offset == 0)
4451 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4452 else
4453 bma->datatype |= XFS_ALLOC_USERDATA;
4455 if (bma->flags & XFS_BMAPI_ZERO)
4456 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
4459 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
4462 * Only want to do the alignment at the eof if it is userdata and
4463 * allocation length is larger than a stripe unit.
4465 if (mp->m_dalign && bma->length >= mp->m_dalign &&
4466 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
4467 error = xfs_bmap_isaeof(bma, whichfork);
4468 if (error)
4469 return error;
4472 error = xfs_bmap_alloc(bma);
4473 if (error)
4474 return error;
4476 if (bma->cur)
4477 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4478 if (bma->blkno == NULLFSBLOCK)
4479 return 0;
4480 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4481 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4482 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4483 bma->cur->bc_private.b.dfops = bma->dfops;
4486 * Bump the number of extents we've allocated
4487 * in this call.
4489 bma->nallocs++;
4491 if (bma->cur)
4492 bma->cur->bc_private.b.flags =
4493 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4495 bma->got.br_startoff = bma->offset;
4496 bma->got.br_startblock = bma->blkno;
4497 bma->got.br_blockcount = bma->length;
4498 bma->got.br_state = XFS_EXT_NORM;
4501 * In the data fork, a wasdelay extent has been initialized, so
4502 * shouldn't be flagged as unwritten.
4504 * For the cow fork, however, we convert delalloc reservations
4505 * (extents allocated for speculative preallocation) to
4506 * allocated unwritten extents, and only convert the unwritten
4507 * extents to real extents when we're about to write the data.
4509 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4510 (bma->flags & XFS_BMAPI_PREALLOC) &&
4511 xfs_sb_version_hasextflgbit(&mp->m_sb))
4512 bma->got.br_state = XFS_EXT_UNWRITTEN;
4514 if (bma->wasdel)
4515 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
4516 else
4517 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
4519 bma->logflags |= tmp_logflags;
4520 if (error)
4521 return error;
4524 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4525 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4526 * the neighbouring ones.
4528 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
4530 ASSERT(bma->got.br_startoff <= bma->offset);
4531 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4532 bma->offset + bma->length);
4533 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4534 bma->got.br_state == XFS_EXT_UNWRITTEN);
4535 return 0;
4538 STATIC int
4539 xfs_bmapi_convert_unwritten(
4540 struct xfs_bmalloca *bma,
4541 struct xfs_bmbt_irec *mval,
4542 xfs_filblks_t len,
4543 int flags)
4545 int whichfork = xfs_bmapi_whichfork(flags);
4546 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4547 int tmp_logflags = 0;
4548 int error;
4550 /* check if we need to do unwritten->real conversion */
4551 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4552 (flags & XFS_BMAPI_PREALLOC))
4553 return 0;
4555 /* check if we need to do real->unwritten conversion */
4556 if (mval->br_state == XFS_EXT_NORM &&
4557 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4558 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4559 return 0;
4562 * Modify (by adding) the state flag, if writing.
4564 ASSERT(mval->br_blockcount <= len);
4565 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4566 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4567 bma->ip, whichfork);
4568 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4569 bma->cur->bc_private.b.dfops = bma->dfops;
4571 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4572 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4575 * Before insertion into the bmbt, zero the range being converted
4576 * if required.
4578 if (flags & XFS_BMAPI_ZERO) {
4579 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4580 mval->br_blockcount);
4581 if (error)
4582 return error;
4585 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4586 &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
4587 &tmp_logflags);
4589 * Log the inode core unconditionally in the unwritten extent conversion
4590 * path because the conversion might not have done so (e.g., if the
4591 * extent count hasn't changed). We need to make sure the inode is dirty
4592 * in the transaction for the sake of fsync(), even if nothing has
4593 * changed, because fsync() will not force the log for this transaction
4594 * unless it sees the inode pinned.
4596 * Note: If we're only converting cow fork extents, there aren't
4597 * any on-disk updates to make, so we don't need to log anything.
4599 if (whichfork != XFS_COW_FORK)
4600 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
4601 if (error)
4602 return error;
4605 * Update our extent pointer, given that
4606 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4607 * of the neighbouring ones.
4609 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
4612 * We may have combined previously unwritten space with written space,
4613 * so generate another request.
4615 if (mval->br_blockcount < len)
4616 return -EAGAIN;
4617 return 0;
4621 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4622 * extent state if necessary. Details behaviour is controlled by the flags
4623 * parameter. Only allocates blocks from a single allocation group, to avoid
4624 * locking problems.
4626 * The returned value in "firstblock" from the first call in a transaction
4627 * must be remembered and presented to subsequent calls in "firstblock".
4628 * An upper bound for the number of blocks to be allocated is supplied to
4629 * the first call in "total"; if no allocation group has that many free
4630 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4633 xfs_bmapi_write(
4634 struct xfs_trans *tp, /* transaction pointer */
4635 struct xfs_inode *ip, /* incore inode */
4636 xfs_fileoff_t bno, /* starting file offs. mapped */
4637 xfs_filblks_t len, /* length to map in file */
4638 int flags, /* XFS_BMAPI_... */
4639 xfs_fsblock_t *firstblock, /* first allocated block
4640 controls a.g. for allocs */
4641 xfs_extlen_t total, /* total blocks needed */
4642 struct xfs_bmbt_irec *mval, /* output: map values */
4643 int *nmap, /* i/o: mval size/count */
4644 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
4646 struct xfs_mount *mp = ip->i_mount;
4647 struct xfs_ifork *ifp;
4648 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
4649 xfs_fileoff_t end; /* end of mapped file region */
4650 int eof; /* after the end of extents */
4651 int error; /* error return */
4652 int n; /* current extent index */
4653 xfs_fileoff_t obno; /* old block number (offset) */
4654 int whichfork; /* data or attr fork */
4656 #ifdef DEBUG
4657 xfs_fileoff_t orig_bno; /* original block number value */
4658 int orig_flags; /* original flags arg value */
4659 xfs_filblks_t orig_len; /* original value of len arg */
4660 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4661 int orig_nmap; /* original value of *nmap */
4663 orig_bno = bno;
4664 orig_len = len;
4665 orig_flags = flags;
4666 orig_mval = mval;
4667 orig_nmap = *nmap;
4668 #endif
4669 whichfork = xfs_bmapi_whichfork(flags);
4671 ASSERT(*nmap >= 1);
4672 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4673 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4674 ASSERT(tp != NULL ||
4675 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4676 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
4677 ASSERT(len > 0);
4678 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
4679 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4680 ASSERT(!(flags & XFS_BMAPI_REMAP) || whichfork == XFS_DATA_FORK);
4681 ASSERT(!(flags & XFS_BMAPI_PREALLOC) || !(flags & XFS_BMAPI_REMAP));
4682 ASSERT(!(flags & XFS_BMAPI_CONVERT) || !(flags & XFS_BMAPI_REMAP));
4684 /* zeroing is for currently only for data extents, not metadata */
4685 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4686 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4688 * we can allocate unwritten extents or pre-zero allocated blocks,
4689 * but it makes no sense to do both at once. This would result in
4690 * zeroing the unwritten extent twice, but it still being an
4691 * unwritten extent....
4693 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4694 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4696 if (unlikely(XFS_TEST_ERROR(
4697 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4698 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4699 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4700 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
4701 return -EFSCORRUPTED;
4704 if (XFS_FORCED_SHUTDOWN(mp))
4705 return -EIO;
4707 ifp = XFS_IFORK_PTR(ip, whichfork);
4709 XFS_STATS_INC(mp, xs_blk_mapw);
4711 if (*firstblock == NULLFSBLOCK) {
4712 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4713 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4714 else
4715 bma.minleft = 1;
4716 } else {
4717 bma.minleft = 0;
4720 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4721 error = xfs_iread_extents(tp, ip, whichfork);
4722 if (error)
4723 goto error0;
4726 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
4727 &bma.prev);
4728 n = 0;
4729 end = bno + len;
4730 obno = bno;
4732 bma.tp = tp;
4733 bma.ip = ip;
4734 bma.total = total;
4735 bma.datatype = 0;
4736 bma.dfops = dfops;
4737 bma.firstblock = firstblock;
4739 while (bno < end && n < *nmap) {
4740 bool need_alloc = false, wasdelay = false;
4742 /* in hole or beyoned EOF? */
4743 if (eof || bma.got.br_startoff > bno) {
4744 if (flags & XFS_BMAPI_DELALLOC) {
4746 * For the COW fork we can reasonably get a
4747 * request for converting an extent that races
4748 * with other threads already having converted
4749 * part of it, as there converting COW to
4750 * regular blocks is not protected using the
4751 * IOLOCK.
4753 ASSERT(flags & XFS_BMAPI_COWFORK);
4754 if (!(flags & XFS_BMAPI_COWFORK)) {
4755 error = -EIO;
4756 goto error0;
4759 if (eof || bno >= end)
4760 break;
4761 } else {
4762 need_alloc = true;
4764 } else {
4766 * Make sure we only reflink into a hole.
4768 ASSERT(!(flags & XFS_BMAPI_REMAP));
4769 if (isnullstartblock(bma.got.br_startblock))
4770 wasdelay = true;
4774 * First, deal with the hole before the allocated space
4775 * that we found, if any.
4777 if (need_alloc || wasdelay) {
4778 bma.eof = eof;
4779 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4780 bma.wasdel = wasdelay;
4781 bma.offset = bno;
4782 bma.flags = flags;
4785 * There's a 32/64 bit type mismatch between the
4786 * allocation length request (which can be 64 bits in
4787 * length) and the bma length request, which is
4788 * xfs_extlen_t and therefore 32 bits. Hence we have to
4789 * check for 32-bit overflows and handle them here.
4791 if (len > (xfs_filblks_t)MAXEXTLEN)
4792 bma.length = MAXEXTLEN;
4793 else
4794 bma.length = len;
4796 ASSERT(len > 0);
4797 ASSERT(bma.length > 0);
4798 error = xfs_bmapi_allocate(&bma);
4799 if (error)
4800 goto error0;
4801 if (bma.blkno == NULLFSBLOCK)
4802 break;
4805 * If this is a CoW allocation, record the data in
4806 * the refcount btree for orphan recovery.
4808 if (whichfork == XFS_COW_FORK) {
4809 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4810 bma.blkno, bma.length);
4811 if (error)
4812 goto error0;
4816 /* Deal with the allocated space we found. */
4817 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4818 end, n, flags);
4820 /* Execute unwritten extent conversion if necessary */
4821 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
4822 if (error == -EAGAIN)
4823 continue;
4824 if (error)
4825 goto error0;
4827 /* update the extent map to return */
4828 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4831 * If we're done, stop now. Stop when we've allocated
4832 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4833 * the transaction may get too big.
4835 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4836 break;
4838 /* Else go on to the next record. */
4839 bma.prev = bma.got;
4840 if (++bma.idx < xfs_iext_count(ifp)) {
4841 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
4842 &bma.got);
4843 } else
4844 eof = 1;
4846 *nmap = n;
4849 * Transform from btree to extents, give it cur.
4851 if (xfs_bmap_wants_extents(ip, whichfork)) {
4852 int tmp_logflags = 0;
4854 ASSERT(bma.cur);
4855 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
4856 &tmp_logflags, whichfork);
4857 bma.logflags |= tmp_logflags;
4858 if (error)
4859 goto error0;
4862 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4863 XFS_IFORK_NEXTENTS(ip, whichfork) >
4864 XFS_IFORK_MAXEXT(ip, whichfork));
4865 error = 0;
4866 error0:
4868 * Log everything. Do this after conversion, there's no point in
4869 * logging the extent records if we've converted to btree format.
4871 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
4872 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4873 bma.logflags &= ~xfs_ilog_fext(whichfork);
4874 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
4875 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
4876 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
4878 * Log whatever the flags say, even if error. Otherwise we might miss
4879 * detecting a case where the data is changed, there's an error,
4880 * and it's not logged so we don't shutdown when we should.
4882 if (bma.logflags)
4883 xfs_trans_log_inode(tp, ip, bma.logflags);
4885 if (bma.cur) {
4886 if (!error) {
4887 ASSERT(*firstblock == NULLFSBLOCK ||
4888 XFS_FSB_TO_AGNO(mp, *firstblock) <=
4889 XFS_FSB_TO_AGNO(mp,
4890 bma.cur->bc_private.b.firstblock));
4891 *firstblock = bma.cur->bc_private.b.firstblock;
4893 xfs_btree_del_cursor(bma.cur,
4894 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4896 if (!error)
4897 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4898 orig_nmap, *nmap);
4899 return error;
4903 * When a delalloc extent is split (e.g., due to a hole punch), the original
4904 * indlen reservation must be shared across the two new extents that are left
4905 * behind.
4907 * Given the original reservation and the worst case indlen for the two new
4908 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4909 * reservation fairly across the two new extents. If necessary, steal available
4910 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4911 * ores == 1). The number of stolen blocks is returned. The availability and
4912 * subsequent accounting of stolen blocks is the responsibility of the caller.
4914 static xfs_filblks_t
4915 xfs_bmap_split_indlen(
4916 xfs_filblks_t ores, /* original res. */
4917 xfs_filblks_t *indlen1, /* ext1 worst indlen */
4918 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4919 xfs_filblks_t avail) /* stealable blocks */
4921 xfs_filblks_t len1 = *indlen1;
4922 xfs_filblks_t len2 = *indlen2;
4923 xfs_filblks_t nres = len1 + len2; /* new total res. */
4924 xfs_filblks_t stolen = 0;
4925 xfs_filblks_t resfactor;
4928 * Steal as many blocks as we can to try and satisfy the worst case
4929 * indlen for both new extents.
4931 if (ores < nres && avail)
4932 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4933 ores += stolen;
4935 /* nothing else to do if we've satisfied the new reservation */
4936 if (ores >= nres)
4937 return stolen;
4940 * We can't meet the total required reservation for the two extents.
4941 * Calculate the percent of the overall shortage between both extents
4942 * and apply this percentage to each of the requested indlen values.
4943 * This distributes the shortage fairly and reduces the chances that one
4944 * of the two extents is left with nothing when extents are repeatedly
4945 * split.
4947 resfactor = (ores * 100);
4948 do_div(resfactor, nres);
4949 len1 *= resfactor;
4950 do_div(len1, 100);
4951 len2 *= resfactor;
4952 do_div(len2, 100);
4953 ASSERT(len1 + len2 <= ores);
4954 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4957 * Hand out the remainder to each extent. If one of the two reservations
4958 * is zero, we want to make sure that one gets a block first. The loop
4959 * below starts with len1, so hand len2 a block right off the bat if it
4960 * is zero.
4962 ores -= (len1 + len2);
4963 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4964 if (ores && !len2 && *indlen2) {
4965 len2++;
4966 ores--;
4968 while (ores) {
4969 if (len1 < *indlen1) {
4970 len1++;
4971 ores--;
4973 if (!ores)
4974 break;
4975 if (len2 < *indlen2) {
4976 len2++;
4977 ores--;
4981 *indlen1 = len1;
4982 *indlen2 = len2;
4984 return stolen;
4988 xfs_bmap_del_extent_delay(
4989 struct xfs_inode *ip,
4990 int whichfork,
4991 xfs_extnum_t *idx,
4992 struct xfs_bmbt_irec *got,
4993 struct xfs_bmbt_irec *del)
4995 struct xfs_mount *mp = ip->i_mount;
4996 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4997 struct xfs_bmbt_irec new;
4998 int64_t da_old, da_new, da_diff = 0;
4999 xfs_fileoff_t del_endoff, got_endoff;
5000 xfs_filblks_t got_indlen, new_indlen, stolen;
5001 int error = 0, state = 0;
5002 bool isrt;
5004 XFS_STATS_INC(mp, xs_del_exlist);
5006 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5007 del_endoff = del->br_startoff + del->br_blockcount;
5008 got_endoff = got->br_startoff + got->br_blockcount;
5009 da_old = startblockval(got->br_startblock);
5010 da_new = 0;
5012 ASSERT(*idx >= 0);
5013 ASSERT(*idx <= xfs_iext_count(ifp));
5014 ASSERT(del->br_blockcount > 0);
5015 ASSERT(got->br_startoff <= del->br_startoff);
5016 ASSERT(got_endoff >= del_endoff);
5018 if (isrt) {
5019 int64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
5021 do_div(rtexts, mp->m_sb.sb_rextsize);
5022 xfs_mod_frextents(mp, rtexts);
5026 * Update the inode delalloc counter now and wait to update the
5027 * sb counters as we might have to borrow some blocks for the
5028 * indirect block accounting.
5030 error = xfs_trans_reserve_quota_nblks(NULL, ip,
5031 -((long)del->br_blockcount), 0,
5032 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
5033 if (error)
5034 return error;
5035 ip->i_delayed_blks -= del->br_blockcount;
5037 if (whichfork == XFS_COW_FORK)
5038 state |= BMAP_COWFORK;
5040 if (got->br_startoff == del->br_startoff)
5041 state |= BMAP_LEFT_CONTIG;
5042 if (got_endoff == del_endoff)
5043 state |= BMAP_RIGHT_CONTIG;
5045 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
5046 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
5048 * Matches the whole extent. Delete the entry.
5050 xfs_iext_remove(ip, *idx, 1, state);
5051 --*idx;
5052 break;
5053 case BMAP_LEFT_CONTIG:
5055 * Deleting the first part of the extent.
5057 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5058 got->br_startoff = del_endoff;
5059 got->br_blockcount -= del->br_blockcount;
5060 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
5061 got->br_blockcount), da_old);
5062 got->br_startblock = nullstartblock((int)da_new);
5063 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5064 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5065 break;
5066 case BMAP_RIGHT_CONTIG:
5068 * Deleting the last part of the extent.
5070 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5071 got->br_blockcount = got->br_blockcount - del->br_blockcount;
5072 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
5073 got->br_blockcount), da_old);
5074 got->br_startblock = nullstartblock((int)da_new);
5075 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5076 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5077 break;
5078 case 0:
5080 * Deleting the middle of the extent.
5082 * Distribute the original indlen reservation across the two new
5083 * extents. Steal blocks from the deleted extent if necessary.
5084 * Stealing blocks simply fudges the fdblocks accounting below.
5085 * Warn if either of the new indlen reservations is zero as this
5086 * can lead to delalloc problems.
5088 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5090 got->br_blockcount = del->br_startoff - got->br_startoff;
5091 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
5093 new.br_blockcount = got_endoff - del_endoff;
5094 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
5096 WARN_ON_ONCE(!got_indlen || !new_indlen);
5097 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
5098 del->br_blockcount);
5100 got->br_startblock = nullstartblock((int)got_indlen);
5101 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5102 trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
5104 new.br_startoff = del_endoff;
5105 new.br_state = got->br_state;
5106 new.br_startblock = nullstartblock((int)new_indlen);
5108 ++*idx;
5109 xfs_iext_insert(ip, *idx, 1, &new, state);
5111 da_new = got_indlen + new_indlen - stolen;
5112 del->br_blockcount -= stolen;
5113 break;
5116 ASSERT(da_old >= da_new);
5117 da_diff = da_old - da_new;
5118 if (!isrt)
5119 da_diff += del->br_blockcount;
5120 if (da_diff)
5121 xfs_mod_fdblocks(mp, da_diff, false);
5122 return error;
5125 void
5126 xfs_bmap_del_extent_cow(
5127 struct xfs_inode *ip,
5128 xfs_extnum_t *idx,
5129 struct xfs_bmbt_irec *got,
5130 struct xfs_bmbt_irec *del)
5132 struct xfs_mount *mp = ip->i_mount;
5133 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
5134 struct xfs_bmbt_irec new;
5135 xfs_fileoff_t del_endoff, got_endoff;
5136 int state = BMAP_COWFORK;
5138 XFS_STATS_INC(mp, xs_del_exlist);
5140 del_endoff = del->br_startoff + del->br_blockcount;
5141 got_endoff = got->br_startoff + got->br_blockcount;
5143 ASSERT(*idx >= 0);
5144 ASSERT(*idx <= xfs_iext_count(ifp));
5145 ASSERT(del->br_blockcount > 0);
5146 ASSERT(got->br_startoff <= del->br_startoff);
5147 ASSERT(got_endoff >= del_endoff);
5148 ASSERT(!isnullstartblock(got->br_startblock));
5150 if (got->br_startoff == del->br_startoff)
5151 state |= BMAP_LEFT_CONTIG;
5152 if (got_endoff == del_endoff)
5153 state |= BMAP_RIGHT_CONTIG;
5155 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
5156 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
5158 * Matches the whole extent. Delete the entry.
5160 xfs_iext_remove(ip, *idx, 1, state);
5161 --*idx;
5162 break;
5163 case BMAP_LEFT_CONTIG:
5165 * Deleting the first part of the extent.
5167 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5168 got->br_startoff = del_endoff;
5169 got->br_blockcount -= del->br_blockcount;
5170 got->br_startblock = del->br_startblock + del->br_blockcount;
5171 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5172 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5173 break;
5174 case BMAP_RIGHT_CONTIG:
5176 * Deleting the last part of the extent.
5178 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5179 got->br_blockcount -= del->br_blockcount;
5180 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5181 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5182 break;
5183 case 0:
5185 * Deleting the middle of the extent.
5187 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5188 got->br_blockcount = del->br_startoff - got->br_startoff;
5189 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5190 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5192 new.br_startoff = del_endoff;
5193 new.br_blockcount = got_endoff - del_endoff;
5194 new.br_state = got->br_state;
5195 new.br_startblock = del->br_startblock + del->br_blockcount;
5197 ++*idx;
5198 xfs_iext_insert(ip, *idx, 1, &new, state);
5199 break;
5204 * Called by xfs_bmapi to update file extent records and the btree
5205 * after removing space (or undoing a delayed allocation).
5207 STATIC int /* error */
5208 xfs_bmap_del_extent(
5209 xfs_inode_t *ip, /* incore inode pointer */
5210 xfs_trans_t *tp, /* current transaction pointer */
5211 xfs_extnum_t *idx, /* extent number to update/delete */
5212 struct xfs_defer_ops *dfops, /* list of extents to be freed */
5213 xfs_btree_cur_t *cur, /* if null, not a btree */
5214 xfs_bmbt_irec_t *del, /* data to remove from extents */
5215 int *logflagsp, /* inode logging flags */
5216 int whichfork, /* data or attr fork */
5217 int bflags) /* bmapi flags */
5219 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
5220 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
5221 xfs_fsblock_t del_endblock=0; /* first block past del */
5222 xfs_fileoff_t del_endoff; /* first offset past del */
5223 int delay; /* current block is delayed allocated */
5224 int do_fx; /* free extent at end of routine */
5225 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
5226 int error; /* error return value */
5227 int flags; /* inode logging flags */
5228 xfs_bmbt_irec_t got; /* current extent entry */
5229 xfs_fileoff_t got_endoff; /* first offset past got */
5230 int i; /* temp state */
5231 xfs_ifork_t *ifp; /* inode fork pointer */
5232 xfs_mount_t *mp; /* mount structure */
5233 xfs_filblks_t nblks; /* quota/sb block count */
5234 xfs_bmbt_irec_t new; /* new record to be inserted */
5235 /* REFERENCED */
5236 uint qfield; /* quota field to update */
5237 xfs_filblks_t temp; /* for indirect length calculations */
5238 xfs_filblks_t temp2; /* for indirect length calculations */
5239 int state = 0;
5241 mp = ip->i_mount;
5242 XFS_STATS_INC(mp, xs_del_exlist);
5244 if (whichfork == XFS_ATTR_FORK)
5245 state |= BMAP_ATTRFORK;
5246 else if (whichfork == XFS_COW_FORK)
5247 state |= BMAP_COWFORK;
5249 ifp = XFS_IFORK_PTR(ip, whichfork);
5250 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
5251 ASSERT(del->br_blockcount > 0);
5252 ep = xfs_iext_get_ext(ifp, *idx);
5253 xfs_bmbt_get_all(ep, &got);
5254 ASSERT(got.br_startoff <= del->br_startoff);
5255 del_endoff = del->br_startoff + del->br_blockcount;
5256 got_endoff = got.br_startoff + got.br_blockcount;
5257 ASSERT(got_endoff >= del_endoff);
5258 delay = isnullstartblock(got.br_startblock);
5259 ASSERT(isnullstartblock(del->br_startblock) == delay);
5260 flags = 0;
5261 qfield = 0;
5262 error = 0;
5264 * If deleting a real allocation, must free up the disk space.
5266 if (!delay) {
5267 flags = XFS_ILOG_CORE;
5269 * Realtime allocation. Free it and record di_nblocks update.
5271 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5272 xfs_fsblock_t bno;
5273 xfs_filblks_t len;
5275 ASSERT(do_mod(del->br_blockcount,
5276 mp->m_sb.sb_rextsize) == 0);
5277 ASSERT(do_mod(del->br_startblock,
5278 mp->m_sb.sb_rextsize) == 0);
5279 bno = del->br_startblock;
5280 len = del->br_blockcount;
5281 do_div(bno, mp->m_sb.sb_rextsize);
5282 do_div(len, mp->m_sb.sb_rextsize);
5283 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5284 if (error)
5285 goto done;
5286 do_fx = 0;
5287 nblks = len * mp->m_sb.sb_rextsize;
5288 qfield = XFS_TRANS_DQ_RTBCOUNT;
5291 * Ordinary allocation.
5293 else {
5294 do_fx = 1;
5295 nblks = del->br_blockcount;
5296 qfield = XFS_TRANS_DQ_BCOUNT;
5299 * Set up del_endblock and cur for later.
5301 del_endblock = del->br_startblock + del->br_blockcount;
5302 if (cur) {
5303 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5304 got.br_startblock, got.br_blockcount,
5305 &i)))
5306 goto done;
5307 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5309 da_old = da_new = 0;
5310 } else {
5311 da_old = startblockval(got.br_startblock);
5312 da_new = 0;
5313 nblks = 0;
5314 do_fx = 0;
5318 * Set flag value to use in switch statement.
5319 * Left-contig is 2, right-contig is 1.
5321 switch (((got.br_startoff == del->br_startoff) << 1) |
5322 (got_endoff == del_endoff)) {
5323 case 3:
5325 * Matches the whole extent. Delete the entry.
5327 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5328 xfs_iext_remove(ip, *idx, 1,
5329 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5330 --*idx;
5331 if (delay)
5332 break;
5334 XFS_IFORK_NEXT_SET(ip, whichfork,
5335 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5336 flags |= XFS_ILOG_CORE;
5337 if (!cur) {
5338 flags |= xfs_ilog_fext(whichfork);
5339 break;
5341 if ((error = xfs_btree_delete(cur, &i)))
5342 goto done;
5343 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5344 break;
5346 case 2:
5348 * Deleting the first part of the extent.
5350 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5351 xfs_bmbt_set_startoff(ep, del_endoff);
5352 temp = got.br_blockcount - del->br_blockcount;
5353 xfs_bmbt_set_blockcount(ep, temp);
5354 if (delay) {
5355 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5356 da_old);
5357 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5358 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5359 da_new = temp;
5360 break;
5362 xfs_bmbt_set_startblock(ep, del_endblock);
5363 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5364 if (!cur) {
5365 flags |= xfs_ilog_fext(whichfork);
5366 break;
5368 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
5369 got.br_blockcount - del->br_blockcount,
5370 got.br_state)))
5371 goto done;
5372 break;
5374 case 1:
5376 * Deleting the last part of the extent.
5378 temp = got.br_blockcount - del->br_blockcount;
5379 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5380 xfs_bmbt_set_blockcount(ep, temp);
5381 if (delay) {
5382 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5383 da_old);
5384 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5385 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5386 da_new = temp;
5387 break;
5389 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5390 if (!cur) {
5391 flags |= xfs_ilog_fext(whichfork);
5392 break;
5394 if ((error = xfs_bmbt_update(cur, got.br_startoff,
5395 got.br_startblock,
5396 got.br_blockcount - del->br_blockcount,
5397 got.br_state)))
5398 goto done;
5399 break;
5401 case 0:
5403 * Deleting the middle of the extent.
5405 temp = del->br_startoff - got.br_startoff;
5406 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5407 xfs_bmbt_set_blockcount(ep, temp);
5408 new.br_startoff = del_endoff;
5409 temp2 = got_endoff - del_endoff;
5410 new.br_blockcount = temp2;
5411 new.br_state = got.br_state;
5412 if (!delay) {
5413 new.br_startblock = del_endblock;
5414 flags |= XFS_ILOG_CORE;
5415 if (cur) {
5416 if ((error = xfs_bmbt_update(cur,
5417 got.br_startoff,
5418 got.br_startblock, temp,
5419 got.br_state)))
5420 goto done;
5421 if ((error = xfs_btree_increment(cur, 0, &i)))
5422 goto done;
5423 cur->bc_rec.b = new;
5424 error = xfs_btree_insert(cur, &i);
5425 if (error && error != -ENOSPC)
5426 goto done;
5428 * If get no-space back from btree insert,
5429 * it tried a split, and we have a zero
5430 * block reservation.
5431 * Fix up our state and return the error.
5433 if (error == -ENOSPC) {
5435 * Reset the cursor, don't trust
5436 * it after any insert operation.
5438 if ((error = xfs_bmbt_lookup_eq(cur,
5439 got.br_startoff,
5440 got.br_startblock,
5441 temp, &i)))
5442 goto done;
5443 XFS_WANT_CORRUPTED_GOTO(mp,
5444 i == 1, done);
5446 * Update the btree record back
5447 * to the original value.
5449 if ((error = xfs_bmbt_update(cur,
5450 got.br_startoff,
5451 got.br_startblock,
5452 got.br_blockcount,
5453 got.br_state)))
5454 goto done;
5456 * Reset the extent record back
5457 * to the original value.
5459 xfs_bmbt_set_blockcount(ep,
5460 got.br_blockcount);
5461 flags = 0;
5462 error = -ENOSPC;
5463 goto done;
5465 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5466 } else
5467 flags |= xfs_ilog_fext(whichfork);
5468 XFS_IFORK_NEXT_SET(ip, whichfork,
5469 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5470 } else {
5471 xfs_filblks_t stolen;
5472 ASSERT(whichfork == XFS_DATA_FORK);
5475 * Distribute the original indlen reservation across the
5476 * two new extents. Steal blocks from the deleted extent
5477 * if necessary. Stealing blocks simply fudges the
5478 * fdblocks accounting in xfs_bunmapi().
5480 temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
5481 temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
5482 stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
5483 del->br_blockcount);
5484 da_new = temp + temp2 - stolen;
5485 del->br_blockcount -= stolen;
5488 * Set the reservation for each extent. Warn if either
5489 * is zero as this can lead to delalloc problems.
5491 WARN_ON_ONCE(!temp || !temp2);
5492 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5493 new.br_startblock = nullstartblock((int)temp2);
5495 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5496 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5497 ++*idx;
5498 break;
5501 /* remove reverse mapping */
5502 if (!delay) {
5503 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5504 if (error)
5505 goto done;
5509 * If we need to, add to list of extents to delete.
5511 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
5512 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5513 error = xfs_refcount_decrease_extent(mp, dfops, del);
5514 if (error)
5515 goto done;
5516 } else
5517 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5518 del->br_blockcount, NULL);
5522 * Adjust inode # blocks in the file.
5524 if (nblks)
5525 ip->i_d.di_nblocks -= nblks;
5527 * Adjust quota data.
5529 if (qfield && !(bflags & XFS_BMAPI_REMAP))
5530 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5533 * Account for change in delayed indirect blocks.
5534 * Nothing to do for disk quota accounting here.
5536 ASSERT(da_old >= da_new);
5537 if (da_old > da_new)
5538 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
5539 done:
5540 *logflagsp = flags;
5541 return error;
5545 * Unmap (remove) blocks from a file.
5546 * If nexts is nonzero then the number of extents to remove is limited to
5547 * that value. If not all extents in the block range can be removed then
5548 * *done is set.
5550 int /* error */
5551 __xfs_bunmapi(
5552 xfs_trans_t *tp, /* transaction pointer */
5553 struct xfs_inode *ip, /* incore inode */
5554 xfs_fileoff_t bno, /* starting offset to unmap */
5555 xfs_filblks_t *rlen, /* i/o: amount remaining */
5556 int flags, /* misc flags */
5557 xfs_extnum_t nexts, /* number of extents max */
5558 xfs_fsblock_t *firstblock, /* first allocated block
5559 controls a.g. for allocs */
5560 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
5562 xfs_btree_cur_t *cur; /* bmap btree cursor */
5563 xfs_bmbt_irec_t del; /* extent being deleted */
5564 int eof; /* is deleting at eof */
5565 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
5566 int error; /* error return value */
5567 xfs_extnum_t extno; /* extent number in list */
5568 xfs_bmbt_irec_t got; /* current extent record */
5569 xfs_ifork_t *ifp; /* inode fork pointer */
5570 int isrt; /* freeing in rt area */
5571 xfs_extnum_t lastx; /* last extent index used */
5572 int logflags; /* transaction logging flags */
5573 xfs_extlen_t mod; /* rt extent offset */
5574 xfs_mount_t *mp; /* mount structure */
5575 xfs_bmbt_irec_t prev; /* previous extent record */
5576 xfs_fileoff_t start; /* first file offset deleted */
5577 int tmp_logflags; /* partial logging flags */
5578 int wasdel; /* was a delayed alloc extent */
5579 int whichfork; /* data or attribute fork */
5580 xfs_fsblock_t sum;
5581 xfs_filblks_t len = *rlen; /* length to unmap in file */
5582 xfs_fileoff_t max_len;
5583 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
5585 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5587 whichfork = xfs_bmapi_whichfork(flags);
5588 ASSERT(whichfork != XFS_COW_FORK);
5589 ifp = XFS_IFORK_PTR(ip, whichfork);
5590 if (unlikely(
5591 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5592 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5593 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5594 ip->i_mount);
5595 return -EFSCORRUPTED;
5597 mp = ip->i_mount;
5598 if (XFS_FORCED_SHUTDOWN(mp))
5599 return -EIO;
5601 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5602 ASSERT(len > 0);
5603 ASSERT(nexts >= 0);
5606 * Guesstimate how many blocks we can unmap without running the risk of
5607 * blowing out the transaction with a mix of EFIs and reflink
5608 * adjustments.
5610 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
5611 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5612 else
5613 max_len = len;
5615 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5616 (error = xfs_iread_extents(tp, ip, whichfork)))
5617 return error;
5618 if (xfs_iext_count(ifp) == 0) {
5619 *rlen = 0;
5620 return 0;
5622 XFS_STATS_INC(mp, xs_blk_unmap);
5623 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5624 start = bno;
5625 bno = start + len - 1;
5626 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5627 &prev);
5630 * Check to see if the given block number is past the end of the
5631 * file, back up to the last block if so...
5633 if (eof) {
5634 ep = xfs_iext_get_ext(ifp, --lastx);
5635 xfs_bmbt_get_all(ep, &got);
5636 bno = got.br_startoff + got.br_blockcount - 1;
5638 logflags = 0;
5639 if (ifp->if_flags & XFS_IFBROOT) {
5640 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5641 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5642 cur->bc_private.b.firstblock = *firstblock;
5643 cur->bc_private.b.dfops = dfops;
5644 cur->bc_private.b.flags = 0;
5645 } else
5646 cur = NULL;
5648 if (isrt) {
5650 * Synchronize by locking the bitmap inode.
5652 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
5653 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5654 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5655 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
5658 extno = 0;
5659 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5660 (nexts == 0 || extno < nexts) && max_len > 0) {
5662 * Is the found extent after a hole in which bno lives?
5663 * Just back up to the previous extent, if so.
5665 if (got.br_startoff > bno) {
5666 if (--lastx < 0)
5667 break;
5668 ep = xfs_iext_get_ext(ifp, lastx);
5669 xfs_bmbt_get_all(ep, &got);
5672 * Is the last block of this extent before the range
5673 * we're supposed to delete? If so, we're done.
5675 bno = XFS_FILEOFF_MIN(bno,
5676 got.br_startoff + got.br_blockcount - 1);
5677 if (bno < start)
5678 break;
5680 * Then deal with the (possibly delayed) allocated space
5681 * we found.
5683 ASSERT(ep != NULL);
5684 del = got;
5685 wasdel = isnullstartblock(del.br_startblock);
5688 * Make sure we don't touch multiple AGF headers out of order
5689 * in a single transaction, as that could cause AB-BA deadlocks.
5691 if (!wasdel && !isrt) {
5692 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5693 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5694 break;
5695 prev_agno = agno;
5697 if (got.br_startoff < start) {
5698 del.br_startoff = start;
5699 del.br_blockcount -= start - got.br_startoff;
5700 if (!wasdel)
5701 del.br_startblock += start - got.br_startoff;
5703 if (del.br_startoff + del.br_blockcount > bno + 1)
5704 del.br_blockcount = bno + 1 - del.br_startoff;
5706 /* How much can we safely unmap? */
5707 if (max_len < del.br_blockcount) {
5708 del.br_startoff += del.br_blockcount - max_len;
5709 if (!wasdel)
5710 del.br_startblock += del.br_blockcount - max_len;
5711 del.br_blockcount = max_len;
5714 sum = del.br_startblock + del.br_blockcount;
5715 if (isrt &&
5716 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5718 * Realtime extent not lined up at the end.
5719 * The extent could have been split into written
5720 * and unwritten pieces, or we could just be
5721 * unmapping part of it. But we can't really
5722 * get rid of part of a realtime extent.
5724 if (del.br_state == XFS_EXT_UNWRITTEN ||
5725 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5727 * This piece is unwritten, or we're not
5728 * using unwritten extents. Skip over it.
5730 ASSERT(bno >= mod);
5731 bno -= mod > del.br_blockcount ?
5732 del.br_blockcount : mod;
5733 if (bno < got.br_startoff) {
5734 if (--lastx >= 0)
5735 xfs_bmbt_get_all(xfs_iext_get_ext(
5736 ifp, lastx), &got);
5738 continue;
5741 * It's written, turn it unwritten.
5742 * This is better than zeroing it.
5744 ASSERT(del.br_state == XFS_EXT_NORM);
5745 ASSERT(tp->t_blk_res > 0);
5747 * If this spans a realtime extent boundary,
5748 * chop it back to the start of the one we end at.
5750 if (del.br_blockcount > mod) {
5751 del.br_startoff += del.br_blockcount - mod;
5752 del.br_startblock += del.br_blockcount - mod;
5753 del.br_blockcount = mod;
5755 del.br_state = XFS_EXT_UNWRITTEN;
5756 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5757 whichfork, &lastx, &cur, &del,
5758 firstblock, dfops, &logflags);
5759 if (error)
5760 goto error0;
5761 goto nodelete;
5763 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5765 * Realtime extent is lined up at the end but not
5766 * at the front. We'll get rid of full extents if
5767 * we can.
5769 mod = mp->m_sb.sb_rextsize - mod;
5770 if (del.br_blockcount > mod) {
5771 del.br_blockcount -= mod;
5772 del.br_startoff += mod;
5773 del.br_startblock += mod;
5774 } else if ((del.br_startoff == start &&
5775 (del.br_state == XFS_EXT_UNWRITTEN ||
5776 tp->t_blk_res == 0)) ||
5777 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5779 * Can't make it unwritten. There isn't
5780 * a full extent here so just skip it.
5782 ASSERT(bno >= del.br_blockcount);
5783 bno -= del.br_blockcount;
5784 if (got.br_startoff > bno) {
5785 if (--lastx >= 0) {
5786 ep = xfs_iext_get_ext(ifp,
5787 lastx);
5788 xfs_bmbt_get_all(ep, &got);
5791 continue;
5792 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5794 * This one is already unwritten.
5795 * It must have a written left neighbor.
5796 * Unwrite the killed part of that one and
5797 * try again.
5799 ASSERT(lastx > 0);
5800 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5801 lastx - 1), &prev);
5802 ASSERT(prev.br_state == XFS_EXT_NORM);
5803 ASSERT(!isnullstartblock(prev.br_startblock));
5804 ASSERT(del.br_startblock ==
5805 prev.br_startblock + prev.br_blockcount);
5806 if (prev.br_startoff < start) {
5807 mod = start - prev.br_startoff;
5808 prev.br_blockcount -= mod;
5809 prev.br_startblock += mod;
5810 prev.br_startoff = start;
5812 prev.br_state = XFS_EXT_UNWRITTEN;
5813 lastx--;
5814 error = xfs_bmap_add_extent_unwritten_real(tp,
5815 ip, whichfork, &lastx, &cur,
5816 &prev, firstblock, dfops,
5817 &logflags);
5818 if (error)
5819 goto error0;
5820 goto nodelete;
5821 } else {
5822 ASSERT(del.br_state == XFS_EXT_NORM);
5823 del.br_state = XFS_EXT_UNWRITTEN;
5824 error = xfs_bmap_add_extent_unwritten_real(tp,
5825 ip, whichfork, &lastx, &cur,
5826 &del, firstblock, dfops,
5827 &logflags);
5828 if (error)
5829 goto error0;
5830 goto nodelete;
5835 * If it's the case where the directory code is running
5836 * with no block reservation, and the deleted block is in
5837 * the middle of its extent, and the resulting insert
5838 * of an extent would cause transformation to btree format,
5839 * then reject it. The calling code will then swap
5840 * blocks around instead.
5841 * We have to do this now, rather than waiting for the
5842 * conversion to btree format, since the transaction
5843 * will be dirty.
5845 if (!wasdel && tp->t_blk_res == 0 &&
5846 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5847 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5848 XFS_IFORK_MAXEXT(ip, whichfork) &&
5849 del.br_startoff > got.br_startoff &&
5850 del.br_startoff + del.br_blockcount <
5851 got.br_startoff + got.br_blockcount) {
5852 error = -ENOSPC;
5853 goto error0;
5857 * Unreserve quota and update realtime free space, if
5858 * appropriate. If delayed allocation, update the inode delalloc
5859 * counter now and wait to update the sb counters as
5860 * xfs_bmap_del_extent() might need to borrow some blocks.
5862 if (wasdel) {
5863 ASSERT(startblockval(del.br_startblock) > 0);
5864 if (isrt) {
5865 xfs_filblks_t rtexts;
5867 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5868 do_div(rtexts, mp->m_sb.sb_rextsize);
5869 xfs_mod_frextents(mp, (int64_t)rtexts);
5870 (void)xfs_trans_reserve_quota_nblks(NULL,
5871 ip, -((long)del.br_blockcount), 0,
5872 XFS_QMOPT_RES_RTBLKS);
5873 } else {
5874 (void)xfs_trans_reserve_quota_nblks(NULL,
5875 ip, -((long)del.br_blockcount), 0,
5876 XFS_QMOPT_RES_REGBLKS);
5878 ip->i_delayed_blks -= del.br_blockcount;
5879 if (cur)
5880 cur->bc_private.b.flags |=
5881 XFS_BTCUR_BPRV_WASDEL;
5882 } else if (cur)
5883 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5885 error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
5886 &tmp_logflags, whichfork, flags);
5887 logflags |= tmp_logflags;
5888 if (error)
5889 goto error0;
5891 if (!isrt && wasdel)
5892 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
5894 max_len -= del.br_blockcount;
5895 bno = del.br_startoff - 1;
5896 nodelete:
5898 * If not done go on to the next (previous) record.
5900 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5901 if (lastx >= 0) {
5902 ep = xfs_iext_get_ext(ifp, lastx);
5903 if (xfs_bmbt_get_startoff(ep) > bno) {
5904 if (--lastx >= 0)
5905 ep = xfs_iext_get_ext(ifp,
5906 lastx);
5908 xfs_bmbt_get_all(ep, &got);
5910 extno++;
5913 if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0)
5914 *rlen = 0;
5915 else
5916 *rlen = bno - start + 1;
5919 * Convert to a btree if necessary.
5921 if (xfs_bmap_needs_btree(ip, whichfork)) {
5922 ASSERT(cur == NULL);
5923 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
5924 &cur, 0, &tmp_logflags, whichfork);
5925 logflags |= tmp_logflags;
5926 if (error)
5927 goto error0;
5930 * transform from btree to extents, give it cur
5932 else if (xfs_bmap_wants_extents(ip, whichfork)) {
5933 ASSERT(cur != NULL);
5934 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5935 whichfork);
5936 logflags |= tmp_logflags;
5937 if (error)
5938 goto error0;
5941 * transform from extents to local?
5943 error = 0;
5944 error0:
5946 * Log everything. Do this after conversion, there's no point in
5947 * logging the extent records if we've converted to btree format.
5949 if ((logflags & xfs_ilog_fext(whichfork)) &&
5950 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5951 logflags &= ~xfs_ilog_fext(whichfork);
5952 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5953 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5954 logflags &= ~xfs_ilog_fbroot(whichfork);
5956 * Log inode even in the error case, if the transaction
5957 * is dirty we'll need to shut down the filesystem.
5959 if (logflags)
5960 xfs_trans_log_inode(tp, ip, logflags);
5961 if (cur) {
5962 if (!error) {
5963 *firstblock = cur->bc_private.b.firstblock;
5964 cur->bc_private.b.allocated = 0;
5966 xfs_btree_del_cursor(cur,
5967 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5969 return error;
5972 /* Unmap a range of a file. */
5974 xfs_bunmapi(
5975 xfs_trans_t *tp,
5976 struct xfs_inode *ip,
5977 xfs_fileoff_t bno,
5978 xfs_filblks_t len,
5979 int flags,
5980 xfs_extnum_t nexts,
5981 xfs_fsblock_t *firstblock,
5982 struct xfs_defer_ops *dfops,
5983 int *done)
5985 int error;
5987 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5988 dfops);
5989 *done = (len == 0);
5990 return error;
5994 * Determine whether an extent shift can be accomplished by a merge with the
5995 * extent that precedes the target hole of the shift.
5997 STATIC bool
5998 xfs_bmse_can_merge(
5999 struct xfs_bmbt_irec *left, /* preceding extent */
6000 struct xfs_bmbt_irec *got, /* current extent to shift */
6001 xfs_fileoff_t shift) /* shift fsb */
6003 xfs_fileoff_t startoff;
6005 startoff = got->br_startoff - shift;
6008 * The extent, once shifted, must be adjacent in-file and on-disk with
6009 * the preceding extent.
6011 if ((left->br_startoff + left->br_blockcount != startoff) ||
6012 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
6013 (left->br_state != got->br_state) ||
6014 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
6015 return false;
6017 return true;
6021 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
6022 * hole in the file. If an extent shift would result in the extent being fully
6023 * adjacent to the extent that currently precedes the hole, we can merge with
6024 * the preceding extent rather than do the shift.
6026 * This function assumes the caller has verified a shift-by-merge is possible
6027 * with the provided extents via xfs_bmse_can_merge().
6029 STATIC int
6030 xfs_bmse_merge(
6031 struct xfs_inode *ip,
6032 int whichfork,
6033 xfs_fileoff_t shift, /* shift fsb */
6034 int current_ext, /* idx of gotp */
6035 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
6036 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
6037 struct xfs_btree_cur *cur,
6038 int *logflags) /* output */
6040 struct xfs_bmbt_irec got;
6041 struct xfs_bmbt_irec left;
6042 xfs_filblks_t blockcount;
6043 int error, i;
6044 struct xfs_mount *mp = ip->i_mount;
6046 xfs_bmbt_get_all(gotp, &got);
6047 xfs_bmbt_get_all(leftp, &left);
6048 blockcount = left.br_blockcount + got.br_blockcount;
6050 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
6051 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6052 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
6055 * Merge the in-core extents. Note that the host record pointers and
6056 * current_ext index are invalid once the extent has been removed via
6057 * xfs_iext_remove().
6059 xfs_bmbt_set_blockcount(leftp, blockcount);
6060 xfs_iext_remove(ip, current_ext, 1, 0);
6063 * Update the on-disk extent count, the btree if necessary and log the
6064 * inode.
6066 XFS_IFORK_NEXT_SET(ip, whichfork,
6067 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
6068 *logflags |= XFS_ILOG_CORE;
6069 if (!cur) {
6070 *logflags |= XFS_ILOG_DEXT;
6071 return 0;
6074 /* lookup and remove the extent to merge */
6075 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
6076 got.br_blockcount, &i);
6077 if (error)
6078 return error;
6079 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
6081 error = xfs_btree_delete(cur, &i);
6082 if (error)
6083 return error;
6084 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
6086 /* lookup and update size of the previous extent */
6087 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
6088 left.br_blockcount, &i);
6089 if (error)
6090 return error;
6091 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
6093 left.br_blockcount = blockcount;
6095 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
6096 left.br_blockcount, left.br_state);
6100 * Shift a single extent.
6102 STATIC int
6103 xfs_bmse_shift_one(
6104 struct xfs_inode *ip,
6105 int whichfork,
6106 xfs_fileoff_t offset_shift_fsb,
6107 int *current_ext,
6108 struct xfs_bmbt_rec_host *gotp,
6109 struct xfs_btree_cur *cur,
6110 int *logflags,
6111 enum shift_direction direction,
6112 struct xfs_defer_ops *dfops)
6114 struct xfs_ifork *ifp;
6115 struct xfs_mount *mp;
6116 xfs_fileoff_t startoff;
6117 struct xfs_bmbt_rec_host *adj_irecp;
6118 struct xfs_bmbt_irec got;
6119 struct xfs_bmbt_irec adj_irec;
6120 int error;
6121 int i;
6122 int total_extents;
6124 mp = ip->i_mount;
6125 ifp = XFS_IFORK_PTR(ip, whichfork);
6126 total_extents = xfs_iext_count(ifp);
6128 xfs_bmbt_get_all(gotp, &got);
6130 /* delalloc extents should be prevented by caller */
6131 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
6133 if (direction == SHIFT_LEFT) {
6134 startoff = got.br_startoff - offset_shift_fsb;
6137 * Check for merge if we've got an extent to the left,
6138 * otherwise make sure there's enough room at the start
6139 * of the file for the shift.
6141 if (!*current_ext) {
6142 if (got.br_startoff < offset_shift_fsb)
6143 return -EINVAL;
6144 goto update_current_ext;
6147 * grab the left extent and check for a large
6148 * enough hole.
6150 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
6151 xfs_bmbt_get_all(adj_irecp, &adj_irec);
6153 if (startoff <
6154 adj_irec.br_startoff + adj_irec.br_blockcount)
6155 return -EINVAL;
6157 /* check whether to merge the extent or shift it down */
6158 if (xfs_bmse_can_merge(&adj_irec, &got,
6159 offset_shift_fsb)) {
6160 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
6161 *current_ext, gotp, adj_irecp,
6162 cur, logflags);
6163 if (error)
6164 return error;
6165 adj_irec = got;
6166 goto update_rmap;
6168 } else {
6169 startoff = got.br_startoff + offset_shift_fsb;
6170 /* nothing to move if this is the last extent */
6171 if (*current_ext >= (total_extents - 1))
6172 goto update_current_ext;
6174 * If this is not the last extent in the file, make sure there
6175 * is enough room between current extent and next extent for
6176 * accommodating the shift.
6178 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
6179 xfs_bmbt_get_all(adj_irecp, &adj_irec);
6180 if (startoff + got.br_blockcount > adj_irec.br_startoff)
6181 return -EINVAL;
6183 * Unlike a left shift (which involves a hole punch),
6184 * a right shift does not modify extent neighbors
6185 * in any way. We should never find mergeable extents
6186 * in this scenario. Check anyways and warn if we
6187 * encounter two extents that could be one.
6189 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
6190 WARN_ON_ONCE(1);
6193 * Increment the extent index for the next iteration, update the start
6194 * offset of the in-core extent and update the btree if applicable.
6196 update_current_ext:
6197 if (direction == SHIFT_LEFT)
6198 (*current_ext)++;
6199 else
6200 (*current_ext)--;
6201 xfs_bmbt_set_startoff(gotp, startoff);
6202 *logflags |= XFS_ILOG_CORE;
6203 adj_irec = got;
6204 if (!cur) {
6205 *logflags |= XFS_ILOG_DEXT;
6206 goto update_rmap;
6209 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
6210 got.br_blockcount, &i);
6211 if (error)
6212 return error;
6213 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
6215 got.br_startoff = startoff;
6216 error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
6217 got.br_blockcount, got.br_state);
6218 if (error)
6219 return error;
6221 update_rmap:
6222 /* update reverse mapping */
6223 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec);
6224 if (error)
6225 return error;
6226 adj_irec.br_startoff = startoff;
6227 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec);
6231 * Shift extent records to the left/right to cover/create a hole.
6233 * The maximum number of extents to be shifted in a single operation is
6234 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
6235 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
6236 * is the length by which each extent is shifted. If there is no hole to shift
6237 * the extents into, this will be considered invalid operation and we abort
6238 * immediately.
6241 xfs_bmap_shift_extents(
6242 struct xfs_trans *tp,
6243 struct xfs_inode *ip,
6244 xfs_fileoff_t *next_fsb,
6245 xfs_fileoff_t offset_shift_fsb,
6246 int *done,
6247 xfs_fileoff_t stop_fsb,
6248 xfs_fsblock_t *firstblock,
6249 struct xfs_defer_ops *dfops,
6250 enum shift_direction direction,
6251 int num_exts)
6253 struct xfs_btree_cur *cur = NULL;
6254 struct xfs_bmbt_rec_host *gotp;
6255 struct xfs_bmbt_irec got;
6256 struct xfs_mount *mp = ip->i_mount;
6257 struct xfs_ifork *ifp;
6258 xfs_extnum_t nexts = 0;
6259 xfs_extnum_t current_ext;
6260 xfs_extnum_t total_extents;
6261 xfs_extnum_t stop_extent;
6262 int error = 0;
6263 int whichfork = XFS_DATA_FORK;
6264 int logflags = 0;
6266 if (unlikely(XFS_TEST_ERROR(
6267 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6268 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6269 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6270 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6271 XFS_ERRLEVEL_LOW, mp);
6272 return -EFSCORRUPTED;
6275 if (XFS_FORCED_SHUTDOWN(mp))
6276 return -EIO;
6278 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
6279 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6280 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
6281 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
6283 ifp = XFS_IFORK_PTR(ip, whichfork);
6284 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6285 /* Read in all the extents */
6286 error = xfs_iread_extents(tp, ip, whichfork);
6287 if (error)
6288 return error;
6291 if (ifp->if_flags & XFS_IFBROOT) {
6292 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6293 cur->bc_private.b.firstblock = *firstblock;
6294 cur->bc_private.b.dfops = dfops;
6295 cur->bc_private.b.flags = 0;
6299 * There may be delalloc extents in the data fork before the range we
6300 * are collapsing out, so we cannot use the count of real extents here.
6301 * Instead we have to calculate it from the incore fork.
6303 total_extents = xfs_iext_count(ifp);
6304 if (total_extents == 0) {
6305 *done = 1;
6306 goto del_cursor;
6310 * In case of first right shift, we need to initialize next_fsb
6312 if (*next_fsb == NULLFSBLOCK) {
6313 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
6314 xfs_bmbt_get_all(gotp, &got);
6315 *next_fsb = got.br_startoff;
6316 if (stop_fsb > *next_fsb) {
6317 *done = 1;
6318 goto del_cursor;
6322 /* Lookup the extent index at which we have to stop */
6323 if (direction == SHIFT_RIGHT) {
6324 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
6325 /* Make stop_extent exclusive of shift range */
6326 stop_extent--;
6327 } else
6328 stop_extent = total_extents;
6331 * Look up the extent index for the fsb where we start shifting. We can
6332 * henceforth iterate with current_ext as extent list changes are locked
6333 * out via ilock.
6335 * gotp can be null in 2 cases: 1) if there are no extents or 2)
6336 * *next_fsb lies in a hole beyond which there are no extents. Either
6337 * way, we are done.
6339 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, &current_ext);
6340 if (!gotp) {
6341 *done = 1;
6342 goto del_cursor;
6345 /* some sanity checking before we finally start shifting extents */
6346 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
6347 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
6348 error = -EIO;
6349 goto del_cursor;
6352 while (nexts++ < num_exts) {
6353 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
6354 &current_ext, gotp, cur, &logflags,
6355 direction, dfops);
6356 if (error)
6357 goto del_cursor;
6359 * If there was an extent merge during the shift, the extent
6360 * count can change. Update the total and grade the next record.
6362 if (direction == SHIFT_LEFT) {
6363 total_extents = xfs_iext_count(ifp);
6364 stop_extent = total_extents;
6367 if (current_ext == stop_extent) {
6368 *done = 1;
6369 *next_fsb = NULLFSBLOCK;
6370 break;
6372 gotp = xfs_iext_get_ext(ifp, current_ext);
6375 if (!*done) {
6376 xfs_bmbt_get_all(gotp, &got);
6377 *next_fsb = got.br_startoff;
6380 del_cursor:
6381 if (cur)
6382 xfs_btree_del_cursor(cur,
6383 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6385 if (logflags)
6386 xfs_trans_log_inode(tp, ip, logflags);
6388 return error;
6392 * Splits an extent into two extents at split_fsb block such that it is
6393 * the first block of the current_ext. @current_ext is a target extent
6394 * to be split. @split_fsb is a block where the extents is split.
6395 * If split_fsb lies in a hole or the first block of extents, just return 0.
6397 STATIC int
6398 xfs_bmap_split_extent_at(
6399 struct xfs_trans *tp,
6400 struct xfs_inode *ip,
6401 xfs_fileoff_t split_fsb,
6402 xfs_fsblock_t *firstfsb,
6403 struct xfs_defer_ops *dfops)
6405 int whichfork = XFS_DATA_FORK;
6406 struct xfs_btree_cur *cur = NULL;
6407 struct xfs_bmbt_rec_host *gotp;
6408 struct xfs_bmbt_irec got;
6409 struct xfs_bmbt_irec new; /* split extent */
6410 struct xfs_mount *mp = ip->i_mount;
6411 struct xfs_ifork *ifp;
6412 xfs_fsblock_t gotblkcnt; /* new block count for got */
6413 xfs_extnum_t current_ext;
6414 int error = 0;
6415 int logflags = 0;
6416 int i = 0;
6418 if (unlikely(XFS_TEST_ERROR(
6419 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6420 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6421 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6422 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6423 XFS_ERRLEVEL_LOW, mp);
6424 return -EFSCORRUPTED;
6427 if (XFS_FORCED_SHUTDOWN(mp))
6428 return -EIO;
6430 ifp = XFS_IFORK_PTR(ip, whichfork);
6431 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6432 /* Read in all the extents */
6433 error = xfs_iread_extents(tp, ip, whichfork);
6434 if (error)
6435 return error;
6439 * gotp can be null in 2 cases: 1) if there are no extents
6440 * or 2) split_fsb lies in a hole beyond which there are
6441 * no extents. Either way, we are done.
6443 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, &current_ext);
6444 if (!gotp)
6445 return 0;
6447 xfs_bmbt_get_all(gotp, &got);
6450 * Check split_fsb lies in a hole or the start boundary offset
6451 * of the extent.
6453 if (got.br_startoff >= split_fsb)
6454 return 0;
6456 gotblkcnt = split_fsb - got.br_startoff;
6457 new.br_startoff = split_fsb;
6458 new.br_startblock = got.br_startblock + gotblkcnt;
6459 new.br_blockcount = got.br_blockcount - gotblkcnt;
6460 new.br_state = got.br_state;
6462 if (ifp->if_flags & XFS_IFBROOT) {
6463 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6464 cur->bc_private.b.firstblock = *firstfsb;
6465 cur->bc_private.b.dfops = dfops;
6466 cur->bc_private.b.flags = 0;
6467 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
6468 got.br_startblock,
6469 got.br_blockcount,
6470 &i);
6471 if (error)
6472 goto del_cursor;
6473 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6476 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
6477 got.br_blockcount = gotblkcnt;
6479 logflags = XFS_ILOG_CORE;
6480 if (cur) {
6481 error = xfs_bmbt_update(cur, got.br_startoff,
6482 got.br_startblock,
6483 got.br_blockcount,
6484 got.br_state);
6485 if (error)
6486 goto del_cursor;
6487 } else
6488 logflags |= XFS_ILOG_DEXT;
6490 /* Add new extent */
6491 current_ext++;
6492 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6493 XFS_IFORK_NEXT_SET(ip, whichfork,
6494 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6496 if (cur) {
6497 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
6498 new.br_startblock, new.br_blockcount,
6499 &i);
6500 if (error)
6501 goto del_cursor;
6502 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
6503 cur->bc_rec.b.br_state = new.br_state;
6505 error = xfs_btree_insert(cur, &i);
6506 if (error)
6507 goto del_cursor;
6508 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6512 * Convert to a btree if necessary.
6514 if (xfs_bmap_needs_btree(ip, whichfork)) {
6515 int tmp_logflags; /* partial log flag return val */
6517 ASSERT(cur == NULL);
6518 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
6519 &cur, 0, &tmp_logflags, whichfork);
6520 logflags |= tmp_logflags;
6523 del_cursor:
6524 if (cur) {
6525 cur->bc_private.b.allocated = 0;
6526 xfs_btree_del_cursor(cur,
6527 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6530 if (logflags)
6531 xfs_trans_log_inode(tp, ip, logflags);
6532 return error;
6536 xfs_bmap_split_extent(
6537 struct xfs_inode *ip,
6538 xfs_fileoff_t split_fsb)
6540 struct xfs_mount *mp = ip->i_mount;
6541 struct xfs_trans *tp;
6542 struct xfs_defer_ops dfops;
6543 xfs_fsblock_t firstfsb;
6544 int error;
6546 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6547 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6548 if (error)
6549 return error;
6551 xfs_ilock(ip, XFS_ILOCK_EXCL);
6552 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6554 xfs_defer_init(&dfops, &firstfsb);
6556 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
6557 &firstfsb, &dfops);
6558 if (error)
6559 goto out;
6561 error = xfs_defer_finish(&tp, &dfops, NULL);
6562 if (error)
6563 goto out;
6565 return xfs_trans_commit(tp);
6567 out:
6568 xfs_defer_cancel(&dfops);
6569 xfs_trans_cancel(tp);
6570 return error;
6573 /* Deferred mapping is only for real extents in the data fork. */
6574 static bool
6575 xfs_bmap_is_update_needed(
6576 struct xfs_bmbt_irec *bmap)
6578 return bmap->br_startblock != HOLESTARTBLOCK &&
6579 bmap->br_startblock != DELAYSTARTBLOCK;
6582 /* Record a bmap intent. */
6583 static int
6584 __xfs_bmap_add(
6585 struct xfs_mount *mp,
6586 struct xfs_defer_ops *dfops,
6587 enum xfs_bmap_intent_type type,
6588 struct xfs_inode *ip,
6589 int whichfork,
6590 struct xfs_bmbt_irec *bmap)
6592 int error;
6593 struct xfs_bmap_intent *bi;
6595 trace_xfs_bmap_defer(mp,
6596 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6597 type,
6598 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6599 ip->i_ino, whichfork,
6600 bmap->br_startoff,
6601 bmap->br_blockcount,
6602 bmap->br_state);
6604 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6605 INIT_LIST_HEAD(&bi->bi_list);
6606 bi->bi_type = type;
6607 bi->bi_owner = ip;
6608 bi->bi_whichfork = whichfork;
6609 bi->bi_bmap = *bmap;
6611 error = xfs_defer_join(dfops, bi->bi_owner);
6612 if (error) {
6613 kmem_free(bi);
6614 return error;
6617 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6618 return 0;
6621 /* Map an extent into a file. */
6623 xfs_bmap_map_extent(
6624 struct xfs_mount *mp,
6625 struct xfs_defer_ops *dfops,
6626 struct xfs_inode *ip,
6627 struct xfs_bmbt_irec *PREV)
6629 if (!xfs_bmap_is_update_needed(PREV))
6630 return 0;
6632 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6633 XFS_DATA_FORK, PREV);
6636 /* Unmap an extent out of a file. */
6638 xfs_bmap_unmap_extent(
6639 struct xfs_mount *mp,
6640 struct xfs_defer_ops *dfops,
6641 struct xfs_inode *ip,
6642 struct xfs_bmbt_irec *PREV)
6644 if (!xfs_bmap_is_update_needed(PREV))
6645 return 0;
6647 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6648 XFS_DATA_FORK, PREV);
6652 * Process one of the deferred bmap operations. We pass back the
6653 * btree cursor to maintain our lock on the bmapbt between calls.
6656 xfs_bmap_finish_one(
6657 struct xfs_trans *tp,
6658 struct xfs_defer_ops *dfops,
6659 struct xfs_inode *ip,
6660 enum xfs_bmap_intent_type type,
6661 int whichfork,
6662 xfs_fileoff_t startoff,
6663 xfs_fsblock_t startblock,
6664 xfs_filblks_t *blockcount,
6665 xfs_exntst_t state)
6667 struct xfs_bmbt_irec bmap;
6668 int nimaps = 1;
6669 xfs_fsblock_t firstfsb;
6670 int flags = XFS_BMAPI_REMAP;
6671 int error = 0;
6673 bmap.br_startblock = startblock;
6674 bmap.br_startoff = startoff;
6675 bmap.br_blockcount = *blockcount;
6676 bmap.br_state = state;
6679 * firstfsb is tied to the transaction lifetime and is used to
6680 * ensure correct AG locking order and schedule work item
6681 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6682 * to only making one bmap call per transaction, so it should
6683 * be safe to have it as a local variable here.
6685 firstfsb = NULLFSBLOCK;
6687 trace_xfs_bmap_deferred(tp->t_mountp,
6688 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6689 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6690 ip->i_ino, whichfork, startoff, *blockcount, state);
6692 if (whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK)
6693 return -EFSCORRUPTED;
6694 if (whichfork == XFS_ATTR_FORK)
6695 flags |= XFS_BMAPI_ATTRFORK;
6697 if (XFS_TEST_ERROR(false, tp->t_mountp,
6698 XFS_ERRTAG_BMAP_FINISH_ONE,
6699 XFS_RANDOM_BMAP_FINISH_ONE))
6700 return -EIO;
6702 switch (type) {
6703 case XFS_BMAP_MAP:
6704 firstfsb = bmap.br_startblock;
6705 error = xfs_bmapi_write(tp, ip, bmap.br_startoff,
6706 bmap.br_blockcount, flags, &firstfsb,
6707 bmap.br_blockcount, &bmap, &nimaps,
6708 dfops);
6709 *blockcount = 0;
6710 break;
6711 case XFS_BMAP_UNMAP:
6712 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6713 XFS_BMAPI_REMAP, 1, &firstfsb, dfops);
6714 break;
6715 default:
6716 ASSERT(0);
6717 error = -EFSCORRUPTED;
6720 return error;