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