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