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