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