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