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