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