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