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