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