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