]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_bmap.c
xfsprogs: Release v6.16.0
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_bmap.c
CommitLineData
37b3b4d6 1// SPDX-License-Identifier: GPL-2.0
2bd0ea18 2/*
5e656dbb 3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
da23017d 4 * All Rights Reserved.
2bd0ea18 5 */
9c799827 6#include "libxfs_priv.h"
b626fb59
DC
7#include "xfs_fs.h"
8#include "xfs_shared.h"
9#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
12#include "xfs_bit.h"
13#include "xfs_sb.h"
14#include "xfs_mount.h"
f944d3d0 15#include "xfs_defer.h"
b626fb59
DC
16#include "xfs_dir2.h"
17#include "xfs_inode.h"
18#include "xfs_btree.h"
19#include "xfs_trans.h"
20#include "xfs_alloc.h"
21#include "xfs_bmap.h"
22#include "xfs_bmap_btree.h"
2cf10e4c 23#include "xfs_errortag.h"
b626fb59
DC
24#include "xfs_trans_space.h"
25#include "xfs_trace.h"
26#include "xfs_attr_leaf.h"
27#include "xfs_quota_defs.h"
85aec44f 28#include "xfs_rmap.h"
f93d2173 29#include "xfs_ag.h"
cf8ce220 30#include "xfs_ag_resv.h"
cfe32f0d 31#include "xfs_refcount.h"
a8616431 32#include "xfs_rtbitmap.h"
6d5b5846 33#include "xfs_health.h"
9e471add 34#include "defer_item.h"
ce160fd9 35#include "xfs_symlink_remote.h"
fadb819b 36#include "xfs_inode_util.h"
15fb060e 37#include "xfs_rtgroup.h"
b626fb59 38
1577541c 39struct kmem_cache *xfs_bmap_intent_cache;
5e656dbb
BN
40
41/*
49f693fa 42 * Miscellaneous helper functions
5e656dbb 43 */
5e656dbb 44
5e656dbb 45/*
49f693fa
DC
46 * Compute and fill in the value of the maximum depth of a bmap btree
47 * in this filesystem. Done once, during mount.
5e656dbb 48 */
49f693fa
DC
49void
50xfs_bmap_compute_maxlevels(
51 xfs_mount_t *mp, /* file system mount structure */
52 int whichfork) /* data or attr fork */
53{
32b5fe85 54 uint64_t maxblocks; /* max blocks at this level */
3a2414fa 55 xfs_extnum_t maxleafents; /* max leaf entries possible */
32b5fe85 56 int level; /* btree level */
49f693fa
DC
57 int maxrootrecs; /* max records in root block */
58 int minleafrecs; /* min records in leaf block */
59 int minnoderecs; /* min records in node block */
60 int sz; /* root block size */
5e656dbb 61
49f693fa 62 /*
5a8b4d6a
CB
63 * The maximum number of extents in a fork, hence the maximum number of
64 * leaf entries, is controlled by the size of the on-disk extent count.
49f693fa 65 *
073f2424
CH
66 * Note that we can no longer assume that if we are in ATTR1 that the
67 * fork offset of all the inodes will be
68 * (xfs_default_attroffset(ip) >> 3) because we could have mounted with
69 * ATTR2 and then mounted back with ATTR1, keeping the i_forkoff's fixed
70 * but probably at various positions. Therefore, for both ATTR1 and
71 * ATTR2 we have to assume the worst case scenario of a minimum size
72 * available.
49f693fa 73 */
5a8b4d6a
CB
74 maxleafents = xfs_iext_max_nextents(xfs_has_large_extent_counts(mp),
75 whichfork);
099e5eb3 76 if (whichfork == XFS_DATA_FORK)
2f8e9b0a 77 sz = xfs_bmdr_space_calc(MINDBTPTRS);
099e5eb3 78 else
2f8e9b0a 79 sz = xfs_bmdr_space_calc(MINABTPTRS);
099e5eb3 80
ff105f75 81 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
49f693fa
DC
82 minleafrecs = mp->m_bmap_dmnr[0];
83 minnoderecs = mp->m_bmap_dmnr[1];
4b85994a 84 maxblocks = howmany_64(maxleafents, minleafrecs);
49f693fa
DC
85 for (level = 1; maxblocks > 1; level++) {
86 if (maxblocks <= maxrootrecs)
87 maxblocks = 1;
88 else
32b5fe85 89 maxblocks = howmany_64(maxblocks, minnoderecs);
49f693fa
DC
90 }
91 mp->m_bm_maxlevels[whichfork] = level;
441815c7 92 ASSERT(mp->m_bm_maxlevels[whichfork] <= xfs_bmbt_maxlevels_ondisk());
49f693fa 93}
5e656dbb 94
4acdeb81
DC
95unsigned int
96xfs_bmap_compute_attr_offset(
97 struct xfs_mount *mp)
98{
99 if (mp->m_sb.sb_inodesize == 256)
2f8e9b0a
DW
100 return XFS_LITINO(mp) - xfs_bmdr_space_calc(MINABTPTRS);
101 return xfs_bmdr_space_calc(6 * MINABTPTRS);
4acdeb81
DC
102}
103
b194c7d8
BN
104STATIC int /* error */
105xfs_bmbt_lookup_eq(
106 struct xfs_btree_cur *cur,
70a93110 107 struct xfs_bmbt_irec *irec,
b194c7d8
BN
108 int *stat) /* success/failure */
109{
70a93110 110 cur->bc_rec.b = *irec;
b194c7d8
BN
111 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
112}
113
114STATIC int /* error */
4f76f49c 115xfs_bmbt_lookup_first(
b194c7d8 116 struct xfs_btree_cur *cur,
b194c7d8
BN
117 int *stat) /* success/failure */
118{
4f76f49c
CH
119 cur->bc_rec.b.br_startoff = 0;
120 cur->bc_rec.b.br_startblock = 0;
121 cur->bc_rec.b.br_blockcount = 0;
b194c7d8
BN
122 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
123}
124
125/*
a2ceac1f
DC
126 * Check if the inode needs to be converted to btree format.
127 */
128static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
129{
722e81c1 130 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
87c472b7 131
1277a5e0 132 return whichfork != XFS_COW_FORK &&
d967a68d 133 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
87c472b7 134 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork);
a2ceac1f
DC
135}
136
137/*
138 * Check if the inode should be converted to extent format.
139 */
140static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
141{
722e81c1 142 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
87c472b7 143
1277a5e0 144 return whichfork != XFS_COW_FORK &&
d967a68d 145 ifp->if_format == XFS_DINODE_FMT_BTREE &&
87c472b7 146 ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork);
a2ceac1f
DC
147}
148
149/*
d0e5f1ff 150 * Update the record referred to by cur to the value given by irec
b194c7d8
BN
151 * This either works (return 0) or gets an EFSCORRUPTED error.
152 */
153STATIC int
154xfs_bmbt_update(
155 struct xfs_btree_cur *cur,
d0e5f1ff 156 struct xfs_bmbt_irec *irec)
b194c7d8
BN
157{
158 union xfs_btree_rec rec;
159
d0e5f1ff 160 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
b194c7d8
BN
161 return xfs_btree_update(cur, &rec);
162}
163
5e656dbb 164/*
49f693fa
DC
165 * Compute the worst-case number of indirect blocks that will be used
166 * for ip's delayed extent of length "len".
5e656dbb 167 */
b5e9bf4e 168xfs_filblks_t
49f693fa 169xfs_bmap_worst_indlen(
b5e9bf4e
CH
170 struct xfs_inode *ip, /* incore inode pointer */
171 xfs_filblks_t len) /* delayed extent length */
57c9fccb 172{
b5e9bf4e
CH
173 struct xfs_mount *mp = ip->i_mount;
174 int maxrecs = mp->m_bmap_dmxr[0];
175 int level;
176 xfs_filblks_t rval;
57c9fccb 177
49f693fa
DC
178 for (level = 0, rval = 0;
179 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
180 level++) {
181 len += maxrecs - 1;
182 do_div(len, maxrecs);
183 rval += len;
7fbe9b54
DW
184 if (len == 1)
185 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
49f693fa
DC
186 level - 1;
187 if (level == 0)
188 maxrecs = mp->m_bmap_dmxr[1];
57c9fccb 189 }
49f693fa 190 return rval;
57c9fccb
NS
191}
192
193/*
49f693fa 194 * Calculate the default attribute fork offset for newly created inodes.
57c9fccb 195 */
49f693fa
DC
196uint
197xfs_default_attroffset(
198 struct xfs_inode *ip)
57c9fccb 199{
c06c9d5a
DC
200 if (ip->i_df.if_format == XFS_DINODE_FMT_DEV)
201 return roundup(sizeof(xfs_dev_t), 8);
4acdeb81 202 return M_IGEO(ip->i_mount)->attr_fork_offset;
57c9fccb
NS
203}
204
205/*
073f2424
CH
206 * Helper routine to reset inode i_forkoff field when switching attribute fork
207 * from local to extent format - we reset it where possible to make space
208 * available for inline data fork extents.
57c9fccb 209 */
49f693fa
DC
210STATIC void
211xfs_bmap_forkoff_reset(
49f693fa
DC
212 xfs_inode_t *ip,
213 int whichfork)
57c9fccb 214{
49f693fa 215 if (whichfork == XFS_ATTR_FORK &&
d967a68d
CH
216 ip->i_df.if_format != XFS_DINODE_FMT_DEV &&
217 ip->i_df.if_format != XFS_DINODE_FMT_BTREE) {
49f693fa 218 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
57c9fccb 219
073f2424
CH
220 if (dfl_forkoff > ip->i_forkoff)
221 ip->i_forkoff = dfl_forkoff;
49f693fa 222 }
57c9fccb
NS
223}
224
b4b20c27
CH
225static int
226xfs_bmap_read_buf(
227 struct xfs_mount *mp, /* file system mount point */
228 struct xfs_trans *tp, /* transaction pointer */
229 xfs_fsblock_t fsbno, /* file system block number */
230 struct xfs_buf **bpp) /* buffer for fsbno */
231{
232 struct xfs_buf *bp; /* return value */
233 int error;
234
235 if (!xfs_verify_fsbno(mp, fsbno))
236 return -EFSCORRUPTED;
237 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
238 XFS_FSB_TO_DADDR(mp, fsbno), mp->m_bsize, 0, &bp,
239 &xfs_bmbt_buf_ops);
240 if (!error) {
241 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
242 *bpp = bp;
243 }
244 return error;
245}
246
49f693fa
DC
247#ifdef DEBUG
248STATIC struct xfs_buf *
249xfs_bmap_get_bp(
250 struct xfs_btree_cur *cur,
251 xfs_fsblock_t bno)
252{
2fdd378a 253 struct xfs_log_item *lip;
49f693fa 254 int i;
56b2de80 255
49f693fa
DC
256 if (!cur)
257 return NULL;
2bd0ea18 258
100a1b52 259 for (i = 0; i < cur->bc_maxlevels; i++) {
5df9b067 260 if (!cur->bc_levels[i].bp)
49f693fa 261 break;
5df9b067
DW
262 if (xfs_buf_daddr(cur->bc_levels[i].bp) == bno)
263 return cur->bc_levels[i].bp;
49f693fa 264 }
56b2de80 265
49f693fa 266 /* Chase down all the log items to see if the bp is there */
2fdd378a
DC
267 list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
268 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
269
49f693fa 270 if (bip->bli_item.li_type == XFS_LI_BUF &&
d4aaa66b 271 xfs_buf_daddr(bip->bli_buf) == bno)
49f693fa
DC
272 return bip->bli_buf;
273 }
2bd0ea18 274
49f693fa
DC
275 return NULL;
276}
56b2de80 277
49f693fa
DC
278STATIC void
279xfs_check_block(
280 struct xfs_btree_block *block,
281 xfs_mount_t *mp,
282 int root,
283 short sz)
284{
285 int i, j, dmxr;
286 __be64 *pp, *thispa; /* pointer to block address */
287 xfs_bmbt_key_t *prevp, *keyp;
2bd0ea18 288
49f693fa 289 ASSERT(be16_to_cpu(block->bb_level) > 0);
56b2de80 290
49f693fa
DC
291 prevp = NULL;
292 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
293 dmxr = mp->m_bmap_dmxr[0];
2f8e9b0a 294 keyp = xfs_bmbt_key_addr(mp, block, i);
a2ceac1f 295
49f693fa
DC
296 if (prevp) {
297 ASSERT(be64_to_cpu(prevp->br_startoff) <
298 be64_to_cpu(keyp->br_startoff));
299 }
300 prevp = keyp;
2bd0ea18 301
2bd0ea18 302 /*
49f693fa 303 * Compare the block numbers to see if there are dups.
2bd0ea18 304 */
49f693fa 305 if (root)
2f8e9b0a 306 pp = xfs_bmap_broot_ptr_addr(mp, block, i, sz);
49f693fa 307 else
2f8e9b0a 308 pp = xfs_bmbt_ptr_addr(mp, block, i, dmxr);
49f693fa
DC
309
310 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
311 if (root)
2f8e9b0a 312 thispa = xfs_bmap_broot_ptr_addr(mp, block, j, sz);
49f693fa 313 else
2f8e9b0a 314 thispa = xfs_bmbt_ptr_addr(mp, block, j, dmxr);
49f693fa 315 if (*thispa == *pp) {
be98db85 316 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %lld",
49f693fa
DC
317 __func__, j, i,
318 (unsigned long long)be64_to_cpu(*thispa));
4d5e2888 319 xfs_err(mp, "%s: ptrs are equal in node\n",
49f693fa 320 __func__);
4d5e2888 321 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
49f693fa 322 }
2bd0ea18 323 }
49f693fa
DC
324 }
325}
a2ceac1f 326
49f693fa
DC
327/*
328 * Check that the extents for the inode ip are in the right order in all
f07ae2a6
DC
329 * btree leaves. THis becomes prohibitively expensive for large extent count
330 * files, so don't bother with inodes that have more than 10,000 extents in
331 * them. The btree record ordering checks will still be done, so for such large
332 * bmapbt constructs that is going to catch most corruptions.
49f693fa 333 */
49f693fa
DC
334STATIC void
335xfs_bmap_check_leaf_extents(
ec924d04 336 struct xfs_btree_cur *cur, /* btree cursor or null */
49f693fa
DC
337 xfs_inode_t *ip, /* incore inode pointer */
338 int whichfork) /* data or attr fork */
339{
d967a68d 340 struct xfs_mount *mp = ip->i_mount;
722e81c1 341 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
49f693fa
DC
342 struct xfs_btree_block *block; /* current btree block */
343 xfs_fsblock_t bno; /* block # of "block" */
167137fe 344 struct xfs_buf *bp; /* buffer for "block" */
49f693fa
DC
345 int error; /* error return value */
346 xfs_extnum_t i=0, j; /* index into the extents list */
49f693fa 347 int level; /* btree level, for checking */
49f693fa
DC
348 __be64 *pp; /* pointer to block address */
349 xfs_bmbt_rec_t *ep; /* pointer to current extent */
350 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
351 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
352 int bp_release = 0;
353
d967a68d 354 if (ifp->if_format != XFS_DINODE_FMT_BTREE)
49f693fa 355 return;
49f693fa 356
f07ae2a6 357 /* skip large extent count inodes */
87c472b7 358 if (ip->i_df.if_nextents > 10000)
f07ae2a6
DC
359 return;
360
49f693fa 361 bno = NULLFSBLOCK;
49f693fa
DC
362 block = ifp->if_broot;
363 /*
364 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
365 */
366 level = be16_to_cpu(block->bb_level);
367 ASSERT(level > 0);
368 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
2f8e9b0a 369 pp = xfs_bmap_broot_ptr_addr(mp, block, 1, ifp->if_broot_bytes);
49f693fa
DC
370 bno = be64_to_cpu(*pp);
371
5a35bf2c 372 ASSERT(bno != NULLFSBLOCK);
49f693fa
DC
373 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
374 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
375
376 /*
377 * Go down the tree until leaf level is reached, following the first
378 * pointer (leftmost) at each level.
379 */
380 while (level-- > 0) {
381 /* See if buf is in cur first */
382 bp_release = 0;
383 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
384 if (!bp) {
385 bp_release = 1;
b4b20c27 386 error = xfs_bmap_read_buf(mp, NULL, bno, &bp);
e30bad41
DW
387 if (xfs_metadata_is_sick(error))
388 xfs_btree_mark_sick(cur);
2bd0ea18 389 if (error)
49f693fa 390 goto error_norelse;
2bd0ea18 391 }
49f693fa 392 block = XFS_BUF_TO_BLOCK(bp);
49f693fa
DC
393 if (level == 0)
394 break;
2bd0ea18 395
2bd0ea18 396 /*
49f693fa
DC
397 * Check this block for basic sanity (increasing keys and
398 * no duplicate blocks).
2bd0ea18 399 */
49f693fa
DC
400
401 xfs_check_block(block, mp, 0, 0);
2f8e9b0a 402 pp = xfs_bmbt_ptr_addr(mp, block, 1, mp->m_bmap_dmxr[1]);
49f693fa 403 bno = be64_to_cpu(*pp);
fbb4fa7f 404 if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbno(mp, bno))) {
4467a60a 405 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
406 error = -EFSCORRUPTED;
407 goto error0;
408 }
49f693fa
DC
409 if (bp_release) {
410 bp_release = 0;
411 xfs_trans_brelse(NULL, bp);
2bd0ea18 412 }
49f693fa 413 }
a2ceac1f 414
49f693fa
DC
415 /*
416 * Here with bp and block set to the leftmost leaf node in the tree.
417 */
418 i = 0;
a2ceac1f 419
49f693fa
DC
420 /*
421 * Loop over all leaf nodes checking that all extents are in the right order.
422 */
423 for (;;) {
424 xfs_fsblock_t nextbno;
425 xfs_extnum_t num_recs;
426
427
428 num_recs = xfs_btree_get_numrecs(block);
2bd0ea18 429
2bd0ea18 430 /*
49f693fa 431 * Read-ahead the next leaf block, if any.
2bd0ea18 432 */
a2ceac1f 433
49f693fa 434 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
a2ceac1f 435
49f693fa
DC
436 /*
437 * Check all the extents to make sure they are OK.
438 * If we had a previous block, the last entry should
439 * conform with the first entry in this one.
440 */
2bd0ea18 441
2f8e9b0a 442 ep = xfs_bmbt_rec_addr(mp, block, 1);
49f693fa
DC
443 if (i) {
444 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
445 xfs_bmbt_disk_get_blockcount(&last) <=
446 xfs_bmbt_disk_get_startoff(ep));
447 }
448 for (j = 1; j < num_recs; j++) {
2f8e9b0a 449 nextp = xfs_bmbt_rec_addr(mp, block, j + 1);
49f693fa
DC
450 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
451 xfs_bmbt_disk_get_blockcount(ep) <=
452 xfs_bmbt_disk_get_startoff(nextp));
453 ep = nextp;
454 }
455
456 last = *ep;
457 i += num_recs;
458 if (bp_release) {
459 bp_release = 0;
460 xfs_trans_brelse(NULL, bp);
461 }
462 bno = nextbno;
2bd0ea18 463 /*
49f693fa 464 * If we've reached the end, stop.
2bd0ea18 465 */
49f693fa
DC
466 if (bno == NULLFSBLOCK)
467 break;
a2ceac1f 468
49f693fa
DC
469 bp_release = 0;
470 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
471 if (!bp) {
472 bp_release = 1;
b4b20c27 473 error = xfs_bmap_read_buf(mp, NULL, bno, &bp);
e30bad41
DW
474 if (xfs_metadata_is_sick(error))
475 xfs_btree_mark_sick(cur);
a2ceac1f 476 if (error)
49f693fa 477 goto error_norelse;
2bd0ea18 478 }
49f693fa 479 block = XFS_BUF_TO_BLOCK(bp);
a2ceac1f 480 }
4d4a192c 481
49f693fa 482 return;
a2ceac1f 483
49f693fa
DC
484error0:
485 xfs_warn(mp, "%s: at error0", __func__);
486 if (bp_release)
487 xfs_trans_brelse(NULL, bp);
488error_norelse:
4b85994a 489 xfs_warn(mp, "%s: BAD after btree leaves for %llu extents",
49f693fa 490 __func__, i);
4d5e2888
DW
491 xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
492 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
49f693fa 493 return;
2bd0ea18
NS
494}
495
49f693fa
DC
496/*
497 * Validate that the bmbt_irecs being returned from bmapi are valid
e6d77a21
DC
498 * given the caller's original parameters. Specifically check the
499 * ranges of the returned irecs to ensure that they only extend beyond
49f693fa
DC
500 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
501 */
502STATIC void
503xfs_bmap_validate_ret(
504 xfs_fileoff_t bno,
505 xfs_filblks_t len,
6e22af31 506 uint32_t flags,
49f693fa
DC
507 xfs_bmbt_irec_t *mval,
508 int nmap,
509 int ret_nmap)
510{
511 int i; /* index to map values */
a2ceac1f 512
49f693fa 513 ASSERT(ret_nmap <= nmap);
a2ceac1f 514
49f693fa
DC
515 for (i = 0; i < ret_nmap; i++) {
516 ASSERT(mval[i].br_blockcount > 0);
517 if (!(flags & XFS_BMAPI_ENTIRE)) {
518 ASSERT(mval[i].br_startoff >= bno);
519 ASSERT(mval[i].br_blockcount <= len);
520 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
521 bno + len);
522 } else {
523 ASSERT(mval[i].br_startoff < bno + len);
524 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
525 bno);
526 }
527 ASSERT(i == 0 ||
528 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
529 mval[i].br_startoff);
530 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
531 mval[i].br_startblock != HOLESTARTBLOCK);
532 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
533 mval[i].br_state == XFS_EXT_UNWRITTEN);
534 }
535}
56b2de80 536
49f693fa
DC
537#else
538#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
9587a34c 539#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
49f693fa 540#endif /* DEBUG */
56b2de80 541
49f693fa
DC
542/*
543 * Inode fork format manipulation functions
544 */
a2ceac1f 545
49f693fa 546/*
939ebc1a
CH
547 * Convert the inode format to extent format if it currently is in btree format,
548 * but the extent list is small enough that it fits into the extent format.
549 *
550 * Since the extents are already in-core, all we have to do is give up the space
551 * for the btree root and pitch the leaf block.
49f693fa
DC
552 */
553STATIC int /* error */
554xfs_bmap_btree_to_extents(
939ebc1a
CH
555 struct xfs_trans *tp, /* transaction pointer */
556 struct xfs_inode *ip, /* incore inode pointer */
557 struct xfs_btree_cur *cur, /* btree cursor */
49f693fa
DC
558 int *logflagsp, /* inode logging flags */
559 int whichfork) /* data or attr fork */
560{
722e81c1 561 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
939ebc1a
CH
562 struct xfs_mount *mp = ip->i_mount;
563 struct xfs_btree_block *rblock = ifp->if_broot;
49f693fa
DC
564 struct xfs_btree_block *cblock;/* child btree block */
565 xfs_fsblock_t cbno; /* child block number */
167137fe 566 struct xfs_buf *cbp; /* child block's buffer */
49f693fa 567 int error; /* error return value */
49f693fa 568 __be64 *pp; /* ptr to block address */
85aec44f 569 struct xfs_owner_info oinfo;
56b2de80 570
939ebc1a
CH
571 /* check if we actually need the extent format first: */
572 if (!xfs_bmap_wants_extents(ip, whichfork))
573 return 0;
574
575 ASSERT(cur);
1277a5e0 576 ASSERT(whichfork != XFS_COW_FORK);
d967a68d 577 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
49f693fa
DC
578 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
579 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
07037e85 580 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, false) == 1);
939ebc1a 581
2f8e9b0a 582 pp = xfs_bmap_broot_ptr_addr(mp, rblock, 1, ifp->if_broot_bytes);
49f693fa 583 cbno = be64_to_cpu(*pp);
49f693fa 584#ifdef DEBUG
29c1de28 585 if (XFS_IS_CORRUPT(cur->bc_mp, !xfs_verify_fsbno(mp, cbno))) {
4467a60a 586 xfs_btree_mark_sick(cur);
fbb4fa7f 587 return -EFSCORRUPTED;
4467a60a 588 }
49f693fa 589#endif
b4b20c27 590 error = xfs_bmap_read_buf(mp, tp, cbno, &cbp);
e30bad41
DW
591 if (xfs_metadata_is_sick(error))
592 xfs_btree_mark_sick(cur);
49f693fa
DC
593 if (error)
594 return error;
595 cblock = XFS_BUF_TO_BLOCK(cbp);
596 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
597 return error;
cd3e5d3c 598
85aec44f 599 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
ef16737e 600 error = xfs_free_extent_later(cur->bc_tp, cbno, 1, &oinfo,
ad2fb6bc 601 XFS_AG_RESV_NONE, 0);
cd3e5d3c
DC
602 if (error)
603 return error;
604
aa00f286 605 ip->i_nblocks--;
49f693fa
DC
606 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
607 xfs_trans_binval(tp, cbp);
5df9b067
DW
608 if (cur->bc_levels[0].bp == cbp)
609 cur->bc_levels[0].bp = NULL;
5d7c26f3 610 xfs_bmap_broot_realloc(ip, whichfork, 0);
49f693fa 611 ASSERT(ifp->if_broot == NULL);
d967a68d 612 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
939ebc1a 613 *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
49f693fa
DC
614 return 0;
615}
2bd0ea18
NS
616
617/*
49f693fa
DC
618 * Convert an extents-format file into a btree-format file.
619 * The new file will have a root block (in the inode) and a single child block.
2bd0ea18 620 */
49f693fa
DC
621STATIC int /* error */
622xfs_bmap_extents_to_btree(
52f6ed9d
BF
623 struct xfs_trans *tp, /* transaction pointer */
624 struct xfs_inode *ip, /* incore inode pointer */
52f6ed9d 625 struct xfs_btree_cur **curp, /* cursor returned to caller */
49f693fa
DC
626 int wasdel, /* converting a delayed alloc */
627 int *logflagsp, /* inode logging flags */
628 int whichfork) /* data or attr fork */
2bd0ea18 629{
49f693fa 630 struct xfs_btree_block *ablock; /* allocated (child) bt block */
52f6ed9d
BF
631 struct xfs_buf *abp; /* buffer for ablock */
632 struct xfs_alloc_arg args; /* allocation arguments */
633 struct xfs_bmbt_rec *arp; /* child record pointer */
49f693fa 634 struct xfs_btree_block *block; /* btree root block */
52f6ed9d 635 struct xfs_btree_cur *cur; /* bmap btree cursor */
49f693fa 636 int error; /* error return value */
52f6ed9d
BF
637 struct xfs_ifork *ifp; /* inode fork pointer */
638 struct xfs_bmbt_key *kp; /* root block key pointer */
639 struct xfs_mount *mp; /* mount structure */
49f693fa 640 xfs_bmbt_ptr_t *pp; /* root block address pointer */
9788e059 641 struct xfs_iext_cursor icur;
62ed7338 642 struct xfs_bmbt_irec rec;
9788e059 643 xfs_extnum_t cnt = 0;
2bd0ea18 644
5dfa5cd2 645 mp = ip->i_mount;
1277a5e0 646 ASSERT(whichfork != XFS_COW_FORK);
722e81c1 647 ifp = xfs_ifork_ptr(ip, whichfork);
d967a68d 648 ASSERT(ifp->if_format == XFS_DINODE_FMT_EXTENTS);
56b2de80 649
2bd0ea18 650 /*
d7c2f6cb
DC
651 * Make space in the inode incore. This needs to be undone if we fail
652 * to expand the root.
2bd0ea18 653 */
5d7c26f3 654 block = xfs_bmap_broot_realloc(ip, whichfork, 1);
56b2de80 655
2bd0ea18 656 /*
49f693fa 657 * Fill in the root.
2bd0ea18 658 */
8016d20c 659 xfs_bmbt_init_block(ip, block, NULL, 1, 1);
49f693fa
DC
660 /*
661 * Need a cursor. Can't allocate until bb_level is filled in.
662 */
49f693fa 663 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
d242dfc3
CH
664 if (wasdel)
665 cur->bc_flags |= XFS_BTREE_BMBT_WASDEL;
49f693fa
DC
666 /*
667 * Convert to a btree with two levels, one record in root.
668 */
d967a68d 669 ifp->if_format = XFS_DINODE_FMT_BTREE;
49f693fa
DC
670 memset(&args, 0, sizeof(args));
671 args.tp = tp;
672 args.mp = mp;
85aec44f 673 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
80375d24 674
49f693fa
DC
675 args.minlen = args.maxlen = args.prod = 1;
676 args.wasdel = wasdel;
677 *logflagsp = 0;
1a5a98d5
DC
678 error = xfs_alloc_vextent_start_ag(&args,
679 XFS_INO_TO_FSB(mp, ip->i_ino));
d7c2f6cb
DC
680 if (error)
681 goto out_root_realloc;
68d06dce 682
80375d24
DC
683 /*
684 * Allocation can't fail, the space was reserved.
685 */
c1587ecf 686 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
b5c71bcd 687 error = -ENOSPC;
d7c2f6cb 688 goto out_root_realloc;
c1587ecf 689 }
d7c2f6cb 690
6aba4616 691 cur->bc_bmap.allocated++;
aa00f286 692 ip->i_nblocks++;
49f693fa 693 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
78fcd346
DW
694 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
695 XFS_FSB_TO_DADDR(mp, args.fsbno),
696 mp->m_bsize, 0, &abp);
697 if (error)
d7c2f6cb 698 goto out_unreserve_dquot;
d7c2f6cb 699
2bd0ea18 700 /*
49f693fa 701 * Fill in the child block.
2bd0ea18 702 */
49f693fa 703 ablock = XFS_BUF_TO_BLOCK(abp);
8016d20c 704 xfs_bmbt_init_block(ip, ablock, abp, 0, 0);
5dfa5cd2 705
9788e059 706 for_each_xfs_iext(ifp, &icur, &rec) {
62ed7338
CH
707 if (isnullstartblock(rec.br_startblock))
708 continue;
2f8e9b0a 709 arp = xfs_bmbt_rec_addr(mp, ablock, 1 + cnt);
62ed7338
CH
710 xfs_bmbt_disk_set_all(arp, &rec);
711 cnt++;
49f693fa 712 }
87c472b7 713 ASSERT(cnt == ifp->if_nextents);
49f693fa 714 xfs_btree_set_numrecs(ablock, cnt);
56b2de80 715
49f693fa
DC
716 /*
717 * Fill in the root key and pointer.
718 */
2f8e9b0a
DW
719 kp = xfs_bmbt_key_addr(mp, block, 1);
720 arp = xfs_bmbt_rec_addr(mp, ablock, 1);
49f693fa 721 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
2f8e9b0a 722 pp = xfs_bmbt_ptr_addr(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
49f693fa
DC
723 be16_to_cpu(block->bb_level)));
724 *pp = cpu_to_be64(args.fsbno);
2bd0ea18 725
49f693fa
DC
726 /*
727 * Do all this logging at the end so that
728 * the root is at the right level.
729 */
5dfa5cd2 730 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
613e6057 731 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
49f693fa
DC
732 ASSERT(*curp == NULL);
733 *curp = cur;
734 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
735 return 0;
b5c71bcd 736
d7c2f6cb 737out_unreserve_dquot:
b5c71bcd 738 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
d7c2f6cb 739out_root_realloc:
5d7c26f3 740 xfs_bmap_broot_realloc(ip, whichfork, 0);
d967a68d 741 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
d7c2f6cb 742 ASSERT(ifp->if_broot == NULL);
b5c71bcd
SH
743 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
744
745 return error;
49f693fa 746}
a2ceac1f 747
49f693fa
DC
748/*
749 * Convert a local file to an extents file.
750 * This code is out of bounds for data forks of regular files,
751 * since the file data needs to get logged so things will stay consistent.
752 * (The bmap-level manipulations are ok, though).
753 */
3f17ed4b
DC
754void
755xfs_bmap_local_to_extents_empty(
feee8e52 756 struct xfs_trans *tp,
3f17ed4b
DC
757 struct xfs_inode *ip,
758 int whichfork)
759{
722e81c1 760 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
3f17ed4b 761
1277a5e0 762 ASSERT(whichfork != XFS_COW_FORK);
d967a68d 763 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
3f17ed4b 764 ASSERT(ifp->if_bytes == 0);
87c472b7 765 ASSERT(ifp->if_nextents == 0);
3f17ed4b 766
ff105f75 767 xfs_bmap_forkoff_reset(ip, whichfork);
f8146197 768 ifp->if_data = NULL;
b37d753d 769 ifp->if_height = 0;
d967a68d 770 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
feee8e52 771 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3f17ed4b
DC
772}
773
774
7469e993 775int /* error */
49f693fa
DC
776xfs_bmap_local_to_extents(
777 xfs_trans_t *tp, /* transaction pointer */
778 xfs_inode_t *ip, /* incore inode pointer */
49f693fa
DC
779 xfs_extlen_t total, /* total blocks needed by transaction */
780 int *logflagsp, /* inode logging flags */
781 int whichfork,
5dfa5cd2
DC
782 void (*init_fn)(struct xfs_trans *tp,
783 struct xfs_buf *bp,
49f693fa 784 struct xfs_inode *ip,
7469e993
DW
785 struct xfs_ifork *ifp, void *priv),
786 void *priv)
49f693fa 787{
3f17ed4b 788 int error = 0;
49f693fa 789 int flags; /* logging flags returned */
e07055b8 790 struct xfs_ifork *ifp; /* inode fork pointer */
3f17ed4b 791 xfs_alloc_arg_t args; /* allocation arguments */
167137fe 792 struct xfs_buf *bp; /* buffer for extent block */
05104e43 793 struct xfs_bmbt_irec rec;
9788e059 794 struct xfs_iext_cursor icur;
2bd0ea18 795
49f693fa
DC
796 /*
797 * We don't want to deal with the case of keeping inode data inline yet.
798 * So sending the data fork of a regular inode is invalid.
799 */
e37bf53c 800 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
722e81c1 801 ifp = xfs_ifork_ptr(ip, whichfork);
d967a68d 802 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
3f17ed4b
DC
803
804 if (!ifp->if_bytes) {
feee8e52 805 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
3f17ed4b
DC
806 flags = XFS_ILOG_CORE;
807 goto done;
808 }
809
49f693fa
DC
810 flags = 0;
811 error = 0;
3f17ed4b
DC
812 memset(&args, 0, sizeof(args));
813 args.tp = tp;
814 args.mp = ip->i_mount;
04215b9f
DC
815 args.total = total;
816 args.minlen = args.maxlen = args.prod = 1;
85aec44f 817 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
1a5a98d5 818
3f17ed4b
DC
819 /*
820 * Allocate a block. We know we need only one, since the
821 * file currently fits in an inode.
822 */
3f17ed4b
DC
823 args.total = total;
824 args.minlen = args.maxlen = args.prod = 1;
1a5a98d5
DC
825 error = xfs_alloc_vextent_start_ag(&args,
826 XFS_INO_TO_FSB(args.mp, ip->i_ino));
3f17ed4b
DC
827 if (error)
828 goto done;
829
830 /* Can't fail, the space was reserved. */
831 ASSERT(args.fsbno != NULLFSBLOCK);
832 ASSERT(args.len == 1);
78fcd346
DW
833 error = xfs_trans_get_buf(tp, args.mp->m_ddev_targp,
834 XFS_FSB_TO_DADDR(args.mp, args.fsbno),
835 args.mp->m_bsize, 0, &bp);
836 if (error)
837 goto done;
3f17ed4b 838
19ebedcf 839 /*
f44fbde0 840 * Initialize the block, copy the data and log the remote buffer.
19ebedcf 841 *
f44fbde0
BF
842 * The callout is responsible for logging because the remote format
843 * might differ from the local format and thus we don't know how much to
844 * log here. Note that init_fn must also set the buffer log item type
845 * correctly.
19ebedcf 846 */
7469e993 847 init_fn(tp, bp, ip, ifp, priv);
3f17ed4b 848
f44fbde0 849 /* account for the change in fork size */
3f17ed4b 850 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
feee8e52 851 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
49f693fa 852 flags |= XFS_ILOG_CORE;
3f17ed4b 853
f8146197 854 ifp->if_data = NULL;
b37d753d
CH
855 ifp->if_height = 0;
856
05104e43
CH
857 rec.br_startoff = 0;
858 rec.br_startblock = args.fsbno;
859 rec.br_blockcount = 1;
860 rec.br_state = XFS_EXT_NORM;
9788e059 861 xfs_iext_first(ifp, &icur);
26a75f67 862 xfs_iext_insert(ip, &icur, &rec, 0);
05104e43 863
87c472b7 864 ifp->if_nextents = 1;
aa00f286 865 ip->i_nblocks = 1;
80375d24 866 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3f17ed4b
DC
867 flags |= xfs_ilog_fext(whichfork);
868
49f693fa
DC
869done:
870 *logflagsp = flags;
871 return error;
2bd0ea18
NS
872}
873
874/*
49f693fa 875 * Called from xfs_bmap_add_attrfork to handle btree format files.
2bd0ea18 876 */
49f693fa
DC
877STATIC int /* error */
878xfs_bmap_add_attrfork_btree(
879 xfs_trans_t *tp, /* transaction pointer */
880 xfs_inode_t *ip, /* incore inode pointer */
49f693fa 881 int *flags) /* inode logging flags */
2bd0ea18 882{
e0144e40 883 struct xfs_btree_block *block = ip->i_df.if_broot;
ec924d04 884 struct xfs_btree_cur *cur; /* btree cursor */
49f693fa
DC
885 int error; /* error return value */
886 xfs_mount_t *mp; /* file system mount struct */
887 int stat; /* newroot status */
56b2de80 888
49f693fa 889 mp = ip->i_mount;
e0144e40 890
2f8e9b0a 891 if (xfs_bmap_bmdr_space(block) <= xfs_inode_data_fork_size(ip))
49f693fa
DC
892 *flags |= XFS_ILOG_DBROOT;
893 else {
894 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4f76f49c
CH
895 error = xfs_bmbt_lookup_first(cur, &stat);
896 if (error)
49f693fa
DC
897 goto error0;
898 /* must be at least one entry */
fbb4fa7f 899 if (XFS_IS_CORRUPT(mp, stat != 1)) {
4467a60a 900 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
901 error = -EFSCORRUPTED;
902 goto error0;
903 }
49f693fa
DC
904 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
905 goto error0;
906 if (stat == 0) {
907 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
12b53197 908 return -ENOSPC;
49f693fa 909 }
6aba4616 910 cur->bc_bmap.allocated = 0;
49f693fa 911 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
2bd0ea18 912 }
49f693fa
DC
913 return 0;
914error0:
915 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
916 return error;
917}
56b2de80 918
49f693fa
DC
919/*
920 * Called from xfs_bmap_add_attrfork to handle extents format files.
921 */
922STATIC int /* error */
923xfs_bmap_add_attrfork_extents(
52f6ed9d
BF
924 struct xfs_trans *tp, /* transaction pointer */
925 struct xfs_inode *ip, /* incore inode pointer */
49f693fa
DC
926 int *flags) /* inode logging flags */
927{
ec924d04 928 struct xfs_btree_cur *cur; /* bmap btree cursor */
49f693fa
DC
929 int error; /* error return value */
930
87c472b7 931 if (ip->i_df.if_nextents * sizeof(struct xfs_bmbt_rec) <=
eae3e30d 932 xfs_inode_data_fork_size(ip))
49f693fa
DC
933 return 0;
934 cur = NULL;
f7253505
BF
935 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
936 XFS_DATA_FORK);
49f693fa 937 if (cur) {
6aba4616 938 cur->bc_bmap.allocated = 0;
660265b7 939 xfs_btree_del_cursor(cur, error);
2bd0ea18 940 }
49f693fa
DC
941 return error;
942}
56b2de80 943
49f693fa
DC
944/*
945 * Called from xfs_bmap_add_attrfork to handle local format files. Each
946 * different data fork content type needs a different callout to do the
947 * conversion. Some are basic and only require special block initialisation
948 * callouts for the data formating, others (directories) are so specialised they
949 * handle everything themselves.
950 *
951 * XXX (dgc): investigate whether directory conversion can use the generic
952 * formatting callout. It should be possible - it's just a very complex
5dfa5cd2 953 * formatter.
49f693fa
DC
954 */
955STATIC int /* error */
956xfs_bmap_add_attrfork_local(
d9313ca4
BF
957 struct xfs_trans *tp, /* transaction pointer */
958 struct xfs_inode *ip, /* incore inode pointer */
49f693fa
DC
959 int *flags) /* inode logging flags */
960{
d9313ca4 961 struct xfs_da_args dargs; /* args for dir/attr code */
56b2de80 962
eae3e30d 963 if (ip->i_df.if_bytes <= xfs_inode_data_fork_size(ip))
49f693fa 964 return 0;
a2ceac1f 965
e37bf53c 966 if (S_ISDIR(VFS_I(ip)->i_mode)) {
49f693fa 967 memset(&dargs, 0, sizeof(dargs));
ff105f75 968 dargs.geo = ip->i_mount->m_dir_geo;
49f693fa 969 dargs.dp = ip;
ff105f75 970 dargs.total = dargs.geo->fsbcount;
49f693fa
DC
971 dargs.whichfork = XFS_DATA_FORK;
972 dargs.trans = tp;
0e232547 973 dargs.owner = ip->i_ino;
49f693fa
DC
974 return xfs_dir2_sf_to_block(&dargs);
975 }
2bd0ea18 976
e37bf53c 977 if (S_ISLNK(VFS_I(ip)->i_mode))
f7253505 978 return xfs_bmap_local_to_extents(tp, ip, 1, flags,
7469e993
DW
979 XFS_DATA_FORK, xfs_symlink_local_to_remote,
980 NULL);
56b2de80 981
3f17ed4b
DC
982 /* should only be called for types that support local format data */
983 ASSERT(0);
6d5b5846 984 xfs_bmap_mark_sick(ip, XFS_ATTR_FORK);
12b53197 985 return -EFSCORRUPTED;
49f693fa 986}
2bd0ea18 987
652a2133
DC
988/*
989 * Set an inode attr fork offset based on the format of the data fork.
990 */
8a3be25d 991static int
932b563e
AH
992xfs_bmap_set_attrforkoff(
993 struct xfs_inode *ip,
994 int size,
995 int *version)
996{
c06c9d5a
DC
997 int default_size = xfs_default_attroffset(ip) >> 3;
998
d967a68d 999 switch (ip->i_df.if_format) {
932b563e 1000 case XFS_DINODE_FMT_DEV:
c06c9d5a 1001 ip->i_forkoff = default_size;
932b563e
AH
1002 break;
1003 case XFS_DINODE_FMT_LOCAL:
1004 case XFS_DINODE_FMT_EXTENTS:
1005 case XFS_DINODE_FMT_BTREE:
073f2424
CH
1006 ip->i_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1007 if (!ip->i_forkoff)
c06c9d5a 1008 ip->i_forkoff = default_size;
914e2a04 1009 else if (xfs_has_attr2(ip->i_mount) && version)
932b563e
AH
1010 *version = 2;
1011 break;
1012 default:
1013 ASSERT(0);
1014 return -EINVAL;
1015 }
1016
1017 return 0;
1018}
1019
49f693fa 1020/*
c7e81911
DW
1021 * Convert inode from non-attributed to attributed. Caller must hold the
1022 * ILOCK_EXCL and the file cannot have an attr fork.
49f693fa
DC
1023 */
1024int /* error code */
1025xfs_bmap_add_attrfork(
c7e81911
DW
1026 struct xfs_trans *tp,
1027 struct xfs_inode *ip, /* incore inode pointer */
49f693fa
DC
1028 int size, /* space new attribute needs */
1029 int rsvd) /* xact may use reserved blks */
1030{
c7e81911 1031 struct xfs_mount *mp = tp->t_mountp;
49f693fa 1032 int version = 1; /* superblock attr version */
49f693fa
DC
1033 int logflags; /* logging flags */
1034 int error; /* error return value */
56b2de80 1035
c7e81911 1036 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
06778e78 1037 if (!xfs_is_metadir_inode(ip))
52d69572 1038 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
c7e81911 1039 ASSERT(!xfs_inode_has_attr_fork(ip));
a2ceac1f 1040
49f693fa 1041 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
932b563e
AH
1042 error = xfs_bmap_set_attrforkoff(ip, size, &version);
1043 if (error)
c7e81911 1044 return error;
17e074de 1045
7ff5f1ed 1046 xfs_ifork_init_attr(ip, XFS_DINODE_FMT_EXTENTS, 0);
49f693fa 1047 logflags = 0;
d967a68d 1048 switch (ip->i_df.if_format) {
49f693fa 1049 case XFS_DINODE_FMT_LOCAL:
d9313ca4 1050 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
49f693fa
DC
1051 break;
1052 case XFS_DINODE_FMT_EXTENTS:
d9313ca4 1053 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
49f693fa
DC
1054 break;
1055 case XFS_DINODE_FMT_BTREE:
d9313ca4 1056 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
49f693fa
DC
1057 break;
1058 default:
1059 error = 0;
1060 break;
1061 }
1062 if (logflags)
1063 xfs_trans_log_inode(tp, ip, logflags);
1064 if (error)
c7e81911 1065 return error;
b16a427a
DC
1066 if (!xfs_has_attr(mp) ||
1067 (!xfs_has_attr2(mp) && version == 2)) {
19ebedcf 1068 bool log_sb = false;
a2ceac1f 1069
49f693fa 1070 spin_lock(&mp->m_sb_lock);
b16a427a
DC
1071 if (!xfs_has_attr(mp)) {
1072 xfs_add_attr(mp);
19ebedcf 1073 log_sb = true;
49f693fa 1074 }
b16a427a
DC
1075 if (!xfs_has_attr2(mp) && version == 2) {
1076 xfs_add_attr2(mp);
19ebedcf 1077 log_sb = true;
49f693fa 1078 }
19ebedcf
DC
1079 spin_unlock(&mp->m_sb_lock);
1080 if (log_sb)
1081 xfs_log_sb(tp);
49f693fa
DC
1082 }
1083
c7e81911 1084 return 0;
2bd0ea18
NS
1085}
1086
399ab595 1087/*
49f693fa
DC
1088 * Internal and external extent tree search functions.
1089 */
399ab595 1090
9a7ae5a1
DW
1091struct xfs_iread_state {
1092 struct xfs_iext_cursor icur;
1093 xfs_extnum_t loaded;
1094};
1095
830c99b1
DW
1096int
1097xfs_bmap_complain_bad_rec(
1098 struct xfs_inode *ip,
1099 int whichfork,
1100 xfs_failaddr_t fa,
1101 const struct xfs_bmbt_irec *irec)
1102{
1103 struct xfs_mount *mp = ip->i_mount;
1104 const char *forkname;
1105
1106 switch (whichfork) {
1107 case XFS_DATA_FORK: forkname = "data"; break;
1108 case XFS_ATTR_FORK: forkname = "attr"; break;
1109 case XFS_COW_FORK: forkname = "CoW"; break;
1110 default: forkname = "???"; break;
1111 }
1112
1113 xfs_warn(mp,
1114 "Bmap BTree record corruption in inode 0x%llx %s fork detected at %pS!",
1115 ip->i_ino, forkname, fa);
1116 xfs_warn(mp,
1117 "Offset 0x%llx, start block 0x%llx, block count 0x%llx state 0x%x",
1118 irec->br_startoff, irec->br_startblock, irec->br_blockcount,
1119 irec->br_state);
1120
1121 return -EFSCORRUPTED;
1122}
1123
9a7ae5a1
DW
1124/* Stuff every bmbt record from this block into the incore extent map. */
1125static int
1126xfs_iread_bmbt_block(
1127 struct xfs_btree_cur *cur,
1128 int level,
1129 void *priv)
1130{
1131 struct xfs_iread_state *ir = priv;
1132 struct xfs_mount *mp = cur->bc_mp;
116c6a88 1133 struct xfs_inode *ip = cur->bc_ino.ip;
9a7ae5a1
DW
1134 struct xfs_btree_block *block;
1135 struct xfs_buf *bp;
1136 struct xfs_bmbt_rec *frp;
1137 xfs_extnum_t num_recs;
1138 xfs_extnum_t j;
116c6a88 1139 int whichfork = cur->bc_ino.whichfork;
722e81c1 1140 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
9a7ae5a1
DW
1141
1142 block = xfs_btree_get_block(cur, level, &bp);
1143
1144 /* Abort if we find more records than nextents. */
1145 num_recs = xfs_btree_get_numrecs(block);
87c472b7 1146 if (unlikely(ir->loaded + num_recs > ifp->if_nextents)) {
9a7ae5a1
DW
1147 xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).",
1148 (unsigned long long)ip->i_ino);
1149 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block,
1150 sizeof(*block), __this_address);
6d5b5846 1151 xfs_bmap_mark_sick(ip, whichfork);
9a7ae5a1
DW
1152 return -EFSCORRUPTED;
1153 }
1154
1155 /* Copy records into the incore cache. */
2f8e9b0a 1156 frp = xfs_bmbt_rec_addr(mp, block, 1);
9a7ae5a1
DW
1157 for (j = 0; j < num_recs; j++, frp++, ir->loaded++) {
1158 struct xfs_bmbt_irec new;
1159 xfs_failaddr_t fa;
1160
1161 xfs_bmbt_disk_get_all(frp, &new);
1162 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1163 if (fa) {
1164 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1165 "xfs_iread_extents(2)", frp,
1166 sizeof(*frp), fa);
6d5b5846 1167 xfs_bmap_mark_sick(ip, whichfork);
830c99b1
DW
1168 return xfs_bmap_complain_bad_rec(ip, whichfork, fa,
1169 &new);
9a7ae5a1
DW
1170 }
1171 xfs_iext_insert(ip, &ir->icur, &new,
1172 xfs_bmap_fork_to_state(whichfork));
1173 trace_xfs_read_extent(ip, &ir->icur,
1174 xfs_bmap_fork_to_state(whichfork), _THIS_IP_);
87c472b7 1175 xfs_iext_next(ifp, &ir->icur);
9a7ae5a1
DW
1176 }
1177
1178 return 0;
1179}
1180
49f693fa 1181/*
6d79c95c 1182 * Read in extents from a btree-format inode.
49f693fa 1183 */
6d79c95c
CH
1184int
1185xfs_iread_extents(
1186 struct xfs_trans *tp,
1187 struct xfs_inode *ip,
1188 int whichfork)
49f693fa 1189{
9a7ae5a1 1190 struct xfs_iread_state ir;
722e81c1 1191 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
9a7ae5a1
DW
1192 struct xfs_mount *mp = ip->i_mount;
1193 struct xfs_btree_cur *cur;
6d79c95c
CH
1194 int error;
1195
30de9f1c 1196 if (!xfs_need_iread_extents(ifp))
e00c57e7
CH
1197 return 0;
1198
d3d1a2cb 1199 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
6d79c95c 1200
9a7ae5a1
DW
1201 ir.loaded = 0;
1202 xfs_iext_first(ifp, &ir.icur);
1203 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1204 error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block,
1205 XFS_BTREE_VISIT_RECORDS, &ir);
1206 xfs_btree_del_cursor(cur, error);
1207 if (error)
1208 goto out;
6d79c95c 1209
87c472b7 1210 if (XFS_IS_CORRUPT(mp, ir.loaded != ifp->if_nextents)) {
4467a60a 1211 xfs_bmap_mark_sick(ip, whichfork);
6d79c95c
CH
1212 error = -EFSCORRUPTED;
1213 goto out;
1214 }
9a7ae5a1 1215 ASSERT(ir.loaded == xfs_iext_count(ifp));
0bf7f8c3
DW
1216 /*
1217 * Use release semantics so that we can use acquire semantics in
1218 * xfs_need_iread_extents and be guaranteed to see a valid mapping tree
1219 * after that load.
1220 */
1221 smp_store_release(&ifp->if_needextents, 0);
49f693fa 1222 return 0;
6d79c95c 1223out:
6d5b5846
DW
1224 if (xfs_metadata_is_sick(error))
1225 xfs_bmap_mark_sick(ip, whichfork);
6d79c95c
CH
1226 xfs_iext_destroy(ifp);
1227 return error;
49f693fa 1228}
399ab595 1229
49f693fa 1230/*
2d3a6501
CH
1231 * Returns the relative block number of the first unused block(s) in the given
1232 * fork with at least "len" logically contiguous blocks free. This is the
1233 * lowest-address hole if the fork has holes, else the first block past the end
1234 * of fork. Return 0 if the fork is currently local (in-inode).
49f693fa
DC
1235 */
1236int /* error */
1237xfs_bmap_first_unused(
2d3a6501
CH
1238 struct xfs_trans *tp, /* transaction pointer */
1239 struct xfs_inode *ip, /* incore inode */
1240 xfs_extlen_t len, /* size of hole to find */
1241 xfs_fileoff_t *first_unused, /* unused block */
1242 int whichfork) /* data or attr fork */
49f693fa 1243{
722e81c1 1244 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
2d3a6501 1245 struct xfs_bmbt_irec got;
9788e059 1246 struct xfs_iext_cursor icur;
2d3a6501
CH
1247 xfs_fileoff_t lastaddr = 0;
1248 xfs_fileoff_t lowest, max;
1249 int error;
49f693fa 1250
d967a68d 1251 if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
49f693fa
DC
1252 *first_unused = 0;
1253 return 0;
1254 }
f71f3bc3 1255
d967a68d
CH
1256 ASSERT(xfs_ifork_has_extents(ifp));
1257
e00c57e7
CH
1258 error = xfs_iread_extents(tp, ip, whichfork);
1259 if (error)
1260 return error;
f71f3bc3 1261
2d3a6501 1262 lowest = max = *first_unused;
9788e059 1263 for_each_xfs_iext(ifp, &icur, &got) {
2bd0ea18 1264 /*
49f693fa 1265 * See if the hole before this extent will work.
2bd0ea18 1266 */
f71f3bc3 1267 if (got.br_startoff >= lowest + len &&
2d3a6501
CH
1268 got.br_startoff - max >= len)
1269 break;
f71f3bc3 1270 lastaddr = got.br_startoff + got.br_blockcount;
49f693fa
DC
1271 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1272 }
2d3a6501 1273
49f693fa
DC
1274 *first_unused = max;
1275 return 0;
1276}
1277
1278/*
e6d77a21 1279 * Returns the file-relative block number of the last block - 1 before
49f693fa
DC
1280 * last_block (input value) in the file.
1281 * This is not based on i_size, it is based on the extent records.
1282 * Returns 0 for local files, as they do not have extent records.
1283 */
1284int /* error */
1285xfs_bmap_last_before(
b0385364
CH
1286 struct xfs_trans *tp, /* transaction pointer */
1287 struct xfs_inode *ip, /* incore inode */
1288 xfs_fileoff_t *last_block, /* last block */
1289 int whichfork) /* data or attr fork */
49f693fa 1290{
722e81c1 1291 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
b0385364 1292 struct xfs_bmbt_irec got;
9788e059 1293 struct xfs_iext_cursor icur;
b0385364 1294 int error;
49f693fa 1295
d967a68d 1296 switch (ifp->if_format) {
b0385364 1297 case XFS_DINODE_FMT_LOCAL:
49f693fa
DC
1298 *last_block = 0;
1299 return 0;
b0385364
CH
1300 case XFS_DINODE_FMT_BTREE:
1301 case XFS_DINODE_FMT_EXTENTS:
1302 break;
1303 default:
a0264b73 1304 ASSERT(0);
6d5b5846 1305 xfs_bmap_mark_sick(ip, whichfork);
88afc9cc 1306 return -EFSCORRUPTED;
49f693fa 1307 }
b0385364 1308
e00c57e7
CH
1309 error = xfs_iread_extents(tp, ip, whichfork);
1310 if (error)
1311 return error;
b0385364 1312
9788e059 1313 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
d6fbe8fe 1314 *last_block = 0;
49f693fa 1315 return 0;
5e656dbb
BN
1316}
1317
613e6057 1318int
49f693fa
DC
1319xfs_bmap_last_extent(
1320 struct xfs_trans *tp,
1321 struct xfs_inode *ip,
1322 int whichfork,
1323 struct xfs_bmbt_irec *rec,
1324 int *is_empty)
56b2de80 1325{
722e81c1 1326 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
9788e059 1327 struct xfs_iext_cursor icur;
56b2de80
DC
1328 int error;
1329
e00c57e7
CH
1330 error = xfs_iread_extents(tp, ip, whichfork);
1331 if (error)
1332 return error;
49f693fa 1333
9788e059
CH
1334 xfs_iext_last(ifp, &icur);
1335 if (!xfs_iext_get_extent(ifp, &icur, rec))
49f693fa 1336 *is_empty = 1;
9788e059
CH
1337 else
1338 *is_empty = 0;
49f693fa
DC
1339 return 0;
1340}
1341
1342/*
1343 * Check the last inode extent to determine whether this allocation will result
1344 * in blocks being allocated at the end of the file. When we allocate new data
1345 * blocks at the end of the file which do not start at the previous data block,
1346 * we will try to align the new blocks at stripe unit boundaries.
1347 *
ff105f75 1348 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
49f693fa
DC
1349 * at, or past the EOF.
1350 */
1351STATIC int
1352xfs_bmap_isaeof(
1353 struct xfs_bmalloca *bma,
1354 int whichfork)
1355{
1356 struct xfs_bmbt_irec rec;
1357 int is_empty;
1358 int error;
1359
180d3cd8 1360 bma->aeof = false;
49f693fa
DC
1361 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1362 &is_empty);
ff105f75 1363 if (error)
49f693fa 1364 return error;
56b2de80 1365
ff105f75 1366 if (is_empty) {
180d3cd8 1367 bma->aeof = true;
ff105f75
DC
1368 return 0;
1369 }
1370
56b2de80 1371 /*
49f693fa
DC
1372 * Check if we are allocation or past the last extent, or at least into
1373 * the last delayed allocated extent.
56b2de80 1374 */
49f693fa
DC
1375 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1376 (bma->offset >= rec.br_startoff &&
1377 isnullstartblock(rec.br_startblock));
1378 return 0;
1379}
56b2de80 1380
49f693fa
DC
1381/*
1382 * Returns the file-relative block number of the first block past eof in
1383 * the file. This is not based on i_size, it is based on the extent records.
1384 * Returns 0 for local files, as they do not have extent records.
1385 */
1386int
1387xfs_bmap_last_offset(
49f693fa
DC
1388 struct xfs_inode *ip,
1389 xfs_fileoff_t *last_block,
1390 int whichfork)
1391{
722e81c1 1392 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
49f693fa
DC
1393 struct xfs_bmbt_irec rec;
1394 int is_empty;
1395 int error;
1396
1397 *last_block = 0;
1398
d967a68d 1399 if (ifp->if_format == XFS_DINODE_FMT_LOCAL)
49f693fa
DC
1400 return 0;
1401
4467a60a
DW
1402 if (XFS_IS_CORRUPT(ip->i_mount, !xfs_ifork_has_extents(ifp))) {
1403 xfs_bmap_mark_sick(ip, whichfork);
88afc9cc 1404 return -EFSCORRUPTED;
4467a60a 1405 }
49f693fa
DC
1406
1407 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1408 if (error || is_empty)
1409 return error;
1410
1411 *last_block = rec.br_startoff + rec.br_blockcount;
1412 return 0;
1413}
1414
49f693fa
DC
1415/*
1416 * Extent tree manipulation functions used during allocation.
1417 */
1418
15fb060e
CH
1419static inline bool
1420xfs_bmap_same_rtgroup(
1421 struct xfs_inode *ip,
1422 int whichfork,
1423 struct xfs_bmbt_irec *left,
1424 struct xfs_bmbt_irec *right)
1425{
1426 struct xfs_mount *mp = ip->i_mount;
1427
1428 if (xfs_ifork_is_realtime(ip, whichfork) && xfs_has_rtgroups(mp)) {
1429 if (xfs_rtb_to_rgno(mp, left->br_startblock) !=
1430 xfs_rtb_to_rgno(mp, right->br_startblock))
1431 return false;
1432 }
1433
1434 return true;
1435}
1436
49f693fa
DC
1437/*
1438 * Convert a delayed allocation to a real allocation.
1439 */
1440STATIC int /* error */
1441xfs_bmap_add_extent_delay_real(
1277a5e0
DW
1442 struct xfs_bmalloca *bma,
1443 int whichfork)
49f693fa 1444{
87c472b7 1445 struct xfs_mount *mp = bma->ip->i_mount;
722e81c1 1446 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
49f693fa 1447 struct xfs_bmbt_irec *new = &bma->got;
49f693fa
DC
1448 int error; /* error return value */
1449 int i; /* temp state */
49f693fa
DC
1450 xfs_fileoff_t new_endoff; /* end offset of new entry */
1451 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1452 /* left is 0, right is 1, prev is 2 */
1453 int rval=0; /* return value (logging flags) */
7ce54306 1454 uint32_t state = xfs_bmap_fork_to_state(whichfork);
49f693fa
DC
1455 xfs_filblks_t da_new; /* new count del alloc blocks used */
1456 xfs_filblks_t da_old; /* old count del alloc blocks used */
1457 xfs_filblks_t temp=0; /* value for da_new calculations */
49f693fa 1458 int tmp_rval; /* partial logging flags */
19884717 1459 struct xfs_bmbt_irec old;
49f693fa 1460
1277a5e0 1461 ASSERT(whichfork != XFS_ATTR_FORK);
49f693fa 1462 ASSERT(!isnullstartblock(new->br_startblock));
d242dfc3 1463 ASSERT(!bma->cur || (bma->cur->bc_flags & XFS_BTREE_BMBT_WASDEL));
56b2de80 1464
79896434 1465 XFS_STATS_INC(mp, xs_add_exlist);
49f693fa
DC
1466
1467#define LEFT r[0]
1468#define RIGHT r[1]
1469#define PREV r[2]
56b2de80
DC
1470
1471 /*
49f693fa 1472 * Set up a bunch of variables to make the tests simpler.
56b2de80 1473 */
9788e059 1474 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
49f693fa 1475 new_endoff = new->br_startoff + new->br_blockcount;
19884717 1476 ASSERT(isnullstartblock(PREV.br_startblock));
49f693fa
DC
1477 ASSERT(PREV.br_startoff <= new->br_startoff);
1478 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1479
1480 da_old = startblockval(PREV.br_startblock);
1481 da_new = 0;
1482
56b2de80 1483 /*
49f693fa
DC
1484 * Set flags determining what part of the previous delayed allocation
1485 * extent is being replaced by a real allocation.
56b2de80 1486 */
49f693fa
DC
1487 if (PREV.br_startoff == new->br_startoff)
1488 state |= BMAP_LEFT_FILLING;
1489 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1490 state |= BMAP_RIGHT_FILLING;
1491
56b2de80 1492 /*
49f693fa
DC
1493 * Check and set flags if this segment has a left neighbor.
1494 * Don't set contiguous if the combined extent would be too large.
56b2de80 1495 */
9788e059 1496 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
49f693fa 1497 state |= BMAP_LEFT_VALID;
49f693fa
DC
1498 if (isnullstartblock(LEFT.br_startblock))
1499 state |= BMAP_LEFT_DELAY;
1500 }
1501
1502 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1503 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1504 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1505 LEFT.br_state == new->br_state &&
15fb060e
CH
1506 LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
1507 xfs_bmap_same_rtgroup(bma->ip, whichfork, &LEFT, new))
49f693fa 1508 state |= BMAP_LEFT_CONTIG;
56b2de80
DC
1509
1510 /*
49f693fa
DC
1511 * Check and set flags if this segment has a right neighbor.
1512 * Don't set contiguous if the combined extent would be too large.
1513 * Also check for all-three-contiguous being too large.
56b2de80 1514 */
9788e059 1515 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
49f693fa 1516 state |= BMAP_RIGHT_VALID;
49f693fa
DC
1517 if (isnullstartblock(RIGHT.br_startblock))
1518 state |= BMAP_RIGHT_DELAY;
1519 }
56b2de80 1520
49f693fa
DC
1521 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1522 new_endoff == RIGHT.br_startoff &&
1523 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1524 new->br_state == RIGHT.br_state &&
d3e0c71f 1525 new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
49f693fa
DC
1526 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1527 BMAP_RIGHT_FILLING)) !=
1528 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1529 BMAP_RIGHT_FILLING) ||
1530 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
15fb060e
CH
1531 <= XFS_MAX_BMBT_EXTLEN) &&
1532 xfs_bmap_same_rtgroup(bma->ip, whichfork, new, &RIGHT))
49f693fa 1533 state |= BMAP_RIGHT_CONTIG;
5e656dbb 1534
49f693fa
DC
1535 error = 0;
1536 /*
1537 * Switch out based on the FILLING and CONTIG state bits.
1538 */
1539 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1540 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1541 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1542 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1543 /*
1544 * Filling in all of a previously delayed allocation extent.
1545 * The left and right neighbors are both contiguous with new.
1546 */
19884717 1547 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
a2ceac1f 1548
cf455a62
CH
1549 xfs_iext_remove(bma->ip, &bma->icur, state);
1550 xfs_iext_remove(bma->ip, &bma->icur, state);
9788e059
CH
1551 xfs_iext_prev(ifp, &bma->icur);
1552 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
87c472b7 1553 ifp->if_nextents--;
12ebbfe1 1554
49f693fa
DC
1555 if (bma->cur == NULL)
1556 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1557 else {
1558 rval = XFS_ILOG_CORE;
70a93110 1559 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
49f693fa
DC
1560 if (error)
1561 goto done;
fbb4fa7f 1562 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 1563 xfs_btree_mark_sick(bma->cur);
fbb4fa7f
DW
1564 error = -EFSCORRUPTED;
1565 goto done;
1566 }
49f693fa
DC
1567 error = xfs_btree_delete(bma->cur, &i);
1568 if (error)
1569 goto done;
fbb4fa7f 1570 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 1571 xfs_btree_mark_sick(bma->cur);
fbb4fa7f
DW
1572 error = -EFSCORRUPTED;
1573 goto done;
1574 }
49f693fa
DC
1575 error = xfs_btree_decrement(bma->cur, 0, &i);
1576 if (error)
1577 goto done;
fbb4fa7f 1578 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 1579 xfs_btree_mark_sick(bma->cur);
fbb4fa7f
DW
1580 error = -EFSCORRUPTED;
1581 goto done;
1582 }
d0e5f1ff 1583 error = xfs_bmbt_update(bma->cur, &LEFT);
49f693fa
DC
1584 if (error)
1585 goto done;
5e656dbb 1586 }
2c4e8ce7 1587 ASSERT(da_new <= da_old);
49f693fa
DC
1588 break;
1589
1590 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1591 /*
1592 * Filling in all of a previously delayed allocation extent.
1593 * The left neighbor is contiguous, the right is not.
1594 */
19884717 1595 old = LEFT;
19884717 1596 LEFT.br_blockcount += PREV.br_blockcount;
12ebbfe1 1597
cf455a62 1598 xfs_iext_remove(bma->ip, &bma->icur, state);
9788e059
CH
1599 xfs_iext_prev(ifp, &bma->icur);
1600 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
49f693fa 1601
49f693fa
DC
1602 if (bma->cur == NULL)
1603 rval = XFS_ILOG_DEXT;
1604 else {
1605 rval = 0;
70a93110 1606 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
49f693fa
DC
1607 if (error)
1608 goto done;
fbb4fa7f 1609 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 1610 xfs_btree_mark_sick(bma->cur);
fbb4fa7f
DW
1611 error = -EFSCORRUPTED;
1612 goto done;
1613 }
d0e5f1ff 1614 error = xfs_bmbt_update(bma->cur, &LEFT);
49f693fa
DC
1615 if (error)
1616 goto done;
1617 }
2c4e8ce7 1618 ASSERT(da_new <= da_old);
49f693fa
DC
1619 break;
1620
1621 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1622 /*
1623 * Filling in all of a previously delayed allocation extent.
3833299f
DC
1624 * The right neighbor is contiguous, the left is not. Take care
1625 * with delay -> unwritten extent allocation here because the
1626 * delalloc record we are overwriting is always written.
49f693fa 1627 */
19884717
CH
1628 PREV.br_startblock = new->br_startblock;
1629 PREV.br_blockcount += RIGHT.br_blockcount;
3833299f 1630 PREV.br_state = new->br_state;
12ebbfe1 1631
9788e059 1632 xfs_iext_next(ifp, &bma->icur);
cf455a62 1633 xfs_iext_remove(bma->ip, &bma->icur, state);
9788e059
CH
1634 xfs_iext_prev(ifp, &bma->icur);
1635 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
49f693fa 1636
49f693fa
DC
1637 if (bma->cur == NULL)
1638 rval = XFS_ILOG_DEXT;
1639 else {
1640 rval = 0;
70a93110 1641 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
49f693fa
DC
1642 if (error)
1643 goto done;
fbb4fa7f 1644 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 1645 xfs_btree_mark_sick(bma->cur);
fbb4fa7f
DW
1646 error = -EFSCORRUPTED;
1647 goto done;
1648 }
d0e5f1ff 1649 error = xfs_bmbt_update(bma->cur, &PREV);
49f693fa
DC
1650 if (error)
1651 goto done;
1652 }
2c4e8ce7 1653 ASSERT(da_new <= da_old);
49f693fa
DC
1654 break;
1655
1656 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1657 /*
1658 * Filling in all of a previously delayed allocation extent.
1659 * Neither the left nor right neighbors are contiguous with
1660 * the new one.
1661 */
19884717
CH
1662 PREV.br_startblock = new->br_startblock;
1663 PREV.br_state = new->br_state;
9788e059 1664 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
87c472b7 1665 ifp->if_nextents++;
5e656dbb 1666
49f693fa
DC
1667 if (bma->cur == NULL)
1668 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1669 else {
1670 rval = XFS_ILOG_CORE;
70a93110 1671 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
49f693fa
DC
1672 if (error)
1673 goto done;
fbb4fa7f 1674 if (XFS_IS_CORRUPT(mp, i != 0)) {
4467a60a 1675 xfs_btree_mark_sick(bma->cur);
fbb4fa7f
DW
1676 error = -EFSCORRUPTED;
1677 goto done;
1678 }
49f693fa
DC
1679 error = xfs_btree_insert(bma->cur, &i);
1680 if (error)
1681 goto done;
fbb4fa7f 1682 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 1683 xfs_btree_mark_sick(bma->cur);
fbb4fa7f
DW
1684 error = -EFSCORRUPTED;
1685 goto done;
1686 }
49f693fa 1687 }
2c4e8ce7 1688 ASSERT(da_new <= da_old);
49f693fa 1689 break;
5e656dbb 1690
49f693fa
DC
1691 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1692 /*
1693 * Filling in the first part of a previous delayed allocation.
1694 * The left neighbor is contiguous.
1695 */
19884717
CH
1696 old = LEFT;
1697 temp = PREV.br_blockcount - new->br_blockcount;
1698 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1699 startblockval(PREV.br_startblock));
1700
19884717 1701 LEFT.br_blockcount += new->br_blockcount;
a2ceac1f 1702
bc5dec54 1703 PREV.br_blockcount = temp;
19884717
CH
1704 PREV.br_startoff += new->br_blockcount;
1705 PREV.br_startblock = nullstartblock(da_new);
12ebbfe1 1706
9788e059
CH
1707 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1708 xfs_iext_prev(ifp, &bma->icur);
1709 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
19884717 1710
49f693fa
DC
1711 if (bma->cur == NULL)
1712 rval = XFS_ILOG_DEXT;
1713 else {
1714 rval = 0;
70a93110 1715 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
49f693fa
DC
1716 if (error)
1717 goto done;
fbb4fa7f 1718 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 1719 xfs_btree_mark_sick(bma->cur);
fbb4fa7f
DW
1720 error = -EFSCORRUPTED;
1721 goto done;
1722 }
d0e5f1ff 1723 error = xfs_bmbt_update(bma->cur, &LEFT);
49f693fa
DC
1724 if (error)
1725 goto done;
2bd0ea18 1726 }
2c4e8ce7 1727 ASSERT(da_new <= da_old);
49f693fa
DC
1728 break;
1729
1730 case BMAP_LEFT_FILLING:
2bd0ea18 1731 /*
49f693fa
DC
1732 * Filling in the first part of a previous delayed allocation.
1733 * The left neighbor is not contiguous.
5000d01d 1734 */
9788e059 1735 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
87c472b7
CH
1736 ifp->if_nextents++;
1737
49f693fa
DC
1738 if (bma->cur == NULL)
1739 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1740 else {
1741 rval = XFS_ILOG_CORE;
70a93110 1742 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
49f693fa
DC
1743 if (error)
1744 goto done;
fbb4fa7f 1745 if (XFS_IS_CORRUPT(mp, i != 0)) {
4467a60a 1746 xfs_btree_mark_sick(bma->cur);
fbb4fa7f
DW
1747 error = -EFSCORRUPTED;
1748 goto done;
1749 }
49f693fa
DC
1750 error = xfs_btree_insert(bma->cur, &i);
1751 if (error)
1752 goto done;
fbb4fa7f 1753 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 1754 xfs_btree_mark_sick(bma->cur);
fbb4fa7f
DW
1755 error = -EFSCORRUPTED;
1756 goto done;
1757 }
49f693fa
DC
1758 }
1759
36e8786d 1760 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
49f693fa 1761 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
f7253505 1762 &bma->cur, 1, &tmp_rval, whichfork);
49f693fa
DC
1763 rval |= tmp_rval;
1764 if (error)
1765 goto done;
1766 }
19884717
CH
1767
1768 temp = PREV.br_blockcount - new->br_blockcount;
49f693fa
DC
1769 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1770 startblockval(PREV.br_startblock) -
6aba4616 1771 (bma->cur ? bma->cur->bc_bmap.allocated : 0));
19884717 1772
19884717
CH
1773 PREV.br_startoff = new_endoff;
1774 PREV.br_blockcount = temp;
1775 PREV.br_startblock = nullstartblock(da_new);
9788e059 1776 xfs_iext_next(ifp, &bma->icur);
26a75f67 1777 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
9788e059 1778 xfs_iext_prev(ifp, &bma->icur);
49f693fa
DC
1779 break;
1780
1781 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
5e656dbb 1782 /*
49f693fa
DC
1783 * Filling in the last part of a previous delayed allocation.
1784 * The right neighbor is contiguous with the new allocation.
5e656dbb 1785 */
19884717 1786 old = RIGHT;
19884717
CH
1787 RIGHT.br_startoff = new->br_startoff;
1788 RIGHT.br_startblock = new->br_startblock;
1789 RIGHT.br_blockcount += new->br_blockcount;
19884717 1790
49f693fa
DC
1791 if (bma->cur == NULL)
1792 rval = XFS_ILOG_DEXT;
1793 else {
1794 rval = 0;
70a93110 1795 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
49f693fa
DC
1796 if (error)
1797 goto done;
fbb4fa7f 1798 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 1799 xfs_btree_mark_sick(bma->cur);
fbb4fa7f
DW
1800 error = -EFSCORRUPTED;
1801 goto done;
1802 }
d0e5f1ff 1803 error = xfs_bmbt_update(bma->cur, &RIGHT);
49f693fa
DC
1804 if (error)
1805 goto done;
1806 }
1807
19884717 1808 temp = PREV.br_blockcount - new->br_blockcount;
49f693fa
DC
1809 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1810 startblockval(PREV.br_startblock));
19884717 1811
19884717
CH
1812 PREV.br_blockcount = temp;
1813 PREV.br_startblock = nullstartblock(da_new);
49f693fa 1814
9788e059
CH
1815 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1816 xfs_iext_next(ifp, &bma->icur);
1817 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
2c4e8ce7 1818 ASSERT(da_new <= da_old);
49f693fa
DC
1819 break;
1820
1821 case BMAP_RIGHT_FILLING:
a2ceac1f 1822 /*
49f693fa
DC
1823 * Filling in the last part of a previous delayed allocation.
1824 * The right neighbor is not contiguous.
a2ceac1f 1825 */
9788e059 1826 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
87c472b7
CH
1827 ifp->if_nextents++;
1828
49f693fa
DC
1829 if (bma->cur == NULL)
1830 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1831 else {
1832 rval = XFS_ILOG_CORE;
70a93110 1833 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
49f693fa
DC
1834 if (error)
1835 goto done;
fbb4fa7f 1836 if (XFS_IS_CORRUPT(mp, i != 0)) {
4467a60a 1837 xfs_btree_mark_sick(bma->cur);
fbb4fa7f
DW
1838 error = -EFSCORRUPTED;
1839 goto done;
1840 }
49f693fa
DC
1841 error = xfs_btree_insert(bma->cur, &i);
1842 if (error)
1843 goto done;
fbb4fa7f 1844 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 1845 xfs_btree_mark_sick(bma->cur);
fbb4fa7f
DW
1846 error = -EFSCORRUPTED;
1847 goto done;
1848 }
49f693fa 1849 }
a2ceac1f 1850
36e8786d 1851 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
49f693fa 1852 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
f7253505 1853 &bma->cur, 1, &tmp_rval, whichfork);
49f693fa
DC
1854 rval |= tmp_rval;
1855 if (error)
1856 goto done;
1857 }
19884717
CH
1858
1859 temp = PREV.br_blockcount - new->br_blockcount;
49f693fa
DC
1860 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1861 startblockval(PREV.br_startblock) -
6aba4616 1862 (bma->cur ? bma->cur->bc_bmap.allocated : 0));
19884717 1863
19884717
CH
1864 PREV.br_startblock = nullstartblock(da_new);
1865 PREV.br_blockcount = temp;
26a75f67 1866 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
9788e059 1867 xfs_iext_next(ifp, &bma->icur);
2c4e8ce7 1868 ASSERT(da_new <= da_old);
49f693fa
DC
1869 break;
1870
1871 case 0:
5e656dbb 1872 /*
49f693fa
DC
1873 * Filling in the middle part of a previous delayed allocation.
1874 * Contiguity is impossible here.
1875 * This case is avoided almost all the time.
1876 *
1877 * We start with a delayed allocation:
1878 *
1879 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1880 * PREV @ idx
1881 *
1882 * and we are allocating:
1883 * +rrrrrrrrrrrrrrrrr+
1884 * new
1885 *
1886 * and we set it up for insertion as:
1887 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1888 * new
1889 * PREV @ idx LEFT RIGHT
1890 * inserted at idx + 1
5e656dbb 1891 */
19884717
CH
1892 old = PREV;
1893
1894 /* LEFT is the new middle */
49f693fa 1895 LEFT = *new;
19884717
CH
1896
1897 /* RIGHT is the new right */
49f693fa 1898 RIGHT.br_state = PREV.br_state;
49f693fa 1899 RIGHT.br_startoff = new_endoff;
19884717
CH
1900 RIGHT.br_blockcount =
1901 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1902 RIGHT.br_startblock =
1903 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1904 RIGHT.br_blockcount));
1905
1906 /* truncate PREV */
19884717
CH
1907 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1908 PREV.br_startblock =
1909 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1910 PREV.br_blockcount));
9788e059 1911 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
19884717 1912
9788e059 1913 xfs_iext_next(ifp, &bma->icur);
26a75f67
CH
1914 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1915 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
87c472b7 1916 ifp->if_nextents++;
19884717 1917
49f693fa
DC
1918 if (bma->cur == NULL)
1919 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1920 else {
1921 rval = XFS_ILOG_CORE;
70a93110 1922 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
49f693fa
DC
1923 if (error)
1924 goto done;
fbb4fa7f 1925 if (XFS_IS_CORRUPT(mp, i != 0)) {
4467a60a 1926 xfs_btree_mark_sick(bma->cur);
fbb4fa7f
DW
1927 error = -EFSCORRUPTED;
1928 goto done;
1929 }
49f693fa
DC
1930 error = xfs_btree_insert(bma->cur, &i);
1931 if (error)
1932 goto done;
fbb4fa7f 1933 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 1934 xfs_btree_mark_sick(bma->cur);
fbb4fa7f
DW
1935 error = -EFSCORRUPTED;
1936 goto done;
1937 }
49f693fa 1938 }
5e656dbb 1939
36e8786d 1940 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
49f693fa 1941 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
f7253505 1942 &bma->cur, 1, &tmp_rval, whichfork);
49f693fa
DC
1943 rval |= tmp_rval;
1944 if (error)
1945 goto done;
1946 }
19884717
CH
1947
1948 da_new = startblockval(PREV.br_startblock) +
1949 startblockval(RIGHT.br_startblock);
49f693fa
DC
1950 break;
1951
1952 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1953 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1954 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1955 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1956 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1957 case BMAP_LEFT_CONTIG:
1958 case BMAP_RIGHT_CONTIG:
1959 /*
1960 * These cases are all impossible.
1961 */
1962 ASSERT(0);
1963 }
1964
5f847f1e 1965 /* add reverse mapping unless caller opted out */
46d29bb9
DW
1966 if (!(bma->flags & XFS_BMAPI_NORMAP))
1967 xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
d7f80320 1968
49f693fa 1969 /* convert to a btree if necessary */
36e8786d 1970 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
49f693fa
DC
1971 int tmp_logflags; /* partial log flag return val */
1972
1973 ASSERT(bma->cur == NULL);
1974 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
f7253505
BF
1975 &bma->cur, da_old > 0, &tmp_logflags,
1976 whichfork);
49f693fa
DC
1977 bma->logflags |= tmp_logflags;
1978 if (error)
1979 goto done;
1980 }
1981
f73690fe 1982 if (da_new != da_old)
ac315eaf 1983 xfs_mod_delalloc(bma->ip, 0, (int64_t)da_new - da_old);
f73690fe 1984
5bfca476 1985 if (bma->cur) {
6aba4616
CH
1986 da_new += bma->cur->bc_bmap.allocated;
1987 bma->cur->bc_bmap.allocated = 0;
49f693fa
DC
1988 }
1989
5bfca476 1990 /* adjust for changes in reserved delayed indirect blocks */
2c4e8ce7 1991 if (da_new < da_old)
de959abb 1992 xfs_add_fdblocks(mp, da_old - da_new);
2c4e8ce7
CH
1993 else if (da_new > da_old)
1994 error = xfs_dec_fdblocks(mp, da_new - da_old, true);
49f693fa 1995
36e8786d 1996 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
49f693fa 1997done:
1277a5e0
DW
1998 if (whichfork != XFS_COW_FORK)
1999 bma->logflags |= rval;
49f693fa
DC
2000 return error;
2001#undef LEFT
2002#undef RIGHT
2003#undef PREV
2bd0ea18
NS
2004}
2005
2006/*
49f693fa 2007 * Convert an unwritten allocation to a real allocation or vice versa.
2bd0ea18 2008 */
9fa4db19 2009int /* error */
49f693fa
DC
2010xfs_bmap_add_extent_unwritten_real(
2011 struct xfs_trans *tp,
2bd0ea18 2012 xfs_inode_t *ip, /* incore inode pointer */
4072e4b4 2013 int whichfork,
9788e059 2014 struct xfs_iext_cursor *icur,
ec924d04 2015 struct xfs_btree_cur **curp, /* if *curp is null, not a btree */
49f693fa 2016 xfs_bmbt_irec_t *new, /* new data to add to file extents */
49f693fa 2017 int *logflagsp) /* inode logging flags */
2bd0ea18 2018{
ec924d04 2019 struct xfs_btree_cur *cur; /* btree cursor */
2bd0ea18 2020 int error; /* error return value */
2bd0ea18 2021 int i; /* temp state */
e07055b8 2022 struct xfs_ifork *ifp; /* inode fork pointer */
49f693fa 2023 xfs_fileoff_t new_endoff; /* end offset of new entry */
49f693fa
DC
2024 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2025 /* left is 0, right is 1, prev is 2 */
2026 int rval=0; /* return value (logging flags) */
7ce54306 2027 uint32_t state = xfs_bmap_fork_to_state(whichfork);
4072e4b4 2028 struct xfs_mount *mp = ip->i_mount;
fd19685d 2029 struct xfs_bmbt_irec old;
5000d01d 2030
49f693fa 2031 *logflagsp = 0;
56b2de80 2032
49f693fa 2033 cur = *curp;
722e81c1 2034 ifp = xfs_ifork_ptr(ip, whichfork);
56b2de80 2035
49f693fa
DC
2036 ASSERT(!isnullstartblock(new->br_startblock));
2037
79896434 2038 XFS_STATS_INC(mp, xs_add_exlist);
49f693fa
DC
2039
2040#define LEFT r[0]
2041#define RIGHT r[1]
2042#define PREV r[2]
2043
2044 /*
2045 * Set up a bunch of variables to make the tests simpler.
2046 */
2bd0ea18 2047 error = 0;
9788e059 2048 xfs_iext_get_extent(ifp, icur, &PREV);
fd19685d 2049 ASSERT(new->br_state != PREV.br_state);
49f693fa
DC
2050 new_endoff = new->br_startoff + new->br_blockcount;
2051 ASSERT(PREV.br_startoff <= new->br_startoff);
2052 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
2053
2bd0ea18 2054 /*
49f693fa
DC
2055 * Set flags determining what part of the previous oldext allocation
2056 * extent is being replaced by a newext allocation.
2bd0ea18 2057 */
49f693fa
DC
2058 if (PREV.br_startoff == new->br_startoff)
2059 state |= BMAP_LEFT_FILLING;
2060 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2061 state |= BMAP_RIGHT_FILLING;
2bd0ea18 2062
49f693fa
DC
2063 /*
2064 * Check and set flags if this segment has a left neighbor.
2065 * Don't set contiguous if the combined extent would be too large.
2066 */
9788e059 2067 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
49f693fa 2068 state |= BMAP_LEFT_VALID;
49f693fa
DC
2069 if (isnullstartblock(LEFT.br_startblock))
2070 state |= BMAP_LEFT_DELAY;
2bd0ea18 2071 }
49f693fa
DC
2072
2073 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2074 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2075 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
fd19685d 2076 LEFT.br_state == new->br_state &&
15fb060e
CH
2077 LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2078 xfs_bmap_same_rtgroup(ip, whichfork, &LEFT, new))
49f693fa
DC
2079 state |= BMAP_LEFT_CONTIG;
2080
2bd0ea18 2081 /*
49f693fa
DC
2082 * Check and set flags if this segment has a right neighbor.
2083 * Don't set contiguous if the combined extent would be too large.
2084 * Also check for all-three-contiguous being too large.
2bd0ea18 2085 */
9788e059 2086 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
49f693fa 2087 state |= BMAP_RIGHT_VALID;
49f693fa
DC
2088 if (isnullstartblock(RIGHT.br_startblock))
2089 state |= BMAP_RIGHT_DELAY;
2090 }
a2ceac1f 2091
49f693fa
DC
2092 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2093 new_endoff == RIGHT.br_startoff &&
2094 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
fd19685d 2095 new->br_state == RIGHT.br_state &&
d3e0c71f 2096 new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
49f693fa
DC
2097 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2098 BMAP_RIGHT_FILLING)) !=
2099 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2100 BMAP_RIGHT_FILLING) ||
2101 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
15fb060e
CH
2102 <= XFS_MAX_BMBT_EXTLEN) &&
2103 xfs_bmap_same_rtgroup(ip, whichfork, new, &RIGHT))
49f693fa 2104 state |= BMAP_RIGHT_CONTIG;
2bd0ea18 2105
49f693fa
DC
2106 /*
2107 * Switch out based on the FILLING and CONTIG state bits.
2108 */
2109 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2110 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2111 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2112 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2bd0ea18 2113 /*
49f693fa
DC
2114 * Setting all of a previous oldext extent to newext.
2115 * The left and right neighbors are both contiguous with new.
2bd0ea18 2116 */
fd19685d 2117 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
49f693fa 2118
cf455a62
CH
2119 xfs_iext_remove(ip, icur, state);
2120 xfs_iext_remove(ip, icur, state);
9788e059
CH
2121 xfs_iext_prev(ifp, icur);
2122 xfs_iext_update_extent(ip, state, icur, &LEFT);
87c472b7 2123 ifp->if_nextents -= 2;
49f693fa
DC
2124 if (cur == NULL)
2125 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2126 else {
2127 rval = XFS_ILOG_CORE;
70a93110
CH
2128 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2129 if (error)
49f693fa 2130 goto done;
fbb4fa7f 2131 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2132 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2133 error = -EFSCORRUPTED;
2134 goto done;
2135 }
49f693fa
DC
2136 if ((error = xfs_btree_delete(cur, &i)))
2137 goto done;
fbb4fa7f 2138 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2139 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2140 error = -EFSCORRUPTED;
2141 goto done;
2142 }
49f693fa
DC
2143 if ((error = xfs_btree_decrement(cur, 0, &i)))
2144 goto done;
fbb4fa7f 2145 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2146 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2147 error = -EFSCORRUPTED;
2148 goto done;
2149 }
49f693fa
DC
2150 if ((error = xfs_btree_delete(cur, &i)))
2151 goto done;
fbb4fa7f 2152 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2153 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2154 error = -EFSCORRUPTED;
2155 goto done;
2156 }
49f693fa
DC
2157 if ((error = xfs_btree_decrement(cur, 0, &i)))
2158 goto done;
fbb4fa7f 2159 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2160 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2161 error = -EFSCORRUPTED;
2162 goto done;
2163 }
d0e5f1ff 2164 error = xfs_bmbt_update(cur, &LEFT);
fd19685d 2165 if (error)
49f693fa 2166 goto done;
2bd0ea18 2167 }
2bd0ea18
NS
2168 break;
2169
49f693fa 2170 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2bd0ea18 2171 /*
49f693fa
DC
2172 * Setting all of a previous oldext extent to newext.
2173 * The left neighbor is contiguous, the right is not.
2bd0ea18 2174 */
fd19685d 2175 LEFT.br_blockcount += PREV.br_blockcount;
49f693fa 2176
cf455a62 2177 xfs_iext_remove(ip, icur, state);
9788e059
CH
2178 xfs_iext_prev(ifp, icur);
2179 xfs_iext_update_extent(ip, state, icur, &LEFT);
87c472b7 2180 ifp->if_nextents--;
49f693fa
DC
2181 if (cur == NULL)
2182 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2183 else {
2184 rval = XFS_ILOG_CORE;
70a93110
CH
2185 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2186 if (error)
49f693fa 2187 goto done;
fbb4fa7f 2188 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2189 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2190 error = -EFSCORRUPTED;
2191 goto done;
2192 }
49f693fa
DC
2193 if ((error = xfs_btree_delete(cur, &i)))
2194 goto done;
fbb4fa7f 2195 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2196 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2197 error = -EFSCORRUPTED;
2198 goto done;
2199 }
49f693fa
DC
2200 if ((error = xfs_btree_decrement(cur, 0, &i)))
2201 goto done;
fbb4fa7f 2202 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2203 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2204 error = -EFSCORRUPTED;
2205 goto done;
2206 }
d0e5f1ff 2207 error = xfs_bmbt_update(cur, &LEFT);
fd19685d 2208 if (error)
49f693fa 2209 goto done;
2bd0ea18 2210 }
2bd0ea18 2211 break;
5000d01d 2212
49f693fa 2213 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2bd0ea18 2214 /*
49f693fa
DC
2215 * Setting all of a previous oldext extent to newext.
2216 * The right neighbor is contiguous, the left is not.
2bd0ea18 2217 */
fd19685d
CH
2218 PREV.br_blockcount += RIGHT.br_blockcount;
2219 PREV.br_state = new->br_state;
8121dd76 2220
9788e059 2221 xfs_iext_next(ifp, icur);
cf455a62 2222 xfs_iext_remove(ip, icur, state);
9788e059
CH
2223 xfs_iext_prev(ifp, icur);
2224 xfs_iext_update_extent(ip, state, icur, &PREV);
87c472b7 2225 ifp->if_nextents--;
fd19685d 2226
49f693fa
DC
2227 if (cur == NULL)
2228 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2229 else {
2230 rval = XFS_ILOG_CORE;
70a93110
CH
2231 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2232 if (error)
49f693fa 2233 goto done;
fbb4fa7f 2234 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2235 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2236 error = -EFSCORRUPTED;
2237 goto done;
2238 }
49f693fa
DC
2239 if ((error = xfs_btree_delete(cur, &i)))
2240 goto done;
fbb4fa7f 2241 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2242 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2243 error = -EFSCORRUPTED;
2244 goto done;
2245 }
49f693fa
DC
2246 if ((error = xfs_btree_decrement(cur, 0, &i)))
2247 goto done;
fbb4fa7f 2248 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2249 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2250 error = -EFSCORRUPTED;
2251 goto done;
2252 }
d0e5f1ff 2253 error = xfs_bmbt_update(cur, &PREV);
fd19685d 2254 if (error)
49f693fa
DC
2255 goto done;
2256 }
2257 break;
2bd0ea18 2258
49f693fa
DC
2259 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
2260 /*
2261 * Setting all of a previous oldext extent to newext.
2262 * Neither the left nor right neighbors are contiguous with
2263 * the new one.
2264 */
fd19685d 2265 PREV.br_state = new->br_state;
9788e059 2266 xfs_iext_update_extent(ip, state, icur, &PREV);
2bd0ea18 2267
49f693fa
DC
2268 if (cur == NULL)
2269 rval = XFS_ILOG_DEXT;
2270 else {
2271 rval = 0;
70a93110
CH
2272 error = xfs_bmbt_lookup_eq(cur, new, &i);
2273 if (error)
49f693fa 2274 goto done;
fbb4fa7f 2275 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2276 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2277 error = -EFSCORRUPTED;
2278 goto done;
2279 }
d0e5f1ff 2280 error = xfs_bmbt_update(cur, &PREV);
fd19685d 2281 if (error)
49f693fa
DC
2282 goto done;
2283 }
2284 break;
2bd0ea18 2285
49f693fa
DC
2286 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2287 /*
2288 * Setting the first part of a previous oldext extent to newext.
2289 * The left neighbor is contiguous.
2290 */
fd19685d 2291 LEFT.br_blockcount += new->br_blockcount;
a2ceac1f 2292
fd19685d 2293 old = PREV;
fd19685d
CH
2294 PREV.br_startoff += new->br_blockcount;
2295 PREV.br_startblock += new->br_blockcount;
2296 PREV.br_blockcount -= new->br_blockcount;
b3563c19 2297
9788e059
CH
2298 xfs_iext_update_extent(ip, state, icur, &PREV);
2299 xfs_iext_prev(ifp, icur);
2300 xfs_iext_update_extent(ip, state, icur, &LEFT);
b3563c19 2301
49f693fa
DC
2302 if (cur == NULL)
2303 rval = XFS_ILOG_DEXT;
2304 else {
2305 rval = 0;
70a93110 2306 error = xfs_bmbt_lookup_eq(cur, &old, &i);
fd19685d 2307 if (error)
49f693fa 2308 goto done;
fbb4fa7f 2309 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2310 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2311 error = -EFSCORRUPTED;
2312 goto done;
2313 }
d0e5f1ff 2314 error = xfs_bmbt_update(cur, &PREV);
fd19685d 2315 if (error)
49f693fa 2316 goto done;
fd19685d
CH
2317 error = xfs_btree_decrement(cur, 0, &i);
2318 if (error)
49f693fa 2319 goto done;
d0e5f1ff 2320 error = xfs_bmbt_update(cur, &LEFT);
49f693fa
DC
2321 if (error)
2322 goto done;
2bd0ea18 2323 }
49f693fa 2324 break;
b3563c19 2325
49f693fa
DC
2326 case BMAP_LEFT_FILLING:
2327 /*
2328 * Setting the first part of a previous oldext extent to newext.
2329 * The left neighbor is not contiguous.
2330 */
fd19685d 2331 old = PREV;
fd19685d
CH
2332 PREV.br_startoff += new->br_blockcount;
2333 PREV.br_startblock += new->br_blockcount;
2334 PREV.br_blockcount -= new->br_blockcount;
2bd0ea18 2335
9788e059 2336 xfs_iext_update_extent(ip, state, icur, &PREV);
26a75f67 2337 xfs_iext_insert(ip, icur, new, state);
87c472b7
CH
2338 ifp->if_nextents++;
2339
49f693fa
DC
2340 if (cur == NULL)
2341 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2342 else {
2343 rval = XFS_ILOG_CORE;
70a93110 2344 error = xfs_bmbt_lookup_eq(cur, &old, &i);
fd19685d 2345 if (error)
49f693fa 2346 goto done;
fbb4fa7f 2347 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2348 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2349 error = -EFSCORRUPTED;
2350 goto done;
2351 }
d0e5f1ff 2352 error = xfs_bmbt_update(cur, &PREV);
fd19685d 2353 if (error)
49f693fa
DC
2354 goto done;
2355 cur->bc_rec.b = *new;
2356 if ((error = xfs_btree_insert(cur, &i)))
2357 goto done;
fbb4fa7f 2358 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2359 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2360 error = -EFSCORRUPTED;
2361 goto done;
2362 }
49f693fa
DC
2363 }
2364 break;
56b2de80 2365
49f693fa
DC
2366 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2367 /*
2368 * Setting the last part of a previous oldext extent to newext.
2369 * The right neighbor is contiguous with the new allocation.
2370 */
fd19685d 2371 old = PREV;
fd19685d 2372 PREV.br_blockcount -= new->br_blockcount;
56b2de80 2373
fd19685d
CH
2374 RIGHT.br_startoff = new->br_startoff;
2375 RIGHT.br_startblock = new->br_startblock;
2376 RIGHT.br_blockcount += new->br_blockcount;
8121dd76 2377
9788e059
CH
2378 xfs_iext_update_extent(ip, state, icur, &PREV);
2379 xfs_iext_next(ifp, icur);
2380 xfs_iext_update_extent(ip, state, icur, &RIGHT);
56b2de80 2381
49f693fa
DC
2382 if (cur == NULL)
2383 rval = XFS_ILOG_DEXT;
2384 else {
2385 rval = 0;
70a93110 2386 error = xfs_bmbt_lookup_eq(cur, &old, &i);
fd19685d 2387 if (error)
49f693fa 2388 goto done;
fbb4fa7f 2389 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2390 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2391 error = -EFSCORRUPTED;
2392 goto done;
2393 }
d0e5f1ff 2394 error = xfs_bmbt_update(cur, &PREV);
fd19685d 2395 if (error)
49f693fa 2396 goto done;
fd19685d
CH
2397 error = xfs_btree_increment(cur, 0, &i);
2398 if (error)
49f693fa 2399 goto done;
d0e5f1ff 2400 error = xfs_bmbt_update(cur, &RIGHT);
fd19685d 2401 if (error)
49f693fa
DC
2402 goto done;
2403 }
2404 break;
ca86e759 2405
49f693fa
DC
2406 case BMAP_RIGHT_FILLING:
2407 /*
2408 * Setting the last part of a previous oldext extent to newext.
2409 * The right neighbor is not contiguous.
2410 */
fd19685d 2411 old = PREV;
fd19685d 2412 PREV.br_blockcount -= new->br_blockcount;
2bd0ea18 2413
9788e059
CH
2414 xfs_iext_update_extent(ip, state, icur, &PREV);
2415 xfs_iext_next(ifp, icur);
26a75f67 2416 xfs_iext_insert(ip, icur, new, state);
87c472b7 2417 ifp->if_nextents++;
2bd0ea18 2418
49f693fa
DC
2419 if (cur == NULL)
2420 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2421 else {
2422 rval = XFS_ILOG_CORE;
70a93110 2423 error = xfs_bmbt_lookup_eq(cur, &old, &i);
fd19685d 2424 if (error)
49f693fa 2425 goto done;
fbb4fa7f 2426 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2427 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2428 error = -EFSCORRUPTED;
2429 goto done;
2430 }
d0e5f1ff 2431 error = xfs_bmbt_update(cur, &PREV);
fd19685d 2432 if (error)
49f693fa 2433 goto done;
70a93110
CH
2434 error = xfs_bmbt_lookup_eq(cur, new, &i);
2435 if (error)
49f693fa 2436 goto done;
fbb4fa7f 2437 if (XFS_IS_CORRUPT(mp, i != 0)) {
4467a60a 2438 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2439 error = -EFSCORRUPTED;
2440 goto done;
2441 }
49f693fa
DC
2442 if ((error = xfs_btree_insert(cur, &i)))
2443 goto done;
fbb4fa7f 2444 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2445 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2446 error = -EFSCORRUPTED;
2447 goto done;
2448 }
49f693fa
DC
2449 }
2450 break;
2451
2452 case 0:
2bd0ea18 2453 /*
49f693fa
DC
2454 * Setting the middle part of a previous oldext extent to
2455 * newext. Contiguity is impossible here.
2456 * One extent becomes three extents.
2bd0ea18 2457 */
fd19685d 2458 old = PREV;
fd19685d 2459 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
49f693fa
DC
2460
2461 r[0] = *new;
2462 r[1].br_startoff = new_endoff;
2463 r[1].br_blockcount =
fd19685d 2464 old.br_startoff + old.br_blockcount - new_endoff;
49f693fa 2465 r[1].br_startblock = new->br_startblock + new->br_blockcount;
fd19685d 2466 r[1].br_state = PREV.br_state;
49f693fa 2467
9788e059
CH
2468 xfs_iext_update_extent(ip, state, icur, &PREV);
2469 xfs_iext_next(ifp, icur);
26a75f67
CH
2470 xfs_iext_insert(ip, icur, &r[1], state);
2471 xfs_iext_insert(ip, icur, &r[0], state);
87c472b7 2472 ifp->if_nextents += 2;
49f693fa 2473
49f693fa
DC
2474 if (cur == NULL)
2475 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2476 else {
2477 rval = XFS_ILOG_CORE;
70a93110 2478 error = xfs_bmbt_lookup_eq(cur, &old, &i);
fd19685d 2479 if (error)
49f693fa 2480 goto done;
fbb4fa7f 2481 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2482 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2483 error = -EFSCORRUPTED;
2484 goto done;
2485 }
49f693fa 2486 /* new right extent - oldext */
d0e5f1ff
CH
2487 error = xfs_bmbt_update(cur, &r[1]);
2488 if (error)
49f693fa
DC
2489 goto done;
2490 /* new left extent - oldext */
2491 cur->bc_rec.b = PREV;
49f693fa
DC
2492 if ((error = xfs_btree_insert(cur, &i)))
2493 goto done;
fbb4fa7f 2494 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2495 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2496 error = -EFSCORRUPTED;
2497 goto done;
2498 }
49f693fa
DC
2499 /*
2500 * Reset the cursor to the position of the new extent
2501 * we are about to insert as we can't trust it after
2502 * the previous insert.
2503 */
70a93110
CH
2504 error = xfs_bmbt_lookup_eq(cur, new, &i);
2505 if (error)
49f693fa 2506 goto done;
fbb4fa7f 2507 if (XFS_IS_CORRUPT(mp, i != 0)) {
4467a60a 2508 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2509 error = -EFSCORRUPTED;
2510 goto done;
2511 }
49f693fa 2512 /* new middle extent - newext */
49f693fa
DC
2513 if ((error = xfs_btree_insert(cur, &i)))
2514 goto done;
fbb4fa7f 2515 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2516 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2517 error = -EFSCORRUPTED;
2518 goto done;
2519 }
2bd0ea18 2520 }
49f693fa
DC
2521 break;
2522
2523 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2524 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2525 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2526 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2527 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2528 case BMAP_LEFT_CONTIG:
2529 case BMAP_RIGHT_CONTIG:
5000d01d 2530 /*
49f693fa 2531 * These cases are all impossible.
2bd0ea18 2532 */
49f693fa
DC
2533 ASSERT(0);
2534 }
2535
d7f80320 2536 /* update reverse mappings */
46d29bb9 2537 xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
d7f80320 2538
49f693fa 2539 /* convert to a btree if necessary */
4072e4b4 2540 if (xfs_bmap_needs_btree(ip, whichfork)) {
49f693fa
DC
2541 int tmp_logflags; /* partial log flag return val */
2542
2543 ASSERT(cur == NULL);
f7253505
BF
2544 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2545 &tmp_logflags, whichfork);
49f693fa
DC
2546 *logflagsp |= tmp_logflags;
2547 if (error)
2548 goto done;
ca86e759 2549 }
49f693fa
DC
2550
2551 /* clear out the allocated field, done with it now in any case. */
2552 if (cur) {
6aba4616 2553 cur->bc_bmap.allocated = 0;
49f693fa
DC
2554 *curp = cur;
2555 }
2556
4072e4b4 2557 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
2bd0ea18 2558done:
49f693fa 2559 *logflagsp |= rval;
2bd0ea18 2560 return error;
49f693fa
DC
2561#undef LEFT
2562#undef RIGHT
2563#undef PREV
2bd0ea18
NS
2564}
2565
2bd0ea18 2566/*
49f693fa 2567 * Convert a hole to a real allocation.
2bd0ea18 2568 */
49f693fa
DC
2569STATIC int /* error */
2570xfs_bmap_add_extent_hole_real(
972432f2
CH
2571 struct xfs_trans *tp,
2572 struct xfs_inode *ip,
2573 int whichfork,
9788e059 2574 struct xfs_iext_cursor *icur,
972432f2
CH
2575 struct xfs_btree_cur **curp,
2576 struct xfs_bmbt_irec *new,
5f847f1e 2577 int *logflagsp,
6e22af31 2578 uint32_t flags)
5000d01d 2579{
722e81c1 2580 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
972432f2
CH
2581 struct xfs_mount *mp = ip->i_mount;
2582 struct xfs_btree_cur *cur = *curp;
49f693fa
DC
2583 int error; /* error return value */
2584 int i; /* temp state */
49f693fa
DC
2585 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2586 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2587 int rval=0; /* return value (logging flags) */
7ce54306 2588 uint32_t state = xfs_bmap_fork_to_state(whichfork);
3281eb91 2589 struct xfs_bmbt_irec old;
2bd0ea18 2590
49f693fa 2591 ASSERT(!isnullstartblock(new->br_startblock));
d242dfc3 2592 ASSERT(!cur || !(cur->bc_flags & XFS_BTREE_BMBT_WASDEL));
5e656dbb 2593
79896434 2594 XFS_STATS_INC(mp, xs_add_exlist);
49f693fa 2595
49f693fa
DC
2596 /*
2597 * Check and set flags if this segment has a left neighbor.
2598 */
9788e059 2599 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
49f693fa 2600 state |= BMAP_LEFT_VALID;
49f693fa
DC
2601 if (isnullstartblock(left.br_startblock))
2602 state |= BMAP_LEFT_DELAY;
5e656dbb 2603 }
2bd0ea18 2604
49f693fa
DC
2605 /*
2606 * Check and set flags if this segment has a current value.
2607 * Not true if we're inserting into the "hole" at eof.
2608 */
9788e059 2609 if (xfs_iext_get_extent(ifp, icur, &right)) {
49f693fa 2610 state |= BMAP_RIGHT_VALID;
49f693fa
DC
2611 if (isnullstartblock(right.br_startblock))
2612 state |= BMAP_RIGHT_DELAY;
2bd0ea18 2613 }
2bd0ea18 2614
49f693fa
DC
2615 /*
2616 * We're inserting a real allocation between "left" and "right".
2617 * Set the contiguity flags. Don't let extents get too large.
2618 */
2619 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2620 left.br_startoff + left.br_blockcount == new->br_startoff &&
2621 left.br_startblock + left.br_blockcount == new->br_startblock &&
2622 left.br_state == new->br_state &&
15fb060e
CH
2623 left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2624 xfs_bmap_same_rtgroup(ip, whichfork, &left, new))
49f693fa 2625 state |= BMAP_LEFT_CONTIG;
57c9fccb 2626
49f693fa
DC
2627 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2628 new->br_startoff + new->br_blockcount == right.br_startoff &&
2629 new->br_startblock + new->br_blockcount == right.br_startblock &&
2630 new->br_state == right.br_state &&
d3e0c71f 2631 new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
49f693fa
DC
2632 (!(state & BMAP_LEFT_CONTIG) ||
2633 left.br_blockcount + new->br_blockcount +
15fb060e
CH
2634 right.br_blockcount <= XFS_MAX_BMBT_EXTLEN) &&
2635 xfs_bmap_same_rtgroup(ip, whichfork, new, &right))
49f693fa 2636 state |= BMAP_RIGHT_CONTIG;
ca86e759 2637
49f693fa
DC
2638 error = 0;
2639 /*
2640 * Select which case we're in here, and implement it.
2641 */
2642 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2643 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
57c9fccb 2644 /*
49f693fa
DC
2645 * New allocation is contiguous with real allocations on the
2646 * left and on the right.
2647 * Merge all three into a single extent record.
57c9fccb 2648 */
3281eb91 2649 left.br_blockcount += new->br_blockcount + right.br_blockcount;
56b2de80 2650
cf455a62 2651 xfs_iext_remove(ip, icur, state);
9788e059
CH
2652 xfs_iext_prev(ifp, icur);
2653 xfs_iext_update_extent(ip, state, icur, &left);
87c472b7 2654 ifp->if_nextents--;
56b2de80 2655
972432f2 2656 if (cur == NULL) {
49f693fa
DC
2657 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2658 } else {
2659 rval = XFS_ILOG_CORE;
70a93110 2660 error = xfs_bmbt_lookup_eq(cur, &right, &i);
49f693fa
DC
2661 if (error)
2662 goto done;
fbb4fa7f 2663 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2664 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2665 error = -EFSCORRUPTED;
2666 goto done;
2667 }
972432f2 2668 error = xfs_btree_delete(cur, &i);
49f693fa
DC
2669 if (error)
2670 goto done;
fbb4fa7f 2671 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2672 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2673 error = -EFSCORRUPTED;
2674 goto done;
2675 }
972432f2 2676 error = xfs_btree_decrement(cur, 0, &i);
49f693fa
DC
2677 if (error)
2678 goto done;
fbb4fa7f 2679 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2680 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2681 error = -EFSCORRUPTED;
2682 goto done;
2683 }
d0e5f1ff 2684 error = xfs_bmbt_update(cur, &left);
49f693fa
DC
2685 if (error)
2686 goto done;
2687 }
57c9fccb 2688 break;
49f693fa
DC
2689
2690 case BMAP_LEFT_CONTIG:
2691 /*
2692 * New allocation is contiguous with a real allocation
2693 * on the left.
2694 * Merge the new allocation with the left neighbor.
2695 */
3281eb91 2696 old = left;
3281eb91 2697 left.br_blockcount += new->br_blockcount;
501dd276 2698
9788e059
CH
2699 xfs_iext_prev(ifp, icur);
2700 xfs_iext_update_extent(ip, state, icur, &left);
49f693fa 2701
972432f2 2702 if (cur == NULL) {
49f693fa
DC
2703 rval = xfs_ilog_fext(whichfork);
2704 } else {
2705 rval = 0;
70a93110 2706 error = xfs_bmbt_lookup_eq(cur, &old, &i);
49f693fa
DC
2707 if (error)
2708 goto done;
fbb4fa7f 2709 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2710 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2711 error = -EFSCORRUPTED;
2712 goto done;
2713 }
d0e5f1ff 2714 error = xfs_bmbt_update(cur, &left);
49f693fa
DC
2715 if (error)
2716 goto done;
2717 }
57c9fccb 2718 break;
49f693fa
DC
2719
2720 case BMAP_RIGHT_CONTIG:
2721 /*
2722 * New allocation is contiguous with a real allocation
2723 * on the right.
2724 * Merge the new allocation with the right neighbor.
2725 */
3281eb91 2726 old = right;
df926c07 2727
3281eb91
CH
2728 right.br_startoff = new->br_startoff;
2729 right.br_startblock = new->br_startblock;
2730 right.br_blockcount += new->br_blockcount;
9788e059 2731 xfs_iext_update_extent(ip, state, icur, &right);
49f693fa 2732
972432f2 2733 if (cur == NULL) {
49f693fa
DC
2734 rval = xfs_ilog_fext(whichfork);
2735 } else {
2736 rval = 0;
70a93110 2737 error = xfs_bmbt_lookup_eq(cur, &old, &i);
49f693fa
DC
2738 if (error)
2739 goto done;
fbb4fa7f 2740 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2741 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2742 error = -EFSCORRUPTED;
2743 goto done;
2744 }
d0e5f1ff 2745 error = xfs_bmbt_update(cur, &right);
49f693fa
DC
2746 if (error)
2747 goto done;
2748 }
2749 break;
2750
2751 case 0:
2752 /*
2753 * New allocation is not contiguous with another
2754 * real allocation.
2755 * Insert a new entry.
2756 */
26a75f67 2757 xfs_iext_insert(ip, icur, new, state);
87c472b7
CH
2758 ifp->if_nextents++;
2759
972432f2 2760 if (cur == NULL) {
49f693fa
DC
2761 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2762 } else {
2763 rval = XFS_ILOG_CORE;
70a93110 2764 error = xfs_bmbt_lookup_eq(cur, new, &i);
49f693fa
DC
2765 if (error)
2766 goto done;
fbb4fa7f 2767 if (XFS_IS_CORRUPT(mp, i != 0)) {
4467a60a 2768 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2769 error = -EFSCORRUPTED;
2770 goto done;
2771 }
972432f2 2772 error = xfs_btree_insert(cur, &i);
49f693fa
DC
2773 if (error)
2774 goto done;
fbb4fa7f 2775 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2776 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2777 error = -EFSCORRUPTED;
2778 goto done;
2779 }
49f693fa 2780 }
57c9fccb 2781 break;
57c9fccb 2782 }
a2ceac1f 2783
5f847f1e 2784 /* add reverse mapping unless caller opted out */
46d29bb9
DW
2785 if (!(flags & XFS_BMAPI_NORMAP))
2786 xfs_rmap_map_extent(tp, ip, whichfork, new);
d7f80320 2787
49f693fa 2788 /* convert to a btree if necessary */
972432f2 2789 if (xfs_bmap_needs_btree(ip, whichfork)) {
49f693fa 2790 int tmp_logflags; /* partial log flag return val */
3f853c7a 2791
972432f2 2792 ASSERT(cur == NULL);
f7253505
BF
2793 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2794 &tmp_logflags, whichfork);
972432f2
CH
2795 *logflagsp |= tmp_logflags;
2796 cur = *curp;
49f693fa
DC
2797 if (error)
2798 goto done;
57c9fccb 2799 }
a2ceac1f 2800
49f693fa 2801 /* clear out the allocated field, done with it now in any case. */
972432f2 2802 if (cur)
6aba4616 2803 cur->bc_bmap.allocated = 0;
49f693fa 2804
972432f2 2805 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
49f693fa 2806done:
972432f2 2807 *logflagsp |= rval;
57c9fccb
NS
2808 return error;
2809}
2810
2bd0ea18 2811/*
49f693fa 2812 * Functions used in the extent read, allocate and remove paths
2bd0ea18 2813 */
2bd0ea18 2814
5000d01d 2815/*
fd2f92c8 2816 * Adjust the size of the new extent based on i_extsize and rt extsize.
2bd0ea18 2817 */
613e6057 2818int
49f693fa
DC
2819xfs_bmap_extsize_align(
2820 xfs_mount_t *mp,
2821 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2822 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2823 xfs_extlen_t extsz, /* align to this extent size */
2824 int rt, /* is this a realtime inode? */
2825 int eof, /* is extent at end-of-file? */
2826 int delay, /* creating delalloc extent? */
2827 int convert, /* overwriting unwritten extent? */
2828 xfs_fileoff_t *offp, /* in/out: aligned offset */
2829 xfs_extlen_t *lenp) /* in/out: aligned length */
2bd0ea18 2830{
49f693fa
DC
2831 xfs_fileoff_t orig_off; /* original offset */
2832 xfs_extlen_t orig_alen; /* original length */
2833 xfs_fileoff_t orig_end; /* original off+len */
2834 xfs_fileoff_t nexto; /* next file offset */
2835 xfs_fileoff_t prevo; /* previous file offset */
2836 xfs_fileoff_t align_off; /* temp for offset */
2837 xfs_extlen_t align_alen; /* temp for length */
2838 xfs_extlen_t temp; /* temp for calculations */
2839
2840 if (convert)
2841 return 0;
2842
2843 orig_off = align_off = *offp;
2844 orig_alen = align_alen = *lenp;
2845 orig_end = orig_off + orig_alen;
2bd0ea18
NS
2846
2847 /*
49f693fa
DC
2848 * If this request overlaps an existing extent, then don't
2849 * attempt to perform any additional alignment.
2bd0ea18 2850 */
49f693fa
DC
2851 if (!delay && !eof &&
2852 (orig_off >= gotp->br_startoff) &&
2853 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2854 return 0;
2bd0ea18 2855 }
57c9fccb 2856
49f693fa
DC
2857 /*
2858 * If the file offset is unaligned vs. the extent size
2859 * we need to align it. This will be possible unless
2860 * the file was previously written with a kernel that didn't
2861 * perform this alignment, or if a truncate shot us in the
2862 * foot.
2863 */
5a595099 2864 div_u64_rem(orig_off, extsz, &temp);
49f693fa
DC
2865 if (temp) {
2866 align_alen += temp;
2867 align_off -= temp;
2868 }
7cc23f0c
DC
2869
2870 /* Same adjustment for the end of the requested area. */
2871 temp = (align_alen % extsz);
2872 if (temp)
2873 align_alen += extsz - temp;
2874
49f693fa 2875 /*
7cc23f0c 2876 * For large extent hint sizes, the aligned extent might be larger than
d3e0c71f
CB
2877 * XFS_BMBT_MAX_EXTLEN. In that case, reduce the size by an extsz so
2878 * that it pulls the length back under XFS_BMBT_MAX_EXTLEN. The outer
2879 * allocation loops handle short allocation just fine, so it is safe to
2880 * do this. We only want to do it when we are forced to, though, because
2881 * it means more allocation operations are required.
49f693fa 2882 */
d3e0c71f 2883 while (align_alen > XFS_MAX_BMBT_EXTLEN)
7cc23f0c 2884 align_alen -= extsz;
d3e0c71f 2885 ASSERT(align_alen <= XFS_MAX_BMBT_EXTLEN);
7cc23f0c 2886
49f693fa
DC
2887 /*
2888 * If the previous block overlaps with this proposed allocation
2889 * then move the start forward without adjusting the length.
2890 */
2891 if (prevp->br_startoff != NULLFILEOFF) {
2892 if (prevp->br_startblock == HOLESTARTBLOCK)
2893 prevo = prevp->br_startoff;
2894 else
2895 prevo = prevp->br_startoff + prevp->br_blockcount;
2896 } else
2897 prevo = 0;
2898 if (align_off != orig_off && align_off < prevo)
2899 align_off = prevo;
2900 /*
2901 * If the next block overlaps with this proposed allocation
2902 * then move the start back without adjusting the length,
2903 * but not before offset 0.
2904 * This may of course make the start overlap previous block,
2905 * and if we hit the offset 0 limit then the next block
2906 * can still overlap too.
2907 */
2908 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2909 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2910 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2911 nexto = gotp->br_startoff + gotp->br_blockcount;
2912 else
2913 nexto = gotp->br_startoff;
2914 } else
2915 nexto = NULLFILEOFF;
2916 if (!eof &&
2917 align_off + align_alen != orig_end &&
2918 align_off + align_alen > nexto)
2919 align_off = nexto > align_alen ? nexto - align_alen : 0;
2920 /*
2921 * If we're now overlapping the next or previous extent that
2922 * means we can't fit an extsz piece in this hole. Just move
2923 * the start forward to the first valid spot and set
2924 * the length so we hit the end.
2925 */
2926 if (align_off != orig_off && align_off < prevo)
2927 align_off = prevo;
2928 if (align_off + align_alen != orig_end &&
2929 align_off + align_alen > nexto &&
2930 nexto != NULLFILEOFF) {
2931 ASSERT(nexto > prevo);
2932 align_alen = nexto - align_off;
57c9fccb 2933 }
2bd0ea18 2934
49f693fa
DC
2935 /*
2936 * If realtime, and the result isn't a multiple of the realtime
2937 * extent size we need to remove blocks until it is.
2938 */
43ed0e42 2939 if (rt && (temp = xfs_extlen_to_rtxmod(mp, align_alen))) {
2bd0ea18 2940 /*
49f693fa
DC
2941 * We're not covering the original request, or
2942 * we won't be able to once we fix the length.
2bd0ea18 2943 */
49f693fa
DC
2944 if (orig_off < align_off ||
2945 orig_end > align_off + align_alen ||
2946 align_alen - temp < orig_alen)
12b53197 2947 return -EINVAL;
49f693fa
DC
2948 /*
2949 * Try to fix it by moving the start up.
2950 */
2951 if (align_off + temp <= orig_off) {
2952 align_alen -= temp;
2953 align_off += temp;
2bd0ea18 2954 }
49f693fa
DC
2955 /*
2956 * Try to fix it by moving the end in.
2957 */
2958 else if (align_off + align_alen - temp >= orig_end)
2959 align_alen -= temp;
2960 /*
2961 * Set the start to the minimum then trim the length.
2962 */
2963 else {
2964 align_alen -= orig_off - align_off;
2965 align_off = orig_off;
43ed0e42 2966 align_alen -= xfs_extlen_to_rtxmod(mp, align_alen);
49f693fa
DC
2967 }
2968 /*
2969 * Result doesn't cover the request, fail it.
2970 */
2971 if (orig_off < align_off || orig_end > align_off + align_alen)
12b53197 2972 return -EINVAL;
49f693fa
DC
2973 } else {
2974 ASSERT(orig_off >= align_off);
d3e0c71f 2975 /* see XFS_BMBT_MAX_EXTLEN handling above */
7cc23f0c 2976 ASSERT(orig_end <= align_off + align_alen ||
d3e0c71f 2977 align_alen + extsz > XFS_MAX_BMBT_EXTLEN);
2bd0ea18 2978 }
49f693fa
DC
2979
2980#ifdef DEBUG
2981 if (!eof && gotp->br_startoff != NULLFILEOFF)
2982 ASSERT(align_off + align_alen <= gotp->br_startoff);
2983 if (prevp->br_startoff != NULLFILEOFF)
2984 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2985#endif
2986
2987 *lenp = align_alen;
2988 *offp = align_off;
2bd0ea18
NS
2989 return 0;
2990}
2991
4fb1557f
CH
2992static inline bool
2993xfs_bmap_adjacent_valid(
2994 struct xfs_bmalloca *ap,
2995 xfs_fsblock_t x,
2996 xfs_fsblock_t y)
2997{
2998 struct xfs_mount *mp = ap->ip->i_mount;
2999
3000 if (XFS_IS_REALTIME_INODE(ap->ip) &&
9f09e4e0 3001 (ap->datatype & XFS_ALLOC_USERDATA)) {
9f09e4e0 3002 if (!xfs_has_rtgroups(mp))
606a3744 3003 return x < mp->m_sb.sb_rblocks;
9f09e4e0
CH
3004
3005 return xfs_rtb_to_rgno(mp, x) == xfs_rtb_to_rgno(mp, y) &&
3006 xfs_rtb_to_rgno(mp, x) < mp->m_sb.sb_rgcount &&
3007 xfs_rtb_to_rtx(mp, x) < mp->m_sb.sb_rgextents;
3008
3009 }
4fb1557f
CH
3010
3011 return XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) &&
3012 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount &&
3013 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks;
3014}
3015
49f693fa
DC
3016#define XFS_ALLOC_GAP_UNITS 4
3017
55d00e3d
CH
3018/* returns true if ap->blkno was modified */
3019bool
49f693fa 3020xfs_bmap_adjacent(
613e6057 3021 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
2bd0ea18 3022{
4fb1557f
CH
3023 xfs_fsblock_t adjust; /* adjustment to block numbers */
3024
49f693fa
DC
3025 /*
3026 * If allocating at eof, and there's a previous real block,
3027 * try to use its last block as our starting point.
3028 */
3029 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3030 !isnullstartblock(ap->prev.br_startblock) &&
4fb1557f
CH
3031 xfs_bmap_adjacent_valid(ap,
3032 ap->prev.br_startblock + ap->prev.br_blockcount,
3033 ap->prev.br_startblock)) {
49f693fa
DC
3034 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3035 /*
3036 * Adjust for the gap between prevp and us.
3037 */
3038 adjust = ap->offset -
3039 (ap->prev.br_startoff + ap->prev.br_blockcount);
4fb1557f
CH
3040 if (adjust && xfs_bmap_adjacent_valid(ap, ap->blkno + adjust,
3041 ap->prev.br_startblock))
49f693fa 3042 ap->blkno += adjust;
55d00e3d 3043 return true;
2bd0ea18 3044 }
49f693fa
DC
3045 /*
3046 * If not at eof, then compare the two neighbor blocks.
3047 * Figure out whether either one gives us a good starting point,
3048 * and pick the better one.
3049 */
55d00e3d 3050 if (!ap->eof) {
49f693fa
DC
3051 xfs_fsblock_t gotbno; /* right side block number */
3052 xfs_fsblock_t gotdiff=0; /* right side difference */
3053 xfs_fsblock_t prevbno; /* left side block number */
3054 xfs_fsblock_t prevdiff=0; /* left side difference */
3055
3056 /*
3057 * If there's a previous (left) block, select a requested
3058 * start block based on it.
3059 */
3060 if (ap->prev.br_startoff != NULLFILEOFF &&
3061 !isnullstartblock(ap->prev.br_startblock) &&
3062 (prevbno = ap->prev.br_startblock +
3063 ap->prev.br_blockcount) &&
4fb1557f
CH
3064 xfs_bmap_adjacent_valid(ap, prevbno,
3065 ap->prev.br_startblock)) {
49f693fa
DC
3066 /*
3067 * Calculate gap to end of previous block.
3068 */
3069 adjust = prevdiff = ap->offset -
3070 (ap->prev.br_startoff +
3071 ap->prev.br_blockcount);
3072 /*
3073 * Figure the startblock based on the previous block's
3074 * end and the gap size.
3075 * Heuristic!
3076 * If the gap is large relative to the piece we're
3077 * allocating, or using it gives us an invalid block
3078 * number, then just use the end of the previous block.
3079 */
3080 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
4fb1557f
CH
3081 xfs_bmap_adjacent_valid(ap, prevbno + prevdiff,
3082 ap->prev.br_startblock))
49f693fa
DC
3083 prevbno += adjust;
3084 else
3085 prevdiff += adjust;
49f693fa
DC
3086 }
3087 /*
3088 * No previous block or can't follow it, just default.
3089 */
3090 else
3091 prevbno = NULLFSBLOCK;
3092 /*
3093 * If there's a following (right) block, select a requested
3094 * start block based on it.
3095 */
3096 if (!isnullstartblock(ap->got.br_startblock)) {
3097 /*
3098 * Calculate gap to start of next block.
3099 */
3100 adjust = gotdiff = ap->got.br_startoff - ap->offset;
3101 /*
3102 * Figure the startblock based on the next block's
3103 * start and the gap size.
3104 */
3105 gotbno = ap->got.br_startblock;
3106 /*
3107 * Heuristic!
3108 * If the gap is large relative to the piece we're
3109 * allocating, or using it gives us an invalid block
3110 * number, then just use the start of the next block
3111 * offset by our length.
3112 */
3113 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
4fb1557f
CH
3114 xfs_bmap_adjacent_valid(ap, gotbno - gotdiff,
3115 gotbno))
49f693fa 3116 gotbno -= adjust;
4fb1557f
CH
3117 else if (xfs_bmap_adjacent_valid(ap, gotbno - ap->length,
3118 gotbno)) {
49f693fa
DC
3119 gotbno -= ap->length;
3120 gotdiff += adjust - ap->length;
3121 } else
3122 gotdiff += adjust;
49f693fa
DC
3123 }
3124 /*
3125 * No next block, just default.
3126 */
2bd0ea18 3127 else
49f693fa
DC
3128 gotbno = NULLFSBLOCK;
3129 /*
3130 * If both valid, pick the better one, else the only good
3131 * one, else ap->blkno is already set (to 0 or the inode block).
3132 */
55d00e3d 3133 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) {
49f693fa 3134 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
55d00e3d
CH
3135 return true;
3136 }
3137 if (prevbno != NULLFSBLOCK) {
49f693fa 3138 ap->blkno = prevbno;
55d00e3d
CH
3139 return true;
3140 }
3141 if (gotbno != NULLFSBLOCK) {
49f693fa 3142 ap->blkno = gotbno;
55d00e3d
CH
3143 return true;
3144 }
a2ceac1f 3145 }
4fb1557f 3146
55d00e3d 3147 return false;
a2ceac1f
DC
3148}
3149
c92d3040 3150int
ff105f75 3151xfs_bmap_longest_free_extent(
5cb4ffc8 3152 struct xfs_perag *pag,
ff105f75 3153 struct xfs_trans *tp,
4ccd9b2f 3154 xfs_extlen_t *blen)
ff105f75 3155{
ff105f75
DC
3156 xfs_extlen_t longest;
3157 int error = 0;
3158
03dc2ef2 3159 if (!xfs_perag_initialised_agf(pag)) {
f9084bd9 3160 error = xfs_alloc_read_agf(pag, tp, XFS_ALLOC_FLAG_TRYLOCK,
87db57ba 3161 NULL);
4ccd9b2f 3162 if (error)
5cb4ffc8 3163 return error;
ff105f75
DC
3164 }
3165
1421de38 3166 longest = xfs_alloc_longest_free_extent(pag,
6a271c73 3167 xfs_alloc_min_freelist(pag_mount(pag), pag),
cf8ce220 3168 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
ff105f75
DC
3169 if (*blen < longest)
3170 *blen = longest;
3171
5cb4ffc8 3172 return 0;
ff105f75
DC
3173}
3174
4ccd9b2f 3175static xfs_extlen_t
ff105f75
DC
3176xfs_bmap_select_minlen(
3177 struct xfs_bmalloca *ap,
3178 struct xfs_alloc_arg *args,
4ccd9b2f 3179 xfs_extlen_t blen)
ff105f75 3180{
4ccd9b2f
DC
3181
3182 /*
3183 * Since we used XFS_ALLOC_FLAG_TRYLOCK in _longest_free_extent(), it is
3184 * possible that there is enough contiguous free space for this request.
3185 */
3186 if (blen < ap->minlen)
3187 return ap->minlen;
3188
3189 /*
3190 * If the best seen length is less than the request length,
3191 * use the best as the minimum, otherwise we've got the maxlen we
3192 * were asked for.
3193 */
3194 if (blen < args->maxlen)
3195 return blen;
3196 return args->maxlen;
ff105f75
DC
3197}
3198
3ed4e682 3199static int
80375d24 3200xfs_bmap_btalloc_select_lengths(
49f693fa
DC
3201 struct xfs_bmalloca *ap,
3202 struct xfs_alloc_arg *args,
3203 xfs_extlen_t *blen)
a2ceac1f 3204{
3ed4e682 3205 struct xfs_mount *mp = args->mp;
5cb4ffc8
DC
3206 struct xfs_perag *pag;
3207 xfs_agnumber_t agno, startag;
5cb4ffc8 3208 int error = 0;
a2ceac1f 3209
80375d24
DC
3210 if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3211 args->total = ap->minlen;
3212 args->minlen = ap->minlen;
3213 return 0;
3214 }
a2ceac1f 3215
80375d24 3216 args->total = ap->total;
3ed4e682 3217 startag = XFS_FSB_TO_AGNO(mp, ap->blkno);
49f693fa 3218 if (startag == NULLAGNUMBER)
5cb4ffc8 3219 startag = 0;
a2ceac1f 3220
5cb4ffc8
DC
3221 *blen = 0;
3222 for_each_perag_wrap(mp, startag, agno, pag) {
4ccd9b2f
DC
3223 error = xfs_bmap_longest_free_extent(pag, args->tp, blen);
3224 if (error && error != -EAGAIN)
5cb4ffc8 3225 break;
4ccd9b2f 3226 error = 0;
5cb4ffc8 3227 if (*blen >= args->maxlen)
49f693fa 3228 break;
49f693fa 3229 }
5cb4ffc8
DC
3230 if (pag)
3231 xfs_perag_rele(pag);
2bd0ea18 3232
4ccd9b2f 3233 args->minlen = xfs_bmap_select_minlen(ap, args, *blen);
5cb4ffc8 3234 return error;
ff105f75
DC
3235}
3236
3cb68815 3237/* Update all inode and quota accounting for the allocation we just did. */
6d40dad2
CH
3238void
3239xfs_bmap_alloc_account(
0d263da3 3240 struct xfs_bmalloca *ap)
3cb68815 3241{
6d40dad2 3242 bool isrt = XFS_IS_REALTIME_INODE(ap->ip) &&
26da3999 3243 !(ap->flags & XFS_BMAPI_ATTRFORK);
6d40dad2
CH
3244 uint fld;
3245
d07cc724
DW
3246 if (ap->flags & XFS_BMAPI_COWFORK) {
3247 /*
3248 * COW fork blocks are in-core only and thus are treated as
3249 * in-core quota reservation (like delalloc blocks) even when
3250 * converted to real blocks. The quota reservation is not
3251 * accounted to disk until blocks are remapped to the data
3252 * fork. So if these blocks were previously delalloc, we
3253 * already have quota reservation and there's nothing to do
3254 * yet.
3255 */
f73690fe 3256 if (ap->wasdel) {
ac315eaf 3257 xfs_mod_delalloc(ap->ip, -(int64_t)ap->length, 0);
d07cc724 3258 return;
f73690fe 3259 }
d07cc724
DW
3260
3261 /*
3262 * Otherwise, we've allocated blocks in a hole. The transaction
3263 * has acquired in-core quota reservation for this extent.
3264 * Rather than account these as real blocks, however, we reduce
3265 * the transaction quota reservation based on the allocation.
3266 * This essentially transfers the transaction quota reservation
3267 * to that of a delalloc extent.
3268 */
0d263da3 3269 ap->ip->i_delayed_blks += ap->length;
6d40dad2
CH
3270 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, isrt ?
3271 XFS_TRANS_DQ_RES_RTBLKS : XFS_TRANS_DQ_RES_BLKS,
0d263da3 3272 -(long)ap->length);
d07cc724
DW
3273 return;
3274 }
3275
3276 /* data/attr fork only */
0d263da3 3277 ap->ip->i_nblocks += ap->length;
3cb68815 3278 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
f73690fe 3279 if (ap->wasdel) {
0d263da3 3280 ap->ip->i_delayed_blks -= ap->length;
ac315eaf 3281 xfs_mod_delalloc(ap->ip, -(int64_t)ap->length, 0);
6d40dad2
CH
3282 fld = isrt ? XFS_TRANS_DQ_DELRTBCOUNT : XFS_TRANS_DQ_DELBCOUNT;
3283 } else {
3284 fld = isrt ? XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
f73690fe 3285 }
6d40dad2
CH
3286
3287 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, fld, ap->length);
3cb68815
DW
3288}
3289
3f08f006
CB
3290static int
3291xfs_bmap_compute_alignments(
3292 struct xfs_bmalloca *ap,
3293 struct xfs_alloc_arg *args)
3294{
3295 struct xfs_mount *mp = args->mp;
3296 xfs_extlen_t align = 0; /* minimum allocation alignment */
3297 int stripe_align = 0;
3f08f006
CB
3298
3299 /* stripe alignment for allocation is determined by mount parameters */
914e2a04 3300 if (mp->m_swidth && xfs_has_swalloc(mp))
3f08f006
CB
3301 stripe_align = mp->m_swidth;
3302 else if (mp->m_dalign)
3303 stripe_align = mp->m_dalign;
3304
3305 if (ap->flags & XFS_BMAPI_COWFORK)
3306 align = xfs_get_cowextsz_hint(ap->ip);
3307 else if (ap->datatype & XFS_ALLOC_USERDATA)
3308 align = xfs_get_extsz_hint(ap->ip);
5049ea2e
JG
3309
3310 /* Try to align start block to any minimum allocation alignment */
3311 if (align > 1 && (ap->flags & XFS_BMAPI_EXTSZALIGN))
3312 args->alignment = align;
3313
3f08f006 3314 if (align) {
3af6ab0a
CB
3315 if (xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, align, 0,
3316 ap->eof, 0, ap->conv, &ap->offset,
3317 &ap->length))
3318 ASSERT(0);
3f08f006
CB
3319 ASSERT(ap->length);
3320 }
3321
3322 /* apply extent size hints if obtained earlier */
3323 if (align) {
3324 args->prod = align;
3325 div_u64_rem(ap->offset, args->prod, &args->mod);
3326 if (args->mod)
3327 args->mod = args->prod - args->mod;
3328 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
3329 args->prod = 1;
3330 args->mod = 0;
3331 } else {
3332 args->prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
3333 div_u64_rem(ap->offset, args->prod, &args->mod);
3334 if (args->mod)
3335 args->mod = args->prod - args->mod;
3336 }
3337
3338 return stripe_align;
3339}
3340
fc177ab0
CB
3341static void
3342xfs_bmap_process_allocated_extent(
3343 struct xfs_bmalloca *ap,
3344 struct xfs_alloc_arg *args,
3345 xfs_fileoff_t orig_offset,
3346 xfs_extlen_t orig_length)
3347{
fc177ab0 3348 ap->blkno = args->fsbno;
fc177ab0
CB
3349 ap->length = args->len;
3350 /*
3351 * If the extent size hint is active, we tried to round the
3352 * caller's allocation request offset down to extsz and the
3353 * length up to another extsz boundary. If we found a free
3354 * extent we mapped it in starting at this new offset. If the
3355 * newly mapped space isn't long enough to cover any of the
3356 * range of offsets that was originally requested, move the
3357 * mapping up so that we can fill as much of the caller's
3358 * original request as possible. Free space is apparently
3359 * very fragmented so we're unlikely to be able to satisfy the
3360 * hints anyway.
3361 */
3362 if (ap->length <= orig_length)
3363 ap->offset = orig_offset;
3364 else if (ap->offset + ap->length < orig_offset + orig_length)
3365 ap->offset = orig_offset + orig_length - ap->length;
6d40dad2 3366 xfs_bmap_alloc_account(ap);
fc177ab0
CB
3367}
3368
3006cea4
CB
3369static int
3370xfs_bmap_exact_minlen_extent_alloc(
31f5b24c
CH
3371 struct xfs_bmalloca *ap,
3372 struct xfs_alloc_arg *args)
b3563c19 3373{
3006cea4 3374 if (ap->minlen != 1) {
31f5b24c 3375 args->fsbno = NULLFSBLOCK;
3006cea4
CB
3376 return 0;
3377 }
3378
31f5b24c
CH
3379 args->alloc_minlen_only = 1;
3380 args->minlen = args->maxlen = ap->minlen;
3381 args->total = ap->total;
3006cea4 3382
80375d24
DC
3383 /*
3384 * Unlike the longest extent available in an AG, we don't track
3385 * the length of an AG's shortest extent.
3386 * XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT is a debug only knob and
3387 * hence we can afford to start traversing from the 0th AG since
3388 * we need not be concerned about a drop in performance in
3389 * "debug only" code paths.
3390 */
31f5b24c 3391 ap->blkno = XFS_AGB_TO_FSB(ap->ip->i_mount, 0, 0);
3006cea4 3392
43f4e9be
CH
3393 /*
3394 * Call xfs_bmap_btalloc_low_space here as it first does a "normal" AG
3395 * iteration and then drops args->total to args->minlen, which might be
3396 * required to find an allocation for the transaction reservation when
3397 * the file system is very full.
3398 */
3399 return xfs_bmap_btalloc_low_space(ap, args);
3006cea4 3400}
3006cea4 3401
3ed4e682
DC
3402/*
3403 * If we are not low on available data blocks and we are allocating at
3404 * EOF, optimise allocation for contiguous file extension and/or stripe
3405 * alignment of the new extent.
3406 *
3407 * NOTE: ap->aeof is only set if the allocation length is >= the
3408 * stripe unit and the allocation offset is at the end of file.
3409 */
3410static int
3411xfs_bmap_btalloc_at_eof(
3412 struct xfs_bmalloca *ap,
3413 struct xfs_alloc_arg *args,
3414 xfs_extlen_t blen,
1a5a98d5
DC
3415 int stripe_align,
3416 bool ag_only)
3006cea4 3417{
3ed4e682 3418 struct xfs_mount *mp = args->mp;
c371ca85 3419 struct xfs_perag *caller_pag = args->pag;
3006cea4 3420 int error;
3006cea4 3421
3ed4e682 3422 /*
c89b1f70
JZ
3423 * If there are already extents in the file, and xfs_bmap_adjacent() has
3424 * given a better blkno, try an exact EOF block allocation to extend the
3425 * file as a contiguous extent. If that fails, or it's the first
3426 * allocation in a file, just try for a stripe aligned allocation.
3ed4e682 3427 */
c89b1f70 3428 if (ap->eof) {
3ed4e682 3429 xfs_extlen_t nextminlen = 0;
ff105f75 3430
3ed4e682
DC
3431 /*
3432 * Compute the minlen+alignment for the next case. Set slop so
3433 * that the value of minlen+alignment+slop doesn't go up between
3434 * the calls.
3435 */
32cd26bf 3436 args->alignment = 1;
3ed4e682
DC
3437 if (blen > stripe_align && blen <= args->maxlen)
3438 nextminlen = blen - stripe_align;
3439 else
3440 nextminlen = args->minlen;
3441 if (nextminlen + stripe_align > args->minlen + 1)
3442 args->minalignslop = nextminlen + stripe_align -
3443 args->minlen - 1;
3444 else
3445 args->minalignslop = 0;
3446
c371ca85
DC
3447 if (!caller_pag)
3448 args->pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, ap->blkno));
c4e10f2b 3449 error = xfs_alloc_vextent_exact_bno(args, ap->blkno);
2897a1c2 3450 if (!caller_pag) {
c371ca85 3451 xfs_perag_put(args->pag);
2897a1c2
DW
3452 args->pag = NULL;
3453 }
3ed4e682
DC
3454 if (error)
3455 return error;
ff105f75 3456
3ed4e682
DC
3457 if (args->fsbno != NULLFSBLOCK)
3458 return 0;
3459 /*
3460 * Exact allocation failed. Reset to try an aligned allocation
3461 * according to the original allocation specification.
3462 */
3ed4e682
DC
3463 args->alignment = stripe_align;
3464 args->minlen = nextminlen;
3465 args->minalignslop = 0;
3466 } else {
3ed4e682
DC
3467 /*
3468 * Adjust minlen to try and preserve alignment if we
3469 * can't guarantee an aligned maxlen extent.
3470 */
32cd26bf 3471 args->alignment = stripe_align;
3ed4e682
DC
3472 if (blen > args->alignment &&
3473 blen <= args->maxlen + args->alignment)
3474 args->minlen = blen - args->alignment;
3475 args->minalignslop = 0;
3476 }
3477
c371ca85 3478 if (ag_only) {
15653695 3479 error = xfs_alloc_vextent_near_bno(args, ap->blkno);
c371ca85
DC
3480 } else {
3481 args->pag = NULL;
1a5a98d5 3482 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
c371ca85
DC
3483 ASSERT(args->pag == NULL);
3484 args->pag = caller_pag;
3485 }
3ed4e682
DC
3486 if (error)
3487 return error;
3488
3489 if (args->fsbno != NULLFSBLOCK)
3490 return 0;
3491
3492 /*
3493 * Allocation failed, so turn return the allocation args to their
3494 * original non-aligned state so the caller can proceed on allocation
3495 * failure as if this function was never called.
3496 */
3ed4e682
DC
3497 args->alignment = 1;
3498 return 0;
3499}
3500
451b902f
DC
3501/*
3502 * We have failed multiple allocation attempts so now are in a low space
3503 * allocation situation. Try a locality first full filesystem minimum length
3504 * allocation whilst still maintaining necessary total block reservation
3505 * requirements.
3506 *
3507 * If that fails, we are now critically low on space, so perform a last resort
3508 * allocation attempt: no reserve, no locality, blocking, minimum length, full
3509 * filesystem free space scan. We also indicate to future allocations in this
3510 * transaction that we are critically low on space so they don't waste time on
3511 * allocation modes that are unlikely to succeed.
3512 */
12ae6818 3513int
451b902f
DC
3514xfs_bmap_btalloc_low_space(
3515 struct xfs_bmalloca *ap,
3516 struct xfs_alloc_arg *args)
3517{
3518 int error;
3519
3520 if (args->minlen > ap->minlen) {
3521 args->minlen = ap->minlen;
3522 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3523 if (error || args->fsbno != NULLFSBLOCK)
3524 return error;
3525 }
3526
3527 /* Last ditch attempt before failure is declared. */
3528 args->total = ap->minlen;
3529 error = xfs_alloc_vextent_first_ag(args, 0);
3530 if (error)
3531 return error;
3532 ap->tp->t_flags |= XFS_TRANS_LOWMODE;
3533 return 0;
3534}
3535
3536static int
3537xfs_bmap_btalloc_filestreams(
3ed4e682
DC
3538 struct xfs_bmalloca *ap,
3539 struct xfs_alloc_arg *args,
3540 int stripe_align)
3541{
3ed4e682 3542 xfs_extlen_t blen = 0;
c371ca85 3543 int error = 0;
3ed4e682 3544
12ae6818
DC
3545
3546 error = xfs_filestream_select_ag(ap, args, &blen);
3547 if (error)
3548 return error;
c371ca85 3549 ASSERT(args->pag);
1a5a98d5 3550
3ed4e682 3551 /*
12ae6818
DC
3552 * If we are in low space mode, then optimal allocation will fail so
3553 * prepare for minimal allocation and jump to the low space algorithm
3554 * immediately.
3ed4e682 3555 */
451b902f
DC
3556 if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3557 args->minlen = ap->minlen;
c371ca85 3558 ASSERT(args->fsbno == NULLFSBLOCK);
12ae6818 3559 goto out_low_space;
80375d24 3560 }
80375d24 3561
12ae6818 3562 args->minlen = xfs_bmap_select_minlen(ap, args, blen);
c371ca85 3563 if (ap->aeof)
451b902f
DC
3564 error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
3565 true);
451b902f 3566
c371ca85
DC
3567 if (!error && args->fsbno == NULLFSBLOCK)
3568 error = xfs_alloc_vextent_near_bno(args, ap->blkno);
3569
3570out_low_space:
3571 /*
3572 * We are now done with the perag reference for the filestreams
3573 * association provided by xfs_filestream_select_ag(). Release it now as
3574 * we've either succeeded, had a fatal error or we are out of space and
3575 * need to do a full filesystem scan for free space which will take it's
3576 * own references.
3577 */
3578 xfs_perag_rele(args->pag);
3579 args->pag = NULL;
451b902f
DC
3580 if (error || args->fsbno != NULLFSBLOCK)
3581 return error;
3582
3583 return xfs_bmap_btalloc_low_space(ap, args);
3584}
3585
3586static int
3587xfs_bmap_btalloc_best_length(
3588 struct xfs_bmalloca *ap,
3589 struct xfs_alloc_arg *args,
3590 int stripe_align)
3591{
3592 xfs_extlen_t blen = 0;
3593 int error;
3594
3595 ap->blkno = XFS_INO_TO_FSB(args->mp, ap->ip->i_ino);
c89b1f70
JZ
3596 if (!xfs_bmap_adjacent(ap))
3597 ap->eof = false;
451b902f
DC
3598
3599 /*
3600 * Search for an allocation group with a single extent large enough for
3601 * the request. If one isn't found, then adjust the minimum allocation
3602 * size to the largest space found.
3603 */
3604 error = xfs_bmap_btalloc_select_lengths(ap, args, &blen);
80375d24
DC
3605 if (error)
3606 return error;
3f08f006 3607
2bd0ea18 3608 /*
3ed4e682
DC
3609 * Don't attempt optimal EOF allocation if previous allocations barely
3610 * succeeded due to being near ENOSPC. It is highly unlikely we'll get
3611 * optimal or even aligned allocations in this case, so don't waste time
3612 * trying.
2bd0ea18 3613 */
3ed4e682 3614 if (ap->aeof && !(ap->tp->t_flags & XFS_TRANS_LOWMODE)) {
1a5a98d5 3615 error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
451b902f
DC
3616 false);
3617 if (error || args->fsbno != NULLFSBLOCK)
3ed4e682 3618 return error;
49f693fa 3619 }
9542ae13 3620
451b902f
DC
3621 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3622 if (error || args->fsbno != NULLFSBLOCK)
49f693fa 3623 return error;
3ed4e682 3624
451b902f 3625 return xfs_bmap_btalloc_low_space(ap, args);
3ed4e682
DC
3626}
3627
3628static int
3629xfs_bmap_btalloc(
3630 struct xfs_bmalloca *ap)
3631{
3632 struct xfs_mount *mp = ap->ip->i_mount;
3633 struct xfs_alloc_arg args = {
3634 .tp = ap->tp,
3635 .mp = mp,
3636 .fsbno = NULLFSBLOCK,
3637 .oinfo = XFS_RMAP_OINFO_SKIP_UPDATE,
3638 .minleft = ap->minleft,
3639 .wasdel = ap->wasdel,
3640 .resv = XFS_AG_RESV_NONE,
3641 .datatype = ap->datatype,
3642 .alignment = 1,
3643 .minalignslop = 0,
3644 };
3645 xfs_fileoff_t orig_offset;
3646 xfs_extlen_t orig_length;
3647 int error;
3648 int stripe_align;
3649
3650 ASSERT(ap->length);
3651 orig_offset = ap->offset;
3652 orig_length = ap->length;
3653
3654 stripe_align = xfs_bmap_compute_alignments(ap, &args);
3655
3656 /* Trim the allocation back to the maximum an AG can fit. */
3657 args.maxlen = min(ap->length, mp->m_ag_max_usable);
3658
31f5b24c
CH
3659 if (unlikely(XFS_TEST_ERROR(false, mp,
3660 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
3661 error = xfs_bmap_exact_minlen_extent_alloc(ap, &args);
3662 else if ((ap->datatype & XFS_ALLOC_USERDATA) &&
3663 xfs_inode_is_filestream(ap->ip))
451b902f
DC
3664 error = xfs_bmap_btalloc_filestreams(ap, &args, stripe_align);
3665 else
3666 error = xfs_bmap_btalloc_best_length(ap, &args, stripe_align);
3ed4e682
DC
3667 if (error)
3668 return error;
fc177ab0 3669
49f693fa 3670 if (args.fsbno != NULLFSBLOCK) {
fc177ab0
CB
3671 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3672 orig_length);
49f693fa
DC
3673 } else {
3674 ap->blkno = NULLFSBLOCK;
3675 ap->length = 0;
2bd0ea18 3676 }
2bd0ea18 3677 return 0;
56b2de80
DC
3678}
3679
b3fd8db7
DW
3680/* Trim extent to fit a logical block range. */
3681void
3682xfs_trim_extent(
3683 struct xfs_bmbt_irec *irec,
3684 xfs_fileoff_t bno,
3685 xfs_filblks_t len)
3686{
3687 xfs_fileoff_t distance;
3688 xfs_fileoff_t end = bno + len;
3689
3690 if (irec->br_startoff + irec->br_blockcount <= bno ||
3691 irec->br_startoff >= end) {
3692 irec->br_blockcount = 0;
3693 return;
3694 }
3695
3696 if (irec->br_startoff < bno) {
3697 distance = bno - irec->br_startoff;
3698 if (isnullstartblock(irec->br_startblock))
3699 irec->br_startblock = DELAYSTARTBLOCK;
3700 if (irec->br_startblock != DELAYSTARTBLOCK &&
3701 irec->br_startblock != HOLESTARTBLOCK)
3702 irec->br_startblock += distance;
3703 irec->br_startoff += distance;
3704 irec->br_blockcount -= distance;
3705 }
3706
3707 if (end < irec->br_startoff + irec->br_blockcount) {
3708 distance = irec->br_startoff + irec->br_blockcount - end;
3709 irec->br_blockcount -= distance;
3710 }
3711}
3712
2bd0ea18 3713/*
a2ceac1f 3714 * Trim the returned map to the required bounds
2bd0ea18 3715 */
a2ceac1f
DC
3716STATIC void
3717xfs_bmapi_trim_map(
3718 struct xfs_bmbt_irec *mval,
3719 struct xfs_bmbt_irec *got,
3720 xfs_fileoff_t *bno,
3721 xfs_filblks_t len,
3722 xfs_fileoff_t obno,
3723 xfs_fileoff_t end,
3724 int n,
6e22af31 3725 uint32_t flags)
2bd0ea18 3726{
a2ceac1f
DC
3727 if ((flags & XFS_BMAPI_ENTIRE) ||
3728 got->br_startoff + got->br_blockcount <= obno) {
3729 *mval = *got;
3730 if (isnullstartblock(got->br_startblock))
3731 mval->br_startblock = DELAYSTARTBLOCK;
3732 return;
63be04eb 3733 }
a2ceac1f
DC
3734
3735 if (obno > *bno)
3736 *bno = obno;
3737 ASSERT((*bno >= obno) || (n == 0));
3738 ASSERT(*bno < end);
3739 mval->br_startoff = *bno;
3740 if (isnullstartblock(got->br_startblock))
3741 mval->br_startblock = DELAYSTARTBLOCK;
2bd0ea18 3742 else
a2ceac1f
DC
3743 mval->br_startblock = got->br_startblock +
3744 (*bno - got->br_startoff);
2bd0ea18 3745 /*
a2ceac1f
DC
3746 * Return the minimum of what we got and what we asked for for
3747 * the length. We can use the len variable here because it is
3748 * modified below and we could have been there before coming
3749 * here if the first part of the allocation didn't overlap what
3750 * was asked for.
2bd0ea18 3751 */
a2ceac1f
DC
3752 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3753 got->br_blockcount - (*bno - got->br_startoff));
3754 mval->br_state = got->br_state;
3755 ASSERT(mval->br_blockcount <= len);
3756 return;
3757}
56b2de80 3758
a2ceac1f
DC
3759/*
3760 * Update and validate the extent map to return
3761 */
3762STATIC void
3763xfs_bmapi_update_map(
3764 struct xfs_bmbt_irec **map,
3765 xfs_fileoff_t *bno,
3766 xfs_filblks_t *len,
3767 xfs_fileoff_t obno,
3768 xfs_fileoff_t end,
3769 int *n,
6e22af31 3770 uint32_t flags)
a2ceac1f
DC
3771{
3772 xfs_bmbt_irec_t *mval = *map;
3773
3774 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3775 ((mval->br_startoff + mval->br_blockcount) <= end));
3776 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3777 (mval->br_startoff < obno));
3778
3779 *bno = mval->br_startoff + mval->br_blockcount;
3780 *len = end - *bno;
3781 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3782 /* update previous map with new information */
3783 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3784 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3785 ASSERT(mval->br_state == mval[-1].br_state);
3786 mval[-1].br_blockcount = mval->br_blockcount;
3787 mval[-1].br_state = mval->br_state;
3788 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3789 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3790 mval[-1].br_startblock != HOLESTARTBLOCK &&
3791 mval->br_startblock == mval[-1].br_startblock +
3792 mval[-1].br_blockcount &&
8197bceb 3793 mval[-1].br_state == mval->br_state) {
a2ceac1f
DC
3794 ASSERT(mval->br_startoff ==
3795 mval[-1].br_startoff + mval[-1].br_blockcount);
3796 mval[-1].br_blockcount += mval->br_blockcount;
3797 } else if (*n > 0 &&
3798 mval->br_startblock == DELAYSTARTBLOCK &&
3799 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3800 mval->br_startoff ==
3801 mval[-1].br_startoff + mval[-1].br_blockcount) {
3802 mval[-1].br_blockcount += mval->br_blockcount;
3803 mval[-1].br_state = mval->br_state;
3804 } else if (!((*n == 0) &&
3805 ((mval->br_startoff + mval->br_blockcount) <=
3806 obno))) {
3807 mval++;
3808 (*n)++;
3809 }
3810 *map = mval;
3811}
399ab595 3812
a2ceac1f
DC
3813/*
3814 * Map file blocks to filesystem blocks without allocation.
3815 */
3816int
3817xfs_bmapi_read(
3818 struct xfs_inode *ip,
3819 xfs_fileoff_t bno,
3820 xfs_filblks_t len,
3821 struct xfs_bmbt_irec *mval,
3822 int *nmap,
6e22af31 3823 uint32_t flags)
a2ceac1f
DC
3824{
3825 struct xfs_mount *mp = ip->i_mount;
212be827 3826 int whichfork = xfs_bmapi_whichfork(flags);
722e81c1 3827 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
a2ceac1f 3828 struct xfs_bmbt_irec got;
a2ceac1f
DC
3829 xfs_fileoff_t obno;
3830 xfs_fileoff_t end;
9788e059 3831 struct xfs_iext_cursor icur;
a2ceac1f 3832 int error;
c2d73ed3 3833 bool eof = false;
a2ceac1f 3834 int n = 0;
399ab595 3835
a2ceac1f 3836 ASSERT(*nmap >= 1);
00773e64 3837 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE)));
d3d1a2cb 3838 xfs_assert_ilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL);
062998e3 3839
6d5b5846
DW
3840 if (WARN_ON_ONCE(!ifp)) {
3841 xfs_bmap_mark_sick(ip, whichfork);
212be827 3842 return -EFSCORRUPTED;
6d5b5846 3843 }
212be827 3844
d967a68d 3845 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
6d5b5846
DW
3846 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
3847 xfs_bmap_mark_sick(ip, whichfork);
12b53197 3848 return -EFSCORRUPTED;
6d5b5846 3849 }
062998e3 3850
93adb06a 3851 if (xfs_is_shutdown(mp))
12b53197 3852 return -EIO;
399ab595 3853
79896434 3854 XFS_STATS_INC(mp, xs_blk_mapr);
a2ceac1f 3855
e00c57e7
CH
3856 error = xfs_iread_extents(NULL, ip, whichfork);
3857 if (error)
3858 return error;
a2ceac1f 3859
9788e059 3860 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
c2d73ed3 3861 eof = true;
a2ceac1f
DC
3862 end = bno + len;
3863 obno = bno;
3864
3865 while (bno < end && n < *nmap) {
3866 /* Reading past eof, act as though there's a hole up to end. */
3867 if (eof)
3868 got.br_startoff = end;
3869 if (got.br_startoff > bno) {
3870 /* Reading in a hole. */
2bd0ea18
NS
3871 mval->br_startoff = bno;
3872 mval->br_startblock = HOLESTARTBLOCK;
3873 mval->br_blockcount =
3874 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3875 mval->br_state = XFS_EXT_NORM;
3876 bno += mval->br_blockcount;
3877 len -= mval->br_blockcount;
3878 mval++;
3879 n++;
3880 continue;
3881 }
a2ceac1f
DC
3882
3883 /* set up the extent map to return. */
3884 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3885 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3886
3887 /* If we're done, stop now. */
3888 if (bno >= end || n >= *nmap)
3889 break;
3890
3891 /* Else go on to the next record. */
9788e059 3892 if (!xfs_iext_next_extent(ifp, &icur, &got))
c2d73ed3 3893 eof = true;
a2ceac1f
DC
3894 }
3895 *nmap = n;
3896 return 0;
3897}
3898
ff105f75
DC
3899static int
3900xfs_bmapi_allocate(
a2ceac1f
DC
3901 struct xfs_bmalloca *bma)
3902{
3903 struct xfs_mount *mp = bma->ip->i_mount;
1277a5e0 3904 int whichfork = xfs_bmapi_whichfork(bma->flags);
722e81c1 3905 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
a2ceac1f
DC
3906 int error;
3907
3908 ASSERT(bma->length > 0);
3c9133f8 3909 ASSERT(bma->length <= XFS_MAX_BMBT_EXTLEN);
a2ceac1f 3910
2ac7663a
CH
3911 if (bma->flags & XFS_BMAPI_CONTIG)
3912 bma->minlen = bma->length;
3913 else
3914 bma->minlen = 1;
a2ceac1f 3915
a7c063b2
CH
3916 if (!(bma->flags & XFS_BMAPI_METADATA)) {
3917 /*
3918 * For the data and COW fork, the first data in the file is
3919 * treated differently to all other allocations. For the
3920 * attribute fork, we only need to ensure the allocated range
3921 * is not on the busy list.
3922 */
3923 bma->datatype = XFS_ALLOC_NOBUSY;
3924 if (whichfork == XFS_DATA_FORK || whichfork == XFS_COW_FORK) {
3925 bma->datatype |= XFS_ALLOC_USERDATA;
3926 if (bma->offset == 0)
3927 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
3928
3929 if (mp->m_dalign && bma->length >= mp->m_dalign) {
3930 error = xfs_bmap_isaeof(bma, whichfork);
3931 if (error)
3932 return error;
3933 }
3934 }
3006cea4 3935 }
a7c063b2
CH
3936
3937 if ((bma->datatype & XFS_ALLOC_USERDATA) &&
3938 XFS_IS_REALTIME_INODE(bma->ip))
3939 error = xfs_bmap_rtalloc(bma);
a7c063b2
CH
3940 else
3941 error = xfs_bmap_btalloc(bma);
4372cf65 3942 if (error)
a2ceac1f 3943 return error;
4372cf65
CH
3944 if (bma->blkno == NULLFSBLOCK)
3945 return -ENOSPC;
a2ceac1f 3946
32b91c2a
CH
3947 if (WARN_ON_ONCE(!xfs_valid_startblock(bma->ip, bma->blkno))) {
3948 xfs_bmap_mark_sick(bma->ip, whichfork);
3949 return -EFSCORRUPTED;
3950 }
3951
c3a24cde
CH
3952 if (bma->flags & XFS_BMAPI_ZERO) {
3953 error = xfs_zero_extent(bma->ip, bma->blkno, bma->length);
3954 if (error)
3955 return error;
3956 }
3957
84094546 3958 if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur)
a2ceac1f 3959 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
a2ceac1f
DC
3960 /*
3961 * Bump the number of extents we've allocated
3962 * in this call.
3963 */
3964 bma->nallocs++;
3965
d242dfc3
CH
3966 if (bma->cur && bma->wasdel)
3967 bma->cur->bc_flags |= XFS_BTREE_BMBT_WASDEL;
a2ceac1f
DC
3968
3969 bma->got.br_startoff = bma->offset;
3970 bma->got.br_startblock = bma->blkno;
3971 bma->got.br_blockcount = bma->length;
3972 bma->got.br_state = XFS_EXT_NORM;
3973
edc9bb69 3974 if (bma->flags & XFS_BMAPI_PREALLOC)
a2ceac1f
DC
3975 bma->got.br_state = XFS_EXT_UNWRITTEN;
3976
3977 if (bma->wasdel)
1277a5e0 3978 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
a2ceac1f 3979 else
972432f2 3980 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
9788e059 3981 whichfork, &bma->icur, &bma->cur, &bma->got,
64e8b4a7 3982 &bma->logflags, bma->flags);
a2ceac1f
DC
3983 if (error)
3984 return error;
3985
3986 /*
3987 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
3988 * or xfs_bmap_add_extent_hole_real might have merged it into one of
3989 * the neighbouring ones.
3990 */
9788e059 3991 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
a2ceac1f
DC
3992
3993 ASSERT(bma->got.br_startoff <= bma->offset);
3994 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
3995 bma->offset + bma->length);
3996 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
3997 bma->got.br_state == XFS_EXT_UNWRITTEN);
3998 return 0;
3999}
4000
a2ceac1f
DC
4001STATIC int
4002xfs_bmapi_convert_unwritten(
4003 struct xfs_bmalloca *bma,
4004 struct xfs_bmbt_irec *mval,
4005 xfs_filblks_t len,
6e22af31 4006 uint32_t flags)
a2ceac1f 4007{
cb8a004a 4008 int whichfork = xfs_bmapi_whichfork(flags);
722e81c1 4009 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
a2ceac1f
DC
4010 int tmp_logflags = 0;
4011 int error;
4012
4013 /* check if we need to do unwritten->real conversion */
4014 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4015 (flags & XFS_BMAPI_PREALLOC))
4016 return 0;
4017
4018 /* check if we need to do real->unwritten conversion */
4019 if (mval->br_state == XFS_EXT_NORM &&
4020 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4021 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4022 return 0;
4023
4024 /*
4025 * Modify (by adding) the state flag, if writing.
4026 */
4027 ASSERT(mval->br_blockcount <= len);
84094546 4028 if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur) {
a2ceac1f
DC
4029 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4030 bma->ip, whichfork);
a2ceac1f
DC
4031 }
4032 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4033 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4034
9542ae13
DC
4035 /*
4036 * Before insertion into the bmbt, zero the range being converted
4037 * if required.
4038 */
4039 if (flags & XFS_BMAPI_ZERO) {
4040 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4041 mval->br_blockcount);
4042 if (error)
4043 return error;
4044 }
4045
4072e4b4 4046 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
64e8b4a7 4047 &bma->icur, &bma->cur, mval, &tmp_logflags);
23fc058d
BF
4048 /*
4049 * Log the inode core unconditionally in the unwritten extent conversion
4050 * path because the conversion might not have done so (e.g., if the
4051 * extent count hasn't changed). We need to make sure the inode is dirty
4052 * in the transaction for the sake of fsync(), even if nothing has
4053 * changed, because fsync() will not force the log for this transaction
4054 * unless it sees the inode pinned.
4072e4b4
DW
4055 *
4056 * Note: If we're only converting cow fork extents, there aren't
4057 * any on-disk updates to make, so we don't need to log anything.
23fc058d 4058 */
4072e4b4
DW
4059 if (whichfork != XFS_COW_FORK)
4060 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
a2ceac1f
DC
4061 if (error)
4062 return error;
4063
4064 /*
4065 * Update our extent pointer, given that
4066 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4067 * of the neighbouring ones.
4068 */
9788e059 4069 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
a2ceac1f
DC
4070
4071 /*
4072 * We may have combined previously unwritten space with written space,
4073 * so generate another request.
4074 */
4075 if (mval->br_blockcount < len)
12b53197 4076 return -EAGAIN;
a2ceac1f
DC
4077 return 0;
4078}
4079
4442ae95 4080xfs_extlen_t
ee622798
CH
4081xfs_bmapi_minleft(
4082 struct xfs_trans *tp,
4083 struct xfs_inode *ip,
4084 int fork)
4085{
722e81c1 4086 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, fork);
d967a68d 4087
9cdecd7e 4088 if (tp && tp->t_highest_agno != NULLAGNUMBER)
ee622798 4089 return 0;
d967a68d 4090 if (ifp->if_format != XFS_DINODE_FMT_BTREE)
ee622798 4091 return 1;
d967a68d 4092 return be16_to_cpu(ifp->if_broot->bb_level) + 1;
ee622798
CH
4093}
4094
4095/*
4096 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4097 * a case where the data is changed, there's an error, and it's not logged so we
4098 * don't shutdown when we should. Don't bother logging extents/btree changes if
4099 * we converted to the other format.
4100 */
4101static void
4102xfs_bmapi_finish(
4103 struct xfs_bmalloca *bma,
4104 int whichfork,
4105 int error)
4106{
722e81c1 4107 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
d967a68d 4108
ee622798 4109 if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
d967a68d 4110 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
ee622798
CH
4111 bma->logflags &= ~xfs_ilog_fext(whichfork);
4112 else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
d967a68d 4113 ifp->if_format != XFS_DINODE_FMT_BTREE)
ee622798
CH
4114 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4115
4116 if (bma->logflags)
4117 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4118 if (bma->cur)
4119 xfs_btree_del_cursor(bma->cur, error);
4120}
4121
a2ceac1f
DC
4122/*
4123 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4124 * extent state if necessary. Details behaviour is controlled by the flags
4125 * parameter. Only allocates blocks from a single allocation group, to avoid
4126 * locking problems.
4372cf65
CH
4127 *
4128 * Returns 0 on success and places the extent mappings in mval. nmaps is used
4129 * as an input/output parameter where the caller specifies the maximum number
4130 * of mappings that may be returned and xfs_bmapi_write passes back the number
4131 * of mappings (including existing mappings) it found.
4132 *
4133 * Returns a negative error code on failure, including -ENOSPC when it could not
4134 * allocate any blocks and -ENOSR when it did allocate blocks to convert a
4135 * delalloc range, but those blocks were before the passed in range.
a2ceac1f
DC
4136 */
4137int
4138xfs_bmapi_write(
4139 struct xfs_trans *tp, /* transaction pointer */
4140 struct xfs_inode *ip, /* incore inode */
4141 xfs_fileoff_t bno, /* starting file offs. mapped */
4142 xfs_filblks_t len, /* length to map in file */
6e22af31 4143 uint32_t flags, /* XFS_BMAPI_... */
a2ceac1f
DC
4144 xfs_extlen_t total, /* total blocks needed */
4145 struct xfs_bmbt_irec *mval, /* output: map values */
cbdfb3ab 4146 int *nmap) /* i/o: mval size/count */
a2ceac1f 4147{
93ad4d9c
DW
4148 struct xfs_bmalloca bma = {
4149 .tp = tp,
4150 .ip = ip,
4151 .total = total,
4152 };
a2ceac1f 4153 struct xfs_mount *mp = ip->i_mount;
d967a68d 4154 int whichfork = xfs_bmapi_whichfork(flags);
722e81c1 4155 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
a2ceac1f 4156 xfs_fileoff_t end; /* end of mapped file region */
82411945 4157 bool eof = false; /* after the end of extents */
a2ceac1f
DC
4158 int error; /* error return */
4159 int n; /* current extent index */
4160 xfs_fileoff_t obno; /* old block number (offset) */
a2ceac1f
DC
4161
4162#ifdef DEBUG
4163 xfs_fileoff_t orig_bno; /* original block number value */
4164 int orig_flags; /* original flags arg value */
4165 xfs_filblks_t orig_len; /* original value of len arg */
4166 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4167 int orig_nmap; /* original value of *nmap */
4168
4169 orig_bno = bno;
4170 orig_len = len;
4171 orig_flags = flags;
4172 orig_mval = mval;
4173 orig_nmap = *nmap;
4174#endif
4175
4176 ASSERT(*nmap >= 1);
4177 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
9fa4db19 4178 ASSERT(tp != NULL);
a2ceac1f 4179 ASSERT(len > 0);
d967a68d 4180 ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL);
d3d1a2cb 4181 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
0b44aa85 4182 ASSERT(!(flags & XFS_BMAPI_REMAP));
a2ceac1f 4183
9542ae13
DC
4184 /* zeroing is for currently only for data extents, not metadata */
4185 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4186 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4187 /*
4188 * we can allocate unwritten extents or pre-zero allocated blocks,
4189 * but it makes no sense to do both at once. This would result in
4190 * zeroing the unwritten extent twice, but it still being an
4191 * unwritten extent....
4192 */
4193 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4194 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4195
d967a68d 4196 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
bc73da84 4197 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
6d5b5846 4198 xfs_bmap_mark_sick(ip, whichfork);
12b53197 4199 return -EFSCORRUPTED;
a2ceac1f
DC
4200 }
4201
93adb06a 4202 if (xfs_is_shutdown(mp))
12b53197 4203 return -EIO;
a2ceac1f 4204
79896434 4205 XFS_STATS_INC(mp, xs_blk_mapw);
a2ceac1f 4206
e00c57e7
CH
4207 error = xfs_iread_extents(tp, ip, whichfork);
4208 if (error)
4209 goto error0;
a2ceac1f 4210
9788e059 4211 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
82411945 4212 eof = true;
9788e059 4213 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
82411945 4214 bma.prev.br_startoff = NULLFILEOFF;
ee622798 4215 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
a2ceac1f 4216
badaa597
BF
4217 n = 0;
4218 end = bno + len;
4219 obno = bno;
a2ceac1f 4220 while (bno < end && n < *nmap) {
7075a23f
CH
4221 bool need_alloc = false, wasdelay = false;
4222
faaad1df 4223 /* in hole or beyond EOF? */
7075a23f 4224 if (eof || bma.got.br_startoff > bno) {
faaad1df
DW
4225 /*
4226 * CoW fork conversions should /never/ hit EOF or
4227 * holes. There should always be something for us
4228 * to work on.
4229 */
4230 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4231 (flags & XFS_BMAPI_COWFORK)));
4232
330a35fe 4233 need_alloc = true;
0b44aa85
CH
4234 } else if (isnullstartblock(bma.got.br_startblock)) {
4235 wasdelay = true;
7075a23f 4236 }
34621a47 4237
2bd0ea18 4238 /*
a2ceac1f
DC
4239 * First, deal with the hole before the allocated space
4240 * that we found, if any.
2bd0ea18 4241 */
9fa4db19 4242 if (need_alloc || wasdelay) {
a2ceac1f
DC
4243 bma.eof = eof;
4244 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4245 bma.wasdel = wasdelay;
4246 bma.offset = bno;
4247 bma.flags = flags;
4248
2bd0ea18 4249 /*
a2ceac1f
DC
4250 * There's a 32/64 bit type mismatch between the
4251 * allocation length request (which can be 64 bits in
4252 * length) and the bma length request, which is
4253 * xfs_extlen_t and therefore 32 bits. Hence we have to
ae85a9c8
CH
4254 * be careful and do the min() using the larger type to
4255 * avoid overflows.
2bd0ea18 4256 */
ae85a9c8 4257 bma.length = XFS_FILBLKS_MIN(len, XFS_MAX_BMBT_EXTLEN);
a2ceac1f 4258
3c9133f8 4259 if (wasdelay) {
884c8799
CH
4260 bma.length = XFS_FILBLKS_MIN(bma.length,
4261 bma.got.br_blockcount -
4262 (bno - bma.got.br_startoff));
3c9133f8
CH
4263 } else {
4264 if (!eof)
4265 bma.length = XFS_FILBLKS_MIN(bma.length,
4266 bma.got.br_startoff - bno);
4267 }
4268
a2ceac1f
DC
4269 ASSERT(bma.length > 0);
4270 error = xfs_bmapi_allocate(&bma);
4372cf65
CH
4271 if (error) {
4272 /*
4273 * If we already allocated space in a previous
4274 * iteration return what we go so far when
4275 * running out of space.
4276 */
4277 if (error == -ENOSPC && bma.nallocs)
4278 break;
2bd0ea18 4279 goto error0;
4372cf65 4280 }
10e65503
DW
4281
4282 /*
4283 * If this is a CoW allocation, record the data in
4284 * the refcount btree for orphan recovery.
4285 */
5965a482 4286 if (whichfork == XFS_COW_FORK)
82d0c7cb
DW
4287 xfs_refcount_alloc_cow_extent(tp,
4288 XFS_IS_REALTIME_INODE(ip),
4289 bma.blkno, bma.length);
2bd0ea18
NS
4290 }
4291
a2ceac1f
DC
4292 /* Deal with the allocated space we found. */
4293 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4294 end, n, flags);
4295
4296 /* Execute unwritten extent conversion if necessary */
4297 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
12b53197 4298 if (error == -EAGAIN)
a2ceac1f
DC
4299 continue;
4300 if (error)
4301 goto error0;
4302
4303 /* update the extent map to return */
4304 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4305
2bd0ea18
NS
4306 /*
4307 * If we're done, stop now. Stop when we've allocated
4308 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4309 * the transaction may get too big.
4310 */
a2ceac1f 4311 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
2bd0ea18 4312 break;
a2ceac1f
DC
4313
4314 /* Else go on to the next record. */
4315 bma.prev = bma.got;
9788e059 4316 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
82411945 4317 eof = true;
2bd0ea18 4318 }
a2ceac1f 4319
939ebc1a
CH
4320 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4321 whichfork);
4322 if (error)
4323 goto error0;
a2ceac1f 4324
d967a68d 4325 ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE ||
87c472b7 4326 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork));
ee622798
CH
4327 xfs_bmapi_finish(&bma, whichfork, 0);
4328 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4372cf65
CH
4329 orig_nmap, n);
4330
4331 /*
4332 * When converting delayed allocations, xfs_bmapi_allocate ignores
4333 * the passed in bno and always converts from the start of the found
4334 * delalloc extent.
4335 *
4336 * To avoid a successful return with *nmap set to 0, return the magic
4337 * -ENOSR error code for this particular case so that the caller can
4338 * handle it.
4339 */
4340 if (!n) {
4341 ASSERT(bma.nallocs >= *nmap);
4342 return -ENOSR;
4343 }
4344 *nmap = n;
ee622798 4345 return 0;
2bd0ea18 4346error0:
ee622798 4347 xfs_bmapi_finish(&bma, whichfork, error);
2bd0ea18
NS
4348 return error;
4349}
4350
badaa597
BF
4351/*
4352 * Convert an existing delalloc extent to real blocks based on file offset. This
4353 * attempts to allocate the entire delalloc extent and may require multiple
4354 * invocations to allocate the target offset if a large enough physical extent
4355 * is not available.
4356 */
44e4bdc6
ZY
4357static int
4358xfs_bmapi_convert_one_delalloc(
badaa597 4359 struct xfs_inode *ip,
badaa597 4360 int whichfork,
75f533e6
CH
4361 xfs_off_t offset,
4362 struct iomap *iomap,
c784c9d2 4363 unsigned int *seq)
badaa597 4364{
722e81c1 4365 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
c784c9d2 4366 struct xfs_mount *mp = ip->i_mount;
75f533e6 4367 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
330a35fe 4368 struct xfs_bmalloca bma = { NULL };
75f533e6 4369 uint16_t flags = 0;
c784c9d2 4370 struct xfs_trans *tp;
badaa597 4371 int error;
badaa597 4372
75f533e6
CH
4373 if (whichfork == XFS_COW_FORK)
4374 flags |= IOMAP_F_SHARED;
4375
c784c9d2
CH
4376 /*
4377 * Space for the extent and indirect blocks was reserved when the
4378 * delalloc extent was created so there's no need to do so here.
4379 */
4380 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4381 XFS_TRANS_RESERVE, &tp);
4382 if (error)
4383 return error;
4384
4385 xfs_ilock(ip, XFS_ILOCK_EXCL);
fcba1629 4386 xfs_trans_ijoin(tp, ip, 0);
75cb5a60 4387
7163cfcd 4388 error = xfs_iext_count_extend(tp, ip, whichfork,
75cb5a60
CB
4389 XFS_IEXT_ADD_NOSPLIT_CNT);
4390 if (error)
4391 goto out_trans_cancel;
4392
330a35fe
CH
4393 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4394 bma.got.br_startoff > offset_fsb) {
4395 /*
4396 * No extent found in the range we are trying to convert. This
4397 * should only happen for the COW fork, where another thread
4398 * might have moved the extent to the data fork in the meantime.
4399 */
4400 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
c784c9d2
CH
4401 error = -EAGAIN;
4402 goto out_trans_cancel;
330a35fe 4403 }
badaa597
BF
4404
4405 /*
330a35fe
CH
4406 * If we find a real extent here we raced with another thread converting
4407 * the extent. Just return the real extent at this offset.
badaa597 4408 */
330a35fe 4409 if (!isnullstartblock(bma.got.br_startblock)) {
1dcdf505
DC
4410 xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
4411 xfs_iomap_inode_sequence(ip, flags));
7cd511c5
ZY
4412 if (seq)
4413 *seq = READ_ONCE(ifp->if_seq);
c784c9d2 4414 goto out_trans_cancel;
330a35fe
CH
4415 }
4416
4417 bma.tp = tp;
4418 bma.ip = ip;
4419 bma.wasdel = true;
330a35fe 4420 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
edc9bb69 4421
3c9133f8
CH
4422 /*
4423 * Always allocate convert from the start of the delalloc extent even if
4424 * that is outside the passed in range to create large contiguous
4425 * extents on disk.
4426 */
4427 bma.offset = bma.got.br_startoff;
4428 bma.length = bma.got.br_blockcount;
4429
edc9bb69
DW
4430 /*
4431 * When we're converting the delalloc reservations backing dirty pages
4432 * in the page cache, we must be careful about how we create the new
4433 * extents:
4434 *
4435 * New CoW fork extents are created unwritten, turned into real extents
4436 * when we're about to write the data to disk, and mapped into the data
4437 * fork after the write finishes. End of story.
4438 *
4439 * New data fork extents must be mapped in as unwritten and converted
4440 * to real extents after the write succeeds to avoid exposing stale
4441 * disk contents if we crash.
4442 */
4443 bma.flags = XFS_BMAPI_PREALLOC;
330a35fe 4444 if (whichfork == XFS_COW_FORK)
edc9bb69 4445 bma.flags |= XFS_BMAPI_COWFORK;
330a35fe
CH
4446
4447 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4448 bma.prev.br_startoff = NULLFILEOFF;
4449
4450 error = xfs_bmapi_allocate(&bma);
4451 if (error)
4452 goto out_finish;
4453
44e165df
CH
4454 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4455 XFS_STATS_INC(mp, xs_xstrat_quick);
4456
330a35fe 4457 ASSERT(!isnullstartblock(bma.got.br_startblock));
1dcdf505
DC
4458 xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
4459 xfs_iomap_inode_sequence(ip, flags));
7cd511c5
ZY
4460 if (seq)
4461 *seq = READ_ONCE(ifp->if_seq);
330a35fe 4462
5965a482 4463 if (whichfork == XFS_COW_FORK)
82d0c7cb
DW
4464 xfs_refcount_alloc_cow_extent(tp, XFS_IS_REALTIME_INODE(ip),
4465 bma.blkno, bma.length);
330a35fe
CH
4466
4467 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4468 whichfork);
c784c9d2
CH
4469 if (error)
4470 goto out_finish;
4471
4472 xfs_bmapi_finish(&bma, whichfork, 0);
4473 error = xfs_trans_commit(tp);
4474 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4475 return error;
4476
330a35fe
CH
4477out_finish:
4478 xfs_bmapi_finish(&bma, whichfork, error);
c784c9d2
CH
4479out_trans_cancel:
4480 xfs_trans_cancel(tp);
4481 xfs_iunlock(ip, XFS_ILOCK_EXCL);
badaa597
BF
4482 return error;
4483}
4484
44e4bdc6
ZY
4485/*
4486 * Pass in a dellalloc extent and convert it to real extents, return the real
4487 * extent that maps offset_fsb in iomap.
4488 */
4489int
4490xfs_bmapi_convert_delalloc(
4491 struct xfs_inode *ip,
4492 int whichfork,
4493 loff_t offset,
4494 struct iomap *iomap,
4495 unsigned int *seq)
4496{
4497 int error;
4498
4499 /*
4500 * Attempt to allocate whatever delalloc extent currently backs offset
4501 * and put the result into iomap. Allocate in a loop because it may
4502 * take several attempts to allocate real blocks for a contiguous
4503 * delalloc extent if free space is sufficiently fragmented.
4504 */
4505 do {
4506 error = xfs_bmapi_convert_one_delalloc(ip, whichfork, offset,
4507 iomap, seq);
4508 if (error)
4509 return error;
4510 } while (iomap->offset + iomap->length <= offset);
4511
4512 return 0;
4513}
4514
26d6a481 4515int
0b44aa85
CH
4516xfs_bmapi_remap(
4517 struct xfs_trans *tp,
4518 struct xfs_inode *ip,
4519 xfs_fileoff_t bno,
4520 xfs_filblks_t len,
4521 xfs_fsblock_t startblock,
6e22af31 4522 uint32_t flags)
0b44aa85
CH
4523{
4524 struct xfs_mount *mp = ip->i_mount;
26d6a481 4525 struct xfs_ifork *ifp;
0b44aa85 4526 struct xfs_btree_cur *cur = NULL;
0b44aa85 4527 struct xfs_bmbt_irec got;
9788e059 4528 struct xfs_iext_cursor icur;
26d6a481 4529 int whichfork = xfs_bmapi_whichfork(flags);
0b44aa85
CH
4530 int logflags = 0, error;
4531
722e81c1 4532 ifp = xfs_ifork_ptr(ip, whichfork);
0b44aa85 4533 ASSERT(len > 0);
d3e0c71f 4534 ASSERT(len <= (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN);
d3d1a2cb 4535 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
36cfb334
DW
4536 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4537 XFS_BMAPI_NORMAP)));
4538 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4539 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
0b44aa85 4540
d967a68d 4541 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
bc73da84 4542 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
6d5b5846 4543 xfs_bmap_mark_sick(ip, whichfork);
0b44aa85
CH
4544 return -EFSCORRUPTED;
4545 }
4546
93adb06a 4547 if (xfs_is_shutdown(mp))
0b44aa85
CH
4548 return -EIO;
4549
e00c57e7
CH
4550 error = xfs_iread_extents(tp, ip, whichfork);
4551 if (error)
4552 return error;
0b44aa85 4553
9788e059 4554 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
0b44aa85
CH
4555 /* make sure we only reflink into a hole. */
4556 ASSERT(got.br_startoff > bno);
4557 ASSERT(got.br_startoff - bno >= len);
4558 }
4559
aa00f286 4560 ip->i_nblocks += len;
a8c3578c 4561 ip->i_delayed_blks -= len; /* see xfs_bmap_defer_add */
05422db6 4562 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
0b44aa85 4563
d242dfc3 4564 if (ifp->if_format == XFS_DINODE_FMT_BTREE)
26d6a481 4565 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
0b44aa85
CH
4566
4567 got.br_startoff = bno;
4568 got.br_startblock = startblock;
4569 got.br_blockcount = len;
36cfb334
DW
4570 if (flags & XFS_BMAPI_PREALLOC)
4571 got.br_state = XFS_EXT_UNWRITTEN;
4572 else
4573 got.br_state = XFS_EXT_NORM;
0b44aa85 4574
26d6a481 4575 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
64e8b4a7 4576 &cur, &got, &logflags, flags);
0b44aa85
CH
4577 if (error)
4578 goto error0;
4579
939ebc1a 4580 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
0b44aa85
CH
4581
4582error0:
d967a68d 4583 if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS)
0b44aa85 4584 logflags &= ~XFS_ILOG_DEXT;
d967a68d 4585 else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE)
0b44aa85
CH
4586 logflags &= ~XFS_ILOG_DBROOT;
4587
4588 if (logflags)
4589 xfs_trans_log_inode(tp, ip, logflags);
660265b7
DW
4590 if (cur)
4591 xfs_btree_del_cursor(cur, error);
0b44aa85
CH
4592 return error;
4593}
4594
01d1b786
BF
4595/*
4596 * When a delalloc extent is split (e.g., due to a hole punch), the original
4597 * indlen reservation must be shared across the two new extents that are left
4598 * behind.
4599 *
4600 * Given the original reservation and the worst case indlen for the two new
4601 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
731ccdf9
BF
4602 * reservation fairly across the two new extents. If necessary, steal available
4603 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4604 * ores == 1). The number of stolen blocks is returned. The availability and
4605 * subsequent accounting of stolen blocks is the responsibility of the caller.
01d1b786 4606 */
0eec2750 4607static void
01d1b786
BF
4608xfs_bmap_split_indlen(
4609 xfs_filblks_t ores, /* original res. */
4610 xfs_filblks_t *indlen1, /* ext1 worst indlen */
0eec2750 4611 xfs_filblks_t *indlen2) /* ext2 worst indlen */
01d1b786
BF
4612{
4613 xfs_filblks_t len1 = *indlen1;
4614 xfs_filblks_t len2 = *indlen2;
4615 xfs_filblks_t nres = len1 + len2; /* new total res. */
775762e4 4616 xfs_filblks_t resfactor;
731ccdf9 4617
775762e4
BF
4618 /*
4619 * We can't meet the total required reservation for the two extents.
4620 * Calculate the percent of the overall shortage between both extents
4621 * and apply this percentage to each of the requested indlen values.
4622 * This distributes the shortage fairly and reduces the chances that one
4623 * of the two extents is left with nothing when extents are repeatedly
4624 * split.
4625 */
4626 resfactor = (ores * 100);
4627 do_div(resfactor, nres);
4628 len1 *= resfactor;
4629 do_div(len1, 100);
4630 len2 *= resfactor;
4631 do_div(len2, 100);
4632 ASSERT(len1 + len2 <= ores);
4633 ASSERT(len1 < *indlen1 && len2 < *indlen2);
01d1b786
BF
4634
4635 /*
775762e4
BF
4636 * Hand out the remainder to each extent. If one of the two reservations
4637 * is zero, we want to make sure that one gets a block first. The loop
4638 * below starts with len1, so hand len2 a block right off the bat if it
4639 * is zero.
01d1b786 4640 */
775762e4
BF
4641 ores -= (len1 + len2);
4642 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4643 if (ores && !len2 && *indlen2) {
4644 len2++;
4645 ores--;
4646 }
4647 while (ores) {
4648 if (len1 < *indlen1) {
4649 len1++;
4650 ores--;
01d1b786 4651 }
775762e4 4652 if (!ores)
01d1b786 4653 break;
775762e4
BF
4654 if (len2 < *indlen2) {
4655 len2++;
4656 ores--;
01d1b786
BF
4657 }
4658 }
4659
4660 *indlen1 = len1;
4661 *indlen2 = len2;
4662}
4663
d0348eb2 4664void
ece930fa
CH
4665xfs_bmap_del_extent_delay(
4666 struct xfs_inode *ip,
4667 int whichfork,
9788e059 4668 struct xfs_iext_cursor *icur,
ece930fa 4669 struct xfs_bmbt_irec *got,
5dc014c0
CH
4670 struct xfs_bmbt_irec *del,
4671 uint32_t bflags) /* bmapi flags */
ece930fa
CH
4672{
4673 struct xfs_mount *mp = ip->i_mount;
722e81c1 4674 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
ece930fa
CH
4675 struct xfs_bmbt_irec new;
4676 int64_t da_old, da_new, da_diff = 0;
4677 xfs_fileoff_t del_endoff, got_endoff;
0eec2750 4678 xfs_filblks_t got_indlen, new_indlen, stolen = 0;
7ce54306 4679 uint32_t state = xfs_bmap_fork_to_state(whichfork);
15f2811c 4680 uint64_t fdblocks;
ece930fa
CH
4681 bool isrt;
4682
4683 XFS_STATS_INC(mp, xs_del_exlist);
4684
36a93334 4685 isrt = xfs_ifork_is_realtime(ip, whichfork);
ece930fa
CH
4686 del_endoff = del->br_startoff + del->br_blockcount;
4687 got_endoff = got->br_startoff + got->br_blockcount;
4688 da_old = startblockval(got->br_startblock);
4689 da_new = 0;
4690
ece930fa
CH
4691 ASSERT(del->br_blockcount > 0);
4692 ASSERT(got->br_startoff <= del->br_startoff);
4693 ASSERT(got_endoff >= del_endoff);
4694
ece930fa
CH
4695 /*
4696 * Update the inode delalloc counter now and wait to update the
4697 * sb counters as we might have to borrow some blocks for the
4698 * indirect block accounting.
4699 */
d0348eb2 4700 xfs_quota_unreserve_blkres(ip, del->br_blockcount);
ece930fa
CH
4701 ip->i_delayed_blks -= del->br_blockcount;
4702
ece930fa 4703 if (got->br_startoff == del->br_startoff)
d0a03e5a 4704 state |= BMAP_LEFT_FILLING;
ece930fa 4705 if (got_endoff == del_endoff)
d0a03e5a 4706 state |= BMAP_RIGHT_FILLING;
ece930fa 4707
d0a03e5a
CH
4708 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4709 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
ece930fa
CH
4710 /*
4711 * Matches the whole extent. Delete the entry.
4712 */
cf455a62 4713 xfs_iext_remove(ip, icur, state);
9788e059 4714 xfs_iext_prev(ifp, icur);
ece930fa 4715 break;
d0a03e5a 4716 case BMAP_LEFT_FILLING:
ece930fa
CH
4717 /*
4718 * Deleting the first part of the extent.
4719 */
ece930fa
CH
4720 got->br_startoff = del_endoff;
4721 got->br_blockcount -= del->br_blockcount;
4722 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4723 got->br_blockcount), da_old);
4724 got->br_startblock = nullstartblock((int)da_new);
9788e059 4725 xfs_iext_update_extent(ip, state, icur, got);
ece930fa 4726 break;
d0a03e5a 4727 case BMAP_RIGHT_FILLING:
ece930fa
CH
4728 /*
4729 * Deleting the last part of the extent.
4730 */
ece930fa
CH
4731 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4732 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4733 got->br_blockcount), da_old);
4734 got->br_startblock = nullstartblock((int)da_new);
9788e059 4735 xfs_iext_update_extent(ip, state, icur, got);
ece930fa
CH
4736 break;
4737 case 0:
4738 /*
4739 * Deleting the middle of the extent.
4740 *
4741 * Distribute the original indlen reservation across the two new
4742 * extents. Steal blocks from the deleted extent if necessary.
4743 * Stealing blocks simply fudges the fdblocks accounting below.
4744 * Warn if either of the new indlen reservations is zero as this
4745 * can lead to delalloc problems.
4746 */
ece930fa
CH
4747 got->br_blockcount = del->br_startoff - got->br_startoff;
4748 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4749
4750 new.br_blockcount = got_endoff - del_endoff;
4751 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4752
4753 WARN_ON_ONCE(!got_indlen || !new_indlen);
0eec2750
CH
4754 /*
4755 * Steal as many blocks as we can to try and satisfy the worst
4756 * case indlen for both new extents.
9336c2e0
CH
4757 *
4758 * However, we can't just steal reservations from the data
4759 * blocks if this is an RT inodes as the data and metadata
4760 * blocks come from different pools. We'll have to live with
4761 * under-filled indirect reservation in this case.
0eec2750
CH
4762 */
4763 da_new = got_indlen + new_indlen;
9336c2e0 4764 if (da_new > da_old && !isrt) {
0eec2750
CH
4765 stolen = XFS_FILBLKS_MIN(da_new - da_old,
4766 del->br_blockcount);
4767 da_old += stolen;
4768 }
4769 if (da_new > da_old)
4770 xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen);
4771 da_new = got_indlen + new_indlen;
ece930fa
CH
4772
4773 got->br_startblock = nullstartblock((int)got_indlen);
ece930fa
CH
4774
4775 new.br_startoff = del_endoff;
4776 new.br_state = got->br_state;
4777 new.br_startblock = nullstartblock((int)new_indlen);
4778
9788e059
CH
4779 xfs_iext_update_extent(ip, state, icur, got);
4780 xfs_iext_next(ifp, icur);
26a75f67 4781 xfs_iext_insert(ip, icur, &new, state);
ece930fa 4782
ece930fa
CH
4783 del->br_blockcount -= stolen;
4784 break;
4785 }
4786
4787 ASSERT(da_old >= da_new);
4788 da_diff = da_old - da_new;
15f2811c
CH
4789 fdblocks = da_diff;
4790
d22f260a 4791 if (bflags & XFS_BMAPI_REMAP) {
5dc014c0 4792 ;
d22f260a
CH
4793 } else if (isrt) {
4794 xfs_rtbxlen_t rtxlen;
4795
4796 rtxlen = xfs_blen_to_rtbxlen(mp, del->br_blockcount);
4797 if (xfs_is_zoned_inode(ip))
4798 xfs_zoned_add_available(mp, rtxlen);
4799 xfs_add_frextents(mp, rtxlen);
4800 } else {
15f2811c 4801 fdblocks += del->br_blockcount;
d22f260a 4802 }
15f2811c
CH
4803
4804 xfs_add_fdblocks(mp, fdblocks);
ac315eaf 4805 xfs_mod_delalloc(ip, -(int64_t)del->br_blockcount, -da_diff);
ece930fa
CH
4806}
4807
4808void
4809xfs_bmap_del_extent_cow(
4810 struct xfs_inode *ip,
9788e059 4811 struct xfs_iext_cursor *icur,
ece930fa
CH
4812 struct xfs_bmbt_irec *got,
4813 struct xfs_bmbt_irec *del)
4814{
4815 struct xfs_mount *mp = ip->i_mount;
722e81c1 4816 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_COW_FORK);
ece930fa
CH
4817 struct xfs_bmbt_irec new;
4818 xfs_fileoff_t del_endoff, got_endoff;
7ce54306 4819 uint32_t state = BMAP_COWFORK;
ece930fa
CH
4820
4821 XFS_STATS_INC(mp, xs_del_exlist);
4822
4823 del_endoff = del->br_startoff + del->br_blockcount;
4824 got_endoff = got->br_startoff + got->br_blockcount;
4825
ece930fa
CH
4826 ASSERT(del->br_blockcount > 0);
4827 ASSERT(got->br_startoff <= del->br_startoff);
4828 ASSERT(got_endoff >= del_endoff);
4829 ASSERT(!isnullstartblock(got->br_startblock));
4830
4831 if (got->br_startoff == del->br_startoff)
d0a03e5a 4832 state |= BMAP_LEFT_FILLING;
ece930fa 4833 if (got_endoff == del_endoff)
d0a03e5a 4834 state |= BMAP_RIGHT_FILLING;
ece930fa 4835
d0a03e5a
CH
4836 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4837 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
ece930fa
CH
4838 /*
4839 * Matches the whole extent. Delete the entry.
4840 */
cf455a62 4841 xfs_iext_remove(ip, icur, state);
9788e059 4842 xfs_iext_prev(ifp, icur);
ece930fa 4843 break;
d0a03e5a 4844 case BMAP_LEFT_FILLING:
ece930fa
CH
4845 /*
4846 * Deleting the first part of the extent.
4847 */
ece930fa
CH
4848 got->br_startoff = del_endoff;
4849 got->br_blockcount -= del->br_blockcount;
4850 got->br_startblock = del->br_startblock + del->br_blockcount;
9788e059 4851 xfs_iext_update_extent(ip, state, icur, got);
ece930fa 4852 break;
d0a03e5a 4853 case BMAP_RIGHT_FILLING:
ece930fa
CH
4854 /*
4855 * Deleting the last part of the extent.
4856 */
ece930fa 4857 got->br_blockcount -= del->br_blockcount;
9788e059 4858 xfs_iext_update_extent(ip, state, icur, got);
ece930fa
CH
4859 break;
4860 case 0:
4861 /*
4862 * Deleting the middle of the extent.
4863 */
ece930fa 4864 got->br_blockcount = del->br_startoff - got->br_startoff;
ece930fa
CH
4865
4866 new.br_startoff = del_endoff;
4867 new.br_blockcount = got_endoff - del_endoff;
4868 new.br_state = got->br_state;
4869 new.br_startblock = del->br_startblock + del->br_blockcount;
4870
9788e059
CH
4871 xfs_iext_update_extent(ip, state, icur, got);
4872 xfs_iext_next(ifp, icur);
26a75f67 4873 xfs_iext_insert(ip, icur, &new, state);
ece930fa
CH
4874 break;
4875 }
d07cc724 4876 ip->i_delayed_blks -= del->br_blockcount;
ece930fa
CH
4877}
4878
7ac3ad4c
CH
4879static int
4880xfs_bmap_free_rtblocks(
4881 struct xfs_trans *tp,
4882 struct xfs_bmbt_irec *del)
4883{
55ed4049 4884 struct xfs_rtgroup *rtg;
7ac3ad4c
CH
4885 int error;
4886
55ed4049
CH
4887 rtg = xfs_rtgroup_grab(tp->t_mountp, 0);
4888 if (!rtg)
4889 return -EIO;
4890
7ac3ad4c
CH
4891 /*
4892 * Ensure the bitmap and summary inodes are locked and joined to the
4893 * transaction before modifying them.
4894 */
4895 if (!(tp->t_flags & XFS_TRANS_RTBITMAP_LOCKED)) {
4896 tp->t_flags |= XFS_TRANS_RTBITMAP_LOCKED;
55ed4049
CH
4897 xfs_rtgroup_lock(rtg, XFS_RTGLOCK_BITMAP);
4898 xfs_rtgroup_trans_join(tp, rtg, XFS_RTGLOCK_BITMAP);
7ac3ad4c
CH
4899 }
4900
55ed4049
CH
4901 error = xfs_rtfree_blocks(tp, rtg, del->br_startblock,
4902 del->br_blockcount);
4903 xfs_rtgroup_rele(rtg);
7ac3ad4c
CH
4904 return error;
4905}
4906
2bd0ea18 4907/*
49f693fa 4908 * Called by xfs_bmapi to update file extent records and the btree
ad68fd19 4909 * after removing space.
2bd0ea18 4910 */
49f693fa 4911STATIC int /* error */
ad68fd19 4912xfs_bmap_del_extent_real(
49f693fa
DC
4913 xfs_inode_t *ip, /* incore inode pointer */
4914 xfs_trans_t *tp, /* current transaction pointer */
9788e059 4915 struct xfs_iext_cursor *icur,
ec924d04 4916 struct xfs_btree_cur *cur, /* if null, not a btree */
49f693fa
DC
4917 xfs_bmbt_irec_t *del, /* data to remove from extents */
4918 int *logflagsp, /* inode logging flags */
36b16da8 4919 int whichfork, /* data or attr fork */
6e22af31 4920 uint32_t bflags) /* bmapi flags */
2bd0ea18 4921{
49f693fa
DC
4922 xfs_fsblock_t del_endblock=0; /* first block past del */
4923 xfs_fileoff_t del_endoff; /* first offset past del */
4bcaa909 4924 int error = 0; /* error return value */
70bf7533 4925 struct xfs_bmbt_irec got; /* current extent entry */
49f693fa
DC
4926 xfs_fileoff_t got_endoff; /* first offset past got */
4927 int i; /* temp state */
e07055b8 4928 struct xfs_ifork *ifp; /* inode fork pointer */
49f693fa
DC
4929 xfs_mount_t *mp; /* mount structure */
4930 xfs_filblks_t nblks; /* quota/sb block count */
4931 xfs_bmbt_irec_t new; /* new record to be inserted */
4932 /* REFERENCED */
4933 uint qfield; /* quota field to update */
7ce54306 4934 uint32_t state = xfs_bmap_fork_to_state(whichfork);
70bf7533 4935 struct xfs_bmbt_irec old;
a2ceac1f 4936
aedfcddb
JZ
4937 *logflagsp = 0;
4938
79896434
BD
4939 mp = ip->i_mount;
4940 XFS_STATS_INC(mp, xs_del_exlist);
a2ceac1f 4941
722e81c1 4942 ifp = xfs_ifork_ptr(ip, whichfork);
49f693fa 4943 ASSERT(del->br_blockcount > 0);
9788e059 4944 xfs_iext_get_extent(ifp, icur, &got);
49f693fa
DC
4945 ASSERT(got.br_startoff <= del->br_startoff);
4946 del_endoff = del->br_startoff + del->br_blockcount;
4947 got_endoff = got.br_startoff + got.br_blockcount;
4948 ASSERT(got_endoff >= del_endoff);
ad68fd19 4949 ASSERT(!isnullstartblock(got.br_startblock));
49f693fa 4950 qfield = 0;
ad68fd19 4951
bd92a38b
CH
4952 /*
4953 * If it's the case where the directory code is running with no block
4954 * reservation, and the deleted block is in the middle of its extent,
4955 * and the resulting insert of an extent would cause transformation to
4956 * btree format, then reject it. The calling code will then swap blocks
4957 * around instead. We have to do this now, rather than waiting for the
4958 * conversion to btree format, since the transaction will be dirty then.
4959 */
4960 if (tp->t_blk_res == 0 &&
d967a68d 4961 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
87c472b7 4962 ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) &&
bd92a38b
CH
4963 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
4964 return -ENOSPC;
4965
aedfcddb 4966 *logflagsp = XFS_ILOG_CORE;
4bcaa909 4967 if (xfs_ifork_is_realtime(ip, whichfork))
ad68fd19 4968 qfield = XFS_TRANS_DQ_RTBCOUNT;
4bcaa909 4969 else
ad68fd19 4970 qfield = XFS_TRANS_DQ_BCOUNT;
4dbd5762 4971 nblks = del->br_blockcount;
ad68fd19
CH
4972
4973 del_endblock = del->br_startblock + del->br_blockcount;
4974 if (cur) {
70a93110 4975 error = xfs_bmbt_lookup_eq(cur, &got, &i);
ad68fd19 4976 if (error)
aedfcddb 4977 return error;
4467a60a
DW
4978 if (XFS_IS_CORRUPT(mp, i != 1)) {
4979 xfs_btree_mark_sick(cur);
aedfcddb 4980 return -EFSCORRUPTED;
4467a60a 4981 }
49f693fa 4982 }
85aec44f 4983
b039ac79
CH
4984 if (got.br_startoff == del->br_startoff)
4985 state |= BMAP_LEFT_FILLING;
4986 if (got_endoff == del_endoff)
4987 state |= BMAP_RIGHT_FILLING;
4988
4989 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4990 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
49f693fa
DC
4991 /*
4992 * Matches the whole extent. Delete the entry.
4993 */
cf455a62 4994 xfs_iext_remove(ip, icur, state);
9788e059 4995 xfs_iext_prev(ifp, icur);
87c472b7
CH
4996 ifp->if_nextents--;
4997
aedfcddb 4998 *logflagsp |= XFS_ILOG_CORE;
49f693fa 4999 if (!cur) {
aedfcddb 5000 *logflagsp |= xfs_ilog_fext(whichfork);
49f693fa 5001 break;
2bd0ea18 5002 }
49f693fa 5003 if ((error = xfs_btree_delete(cur, &i)))
aedfcddb 5004 return error;
4467a60a
DW
5005 if (XFS_IS_CORRUPT(mp, i != 1)) {
5006 xfs_btree_mark_sick(cur);
aedfcddb 5007 return -EFSCORRUPTED;
4467a60a 5008 }
49f693fa 5009 break;
b039ac79 5010 case BMAP_LEFT_FILLING:
2bd0ea18 5011 /*
49f693fa 5012 * Deleting the first part of the extent.
2bd0ea18 5013 */
70bf7533
CH
5014 got.br_startoff = del_endoff;
5015 got.br_startblock = del_endblock;
5016 got.br_blockcount -= del->br_blockcount;
9788e059 5017 xfs_iext_update_extent(ip, state, icur, &got);
49f693fa 5018 if (!cur) {
aedfcddb 5019 *logflagsp |= xfs_ilog_fext(whichfork);
49f693fa
DC
5020 break;
5021 }
d0e5f1ff 5022 error = xfs_bmbt_update(cur, &got);
70bf7533 5023 if (error)
aedfcddb 5024 return error;
49f693fa 5025 break;
b039ac79 5026 case BMAP_RIGHT_FILLING:
2bd0ea18 5027 /*
49f693fa 5028 * Deleting the last part of the extent.
2bd0ea18 5029 */
70bf7533 5030 got.br_blockcount -= del->br_blockcount;
9788e059 5031 xfs_iext_update_extent(ip, state, icur, &got);
49f693fa 5032 if (!cur) {
aedfcddb 5033 *logflagsp |= xfs_ilog_fext(whichfork);
49f693fa
DC
5034 break;
5035 }
d0e5f1ff 5036 error = xfs_bmbt_update(cur, &got);
70bf7533 5037 if (error)
aedfcddb 5038 return error;
49f693fa 5039 break;
49f693fa
DC
5040 case 0:
5041 /*
5042 * Deleting the middle of the extent.
5043 */
7428ec5b 5044
70bf7533 5045 old = got;
df926c07 5046
70bf7533 5047 got.br_blockcount = del->br_startoff - got.br_startoff;
9788e059 5048 xfs_iext_update_extent(ip, state, icur, &got);
70bf7533 5049
49f693fa 5050 new.br_startoff = del_endoff;
70bf7533 5051 new.br_blockcount = got_endoff - del_endoff;
49f693fa 5052 new.br_state = got.br_state;
ad68fd19 5053 new.br_startblock = del_endblock;
70bf7533 5054
aedfcddb 5055 *logflagsp |= XFS_ILOG_CORE;
ad68fd19 5056 if (cur) {
d0e5f1ff 5057 error = xfs_bmbt_update(cur, &got);
ad68fd19 5058 if (error)
aedfcddb 5059 return error;
ad68fd19
CH
5060 error = xfs_btree_increment(cur, 0, &i);
5061 if (error)
aedfcddb 5062 return error;
ad68fd19
CH
5063 cur->bc_rec.b = new;
5064 error = xfs_btree_insert(cur, &i);
5065 if (error && error != -ENOSPC)
aedfcddb 5066 return error;
ad68fd19
CH
5067 /*
5068 * If get no-space back from btree insert, it tried a
5069 * split, and we have a zero block reservation. Fix up
5070 * our state and return the error.
5071 */
5072 if (error == -ENOSPC) {
49f693fa 5073 /*
ad68fd19
CH
5074 * Reset the cursor, don't trust it after any
5075 * insert operation.
49f693fa 5076 */
70a93110 5077 error = xfs_bmbt_lookup_eq(cur, &got, &i);
ad68fd19 5078 if (error)
aedfcddb 5079 return error;
4467a60a
DW
5080 if (XFS_IS_CORRUPT(mp, i != 1)) {
5081 xfs_btree_mark_sick(cur);
aedfcddb 5082 return -EFSCORRUPTED;
4467a60a 5083 }
ad68fd19
CH
5084 /*
5085 * Update the btree record back
5086 * to the original value.
5087 */
d0e5f1ff 5088 error = xfs_bmbt_update(cur, &old);
ad68fd19 5089 if (error)
aedfcddb 5090 return error;
ad68fd19
CH
5091 /*
5092 * Reset the extent record back
5093 * to the original value.
5094 */
9788e059 5095 xfs_iext_update_extent(ip, state, icur, &old);
aedfcddb
JZ
5096 *logflagsp = 0;
5097 return -ENOSPC;
fbb4fa7f 5098 }
4467a60a
DW
5099 if (XFS_IS_CORRUPT(mp, i != 1)) {
5100 xfs_btree_mark_sick(cur);
aedfcddb 5101 return -EFSCORRUPTED;
4467a60a 5102 }
ad68fd19 5103 } else
aedfcddb 5104 *logflagsp |= xfs_ilog_fext(whichfork);
87c472b7
CH
5105
5106 ifp->if_nextents++;
9788e059 5107 xfs_iext_next(ifp, icur);
26a75f67 5108 xfs_iext_insert(ip, icur, &new, state);
49f693fa 5109 break;
2bd0ea18 5110 }
d7f80320
DW
5111
5112 /* remove reverse mapping */
46d29bb9 5113 xfs_rmap_unmap_extent(tp, ip, whichfork, del);
d7f80320 5114
2bd0ea18 5115 /*
49f693fa 5116 * If we need to, add to list of extents to delete.
2bd0ea18 5117 */
4bcaa909 5118 if (!(bflags & XFS_BMAPI_REMAP)) {
f95f8a06
DW
5119 bool isrt = xfs_ifork_is_realtime(ip, whichfork);
5120
cfe32f0d 5121 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
82d0c7cb 5122 xfs_refcount_decrease_extent(tp, isrt, del);
f95f8a06 5123 } else if (isrt && !xfs_has_rtgroups(mp)) {
7ac3ad4c 5124 error = xfs_bmap_free_rtblocks(tp, del);
3a13f959 5125 } else {
ad2fb6bc
DW
5126 unsigned int efi_flags = 0;
5127
5128 if ((bflags & XFS_BMAPI_NODISCARD) ||
5129 del->br_state == XFS_EXT_UNWRITTEN)
5130 efi_flags |= XFS_FREE_EXTENT_SKIP_DISCARD;
5131
f95f8a06
DW
5132 /*
5133 * Historically, we did not use EFIs to free realtime
5134 * extents. However, when reverse mapping is enabled,
5135 * we must maintain the same order of operations as the
5136 * data device, which is: Remove the file mapping,
5137 * remove the reverse mapping, and then free the
5138 * blocks. Reflink for realtime volumes requires the
5139 * same sort of ordering. Both features rely on
5140 * rtgroups, so let's gate rt EFI usage on rtgroups.
5141 */
5142 if (isrt)
5143 efi_flags |= XFS_FREE_EXTENT_REALTIME;
5144
8876f46b 5145 error = xfs_free_extent_later(tp, del->br_startblock,
cc5af22a 5146 del->br_blockcount, NULL,
ad2fb6bc 5147 XFS_AG_RESV_NONE, efi_flags);
3a13f959 5148 }
4bcaa909
CH
5149 if (error)
5150 return error;
cfe32f0d
DW
5151 }
5152
2bd0ea18 5153 /*
49f693fa 5154 * Adjust inode # blocks in the file.
2bd0ea18 5155 */
49f693fa 5156 if (nblks)
aa00f286 5157 ip->i_nblocks -= nblks;
2bd0ea18 5158 /*
49f693fa 5159 * Adjust quota data.
2bd0ea18 5160 */
36b16da8 5161 if (qfield && !(bflags & XFS_BMAPI_REMAP))
49f693fa
DC
5162 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5163
aedfcddb 5164 return 0;
2bd0ea18
NS
5165}
5166
5167/*
49f693fa
DC
5168 * Unmap (remove) blocks from a file.
5169 * If nexts is nonzero then the number of extents to remove is limited to
5170 * that value. If not all extents in the block range can be removed then
5171 * *done is set.
2bd0ea18 5172 */
f76f73b3 5173static int
3d36acda 5174__xfs_bunmapi(
160ed12f 5175 struct xfs_trans *tp, /* transaction pointer */
49f693fa 5176 struct xfs_inode *ip, /* incore inode */
675b5a20 5177 xfs_fileoff_t start, /* first file offset deleted */
3d36acda 5178 xfs_filblks_t *rlen, /* i/o: amount remaining */
6e22af31 5179 uint32_t flags, /* misc flags */
d3c5f3dd 5180 xfs_extnum_t nexts) /* number of extents max */
2bd0ea18 5181{
160ed12f
BF
5182 struct xfs_btree_cur *cur; /* bmap btree cursor */
5183 struct xfs_bmbt_irec del; /* extent being deleted */
49f693fa
DC
5184 int error; /* error return value */
5185 xfs_extnum_t extno; /* extent number in list */
160ed12f 5186 struct xfs_bmbt_irec got; /* current extent record */
e07055b8 5187 struct xfs_ifork *ifp; /* inode fork pointer */
49f693fa 5188 int isrt; /* freeing in rt area */
49f693fa
DC
5189 int logflags; /* transaction logging flags */
5190 xfs_extlen_t mod; /* rt extent offset */
bc73da84 5191 struct xfs_mount *mp = ip->i_mount;
49f693fa
DC
5192 int tmp_logflags; /* partial logging flags */
5193 int wasdel; /* was a delayed alloc extent */
5194 int whichfork; /* data or attribute fork */
3d36acda 5195 xfs_filblks_t len = *rlen; /* length to unmap in file */
675b5a20 5196 xfs_fileoff_t end;
9788e059
CH
5197 struct xfs_iext_cursor icur;
5198 bool done = false;
2bd0ea18 5199
675b5a20 5200 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
a2ceac1f 5201
cb8a004a
DW
5202 whichfork = xfs_bmapi_whichfork(flags);
5203 ASSERT(whichfork != XFS_COW_FORK);
722e81c1 5204 ifp = xfs_ifork_ptr(ip, whichfork);
6d5b5846
DW
5205 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp))) {
5206 xfs_bmap_mark_sick(ip, whichfork);
12b53197 5207 return -EFSCORRUPTED;
6d5b5846 5208 }
93adb06a 5209 if (xfs_is_shutdown(mp))
12b53197 5210 return -EIO;
56b2de80 5211
d3d1a2cb 5212 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
49f693fa
DC
5213 ASSERT(len > 0);
5214 ASSERT(nexts >= 0);
56b2de80 5215
e00c57e7
CH
5216 error = xfs_iread_extents(tp, ip, whichfork);
5217 if (error)
49f693fa 5218 return error;
e00c57e7 5219
d09d4e5f 5220 if (xfs_iext_count(ifp) == 0) {
3d36acda 5221 *rlen = 0;
49f693fa 5222 return 0;
56b2de80 5223 }
79896434 5224 XFS_STATS_INC(mp, xs_blk_unmap);
36a93334 5225 isrt = xfs_ifork_is_realtime(ip, whichfork);
d6fbe8fe 5226 end = start + len;
a2ceac1f 5227
9788e059 5228 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
d6fbe8fe
CH
5229 *rlen = 0;
5230 return 0;
49f693fa 5231 }
d6fbe8fe 5232 end--;
246eb90a 5233
49f693fa 5234 logflags = 0;
84094546 5235 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
d967a68d 5236 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
49f693fa 5237 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
49f693fa
DC
5238 } else
5239 cur = NULL;
a2ceac1f 5240
49f693fa 5241 extno = 0;
9788e059 5242 while (end != (xfs_fileoff_t)-1 && end >= start &&
eb9a1cac 5243 (nexts == 0 || extno < nexts)) {
a2ceac1f 5244 /*
675b5a20 5245 * Is the found extent after a hole in which end lives?
49f693fa 5246 * Just back up to the previous extent, if so.
a2ceac1f 5247 */
9788e059
CH
5248 if (got.br_startoff > end &&
5249 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5250 done = true;
5251 break;
a2ceac1f 5252 }
49f693fa
DC
5253 /*
5254 * Is the last block of this extent before the range
5255 * we're supposed to delete? If so, we're done.
5256 */
675b5a20 5257 end = XFS_FILEOFF_MIN(end,
49f693fa 5258 got.br_startoff + got.br_blockcount - 1);
675b5a20 5259 if (end < start)
49f693fa
DC
5260 break;
5261 /*
5262 * Then deal with the (possibly delayed) allocated space
5263 * we found.
5264 */
49f693fa
DC
5265 del = got;
5266 wasdel = isnullstartblock(del.br_startblock);
15a8bccc 5267
49f693fa
DC
5268 if (got.br_startoff < start) {
5269 del.br_startoff = start;
5270 del.br_blockcount -= start - got.br_startoff;
5271 if (!wasdel)
5272 del.br_startblock += start - got.br_startoff;
5273 }
675b5a20
CH
5274 if (del.br_startoff + del.br_blockcount > end + 1)
5275 del.br_blockcount = end + 1 - del.br_startoff;
594956fa 5276
c2be7e83 5277 if (!isrt || (flags & XFS_BMAPI_REMAP))
5a595099
DC
5278 goto delete;
5279
1dac259c
DW
5280 mod = xfs_rtb_to_rtxoff(mp,
5281 del.br_startblock + del.br_blockcount);
5a595099 5282 if (mod) {
49f693fa
DC
5283 /*
5284 * Realtime extent not lined up at the end.
5285 * The extent could have been split into written
5286 * and unwritten pieces, or we could just be
5287 * unmapping part of it. But we can't really
5288 * get rid of part of a realtime extent.
5289 */
b7fdeb4d 5290 if (del.br_state == XFS_EXT_UNWRITTEN) {
49f693fa
DC
5291 /*
5292 * This piece is unwritten, or we're not
5293 * using unwritten extents. Skip over it.
5294 */
c2be7e83 5295 ASSERT((flags & XFS_BMAPI_REMAP) || end >= mod);
675b5a20 5296 end -= mod > del.br_blockcount ?
49f693fa 5297 del.br_blockcount : mod;
9788e059
CH
5298 if (end < got.br_startoff &&
5299 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5300 done = true;
5301 break;
49f693fa
DC
5302 }
5303 continue;
5304 }
5305 /*
5306 * It's written, turn it unwritten.
5307 * This is better than zeroing it.
5308 */
5309 ASSERT(del.br_state == XFS_EXT_NORM);
0268fdc3 5310 ASSERT(tp->t_blk_res > 0);
49f693fa
DC
5311 /*
5312 * If this spans a realtime extent boundary,
5313 * chop it back to the start of the one we end at.
5314 */
5315 if (del.br_blockcount > mod) {
5316 del.br_startoff += del.br_blockcount - mod;
5317 del.br_startblock += del.br_blockcount - mod;
5318 del.br_blockcount = mod;
5319 }
5320 del.br_state = XFS_EXT_UNWRITTEN;
5321 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
9788e059 5322 whichfork, &icur, &cur, &del,
64e8b4a7 5323 &logflags);
49f693fa
DC
5324 if (error)
5325 goto error0;
5326 goto nodelete;
a2ceac1f 5327 }
1dac259c
DW
5328
5329 mod = xfs_rtb_to_rtxoff(mp, del.br_startblock);
5a595099 5330 if (mod) {
f8446e51
OS
5331 xfs_extlen_t off = mp->m_sb.sb_rextsize - mod;
5332
49f693fa
DC
5333 /*
5334 * Realtime extent is lined up at the end but not
5335 * at the front. We'll get rid of full extents if
5336 * we can.
5337 */
f8446e51
OS
5338 if (del.br_blockcount > off) {
5339 del.br_blockcount -= off;
5340 del.br_startoff += off;
5341 del.br_startblock += off;
b7fdeb4d
CH
5342 } else if (del.br_startoff == start &&
5343 (del.br_state == XFS_EXT_UNWRITTEN ||
5344 tp->t_blk_res == 0)) {
49f693fa
DC
5345 /*
5346 * Can't make it unwritten. There isn't
5347 * a full extent here so just skip it.
5348 */
675b5a20
CH
5349 ASSERT(end >= del.br_blockcount);
5350 end -= del.br_blockcount;
9788e059
CH
5351 if (got.br_startoff > end &&
5352 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5353 done = true;
5354 break;
5355 }
49f693fa
DC
5356 continue;
5357 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
246eb90a 5358 struct xfs_bmbt_irec prev;
f8446e51 5359 xfs_fileoff_t unwrite_start;
246eb90a 5360
49f693fa
DC
5361 /*
5362 * This one is already unwritten.
5363 * It must have a written left neighbor.
5364 * Unwrite the killed part of that one and
5365 * try again.
5366 */
9788e059
CH
5367 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5368 ASSERT(0);
49f693fa
DC
5369 ASSERT(prev.br_state == XFS_EXT_NORM);
5370 ASSERT(!isnullstartblock(prev.br_startblock));
5371 ASSERT(del.br_startblock ==
5372 prev.br_startblock + prev.br_blockcount);
f8446e51
OS
5373 unwrite_start = max3(start,
5374 del.br_startoff - mod,
5375 prev.br_startoff);
5376 mod = unwrite_start - prev.br_startoff;
5377 prev.br_startoff = unwrite_start;
5378 prev.br_startblock += mod;
5379 prev.br_blockcount -= mod;
49f693fa 5380 prev.br_state = XFS_EXT_UNWRITTEN;
49f693fa 5381 error = xfs_bmap_add_extent_unwritten_real(tp,
9788e059 5382 ip, whichfork, &icur, &cur,
64e8b4a7 5383 &prev, &logflags);
49f693fa
DC
5384 if (error)
5385 goto error0;
5386 goto nodelete;
5387 } else {
5388 ASSERT(del.br_state == XFS_EXT_NORM);
5389 del.br_state = XFS_EXT_UNWRITTEN;
5390 error = xfs_bmap_add_extent_unwritten_real(tp,
9788e059 5391 ip, whichfork, &icur, &cur,
64e8b4a7 5392 &del, &logflags);
49f693fa
DC
5393 if (error)
5394 goto error0;
5395 goto nodelete;
5396 }
5397 }
a2ceac1f 5398
5a595099 5399delete:
8359e0b9 5400 if (wasdel) {
5dc014c0
CH
5401 xfs_bmap_del_extent_delay(ip, whichfork, &icur, &got,
5402 &del, flags);
ad68fd19 5403 } else {
52f6ed9d
BF
5404 error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5405 &del, &tmp_logflags, whichfork,
ad68fd19
CH
5406 flags);
5407 logflags |= tmp_logflags;
d0348eb2
CH
5408 if (error)
5409 goto error0;
e9fa15aa 5410 }
8359e0b9 5411
675b5a20 5412 end = del.br_startoff - 1;
49f693fa 5413nodelete:
a2ceac1f 5414 /*
49f693fa 5415 * If not done go on to the next (previous) record.
a2ceac1f 5416 */
675b5a20 5417 if (end != (xfs_fileoff_t)-1 && end >= start) {
9788e059
CH
5418 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5419 (got.br_startoff > end &&
5420 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5421 done = true;
5422 break;
49f693fa
DC
5423 }
5424 extno++;
a2ceac1f 5425 }
a2ceac1f 5426 }
9788e059 5427 if (done || end == (xfs_fileoff_t)-1 || end < start)
3d36acda
DW
5428 *rlen = 0;
5429 else
675b5a20 5430 *rlen = end - start + 1;
56b2de80 5431
49f693fa
DC
5432 /*
5433 * Convert to a btree if necessary.
5434 */
5435 if (xfs_bmap_needs_btree(ip, whichfork)) {
5436 ASSERT(cur == NULL);
f7253505
BF
5437 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5438 &tmp_logflags, whichfork);
49f693fa 5439 logflags |= tmp_logflags;
939ebc1a
CH
5440 } else {
5441 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
49f693fa 5442 whichfork);
56b2de80 5443 }
939ebc1a 5444
49f693fa
DC
5445error0:
5446 /*
5447 * Log everything. Do this after conversion, there's no point in
5448 * logging the extent records if we've converted to btree format.
5449 */
5450 if ((logflags & xfs_ilog_fext(whichfork)) &&
d967a68d 5451 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
49f693fa
DC
5452 logflags &= ~xfs_ilog_fext(whichfork);
5453 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
d967a68d 5454 ifp->if_format != XFS_DINODE_FMT_BTREE)
49f693fa
DC
5455 logflags &= ~xfs_ilog_fbroot(whichfork);
5456 /*
5457 * Log inode even in the error case, if the transaction
5458 * is dirty we'll need to shut down the filesystem.
5459 */
5460 if (logflags)
5461 xfs_trans_log_inode(tp, ip, logflags);
5462 if (cur) {
f44a348b 5463 if (!error)
6aba4616 5464 cur->bc_bmap.allocated = 0;
660265b7 5465 xfs_btree_del_cursor(cur, error);
56b2de80 5466 }
49f693fa 5467 return error;
a2ceac1f 5468}
ff105f75 5469
3d36acda
DW
5470/* Unmap a range of a file. */
5471int
5472xfs_bunmapi(
5473 xfs_trans_t *tp,
5474 struct xfs_inode *ip,
5475 xfs_fileoff_t bno,
5476 xfs_filblks_t len,
6e22af31 5477 uint32_t flags,
3d36acda 5478 xfs_extnum_t nexts,
3d36acda
DW
5479 int *done)
5480{
5481 int error;
5482
d3c5f3dd 5483 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
3d36acda
DW
5484 *done = (len == 0);
5485 return error;
5486}
5487
5a35bf2c
DC
5488/*
5489 * Determine whether an extent shift can be accomplished by a merge with the
5490 * extent that precedes the target hole of the shift.
5491 */
5492STATIC bool
5493xfs_bmse_can_merge(
15fb060e
CH
5494 struct xfs_inode *ip,
5495 int whichfork,
5a35bf2c
DC
5496 struct xfs_bmbt_irec *left, /* preceding extent */
5497 struct xfs_bmbt_irec *got, /* current extent to shift */
5498 xfs_fileoff_t shift) /* shift fsb */
5499{
5500 xfs_fileoff_t startoff;
5501
5502 startoff = got->br_startoff - shift;
5503
5504 /*
5505 * The extent, once shifted, must be adjacent in-file and on-disk with
5506 * the preceding extent.
5507 */
5508 if ((left->br_startoff + left->br_blockcount != startoff) ||
5509 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5510 (left->br_state != got->br_state) ||
15fb060e
CH
5511 (left->br_blockcount + got->br_blockcount > XFS_MAX_BMBT_EXTLEN) ||
5512 !xfs_bmap_same_rtgroup(ip, whichfork, left, got))
5a35bf2c
DC
5513 return false;
5514
5515 return true;
5516}
5517
5518/*
5519 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5520 * hole in the file. If an extent shift would result in the extent being fully
5521 * adjacent to the extent that currently precedes the hole, we can merge with
5522 * the preceding extent rather than do the shift.
5523 *
5524 * This function assumes the caller has verified a shift-by-merge is possible
5525 * with the provided extents via xfs_bmse_can_merge().
5526 */
5527STATIC int
5528xfs_bmse_merge(
21375e5d 5529 struct xfs_trans *tp,
5a35bf2c
DC
5530 struct xfs_inode *ip,
5531 int whichfork,
5532 xfs_fileoff_t shift, /* shift fsb */
9788e059 5533 struct xfs_iext_cursor *icur,
2f082827
CH
5534 struct xfs_bmbt_irec *got, /* extent to shift */
5535 struct xfs_bmbt_irec *left, /* preceding extent */
5a35bf2c 5536 struct xfs_btree_cur *cur,
21375e5d 5537 int *logflags) /* output */
5a35bf2c 5538{
722e81c1 5539 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
2f082827 5540 struct xfs_bmbt_irec new;
5a35bf2c
DC
5541 xfs_filblks_t blockcount;
5542 int error, i;
19ebedcf 5543 struct xfs_mount *mp = ip->i_mount;
5a35bf2c 5544
2f082827 5545 blockcount = left->br_blockcount + got->br_blockcount;
5a35bf2c 5546
d3d1a2cb 5547 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
15fb060e 5548 ASSERT(xfs_bmse_can_merge(ip, whichfork, left, got, shift));
5a35bf2c 5549
2f082827
CH
5550 new = *left;
5551 new.br_blockcount = blockcount;
5a35bf2c
DC
5552
5553 /*
5554 * Update the on-disk extent count, the btree if necessary and log the
5555 * inode.
5556 */
87c472b7 5557 ifp->if_nextents--;
5a35bf2c
DC
5558 *logflags |= XFS_ILOG_CORE;
5559 if (!cur) {
5560 *logflags |= XFS_ILOG_DEXT;
2f082827 5561 goto done;
5a35bf2c
DC
5562 }
5563
5564 /* lookup and remove the extent to merge */
70a93110 5565 error = xfs_bmbt_lookup_eq(cur, got, &i);
5a35bf2c
DC
5566 if (error)
5567 return error;
4467a60a
DW
5568 if (XFS_IS_CORRUPT(mp, i != 1)) {
5569 xfs_btree_mark_sick(cur);
fbb4fa7f 5570 return -EFSCORRUPTED;
4467a60a 5571 }
5a35bf2c
DC
5572
5573 error = xfs_btree_delete(cur, &i);
5574 if (error)
5575 return error;
4467a60a
DW
5576 if (XFS_IS_CORRUPT(mp, i != 1)) {
5577 xfs_btree_mark_sick(cur);
fbb4fa7f 5578 return -EFSCORRUPTED;
4467a60a 5579 }
5a35bf2c
DC
5580
5581 /* lookup and update size of the previous extent */
70a93110 5582 error = xfs_bmbt_lookup_eq(cur, left, &i);
5a35bf2c
DC
5583 if (error)
5584 return error;
4467a60a
DW
5585 if (XFS_IS_CORRUPT(mp, i != 1)) {
5586 xfs_btree_mark_sick(cur);
fbb4fa7f 5587 return -EFSCORRUPTED;
4467a60a 5588 }
5a35bf2c 5589
d0e5f1ff 5590 error = xfs_bmbt_update(cur, &new);
2f082827
CH
5591 if (error)
5592 return error;
5593
e102336b
BF
5594 /* change to extent format if required after extent removal */
5595 error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork);
5596 if (error)
5597 return error;
5598
2f082827 5599done:
cf455a62 5600 xfs_iext_remove(ip, icur, 0);
87c472b7 5601 xfs_iext_prev(ifp, icur);
9788e059
CH
5602 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5603 &new);
5a35bf2c 5604
479284b7 5605 /* update reverse mapping. rmap functions merge the rmaps for us */
46d29bb9 5606 xfs_rmap_unmap_extent(tp, ip, whichfork, got);
479284b7
DW
5607 memcpy(&new, got, sizeof(new));
5608 new.br_startoff = left->br_startoff + left->br_blockcount;
46d29bb9
DW
5609 xfs_rmap_map_extent(tp, ip, whichfork, &new);
5610 return 0;
5a35bf2c
DC
5611}
5612
364937e2
CH
5613static int
5614xfs_bmap_shift_update_extent(
21375e5d 5615 struct xfs_trans *tp,
364937e2
CH
5616 struct xfs_inode *ip,
5617 int whichfork,
9788e059 5618 struct xfs_iext_cursor *icur,
364937e2
CH
5619 struct xfs_bmbt_irec *got,
5620 struct xfs_btree_cur *cur,
5621 int *logflags,
364937e2 5622 xfs_fileoff_t startoff)
5a35bf2c 5623{
364937e2 5624 struct xfs_mount *mp = ip->i_mount;
f36ccac2 5625 struct xfs_bmbt_irec prev = *got;
364937e2 5626 int error, i;
2f082827 5627
5a35bf2c 5628 *logflags |= XFS_ILOG_CORE;
2f082827 5629
f36ccac2 5630 got->br_startoff = startoff;
2f082827
CH
5631
5632 if (cur) {
f36ccac2 5633 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
2f082827
CH
5634 if (error)
5635 return error;
4467a60a
DW
5636 if (XFS_IS_CORRUPT(mp, i != 1)) {
5637 xfs_btree_mark_sick(cur);
fbb4fa7f 5638 return -EFSCORRUPTED;
4467a60a 5639 }
2f082827 5640
f36ccac2 5641 error = xfs_bmbt_update(cur, got);
2f082827
CH
5642 if (error)
5643 return error;
5644 } else {
5a35bf2c 5645 *logflags |= XFS_ILOG_DEXT;
5a35bf2c
DC
5646 }
5647
9788e059
CH
5648 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5649 got);
d7f80320 5650
d7f80320 5651 /* update reverse mapping */
46d29bb9
DW
5652 xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
5653 xfs_rmap_map_extent(tp, ip, whichfork, got);
5654 return 0;
5a35bf2c
DC
5655}
5656
ff105f75 5657int
a9f5d25e 5658xfs_bmap_collapse_extents(
ff105f75
DC
5659 struct xfs_trans *tp,
5660 struct xfs_inode *ip,
19ebedcf 5661 xfs_fileoff_t *next_fsb,
ff105f75 5662 xfs_fileoff_t offset_shift_fsb,
a397f27d 5663 bool *done)
ff105f75 5664{
a9f5d25e
CH
5665 int whichfork = XFS_DATA_FORK;
5666 struct xfs_mount *mp = ip->i_mount;
722e81c1 5667 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
a9f5d25e 5668 struct xfs_btree_cur *cur = NULL;
364937e2 5669 struct xfs_bmbt_irec got, prev;
9788e059 5670 struct xfs_iext_cursor icur;
364937e2 5671 xfs_fileoff_t new_startoff;
a9f5d25e
CH
5672 int error = 0;
5673 int logflags = 0;
ff105f75 5674
d967a68d 5675 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
bc73da84 5676 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
6d5b5846 5677 xfs_bmap_mark_sick(ip, whichfork);
12b53197 5678 return -EFSCORRUPTED;
ff105f75
DC
5679 }
5680
93adb06a 5681 if (xfs_is_shutdown(mp))
12b53197 5682 return -EIO;
ff105f75 5683
d3d1a2cb 5684 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
ff105f75 5685
e00c57e7
CH
5686 error = xfs_iread_extents(tp, ip, whichfork);
5687 if (error)
5688 return error;
ff105f75 5689
d242dfc3 5690 if (ifp->if_format == XFS_DINODE_FMT_BTREE)
ff105f75 5691 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5a35bf2c 5692
9788e059 5693 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
a9f5d25e
CH
5694 *done = true;
5695 goto del_cursor;
5696 }
fbb4fa7f 5697 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
4467a60a 5698 xfs_bmap_mark_sick(ip, whichfork);
fbb4fa7f
DW
5699 error = -EFSCORRUPTED;
5700 goto del_cursor;
5701 }
a9f5d25e 5702
364937e2 5703 new_startoff = got.br_startoff - offset_shift_fsb;
9788e059 5704 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
364937e2
CH
5705 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5706 error = -EINVAL;
5707 goto del_cursor;
5708 }
5709
15fb060e
CH
5710 if (xfs_bmse_can_merge(ip, whichfork, &prev, &got,
5711 offset_shift_fsb)) {
21375e5d
BF
5712 error = xfs_bmse_merge(tp, ip, whichfork,
5713 offset_shift_fsb, &icur, &got, &prev,
5714 cur, &logflags);
364937e2
CH
5715 if (error)
5716 goto del_cursor;
5717 goto done;
5718 }
5719 } else {
5720 if (got.br_startoff < offset_shift_fsb) {
5721 error = -EINVAL;
5722 goto del_cursor;
5723 }
5724 }
5725
21375e5d
BF
5726 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5727 cur, &logflags, new_startoff);
a9f5d25e
CH
5728 if (error)
5729 goto del_cursor;
bac20498 5730
c162e319 5731done:
9788e059
CH
5732 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5733 *done = true;
5734 goto del_cursor;
a9f5d25e 5735 }
a9f5d25e 5736
364937e2 5737 *next_fsb = got.br_startoff;
a9f5d25e
CH
5738del_cursor:
5739 if (cur)
660265b7 5740 xfs_btree_del_cursor(cur, error);
a9f5d25e
CH
5741 if (logflags)
5742 xfs_trans_log_inode(tp, ip, logflags);
a9f5d25e
CH
5743 return error;
5744}
5745
5b501597
DW
5746/* Make sure we won't be right-shifting an extent past the maximum bound. */
5747int
5748xfs_bmap_can_insert_extents(
5749 struct xfs_inode *ip,
5750 xfs_fileoff_t off,
5751 xfs_fileoff_t shift)
5752{
5753 struct xfs_bmbt_irec got;
5754 int is_empty;
5755 int error = 0;
5756
d3d1a2cb 5757 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL);
5b501597 5758
93adb06a 5759 if (xfs_is_shutdown(ip->i_mount))
5b501597
DW
5760 return -EIO;
5761
5762 xfs_ilock(ip, XFS_ILOCK_EXCL);
5763 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
5764 if (!error && !is_empty && got.br_startoff >= off &&
5765 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
5766 error = -EINVAL;
5767 xfs_iunlock(ip, XFS_ILOCK_EXCL);
5768
5769 return error;
5770}
5771
a9f5d25e
CH
5772int
5773xfs_bmap_insert_extents(
5774 struct xfs_trans *tp,
5775 struct xfs_inode *ip,
5776 xfs_fileoff_t *next_fsb,
5777 xfs_fileoff_t offset_shift_fsb,
5778 bool *done,
a397f27d 5779 xfs_fileoff_t stop_fsb)
a9f5d25e
CH
5780{
5781 int whichfork = XFS_DATA_FORK;
5782 struct xfs_mount *mp = ip->i_mount;
722e81c1 5783 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
a9f5d25e 5784 struct xfs_btree_cur *cur = NULL;
bd80a804 5785 struct xfs_bmbt_irec got, next;
9788e059 5786 struct xfs_iext_cursor icur;
364937e2 5787 xfs_fileoff_t new_startoff;
a9f5d25e
CH
5788 int error = 0;
5789 int logflags = 0;
5790
d967a68d 5791 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
bc73da84 5792 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
6d5b5846 5793 xfs_bmap_mark_sick(ip, whichfork);
a9f5d25e
CH
5794 return -EFSCORRUPTED;
5795 }
5796
93adb06a 5797 if (xfs_is_shutdown(mp))
a9f5d25e
CH
5798 return -EIO;
5799
d3d1a2cb 5800 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
a9f5d25e 5801
e00c57e7
CH
5802 error = xfs_iread_extents(tp, ip, whichfork);
5803 if (error)
5804 return error;
a9f5d25e 5805
d242dfc3 5806 if (ifp->if_format == XFS_DINODE_FMT_BTREE)
a9f5d25e 5807 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
a9f5d25e 5808
19ebedcf 5809 if (*next_fsb == NULLFSBLOCK) {
9788e059
CH
5810 xfs_iext_last(ifp, &icur);
5811 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
bd80a804 5812 stop_fsb > got.br_startoff) {
a9f5d25e 5813 *done = true;
19ebedcf
DC
5814 goto del_cursor;
5815 }
b6ad780b 5816 } else {
9788e059 5817 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
a9f5d25e 5818 *done = true;
b6ad780b
CH
5819 goto del_cursor;
5820 }
19ebedcf 5821 }
fbb4fa7f 5822 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
4467a60a 5823 xfs_bmap_mark_sick(ip, whichfork);
fbb4fa7f
DW
5824 error = -EFSCORRUPTED;
5825 goto del_cursor;
5826 }
19ebedcf 5827
e4d719e1 5828 if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) {
4467a60a 5829 xfs_bmap_mark_sick(ip, whichfork);
88afc9cc 5830 error = -EFSCORRUPTED;
a9f5d25e 5831 goto del_cursor;
19ebedcf
DC
5832 }
5833
364937e2 5834 new_startoff = got.br_startoff + offset_shift_fsb;
9788e059 5835 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
364937e2
CH
5836 if (new_startoff + got.br_blockcount > next.br_startoff) {
5837 error = -EINVAL;
5838 goto del_cursor;
5839 }
5840
5841 /*
5842 * Unlike a left shift (which involves a hole punch), a right
5843 * shift does not modify extent neighbors in any way. We should
5844 * never find mergeable extents in this scenario. Check anyways
5845 * and warn if we encounter two extents that could be one.
5846 */
15fb060e
CH
5847 if (xfs_bmse_can_merge(ip, whichfork, &got, &next,
5848 offset_shift_fsb))
364937e2
CH
5849 WARN_ON_ONCE(1);
5850 }
5851
21375e5d
BF
5852 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5853 cur, &logflags, new_startoff);
6835c363
CH
5854 if (error)
5855 goto del_cursor;
bd80a804 5856
9788e059 5857 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
bd80a804 5858 stop_fsb >= got.br_startoff + got.br_blockcount) {
a9f5d25e 5859 *done = true;
6835c363 5860 goto del_cursor;
ff105f75
DC
5861 }
5862
6835c363 5863 *next_fsb = got.br_startoff;
ff105f75
DC
5864del_cursor:
5865 if (cur)
660265b7 5866 xfs_btree_del_cursor(cur, error);
5a35bf2c
DC
5867 if (logflags)
5868 xfs_trans_log_inode(tp, ip, logflags);
ff105f75
DC
5869 return error;
5870}
19ebedcf
DC
5871
5872/*
9788e059
CH
5873 * Splits an extent into two extents at split_fsb block such that it is the
5874 * first block of the current_ext. @ext is a target extent to be split.
5875 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5876 * hole or the first block of extents, just return 0.
19ebedcf 5877 */
905a3c99
BF
5878int
5879xfs_bmap_split_extent(
19ebedcf
DC
5880 struct xfs_trans *tp,
5881 struct xfs_inode *ip,
312350cd 5882 xfs_fileoff_t split_fsb)
19ebedcf
DC
5883{
5884 int whichfork = XFS_DATA_FORK;
722e81c1 5885 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
19ebedcf 5886 struct xfs_btree_cur *cur = NULL;
19ebedcf
DC
5887 struct xfs_bmbt_irec got;
5888 struct xfs_bmbt_irec new; /* split extent */
5889 struct xfs_mount *mp = ip->i_mount;
19ebedcf 5890 xfs_fsblock_t gotblkcnt; /* new block count for got */
9788e059 5891 struct xfs_iext_cursor icur;
19ebedcf
DC
5892 int error = 0;
5893 int logflags = 0;
5894 int i = 0;
5895
d967a68d 5896 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
bc73da84 5897 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
6d5b5846 5898 xfs_bmap_mark_sick(ip, whichfork);
19ebedcf
DC
5899 return -EFSCORRUPTED;
5900 }
5901
93adb06a 5902 if (xfs_is_shutdown(mp))
19ebedcf
DC
5903 return -EIO;
5904
e00c57e7
CH
5905 /* Read in all the extents */
5906 error = xfs_iread_extents(tp, ip, whichfork);
5907 if (error)
5908 return error;
19ebedcf
DC
5909
5910 /*
455044a6 5911 * If there are not extents, or split_fsb lies in a hole we are done.
19ebedcf 5912 */
9788e059 5913 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
455044a6 5914 got.br_startoff >= split_fsb)
19ebedcf
DC
5915 return 0;
5916
5917 gotblkcnt = split_fsb - got.br_startoff;
5918 new.br_startoff = split_fsb;
5919 new.br_startblock = got.br_startblock + gotblkcnt;
5920 new.br_blockcount = got.br_blockcount - gotblkcnt;
5921 new.br_state = got.br_state;
5922
84094546 5923 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
19ebedcf 5924 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
70a93110 5925 error = xfs_bmbt_lookup_eq(cur, &got, &i);
19ebedcf
DC
5926 if (error)
5927 goto del_cursor;
fbb4fa7f 5928 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 5929 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
5930 error = -EFSCORRUPTED;
5931 goto del_cursor;
5932 }
19ebedcf
DC
5933 }
5934
19ebedcf 5935 got.br_blockcount = gotblkcnt;
9788e059
CH
5936 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
5937 &got);
19ebedcf
DC
5938
5939 logflags = XFS_ILOG_CORE;
5940 if (cur) {
d0e5f1ff 5941 error = xfs_bmbt_update(cur, &got);
19ebedcf
DC
5942 if (error)
5943 goto del_cursor;
5944 } else
5945 logflags |= XFS_ILOG_DEXT;
5946
5947 /* Add new extent */
9788e059 5948 xfs_iext_next(ifp, &icur);
26a75f67 5949 xfs_iext_insert(ip, &icur, &new, 0);
87c472b7 5950 ifp->if_nextents++;
19ebedcf
DC
5951
5952 if (cur) {
70a93110 5953 error = xfs_bmbt_lookup_eq(cur, &new, &i);
19ebedcf
DC
5954 if (error)
5955 goto del_cursor;
fbb4fa7f 5956 if (XFS_IS_CORRUPT(mp, i != 0)) {
4467a60a 5957 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
5958 error = -EFSCORRUPTED;
5959 goto del_cursor;
5960 }
19ebedcf
DC
5961 error = xfs_btree_insert(cur, &i);
5962 if (error)
5963 goto del_cursor;
fbb4fa7f 5964 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 5965 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
5966 error = -EFSCORRUPTED;
5967 goto del_cursor;
5968 }
19ebedcf
DC
5969 }
5970
5971 /*
5972 * Convert to a btree if necessary.
5973 */
5974 if (xfs_bmap_needs_btree(ip, whichfork)) {
5975 int tmp_logflags; /* partial log flag return val */
5976
5977 ASSERT(cur == NULL);
f7253505
BF
5978 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5979 &tmp_logflags, whichfork);
19ebedcf
DC
5980 logflags |= tmp_logflags;
5981 }
5982
5983del_cursor:
5984 if (cur) {
6aba4616 5985 cur->bc_bmap.allocated = 0;
660265b7 5986 xfs_btree_del_cursor(cur, error);
19ebedcf
DC
5987 }
5988
5989 if (logflags)
5990 xfs_trans_log_inode(tp, ip, logflags);
5991 return error;
5992}
5993
aeb88300 5994/* Record a bmap intent. */
11900c01 5995static inline void
aeb88300 5996__xfs_bmap_add(
21375e5d 5997 struct xfs_trans *tp,
aeb88300
DW
5998 enum xfs_bmap_intent_type type,
5999 struct xfs_inode *ip,
6000 int whichfork,
6001 struct xfs_bmbt_irec *bmap)
6002{
aeb88300
DW
6003 struct xfs_bmap_intent *bi;
6004
11900c01
DW
6005 if ((whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK) ||
6006 bmap->br_startblock == HOLESTARTBLOCK ||
6007 bmap->br_startblock == DELAYSTARTBLOCK)
6008 return;
6009
a6cb3f66 6010 bi = kmem_cache_alloc(xfs_bmap_intent_cache, GFP_KERNEL | __GFP_NOFAIL);
aeb88300
DW
6011 INIT_LIST_HEAD(&bi->bi_list);
6012 bi->bi_type = type;
6013 bi->bi_owner = ip;
6014 bi->bi_whichfork = whichfork;
6015 bi->bi_bmap = *bmap;
6016
9e471add 6017 xfs_bmap_defer_add(tp, bi);
aeb88300
DW
6018}
6019
6020/* Map an extent into a file. */
60a802cc 6021void
aeb88300 6022xfs_bmap_map_extent(
21375e5d 6023 struct xfs_trans *tp,
aeb88300 6024 struct xfs_inode *ip,
11900c01 6025 int whichfork,
aeb88300
DW
6026 struct xfs_bmbt_irec *PREV)
6027{
11900c01 6028 __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, whichfork, PREV);
aeb88300
DW
6029}
6030
6031/* Unmap an extent out of a file. */
60a802cc 6032void
aeb88300 6033xfs_bmap_unmap_extent(
21375e5d 6034 struct xfs_trans *tp,
aeb88300 6035 struct xfs_inode *ip,
11900c01 6036 int whichfork,
aeb88300
DW
6037 struct xfs_bmbt_irec *PREV)
6038{
11900c01 6039 __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, whichfork, PREV);
aeb88300
DW
6040}
6041
6042/*
6043 * Process one of the deferred bmap operations. We pass back the
6044 * btree cursor to maintain our lock on the bmapbt between calls.
6045 */
6046int
6047xfs_bmap_finish_one(
6048 struct xfs_trans *tp,
afe1175f 6049 struct xfs_bmap_intent *bi)
aeb88300 6050{
afe1175f 6051 struct xfs_bmbt_irec *bmap = &bi->bi_bmap;
594956fa 6052 int error = 0;
11900c01
DW
6053 int flags = 0;
6054
6055 if (bi->bi_whichfork == XFS_ATTR_FORK)
6056 flags |= XFS_BMAPI_ATTRFORK;
aeb88300 6057
9cdecd7e 6058 ASSERT(tp->t_highest_agno == NULLAGNUMBER);
66d19ae1 6059
588d0291 6060 trace_xfs_bmap_deferred(bi);
aeb88300 6061
11900c01 6062 if (XFS_TEST_ERROR(false, tp->t_mountp, XFS_ERRTAG_BMAP_FINISH_ONE))
aeb88300
DW
6063 return -EIO;
6064
afe1175f 6065 switch (bi->bi_type) {
aeb88300 6066 case XFS_BMAP_MAP:
409e07e5
DW
6067 if (bi->bi_bmap.br_state == XFS_EXT_UNWRITTEN)
6068 flags |= XFS_BMAPI_PREALLOC;
afe1175f 6069 error = xfs_bmapi_remap(tp, bi->bi_owner, bmap->br_startoff,
11900c01
DW
6070 bmap->br_blockcount, bmap->br_startblock,
6071 flags);
afe1175f 6072 bmap->br_blockcount = 0;
aeb88300
DW
6073 break;
6074 case XFS_BMAP_UNMAP:
afe1175f 6075 error = __xfs_bunmapi(tp, bi->bi_owner, bmap->br_startoff,
11900c01
DW
6076 &bmap->br_blockcount, flags | XFS_BMAPI_REMAP,
6077 1);
aeb88300
DW
6078 break;
6079 default:
6080 ASSERT(0);
6d5b5846 6081 xfs_bmap_mark_sick(bi->bi_owner, bi->bi_whichfork);
aeb88300
DW
6082 error = -EFSCORRUPTED;
6083 }
6084
6085 return error;
6086}
0cf6a3a9 6087
7c5d4f92 6088/* Check that an extent does not have invalid flags or bad ranges. */
0cf6a3a9 6089xfs_failaddr_t
7c5d4f92
DW
6090xfs_bmap_validate_extent_raw(
6091 struct xfs_mount *mp,
6092 bool rtfile,
0cf6a3a9
DW
6093 int whichfork,
6094 struct xfs_bmbt_irec *irec)
6095{
7626c690 6096 if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
92415d01
DW
6097 return __this_address;
6098
7c5d4f92 6099 if (rtfile && whichfork == XFS_DATA_FORK) {
95bc0a4d
DW
6100 if (!xfs_verify_rtbext(mp, irec->br_startblock,
6101 irec->br_blockcount))
0cf6a3a9
DW
6102 return __this_address;
6103 } else {
f28f7e4a
DW
6104 if (!xfs_verify_fsbext(mp, irec->br_startblock,
6105 irec->br_blockcount))
0cf6a3a9
DW
6106 return __this_address;
6107 }
b7fdeb4d
CH
6108 if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6109 return __this_address;
0cf6a3a9
DW
6110 return NULL;
6111}
1577541c
DW
6112
6113int __init
6114xfs_bmap_intent_init_cache(void)
6115{
6116 xfs_bmap_intent_cache = kmem_cache_create("xfs_bmap_intent",
6117 sizeof(struct xfs_bmap_intent),
6118 0, 0, NULL);
6119
6120 return xfs_bmap_intent_cache != NULL ? 0 : -ENOMEM;
6121}
6122
6123void
6124xfs_bmap_intent_destroy_cache(void)
6125{
6126 kmem_cache_destroy(xfs_bmap_intent_cache);
6127 xfs_bmap_intent_cache = NULL;
6128}
7c5d4f92
DW
6129
6130/* Check that an inode's extent does not have invalid flags or bad ranges. */
6131xfs_failaddr_t
6132xfs_bmap_validate_extent(
6133 struct xfs_inode *ip,
6134 int whichfork,
6135 struct xfs_bmbt_irec *irec)
6136{
6137 return xfs_bmap_validate_extent_raw(ip->i_mount,
6138 XFS_IS_REALTIME_INODE(ip), whichfork, irec);
6139}
f76f73b3
DW
6140
6141/*
6142 * Used in xfs_itruncate_extents(). This is the maximum number of extents
6143 * freed from a file in a single transaction.
6144 */
6145#define XFS_ITRUNC_MAX_EXTENTS 2
6146
6147/*
6148 * Unmap every extent in part of an inode's fork. We don't do any higher level
6149 * invalidation work at all.
6150 */
6151int
6152xfs_bunmapi_range(
6153 struct xfs_trans **tpp,
6154 struct xfs_inode *ip,
6155 uint32_t flags,
6156 xfs_fileoff_t startoff,
6157 xfs_fileoff_t endoff)
6158{
6159 xfs_filblks_t unmap_len = endoff - startoff + 1;
6160 int error = 0;
6161
d3d1a2cb 6162 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
f76f73b3
DW
6163
6164 while (unmap_len > 0) {
6165 ASSERT((*tpp)->t_highest_agno == NULLAGNUMBER);
6166 error = __xfs_bunmapi(*tpp, ip, startoff, &unmap_len, flags,
6167 XFS_ITRUNC_MAX_EXTENTS);
6168 if (error)
6169 goto out;
6170
6171 /* free the just unmapped extents */
6172 error = xfs_defer_finish(tpp);
6173 if (error)
6174 goto out;
03a887e4 6175 cond_resched();
f76f73b3
DW
6176 }
6177out:
6178 return error;
6179}
048c5321
DW
6180
6181struct xfs_bmap_query_range {
6182 xfs_bmap_query_range_fn fn;
6183 void *priv;
6184};
6185
6186/* Format btree record and pass to our callback. */
6187STATIC int
6188xfs_bmap_query_range_helper(
6189 struct xfs_btree_cur *cur,
6190 const union xfs_btree_rec *rec,
6191 void *priv)
6192{
6193 struct xfs_bmap_query_range *query = priv;
6194 struct xfs_bmbt_irec irec;
6195 xfs_failaddr_t fa;
6196
6197 xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
6198 fa = xfs_bmap_validate_extent(cur->bc_ino.ip, cur->bc_ino.whichfork,
6199 &irec);
6200 if (fa) {
6201 xfs_btree_mark_sick(cur);
6202 return xfs_bmap_complain_bad_rec(cur->bc_ino.ip,
6203 cur->bc_ino.whichfork, fa, &irec);
6204 }
6205
6206 return query->fn(cur, &irec, query->priv);
6207}
6208
6209/* Find all bmaps. */
6210int
6211xfs_bmap_query_all(
6212 struct xfs_btree_cur *cur,
6213 xfs_bmap_query_range_fn fn,
6214 void *priv)
6215{
6216 struct xfs_bmap_query_range query = {
6217 .priv = priv,
6218 .fn = fn,
6219 };
6220
6221 return xfs_btree_query_all(cur, xfs_bmap_query_range_helper, &query);
6222}
8a8799bb
DW
6223
6224/* Helper function to extract extent size hint from inode */
6225xfs_extlen_t
6226xfs_get_extsz_hint(
6227 struct xfs_inode *ip)
6228{
6229 /*
6230 * No point in aligning allocations if we need to COW to actually
6231 * write to them.
6232 */
d243e767
DW
6233 if (!xfs_is_always_cow_inode(ip) &&
6234 (ip->i_diflags & XFS_DIFLAG_EXTSIZE) && ip->i_extsize)
8a8799bb
DW
6235 return ip->i_extsize;
6236 if (XFS_IS_REALTIME_INODE(ip) &&
6237 ip->i_mount->m_sb.sb_rextsize > 1)
6238 return ip->i_mount->m_sb.sb_rextsize;
6239 return 0;
6240}
6241
6242/*
6243 * Helper function to extract CoW extent size hint from inode.
6244 * Between the extent size hint and the CoW extent size hint, we
6245 * return the greater of the two. If the value is zero (automatic),
6246 * use the default size.
6247 */
6248xfs_extlen_t
6249xfs_get_cowextsz_hint(
6250 struct xfs_inode *ip)
6251{
6252 xfs_extlen_t a, b;
6253
6254 a = 0;
6255 if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
6256 a = ip->i_cowextsize;
3ae3aadc
DW
6257 if (XFS_IS_REALTIME_INODE(ip)) {
6258 b = 0;
6259 if (ip->i_diflags & XFS_DIFLAG_EXTSIZE)
6260 b = ip->i_extsize;
6261 } else {
6262 b = xfs_get_extsz_hint(ip);
6263 }
8a8799bb
DW
6264
6265 a = max(a, b);
6266 if (a == 0)
6267 return XFS_DEFAULT_COWEXTSZ_HINT;
6268 return a;
6269}