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