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