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