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