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