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