blob: 8090e8249116d0b24d506b495455b996be555eac [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11007#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11008#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +11009#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110012#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_sb.h"
Dave Chinnerf5ea1102013-04-24 18:58:02 +100014#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100015#include "xfs_defer.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100016#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110018#include "xfs_btree.h"
Dave Chinner239880e2013-10-23 10:50:10 +110019#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_alloc.h"
21#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100022#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110023#include "xfs_bmap_btree.h"
Darrick J. Wong13928112023-10-16 09:21:47 -070024#include "xfs_rtbitmap.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070025#include "xfs_errortag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_quota.h"
28#include "xfs_trans_space.h"
29#include "xfs_buf_item.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000030#include "xfs_trace.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110031#include "xfs_attr_leaf.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110032#include "xfs_filestream.h"
Darrick J. Wong340785c2016-08-03 11:33:42 +100033#include "xfs_rmap.h"
Dave Chinner9bbafc712021-06-02 10:48:24 +100034#include "xfs_ag.h"
Darrick J. Wong3fd129b2016-09-19 10:30:52 +100035#include "xfs_ag_resv.h"
Darrick J. Wong62aab202016-10-03 09:11:23 -070036#include "xfs_refcount.h"
Brian Foster974ae922016-11-28 14:57:42 +110037#include "xfs_icache.h"
Christoph Hellwig4e087a32019-10-17 13:12:06 -070038#include "xfs_iomap.h"
Darrick J. Wong1196f3f2024-02-22 12:31:51 -080039#include "xfs_health.h"
Darrick J. Wong80284112024-02-22 12:44:21 -080040#include "xfs_bmap_item.h"
Darrick J. Wong622d88e22024-02-22 12:45:01 -080041#include "xfs_symlink_remote.h"
Darrick J. Wongb7c477b2024-07-02 11:22:32 -070042#include "xfs_inode_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Darrick J. Wongf3c799c2021-10-12 14:11:01 -070044struct kmem_cache *xfs_bmap_intent_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/*
Dave Chinner9e5987a72013-02-25 12:31:26 +110047 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
Dave Chinner9e5987a72013-02-25 12:31:26 +110051 * Compute and fill in the value of the maximum depth of a bmap btree
52 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 */
Dave Chinner9e5987a72013-02-25 12:31:26 +110054void
55xfs_bmap_compute_maxlevels(
56 xfs_mount_t *mp, /* file system mount structure */
57 int whichfork) /* data or attr fork */
58{
Chandan Babu R0c35e7b2021-11-16 09:20:01 +000059 uint64_t maxblocks; /* max blocks at this level */
Chandan Babu Rbb1d50492021-02-26 11:24:31 +053060 xfs_extnum_t maxleafents; /* max leaf entries possible */
Dave Chinner9e5987a72013-02-25 12:31:26 +110061 int level; /* btree level */
Dave Chinner9e5987a72013-02-25 12:31:26 +110062 int maxrootrecs; /* max records in root block */
63 int minleafrecs; /* min records in leaf block */
64 int minnoderecs; /* min records in node block */
65 int sz; /* root block size */
66
67 /*
Chandan Babu Rdf9ad5c2021-11-16 09:54:37 +000068 * The maximum number of extents in a fork, hence the maximum number of
69 * leaf entries, is controlled by the size of the on-disk extent count.
Dave Chinner9e5987a72013-02-25 12:31:26 +110070 *
Christoph Hellwig7821ea32021-03-29 11:11:44 -070071 * Note that we can no longer assume that if we are in ATTR1 that the
72 * fork offset of all the inodes will be
73 * (xfs_default_attroffset(ip) >> 3) because we could have mounted with
74 * ATTR2 and then mounted back with ATTR1, keeping the i_forkoff's fixed
75 * but probably at various positions. Therefore, for both ATTR1 and
76 * ATTR2 we have to assume the worst case scenario of a minimum size
77 * available.
Dave Chinner9e5987a72013-02-25 12:31:26 +110078 */
Chandan Babu Rdf9ad5c2021-11-16 09:54:37 +000079 maxleafents = xfs_iext_max_nextents(xfs_has_large_extent_counts(mp),
80 whichfork);
Chandan Babu R9feb8f12020-08-27 15:09:10 +053081 if (whichfork == XFS_DATA_FORK)
Darrick J. Wong79124b32024-08-30 15:37:20 -070082 sz = xfs_bmdr_space_calc(MINDBTPTRS);
Chandan Babu R9feb8f12020-08-27 15:09:10 +053083 else
Darrick J. Wong79124b32024-08-30 15:37:20 -070084 sz = xfs_bmdr_space_calc(MINABTPTRS);
Chandan Babu R9feb8f12020-08-27 15:09:10 +053085
Eric Sandeen152d93b2014-04-14 18:58:51 +100086 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +110087 minleafrecs = mp->m_bmap_dmnr[0];
88 minnoderecs = mp->m_bmap_dmnr[1];
Chandan Babu R755c38f2021-11-16 07:28:40 +000089 maxblocks = howmany_64(maxleafents, minleafrecs);
Dave Chinner9e5987a72013-02-25 12:31:26 +110090 for (level = 1; maxblocks > 1; level++) {
91 if (maxblocks <= maxrootrecs)
92 maxblocks = 1;
93 else
Chandan Babu R0c35e7b2021-11-16 09:20:01 +000094 maxblocks = howmany_64(maxblocks, minnoderecs);
Dave Chinner9e5987a72013-02-25 12:31:26 +110095 }
96 mp->m_bm_maxlevels[whichfork] = level;
Darrick J. Wong0ed5f732021-09-23 10:32:06 -070097 ASSERT(mp->m_bm_maxlevels[whichfork] <= xfs_bmbt_maxlevels_ondisk());
Dave Chinner9e5987a72013-02-25 12:31:26 +110098}
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Dave Chinnerb2941042021-04-06 07:03:24 -0700100unsigned int
101xfs_bmap_compute_attr_offset(
102 struct xfs_mount *mp)
103{
104 if (mp->m_sb.sb_inodesize == 256)
Darrick J. Wong79124b32024-08-30 15:37:20 -0700105 return XFS_LITINO(mp) - xfs_bmdr_space_calc(MINABTPTRS);
106 return xfs_bmdr_space_calc(6 * MINABTPTRS);
Dave Chinnerb2941042021-04-06 07:03:24 -0700107}
108
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100109STATIC int /* error */
110xfs_bmbt_lookup_eq(
111 struct xfs_btree_cur *cur,
Christoph Hellwige16cf9b2017-10-17 14:16:26 -0700112 struct xfs_bmbt_irec *irec,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100113 int *stat) /* success/failure */
114{
Christoph Hellwige16cf9b2017-10-17 14:16:26 -0700115 cur->bc_rec.b = *irec;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100116 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
117}
118
119STATIC int /* error */
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700120xfs_bmbt_lookup_first(
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100121 struct xfs_btree_cur *cur,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100122 int *stat) /* success/failure */
123{
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700124 cur->bc_rec.b.br_startoff = 0;
125 cur->bc_rec.b.br_startblock = 0;
126 cur->bc_rec.b.br_blockcount = 0;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100127 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
128}
129
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100130/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000131 * Check if the inode needs to be converted to btree format.
132 */
133static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
134{
Darrick J. Wong732436e2022-07-09 10:56:05 -0700135 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700136
Darrick J. Wong60b49842016-10-03 09:11:34 -0700137 return whichfork != XFS_COW_FORK &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700138 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700139 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000140}
141
142/*
143 * Check if the inode should be converted to extent format.
144 */
145static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
146{
Darrick J. Wong732436e2022-07-09 10:56:05 -0700147 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700148
Darrick J. Wong60b49842016-10-03 09:11:34 -0700149 return whichfork != XFS_COW_FORK &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700150 ifp->if_format == XFS_DINODE_FMT_BTREE &&
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700151 ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000152}
153
154/*
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700155 * Update the record referred to by cur to the value given by irec
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100156 * This either works (return 0) or gets an EFSCORRUPTED error.
157 */
158STATIC int
159xfs_bmbt_update(
160 struct xfs_btree_cur *cur,
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700161 struct xfs_bmbt_irec *irec)
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100162{
163 union xfs_btree_rec rec;
164
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700165 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100166 return xfs_btree_update(cur, &rec);
167}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/*
Dave Chinner9e5987a72013-02-25 12:31:26 +1100170 * Compute the worst-case number of indirect blocks that will be used
171 * for ip's delayed extent of length "len".
172 */
173STATIC xfs_filblks_t
174xfs_bmap_worst_indlen(
175 xfs_inode_t *ip, /* incore inode pointer */
176 xfs_filblks_t len) /* delayed extent length */
177{
178 int level; /* btree level number */
179 int maxrecs; /* maximum record count at this level */
180 xfs_mount_t *mp; /* mount structure */
181 xfs_filblks_t rval; /* return value */
182
183 mp = ip->i_mount;
184 maxrecs = mp->m_bmap_dmxr[0];
185 for (level = 0, rval = 0;
186 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
187 level++) {
188 len += maxrecs - 1;
189 do_div(len, maxrecs);
190 rval += len;
Darrick J. Wong5e5c9432017-09-18 09:41:17 -0700191 if (len == 1)
192 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
Dave Chinner9e5987a72013-02-25 12:31:26 +1100193 level - 1;
194 if (level == 0)
195 maxrecs = mp->m_bmap_dmxr[1];
196 }
197 return rval;
198}
199
200/*
201 * Calculate the default attribute fork offset for newly created inodes.
202 */
203uint
204xfs_default_attroffset(
205 struct xfs_inode *ip)
206{
Dave Chinner683ec9b2021-04-06 07:02:04 -0700207 if (ip->i_df.if_format == XFS_DINODE_FMT_DEV)
208 return roundup(sizeof(xfs_dev_t), 8);
Dave Chinnerb2941042021-04-06 07:03:24 -0700209 return M_IGEO(ip->i_mount)->attr_fork_offset;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100210}
211
212/*
Christoph Hellwig7821ea32021-03-29 11:11:44 -0700213 * Helper routine to reset inode i_forkoff field when switching attribute fork
214 * from local to extent format - we reset it where possible to make space
215 * available for inline data fork extents.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100216 */
217STATIC void
218xfs_bmap_forkoff_reset(
Dave Chinner9e5987a72013-02-25 12:31:26 +1100219 xfs_inode_t *ip,
220 int whichfork)
221{
222 if (whichfork == XFS_ATTR_FORK &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700223 ip->i_df.if_format != XFS_DINODE_FMT_DEV &&
224 ip->i_df.if_format != XFS_DINODE_FMT_BTREE) {
Dave Chinner9e5987a72013-02-25 12:31:26 +1100225 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
226
Christoph Hellwig7821ea32021-03-29 11:11:44 -0700227 if (dfl_forkoff > ip->i_forkoff)
228 ip->i_forkoff = dfl_forkoff;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100229 }
230}
231
Christoph Hellwig6a701eb2024-02-22 12:41:01 -0800232static int
233xfs_bmap_read_buf(
234 struct xfs_mount *mp, /* file system mount point */
235 struct xfs_trans *tp, /* transaction pointer */
236 xfs_fsblock_t fsbno, /* file system block number */
237 struct xfs_buf **bpp) /* buffer for fsbno */
238{
239 struct xfs_buf *bp; /* return value */
240 int error;
241
242 if (!xfs_verify_fsbno(mp, fsbno))
243 return -EFSCORRUPTED;
244 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
245 XFS_FSB_TO_DADDR(mp, fsbno), mp->m_bsize, 0, &bp,
246 &xfs_bmbt_buf_ops);
247 if (!error) {
248 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
249 *bpp = bp;
250 }
251 return error;
252}
253
Dave Chinner9e5987a72013-02-25 12:31:26 +1100254#ifdef DEBUG
255STATIC struct xfs_buf *
256xfs_bmap_get_bp(
257 struct xfs_btree_cur *cur,
258 xfs_fsblock_t bno)
259{
Dave Chinnere6631f82018-05-09 07:49:37 -0700260 struct xfs_log_item *lip;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100261 int i;
262
263 if (!cur)
264 return NULL;
265
Darrick J. Wongc0643f62021-09-16 12:26:56 -0700266 for (i = 0; i < cur->bc_maxlevels; i++) {
Darrick J. Wong6ca444cf2021-09-16 12:24:04 -0700267 if (!cur->bc_levels[i].bp)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100268 break;
Darrick J. Wong6ca444cf2021-09-16 12:24:04 -0700269 if (xfs_buf_daddr(cur->bc_levels[i].bp) == bno)
270 return cur->bc_levels[i].bp;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100271 }
272
273 /* Chase down all the log items to see if the bp is there */
Dave Chinnere6631f82018-05-09 07:49:37 -0700274 list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
275 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
276
Dave Chinner9e5987a72013-02-25 12:31:26 +1100277 if (bip->bli_item.li_type == XFS_LI_BUF &&
Dave Chinner04fcad82021-08-18 18:46:57 -0700278 xfs_buf_daddr(bip->bli_buf) == bno)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100279 return bip->bli_buf;
280 }
281
282 return NULL;
283}
284
285STATIC void
286xfs_check_block(
287 struct xfs_btree_block *block,
288 xfs_mount_t *mp,
289 int root,
290 short sz)
291{
292 int i, j, dmxr;
293 __be64 *pp, *thispa; /* pointer to block address */
294 xfs_bmbt_key_t *prevp, *keyp;
295
296 ASSERT(be16_to_cpu(block->bb_level) > 0);
297
298 prevp = NULL;
299 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
300 dmxr = mp->m_bmap_dmxr[0];
Darrick J. Wong79124b32024-08-30 15:37:20 -0700301 keyp = xfs_bmbt_key_addr(mp, block, i);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100302
303 if (prevp) {
304 ASSERT(be64_to_cpu(prevp->br_startoff) <
305 be64_to_cpu(keyp->br_startoff));
306 }
307 prevp = keyp;
308
309 /*
310 * Compare the block numbers to see if there are dups.
311 */
312 if (root)
Darrick J. Wong79124b32024-08-30 15:37:20 -0700313 pp = xfs_bmap_broot_ptr_addr(mp, block, i, sz);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100314 else
Darrick J. Wong79124b32024-08-30 15:37:20 -0700315 pp = xfs_bmbt_ptr_addr(mp, block, i, dmxr);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100316
317 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
318 if (root)
Darrick J. Wong79124b32024-08-30 15:37:20 -0700319 thispa = xfs_bmap_broot_ptr_addr(mp, block, j, sz);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100320 else
Darrick J. Wong79124b32024-08-30 15:37:20 -0700321 thispa = xfs_bmbt_ptr_addr(mp, block, j, dmxr);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100322 if (*thispa == *pp) {
Zeng Heng78b0f582022-09-19 06:47:14 +1000323 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %lld",
Dave Chinner9e5987a72013-02-25 12:31:26 +1100324 __func__, j, i,
325 (unsigned long long)be64_to_cpu(*thispa));
Darrick J. Wongcec57252018-05-04 15:31:21 -0700326 xfs_err(mp, "%s: ptrs are equal in node\n",
Dave Chinner9e5987a72013-02-25 12:31:26 +1100327 __func__);
Darrick J. Wongcec57252018-05-04 15:31:21 -0700328 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100329 }
330 }
331 }
332}
333
334/*
335 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100336 * btree leaves. THis becomes prohibitively expensive for large extent count
337 * files, so don't bother with inodes that have more than 10,000 extents in
338 * them. The btree record ordering checks will still be done, so for such large
339 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100340 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100341STATIC void
342xfs_bmap_check_leaf_extents(
Darrick J. Wongae127f02021-09-16 12:18:47 -0700343 struct xfs_btree_cur *cur, /* btree cursor or null */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100344 xfs_inode_t *ip, /* incore inode pointer */
345 int whichfork) /* data or attr fork */
346{
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700347 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wong732436e2022-07-09 10:56:05 -0700348 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100349 struct xfs_btree_block *block; /* current btree block */
350 xfs_fsblock_t bno; /* block # of "block" */
Dave Chinnere8222612020-12-16 16:07:34 -0800351 struct xfs_buf *bp; /* buffer for "block" */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100352 int error; /* error return value */
353 xfs_extnum_t i=0, j; /* index into the extents list */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100354 int level; /* btree level, for checking */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100355 __be64 *pp; /* pointer to block address */
356 xfs_bmbt_rec_t *ep; /* pointer to current extent */
357 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
358 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
359 int bp_release = 0;
360
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700361 if (ifp->if_format != XFS_DINODE_FMT_BTREE)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100362 return;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100363
Dave Chinnere3543812016-01-08 11:28:49 +1100364 /* skip large extent count inodes */
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700365 if (ip->i_df.if_nextents > 10000)
Dave Chinnere3543812016-01-08 11:28:49 +1100366 return;
367
Dave Chinner9e5987a72013-02-25 12:31:26 +1100368 bno = NULLFSBLOCK;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100369 block = ifp->if_broot;
370 /*
371 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
372 */
373 level = be16_to_cpu(block->bb_level);
374 ASSERT(level > 0);
375 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
Darrick J. Wong79124b32024-08-30 15:37:20 -0700376 pp = xfs_bmap_broot_ptr_addr(mp, block, 1, ifp->if_broot_bytes);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100377 bno = be64_to_cpu(*pp);
378
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000379 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100380 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
381 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
382
383 /*
384 * Go down the tree until leaf level is reached, following the first
385 * pointer (leftmost) at each level.
386 */
387 while (level-- > 0) {
388 /* See if buf is in cur first */
389 bp_release = 0;
390 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
391 if (!bp) {
392 bp_release = 1;
Christoph Hellwig6a701eb2024-02-22 12:41:01 -0800393 error = xfs_bmap_read_buf(mp, NULL, bno, &bp);
Darrick J. Wonga78d10f2024-02-22 12:32:09 -0800394 if (xfs_metadata_is_sick(error))
395 xfs_btree_mark_sick(cur);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100396 if (error)
397 goto error_norelse;
398 }
399 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100400 if (level == 0)
401 break;
402
403 /*
404 * Check this block for basic sanity (increasing keys and
405 * no duplicate blocks).
406 */
407
408 xfs_check_block(block, mp, 0, 0);
Darrick J. Wong79124b32024-08-30 15:37:20 -0700409 pp = xfs_bmbt_ptr_addr(mp, block, 1, mp->m_bmap_dmxr[1]);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100410 bno = be64_to_cpu(*pp);
Darrick J. Wongf9e03702019-11-11 12:52:18 -0800411 if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbno(mp, bno))) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -0800412 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -0800413 error = -EFSCORRUPTED;
414 goto error0;
415 }
Dave Chinner9e5987a72013-02-25 12:31:26 +1100416 if (bp_release) {
417 bp_release = 0;
418 xfs_trans_brelse(NULL, bp);
419 }
420 }
421
422 /*
423 * Here with bp and block set to the leftmost leaf node in the tree.
424 */
425 i = 0;
426
427 /*
428 * Loop over all leaf nodes checking that all extents are in the right order.
429 */
430 for (;;) {
431 xfs_fsblock_t nextbno;
432 xfs_extnum_t num_recs;
433
434
435 num_recs = xfs_btree_get_numrecs(block);
436
437 /*
438 * Read-ahead the next leaf block, if any.
439 */
440
441 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
442
443 /*
444 * Check all the extents to make sure they are OK.
445 * If we had a previous block, the last entry should
446 * conform with the first entry in this one.
447 */
448
Darrick J. Wong79124b32024-08-30 15:37:20 -0700449 ep = xfs_bmbt_rec_addr(mp, block, 1);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100450 if (i) {
451 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
452 xfs_bmbt_disk_get_blockcount(&last) <=
453 xfs_bmbt_disk_get_startoff(ep));
454 }
455 for (j = 1; j < num_recs; j++) {
Darrick J. Wong79124b32024-08-30 15:37:20 -0700456 nextp = xfs_bmbt_rec_addr(mp, block, j + 1);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100457 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
458 xfs_bmbt_disk_get_blockcount(ep) <=
459 xfs_bmbt_disk_get_startoff(nextp));
460 ep = nextp;
461 }
462
463 last = *ep;
464 i += num_recs;
465 if (bp_release) {
466 bp_release = 0;
467 xfs_trans_brelse(NULL, bp);
468 }
469 bno = nextbno;
470 /*
471 * If we've reached the end, stop.
472 */
473 if (bno == NULLFSBLOCK)
474 break;
475
476 bp_release = 0;
477 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
478 if (!bp) {
479 bp_release = 1;
Christoph Hellwig6a701eb2024-02-22 12:41:01 -0800480 error = xfs_bmap_read_buf(mp, NULL, bno, &bp);
Darrick J. Wonga78d10f2024-02-22 12:32:09 -0800481 if (xfs_metadata_is_sick(error))
482 xfs_btree_mark_sick(cur);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100483 if (error)
484 goto error_norelse;
485 }
486 block = XFS_BUF_TO_BLOCK(bp);
487 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100488
Dave Chinner9e5987a72013-02-25 12:31:26 +1100489 return;
490
491error0:
492 xfs_warn(mp, "%s: at error0", __func__);
493 if (bp_release)
494 xfs_trans_brelse(NULL, bp);
495error_norelse:
Chandan Babu R755c38f2021-11-16 07:28:40 +0000496 xfs_warn(mp, "%s: BAD after btree leaves for %llu extents",
Dave Chinner9e5987a72013-02-25 12:31:26 +1100497 __func__, i);
Darrick J. Wongcec57252018-05-04 15:31:21 -0700498 xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
499 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100500 return;
501}
502
503/*
Dave Chinner9e5987a72013-02-25 12:31:26 +1100504 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000505 * given the caller's original parameters. Specifically check the
506 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a72013-02-25 12:31:26 +1100507 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
508 */
509STATIC void
510xfs_bmap_validate_ret(
511 xfs_fileoff_t bno,
512 xfs_filblks_t len,
Dave Chinnere7d410a2022-04-21 10:46:09 +1000513 uint32_t flags,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100514 xfs_bmbt_irec_t *mval,
515 int nmap,
516 int ret_nmap)
517{
518 int i; /* index to map values */
519
520 ASSERT(ret_nmap <= nmap);
521
522 for (i = 0; i < ret_nmap; i++) {
523 ASSERT(mval[i].br_blockcount > 0);
524 if (!(flags & XFS_BMAPI_ENTIRE)) {
525 ASSERT(mval[i].br_startoff >= bno);
526 ASSERT(mval[i].br_blockcount <= len);
527 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
528 bno + len);
529 } else {
530 ASSERT(mval[i].br_startoff < bno + len);
531 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
532 bno);
533 }
534 ASSERT(i == 0 ||
535 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
536 mval[i].br_startoff);
537 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
538 mval[i].br_startblock != HOLESTARTBLOCK);
539 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
540 mval[i].br_state == XFS_EXT_UNWRITTEN);
541 }
542}
543
544#else
545#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
Darrick J. Wong7bf7a192017-08-31 15:11:06 -0700546#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100547#endif /* DEBUG */
548
549/*
Dave Chinner9e5987a72013-02-25 12:31:26 +1100550 * Inode fork format manipulation functions
551 */
552
553/*
Christoph Hellwigb101e332019-02-15 08:02:47 -0800554 * Convert the inode format to extent format if it currently is in btree format,
555 * but the extent list is small enough that it fits into the extent format.
556 *
557 * Since the extents are already in-core, all we have to do is give up the space
558 * for the btree root and pitch the leaf block.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100559 */
560STATIC int /* error */
561xfs_bmap_btree_to_extents(
Christoph Hellwigb101e332019-02-15 08:02:47 -0800562 struct xfs_trans *tp, /* transaction pointer */
563 struct xfs_inode *ip, /* incore inode pointer */
564 struct xfs_btree_cur *cur, /* btree cursor */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100565 int *logflagsp, /* inode logging flags */
566 int whichfork) /* data or attr fork */
567{
Darrick J. Wong732436e2022-07-09 10:56:05 -0700568 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
Christoph Hellwigb101e332019-02-15 08:02:47 -0800569 struct xfs_mount *mp = ip->i_mount;
570 struct xfs_btree_block *rblock = ifp->if_broot;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100571 struct xfs_btree_block *cblock;/* child btree block */
572 xfs_fsblock_t cbno; /* child block number */
Dave Chinnere8222612020-12-16 16:07:34 -0800573 struct xfs_buf *cbp; /* child block's buffer */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100574 int error; /* error return value */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100575 __be64 *pp; /* ptr to block address */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000576 struct xfs_owner_info oinfo;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100577
Christoph Hellwigb101e332019-02-15 08:02:47 -0800578 /* check if we actually need the extent format first: */
579 if (!xfs_bmap_wants_extents(ip, whichfork))
580 return 0;
581
582 ASSERT(cur);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700583 ASSERT(whichfork != XFS_COW_FORK);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700584 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100585 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
586 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
Darrick J. Wong411a7122024-08-30 15:37:21 -0700587 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, false) == 1);
Christoph Hellwigb101e332019-02-15 08:02:47 -0800588
Darrick J. Wong79124b32024-08-30 15:37:20 -0700589 pp = xfs_bmap_broot_ptr_addr(mp, rblock, 1, ifp->if_broot_bytes);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100590 cbno = be64_to_cpu(*pp);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100591#ifdef DEBUG
Christoph Hellwigfb0793f2024-02-22 12:40:53 -0800592 if (XFS_IS_CORRUPT(cur->bc_mp, !xfs_verify_fsbno(mp, cbno))) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -0800593 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -0800594 return -EFSCORRUPTED;
Darrick J. Wong989d5ec2024-02-22 12:32:55 -0800595 }
Dave Chinner9e5987a72013-02-25 12:31:26 +1100596#endif
Christoph Hellwig6a701eb2024-02-22 12:41:01 -0800597 error = xfs_bmap_read_buf(mp, tp, cbno, &cbp);
Darrick J. Wonga78d10f2024-02-22 12:32:09 -0800598 if (xfs_metadata_is_sick(error))
599 xfs_btree_mark_sick(cur);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100600 if (error)
601 return error;
602 cblock = XFS_BUF_TO_BLOCK(cbp);
603 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
604 return error;
Dave Chinner7dfee172023-06-05 14:48:15 +1000605
Darrick J. Wong340785c2016-08-03 11:33:42 +1000606 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
Dave Chinnerb742d7b42023-06-28 11:04:32 -0700607 error = xfs_free_extent_later(cur->bc_tp, cbno, 1, &oinfo,
Darrick J. Wong980faec2024-07-02 11:22:51 -0700608 XFS_AG_RESV_NONE, 0);
Dave Chinner7dfee172023-06-05 14:48:15 +1000609 if (error)
610 return error;
611
Christoph Hellwig6e73a542021-03-29 11:11:40 -0700612 ip->i_nblocks--;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100613 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
614 xfs_trans_binval(tp, cbp);
Darrick J. Wong6ca444cf2021-09-16 12:24:04 -0700615 if (cur->bc_levels[0].bp == cbp)
616 cur->bc_levels[0].bp = NULL;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100617 xfs_iroot_realloc(ip, -1, whichfork);
618 ASSERT(ifp->if_broot == NULL);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700619 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
Christoph Hellwigb101e332019-02-15 08:02:47 -0800620 *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100621 return 0;
622}
623
624/*
625 * Convert an extents-format file into a btree-format file.
626 * The new file will have a root block (in the inode) and a single child block.
627 */
628STATIC int /* error */
629xfs_bmap_extents_to_btree(
Brian Foster81ba8f32018-07-11 22:26:16 -0700630 struct xfs_trans *tp, /* transaction pointer */
631 struct xfs_inode *ip, /* incore inode pointer */
Brian Foster81ba8f32018-07-11 22:26:16 -0700632 struct xfs_btree_cur **curp, /* cursor returned to caller */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100633 int wasdel, /* converting a delayed alloc */
634 int *logflagsp, /* inode logging flags */
635 int whichfork) /* data or attr fork */
636{
637 struct xfs_btree_block *ablock; /* allocated (child) bt block */
Brian Foster81ba8f32018-07-11 22:26:16 -0700638 struct xfs_buf *abp; /* buffer for ablock */
639 struct xfs_alloc_arg args; /* allocation arguments */
640 struct xfs_bmbt_rec *arp; /* child record pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100641 struct xfs_btree_block *block; /* btree root block */
Brian Foster81ba8f32018-07-11 22:26:16 -0700642 struct xfs_btree_cur *cur; /* bmap btree cursor */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100643 int error; /* error return value */
Brian Foster81ba8f32018-07-11 22:26:16 -0700644 struct xfs_ifork *ifp; /* inode fork pointer */
645 struct xfs_bmbt_key *kp; /* root block key pointer */
646 struct xfs_mount *mp; /* mount structure */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100647 xfs_bmbt_ptr_t *pp; /* root block address pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700648 struct xfs_iext_cursor icur;
Christoph Hellwig906abed2017-11-03 10:34:43 -0700649 struct xfs_bmbt_irec rec;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700650 xfs_extnum_t cnt = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100651
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500652 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700653 ASSERT(whichfork != XFS_COW_FORK);
Darrick J. Wong732436e2022-07-09 10:56:05 -0700654 ifp = xfs_ifork_ptr(ip, whichfork);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700655 ASSERT(ifp->if_format == XFS_DINODE_FMT_EXTENTS);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100656
657 /*
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000658 * Make space in the inode incore. This needs to be undone if we fail
659 * to expand the root.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100660 */
661 xfs_iroot_realloc(ip, 1, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100662
663 /*
664 * Fill in the root.
665 */
666 block = ifp->if_broot;
Darrick J. Wongc87e3bf2024-02-22 12:35:16 -0800667 xfs_bmbt_init_block(ip, block, NULL, 1, 1);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100668 /*
669 * Need a cursor. Can't allocate until bb_level is filled in.
670 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100671 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Christoph Hellwige9e66df2024-02-22 12:35:13 -0800672 if (wasdel)
673 cur->bc_flags |= XFS_BTREE_BMBT_WASDEL;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100674 /*
675 * Convert to a btree with two levels, one record in root.
676 */
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700677 ifp->if_format = XFS_DINODE_FMT_BTREE;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100678 memset(&args, 0, sizeof(args));
679 args.tp = tp;
680 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000681 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner36b6ad22023-02-11 04:10:06 +1100682
Dave Chinner9e5987a72013-02-25 12:31:26 +1100683 args.minlen = args.maxlen = args.prod = 1;
684 args.wasdel = wasdel;
685 *logflagsp = 0;
Dave Chinner2a7f6d42023-02-13 09:14:53 +1100686 error = xfs_alloc_vextent_start_ag(&args,
687 XFS_INO_TO_FSB(mp, ip->i_ino));
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000688 if (error)
689 goto out_root_realloc;
Darrick J. Wong90e20562016-10-03 09:11:45 -0700690
Dave Chinner36b6ad22023-02-11 04:10:06 +1100691 /*
692 * Allocation can't fail, the space was reserved.
693 */
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800694 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
Shan Hai01239d772018-08-10 17:55:55 -0700695 error = -ENOSPC;
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000696 goto out_root_realloc;
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800697 }
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000698
Christoph Hellwig88ee2f42024-02-22 12:37:03 -0800699 cur->bc_bmap.allocated++;
Christoph Hellwig6e73a542021-03-29 11:11:40 -0700700 ip->i_nblocks++;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100701 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
Darrick J. Wongee647f82020-01-23 17:01:19 -0800702 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
703 XFS_FSB_TO_DADDR(mp, args.fsbno),
704 mp->m_bsize, 0, &abp);
705 if (error)
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000706 goto out_unreserve_dquot;
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000707
Dave Chinner9e5987a72013-02-25 12:31:26 +1100708 /*
709 * Fill in the child block.
710 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100711 ablock = XFS_BUF_TO_BLOCK(abp);
Darrick J. Wongc87e3bf2024-02-22 12:35:16 -0800712 xfs_bmbt_init_block(ip, ablock, abp, 0, 0);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500713
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700714 for_each_xfs_iext(ifp, &icur, &rec) {
Christoph Hellwig906abed2017-11-03 10:34:43 -0700715 if (isnullstartblock(rec.br_startblock))
716 continue;
Darrick J. Wong79124b32024-08-30 15:37:20 -0700717 arp = xfs_bmbt_rec_addr(mp, ablock, 1 + cnt);
Christoph Hellwig906abed2017-11-03 10:34:43 -0700718 xfs_bmbt_disk_set_all(arp, &rec);
719 cnt++;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100720 }
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700721 ASSERT(cnt == ifp->if_nextents);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100722 xfs_btree_set_numrecs(ablock, cnt);
723
724 /*
725 * Fill in the root key and pointer.
726 */
Darrick J. Wong79124b32024-08-30 15:37:20 -0700727 kp = xfs_bmbt_key_addr(mp, block, 1);
728 arp = xfs_bmbt_rec_addr(mp, ablock, 1);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100729 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
Darrick J. Wong79124b32024-08-30 15:37:20 -0700730 pp = xfs_bmbt_ptr_addr(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100731 be16_to_cpu(block->bb_level)));
732 *pp = cpu_to_be64(args.fsbno);
733
734 /*
735 * Do all this logging at the end so that
736 * the root is at the right level.
737 */
738 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
739 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
740 ASSERT(*curp == NULL);
741 *curp = cur;
742 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
743 return 0;
Shan Hai01239d772018-08-10 17:55:55 -0700744
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000745out_unreserve_dquot:
Shan Hai01239d772018-08-10 17:55:55 -0700746 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000747out_root_realloc:
Shan Hai01239d772018-08-10 17:55:55 -0700748 xfs_iroot_realloc(ip, -1, whichfork);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700749 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000750 ASSERT(ifp->if_broot == NULL);
Shan Hai01239d772018-08-10 17:55:55 -0700751 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
752
753 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100754}
755
756/*
757 * Convert a local file to an extents file.
758 * This code is out of bounds for data forks of regular files,
759 * since the file data needs to get logged so things will stay consistent.
760 * (The bmap-level manipulations are ok, though).
761 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000762void
763xfs_bmap_local_to_extents_empty(
Brian Fosteraeea4b72019-10-07 12:54:16 -0700764 struct xfs_trans *tp,
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000765 struct xfs_inode *ip,
766 int whichfork)
767{
Darrick J. Wong732436e2022-07-09 10:56:05 -0700768 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000769
Darrick J. Wong60b49842016-10-03 09:11:34 -0700770 ASSERT(whichfork != XFS_COW_FORK);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700771 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000772 ASSERT(ifp->if_bytes == 0);
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700773 ASSERT(ifp->if_nextents == 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000774
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000775 xfs_bmap_forkoff_reset(ip, whichfork);
Christoph Hellwig6e145f92023-12-20 07:34:55 +0100776 ifp->if_data = NULL;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700777 ifp->if_height = 0;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700778 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
Brian Fosteraeea4b72019-10-07 12:54:16 -0700779 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000780}
781
782
Darrick J. Wongef744be2024-04-15 14:54:58 -0700783int /* error */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100784xfs_bmap_local_to_extents(
785 xfs_trans_t *tp, /* transaction pointer */
786 xfs_inode_t *ip, /* incore inode pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100787 xfs_extlen_t total, /* total blocks needed by transaction */
788 int *logflagsp, /* inode logging flags */
789 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500790 void (*init_fn)(struct xfs_trans *tp,
791 struct xfs_buf *bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100792 struct xfs_inode *ip,
Darrick J. Wongef744be2024-04-15 14:54:58 -0700793 struct xfs_ifork *ifp, void *priv),
794 void *priv)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100795{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000796 int error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100797 int flags; /* logging flags returned */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -0700798 struct xfs_ifork *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000799 xfs_alloc_arg_t args; /* allocation arguments */
Dave Chinnere8222612020-12-16 16:07:34 -0800800 struct xfs_buf *bp; /* buffer for extent block */
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700801 struct xfs_bmbt_irec rec;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700802 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100803
804 /*
805 * We don't want to deal with the case of keeping inode data inline yet.
806 * So sending the data fork of a regular inode is invalid.
807 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100808 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Darrick J. Wong732436e2022-07-09 10:56:05 -0700809 ifp = xfs_ifork_ptr(ip, whichfork);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700810 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000811
812 if (!ifp->if_bytes) {
Brian Fosteraeea4b72019-10-07 12:54:16 -0700813 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000814 flags = XFS_ILOG_CORE;
815 goto done;
816 }
817
Dave Chinner9e5987a72013-02-25 12:31:26 +1100818 flags = 0;
819 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000820 memset(&args, 0, sizeof(args));
821 args.tp = tp;
822 args.mp = ip->i_mount;
Dave Chinner74c36a82023-02-13 09:14:53 +1100823 args.total = total;
824 args.minlen = args.maxlen = args.prod = 1;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000825 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinner2a7f6d42023-02-13 09:14:53 +1100826
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000827 /*
828 * Allocate a block. We know we need only one, since the
829 * file currently fits in an inode.
830 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000831 args.total = total;
832 args.minlen = args.maxlen = args.prod = 1;
Dave Chinner2a7f6d42023-02-13 09:14:53 +1100833 error = xfs_alloc_vextent_start_ag(&args,
834 XFS_INO_TO_FSB(args.mp, ip->i_ino));
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000835 if (error)
836 goto done;
837
838 /* Can't fail, the space was reserved. */
839 ASSERT(args.fsbno != NULLFSBLOCK);
840 ASSERT(args.len == 1);
Darrick J. Wongee647f82020-01-23 17:01:19 -0800841 error = xfs_trans_get_buf(tp, args.mp->m_ddev_targp,
842 XFS_FSB_TO_DADDR(args.mp, args.fsbno),
843 args.mp->m_bsize, 0, &bp);
844 if (error)
845 goto done;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000846
Dave Chinnerfe22d552015-01-22 09:30:06 +1100847 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100848 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100849 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100850 * The callout is responsible for logging because the remote format
851 * might differ from the local format and thus we don't know how much to
852 * log here. Note that init_fn must also set the buffer log item type
853 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100854 */
Darrick J. Wongef744be2024-04-15 14:54:58 -0700855 init_fn(tp, bp, ip, ifp, priv);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000856
Brian Fosterb7cdc662015-10-12 15:40:24 +1100857 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000858 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
Brian Fosteraeea4b72019-10-07 12:54:16 -0700859 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100860 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000861
Christoph Hellwig6e145f92023-12-20 07:34:55 +0100862 ifp->if_data = NULL;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700863 ifp->if_height = 0;
864
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700865 rec.br_startoff = 0;
866 rec.br_startblock = args.fsbno;
867 rec.br_blockcount = 1;
868 rec.br_state = XFS_EXT_NORM;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700869 xfs_iext_first(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -0700870 xfs_iext_insert(ip, &icur, &rec, 0);
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700871
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700872 ifp->if_nextents = 1;
Christoph Hellwig6e73a542021-03-29 11:11:40 -0700873 ip->i_nblocks = 1;
Dave Chinner36b6ad22023-02-11 04:10:06 +1100874 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000875 flags |= xfs_ilog_fext(whichfork);
876
Dave Chinner9e5987a72013-02-25 12:31:26 +1100877done:
878 *logflagsp = flags;
879 return error;
880}
881
882/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 * Called from xfs_bmap_add_attrfork to handle btree format files.
884 */
885STATIC int /* error */
886xfs_bmap_add_attrfork_btree(
887 xfs_trans_t *tp, /* transaction pointer */
888 xfs_inode_t *ip, /* incore inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 int *flags) /* inode logging flags */
890{
Chandan Babu Rb6785e22021-04-02 15:07:33 -0700891 struct xfs_btree_block *block = ip->i_df.if_broot;
Darrick J. Wongae127f02021-09-16 12:18:47 -0700892 struct xfs_btree_cur *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 int error; /* error return value */
894 xfs_mount_t *mp; /* file system mount struct */
895 int stat; /* newroot status */
896
897 mp = ip->i_mount;
Chandan Babu Rb6785e22021-04-02 15:07:33 -0700898
Darrick J. Wong79124b32024-08-30 15:37:20 -0700899 if (xfs_bmap_bmdr_space(block) <= xfs_inode_data_fork_size(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 *flags |= XFS_ILOG_DBROOT;
901 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100902 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700903 error = xfs_bmbt_lookup_first(cur, &stat);
904 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000906 /* must be at least one entry */
Darrick J. Wongf9e03702019-11-11 12:52:18 -0800907 if (XFS_IS_CORRUPT(mp, stat != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -0800908 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -0800909 error = -EFSCORRUPTED;
910 goto error0;
911 }
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100912 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 goto error0;
914 if (stat == 0) {
915 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000916 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
Christoph Hellwig88ee2f42024-02-22 12:37:03 -0800918 cur->bc_bmap.allocated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
920 }
921 return 0;
922error0:
923 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
924 return error;
925}
926
927/*
928 * Called from xfs_bmap_add_attrfork to handle extents format files.
929 */
930STATIC int /* error */
931xfs_bmap_add_attrfork_extents(
Brian Foster81ba8f32018-07-11 22:26:16 -0700932 struct xfs_trans *tp, /* transaction pointer */
933 struct xfs_inode *ip, /* incore inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 int *flags) /* inode logging flags */
935{
Darrick J. Wongae127f02021-09-16 12:18:47 -0700936 struct xfs_btree_cur *cur; /* bmap btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 int error; /* error return value */
938
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700939 if (ip->i_df.if_nextents * sizeof(struct xfs_bmbt_rec) <=
Darrick J. Wongc01147d2022-07-09 10:56:07 -0700940 xfs_inode_data_fork_size(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return 0;
942 cur = NULL;
Brian Foster280253d2018-07-11 22:26:29 -0700943 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
944 XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (cur) {
Christoph Hellwig88ee2f42024-02-22 12:37:03 -0800946 cur->bc_bmap.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -0700947 xfs_btree_del_cursor(cur, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949 return error;
950}
951
952/*
Dave Chinner1e823792013-02-11 15:58:13 +1100953 * Called from xfs_bmap_add_attrfork to handle local format files. Each
954 * different data fork content type needs a different callout to do the
955 * conversion. Some are basic and only require special block initialisation
956 * callouts for the data formating, others (directories) are so specialised they
957 * handle everything themselves.
958 *
959 * XXX (dgc): investigate whether directory conversion can use the generic
960 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500961 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 */
963STATIC int /* error */
964xfs_bmap_add_attrfork_local(
Brian Foster825d75cd2018-07-11 22:26:21 -0700965 struct xfs_trans *tp, /* transaction pointer */
966 struct xfs_inode *ip, /* incore inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 int *flags) /* inode logging flags */
968{
Brian Foster825d75cd2018-07-11 22:26:21 -0700969 struct xfs_da_args dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Darrick J. Wongc01147d2022-07-09 10:56:07 -0700971 if (ip->i_df.if_bytes <= xfs_inode_data_fork_size(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +1100973
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100974 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +1000976 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 dargs.dp = ip;
Dave Chinnerd6cf1302014-06-06 15:14:11 +1000978 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 dargs.whichfork = XFS_DATA_FORK;
980 dargs.trans = tp;
Darrick J. Wong9eef7722024-04-15 14:54:34 -0700981 dargs.owner = ip->i_ino;
Dave Chinner1e823792013-02-11 15:58:13 +1100982 return xfs_dir2_sf_to_block(&dargs);
983 }
984
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100985 if (S_ISLNK(VFS_I(ip)->i_mode))
Brian Foster280253d2018-07-11 22:26:29 -0700986 return xfs_bmap_local_to_extents(tp, ip, 1, flags,
Darrick J. Wongef744be2024-04-15 14:54:58 -0700987 XFS_DATA_FORK, xfs_symlink_local_to_remote,
988 NULL);
Dave Chinner1e823792013-02-11 15:58:13 +1100989
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000990 /* should only be called for types that support local format data */
991 ASSERT(0);
Darrick J. Wong1196f3f2024-02-22 12:31:51 -0800992 xfs_bmap_mark_sick(ip, XFS_ATTR_FORK);
Dave Chinner24513372014-06-25 14:58:08 +1000993 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Dave Chinnere6a688c2021-03-22 09:52:03 -0700996/*
997 * Set an inode attr fork offset based on the format of the data fork.
998 */
Christoph Hellwig5a981e42021-06-02 14:58:59 -0700999static int
Allison Henderson2f3cd802018-10-18 17:21:16 +11001000xfs_bmap_set_attrforkoff(
1001 struct xfs_inode *ip,
1002 int size,
1003 int *version)
1004{
Dave Chinner683ec9b2021-04-06 07:02:04 -07001005 int default_size = xfs_default_attroffset(ip) >> 3;
1006
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001007 switch (ip->i_df.if_format) {
Allison Henderson2f3cd802018-10-18 17:21:16 +11001008 case XFS_DINODE_FMT_DEV:
Dave Chinner683ec9b2021-04-06 07:02:04 -07001009 ip->i_forkoff = default_size;
Allison Henderson2f3cd802018-10-18 17:21:16 +11001010 break;
1011 case XFS_DINODE_FMT_LOCAL:
1012 case XFS_DINODE_FMT_EXTENTS:
1013 case XFS_DINODE_FMT_BTREE:
Christoph Hellwig7821ea32021-03-29 11:11:44 -07001014 ip->i_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1015 if (!ip->i_forkoff)
Dave Chinner683ec9b2021-04-06 07:02:04 -07001016 ip->i_forkoff = default_size;
Dave Chinner0560f312021-08-18 18:46:52 -07001017 else if (xfs_has_attr2(ip->i_mount) && version)
Allison Henderson2f3cd802018-10-18 17:21:16 +11001018 *version = 2;
1019 break;
1020 default:
1021 ASSERT(0);
1022 return -EINVAL;
1023 }
1024
1025 return 0;
1026}
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028/*
Darrick J. Wong55edcd12024-04-22 09:48:15 -07001029 * Convert inode from non-attributed to attributed. Caller must hold the
1030 * ILOCK_EXCL and the file cannot have an attr fork.
Dave Chinner9e5987a72013-02-25 12:31:26 +11001031 */
1032int /* error code */
1033xfs_bmap_add_attrfork(
Darrick J. Wong55edcd12024-04-22 09:48:15 -07001034 struct xfs_trans *tp,
1035 struct xfs_inode *ip, /* incore inode pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001036 int size, /* space new attribute needs */
1037 int rsvd) /* xact may use reserved blks */
1038{
Darrick J. Wong55edcd12024-04-22 09:48:15 -07001039 struct xfs_mount *mp = tp->t_mountp;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001040 int version = 1; /* superblock attr version */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001041 int logflags; /* logging flags */
1042 int error; /* error return value */
1043
Darrick J. Wong55edcd12024-04-22 09:48:15 -07001044 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001045 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Darrick J. Wong55edcd12024-04-22 09:48:15 -07001046 ASSERT(!xfs_inode_has_attr_fork(ip));
Dave Chinner9e5987a72013-02-25 12:31:26 +11001047
Dave Chinner9e5987a72013-02-25 12:31:26 +11001048 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Allison Henderson2f3cd802018-10-18 17:21:16 +11001049 error = xfs_bmap_set_attrforkoff(ip, size, &version);
1050 if (error)
Darrick J. Wong55edcd12024-04-22 09:48:15 -07001051 return error;
Carlos Maiolino32a2b112020-07-22 09:23:10 -07001052
Darrick J. Wong2ed5b092022-07-09 10:56:06 -07001053 xfs_ifork_init_attr(ip, XFS_DINODE_FMT_EXTENTS, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001054 logflags = 0;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001055 switch (ip->i_df.if_format) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001056 case XFS_DINODE_FMT_LOCAL:
Brian Foster825d75cd2018-07-11 22:26:21 -07001057 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001058 break;
1059 case XFS_DINODE_FMT_EXTENTS:
Brian Foster825d75cd2018-07-11 22:26:21 -07001060 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001061 break;
1062 case XFS_DINODE_FMT_BTREE:
Brian Foster825d75cd2018-07-11 22:26:21 -07001063 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001064 break;
1065 default:
1066 error = 0;
1067 break;
1068 }
1069 if (logflags)
1070 xfs_trans_log_inode(tp, ip, logflags);
1071 if (error)
Darrick J. Wong55edcd12024-04-22 09:48:15 -07001072 return error;
Dave Chinner38c26bf2021-08-18 18:46:37 -07001073 if (!xfs_has_attr(mp) ||
1074 (!xfs_has_attr2(mp) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001075 bool log_sb = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001076
1077 spin_lock(&mp->m_sb_lock);
Dave Chinner38c26bf2021-08-18 18:46:37 -07001078 if (!xfs_has_attr(mp)) {
1079 xfs_add_attr(mp);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001080 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001081 }
Dave Chinner38c26bf2021-08-18 18:46:37 -07001082 if (!xfs_has_attr2(mp) && version == 2) {
1083 xfs_add_attr2(mp);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001084 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001085 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001086 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001087 if (log_sb)
1088 xfs_log_sb(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001089 }
1090
Darrick J. Wong55edcd12024-04-22 09:48:15 -07001091 return 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001092}
1093
1094/*
1095 * Internal and external extent tree search functions.
1096 */
1097
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001098struct xfs_iread_state {
1099 struct xfs_iext_cursor icur;
1100 xfs_extnum_t loaded;
1101};
1102
Darrick J. Wong6a3bd8f2023-04-11 19:00:05 -07001103int
1104xfs_bmap_complain_bad_rec(
1105 struct xfs_inode *ip,
1106 int whichfork,
1107 xfs_failaddr_t fa,
1108 const struct xfs_bmbt_irec *irec)
1109{
1110 struct xfs_mount *mp = ip->i_mount;
1111 const char *forkname;
1112
1113 switch (whichfork) {
1114 case XFS_DATA_FORK: forkname = "data"; break;
1115 case XFS_ATTR_FORK: forkname = "attr"; break;
1116 case XFS_COW_FORK: forkname = "CoW"; break;
1117 default: forkname = "???"; break;
1118 }
1119
1120 xfs_warn(mp,
1121 "Bmap BTree record corruption in inode 0x%llx %s fork detected at %pS!",
1122 ip->i_ino, forkname, fa);
1123 xfs_warn(mp,
1124 "Offset 0x%llx, start block 0x%llx, block count 0x%llx state 0x%x",
1125 irec->br_startoff, irec->br_startblock, irec->br_blockcount,
1126 irec->br_state);
1127
1128 return -EFSCORRUPTED;
1129}
1130
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001131/* Stuff every bmbt record from this block into the incore extent map. */
1132static int
1133xfs_iread_bmbt_block(
1134 struct xfs_btree_cur *cur,
1135 int level,
1136 void *priv)
1137{
1138 struct xfs_iread_state *ir = priv;
1139 struct xfs_mount *mp = cur->bc_mp;
Dave Chinner92219c22020-03-10 17:52:53 -07001140 struct xfs_inode *ip = cur->bc_ino.ip;
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001141 struct xfs_btree_block *block;
1142 struct xfs_buf *bp;
1143 struct xfs_bmbt_rec *frp;
1144 xfs_extnum_t num_recs;
1145 xfs_extnum_t j;
Dave Chinner92219c22020-03-10 17:52:53 -07001146 int whichfork = cur->bc_ino.whichfork;
Darrick J. Wong732436e2022-07-09 10:56:05 -07001147 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001148
1149 block = xfs_btree_get_block(cur, level, &bp);
1150
1151 /* Abort if we find more records than nextents. */
1152 num_recs = xfs_btree_get_numrecs(block);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001153 if (unlikely(ir->loaded + num_recs > ifp->if_nextents)) {
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001154 xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).",
1155 (unsigned long long)ip->i_ino);
1156 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block,
1157 sizeof(*block), __this_address);
Darrick J. Wong1196f3f2024-02-22 12:31:51 -08001158 xfs_bmap_mark_sick(ip, whichfork);
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001159 return -EFSCORRUPTED;
1160 }
1161
1162 /* Copy records into the incore cache. */
Darrick J. Wong79124b32024-08-30 15:37:20 -07001163 frp = xfs_bmbt_rec_addr(mp, block, 1);
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001164 for (j = 0; j < num_recs; j++, frp++, ir->loaded++) {
1165 struct xfs_bmbt_irec new;
1166 xfs_failaddr_t fa;
1167
1168 xfs_bmbt_disk_get_all(frp, &new);
1169 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1170 if (fa) {
1171 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1172 "xfs_iread_extents(2)", frp,
1173 sizeof(*frp), fa);
Darrick J. Wong1196f3f2024-02-22 12:31:51 -08001174 xfs_bmap_mark_sick(ip, whichfork);
Darrick J. Wong6a3bd8f2023-04-11 19:00:05 -07001175 return xfs_bmap_complain_bad_rec(ip, whichfork, fa,
1176 &new);
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001177 }
1178 xfs_iext_insert(ip, &ir->icur, &new,
1179 xfs_bmap_fork_to_state(whichfork));
1180 trace_xfs_read_extent(ip, &ir->icur,
1181 xfs_bmap_fork_to_state(whichfork), _THIS_IP_);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001182 xfs_iext_next(ifp, &ir->icur);
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001183 }
1184
1185 return 0;
1186}
1187
Dave Chinner9e5987a72013-02-25 12:31:26 +11001188/*
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001189 * Read in extents from a btree-format inode.
Dave Chinner9e5987a72013-02-25 12:31:26 +11001190 */
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001191int
1192xfs_iread_extents(
1193 struct xfs_trans *tp,
1194 struct xfs_inode *ip,
1195 int whichfork)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001196{
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001197 struct xfs_iread_state ir;
Darrick J. Wong732436e2022-07-09 10:56:05 -07001198 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001199 struct xfs_mount *mp = ip->i_mount;
1200 struct xfs_btree_cur *cur;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001201 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001202
Christoph Hellwigb2197a32021-04-13 11:15:12 -07001203 if (!xfs_need_iread_extents(ifp))
Christoph Hellwig862a8042021-04-13 11:15:09 -07001204 return 0;
1205
Matthew Wilcox (Oracle)3fed24f2024-02-19 15:41:12 +00001206 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001207
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001208 ir.loaded = 0;
1209 xfs_iext_first(ifp, &ir.icur);
1210 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1211 error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block,
1212 XFS_BTREE_VISIT_RECORDS, &ir);
1213 xfs_btree_del_cursor(cur, error);
1214 if (error)
1215 goto out;
1216
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001217 if (XFS_IS_CORRUPT(mp, ir.loaded != ifp->if_nextents)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08001218 xfs_bmap_mark_sick(ip, whichfork);
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001219 error = -EFSCORRUPTED;
1220 goto out;
1221 }
1222 ASSERT(ir.loaded == xfs_iext_count(ifp));
Darrick J. Wongc95356c2023-04-12 15:49:10 +10001223 /*
1224 * Use release semantics so that we can use acquire semantics in
1225 * xfs_need_iread_extents and be guaranteed to see a valid mapping tree
1226 * after that load.
1227 */
1228 smp_store_release(&ifp->if_needextents, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001229 return 0;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001230out:
Darrick J. Wong1196f3f2024-02-22 12:31:51 -08001231 if (xfs_metadata_is_sick(error))
1232 xfs_bmap_mark_sick(ip, whichfork);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001233 xfs_iext_destroy(ifp);
1234 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001235}
1236
Dave Chinner9e5987a72013-02-25 12:31:26 +11001237/*
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001238 * Returns the relative block number of the first unused block(s) in the given
1239 * fork with at least "len" logically contiguous blocks free. This is the
1240 * lowest-address hole if the fork has holes, else the first block past the end
1241 * of fork. Return 0 if the fork is currently local (in-inode).
Dave Chinner9e5987a72013-02-25 12:31:26 +11001242 */
1243int /* error */
1244xfs_bmap_first_unused(
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001245 struct xfs_trans *tp, /* transaction pointer */
1246 struct xfs_inode *ip, /* incore inode */
1247 xfs_extlen_t len, /* size of hole to find */
1248 xfs_fileoff_t *first_unused, /* unused block */
1249 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001250{
Darrick J. Wong732436e2022-07-09 10:56:05 -07001251 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001252 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001253 struct xfs_iext_cursor icur;
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001254 xfs_fileoff_t lastaddr = 0;
1255 xfs_fileoff_t lowest, max;
1256 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001257
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001258 if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001259 *first_unused = 0;
1260 return 0;
1261 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001262
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001263 ASSERT(xfs_ifork_has_extents(ifp));
1264
Christoph Hellwig862a8042021-04-13 11:15:09 -07001265 error = xfs_iread_extents(tp, ip, whichfork);
1266 if (error)
1267 return error;
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001268
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001269 lowest = max = *first_unused;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001270 for_each_xfs_iext(ifp, &icur, &got) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001271 /*
1272 * See if the hole before this extent will work.
1273 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001274 if (got.br_startoff >= lowest + len &&
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001275 got.br_startoff - max >= len)
1276 break;
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001277 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001278 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1279 }
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001280
Dave Chinner9e5987a72013-02-25 12:31:26 +11001281 *first_unused = max;
1282 return 0;
1283}
1284
1285/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001286 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a72013-02-25 12:31:26 +11001287 * last_block (input value) in the file.
1288 * This is not based on i_size, it is based on the extent records.
1289 * Returns 0 for local files, as they do not have extent records.
1290 */
1291int /* error */
1292xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001293 struct xfs_trans *tp, /* transaction pointer */
1294 struct xfs_inode *ip, /* incore inode */
1295 xfs_fileoff_t *last_block, /* last block */
1296 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001297{
Darrick J. Wong732436e2022-07-09 10:56:05 -07001298 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
Christoph Hellwig86685f72016-11-24 11:39:38 +11001299 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001300 struct xfs_iext_cursor icur;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001301 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001302
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001303 switch (ifp->if_format) {
Christoph Hellwig86685f72016-11-24 11:39:38 +11001304 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001305 *last_block = 0;
1306 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001307 case XFS_DINODE_FMT_BTREE:
1308 case XFS_DINODE_FMT_EXTENTS:
1309 break;
1310 default:
Darrick J. Wonga5155b82019-11-02 09:40:53 -07001311 ASSERT(0);
Darrick J. Wong1196f3f2024-02-22 12:31:51 -08001312 xfs_bmap_mark_sick(ip, whichfork);
Darrick J. Wongc2414ad2019-10-28 16:12:34 -07001313 return -EFSCORRUPTED;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001314 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001315
Christoph Hellwig862a8042021-04-13 11:15:09 -07001316 error = xfs_iread_extents(tp, ip, whichfork);
1317 if (error)
1318 return error;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001319
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001320 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
Christoph Hellwigdc560152017-10-23 16:32:39 -07001321 *last_block = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001322 return 0;
1323}
1324
Dave Chinner68988112013-08-12 20:49:42 +10001325int
Dave Chinner9e5987a72013-02-25 12:31:26 +11001326xfs_bmap_last_extent(
1327 struct xfs_trans *tp,
1328 struct xfs_inode *ip,
1329 int whichfork,
1330 struct xfs_bmbt_irec *rec,
1331 int *is_empty)
1332{
Darrick J. Wong732436e2022-07-09 10:56:05 -07001333 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001334 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001335 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001336
Christoph Hellwig862a8042021-04-13 11:15:09 -07001337 error = xfs_iread_extents(tp, ip, whichfork);
1338 if (error)
1339 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001340
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001341 xfs_iext_last(ifp, &icur);
1342 if (!xfs_iext_get_extent(ifp, &icur, rec))
Dave Chinner9e5987a72013-02-25 12:31:26 +11001343 *is_empty = 1;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001344 else
1345 *is_empty = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001346 return 0;
1347}
1348
1349/*
1350 * Check the last inode extent to determine whether this allocation will result
1351 * in blocks being allocated at the end of the file. When we allocate new data
1352 * blocks at the end of the file which do not start at the previous data block,
1353 * we will try to align the new blocks at stripe unit boundaries.
1354 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001355 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a72013-02-25 12:31:26 +11001356 * at, or past the EOF.
1357 */
1358STATIC int
1359xfs_bmap_isaeof(
1360 struct xfs_bmalloca *bma,
1361 int whichfork)
1362{
1363 struct xfs_bmbt_irec rec;
1364 int is_empty;
1365 int error;
1366
Thomas Meyer749f24f2017-10-09 11:38:54 -07001367 bma->aeof = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001368 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1369 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001370 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001371 return error;
1372
Dave Chinner6e708bc2013-11-22 10:41:16 +11001373 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001374 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001375 return 0;
1376 }
1377
Dave Chinner9e5987a72013-02-25 12:31:26 +11001378 /*
1379 * Check if we are allocation or past the last extent, or at least into
1380 * the last delayed allocated extent.
1381 */
1382 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1383 (bma->offset >= rec.br_startoff &&
1384 isnullstartblock(rec.br_startblock));
1385 return 0;
1386}
1387
1388/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001389 * Returns the file-relative block number of the first block past eof in
1390 * the file. This is not based on i_size, it is based on the extent records.
1391 * Returns 0 for local files, as they do not have extent records.
1392 */
1393int
1394xfs_bmap_last_offset(
Dave Chinner9e5987a72013-02-25 12:31:26 +11001395 struct xfs_inode *ip,
1396 xfs_fileoff_t *last_block,
1397 int whichfork)
1398{
Darrick J. Wong732436e2022-07-09 10:56:05 -07001399 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001400 struct xfs_bmbt_irec rec;
1401 int is_empty;
1402 int error;
1403
1404 *last_block = 0;
1405
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001406 if (ifp->if_format == XFS_DINODE_FMT_LOCAL)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001407 return 0;
1408
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08001409 if (XFS_IS_CORRUPT(ip->i_mount, !xfs_ifork_has_extents(ifp))) {
1410 xfs_bmap_mark_sick(ip, whichfork);
Darrick J. Wongc2414ad2019-10-28 16:12:34 -07001411 return -EFSCORRUPTED;
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08001412 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001413
1414 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1415 if (error || is_empty)
1416 return error;
1417
1418 *last_block = rec.br_startoff + rec.br_blockcount;
1419 return 0;
1420}
1421
1422/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001423 * Extent tree manipulation functions used during allocation.
1424 */
1425
1426/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001427 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 */
1429STATIC int /* error */
1430xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001431 struct xfs_bmalloca *bma,
1432 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001434 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong732436e2022-07-09 10:56:05 -07001435 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001436 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 int i; /* temp state */
1439 xfs_fileoff_t new_endoff; /* end offset of new entry */
1440 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1441 /* left is 0, right is 1, prev is 2 */
1442 int rval=0; /* return value (logging flags) */
Dave Chinner0e5b8e42022-04-21 10:46:01 +10001443 uint32_t state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001444 xfs_filblks_t da_new; /* new count del alloc blocks used */
1445 xfs_filblks_t da_old; /* old count del alloc blocks used */
1446 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 int tmp_rval; /* partial logging flags */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001448 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Darrick J. Wong60b49842016-10-03 09:11:34 -07001450 ASSERT(whichfork != XFS_ATTR_FORK);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001451 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwige9e66df2024-02-22 12:35:13 -08001452 ASSERT(!bma->cur || (bma->cur->bc_flags & XFS_BTREE_BMBT_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001453
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001454 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456#define LEFT r[0]
1457#define RIGHT r[1]
1458#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 /*
1461 * Set up a bunch of variables to make the tests simpler.
1462 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001463 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001465 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 ASSERT(PREV.br_startoff <= new->br_startoff);
1467 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001468
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001469 da_old = startblockval(PREV.br_startblock);
1470 da_new = 0;
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 /*
1473 * Set flags determining what part of the previous delayed allocation
1474 * extent is being replaced by a real allocation.
1475 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001476 if (PREV.br_startoff == new->br_startoff)
1477 state |= BMAP_LEFT_FILLING;
1478 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1479 state |= BMAP_RIGHT_FILLING;
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 /*
1482 * Check and set flags if this segment has a left neighbor.
1483 * Don't set contiguous if the combined extent would be too large.
1484 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001485 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001486 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001487 if (isnullstartblock(LEFT.br_startblock))
1488 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001490
1491 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1492 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1493 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1494 LEFT.br_state == new->br_state &&
Chandan Babu R95f0b952021-08-09 12:05:22 +05301495 LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001496 state |= BMAP_LEFT_CONTIG;
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 /*
1499 * Check and set flags if this segment has a right neighbor.
1500 * Don't set contiguous if the combined extent would be too large.
1501 * Also check for all-three-contiguous being too large.
1502 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001503 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001504 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001505 if (isnullstartblock(RIGHT.br_startblock))
1506 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001508
1509 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1510 new_endoff == RIGHT.br_startoff &&
1511 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1512 new->br_state == RIGHT.br_state &&
Chandan Babu R95f0b952021-08-09 12:05:22 +05301513 new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001514 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1515 BMAP_RIGHT_FILLING)) !=
1516 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1517 BMAP_RIGHT_FILLING) ||
1518 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
Chandan Babu R95f0b952021-08-09 12:05:22 +05301519 <= XFS_MAX_BMBT_EXTLEN))
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001520 state |= BMAP_RIGHT_CONTIG;
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 error = 0;
1523 /*
1524 * Switch out based on the FILLING and CONTIG state bits.
1525 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001526 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1527 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1528 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1529 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 /*
1531 * Filling in all of a previously delayed allocation extent.
1532 * The left and right neighbors are both contiguous with new.
1533 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001534 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001535
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001536 xfs_iext_remove(bma->ip, &bma->icur, state);
1537 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001538 xfs_iext_prev(ifp, &bma->icur);
1539 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001540 ifp->if_nextents--;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001541
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001542 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1544 else {
1545 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001546 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001547 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001549 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08001550 xfs_btree_mark_sick(bma->cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001551 error = -EFSCORRUPTED;
1552 goto done;
1553 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001554 error = xfs_btree_delete(bma->cur, &i);
1555 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001557 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08001558 xfs_btree_mark_sick(bma->cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001559 error = -EFSCORRUPTED;
1560 goto done;
1561 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001562 error = xfs_btree_decrement(bma->cur, 0, &i);
1563 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001565 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08001566 xfs_btree_mark_sick(bma->cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001567 error = -EFSCORRUPTED;
1568 goto done;
1569 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001570 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001571 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 goto done;
1573 }
Christoph Hellwigd69bee62024-04-29 08:15:27 +02001574 ASSERT(da_new <= da_old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 break;
1576
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001577 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 /*
1579 * Filling in all of a previously delayed allocation extent.
1580 * The left neighbor is contiguous, the right is not.
1581 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001582 old = LEFT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001583 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001584
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001585 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001586 xfs_iext_prev(ifp, &bma->icur);
1587 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001588
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001589 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 rval = XFS_ILOG_DEXT;
1591 else {
1592 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001593 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001594 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001596 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08001597 xfs_btree_mark_sick(bma->cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001598 error = -EFSCORRUPTED;
1599 goto done;
1600 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001601 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001602 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 goto done;
1604 }
Christoph Hellwigd69bee62024-04-29 08:15:27 +02001605 ASSERT(da_new <= da_old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 break;
1607
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001608 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 /*
1610 * Filling in all of a previously delayed allocation extent.
Dave Chinner9230a0b2018-11-19 22:50:08 -08001611 * The right neighbor is contiguous, the left is not. Take care
1612 * with delay -> unwritten extent allocation here because the
1613 * delalloc record we are overwriting is always written.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001615 PREV.br_startblock = new->br_startblock;
1616 PREV.br_blockcount += RIGHT.br_blockcount;
Dave Chinner9230a0b2018-11-19 22:50:08 -08001617 PREV.br_state = new->br_state;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001618
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001619 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001620 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001621 xfs_iext_prev(ifp, &bma->icur);
1622 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001623
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001624 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 rval = XFS_ILOG_DEXT;
1626 else {
1627 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001628 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001629 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001631 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08001632 xfs_btree_mark_sick(bma->cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001633 error = -EFSCORRUPTED;
1634 goto done;
1635 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001636 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001637 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 goto done;
1639 }
Christoph Hellwigd69bee62024-04-29 08:15:27 +02001640 ASSERT(da_new <= da_old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 break;
1642
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001643 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 /*
1645 * Filling in all of a previously delayed allocation extent.
1646 * Neither the left nor right neighbors are contiguous with
1647 * the new one.
1648 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001649 PREV.br_startblock = new->br_startblock;
1650 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001651 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001652 ifp->if_nextents++;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001653
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001654 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1656 else {
1657 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001658 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001659 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001661 if (XFS_IS_CORRUPT(mp, i != 0)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08001662 xfs_btree_mark_sick(bma->cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001663 error = -EFSCORRUPTED;
1664 goto done;
1665 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001666 error = xfs_btree_insert(bma->cur, &i);
1667 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001669 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08001670 xfs_btree_mark_sick(bma->cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001671 error = -EFSCORRUPTED;
1672 goto done;
1673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
Christoph Hellwigd69bee62024-04-29 08:15:27 +02001675 ASSERT(da_new <= da_old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 break;
1677
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001678 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 /*
1680 * Filling in the first part of a previous delayed allocation.
1681 * The left neighbor is contiguous.
1682 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001683 old = LEFT;
1684 temp = PREV.br_blockcount - new->br_blockcount;
1685 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1686 startblockval(PREV.br_startblock));
1687
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001688 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001689
Christoph Hellwigbf999712017-11-03 10:34:38 -07001690 PREV.br_blockcount = temp;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001691 PREV.br_startoff += new->br_blockcount;
1692 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwig0d045542017-11-03 10:34:39 -07001693
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001694 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1695 xfs_iext_prev(ifp, &bma->icur);
1696 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001697
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001698 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 rval = XFS_ILOG_DEXT;
1700 else {
1701 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001702 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001703 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001705 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08001706 xfs_btree_mark_sick(bma->cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001707 error = -EFSCORRUPTED;
1708 goto done;
1709 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001710 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001711 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 goto done;
1713 }
Christoph Hellwigd69bee62024-04-29 08:15:27 +02001714 ASSERT(da_new <= da_old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 break;
1716
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001717 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 /*
1719 * Filling in the first part of a previous delayed allocation.
1720 * The left neighbor is not contiguous.
1721 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001722 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001723 ifp->if_nextents++;
1724
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001725 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1727 else {
1728 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001729 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001730 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001732 if (XFS_IS_CORRUPT(mp, i != 0)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08001733 xfs_btree_mark_sick(bma->cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001734 error = -EFSCORRUPTED;
1735 goto done;
1736 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001737 error = xfs_btree_insert(bma->cur, &i);
1738 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001740 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08001741 xfs_btree_mark_sick(bma->cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001742 error = -EFSCORRUPTED;
1743 goto done;
1744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001746
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001747 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001748 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001749 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 rval |= tmp_rval;
1751 if (error)
1752 goto done;
1753 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001754
1755 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001756 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001757 startblockval(PREV.br_startblock) -
Christoph Hellwig88ee2f42024-02-22 12:37:03 -08001758 (bma->cur ? bma->cur->bc_bmap.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001759
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001760 PREV.br_startoff = new_endoff;
1761 PREV.br_blockcount = temp;
1762 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001763 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001764 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001765 xfs_iext_prev(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 break;
1767
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001768 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 /*
1770 * Filling in the last part of a previous delayed allocation.
1771 * The right neighbor is contiguous with the new allocation.
1772 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001773 old = RIGHT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001774 RIGHT.br_startoff = new->br_startoff;
1775 RIGHT.br_startblock = new->br_startblock;
1776 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001777
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001778 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 rval = XFS_ILOG_DEXT;
1780 else {
1781 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001782 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001783 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001785 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08001786 xfs_btree_mark_sick(bma->cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001787 error = -EFSCORRUPTED;
1788 goto done;
1789 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001790 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001791 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 goto done;
1793 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001794
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001795 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001796 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001797 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001798
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001799 PREV.br_blockcount = temp;
1800 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001801
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001802 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1803 xfs_iext_next(ifp, &bma->icur);
1804 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
Christoph Hellwigd69bee62024-04-29 08:15:27 +02001805 ASSERT(da_new <= da_old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 break;
1807
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001808 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 /*
1810 * Filling in the last part of a previous delayed allocation.
1811 * The right neighbor is not contiguous.
1812 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001813 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001814 ifp->if_nextents++;
1815
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001816 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1818 else {
1819 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001820 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001821 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001823 if (XFS_IS_CORRUPT(mp, i != 0)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08001824 xfs_btree_mark_sick(bma->cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001825 error = -EFSCORRUPTED;
1826 goto done;
1827 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001828 error = xfs_btree_insert(bma->cur, &i);
1829 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001831 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08001832 xfs_btree_mark_sick(bma->cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001833 error = -EFSCORRUPTED;
1834 goto done;
1835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001837
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001838 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001839 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001840 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 rval |= tmp_rval;
1842 if (error)
1843 goto done;
1844 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001845
1846 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001847 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001848 startblockval(PREV.br_startblock) -
Christoph Hellwig88ee2f42024-02-22 12:37:03 -08001849 (bma->cur ? bma->cur->bc_bmap.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001850
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001851 PREV.br_startblock = nullstartblock(da_new);
1852 PREV.br_blockcount = temp;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001853 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001854 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwigd69bee62024-04-29 08:15:27 +02001855 ASSERT(da_new <= da_old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 break;
1857
1858 case 0:
1859 /*
1860 * Filling in the middle part of a previous delayed allocation.
1861 * Contiguity is impossible here.
1862 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001863 *
1864 * We start with a delayed allocation:
1865 *
1866 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1867 * PREV @ idx
1868 *
1869 * and we are allocating:
1870 * +rrrrrrrrrrrrrrrrr+
1871 * new
1872 *
1873 * and we set it up for insertion as:
1874 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1875 * new
1876 * PREV @ idx LEFT RIGHT
1877 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001879 old = PREV;
1880
1881 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001882 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001883
1884 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001885 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001886 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001887 RIGHT.br_blockcount =
1888 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1889 RIGHT.br_startblock =
1890 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1891 RIGHT.br_blockcount));
1892
1893 /* truncate PREV */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001894 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1895 PREV.br_startblock =
1896 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1897 PREV.br_blockcount));
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001898 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001899
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001900 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001901 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1902 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001903 ifp->if_nextents++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001904
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001905 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1907 else {
1908 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001909 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001910 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001912 if (XFS_IS_CORRUPT(mp, i != 0)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08001913 xfs_btree_mark_sick(bma->cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001914 error = -EFSCORRUPTED;
1915 goto done;
1916 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001917 error = xfs_btree_insert(bma->cur, &i);
1918 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001920 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08001921 xfs_btree_mark_sick(bma->cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001922 error = -EFSCORRUPTED;
1923 goto done;
1924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001926
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001927 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001928 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001929 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 rval |= tmp_rval;
1931 if (error)
1932 goto done;
1933 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001934
1935 da_new = startblockval(PREV.br_startblock) +
1936 startblockval(RIGHT.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 break;
1938
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001939 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1940 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1941 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1942 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1943 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1944 case BMAP_LEFT_CONTIG:
1945 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 /*
1947 * These cases are all impossible.
1948 */
1949 ASSERT(0);
1950 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001951
Darrick J. Wong95eb3082018-05-09 10:02:32 -07001952 /* add reverse mapping unless caller opted out */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07001953 if (!(bma->flags & XFS_BMAPI_NORMAP))
1954 xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10001955
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001956 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001957 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001958 int tmp_logflags; /* partial log flag return val */
1959
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001960 ASSERT(bma->cur == NULL);
1961 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001962 &bma->cur, da_old > 0, &tmp_logflags,
1963 whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001964 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001965 if (error)
1966 goto done;
1967 }
1968
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07001969 if (da_new != da_old)
Christoph Hellwig7099bd02024-04-22 13:20:15 +02001970 xfs_mod_delalloc(bma->ip, 0, (int64_t)da_new - da_old);
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07001971
Christoph Hellwigca1862b2017-10-17 14:16:25 -07001972 if (bma->cur) {
Christoph Hellwig88ee2f42024-02-22 12:37:03 -08001973 da_new += bma->cur->bc_bmap.allocated;
1974 bma->cur->bc_bmap.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001975 }
1976
Christoph Hellwigca1862b2017-10-17 14:16:25 -07001977 /* adjust for changes in reserved delayed indirect blocks */
Christoph Hellwigd69bee62024-04-29 08:15:27 +02001978 if (da_new < da_old)
Christoph Hellwigf30f6562024-04-22 13:20:12 +02001979 xfs_add_fdblocks(mp, da_old - da_new);
Christoph Hellwigd69bee62024-04-29 08:15:27 +02001980 else if (da_new > da_old)
1981 error = xfs_dec_fdblocks(mp, da_new - da_old, true);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001982
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001983 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07001985 if (whichfork != XFS_COW_FORK)
1986 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 return error;
1988#undef LEFT
1989#undef RIGHT
1990#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
1992
1993/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001994 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 */
Christoph Hellwig26b91c72019-02-18 09:38:48 -08001996int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001998 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002000 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002001 struct xfs_iext_cursor *icur,
Darrick J. Wongae127f02021-09-16 12:18:47 -07002002 struct xfs_btree_cur **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002003 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002004 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
Darrick J. Wongae127f02021-09-16 12:18:47 -07002006 struct xfs_btree_cur *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 int i; /* temp state */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07002009 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2012 /* left is 0, right is 1, prev is 2 */
2013 int rval=0; /* return value (logging flags) */
Dave Chinner0e5b8e42022-04-21 10:46:01 +10002014 uint32_t state = xfs_bmap_fork_to_state(whichfork);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002015 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002016 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002018 *logflagsp = 0;
2019
2020 cur = *curp;
Darrick J. Wong732436e2022-07-09 10:56:05 -07002021 ifp = xfs_ifork_ptr(ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002022
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002023 ASSERT(!isnullstartblock(new->br_startblock));
2024
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002025 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027#define LEFT r[0]
2028#define RIGHT r[1]
2029#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 /*
2032 * Set up a bunch of variables to make the tests simpler.
2033 */
2034 error = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002035 xfs_iext_get_extent(ifp, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002036 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 new_endoff = new->br_startoff + new->br_blockcount;
2038 ASSERT(PREV.br_startoff <= new->br_startoff);
2039 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 /*
2042 * Set flags determining what part of the previous oldext allocation
2043 * extent is being replaced by a newext allocation.
2044 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002045 if (PREV.br_startoff == new->br_startoff)
2046 state |= BMAP_LEFT_FILLING;
2047 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2048 state |= BMAP_RIGHT_FILLING;
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 /*
2051 * Check and set flags if this segment has a left neighbor.
2052 * Don't set contiguous if the combined extent would be too large.
2053 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002054 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002055 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002056 if (isnullstartblock(LEFT.br_startblock))
2057 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002059
2060 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2061 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2062 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002063 LEFT.br_state == new->br_state &&
Chandan Babu R95f0b952021-08-09 12:05:22 +05302064 LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002065 state |= BMAP_LEFT_CONTIG;
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 /*
2068 * Check and set flags if this segment has a right neighbor.
2069 * Don't set contiguous if the combined extent would be too large.
2070 * Also check for all-three-contiguous being too large.
2071 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002072 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002073 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002074 if (isnullstartblock(RIGHT.br_startblock))
2075 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002077
2078 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2079 new_endoff == RIGHT.br_startoff &&
2080 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002081 new->br_state == RIGHT.br_state &&
Chandan Babu R95f0b952021-08-09 12:05:22 +05302082 new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002083 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2084 BMAP_RIGHT_FILLING)) !=
2085 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2086 BMAP_RIGHT_FILLING) ||
2087 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
Chandan Babu R95f0b952021-08-09 12:05:22 +05302088 <= XFS_MAX_BMBT_EXTLEN))
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002089 state |= BMAP_RIGHT_CONTIG;
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 /*
2092 * Switch out based on the FILLING and CONTIG state bits.
2093 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002094 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2095 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2096 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2097 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 /*
2099 * Setting all of a previous oldext extent to newext.
2100 * The left and right neighbors are both contiguous with new.
2101 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002102 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002103
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002104 xfs_iext_remove(ip, icur, state);
2105 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002106 xfs_iext_prev(ifp, icur);
2107 xfs_iext_update_extent(ip, state, icur, &LEFT);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002108 ifp->if_nextents -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (cur == NULL)
2110 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2111 else {
2112 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002113 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2114 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002116 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002117 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002118 error = -EFSCORRUPTED;
2119 goto done;
2120 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002121 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002123 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002124 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002125 error = -EFSCORRUPTED;
2126 goto done;
2127 }
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002128 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002130 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002131 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002132 error = -EFSCORRUPTED;
2133 goto done;
2134 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002135 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002137 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002138 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002139 error = -EFSCORRUPTED;
2140 goto done;
2141 }
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002142 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002144 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002145 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002146 error = -EFSCORRUPTED;
2147 goto done;
2148 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002149 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002150 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 goto done;
2152 }
2153 break;
2154
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002155 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 /*
2157 * Setting all of a previous oldext extent to newext.
2158 * The left neighbor is contiguous, the right is not.
2159 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002160 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002161
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002162 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002163 xfs_iext_prev(ifp, icur);
2164 xfs_iext_update_extent(ip, state, icur, &LEFT);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002165 ifp->if_nextents--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if (cur == NULL)
2167 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2168 else {
2169 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002170 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2171 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002173 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002174 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002175 error = -EFSCORRUPTED;
2176 goto done;
2177 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002178 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002180 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002181 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002182 error = -EFSCORRUPTED;
2183 goto done;
2184 }
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002185 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002187 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002188 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002189 error = -EFSCORRUPTED;
2190 goto done;
2191 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002192 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002193 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 goto done;
2195 }
2196 break;
2197
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002198 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 /*
2200 * Setting all of a previous oldext extent to newext.
2201 * The right neighbor is contiguous, the left is not.
2202 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002203 PREV.br_blockcount += RIGHT.br_blockcount;
2204 PREV.br_state = new->br_state;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002205
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002206 xfs_iext_next(ifp, icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002207 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002208 xfs_iext_prev(ifp, icur);
2209 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002210 ifp->if_nextents--;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 if (cur == NULL)
2213 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2214 else {
2215 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002216 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2217 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002219 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002220 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002221 error = -EFSCORRUPTED;
2222 goto done;
2223 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002224 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002226 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002227 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002228 error = -EFSCORRUPTED;
2229 goto done;
2230 }
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002231 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002233 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002234 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002235 error = -EFSCORRUPTED;
2236 goto done;
2237 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002238 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002239 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 goto done;
2241 }
2242 break;
2243
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002244 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 /*
2246 * Setting all of a previous oldext extent to newext.
2247 * Neither the left nor right neighbors are contiguous with
2248 * the new one.
2249 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002250 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002251 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 if (cur == NULL)
2254 rval = XFS_ILOG_DEXT;
2255 else {
2256 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002257 error = xfs_bmbt_lookup_eq(cur, new, &i);
2258 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002260 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002261 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002262 error = -EFSCORRUPTED;
2263 goto done;
2264 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002265 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002266 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 goto done;
2268 }
2269 break;
2270
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002271 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 /*
2273 * Setting the first part of a previous oldext extent to newext.
2274 * The left neighbor is contiguous.
2275 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002276 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002277
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002278 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002279 PREV.br_startoff += new->br_blockcount;
2280 PREV.br_startblock += new->br_blockcount;
2281 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002282
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002283 xfs_iext_update_extent(ip, state, icur, &PREV);
2284 xfs_iext_prev(ifp, icur);
2285 xfs_iext_update_extent(ip, state, icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 if (cur == NULL)
2288 rval = XFS_ILOG_DEXT;
2289 else {
2290 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002291 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002292 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002294 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002295 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002296 error = -EFSCORRUPTED;
2297 goto done;
2298 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002299 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002300 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002302 error = xfs_btree_decrement(cur, 0, &i);
2303 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002305 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002306 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 goto done;
2308 }
2309 break;
2310
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002311 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 /*
2313 * Setting the first part of a previous oldext extent to newext.
2314 * The left neighbor is not contiguous.
2315 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002316 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002317 PREV.br_startoff += new->br_blockcount;
2318 PREV.br_startblock += new->br_blockcount;
2319 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002320
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002321 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002322 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002323 ifp->if_nextents++;
2324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 if (cur == NULL)
2326 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2327 else {
2328 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002329 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002330 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002332 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002333 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002334 error = -EFSCORRUPTED;
2335 goto done;
2336 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002337 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002338 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 goto done;
2340 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002341 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002343 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002344 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002345 error = -EFSCORRUPTED;
2346 goto done;
2347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
2349 break;
2350
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002351 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 /*
2353 * Setting the last part of a previous oldext extent to newext.
2354 * The right neighbor is contiguous with the new allocation.
2355 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002356 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002357 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002358
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002359 RIGHT.br_startoff = new->br_startoff;
2360 RIGHT.br_startblock = new->br_startblock;
2361 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002362
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002363 xfs_iext_update_extent(ip, state, icur, &PREV);
2364 xfs_iext_next(ifp, icur);
2365 xfs_iext_update_extent(ip, state, icur, &RIGHT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (cur == NULL)
2368 rval = XFS_ILOG_DEXT;
2369 else {
2370 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002371 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002372 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002374 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002375 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002376 error = -EFSCORRUPTED;
2377 goto done;
2378 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002379 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002380 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002382 error = xfs_btree_increment(cur, 0, &i);
2383 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002385 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002386 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 goto done;
2388 }
2389 break;
2390
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002391 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 /*
2393 * Setting the last part of a previous oldext extent to newext.
2394 * The right neighbor is not contiguous.
2395 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002396 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002397 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002398
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002399 xfs_iext_update_extent(ip, state, icur, &PREV);
2400 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002401 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002402 ifp->if_nextents++;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002403
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if (cur == NULL)
2405 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2406 else {
2407 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002408 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002409 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002411 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002412 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002413 error = -EFSCORRUPTED;
2414 goto done;
2415 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002416 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002417 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002419 error = xfs_bmbt_lookup_eq(cur, new, &i);
2420 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002422 if (XFS_IS_CORRUPT(mp, i != 0)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002423 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002424 error = -EFSCORRUPTED;
2425 goto done;
2426 }
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002427 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002429 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002430 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002431 error = -EFSCORRUPTED;
2432 goto done;
2433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 }
2435 break;
2436
2437 case 0:
2438 /*
2439 * Setting the middle part of a previous oldext extent to
2440 * newext. Contiguity is impossible here.
2441 * One extent becomes three extents.
2442 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002443 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002444 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 r[0] = *new;
2447 r[1].br_startoff = new_endoff;
2448 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002449 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002451 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002452
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002453 xfs_iext_update_extent(ip, state, icur, &PREV);
2454 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002455 xfs_iext_insert(ip, icur, &r[1], state);
2456 xfs_iext_insert(ip, icur, &r[0], state);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002457 ifp->if_nextents += 2;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002458
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 if (cur == NULL)
2460 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2461 else {
2462 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002463 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002464 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002466 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002467 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002468 error = -EFSCORRUPTED;
2469 goto done;
2470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002472 error = xfs_bmbt_update(cur, &r[1]);
2473 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 goto done;
2475 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002477 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002479 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002480 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002481 error = -EFSCORRUPTED;
2482 goto done;
2483 }
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002484 /*
2485 * Reset the cursor to the position of the new extent
2486 * we are about to insert as we can't trust it after
2487 * the previous insert.
2488 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002489 error = xfs_bmbt_lookup_eq(cur, new, &i);
2490 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002492 if (XFS_IS_CORRUPT(mp, i != 0)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002493 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002494 error = -EFSCORRUPTED;
2495 goto done;
2496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002498 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002500 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002501 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002502 error = -EFSCORRUPTED;
2503 goto done;
2504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 }
2506 break;
2507
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002508 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2509 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2510 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2511 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2512 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2513 case BMAP_LEFT_CONTIG:
2514 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 /*
2516 * These cases are all impossible.
2517 */
2518 ASSERT(0);
2519 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002520
Darrick J. Wong9c194642016-08-03 12:16:05 +10002521 /* update reverse mappings */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07002522 xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002523
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002524 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002525 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002526 int tmp_logflags; /* partial log flag return val */
2527
2528 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07002529 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2530 &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002531 *logflagsp |= tmp_logflags;
2532 if (error)
2533 goto done;
2534 }
2535
2536 /* clear out the allocated field, done with it now in any case. */
2537 if (cur) {
Christoph Hellwig88ee2f42024-02-22 12:37:03 -08002538 cur->bc_bmap.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002539 *curp = cur;
2540 }
2541
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002542 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002544 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 return error;
2546#undef LEFT
2547#undef RIGHT
2548#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549}
2550
2551/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002552 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002554STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555xfs_bmap_add_extent_hole_delay(
2556 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002557 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002558 struct xfs_iext_cursor *icur,
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002559 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560{
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07002561 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2563 xfs_filblks_t newlen=0; /* new indirect size */
2564 xfs_filblks_t oldlen=0; /* old indirect size */
2565 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Dave Chinner0e5b8e42022-04-21 10:46:01 +10002566 uint32_t state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002567 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Darrick J. Wong732436e2022-07-09 10:56:05 -07002569 ifp = xfs_ifork_ptr(ip, whichfork);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002570 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002571
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 /*
2573 * Check and set flags if this segment has a left neighbor
2574 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002575 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002576 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002577 if (isnullstartblock(left.br_startblock))
2578 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 /*
2582 * Check and set flags if the current (right) segment exists.
2583 * If it doesn't exist, we're converting the hole at end-of-file.
2584 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002585 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002586 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002587 if (isnullstartblock(right.br_startblock))
2588 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 /*
2592 * Set contiguity flags on the left and right neighbors.
2593 * Don't let extents get too large, even if the pieces are contiguous.
2594 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002595 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2596 left.br_startoff + left.br_blockcount == new->br_startoff &&
Chandan Babu R95f0b952021-08-09 12:05:22 +05302597 left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002598 state |= BMAP_LEFT_CONTIG;
2599
2600 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2601 new->br_startoff + new->br_blockcount == right.br_startoff &&
Chandan Babu R95f0b952021-08-09 12:05:22 +05302602 new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002603 (!(state & BMAP_LEFT_CONTIG) ||
2604 (left.br_blockcount + new->br_blockcount +
Chandan Babu R95f0b952021-08-09 12:05:22 +05302605 right.br_blockcount <= XFS_MAX_BMBT_EXTLEN)))
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002606 state |= BMAP_RIGHT_CONTIG;
2607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 /*
2609 * Switch out based on the contiguity flags.
2610 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002611 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2612 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 /*
2614 * New allocation is contiguous with delayed allocations
2615 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002616 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 */
2618 temp = left.br_blockcount + new->br_blockcount +
2619 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002620
Eric Sandeen9d87c312009-01-14 23:22:07 -06002621 oldlen = startblockval(left.br_startblock) +
2622 startblockval(new->br_startblock) +
2623 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002624 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2625 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002626 left.br_startblock = nullstartblock(newlen);
2627 left.br_blockcount = temp;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002628
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002629 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002630 xfs_iext_prev(ifp, icur);
2631 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 break;
2633
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002634 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 /*
2636 * New allocation is contiguous with a delayed allocation
2637 * on the left.
2638 * Merge the new allocation with the left neighbor.
2639 */
2640 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002641
Eric Sandeen9d87c312009-01-14 23:22:07 -06002642 oldlen = startblockval(left.br_startblock) +
2643 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002644 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2645 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002646 left.br_blockcount = temp;
2647 left.br_startblock = nullstartblock(newlen);
Christoph Hellwig41d196f2017-11-03 10:34:39 -07002648
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002649 xfs_iext_prev(ifp, icur);
2650 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 break;
2652
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002653 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 /*
2655 * New allocation is contiguous with a delayed allocation
2656 * on the right.
2657 * Merge the new allocation with the right neighbor.
2658 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002660 oldlen = startblockval(new->br_startblock) +
2661 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002662 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2663 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002664 right.br_startoff = new->br_startoff;
2665 right.br_startblock = nullstartblock(newlen);
2666 right.br_blockcount = temp;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002667 xfs_iext_update_extent(ip, state, icur, &right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 break;
2669
2670 case 0:
2671 /*
2672 * New allocation is not contiguous with another
2673 * delayed allocation.
2674 * Insert a new entry.
2675 */
2676 oldlen = newlen = 0;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002677 xfs_iext_insert(ip, icur, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 break;
2679 }
2680 if (oldlen != newlen) {
2681 ASSERT(oldlen > newlen);
Christoph Hellwigf30f6562024-04-22 13:20:12 +02002682 xfs_add_fdblocks(ip->i_mount, oldlen - newlen);
2683
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 /*
2685 * Nothing to do for disk quota accounting here.
2686 */
Christoph Hellwig7099bd02024-04-22 13:20:15 +02002687 xfs_mod_delalloc(ip, 0, (int64_t)newlen - oldlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689}
2690
2691/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002692 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 */
2694STATIC int /* error */
2695xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002696 struct xfs_trans *tp,
2697 struct xfs_inode *ip,
2698 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002699 struct xfs_iext_cursor *icur,
Christoph Hellwig6d045582017-04-11 16:45:54 -07002700 struct xfs_btree_cur **curp,
2701 struct xfs_bmbt_irec *new,
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002702 int *logflagsp,
Dave Chinnere7d410a2022-04-21 10:46:09 +10002703 uint32_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704{
Darrick J. Wong732436e2022-07-09 10:56:05 -07002705 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002706 struct xfs_mount *mp = ip->i_mount;
2707 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2711 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002712 int rval=0; /* return value (logging flags) */
Dave Chinner0e5b8e42022-04-21 10:46:01 +10002713 uint32_t state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002714 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002716 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwige9e66df2024-02-22 12:35:13 -08002717 ASSERT(!cur || !(cur->bc_flags & XFS_BTREE_BMBT_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002718
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002719 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 /*
2722 * Check and set flags if this segment has a left neighbor.
2723 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002724 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002725 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002726 if (isnullstartblock(left.br_startblock))
2727 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 /*
2731 * Check and set flags if this segment has a current value.
2732 * Not true if we're inserting into the "hole" at eof.
2733 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002734 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002735 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002736 if (isnullstartblock(right.br_startblock))
2737 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 /*
2741 * We're inserting a real allocation between "left" and "right".
2742 * Set the contiguity flags. Don't let extents get too large.
2743 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002744 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2745 left.br_startoff + left.br_blockcount == new->br_startoff &&
2746 left.br_startblock + left.br_blockcount == new->br_startblock &&
2747 left.br_state == new->br_state &&
Chandan Babu R95f0b952021-08-09 12:05:22 +05302748 left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002749 state |= BMAP_LEFT_CONTIG;
2750
2751 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2752 new->br_startoff + new->br_blockcount == right.br_startoff &&
2753 new->br_startblock + new->br_blockcount == right.br_startblock &&
2754 new->br_state == right.br_state &&
Chandan Babu R95f0b952021-08-09 12:05:22 +05302755 new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002756 (!(state & BMAP_LEFT_CONTIG) ||
2757 left.br_blockcount + new->br_blockcount +
Chandan Babu R95f0b952021-08-09 12:05:22 +05302758 right.br_blockcount <= XFS_MAX_BMBT_EXTLEN))
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002759 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002761 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 /*
2763 * Select which case we're in here, and implement it.
2764 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002765 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2766 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 /*
2768 * New allocation is contiguous with real allocations on the
2769 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002770 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002772 left.br_blockcount += new->br_blockcount + right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002773
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002774 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002775 xfs_iext_prev(ifp, icur);
2776 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002777 ifp->if_nextents--;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002778
Christoph Hellwig6d045582017-04-11 16:45:54 -07002779 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002780 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002781 } else {
2782 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002783 error = xfs_bmbt_lookup_eq(cur, &right, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002784 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002785 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002786 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002787 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002788 error = -EFSCORRUPTED;
2789 goto done;
2790 }
Christoph Hellwig6d045582017-04-11 16:45:54 -07002791 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002792 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002793 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002794 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002795 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002796 error = -EFSCORRUPTED;
2797 goto done;
2798 }
Christoph Hellwig6d045582017-04-11 16:45:54 -07002799 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002800 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002801 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002802 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002803 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002804 error = -EFSCORRUPTED;
2805 goto done;
2806 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002807 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002808 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002809 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002811 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002813 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 /*
2815 * New allocation is contiguous with a real allocation
2816 * on the left.
2817 * Merge the new allocation with the left neighbor.
2818 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002819 old = left;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002820 left.br_blockcount += new->br_blockcount;
Christoph Hellwig1d2e0082017-11-03 10:34:40 -07002821
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002822 xfs_iext_prev(ifp, icur);
2823 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002824
Christoph Hellwig6d045582017-04-11 16:45:54 -07002825 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002826 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002827 } else {
2828 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002829 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002830 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002831 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002832 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002833 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002834 error = -EFSCORRUPTED;
2835 goto done;
2836 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002837 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002838 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002839 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002841 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002843 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 /*
2845 * New allocation is contiguous with a real allocation
2846 * on the right.
2847 * Merge the new allocation with the right neighbor.
2848 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002849 old = right;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07002850
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002851 right.br_startoff = new->br_startoff;
2852 right.br_startblock = new->br_startblock;
2853 right.br_blockcount += new->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002854 xfs_iext_update_extent(ip, state, icur, &right);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002855
Christoph Hellwig6d045582017-04-11 16:45:54 -07002856 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002857 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002858 } else {
2859 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002860 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002861 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002862 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002863 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002864 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002865 error = -EFSCORRUPTED;
2866 goto done;
2867 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002868 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002869 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002870 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002872 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
2874 case 0:
2875 /*
2876 * New allocation is not contiguous with another
2877 * real allocation.
2878 * Insert a new entry.
2879 */
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002880 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002881 ifp->if_nextents++;
2882
Christoph Hellwig6d045582017-04-11 16:45:54 -07002883 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002884 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002885 } else {
2886 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002887 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002888 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002889 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002890 if (XFS_IS_CORRUPT(mp, i != 0)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002891 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002892 error = -EFSCORRUPTED;
2893 goto done;
2894 }
Christoph Hellwig6d045582017-04-11 16:45:54 -07002895 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002896 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002897 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002898 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08002899 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002900 error = -EFSCORRUPTED;
2901 goto done;
2902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002904 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002906
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002907 /* add reverse mapping unless caller opted out */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07002908 if (!(flags & XFS_BMAPI_NORMAP))
2909 xfs_rmap_map_extent(tp, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002910
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002911 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002912 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002913 int tmp_logflags; /* partial log flag return val */
2914
Christoph Hellwig6d045582017-04-11 16:45:54 -07002915 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07002916 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2917 &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002918 *logflagsp |= tmp_logflags;
2919 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002920 if (error)
2921 goto done;
2922 }
2923
2924 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002925 if (cur)
Christoph Hellwig88ee2f42024-02-22 12:37:03 -08002926 cur->bc_bmap.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002927
Christoph Hellwig6d045582017-04-11 16:45:54 -07002928 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002929done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002930 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002931 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932}
2933
Nathan Scottdd9f4382006-01-11 15:28:28 +11002934/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11002935 * Functions used in the extent read, allocate and remove paths
2936 */
2937
2938/*
Christoph Hellwig031474c2021-03-29 11:11:41 -07002939 * Adjust the size of the new extent based on i_extsize and rt extsize.
Nathan Scottdd9f4382006-01-11 15:28:28 +11002940 */
Dave Chinner68988112013-08-12 20:49:42 +10002941int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002942xfs_bmap_extsize_align(
2943 xfs_mount_t *mp,
2944 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2945 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2946 xfs_extlen_t extsz, /* align to this extent size */
2947 int rt, /* is this a realtime inode? */
2948 int eof, /* is extent at end-of-file? */
2949 int delay, /* creating delalloc extent? */
2950 int convert, /* overwriting unwritten extent? */
2951 xfs_fileoff_t *offp, /* in/out: aligned offset */
2952 xfs_extlen_t *lenp) /* in/out: aligned length */
2953{
2954 xfs_fileoff_t orig_off; /* original offset */
2955 xfs_extlen_t orig_alen; /* original length */
2956 xfs_fileoff_t orig_end; /* original off+len */
2957 xfs_fileoff_t nexto; /* next file offset */
2958 xfs_fileoff_t prevo; /* previous file offset */
2959 xfs_fileoff_t align_off; /* temp for offset */
2960 xfs_extlen_t align_alen; /* temp for length */
2961 xfs_extlen_t temp; /* temp for calculations */
2962
2963 if (convert)
2964 return 0;
2965
2966 orig_off = align_off = *offp;
2967 orig_alen = align_alen = *lenp;
2968 orig_end = orig_off + orig_alen;
2969
2970 /*
2971 * If this request overlaps an existing extent, then don't
2972 * attempt to perform any additional alignment.
2973 */
2974 if (!delay && !eof &&
2975 (orig_off >= gotp->br_startoff) &&
2976 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2977 return 0;
2978 }
2979
2980 /*
2981 * If the file offset is unaligned vs. the extent size
2982 * we need to align it. This will be possible unless
2983 * the file was previously written with a kernel that didn't
2984 * perform this alignment, or if a truncate shot us in the
2985 * foot.
2986 */
Dave Chinner0703a8e2018-06-08 09:54:22 -07002987 div_u64_rem(orig_off, extsz, &temp);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002988 if (temp) {
2989 align_alen += temp;
2990 align_off -= temp;
2991 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10002992
2993 /* Same adjustment for the end of the requested area. */
2994 temp = (align_alen % extsz);
2995 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11002996 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10002997
2998 /*
2999 * For large extent hint sizes, the aligned extent might be larger than
Chandan Babu R95f0b952021-08-09 12:05:22 +05303000 * XFS_BMBT_MAX_EXTLEN. In that case, reduce the size by an extsz so
3001 * that it pulls the length back under XFS_BMBT_MAX_EXTLEN. The outer
3002 * allocation loops handle short allocation just fine, so it is safe to
3003 * do this. We only want to do it when we are forced to, though, because
3004 * it means more allocation operations are required.
Dave Chinner6dea405e2015-05-29 07:40:06 +10003005 */
Chandan Babu R95f0b952021-08-09 12:05:22 +05303006 while (align_alen > XFS_MAX_BMBT_EXTLEN)
Dave Chinner6dea405e2015-05-29 07:40:06 +10003007 align_alen -= extsz;
Chandan Babu R95f0b952021-08-09 12:05:22 +05303008 ASSERT(align_alen <= XFS_MAX_BMBT_EXTLEN);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003009
Nathan Scottdd9f4382006-01-11 15:28:28 +11003010 /*
3011 * If the previous block overlaps with this proposed allocation
3012 * then move the start forward without adjusting the length.
3013 */
3014 if (prevp->br_startoff != NULLFILEOFF) {
3015 if (prevp->br_startblock == HOLESTARTBLOCK)
3016 prevo = prevp->br_startoff;
3017 else
3018 prevo = prevp->br_startoff + prevp->br_blockcount;
3019 } else
3020 prevo = 0;
3021 if (align_off != orig_off && align_off < prevo)
3022 align_off = prevo;
3023 /*
3024 * If the next block overlaps with this proposed allocation
3025 * then move the start back without adjusting the length,
3026 * but not before offset 0.
3027 * This may of course make the start overlap previous block,
3028 * and if we hit the offset 0 limit then the next block
3029 * can still overlap too.
3030 */
3031 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3032 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3033 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3034 nexto = gotp->br_startoff + gotp->br_blockcount;
3035 else
3036 nexto = gotp->br_startoff;
3037 } else
3038 nexto = NULLFILEOFF;
3039 if (!eof &&
3040 align_off + align_alen != orig_end &&
3041 align_off + align_alen > nexto)
3042 align_off = nexto > align_alen ? nexto - align_alen : 0;
3043 /*
3044 * If we're now overlapping the next or previous extent that
3045 * means we can't fit an extsz piece in this hole. Just move
3046 * the start forward to the first valid spot and set
3047 * the length so we hit the end.
3048 */
3049 if (align_off != orig_off && align_off < prevo)
3050 align_off = prevo;
3051 if (align_off + align_alen != orig_end &&
3052 align_off + align_alen > nexto &&
3053 nexto != NULLFILEOFF) {
3054 ASSERT(nexto > prevo);
3055 align_alen = nexto - align_off;
3056 }
3057
3058 /*
3059 * If realtime, and the result isn't a multiple of the realtime
3060 * extent size we need to remove blocks until it is.
3061 */
Darrick J. Wong68db60b2023-10-16 09:34:39 -07003062 if (rt && (temp = xfs_extlen_to_rtxmod(mp, align_alen))) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11003063 /*
3064 * We're not covering the original request, or
3065 * we won't be able to once we fix the length.
3066 */
3067 if (orig_off < align_off ||
3068 orig_end > align_off + align_alen ||
3069 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003070 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003071 /*
3072 * Try to fix it by moving the start up.
3073 */
3074 if (align_off + temp <= orig_off) {
3075 align_alen -= temp;
3076 align_off += temp;
3077 }
3078 /*
3079 * Try to fix it by moving the end in.
3080 */
3081 else if (align_off + align_alen - temp >= orig_end)
3082 align_alen -= temp;
3083 /*
3084 * Set the start to the minimum then trim the length.
3085 */
3086 else {
3087 align_alen -= orig_off - align_off;
3088 align_off = orig_off;
Darrick J. Wong68db60b2023-10-16 09:34:39 -07003089 align_alen -= xfs_extlen_to_rtxmod(mp, align_alen);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003090 }
3091 /*
3092 * Result doesn't cover the request, fail it.
3093 */
3094 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003095 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003096 } else {
3097 ASSERT(orig_off >= align_off);
Chandan Babu R95f0b952021-08-09 12:05:22 +05303098 /* see XFS_BMBT_MAX_EXTLEN handling above */
Dave Chinner6dea405e2015-05-29 07:40:06 +10003099 ASSERT(orig_end <= align_off + align_alen ||
Chandan Babu R95f0b952021-08-09 12:05:22 +05303100 align_alen + extsz > XFS_MAX_BMBT_EXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003101 }
3102
3103#ifdef DEBUG
3104 if (!eof && gotp->br_startoff != NULLFILEOFF)
3105 ASSERT(align_off + align_alen <= gotp->br_startoff);
3106 if (prevp->br_startoff != NULLFILEOFF)
3107 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3108#endif
3109
3110 *lenp = align_alen;
3111 *offp = align_off;
3112 return 0;
3113}
3114
Christoph Hellwig1e21d182024-08-30 15:37:09 -07003115static inline bool
3116xfs_bmap_adjacent_valid(
3117 struct xfs_bmalloca *ap,
3118 xfs_fsblock_t x,
3119 xfs_fsblock_t y)
3120{
3121 struct xfs_mount *mp = ap->ip->i_mount;
3122
3123 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3124 (ap->datatype & XFS_ALLOC_USERDATA))
3125 return x < mp->m_sb.sb_rblocks;
3126
3127 return XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) &&
3128 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount &&
3129 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks;
3130}
3131
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132#define XFS_ALLOC_GAP_UNITS 4
3133
Christoph Hellwig676544c2023-12-18 05:57:24 +01003134/* returns true if ap->blkno was modified */
3135bool
Nathan Scotta365bdd2006-03-14 13:34:16 +11003136xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003137 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138{
Christoph Hellwig1e21d182024-08-30 15:37:09 -07003139 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 /*
3142 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003143 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003145 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3146 !isnullstartblock(ap->prev.br_startblock) &&
Christoph Hellwig1e21d182024-08-30 15:37:09 -07003147 xfs_bmap_adjacent_valid(ap,
3148 ap->prev.br_startblock + ap->prev.br_blockcount,
3149 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003150 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 /*
3152 * Adjust for the gap between prevp and us.
3153 */
Dave Chinner3a756672011-09-18 20:40:58 +00003154 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003155 (ap->prev.br_startoff + ap->prev.br_blockcount);
Christoph Hellwig1e21d182024-08-30 15:37:09 -07003156 if (adjust && xfs_bmap_adjacent_valid(ap, ap->blkno + adjust,
3157 ap->prev.br_startblock))
Dave Chinner3a756672011-09-18 20:40:58 +00003158 ap->blkno += adjust;
Christoph Hellwig676544c2023-12-18 05:57:24 +01003159 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 }
3161 /*
3162 * If not at eof, then compare the two neighbor blocks.
3163 * Figure out whether either one gives us a good starting point,
3164 * and pick the better one.
3165 */
Christoph Hellwig676544c2023-12-18 05:57:24 +01003166 if (!ap->eof) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 xfs_fsblock_t gotbno; /* right side block number */
3168 xfs_fsblock_t gotdiff=0; /* right side difference */
3169 xfs_fsblock_t prevbno; /* left side block number */
3170 xfs_fsblock_t prevdiff=0; /* left side difference */
3171
3172 /*
3173 * If there's a previous (left) block, select a requested
3174 * start block based on it.
3175 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003176 if (ap->prev.br_startoff != NULLFILEOFF &&
3177 !isnullstartblock(ap->prev.br_startblock) &&
3178 (prevbno = ap->prev.br_startblock +
3179 ap->prev.br_blockcount) &&
Christoph Hellwig1e21d182024-08-30 15:37:09 -07003180 xfs_bmap_adjacent_valid(ap, prevbno,
3181 ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 /*
3183 * Calculate gap to end of previous block.
3184 */
Dave Chinner3a756672011-09-18 20:40:58 +00003185 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003186 (ap->prev.br_startoff +
3187 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 /*
3189 * Figure the startblock based on the previous block's
3190 * end and the gap size.
3191 * Heuristic!
3192 * If the gap is large relative to the piece we're
3193 * allocating, or using it gives us an invalid block
3194 * number, then just use the end of the previous block.
3195 */
Dave Chinner3a756672011-09-18 20:40:58 +00003196 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Christoph Hellwig1e21d182024-08-30 15:37:09 -07003197 xfs_bmap_adjacent_valid(ap, prevbno + prevdiff,
3198 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 prevbno += adjust;
3200 else
3201 prevdiff += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 }
3203 /*
3204 * No previous block or can't follow it, just default.
3205 */
3206 else
3207 prevbno = NULLFSBLOCK;
3208 /*
3209 * If there's a following (right) block, select a requested
3210 * start block based on it.
3211 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003212 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 /*
3214 * Calculate gap to start of next block.
3215 */
Dave Chinner3a756672011-09-18 20:40:58 +00003216 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 /*
3218 * Figure the startblock based on the next block's
3219 * start and the gap size.
3220 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003221 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 /*
3223 * Heuristic!
3224 * If the gap is large relative to the piece we're
3225 * allocating, or using it gives us an invalid block
3226 * number, then just use the start of the next block
3227 * offset by our length.
3228 */
Dave Chinner3a756672011-09-18 20:40:58 +00003229 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Christoph Hellwig1e21d182024-08-30 15:37:09 -07003230 xfs_bmap_adjacent_valid(ap, gotbno - gotdiff,
3231 gotbno))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 gotbno -= adjust;
Christoph Hellwig1e21d182024-08-30 15:37:09 -07003233 else if (xfs_bmap_adjacent_valid(ap, gotbno - ap->length,
3234 gotbno)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003235 gotbno -= ap->length;
3236 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 } else
3238 gotdiff += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 }
3240 /*
3241 * No next block, just default.
3242 */
3243 else
3244 gotbno = NULLFSBLOCK;
3245 /*
3246 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003247 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 */
Christoph Hellwig676544c2023-12-18 05:57:24 +01003249 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) {
Dave Chinner3a756672011-09-18 20:40:58 +00003250 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Christoph Hellwig676544c2023-12-18 05:57:24 +01003251 return true;
3252 }
3253 if (prevbno != NULLFSBLOCK) {
Dave Chinner3a756672011-09-18 20:40:58 +00003254 ap->blkno = prevbno;
Christoph Hellwig676544c2023-12-18 05:57:24 +01003255 return true;
3256 }
3257 if (gotbno != NULLFSBLOCK) {
Dave Chinner3a756672011-09-18 20:40:58 +00003258 ap->blkno = gotbno;
Christoph Hellwig676544c2023-12-18 05:57:24 +01003259 return true;
3260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 }
Christoph Hellwig1e21d182024-08-30 15:37:09 -07003262
Christoph Hellwig676544c2023-12-18 05:57:24 +01003263 return false;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003264}
3265
Dave Chinner05cf4922023-02-13 09:14:55 +11003266int
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003267xfs_bmap_longest_free_extent(
Dave Chinner76257a12023-02-13 09:14:53 +11003268 struct xfs_perag *pag,
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003269 struct xfs_trans *tp,
Dave Chinner6b637ad2023-02-13 09:14:55 +11003270 xfs_extlen_t *blen)
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003271{
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003272 xfs_extlen_t longest;
3273 int error = 0;
3274
Dave Chinner7ac2ff82023-02-13 09:14:52 +11003275 if (!xfs_perag_initialised_agf(pag)) {
Dave Chinner08d3e842022-07-07 19:07:40 +10003276 error = xfs_alloc_read_agf(pag, tp, XFS_ALLOC_FLAG_TRYLOCK,
Dave Chinner76b47e52022-07-07 19:07:32 +10003277 NULL);
Dave Chinner6b637ad2023-02-13 09:14:55 +11003278 if (error)
Dave Chinner76257a12023-02-13 09:14:53 +11003279 return error;
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003280 }
3281
Eric Sandeena1f69412018-04-06 10:09:42 -07003282 longest = xfs_alloc_longest_free_extent(pag,
Dave Chinner76257a12023-02-13 09:14:53 +11003283 xfs_alloc_min_freelist(pag->pag_mount, pag),
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003284 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003285 if (*blen < longest)
3286 *blen = longest;
3287
Dave Chinner76257a12023-02-13 09:14:53 +11003288 return 0;
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003289}
3290
Dave Chinner6b637ad2023-02-13 09:14:55 +11003291static xfs_extlen_t
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003292xfs_bmap_select_minlen(
3293 struct xfs_bmalloca *ap,
3294 struct xfs_alloc_arg *args,
Dave Chinner6b637ad2023-02-13 09:14:55 +11003295 xfs_extlen_t blen)
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003296{
Dave Chinner6b637ad2023-02-13 09:14:55 +11003297
3298 /*
3299 * Since we used XFS_ALLOC_FLAG_TRYLOCK in _longest_free_extent(), it is
3300 * possible that there is enough contiguous free space for this request.
3301 */
3302 if (blen < ap->minlen)
3303 return ap->minlen;
3304
3305 /*
3306 * If the best seen length is less than the request length,
3307 * use the best as the minimum, otherwise we've got the maxlen we
3308 * were asked for.
3309 */
3310 if (blen < args->maxlen)
3311 return blen;
3312 return args->maxlen;
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003313}
3314
Dave Chinner85843322023-02-13 09:14:53 +11003315static int
Dave Chinner36b6ad22023-02-11 04:10:06 +11003316xfs_bmap_btalloc_select_lengths(
Christoph Hellwigc467c042010-02-15 23:34:42 +00003317 struct xfs_bmalloca *ap,
3318 struct xfs_alloc_arg *args,
3319 xfs_extlen_t *blen)
3320{
Dave Chinner85843322023-02-13 09:14:53 +11003321 struct xfs_mount *mp = args->mp;
Dave Chinner76257a12023-02-13 09:14:53 +11003322 struct xfs_perag *pag;
3323 xfs_agnumber_t agno, startag;
Dave Chinner76257a12023-02-13 09:14:53 +11003324 int error = 0;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003325
Dave Chinner36b6ad22023-02-11 04:10:06 +11003326 if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3327 args->total = ap->minlen;
3328 args->minlen = ap->minlen;
3329 return 0;
3330 }
Christoph Hellwigc467c042010-02-15 23:34:42 +00003331
Dave Chinner36b6ad22023-02-11 04:10:06 +11003332 args->total = ap->total;
Dave Chinner85843322023-02-13 09:14:53 +11003333 startag = XFS_FSB_TO_AGNO(mp, ap->blkno);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003334 if (startag == NULLAGNUMBER)
Dave Chinner76257a12023-02-13 09:14:53 +11003335 startag = 0;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003336
Dave Chinner76257a12023-02-13 09:14:53 +11003337 *blen = 0;
3338 for_each_perag_wrap(mp, startag, agno, pag) {
Dave Chinner6b637ad2023-02-13 09:14:55 +11003339 error = xfs_bmap_longest_free_extent(pag, args->tp, blen);
3340 if (error && error != -EAGAIN)
Dave Chinner76257a12023-02-13 09:14:53 +11003341 break;
Dave Chinner6b637ad2023-02-13 09:14:55 +11003342 error = 0;
Dave Chinner76257a12023-02-13 09:14:53 +11003343 if (*blen >= args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00003344 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003345 }
Dave Chinner76257a12023-02-13 09:14:53 +11003346 if (pag)
3347 xfs_perag_rele(pag);
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003348
Dave Chinner6b637ad2023-02-13 09:14:55 +11003349 args->minlen = xfs_bmap_select_minlen(ap, args, *blen);
Dave Chinner76257a12023-02-13 09:14:53 +11003350 return error;
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003351}
3352
Darrick J. Wong751f3762018-01-25 13:58:13 -08003353/* Update all inode and quota accounting for the allocation we just did. */
Christoph Hellwig58643462023-12-18 05:57:20 +01003354void
3355xfs_bmap_alloc_account(
Christoph Hellwigeef519d2023-12-18 05:57:19 +01003356 struct xfs_bmalloca *ap)
Darrick J. Wong751f3762018-01-25 13:58:13 -08003357{
Christoph Hellwig58643462023-12-18 05:57:20 +01003358 bool isrt = XFS_IS_REALTIME_INODE(ap->ip) &&
Darrick J. Wongd61b40b2024-01-08 18:17:34 -08003359 !(ap->flags & XFS_BMAPI_ATTRFORK);
Christoph Hellwig58643462023-12-18 05:57:20 +01003360 uint fld;
3361
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003362 if (ap->flags & XFS_BMAPI_COWFORK) {
3363 /*
3364 * COW fork blocks are in-core only and thus are treated as
3365 * in-core quota reservation (like delalloc blocks) even when
3366 * converted to real blocks. The quota reservation is not
3367 * accounted to disk until blocks are remapped to the data
3368 * fork. So if these blocks were previously delalloc, we
3369 * already have quota reservation and there's nothing to do
3370 * yet.
3371 */
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003372 if (ap->wasdel) {
Christoph Hellwig7099bd02024-04-22 13:20:15 +02003373 xfs_mod_delalloc(ap->ip, -(int64_t)ap->length, 0);
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003374 return;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003375 }
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003376
3377 /*
3378 * Otherwise, we've allocated blocks in a hole. The transaction
3379 * has acquired in-core quota reservation for this extent.
3380 * Rather than account these as real blocks, however, we reduce
3381 * the transaction quota reservation based on the allocation.
3382 * This essentially transfers the transaction quota reservation
3383 * to that of a delalloc extent.
3384 */
Christoph Hellwigeef519d2023-12-18 05:57:19 +01003385 ap->ip->i_delayed_blks += ap->length;
Christoph Hellwig58643462023-12-18 05:57:20 +01003386 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, isrt ?
3387 XFS_TRANS_DQ_RES_RTBLKS : XFS_TRANS_DQ_RES_BLKS,
Christoph Hellwigeef519d2023-12-18 05:57:19 +01003388 -(long)ap->length);
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003389 return;
3390 }
3391
3392 /* data/attr fork only */
Christoph Hellwigeef519d2023-12-18 05:57:19 +01003393 ap->ip->i_nblocks += ap->length;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003394 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003395 if (ap->wasdel) {
Christoph Hellwigeef519d2023-12-18 05:57:19 +01003396 ap->ip->i_delayed_blks -= ap->length;
Christoph Hellwig7099bd02024-04-22 13:20:15 +02003397 xfs_mod_delalloc(ap->ip, -(int64_t)ap->length, 0);
Christoph Hellwig58643462023-12-18 05:57:20 +01003398 fld = isrt ? XFS_TRANS_DQ_DELRTBCOUNT : XFS_TRANS_DQ_DELBCOUNT;
3399 } else {
3400 fld = isrt ? XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003401 }
Christoph Hellwig58643462023-12-18 05:57:20 +01003402
3403 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, fld, ap->length);
Darrick J. Wong751f3762018-01-25 13:58:13 -08003404}
3405
Chandan Babu R0961fdd2021-01-22 16:48:16 -08003406static int
3407xfs_bmap_compute_alignments(
3408 struct xfs_bmalloca *ap,
3409 struct xfs_alloc_arg *args)
3410{
3411 struct xfs_mount *mp = args->mp;
3412 xfs_extlen_t align = 0; /* minimum allocation alignment */
3413 int stripe_align = 0;
Chandan Babu R0961fdd2021-01-22 16:48:16 -08003414
3415 /* stripe alignment for allocation is determined by mount parameters */
Dave Chinner0560f312021-08-18 18:46:52 -07003416 if (mp->m_swidth && xfs_has_swalloc(mp))
Chandan Babu R0961fdd2021-01-22 16:48:16 -08003417 stripe_align = mp->m_swidth;
3418 else if (mp->m_dalign)
3419 stripe_align = mp->m_dalign;
3420
3421 if (ap->flags & XFS_BMAPI_COWFORK)
3422 align = xfs_get_cowextsz_hint(ap->ip);
3423 else if (ap->datatype & XFS_ALLOC_USERDATA)
3424 align = xfs_get_extsz_hint(ap->ip);
3425 if (align) {
Chandan Babu R560ab6c2021-01-27 08:35:14 -08003426 if (xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, align, 0,
3427 ap->eof, 0, ap->conv, &ap->offset,
3428 &ap->length))
3429 ASSERT(0);
Chandan Babu R0961fdd2021-01-22 16:48:16 -08003430 ASSERT(ap->length);
3431 }
3432
3433 /* apply extent size hints if obtained earlier */
3434 if (align) {
3435 args->prod = align;
3436 div_u64_rem(ap->offset, args->prod, &args->mod);
3437 if (args->mod)
3438 args->mod = args->prod - args->mod;
3439 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
3440 args->prod = 1;
3441 args->mod = 0;
3442 } else {
3443 args->prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
3444 div_u64_rem(ap->offset, args->prod, &args->mod);
3445 if (args->mod)
3446 args->mod = args->prod - args->mod;
3447 }
3448
3449 return stripe_align;
3450}
3451
Chandan Babu R07c72e52021-01-22 16:48:17 -08003452static void
3453xfs_bmap_process_allocated_extent(
3454 struct xfs_bmalloca *ap,
3455 struct xfs_alloc_arg *args,
3456 xfs_fileoff_t orig_offset,
3457 xfs_extlen_t orig_length)
3458{
Chandan Babu R07c72e52021-01-22 16:48:17 -08003459 ap->blkno = args->fsbno;
Chandan Babu R07c72e52021-01-22 16:48:17 -08003460 ap->length = args->len;
3461 /*
3462 * If the extent size hint is active, we tried to round the
3463 * caller's allocation request offset down to extsz and the
3464 * length up to another extsz boundary. If we found a free
3465 * extent we mapped it in starting at this new offset. If the
3466 * newly mapped space isn't long enough to cover any of the
3467 * range of offsets that was originally requested, move the
3468 * mapping up so that we can fill as much of the caller's
3469 * original request as possible. Free space is apparently
3470 * very fragmented so we're unlikely to be able to satisfy the
3471 * hints anyway.
3472 */
3473 if (ap->length <= orig_length)
3474 ap->offset = orig_offset;
3475 else if (ap->offset + ap->length < orig_offset + orig_length)
3476 ap->offset = orig_offset + orig_length - ap->length;
Christoph Hellwig58643462023-12-18 05:57:20 +01003477 xfs_bmap_alloc_account(ap);
Chandan Babu R07c72e52021-01-22 16:48:17 -08003478}
3479
Chandan Babu R30151962021-01-22 16:48:17 -08003480#ifdef DEBUG
3481static int
3482xfs_bmap_exact_minlen_extent_alloc(
3483 struct xfs_bmalloca *ap)
3484{
3485 struct xfs_mount *mp = ap->ip->i_mount;
3486 struct xfs_alloc_arg args = { .tp = ap->tp, .mp = mp };
3487 xfs_fileoff_t orig_offset;
3488 xfs_extlen_t orig_length;
3489 int error;
3490
3491 ASSERT(ap->length);
3492
3493 if (ap->minlen != 1) {
3494 ap->blkno = NULLFSBLOCK;
3495 ap->length = 0;
3496 return 0;
3497 }
3498
3499 orig_offset = ap->offset;
3500 orig_length = ap->length;
3501
3502 args.alloc_minlen_only = 1;
3503
3504 xfs_bmap_compute_alignments(ap, &args);
3505
Dave Chinner36b6ad22023-02-11 04:10:06 +11003506 /*
3507 * Unlike the longest extent available in an AG, we don't track
3508 * the length of an AG's shortest extent.
3509 * XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT is a debug only knob and
3510 * hence we can afford to start traversing from the 0th AG since
3511 * we need not be concerned about a drop in performance in
3512 * "debug only" code paths.
3513 */
3514 ap->blkno = XFS_AGB_TO_FSB(mp, 0, 0);
Chandan Babu R30151962021-01-22 16:48:17 -08003515
Chandan Babu R30151962021-01-22 16:48:17 -08003516 args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
Chandan Babu R6e8bd392021-03-25 11:55:10 -07003517 args.minlen = args.maxlen = ap->minlen;
3518 args.total = ap->total;
Chandan Babu R30151962021-01-22 16:48:17 -08003519
3520 args.alignment = 1;
3521 args.minalignslop = 0;
3522
3523 args.minleft = ap->minleft;
3524 args.wasdel = ap->wasdel;
3525 args.resv = XFS_AG_RESV_NONE;
3526 args.datatype = ap->datatype;
3527
Dave Chinner319c9e82023-02-13 09:14:53 +11003528 error = xfs_alloc_vextent_first_ag(&args, ap->blkno);
Chandan Babu R30151962021-01-22 16:48:17 -08003529 if (error)
3530 return error;
3531
3532 if (args.fsbno != NULLFSBLOCK) {
3533 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3534 orig_length);
3535 } else {
3536 ap->blkno = NULLFSBLOCK;
3537 ap->length = 0;
3538 }
3539
3540 return 0;
3541}
3542#else
3543
3544#define xfs_bmap_exact_minlen_extent_alloc(bma) (-EFSCORRUPTED)
3545
3546#endif
3547
Dave Chinner85843322023-02-13 09:14:53 +11003548/*
3549 * If we are not low on available data blocks and we are allocating at
3550 * EOF, optimise allocation for contiguous file extension and/or stripe
3551 * alignment of the new extent.
3552 *
3553 * NOTE: ap->aeof is only set if the allocation length is >= the
3554 * stripe unit and the allocation offset is at the end of file.
3555 */
3556static int
3557xfs_bmap_btalloc_at_eof(
3558 struct xfs_bmalloca *ap,
3559 struct xfs_alloc_arg *args,
3560 xfs_extlen_t blen,
Dave Chinner2a7f6d42023-02-13 09:14:53 +11003561 int stripe_align,
3562 bool ag_only)
Dave Chinner85843322023-02-13 09:14:53 +11003563{
3564 struct xfs_mount *mp = args->mp;
Dave Chinnerf8f1ed12023-02-13 09:14:56 +11003565 struct xfs_perag *caller_pag = args->pag;
Dave Chinner85843322023-02-13 09:14:53 +11003566 int error;
3567
3568 /*
3569 * If there are already extents in the file, try an exact EOF block
3570 * allocation to extend the file as a contiguous extent. If that fails,
3571 * or it's the first allocation in a file, just try for a stripe aligned
3572 * allocation.
3573 */
3574 if (ap->offset) {
3575 xfs_extlen_t nextminlen = 0;
3576
Dave Chinner85843322023-02-13 09:14:53 +11003577 /*
3578 * Compute the minlen+alignment for the next case. Set slop so
3579 * that the value of minlen+alignment+slop doesn't go up between
3580 * the calls.
3581 */
Dave Chinner230e8fe2023-02-13 09:14:54 +11003582 args->alignment = 1;
Dave Chinner85843322023-02-13 09:14:53 +11003583 if (blen > stripe_align && blen <= args->maxlen)
3584 nextminlen = blen - stripe_align;
3585 else
3586 nextminlen = args->minlen;
3587 if (nextminlen + stripe_align > args->minlen + 1)
3588 args->minalignslop = nextminlen + stripe_align -
3589 args->minlen - 1;
3590 else
3591 args->minalignslop = 0;
3592
Dave Chinnerf8f1ed12023-02-13 09:14:56 +11003593 if (!caller_pag)
3594 args->pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, ap->blkno));
Dave Chinner5f36b2c2023-02-13 09:14:54 +11003595 error = xfs_alloc_vextent_exact_bno(args, ap->blkno);
Darrick J. Wongb82a5c42023-05-02 09:14:27 +10003596 if (!caller_pag) {
Dave Chinnerf8f1ed12023-02-13 09:14:56 +11003597 xfs_perag_put(args->pag);
Darrick J. Wongb82a5c42023-05-02 09:14:27 +10003598 args->pag = NULL;
3599 }
Dave Chinner85843322023-02-13 09:14:53 +11003600 if (error)
3601 return error;
3602
3603 if (args->fsbno != NULLFSBLOCK)
3604 return 0;
3605 /*
3606 * Exact allocation failed. Reset to try an aligned allocation
3607 * according to the original allocation specification.
3608 */
Dave Chinner85843322023-02-13 09:14:53 +11003609 args->alignment = stripe_align;
3610 args->minlen = nextminlen;
3611 args->minalignslop = 0;
3612 } else {
Dave Chinner85843322023-02-13 09:14:53 +11003613 /*
3614 * Adjust minlen to try and preserve alignment if we
3615 * can't guarantee an aligned maxlen extent.
3616 */
Dave Chinner230e8fe2023-02-13 09:14:54 +11003617 args->alignment = stripe_align;
Dave Chinner85843322023-02-13 09:14:53 +11003618 if (blen > args->alignment &&
3619 blen <= args->maxlen + args->alignment)
3620 args->minlen = blen - args->alignment;
3621 args->minalignslop = 0;
3622 }
3623
Dave Chinnerf8f1ed12023-02-13 09:14:56 +11003624 if (ag_only) {
Dave Chinnerdb4710f2023-02-13 09:14:54 +11003625 error = xfs_alloc_vextent_near_bno(args, ap->blkno);
Dave Chinnerf8f1ed12023-02-13 09:14:56 +11003626 } else {
3627 args->pag = NULL;
Dave Chinner2a7f6d42023-02-13 09:14:53 +11003628 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
Dave Chinnerf8f1ed12023-02-13 09:14:56 +11003629 ASSERT(args->pag == NULL);
3630 args->pag = caller_pag;
3631 }
Dave Chinner85843322023-02-13 09:14:53 +11003632 if (error)
3633 return error;
3634
3635 if (args->fsbno != NULLFSBLOCK)
3636 return 0;
3637
3638 /*
3639 * Allocation failed, so turn return the allocation args to their
3640 * original non-aligned state so the caller can proceed on allocation
3641 * failure as if this function was never called.
3642 */
Dave Chinner85843322023-02-13 09:14:53 +11003643 args->alignment = 1;
3644 return 0;
3645}
3646
Dave Chinner89563e72023-02-13 09:14:54 +11003647/*
3648 * We have failed multiple allocation attempts so now are in a low space
3649 * allocation situation. Try a locality first full filesystem minimum length
3650 * allocation whilst still maintaining necessary total block reservation
3651 * requirements.
3652 *
3653 * If that fails, we are now critically low on space, so perform a last resort
3654 * allocation attempt: no reserve, no locality, blocking, minimum length, full
3655 * filesystem free space scan. We also indicate to future allocations in this
3656 * transaction that we are critically low on space so they don't waste time on
3657 * allocation modes that are unlikely to succeed.
3658 */
Dave Chinner8f7747a2023-02-13 09:14:55 +11003659int
Dave Chinner89563e72023-02-13 09:14:54 +11003660xfs_bmap_btalloc_low_space(
3661 struct xfs_bmalloca *ap,
3662 struct xfs_alloc_arg *args)
3663{
3664 int error;
3665
3666 if (args->minlen > ap->minlen) {
3667 args->minlen = ap->minlen;
3668 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3669 if (error || args->fsbno != NULLFSBLOCK)
3670 return error;
3671 }
3672
3673 /* Last ditch attempt before failure is declared. */
3674 args->total = ap->minlen;
3675 error = xfs_alloc_vextent_first_ag(args, 0);
3676 if (error)
3677 return error;
3678 ap->tp->t_flags |= XFS_TRANS_LOWMODE;
3679 return 0;
3680}
3681
3682static int
3683xfs_bmap_btalloc_filestreams(
3684 struct xfs_bmalloca *ap,
3685 struct xfs_alloc_arg *args,
3686 int stripe_align)
3687{
Dave Chinner89563e72023-02-13 09:14:54 +11003688 xfs_extlen_t blen = 0;
Dave Chinnerf8f1ed12023-02-13 09:14:56 +11003689 int error = 0;
Dave Chinner89563e72023-02-13 09:14:54 +11003690
Dave Chinner89563e72023-02-13 09:14:54 +11003691
Dave Chinner8f7747a2023-02-13 09:14:55 +11003692 error = xfs_filestream_select_ag(ap, args, &blen);
Dave Chinner89563e72023-02-13 09:14:54 +11003693 if (error)
3694 return error;
Dave Chinnerf8f1ed12023-02-13 09:14:56 +11003695 ASSERT(args->pag);
Dave Chinner89563e72023-02-13 09:14:54 +11003696
Dave Chinner8f7747a2023-02-13 09:14:55 +11003697 /*
3698 * If we are in low space mode, then optimal allocation will fail so
3699 * prepare for minimal allocation and jump to the low space algorithm
3700 * immediately.
3701 */
3702 if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3703 args->minlen = ap->minlen;
Dave Chinnerf8f1ed12023-02-13 09:14:56 +11003704 ASSERT(args->fsbno == NULLFSBLOCK);
Dave Chinner8f7747a2023-02-13 09:14:55 +11003705 goto out_low_space;
3706 }
3707
3708 args->minlen = xfs_bmap_select_minlen(ap, args, blen);
Dave Chinnerf8f1ed12023-02-13 09:14:56 +11003709 if (ap->aeof)
Dave Chinner89563e72023-02-13 09:14:54 +11003710 error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
3711 true);
Dave Chinner89563e72023-02-13 09:14:54 +11003712
Dave Chinnerf8f1ed12023-02-13 09:14:56 +11003713 if (!error && args->fsbno == NULLFSBLOCK)
3714 error = xfs_alloc_vextent_near_bno(args, ap->blkno);
3715
3716out_low_space:
3717 /*
3718 * We are now done with the perag reference for the filestreams
3719 * association provided by xfs_filestream_select_ag(). Release it now as
3720 * we've either succeeded, had a fatal error or we are out of space and
3721 * need to do a full filesystem scan for free space which will take it's
3722 * own references.
3723 */
3724 xfs_perag_rele(args->pag);
3725 args->pag = NULL;
Dave Chinner89563e72023-02-13 09:14:54 +11003726 if (error || args->fsbno != NULLFSBLOCK)
3727 return error;
3728
3729 return xfs_bmap_btalloc_low_space(ap, args);
3730}
3731
Dave Chinner85843322023-02-13 09:14:53 +11003732static int
3733xfs_bmap_btalloc_best_length(
3734 struct xfs_bmalloca *ap,
3735 struct xfs_alloc_arg *args,
3736 int stripe_align)
3737{
Dave Chinner85843322023-02-13 09:14:53 +11003738 xfs_extlen_t blen = 0;
3739 int error;
3740
Dave Chinner89563e72023-02-13 09:14:54 +11003741 ap->blkno = XFS_INO_TO_FSB(args->mp, ap->ip->i_ino);
Dave Chinner85843322023-02-13 09:14:53 +11003742 xfs_bmap_adjacent(ap);
Dave Chinner85843322023-02-13 09:14:53 +11003743
3744 /*
3745 * Search for an allocation group with a single extent large enough for
3746 * the request. If one isn't found, then adjust the minimum allocation
3747 * size to the largest space found.
3748 */
Dave Chinner89563e72023-02-13 09:14:54 +11003749 error = xfs_bmap_btalloc_select_lengths(ap, args, &blen);
Dave Chinner85843322023-02-13 09:14:53 +11003750 if (error)
3751 return error;
3752
3753 /*
3754 * Don't attempt optimal EOF allocation if previous allocations barely
3755 * succeeded due to being near ENOSPC. It is highly unlikely we'll get
3756 * optimal or even aligned allocations in this case, so don't waste time
3757 * trying.
3758 */
3759 if (ap->aeof && !(ap->tp->t_flags & XFS_TRANS_LOWMODE)) {
Dave Chinner2a7f6d42023-02-13 09:14:53 +11003760 error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
Dave Chinner89563e72023-02-13 09:14:54 +11003761 false);
3762 if (error || args->fsbno != NULLFSBLOCK)
Dave Chinner85843322023-02-13 09:14:53 +11003763 return error;
3764 }
Dave Chinner85843322023-02-13 09:14:53 +11003765
Dave Chinner89563e72023-02-13 09:14:54 +11003766 error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3767 if (error || args->fsbno != NULLFSBLOCK)
Dave Chinner85843322023-02-13 09:14:53 +11003768 return error;
Dave Chinner89563e72023-02-13 09:14:54 +11003769
3770 return xfs_bmap_btalloc_low_space(ap, args);
Dave Chinner85843322023-02-13 09:14:53 +11003771}
3772
3773static int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003774xfs_bmap_btalloc(
Chandan Babu R30151962021-01-22 16:48:17 -08003775 struct xfs_bmalloca *ap)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003776{
Chandan Babu R30151962021-01-22 16:48:17 -08003777 struct xfs_mount *mp = ap->ip->i_mount;
Dave Chinner85843322023-02-13 09:14:53 +11003778 struct xfs_alloc_arg args = {
3779 .tp = ap->tp,
3780 .mp = mp,
3781 .fsbno = NULLFSBLOCK,
3782 .oinfo = XFS_RMAP_OINFO_SKIP_UPDATE,
3783 .minleft = ap->minleft,
3784 .wasdel = ap->wasdel,
3785 .resv = XFS_AG_RESV_NONE,
3786 .datatype = ap->datatype,
3787 .alignment = 1,
3788 .minalignslop = 0,
3789 };
Chandan Babu R30151962021-01-22 16:48:17 -08003790 xfs_fileoff_t orig_offset;
3791 xfs_extlen_t orig_length;
Chandan Babu R30151962021-01-22 16:48:17 -08003792 int error;
3793 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003794
Dave Chinnera99ebf42011-12-01 11:24:20 +00003795 ASSERT(ap->length);
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003796 orig_offset = ap->offset;
3797 orig_length = ap->length;
Dave Chinnera99ebf42011-12-01 11:24:20 +00003798
Chandan Babu R0961fdd2021-01-22 16:48:16 -08003799 stripe_align = xfs_bmap_compute_alignments(ap, &args);
Dave Chinner33177f052013-12-12 16:34:36 +11003800
Dave Chinner14b064c2011-01-27 12:16:28 +11003801 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner9bb54cb2018-06-07 07:54:02 -07003802 args.maxlen = min(ap->length, mp->m_ag_max_usable);
Dave Chinner36b6ad22023-02-11 04:10:06 +11003803
Dave Chinner89563e72023-02-13 09:14:54 +11003804 if ((ap->datatype & XFS_ALLOC_USERDATA) &&
3805 xfs_inode_is_filestream(ap->ip))
3806 error = xfs_bmap_btalloc_filestreams(ap, &args, stripe_align);
3807 else
3808 error = xfs_bmap_btalloc_best_length(ap, &args, stripe_align);
Dave Chinner36b6ad22023-02-11 04:10:06 +11003809 if (error)
3810 return error;
Chandan Babu R0961fdd2021-01-22 16:48:16 -08003811
Chandan Babu R07c72e52021-01-22 16:48:17 -08003812 if (args.fsbno != NULLFSBLOCK) {
3813 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3814 orig_length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003815 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003816 ap->blkno = NULLFSBLOCK;
3817 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 }
3819 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003820}
3821
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003822/* Trim extent to fit a logical block range. */
3823void
3824xfs_trim_extent(
3825 struct xfs_bmbt_irec *irec,
3826 xfs_fileoff_t bno,
3827 xfs_filblks_t len)
3828{
3829 xfs_fileoff_t distance;
3830 xfs_fileoff_t end = bno + len;
3831
3832 if (irec->br_startoff + irec->br_blockcount <= bno ||
3833 irec->br_startoff >= end) {
3834 irec->br_blockcount = 0;
3835 return;
3836 }
3837
3838 if (irec->br_startoff < bno) {
3839 distance = bno - irec->br_startoff;
3840 if (isnullstartblock(irec->br_startblock))
3841 irec->br_startblock = DELAYSTARTBLOCK;
3842 if (irec->br_startblock != DELAYSTARTBLOCK &&
3843 irec->br_startblock != HOLESTARTBLOCK)
3844 irec->br_startblock += distance;
3845 irec->br_startoff += distance;
3846 irec->br_blockcount -= distance;
3847 }
3848
3849 if (end < irec->br_startoff + irec->br_blockcount) {
3850 distance = irec->br_startoff + irec->br_blockcount - end;
3851 irec->br_blockcount -= distance;
3852 }
3853}
3854
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003856 * Trim the returned map to the required bounds
3857 */
3858STATIC void
3859xfs_bmapi_trim_map(
3860 struct xfs_bmbt_irec *mval,
3861 struct xfs_bmbt_irec *got,
3862 xfs_fileoff_t *bno,
3863 xfs_filblks_t len,
3864 xfs_fileoff_t obno,
3865 xfs_fileoff_t end,
3866 int n,
Dave Chinnere7d410a2022-04-21 10:46:09 +10003867 uint32_t flags)
Dave Chinneraef9a892011-09-18 20:40:44 +00003868{
3869 if ((flags & XFS_BMAPI_ENTIRE) ||
3870 got->br_startoff + got->br_blockcount <= obno) {
3871 *mval = *got;
3872 if (isnullstartblock(got->br_startblock))
3873 mval->br_startblock = DELAYSTARTBLOCK;
3874 return;
3875 }
3876
3877 if (obno > *bno)
3878 *bno = obno;
3879 ASSERT((*bno >= obno) || (n == 0));
3880 ASSERT(*bno < end);
3881 mval->br_startoff = *bno;
3882 if (isnullstartblock(got->br_startblock))
3883 mval->br_startblock = DELAYSTARTBLOCK;
3884 else
3885 mval->br_startblock = got->br_startblock +
3886 (*bno - got->br_startoff);
3887 /*
3888 * Return the minimum of what we got and what we asked for for
3889 * the length. We can use the len variable here because it is
3890 * modified below and we could have been there before coming
3891 * here if the first part of the allocation didn't overlap what
3892 * was asked for.
3893 */
3894 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3895 got->br_blockcount - (*bno - got->br_startoff));
3896 mval->br_state = got->br_state;
3897 ASSERT(mval->br_blockcount <= len);
3898 return;
3899}
3900
3901/*
3902 * Update and validate the extent map to return
3903 */
3904STATIC void
3905xfs_bmapi_update_map(
3906 struct xfs_bmbt_irec **map,
3907 xfs_fileoff_t *bno,
3908 xfs_filblks_t *len,
3909 xfs_fileoff_t obno,
3910 xfs_fileoff_t end,
3911 int *n,
Dave Chinnere7d410a2022-04-21 10:46:09 +10003912 uint32_t flags)
Dave Chinneraef9a892011-09-18 20:40:44 +00003913{
3914 xfs_bmbt_irec_t *mval = *map;
3915
3916 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3917 ((mval->br_startoff + mval->br_blockcount) <= end));
3918 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3919 (mval->br_startoff < obno));
3920
3921 *bno = mval->br_startoff + mval->br_blockcount;
3922 *len = end - *bno;
3923 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3924 /* update previous map with new information */
3925 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3926 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3927 ASSERT(mval->br_state == mval[-1].br_state);
3928 mval[-1].br_blockcount = mval->br_blockcount;
3929 mval[-1].br_state = mval->br_state;
3930 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3931 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3932 mval[-1].br_startblock != HOLESTARTBLOCK &&
3933 mval->br_startblock == mval[-1].br_startblock +
3934 mval[-1].br_blockcount &&
Christoph Hellwigc3a2f9f2018-07-11 22:26:01 -07003935 mval[-1].br_state == mval->br_state) {
Dave Chinneraef9a892011-09-18 20:40:44 +00003936 ASSERT(mval->br_startoff ==
3937 mval[-1].br_startoff + mval[-1].br_blockcount);
3938 mval[-1].br_blockcount += mval->br_blockcount;
3939 } else if (*n > 0 &&
3940 mval->br_startblock == DELAYSTARTBLOCK &&
3941 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3942 mval->br_startoff ==
3943 mval[-1].br_startoff + mval[-1].br_blockcount) {
3944 mval[-1].br_blockcount += mval->br_blockcount;
3945 mval[-1].br_state = mval->br_state;
3946 } else if (!((*n == 0) &&
3947 ((mval->br_startoff + mval->br_blockcount) <=
3948 obno))) {
3949 mval++;
3950 (*n)++;
3951 }
3952 *map = mval;
3953}
3954
3955/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003956 * Map file blocks to filesystem blocks without allocation.
3957 */
3958int
3959xfs_bmapi_read(
3960 struct xfs_inode *ip,
3961 xfs_fileoff_t bno,
3962 xfs_filblks_t len,
3963 struct xfs_bmbt_irec *mval,
3964 int *nmap,
Dave Chinnere7d410a2022-04-21 10:46:09 +10003965 uint32_t flags)
Dave Chinner5c8ed202011-09-18 20:40:45 +00003966{
3967 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig4b516ff2020-05-14 14:06:41 -07003968 int whichfork = xfs_bmapi_whichfork(flags);
Darrick J. Wong732436e2022-07-09 10:56:05 -07003969 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003970 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003971 xfs_fileoff_t obno;
3972 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003973 struct xfs_iext_cursor icur;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003974 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003975 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003976 int n = 0;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003977
3978 ASSERT(*nmap >= 1);
Christoph Hellwig1a1c57b2020-05-14 14:06:40 -07003979 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE)));
Matthew Wilcox (Oracle)3fed24f2024-02-19 15:41:12 +00003980 xfs_assert_ilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003981
Darrick J. Wong1196f3f2024-02-22 12:31:51 -08003982 if (WARN_ON_ONCE(!ifp)) {
3983 xfs_bmap_mark_sick(ip, whichfork);
Christoph Hellwig4b516ff2020-05-14 14:06:41 -07003984 return -EFSCORRUPTED;
Darrick J. Wong1196f3f2024-02-22 12:31:51 -08003985 }
Christoph Hellwig4b516ff2020-05-14 14:06:41 -07003986
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07003987 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
Darrick J. Wong1196f3f2024-02-22 12:31:51 -08003988 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
3989 xfs_bmap_mark_sick(ip, whichfork);
Dave Chinner24513372014-06-25 14:58:08 +10003990 return -EFSCORRUPTED;
Darrick J. Wong1196f3f2024-02-22 12:31:51 -08003991 }
Dave Chinner5c8ed202011-09-18 20:40:45 +00003992
Dave Chinner75c8c50f2021-08-18 18:46:53 -07003993 if (xfs_is_shutdown(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003994 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003995
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003996 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003997
Christoph Hellwig862a8042021-04-13 11:15:09 -07003998 error = xfs_iread_extents(NULL, ip, whichfork);
3999 if (error)
4000 return error;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004001
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004002 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11004003 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004004 end = bno + len;
4005 obno = bno;
4006
4007 while (bno < end && n < *nmap) {
4008 /* Reading past eof, act as though there's a hole up to end. */
4009 if (eof)
4010 got.br_startoff = end;
4011 if (got.br_startoff > bno) {
4012 /* Reading in a hole. */
4013 mval->br_startoff = bno;
4014 mval->br_startblock = HOLESTARTBLOCK;
4015 mval->br_blockcount =
4016 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4017 mval->br_state = XFS_EXT_NORM;
4018 bno += mval->br_blockcount;
4019 len -= mval->br_blockcount;
4020 mval++;
4021 n++;
4022 continue;
4023 }
4024
4025 /* set up the extent map to return. */
4026 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4027 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4028
4029 /* If we're done, stop now. */
4030 if (bno >= end || n >= *nmap)
4031 break;
4032
4033 /* Else go on to the next record. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004034 if (!xfs_iext_next_extent(ifp, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11004035 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004036 }
4037 *nmap = n;
4038 return 0;
4039}
4040
Brian Fosterf65e6fad2017-03-08 09:58:08 -08004041/*
4042 * Add a delayed allocation extent to an inode. Blocks are reserved from the
4043 * global pool and the extent inserted into the inode in-core extent tree.
4044 *
4045 * On entry, got refers to the first extent beyond the offset of the extent to
4046 * allocate or eof is specified if no such extent exists. On return, got refers
4047 * to the extent record that was inserted to the inode fork.
4048 *
4049 * Note that the allocated extent may have been merged with contiguous extents
4050 * during insertion into the inode fork. Thus, got does not reflect the current
4051 * state of the inode fork on return. If necessary, the caller can use lastx to
4052 * look up the updated record in the inode fork.
4053 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10004054int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004055xfs_bmapi_reserve_delalloc(
4056 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004057 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11004058 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004059 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11004060 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004061 struct xfs_bmbt_irec *got,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004062 struct xfs_iext_cursor *icur,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004063 int eof)
4064{
4065 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wong732436e2022-07-09 10:56:05 -07004066 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004067 xfs_extlen_t alen;
4068 xfs_extlen_t indlen;
Christoph Hellwigdc1b17a2024-04-22 13:20:13 +02004069 uint64_t fdblocks;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004070 int error;
Darrick J. Wong288e1f62024-06-19 10:32:44 -07004071 xfs_fileoff_t aoff;
4072 bool use_cowextszhint =
4073 whichfork == XFS_COW_FORK && !prealloc;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004074
Darrick J. Wong288e1f62024-06-19 10:32:44 -07004075retry:
Brian Foster974ae922016-11-28 14:57:42 +11004076 /*
4077 * Cap the alloc length. Keep track of prealloc so we know whether to
4078 * tag the inode before we return.
4079 */
Darrick J. Wong288e1f62024-06-19 10:32:44 -07004080 aoff = off;
Chandan Babu R95f0b952021-08-09 12:05:22 +05304081 alen = XFS_FILBLKS_MIN(len + prealloc, XFS_MAX_BMBT_EXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004082 if (!eof)
4083 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11004084 if (prealloc && alen >= len)
4085 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004086
Darrick J. Wong288e1f62024-06-19 10:32:44 -07004087 /*
4088 * If we're targetting the COW fork but aren't creating a speculative
4089 * posteof preallocation, try to expand the reservation to align with
4090 * the COW extent size hint if there's sufficient free space.
4091 *
4092 * Unlike the data fork, the CoW cancellation functions will free all
4093 * the reservations at inactivation, so we don't require that every
4094 * delalloc reservation have a dirty pagecache.
4095 */
4096 if (use_cowextszhint) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004097 struct xfs_bmbt_irec prev;
Shan Hai6ca30722018-01-23 13:56:11 -08004098 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004099
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004100 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004101 prev.br_startoff = NULLFILEOFF;
4102
Shan Hai6ca30722018-01-23 13:56:11 -08004103 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004104 1, 0, &aoff, &alen);
4105 ASSERT(!error);
4106 }
4107
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004108 /*
4109 * Make a transaction-less quota reservation for delayed allocation
4110 * blocks. This number gets adjusted later. We return if we haven't
4111 * allocated blocks already inside this loop.
4112 */
Darrick J. Wong85546502021-01-22 16:48:34 -08004113 error = xfs_quota_reserve_blkres(ip, alen);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004114 if (error)
Darrick J. Wong288e1f62024-06-19 10:32:44 -07004115 goto out;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004116
4117 /*
4118 * Split changing sb for alen and indlen since they could be coming
4119 * from different places.
4120 */
4121 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4122 ASSERT(indlen > 0);
4123
Christoph Hellwigdc1b17a2024-04-22 13:20:13 +02004124 fdblocks = indlen;
4125 if (XFS_IS_REALTIME_INODE(ip)) {
4126 error = xfs_dec_frextents(mp, xfs_rtb_to_rtx(mp, alen));
4127 if (error)
4128 goto out_unreserve_quota;
4129 } else {
4130 fdblocks += alen;
4131 }
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004132
Christoph Hellwigdc1b17a2024-04-22 13:20:13 +02004133 error = xfs_dec_fdblocks(mp, fdblocks, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004134 if (error)
Christoph Hellwigdc1b17a2024-04-22 13:20:13 +02004135 goto out_unreserve_frextents;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004136
4137 ip->i_delayed_blks += alen;
Christoph Hellwig7099bd02024-04-22 13:20:15 +02004138 xfs_mod_delalloc(ip, alen, indlen);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004139
4140 got->br_startoff = aoff;
4141 got->br_startblock = nullstartblock(indlen);
4142 got->br_blockcount = alen;
4143 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004144
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004145 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004146
Brian Foster974ae922016-11-28 14:57:42 +11004147 /*
4148 * Tag the inode if blocks were preallocated. Note that COW fork
4149 * preallocation can occur at the start or end of the extent, even when
4150 * prealloc == 0, so we must also check the aligned offset and length.
4151 */
4152 if (whichfork == XFS_DATA_FORK && prealloc)
4153 xfs_inode_set_eofblocks_tag(ip);
4154 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4155 xfs_inode_set_cowblocks_tag(ip);
4156
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004157 return 0;
4158
Christoph Hellwigdc1b17a2024-04-22 13:20:13 +02004159out_unreserve_frextents:
4160 if (XFS_IS_REALTIME_INODE(ip))
4161 xfs_add_frextents(mp, xfs_rtb_to_rtx(mp, alen));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004162out_unreserve_quota:
4163 if (XFS_IS_QUOTA_ON(mp))
Darrick J. Wong85546502021-01-22 16:48:34 -08004164 xfs_quota_unreserve_blkres(ip, alen);
Darrick J. Wong288e1f62024-06-19 10:32:44 -07004165out:
4166 if (error == -ENOSPC || error == -EDQUOT) {
4167 trace_xfs_delalloc_enospc(ip, off, len);
4168
4169 if (prealloc || use_cowextszhint) {
4170 /* retry without any preallocation */
4171 use_cowextszhint = false;
4172 prealloc = 0;
4173 goto retry;
4174 }
4175 }
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004176 return error;
4177}
4178
Dave Chinnercf11da92014-07-15 07:08:24 +10004179static int
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004180xfs_bmap_alloc_userdata(
4181 struct xfs_bmalloca *bma)
4182{
4183 struct xfs_mount *mp = bma->ip->i_mount;
4184 int whichfork = xfs_bmapi_whichfork(bma->flags);
4185 int error;
4186
4187 /*
4188 * Set the data type being allocated. For the data fork, the first data
4189 * in the file is treated differently to all other allocations. For the
4190 * attribute fork, we only need to ensure the allocated range is not on
4191 * the busy list.
4192 */
4193 bma->datatype = XFS_ALLOC_NOBUSY;
Darrick J. Wongddfdd532022-12-01 09:36:16 -08004194 if (whichfork == XFS_DATA_FORK || whichfork == XFS_COW_FORK) {
Christoph Hellwigc34d5702019-10-30 12:25:00 -07004195 bma->datatype |= XFS_ALLOC_USERDATA;
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004196 if (bma->offset == 0)
4197 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004198
4199 if (mp->m_dalign && bma->length >= mp->m_dalign) {
4200 error = xfs_bmap_isaeof(bma, whichfork);
4201 if (error)
4202 return error;
4203 }
4204
4205 if (XFS_IS_REALTIME_INODE(bma->ip))
4206 return xfs_bmap_rtalloc(bma);
4207 }
4208
Chandan Babu R30151962021-01-22 16:48:17 -08004209 if (unlikely(XFS_TEST_ERROR(false, mp,
4210 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
4211 return xfs_bmap_exact_minlen_extent_alloc(bma);
4212
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004213 return xfs_bmap_btalloc(bma);
4214}
4215
4216static int
Dave Chinnercf11da92014-07-15 07:08:24 +10004217xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004218 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004219{
4220 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004221 int whichfork = xfs_bmapi_whichfork(bma->flags);
Darrick J. Wong732436e2022-07-09 10:56:05 -07004222 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004223 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004224
Dave Chinnera99ebf42011-12-01 11:24:20 +00004225 ASSERT(bma->length > 0);
Christoph Hellwig2a9b99d2024-04-29 08:15:25 +02004226 ASSERT(bma->length <= XFS_MAX_BMBT_EXTLEN);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004227
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004228 if (bma->flags & XFS_BMAPI_CONTIG)
4229 bma->minlen = bma->length;
4230 else
4231 bma->minlen = 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004232
Chandan Babu R30151962021-01-22 16:48:17 -08004233 if (bma->flags & XFS_BMAPI_METADATA) {
4234 if (unlikely(XFS_TEST_ERROR(false, mp,
4235 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
4236 error = xfs_bmap_exact_minlen_extent_alloc(bma);
4237 else
4238 error = xfs_bmap_btalloc(bma);
4239 } else {
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004240 error = xfs_bmap_alloc_userdata(bma);
Chandan Babu R30151962021-01-22 16:48:17 -08004241 }
Christoph Hellwig6773da82024-04-29 08:15:21 +02004242 if (error)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004243 return error;
Christoph Hellwig6773da82024-04-29 08:15:21 +02004244 if (bma->blkno == NULLFSBLOCK)
4245 return -ENOSPC;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004246
Christoph Hellwig04c609e2024-04-29 08:15:23 +02004247 if (WARN_ON_ONCE(!xfs_valid_startblock(bma->ip, bma->blkno))) {
4248 xfs_bmap_mark_sick(bma->ip, whichfork);
4249 return -EFSCORRUPTED;
4250 }
4251
Christoph Hellwigfd638f12019-10-30 12:25:00 -07004252 if (bma->flags & XFS_BMAPI_ZERO) {
4253 error = xfs_zero_extent(bma->ip, bma->blkno, bma->length);
4254 if (error)
4255 return error;
4256 }
4257
Christoph Hellwigac1e0672021-04-13 11:15:11 -07004258 if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur)
Dave Chinner29c8d17a2011-09-18 20:41:00 +00004259 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004260 /*
4261 * Bump the number of extents we've allocated
4262 * in this call.
4263 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004264 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004265
Christoph Hellwige9e66df2024-02-22 12:35:13 -08004266 if (bma->cur && bma->wasdel)
4267 bma->cur->bc_flags |= XFS_BTREE_BMBT_WASDEL;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004268
Dave Chinner963c30c2011-09-18 20:40:59 +00004269 bma->got.br_startoff = bma->offset;
4270 bma->got.br_startblock = bma->blkno;
4271 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004272 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004273
Darrick J. Wonga5949d32020-05-23 09:43:31 -07004274 if (bma->flags & XFS_BMAPI_PREALLOC)
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004275 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004276
Christoph Hellwigc6534242011-09-18 20:41:05 +00004277 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004278 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004279 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004280 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004281 whichfork, &bma->icur, &bma->cur, &bma->got,
Brian Foster92f9da32018-07-11 22:26:28 -07004282 &bma->logflags, bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004283 if (error)
4284 return error;
4285
4286 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004287 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4288 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4289 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004290 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004291 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004292
Dave Chinner963c30c2011-09-18 20:40:59 +00004293 ASSERT(bma->got.br_startoff <= bma->offset);
4294 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4295 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004296 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4297 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004298 return 0;
4299}
4300
Dave Chinnerb447fe52011-09-18 20:40:51 +00004301STATIC int
4302xfs_bmapi_convert_unwritten(
4303 struct xfs_bmalloca *bma,
4304 struct xfs_bmbt_irec *mval,
4305 xfs_filblks_t len,
Dave Chinnere7d410a2022-04-21 10:46:09 +10004306 uint32_t flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004307{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004308 int whichfork = xfs_bmapi_whichfork(flags);
Darrick J. Wong732436e2022-07-09 10:56:05 -07004309 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004310 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004311 int error;
4312
Dave Chinnerb447fe52011-09-18 20:40:51 +00004313 /* check if we need to do unwritten->real conversion */
4314 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4315 (flags & XFS_BMAPI_PREALLOC))
4316 return 0;
4317
4318 /* check if we need to do real->unwritten conversion */
4319 if (mval->br_state == XFS_EXT_NORM &&
4320 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4321 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4322 return 0;
4323
4324 /*
4325 * Modify (by adding) the state flag, if writing.
4326 */
4327 ASSERT(mval->br_blockcount <= len);
Christoph Hellwigac1e0672021-04-13 11:15:11 -07004328 if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur) {
Dave Chinner29c8d17a2011-09-18 20:41:00 +00004329 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004330 bma->ip, whichfork);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004331 }
4332 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4333 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4334
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004335 /*
4336 * Before insertion into the bmbt, zero the range being converted
4337 * if required.
4338 */
4339 if (flags & XFS_BMAPI_ZERO) {
4340 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4341 mval->br_blockcount);
4342 if (error)
4343 return error;
4344 }
4345
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004346 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
Brian Foster92f9da32018-07-11 22:26:28 -07004347 &bma->icur, &bma->cur, mval, &tmp_logflags);
Brian Foster2e588a462015-06-01 07:15:23 +10004348 /*
4349 * Log the inode core unconditionally in the unwritten extent conversion
4350 * path because the conversion might not have done so (e.g., if the
4351 * extent count hasn't changed). We need to make sure the inode is dirty
4352 * in the transaction for the sake of fsync(), even if nothing has
4353 * changed, because fsync() will not force the log for this transaction
4354 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004355 *
4356 * Note: If we're only converting cow fork extents, there aren't
4357 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a462015-06-01 07:15:23 +10004358 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004359 if (whichfork != XFS_COW_FORK)
4360 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004361 if (error)
4362 return error;
4363
4364 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004365 * Update our extent pointer, given that
4366 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4367 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004368 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004369 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004370
4371 /*
4372 * We may have combined previously unwritten space with written space,
4373 * so generate another request.
4374 */
4375 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004376 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004377 return 0;
4378}
4379
Dave Chinnerd5753842023-02-11 04:09:06 +11004380xfs_extlen_t
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004381xfs_bmapi_minleft(
4382 struct xfs_trans *tp,
4383 struct xfs_inode *ip,
4384 int fork)
4385{
Darrick J. Wong732436e2022-07-09 10:56:05 -07004386 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, fork);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004387
Dave Chinner692b6cd2023-02-11 04:11:06 +11004388 if (tp && tp->t_highest_agno != NULLAGNUMBER)
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004389 return 0;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004390 if (ifp->if_format != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004391 return 1;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004392 return be16_to_cpu(ifp->if_broot->bb_level) + 1;
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004393}
4394
4395/*
4396 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4397 * a case where the data is changed, there's an error, and it's not logged so we
4398 * don't shutdown when we should. Don't bother logging extents/btree changes if
4399 * we converted to the other format.
4400 */
4401static void
4402xfs_bmapi_finish(
4403 struct xfs_bmalloca *bma,
4404 int whichfork,
4405 int error)
4406{
Darrick J. Wong732436e2022-07-09 10:56:05 -07004407 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004408
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004409 if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004410 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004411 bma->logflags &= ~xfs_ilog_fext(whichfork);
4412 else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004413 ifp->if_format != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004414 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4415
4416 if (bma->logflags)
4417 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4418 if (bma->cur)
4419 xfs_btree_del_cursor(bma->cur, error);
4420}
4421
Christoph Hellwig44032802011-09-18 20:40:48 +00004422/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004423 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4424 * extent state if necessary. Details behaviour is controlled by the flags
4425 * parameter. Only allocates blocks from a single allocation group, to avoid
4426 * locking problems.
Christoph Hellwig6773da82024-04-29 08:15:21 +02004427 *
4428 * Returns 0 on success and places the extent mappings in mval. nmaps is used
4429 * as an input/output parameter where the caller specifies the maximum number
4430 * of mappings that may be returned and xfs_bmapi_write passes back the number
4431 * of mappings (including existing mappings) it found.
4432 *
4433 * Returns a negative error code on failure, including -ENOSPC when it could not
4434 * allocate any blocks and -ENOSR when it did allocate blocks to convert a
4435 * delalloc range, but those blocks were before the passed in range.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004437int
4438xfs_bmapi_write(
4439 struct xfs_trans *tp, /* transaction pointer */
4440 struct xfs_inode *ip, /* incore inode */
4441 xfs_fileoff_t bno, /* starting file offs. mapped */
4442 xfs_filblks_t len, /* length to map in file */
Dave Chinnere7d410a2022-04-21 10:46:09 +10004443 uint32_t flags, /* XFS_BMAPI_... */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004444 xfs_extlen_t total, /* total blocks needed */
4445 struct xfs_bmbt_irec *mval, /* output: map values */
Brian Foster6e702a52018-07-11 22:26:12 -07004446 int *nmap) /* i/o: mval size/count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447{
Darrick J. Wong4b0bce32019-03-19 08:16:22 -07004448 struct xfs_bmalloca bma = {
4449 .tp = tp,
4450 .ip = ip,
4451 .total = total,
4452 };
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004453 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004454 int whichfork = xfs_bmapi_whichfork(flags);
Darrick J. Wong732436e2022-07-09 10:56:05 -07004455 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004456 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004457 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004458 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004459 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004460 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004461
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004463 xfs_fileoff_t orig_bno; /* original block number value */
4464 int orig_flags; /* original flags arg value */
4465 xfs_filblks_t orig_len; /* original value of len arg */
4466 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4467 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468
4469 orig_bno = bno;
4470 orig_len = len;
4471 orig_flags = flags;
4472 orig_mval = mval;
4473 orig_nmap = *nmap;
4474#endif
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004475
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004477 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
Christoph Hellwig26b91c72019-02-18 09:38:48 -08004478 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004479 ASSERT(len > 0);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004480 ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL);
Matthew Wilcox (Oracle)3fed24f2024-02-19 15:41:12 +00004481 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004482 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004483
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004484 /* zeroing is for currently only for data extents, not metadata */
4485 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4486 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4487 /*
4488 * we can allocate unwritten extents or pre-zero allocated blocks,
4489 * but it makes no sense to do both at once. This would result in
4490 * zeroing the unwritten extent twice, but it still being an
4491 * unwritten extent....
4492 */
4493 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4494 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4495
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004496 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
Darrick J. Wonga71895c2019-11-11 12:53:22 -08004497 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Darrick J. Wong1196f3f2024-02-22 12:31:51 -08004498 xfs_bmap_mark_sick(ip, whichfork);
Dave Chinner24513372014-06-25 14:58:08 +10004499 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004501
Dave Chinner75c8c50f2021-08-18 18:46:53 -07004502 if (xfs_is_shutdown(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004503 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004504
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004505 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004506
Christoph Hellwig862a8042021-04-13 11:15:09 -07004507 error = xfs_iread_extents(tp, ip, whichfork);
4508 if (error)
4509 goto error0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004510
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004511 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004512 eof = true;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004513 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004514 bma.prev.br_startoff = NULLFILEOFF;
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004515 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004516
Brian Foster627209f2019-02-01 09:14:23 -08004517 n = 0;
4518 end = bno + len;
4519 obno = bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004521 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004522
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004523 /* in hole or beyond EOF? */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004524 if (eof || bma.got.br_startoff > bno) {
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004525 /*
4526 * CoW fork conversions should /never/ hit EOF or
4527 * holes. There should always be something for us
4528 * to work on.
4529 */
4530 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4531 (flags & XFS_BMAPI_COWFORK)));
4532
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004533 need_alloc = true;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004534 } else if (isnullstartblock(bma.got.br_startblock)) {
4535 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004536 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004537
4538 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 * First, deal with the hole before the allocated space
4540 * that we found, if any.
4541 */
Christoph Hellwig26b91c72019-02-18 09:38:48 -08004542 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004543 bma.eof = eof;
4544 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4545 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004546 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004547 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004548
Dave Chinnera99ebf42011-12-01 11:24:20 +00004549 /*
4550 * There's a 32/64 bit type mismatch between the
4551 * allocation length request (which can be 64 bits in
4552 * length) and the bma length request, which is
4553 * xfs_extlen_t and therefore 32 bits. Hence we have to
Christoph Hellwig9d069602024-04-29 08:15:24 +02004554 * be careful and do the min() using the larger type to
4555 * avoid overflows.
Dave Chinnera99ebf42011-12-01 11:24:20 +00004556 */
Christoph Hellwig9d069602024-04-29 08:15:24 +02004557 bma.length = XFS_FILBLKS_MIN(len, XFS_MAX_BMBT_EXTLEN);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004558
Christoph Hellwig2a9b99d2024-04-29 08:15:25 +02004559 if (wasdelay) {
Christoph Hellwig21255af2024-04-29 08:15:28 +02004560 bma.length = XFS_FILBLKS_MIN(bma.length,
4561 bma.got.br_blockcount -
4562 (bno - bma.got.br_startoff));
Christoph Hellwig2a9b99d2024-04-29 08:15:25 +02004563 } else {
4564 if (!eof)
4565 bma.length = XFS_FILBLKS_MIN(bma.length,
4566 bma.got.br_startoff - bno);
4567 }
4568
Dave Chinnera99ebf42011-12-01 11:24:20 +00004569 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004570 error = xfs_bmapi_allocate(&bma);
Christoph Hellwig6773da82024-04-29 08:15:21 +02004571 if (error) {
4572 /*
4573 * If we already allocated space in a previous
4574 * iteration return what we go so far when
4575 * running out of space.
4576 */
4577 if (error == -ENOSPC && bma.nallocs)
4578 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 goto error0;
Christoph Hellwig6773da82024-04-29 08:15:21 +02004580 }
Darrick J. Wong174edb02016-10-03 09:11:39 -07004581
4582 /*
4583 * If this is a CoW allocation, record the data in
4584 * the refcount btree for orphan recovery.
4585 */
Darrick J. Wong74b4c5d2019-08-26 17:06:04 -07004586 if (whichfork == XFS_COW_FORK)
4587 xfs_refcount_alloc_cow_extent(tp, bma.blkno,
4588 bma.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004590
Dave Chinneraef9a892011-09-18 20:40:44 +00004591 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004592 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4593 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
Dave Chinnerb447fe52011-09-18 20:40:51 +00004595 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004596 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004597 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004598 continue;
4599 if (error)
4600 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
Dave Chinneraef9a892011-09-18 20:40:44 +00004602 /* update the extent map to return */
4603 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4604
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 /*
4606 * If we're done, stop now. Stop when we've allocated
4607 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4608 * the transaction may get too big.
4609 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004610 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004612
4613 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004614 bma.prev = bma.got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004615 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004616 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004618
Christoph Hellwigb101e332019-02-15 08:02:47 -08004619 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4620 whichfork);
4621 if (error)
4622 goto error0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004623
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004624 ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwigdaf83962020-05-18 10:27:22 -07004625 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork));
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004626 xfs_bmapi_finish(&bma, whichfork, 0);
4627 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
Christoph Hellwig6773da82024-04-29 08:15:21 +02004628 orig_nmap, n);
4629
4630 /*
4631 * When converting delayed allocations, xfs_bmapi_allocate ignores
4632 * the passed in bno and always converts from the start of the found
4633 * delalloc extent.
4634 *
4635 * To avoid a successful return with *nmap set to 0, return the magic
4636 * -ENOSR error code for this particular case so that the caller can
4637 * handle it.
4638 */
4639 if (!n) {
4640 ASSERT(bma.nallocs >= *nmap);
4641 return -ENOSR;
4642 }
4643 *nmap = n;
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004644 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645error0:
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004646 xfs_bmapi_finish(&bma, whichfork, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 return error;
4648}
4649
Brian Foster627209f2019-02-01 09:14:23 -08004650/*
4651 * Convert an existing delalloc extent to real blocks based on file offset. This
4652 * attempts to allocate the entire delalloc extent and may require multiple
4653 * invocations to allocate the target offset if a large enough physical extent
4654 * is not available.
4655 */
Zhang Yi2e083712024-04-25 21:13:29 +08004656static int
4657xfs_bmapi_convert_one_delalloc(
Brian Foster627209f2019-02-01 09:14:23 -08004658 struct xfs_inode *ip,
Brian Foster627209f2019-02-01 09:14:23 -08004659 int whichfork,
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004660 xfs_off_t offset,
4661 struct iomap *iomap,
Christoph Hellwig491ce612019-02-15 08:02:49 -08004662 unsigned int *seq)
Brian Foster627209f2019-02-01 09:14:23 -08004663{
Darrick J. Wong732436e2022-07-09 10:56:05 -07004664 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004665 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004666 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004667 struct xfs_bmalloca bma = { NULL };
Darrick J. Wongaf952ae2019-12-16 11:14:09 -08004668 uint16_t flags = 0;
Christoph Hellwig491ce612019-02-15 08:02:49 -08004669 struct xfs_trans *tp;
Brian Foster627209f2019-02-01 09:14:23 -08004670 int error;
Brian Foster627209f2019-02-01 09:14:23 -08004671
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004672 if (whichfork == XFS_COW_FORK)
4673 flags |= IOMAP_F_SHARED;
4674
Christoph Hellwig491ce612019-02-15 08:02:49 -08004675 /*
4676 * Space for the extent and indirect blocks was reserved when the
4677 * delalloc extent was created so there's no need to do so here.
4678 */
4679 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4680 XFS_TRANS_RESERVE, &tp);
4681 if (error)
4682 return error;
4683
4684 xfs_ilock(ip, XFS_ILOCK_EXCL);
Chandan Babu R4f86bb42022-03-09 07:49:36 +00004685 xfs_trans_ijoin(tp, ip, 0);
Chandan Babu R727e1ac2021-01-22 16:48:11 -08004686
Christoph Hellwig25576c52024-05-02 09:33:55 +02004687 error = xfs_iext_count_extend(tp, ip, whichfork,
Chandan Babu R727e1ac2021-01-22 16:48:11 -08004688 XFS_IEXT_ADD_NOSPLIT_CNT);
4689 if (error)
4690 goto out_trans_cancel;
4691
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004692 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4693 bma.got.br_startoff > offset_fsb) {
4694 /*
4695 * No extent found in the range we are trying to convert. This
4696 * should only happen for the COW fork, where another thread
4697 * might have moved the extent to the data fork in the meantime.
4698 */
4699 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004700 error = -EAGAIN;
4701 goto out_trans_cancel;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004702 }
Brian Foster627209f2019-02-01 09:14:23 -08004703
4704 /*
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004705 * If we find a real extent here we raced with another thread converting
4706 * the extent. Just return the real extent at this offset.
Brian Foster627209f2019-02-01 09:14:23 -08004707 */
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004708 if (!isnullstartblock(bma.got.br_startblock)) {
Dave Chinner304a68b2022-11-29 09:09:17 +11004709 xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
4710 xfs_iomap_inode_sequence(ip, flags));
Zhang Yifc8d0ba2024-04-25 21:13:28 +08004711 if (seq)
4712 *seq = READ_ONCE(ifp->if_seq);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004713 goto out_trans_cancel;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004714 }
4715
4716 bma.tp = tp;
4717 bma.ip = ip;
4718 bma.wasdel = true;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004719 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
Darrick J. Wonga5949d32020-05-23 09:43:31 -07004720
4721 /*
Christoph Hellwig2a9b99d2024-04-29 08:15:25 +02004722 * Always allocate convert from the start of the delalloc extent even if
4723 * that is outside the passed in range to create large contiguous
4724 * extents on disk.
4725 */
4726 bma.offset = bma.got.br_startoff;
4727 bma.length = bma.got.br_blockcount;
4728
4729 /*
Darrick J. Wonga5949d32020-05-23 09:43:31 -07004730 * When we're converting the delalloc reservations backing dirty pages
4731 * in the page cache, we must be careful about how we create the new
4732 * extents:
4733 *
4734 * New CoW fork extents are created unwritten, turned into real extents
4735 * when we're about to write the data to disk, and mapped into the data
4736 * fork after the write finishes. End of story.
4737 *
4738 * New data fork extents must be mapped in as unwritten and converted
4739 * to real extents after the write succeeds to avoid exposing stale
4740 * disk contents if we crash.
4741 */
4742 bma.flags = XFS_BMAPI_PREALLOC;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004743 if (whichfork == XFS_COW_FORK)
Darrick J. Wonga5949d32020-05-23 09:43:31 -07004744 bma.flags |= XFS_BMAPI_COWFORK;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004745
4746 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4747 bma.prev.br_startoff = NULLFILEOFF;
4748
4749 error = xfs_bmapi_allocate(&bma);
4750 if (error)
4751 goto out_finish;
4752
Christoph Hellwig125851a2019-02-15 08:02:49 -08004753 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4754 XFS_STATS_INC(mp, xs_xstrat_quick);
4755
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004756 ASSERT(!isnullstartblock(bma.got.br_startblock));
Dave Chinner304a68b2022-11-29 09:09:17 +11004757 xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
4758 xfs_iomap_inode_sequence(ip, flags));
Zhang Yifc8d0ba2024-04-25 21:13:28 +08004759 if (seq)
4760 *seq = READ_ONCE(ifp->if_seq);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004761
Darrick J. Wong74b4c5d2019-08-26 17:06:04 -07004762 if (whichfork == XFS_COW_FORK)
4763 xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004764
4765 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4766 whichfork);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004767 if (error)
4768 goto out_finish;
4769
4770 xfs_bmapi_finish(&bma, whichfork, 0);
4771 error = xfs_trans_commit(tp);
4772 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4773 return error;
4774
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004775out_finish:
4776 xfs_bmapi_finish(&bma, whichfork, error);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004777out_trans_cancel:
4778 xfs_trans_cancel(tp);
4779 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Brian Foster627209f2019-02-01 09:14:23 -08004780 return error;
4781}
4782
Zhang Yi2e083712024-04-25 21:13:29 +08004783/*
4784 * Pass in a dellalloc extent and convert it to real extents, return the real
4785 * extent that maps offset_fsb in iomap.
4786 */
4787int
4788xfs_bmapi_convert_delalloc(
4789 struct xfs_inode *ip,
4790 int whichfork,
4791 loff_t offset,
4792 struct iomap *iomap,
4793 unsigned int *seq)
4794{
4795 int error;
4796
4797 /*
4798 * Attempt to allocate whatever delalloc extent currently backs offset
4799 * and put the result into iomap. Allocate in a loop because it may
4800 * take several attempts to allocate real blocks for a contiguous
4801 * delalloc extent if free space is sufficiently fragmented.
4802 */
4803 do {
4804 error = xfs_bmapi_convert_one_delalloc(ip, whichfork, offset,
4805 iomap, seq);
4806 if (error)
4807 return error;
4808 } while (iomap->offset + iomap->length <= offset);
4809
4810 return 0;
4811}
4812
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004813int
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004814xfs_bmapi_remap(
4815 struct xfs_trans *tp,
4816 struct xfs_inode *ip,
4817 xfs_fileoff_t bno,
4818 xfs_filblks_t len,
4819 xfs_fsblock_t startblock,
Dave Chinnere7d410a2022-04-21 10:46:09 +10004820 uint32_t flags)
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004821{
4822 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004823 struct xfs_ifork *ifp;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004824 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004825 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004826 struct xfs_iext_cursor icur;
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004827 int whichfork = xfs_bmapi_whichfork(flags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004828 int logflags = 0, error;
4829
Darrick J. Wong732436e2022-07-09 10:56:05 -07004830 ifp = xfs_ifork_ptr(ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004831 ASSERT(len > 0);
Chandan Babu R95f0b952021-08-09 12:05:22 +05304832 ASSERT(len <= (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN);
Matthew Wilcox (Oracle)3fed24f2024-02-19 15:41:12 +00004833 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
Darrick J. Wong7644bd92018-05-14 06:34:35 -07004834 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4835 XFS_BMAPI_NORMAP)));
4836 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4837 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004838
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004839 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
Darrick J. Wonga71895c2019-11-11 12:53:22 -08004840 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Darrick J. Wong1196f3f2024-02-22 12:31:51 -08004841 xfs_bmap_mark_sick(ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004842 return -EFSCORRUPTED;
4843 }
4844
Dave Chinner75c8c50f2021-08-18 18:46:53 -07004845 if (xfs_is_shutdown(mp))
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004846 return -EIO;
4847
Christoph Hellwig862a8042021-04-13 11:15:09 -07004848 error = xfs_iread_extents(tp, ip, whichfork);
4849 if (error)
4850 return error;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004851
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004852 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004853 /* make sure we only reflink into a hole. */
4854 ASSERT(got.br_startoff > bno);
4855 ASSERT(got.br_startoff - bno >= len);
4856 }
4857
Christoph Hellwig6e73a542021-03-29 11:11:40 -07004858 ip->i_nblocks += len;
Christoph Hellwig90fa22d2024-08-27 07:03:21 +02004859 ip->i_delayed_blks -= len; /* see xfs_bmap_defer_add */
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004860 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004861
Christoph Hellwige9e66df2024-02-22 12:35:13 -08004862 if (ifp->if_format == XFS_DINODE_FMT_BTREE)
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004863 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004864
4865 got.br_startoff = bno;
4866 got.br_startblock = startblock;
4867 got.br_blockcount = len;
Darrick J. Wong7644bd92018-05-14 06:34:35 -07004868 if (flags & XFS_BMAPI_PREALLOC)
4869 got.br_state = XFS_EXT_UNWRITTEN;
4870 else
4871 got.br_state = XFS_EXT_NORM;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004872
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004873 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
Brian Foster92f9da32018-07-11 22:26:28 -07004874 &cur, &got, &logflags, flags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004875 if (error)
4876 goto error0;
4877
Christoph Hellwigb101e332019-02-15 08:02:47 -08004878 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004879
4880error0:
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004881 if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004882 logflags &= ~XFS_ILOG_DEXT;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004883 else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE)
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004884 logflags &= ~XFS_ILOG_DBROOT;
4885
4886 if (logflags)
4887 xfs_trans_log_inode(tp, ip, logflags);
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07004888 if (cur)
4889 xfs_btree_del_cursor(cur, error);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004890 return error;
4891}
4892
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004894 * When a delalloc extent is split (e.g., due to a hole punch), the original
4895 * indlen reservation must be shared across the two new extents that are left
4896 * behind.
4897 *
4898 * Given the original reservation and the worst case indlen for the two new
4899 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004900 * reservation fairly across the two new extents. If necessary, steal available
4901 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4902 * ores == 1). The number of stolen blocks is returned. The availability and
4903 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004904 */
Christoph Hellwigda2b9c32024-04-22 13:20:17 +02004905static void
Brian Fostera9bd24a2016-03-15 11:42:46 +11004906xfs_bmap_split_indlen(
4907 xfs_filblks_t ores, /* original res. */
4908 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Christoph Hellwigda2b9c32024-04-22 13:20:17 +02004909 xfs_filblks_t *indlen2) /* ext2 worst indlen */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004910{
4911 xfs_filblks_t len1 = *indlen1;
4912 xfs_filblks_t len2 = *indlen2;
4913 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Foster75d65362017-02-13 22:48:30 -08004914 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004915
4916 /*
Brian Foster75d65362017-02-13 22:48:30 -08004917 * We can't meet the total required reservation for the two extents.
4918 * Calculate the percent of the overall shortage between both extents
4919 * and apply this percentage to each of the requested indlen values.
4920 * This distributes the shortage fairly and reduces the chances that one
4921 * of the two extents is left with nothing when extents are repeatedly
4922 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004923 */
Brian Foster75d65362017-02-13 22:48:30 -08004924 resfactor = (ores * 100);
4925 do_div(resfactor, nres);
4926 len1 *= resfactor;
4927 do_div(len1, 100);
4928 len2 *= resfactor;
4929 do_div(len2, 100);
4930 ASSERT(len1 + len2 <= ores);
4931 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4932
4933 /*
4934 * Hand out the remainder to each extent. If one of the two reservations
4935 * is zero, we want to make sure that one gets a block first. The loop
4936 * below starts with len1, so hand len2 a block right off the bat if it
4937 * is zero.
4938 */
4939 ores -= (len1 + len2);
4940 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4941 if (ores && !len2 && *indlen2) {
4942 len2++;
4943 ores--;
4944 }
4945 while (ores) {
4946 if (len1 < *indlen1) {
4947 len1++;
4948 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004949 }
Brian Foster75d65362017-02-13 22:48:30 -08004950 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004951 break;
Brian Foster75d65362017-02-13 22:48:30 -08004952 if (len2 < *indlen2) {
4953 len2++;
4954 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004955 }
4956 }
4957
4958 *indlen1 = len1;
4959 *indlen2 = len2;
4960}
4961
Christoph Hellwigcc3c92e2024-04-30 14:58:22 +02004962void
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004963xfs_bmap_del_extent_delay(
4964 struct xfs_inode *ip,
4965 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004966 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004967 struct xfs_bmbt_irec *got,
4968 struct xfs_bmbt_irec *del)
4969{
4970 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wong732436e2022-07-09 10:56:05 -07004971 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004972 struct xfs_bmbt_irec new;
4973 int64_t da_old, da_new, da_diff = 0;
4974 xfs_fileoff_t del_endoff, got_endoff;
Christoph Hellwigda2b9c32024-04-22 13:20:17 +02004975 xfs_filblks_t got_indlen, new_indlen, stolen = 0;
Dave Chinner0e5b8e42022-04-21 10:46:01 +10004976 uint32_t state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig7e77d572024-04-22 13:20:14 +02004977 uint64_t fdblocks;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004978 bool isrt;
4979
4980 XFS_STATS_INC(mp, xs_del_exlist);
4981
Darrick J. Wong5049ff42024-02-22 12:43:36 -08004982 isrt = xfs_ifork_is_realtime(ip, whichfork);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004983 del_endoff = del->br_startoff + del->br_blockcount;
4984 got_endoff = got->br_startoff + got->br_blockcount;
4985 da_old = startblockval(got->br_startblock);
4986 da_new = 0;
4987
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004988 ASSERT(del->br_blockcount > 0);
4989 ASSERT(got->br_startoff <= del->br_startoff);
4990 ASSERT(got_endoff >= del_endoff);
4991
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004992 /*
4993 * Update the inode delalloc counter now and wait to update the
4994 * sb counters as we might have to borrow some blocks for the
4995 * indirect block accounting.
4996 */
Christoph Hellwigcc3c92e2024-04-30 14:58:22 +02004997 xfs_quota_unreserve_blkres(ip, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004998 ip->i_delayed_blks -= del->br_blockcount;
4999
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005000 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07005001 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005002 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07005003 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005004
Christoph Hellwig0173c682017-10-17 14:16:22 -07005005 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5006 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005007 /*
5008 * Matches the whole extent. Delete the entry.
5009 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005010 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005011 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005012 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07005013 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005014 /*
5015 * Deleting the first part of the extent.
5016 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005017 got->br_startoff = del_endoff;
5018 got->br_blockcount -= del->br_blockcount;
5019 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
5020 got->br_blockcount), da_old);
5021 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005022 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005023 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07005024 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005025 /*
5026 * Deleting the last part of the extent.
5027 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005028 got->br_blockcount = got->br_blockcount - del->br_blockcount;
5029 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
5030 got->br_blockcount), da_old);
5031 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005032 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005033 break;
5034 case 0:
5035 /*
5036 * Deleting the middle of the extent.
5037 *
5038 * Distribute the original indlen reservation across the two new
5039 * extents. Steal blocks from the deleted extent if necessary.
5040 * Stealing blocks simply fudges the fdblocks accounting below.
5041 * Warn if either of the new indlen reservations is zero as this
5042 * can lead to delalloc problems.
5043 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005044 got->br_blockcount = del->br_startoff - got->br_startoff;
5045 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
5046
5047 new.br_blockcount = got_endoff - del_endoff;
5048 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
5049
5050 WARN_ON_ONCE(!got_indlen || !new_indlen);
Christoph Hellwigda2b9c32024-04-22 13:20:17 +02005051 /*
5052 * Steal as many blocks as we can to try and satisfy the worst
5053 * case indlen for both new extents.
Christoph Hellwigbd1753d2024-04-22 13:20:18 +02005054 *
5055 * However, we can't just steal reservations from the data
5056 * blocks if this is an RT inodes as the data and metadata
5057 * blocks come from different pools. We'll have to live with
5058 * under-filled indirect reservation in this case.
Christoph Hellwigda2b9c32024-04-22 13:20:17 +02005059 */
5060 da_new = got_indlen + new_indlen;
Christoph Hellwigbd1753d2024-04-22 13:20:18 +02005061 if (da_new > da_old && !isrt) {
Christoph Hellwigda2b9c32024-04-22 13:20:17 +02005062 stolen = XFS_FILBLKS_MIN(da_new - da_old,
5063 del->br_blockcount);
5064 da_old += stolen;
5065 }
5066 if (da_new > da_old)
5067 xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen);
5068 da_new = got_indlen + new_indlen;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005069
5070 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005071
5072 new.br_startoff = del_endoff;
5073 new.br_state = got->br_state;
5074 new.br_startblock = nullstartblock((int)new_indlen);
5075
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005076 xfs_iext_update_extent(ip, state, icur, got);
5077 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005078 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005079
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005080 del->br_blockcount -= stolen;
5081 break;
5082 }
5083
5084 ASSERT(da_old >= da_new);
5085 da_diff = da_old - da_new;
Christoph Hellwig7e77d572024-04-22 13:20:14 +02005086 fdblocks = da_diff;
5087
5088 if (isrt)
5089 xfs_add_frextents(mp, xfs_rtb_to_rtx(mp, del->br_blockcount));
5090 else
5091 fdblocks += del->br_blockcount;
5092
5093 xfs_add_fdblocks(mp, fdblocks);
Christoph Hellwig7099bd02024-04-22 13:20:15 +02005094 xfs_mod_delalloc(ip, -(int64_t)del->br_blockcount, -da_diff);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005095}
5096
5097void
5098xfs_bmap_del_extent_cow(
5099 struct xfs_inode *ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005100 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005101 struct xfs_bmbt_irec *got,
5102 struct xfs_bmbt_irec *del)
5103{
5104 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wong732436e2022-07-09 10:56:05 -07005105 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_COW_FORK);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005106 struct xfs_bmbt_irec new;
5107 xfs_fileoff_t del_endoff, got_endoff;
Dave Chinner0e5b8e42022-04-21 10:46:01 +10005108 uint32_t state = BMAP_COWFORK;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005109
5110 XFS_STATS_INC(mp, xs_del_exlist);
5111
5112 del_endoff = del->br_startoff + del->br_blockcount;
5113 got_endoff = got->br_startoff + got->br_blockcount;
5114
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005115 ASSERT(del->br_blockcount > 0);
5116 ASSERT(got->br_startoff <= del->br_startoff);
5117 ASSERT(got_endoff >= del_endoff);
5118 ASSERT(!isnullstartblock(got->br_startblock));
5119
5120 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07005121 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005122 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07005123 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005124
Christoph Hellwig0173c682017-10-17 14:16:22 -07005125 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5126 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005127 /*
5128 * Matches the whole extent. Delete the entry.
5129 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005130 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005131 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005132 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07005133 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005134 /*
5135 * Deleting the first part of the extent.
5136 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005137 got->br_startoff = del_endoff;
5138 got->br_blockcount -= del->br_blockcount;
5139 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005140 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005141 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07005142 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005143 /*
5144 * Deleting the last part of the extent.
5145 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005146 got->br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005147 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005148 break;
5149 case 0:
5150 /*
5151 * Deleting the middle of the extent.
5152 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005153 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005154
5155 new.br_startoff = del_endoff;
5156 new.br_blockcount = got_endoff - del_endoff;
5157 new.br_state = got->br_state;
5158 new.br_startblock = del->br_startblock + del->br_blockcount;
5159
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005160 xfs_iext_update_extent(ip, state, icur, got);
5161 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005162 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005163 break;
5164 }
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08005165 ip->i_delayed_blks -= del->br_blockcount;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005166}
5167
Brian Fostera9bd24a2016-03-15 11:42:46 +11005168/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11005169 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07005170 * after removing space.
Dave Chinner9e5987a72013-02-25 12:31:26 +11005171 */
5172STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005173xfs_bmap_del_extent_real(
Dave Chinner9e5987a72013-02-25 12:31:26 +11005174 xfs_inode_t *ip, /* incore inode pointer */
5175 xfs_trans_t *tp, /* current transaction pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005176 struct xfs_iext_cursor *icur,
Darrick J. Wongae127f02021-09-16 12:18:47 -07005177 struct xfs_btree_cur *cur, /* if null, not a btree */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005178 xfs_bmbt_irec_t *del, /* data to remove from extents */
5179 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005180 int whichfork, /* data or attr fork */
Dave Chinnere7d410a2022-04-21 10:46:09 +10005181 uint32_t bflags) /* bmapi flags */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005182{
Dave Chinner9e5987a72013-02-25 12:31:26 +11005183 xfs_fsblock_t del_endblock=0; /* first block past del */
5184 xfs_fileoff_t del_endoff; /* first offset past del */
Christoph Hellwig9871d092024-04-22 13:20:09 +02005185 int error = 0; /* error return value */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005186 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005187 xfs_fileoff_t got_endoff; /* first offset past got */
5188 int i; /* temp state */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07005189 struct xfs_ifork *ifp; /* inode fork pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005190 xfs_mount_t *mp; /* mount structure */
5191 xfs_filblks_t nblks; /* quota/sb block count */
5192 xfs_bmbt_irec_t new; /* new record to be inserted */
5193 /* REFERENCED */
5194 uint qfield; /* quota field to update */
Dave Chinner0e5b8e42022-04-21 10:46:01 +10005195 uint32_t state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005196 struct xfs_bmbt_irec old;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005197
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005198 *logflagsp = 0;
5199
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005200 mp = ip->i_mount;
5201 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005202
Darrick J. Wong732436e2022-07-09 10:56:05 -07005203 ifp = xfs_ifork_ptr(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005204 ASSERT(del->br_blockcount > 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005205 xfs_iext_get_extent(ifp, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005206 ASSERT(got.br_startoff <= del->br_startoff);
5207 del_endoff = del->br_startoff + del->br_blockcount;
5208 got_endoff = got.br_startoff + got.br_blockcount;
5209 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005210 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a72013-02-25 12:31:26 +11005211 qfield = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005212
Christoph Hellwig1b24b632017-10-17 14:16:22 -07005213 /*
5214 * If it's the case where the directory code is running with no block
5215 * reservation, and the deleted block is in the middle of its extent,
5216 * and the resulting insert of an extent would cause transformation to
5217 * btree format, then reject it. The calling code will then swap blocks
5218 * around instead. We have to do this now, rather than waiting for the
5219 * conversion to btree format, since the transaction will be dirty then.
5220 */
5221 if (tp->t_blk_res == 0 &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005222 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005223 ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) &&
Christoph Hellwig1b24b632017-10-17 14:16:22 -07005224 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
5225 return -ENOSPC;
5226
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005227 *logflagsp = XFS_ILOG_CORE;
Christoph Hellwig9871d092024-04-22 13:20:09 +02005228 if (xfs_ifork_is_realtime(ip, whichfork))
Christoph Hellwige1d75532017-10-17 14:16:21 -07005229 qfield = XFS_TRANS_DQ_RTBCOUNT;
Christoph Hellwig9871d092024-04-22 13:20:09 +02005230 else
Christoph Hellwige1d75532017-10-17 14:16:21 -07005231 qfield = XFS_TRANS_DQ_BCOUNT;
Darrick J. Wong6c664482023-10-16 09:16:22 -07005232 nblks = del->br_blockcount;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005233
5234 del_endblock = del->br_startblock + del->br_blockcount;
5235 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005236 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005237 if (error)
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005238 return error;
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08005239 if (XFS_IS_CORRUPT(mp, i != 1)) {
5240 xfs_btree_mark_sick(cur);
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005241 return -EFSCORRUPTED;
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08005242 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11005243 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005244
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005245 if (got.br_startoff == del->br_startoff)
5246 state |= BMAP_LEFT_FILLING;
5247 if (got_endoff == del_endoff)
5248 state |= BMAP_RIGHT_FILLING;
5249
5250 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5251 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005252 /*
5253 * Matches the whole extent. Delete the entry.
5254 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005255 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005256 xfs_iext_prev(ifp, icur);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005257 ifp->if_nextents--;
5258
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005259 *logflagsp |= XFS_ILOG_CORE;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005260 if (!cur) {
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005261 *logflagsp |= xfs_ilog_fext(whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005262 break;
5263 }
5264 if ((error = xfs_btree_delete(cur, &i)))
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005265 return error;
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08005266 if (XFS_IS_CORRUPT(mp, i != 1)) {
5267 xfs_btree_mark_sick(cur);
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005268 return -EFSCORRUPTED;
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08005269 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11005270 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005271 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005272 /*
5273 * Deleting the first part of the extent.
5274 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005275 got.br_startoff = del_endoff;
5276 got.br_startblock = del_endblock;
5277 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005278 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005279 if (!cur) {
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005280 *logflagsp |= xfs_ilog_fext(whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005281 break;
5282 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005283 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005284 if (error)
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005285 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005286 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005287 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005288 /*
5289 * Deleting the last part of the extent.
5290 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005291 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005292 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005293 if (!cur) {
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005294 *logflagsp |= xfs_ilog_fext(whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005295 break;
5296 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005297 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005298 if (error)
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005299 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005300 break;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005301 case 0:
5302 /*
5303 * Deleting the middle of the extent.
5304 */
Chandan Babu R0dbc5cb2021-01-22 16:48:12 -08005305
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005306 old = got;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07005307
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005308 got.br_blockcount = del->br_startoff - got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005309 xfs_iext_update_extent(ip, state, icur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005310
Dave Chinner9e5987a72013-02-25 12:31:26 +11005311 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005312 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005313 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005314 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005315
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005316 *logflagsp |= XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005317 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005318 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005319 if (error)
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005320 return error;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005321 error = xfs_btree_increment(cur, 0, &i);
5322 if (error)
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005323 return error;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005324 cur->bc_rec.b = new;
5325 error = xfs_btree_insert(cur, &i);
5326 if (error && error != -ENOSPC)
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005327 return error;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005328 /*
5329 * If get no-space back from btree insert, it tried a
5330 * split, and we have a zero block reservation. Fix up
5331 * our state and return the error.
5332 */
5333 if (error == -ENOSPC) {
5334 /*
5335 * Reset the cursor, don't trust it after any
5336 * insert operation.
5337 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005338 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005339 if (error)
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005340 return error;
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08005341 if (XFS_IS_CORRUPT(mp, i != 1)) {
5342 xfs_btree_mark_sick(cur);
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005343 return -EFSCORRUPTED;
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08005344 }
Christoph Hellwige1d75532017-10-17 14:16:21 -07005345 /*
5346 * Update the btree record back
5347 * to the original value.
5348 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005349 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005350 if (error)
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005351 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005352 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005353 * Reset the extent record back
5354 * to the original value.
Dave Chinner9e5987a72013-02-25 12:31:26 +11005355 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005356 xfs_iext_update_extent(ip, state, icur, &old);
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005357 *logflagsp = 0;
5358 return -ENOSPC;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005359 }
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08005360 if (XFS_IS_CORRUPT(mp, i != 1)) {
5361 xfs_btree_mark_sick(cur);
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005362 return -EFSCORRUPTED;
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08005363 }
Christoph Hellwige1d75532017-10-17 14:16:21 -07005364 } else
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005365 *logflagsp |= xfs_ilog_fext(whichfork);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005366
5367 ifp->if_nextents++;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005368 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005369 xfs_iext_insert(ip, icur, &new, state);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005370 break;
5371 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005372
5373 /* remove reverse mapping */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07005374 xfs_rmap_unmap_extent(tp, ip, whichfork, del);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005375
Dave Chinner9e5987a72013-02-25 12:31:26 +11005376 /*
5377 * If we need to, add to list of extents to delete.
5378 */
Christoph Hellwig9871d092024-04-22 13:20:09 +02005379 if (!(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005380 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
Darrick J. Wong74b4c5d2019-08-26 17:06:04 -07005381 xfs_refcount_decrease_extent(tp, del);
Christoph Hellwig9871d092024-04-22 13:20:09 +02005382 } else if (xfs_ifork_is_realtime(ip, whichfork)) {
Christoph Hellwigde37dbd2024-04-22 13:20:10 +02005383 /*
5384 * Ensure the bitmap and summary inodes are locked
5385 * and joined to the transaction before modifying them.
5386 */
5387 if (!(tp->t_flags & XFS_TRANS_RTBITMAP_LOCKED)) {
5388 tp->t_flags |= XFS_TRANS_RTBITMAP_LOCKED;
Christoph Hellwig0a59e4f2024-08-30 15:36:59 -07005389 xfs_rtbitmap_lock(mp);
5390 xfs_rtbitmap_trans_join(tp);
Christoph Hellwigde37dbd2024-04-22 13:20:10 +02005391 }
Christoph Hellwig9871d092024-04-22 13:20:09 +02005392 error = xfs_rtfree_blocks(tp, del->br_startblock,
5393 del->br_blockcount);
Brian Fosterfcb762f2018-05-09 08:45:04 -07005394 } else {
Darrick J. Wong980faec2024-07-02 11:22:51 -07005395 unsigned int efi_flags = 0;
5396
5397 if ((bflags & XFS_BMAPI_NODISCARD) ||
5398 del->br_state == XFS_EXT_UNWRITTEN)
5399 efi_flags |= XFS_FREE_EXTENT_SKIP_DISCARD;
5400
Darrick J. Wong4c88fef2023-12-06 18:40:57 -08005401 error = xfs_free_extent_later(tp, del->br_startblock,
Brian Foster4e529332018-05-10 09:35:42 -07005402 del->br_blockcount, NULL,
Darrick J. Wong980faec2024-07-02 11:22:51 -07005403 XFS_AG_RESV_NONE, efi_flags);
Brian Fosterfcb762f2018-05-09 08:45:04 -07005404 }
Christoph Hellwig9871d092024-04-22 13:20:09 +02005405 if (error)
5406 return error;
Darrick J. Wong62aab202016-10-03 09:11:23 -07005407 }
5408
Dave Chinner9e5987a72013-02-25 12:31:26 +11005409 /*
5410 * Adjust inode # blocks in the file.
5411 */
5412 if (nblks)
Christoph Hellwig6e73a542021-03-29 11:11:40 -07005413 ip->i_nblocks -= nblks;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005414 /*
5415 * Adjust quota data.
5416 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005417 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a72013-02-25 12:31:26 +11005418 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5419
Jiachen Zhange6af9c92023-12-05 13:58:58 +08005420 return 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005421}
5422
5423/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 * Unmap (remove) blocks from a file.
5425 * If nexts is nonzero then the number of extents to remove is limited to
5426 * that value. If not all extents in the block range can be removed then
5427 * *done is set.
5428 */
Darrick J. Wonga59eb5f2023-12-15 10:03:43 -08005429static int
Darrick J. Wong44535932016-10-03 09:11:29 -07005430__xfs_bunmapi(
Brian Fosterccd9d912018-07-11 22:26:13 -07005431 struct xfs_trans *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005433 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005434 xfs_filblks_t *rlen, /* i/o: amount remaining */
Dave Chinnere7d410a2022-04-21 10:46:09 +10005435 uint32_t flags, /* misc flags */
Brian Foster2af52842018-07-11 22:26:25 -07005436 xfs_extnum_t nexts) /* number of extents max */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437{
Brian Fosterccd9d912018-07-11 22:26:13 -07005438 struct xfs_btree_cur *cur; /* bmap btree cursor */
5439 struct xfs_bmbt_irec del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 int error; /* error return value */
5441 xfs_extnum_t extno; /* extent number in list */
Brian Fosterccd9d912018-07-11 22:26:13 -07005442 struct xfs_bmbt_irec got; /* current extent record */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07005443 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 int isrt; /* freeing in rt area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 int logflags; /* transaction logging flags */
5446 xfs_extlen_t mod; /* rt extent offset */
Darrick J. Wonga71895c2019-11-11 12:53:22 -08005447 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 int tmp_logflags; /* partial logging flags */
5449 int wasdel; /* was a delayed alloc extent */
5450 int whichfork; /* data or attribute fork */
Darrick J. Wong44535932016-10-03 09:11:29 -07005451 xfs_filblks_t len = *rlen; /* length to unmap in file */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005452 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005453 struct xfs_iext_cursor icur;
5454 bool done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005456 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005457
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005458 whichfork = xfs_bmapi_whichfork(flags);
5459 ASSERT(whichfork != XFS_COW_FORK);
Darrick J. Wong732436e2022-07-09 10:56:05 -07005460 ifp = xfs_ifork_ptr(ip, whichfork);
Darrick J. Wong1196f3f2024-02-22 12:31:51 -08005461 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp))) {
5462 xfs_bmap_mark_sick(ip, whichfork);
Dave Chinner24513372014-06-25 14:58:08 +10005463 return -EFSCORRUPTED;
Darrick J. Wong1196f3f2024-02-22 12:31:51 -08005464 }
Dave Chinner75c8c50f2021-08-18 18:46:53 -07005465 if (xfs_is_shutdown(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005466 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005467
Matthew Wilcox (Oracle)3fed24f2024-02-19 15:41:12 +00005468 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 ASSERT(len > 0);
5470 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005471
Christoph Hellwig862a8042021-04-13 11:15:09 -07005472 error = xfs_iread_extents(tp, ip, whichfork);
5473 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 return error;
Christoph Hellwig862a8042021-04-13 11:15:09 -07005475
Eric Sandeen5d829302016-11-08 12:59:42 +11005476 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005477 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 return 0;
5479 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005480 XFS_STATS_INC(mp, xs_blk_unmap);
Darrick J. Wong5049ff42024-02-22 12:43:36 -08005481 isrt = xfs_ifork_is_realtime(ip, whichfork);
Christoph Hellwigdc560152017-10-23 16:32:39 -07005482 end = start + len;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005483
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005484 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
Christoph Hellwigdc560152017-10-23 16:32:39 -07005485 *rlen = 0;
5486 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 }
Christoph Hellwigdc560152017-10-23 16:32:39 -07005488 end--;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005489
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 logflags = 0;
Christoph Hellwigac1e0672021-04-13 11:15:11 -07005491 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005492 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005493 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 } else
5495 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005496
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 extno = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005498 while (end != (xfs_fileoff_t)-1 && end >= start &&
Darrick J. Wong4ed64352022-04-25 18:37:15 -07005499 (nexts == 0 || extno < nexts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005501 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 * Just back up to the previous extent, if so.
5503 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005504 if (got.br_startoff > end &&
5505 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5506 done = true;
5507 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 }
5509 /*
5510 * Is the last block of this extent before the range
5511 * we're supposed to delete? If so, we're done.
5512 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005513 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005515 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 break;
5517 /*
5518 * Then deal with the (possibly delayed) allocated space
5519 * we found.
5520 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005522 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005523
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 if (got.br_startoff < start) {
5525 del.br_startoff = start;
5526 del.br_blockcount -= start - got.br_startoff;
5527 if (!wasdel)
5528 del.br_startblock += start - got.br_startoff;
5529 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005530 if (del.br_startoff + del.br_blockcount > end + 1)
5531 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005532
Darrick J. Wong2b6a5ec2024-02-22 12:44:22 -08005533 if (!isrt || (flags & XFS_BMAPI_REMAP))
Dave Chinner0703a8e2018-06-08 09:54:22 -07005534 goto delete;
5535
Darrick J. Wong5dc3a802023-10-16 09:37:07 -07005536 mod = xfs_rtb_to_rtxoff(mp,
5537 del.br_startblock + del.br_blockcount);
Dave Chinner0703a8e2018-06-08 09:54:22 -07005538 if (mod) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 /*
5540 * Realtime extent not lined up at the end.
5541 * The extent could have been split into written
5542 * and unwritten pieces, or we could just be
5543 * unmapping part of it. But we can't really
5544 * get rid of part of a realtime extent.
5545 */
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11005546 if (del.br_state == XFS_EXT_UNWRITTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 /*
5548 * This piece is unwritten, or we're not
5549 * using unwritten extents. Skip over it.
5550 */
Darrick J. Wong2b6a5ec2024-02-22 12:44:22 -08005551 ASSERT((flags & XFS_BMAPI_REMAP) || end >= mod);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005552 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 del.br_blockcount : mod;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005554 if (end < got.br_startoff &&
5555 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5556 done = true;
5557 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 }
5559 continue;
5560 }
5561 /*
5562 * It's written, turn it unwritten.
5563 * This is better than zeroing it.
5564 */
5565 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005566 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 /*
5568 * If this spans a realtime extent boundary,
5569 * chop it back to the start of the one we end at.
5570 */
5571 if (del.br_blockcount > mod) {
5572 del.br_startoff += del.br_blockcount - mod;
5573 del.br_startblock += del.br_blockcount - mod;
5574 del.br_blockcount = mod;
5575 }
5576 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005577 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005578 whichfork, &icur, &cur, &del,
Brian Foster92f9da32018-07-11 22:26:28 -07005579 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 if (error)
5581 goto error0;
5582 goto nodelete;
5583 }
Darrick J. Wong5dc3a802023-10-16 09:37:07 -07005584
5585 mod = xfs_rtb_to_rtxoff(mp, del.br_startblock);
Dave Chinner0703a8e2018-06-08 09:54:22 -07005586 if (mod) {
Omar Sandoval0c4da702019-11-26 16:58:07 -08005587 xfs_extlen_t off = mp->m_sb.sb_rextsize - mod;
5588
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 /*
5590 * Realtime extent is lined up at the end but not
5591 * at the front. We'll get rid of full extents if
5592 * we can.
5593 */
Omar Sandoval0c4da702019-11-26 16:58:07 -08005594 if (del.br_blockcount > off) {
5595 del.br_blockcount -= off;
5596 del.br_startoff += off;
5597 del.br_startblock += off;
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11005598 } else if (del.br_startoff == start &&
5599 (del.br_state == XFS_EXT_UNWRITTEN ||
5600 tp->t_blk_res == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 /*
5602 * Can't make it unwritten. There isn't
5603 * a full extent here so just skip it.
5604 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005605 ASSERT(end >= del.br_blockcount);
5606 end -= del.br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005607 if (got.br_startoff > end &&
5608 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5609 done = true;
5610 break;
5611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 continue;
5613 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005614 struct xfs_bmbt_irec prev;
Omar Sandoval0c4da702019-11-26 16:58:07 -08005615 xfs_fileoff_t unwrite_start;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005616
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 /*
5618 * This one is already unwritten.
5619 * It must have a written left neighbor.
5620 * Unwrite the killed part of that one and
5621 * try again.
5622 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005623 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5624 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005626 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 ASSERT(del.br_startblock ==
5628 prev.br_startblock + prev.br_blockcount);
Omar Sandoval0c4da702019-11-26 16:58:07 -08005629 unwrite_start = max3(start,
5630 del.br_startoff - mod,
5631 prev.br_startoff);
5632 mod = unwrite_start - prev.br_startoff;
5633 prev.br_startoff = unwrite_start;
5634 prev.br_startblock += mod;
5635 prev.br_blockcount -= mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005637 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005638 ip, whichfork, &icur, &cur,
Brian Foster92f9da32018-07-11 22:26:28 -07005639 &prev, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 if (error)
5641 goto error0;
5642 goto nodelete;
5643 } else {
5644 ASSERT(del.br_state == XFS_EXT_NORM);
5645 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005646 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005647 ip, whichfork, &icur, &cur,
Brian Foster92f9da32018-07-11 22:26:28 -07005648 &del, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 if (error)
5650 goto error0;
5651 goto nodelete;
5652 }
5653 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005654
Dave Chinner0703a8e2018-06-08 09:54:22 -07005655delete:
Brian Fosterb2706a02016-03-15 11:42:46 +11005656 if (wasdel) {
Christoph Hellwigcc3c92e2024-04-30 14:58:22 +02005657 xfs_bmap_del_extent_delay(ip, whichfork, &icur, &got, &del);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005658 } else {
Brian Foster81ba8f32018-07-11 22:26:16 -07005659 error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5660 &del, &tmp_logflags, whichfork,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005661 flags);
5662 logflags |= tmp_logflags;
Christoph Hellwigcc3c92e2024-04-30 14:58:22 +02005663 if (error)
5664 goto error0;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005665 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005666
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005667 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 /*
5670 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005672 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005673 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5674 (got.br_startoff > end &&
5675 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5676 done = true;
5677 break;
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 extno++;
5680 }
5681 }
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005682 if (done || end == (xfs_fileoff_t)-1 || end < start)
Darrick J. Wong44535932016-10-03 09:11:29 -07005683 *rlen = 0;
5684 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005685 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005686
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 /*
5688 * Convert to a btree if necessary.
5689 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005690 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07005692 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5693 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 logflags |= tmp_logflags;
Christoph Hellwigb101e332019-02-15 08:02:47 -08005695 } else {
5696 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 }
Christoph Hellwigb101e332019-02-15 08:02:47 -08005699
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700error0:
5701 /*
5702 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005703 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005705 if ((logflags & xfs_ilog_fext(whichfork)) &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005706 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005707 logflags &= ~xfs_ilog_fext(whichfork);
5708 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005709 ifp->if_format != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005710 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 /*
5712 * Log inode even in the error case, if the transaction
5713 * is dirty we'll need to shut down the filesystem.
5714 */
5715 if (logflags)
5716 xfs_trans_log_inode(tp, ip, logflags);
5717 if (cur) {
Brian Fostercf612de2018-07-11 22:26:29 -07005718 if (!error)
Christoph Hellwig88ee2f42024-02-22 12:37:03 -08005719 cur->bc_bmap.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07005720 xfs_btree_del_cursor(cur, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 }
5722 return error;
5723}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005724
Darrick J. Wong44535932016-10-03 09:11:29 -07005725/* Unmap a range of a file. */
5726int
5727xfs_bunmapi(
5728 xfs_trans_t *tp,
5729 struct xfs_inode *ip,
5730 xfs_fileoff_t bno,
5731 xfs_filblks_t len,
Dave Chinnere7d410a2022-04-21 10:46:09 +10005732 uint32_t flags,
Darrick J. Wong44535932016-10-03 09:11:29 -07005733 xfs_extnum_t nexts,
Darrick J. Wong44535932016-10-03 09:11:29 -07005734 int *done)
5735{
5736 int error;
5737
Brian Foster2af52842018-07-11 22:26:25 -07005738 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
Darrick J. Wong44535932016-10-03 09:11:29 -07005739 *done = (len == 0);
5740 return error;
5741}
5742
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005743/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005744 * Determine whether an extent shift can be accomplished by a merge with the
5745 * extent that precedes the target hole of the shift.
5746 */
5747STATIC bool
5748xfs_bmse_can_merge(
5749 struct xfs_bmbt_irec *left, /* preceding extent */
5750 struct xfs_bmbt_irec *got, /* current extent to shift */
5751 xfs_fileoff_t shift) /* shift fsb */
5752{
5753 xfs_fileoff_t startoff;
5754
5755 startoff = got->br_startoff - shift;
5756
5757 /*
5758 * The extent, once shifted, must be adjacent in-file and on-disk with
5759 * the preceding extent.
5760 */
5761 if ((left->br_startoff + left->br_blockcount != startoff) ||
5762 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5763 (left->br_state != got->br_state) ||
Chandan Babu R95f0b952021-08-09 12:05:22 +05305764 (left->br_blockcount + got->br_blockcount > XFS_MAX_BMBT_EXTLEN))
Brian Fosterddb19e32014-09-23 15:38:09 +10005765 return false;
5766
5767 return true;
5768}
5769
5770/*
5771 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5772 * hole in the file. If an extent shift would result in the extent being fully
5773 * adjacent to the extent that currently precedes the hole, we can merge with
5774 * the preceding extent rather than do the shift.
5775 *
5776 * This function assumes the caller has verified a shift-by-merge is possible
5777 * with the provided extents via xfs_bmse_can_merge().
5778 */
5779STATIC int
5780xfs_bmse_merge(
Brian Foster0f37d172018-08-01 07:20:34 -07005781 struct xfs_trans *tp,
Brian Fosterddb19e32014-09-23 15:38:09 +10005782 struct xfs_inode *ip,
5783 int whichfork,
5784 xfs_fileoff_t shift, /* shift fsb */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005785 struct xfs_iext_cursor *icur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005786 struct xfs_bmbt_irec *got, /* extent to shift */
5787 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005788 struct xfs_btree_cur *cur,
Brian Foster0f37d172018-08-01 07:20:34 -07005789 int *logflags) /* output */
Brian Fosterddb19e32014-09-23 15:38:09 +10005790{
Darrick J. Wong732436e2022-07-09 10:56:05 -07005791 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005792 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005793 xfs_filblks_t blockcount;
5794 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005795 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005796
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005797 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005798
Matthew Wilcox (Oracle)3fed24f2024-02-19 15:41:12 +00005799 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005800 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005801
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005802 new = *left;
5803 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005804
5805 /*
5806 * Update the on-disk extent count, the btree if necessary and log the
5807 * inode.
5808 */
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005809 ifp->if_nextents--;
Brian Fosterddb19e32014-09-23 15:38:09 +10005810 *logflags |= XFS_ILOG_CORE;
5811 if (!cur) {
5812 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005813 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005814 }
5815
5816 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005817 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005818 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005819 return error;
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08005820 if (XFS_IS_CORRUPT(mp, i != 1)) {
5821 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005822 return -EFSCORRUPTED;
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08005823 }
Brian Fosterddb19e32014-09-23 15:38:09 +10005824
5825 error = xfs_btree_delete(cur, &i);
5826 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005827 return error;
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08005828 if (XFS_IS_CORRUPT(mp, i != 1)) {
5829 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005830 return -EFSCORRUPTED;
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08005831 }
Brian Fosterddb19e32014-09-23 15:38:09 +10005832
5833 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005834 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005835 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005836 return error;
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08005837 if (XFS_IS_CORRUPT(mp, i != 1)) {
5838 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005839 return -EFSCORRUPTED;
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08005840 }
Brian Fosterddb19e32014-09-23 15:38:09 +10005841
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005842 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005843 if (error)
5844 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005845
Brian Fostere20e1742019-09-18 17:50:45 -07005846 /* change to extent format if required after extent removal */
5847 error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork);
5848 if (error)
5849 return error;
5850
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005851done:
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005852 xfs_iext_remove(ip, icur, 0);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005853 xfs_iext_prev(ifp, icur);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005854 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5855 &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005856
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005857 /* update reverse mapping. rmap functions merge the rmaps for us */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07005858 xfs_rmap_unmap_extent(tp, ip, whichfork, got);
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005859 memcpy(&new, got, sizeof(new));
5860 new.br_startoff = left->br_startoff + left->br_blockcount;
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07005861 xfs_rmap_map_extent(tp, ip, whichfork, &new);
5862 return 0;
Brian Fosterddb19e32014-09-23 15:38:09 +10005863}
5864
Christoph Hellwigbf806282017-10-19 11:07:34 -07005865static int
5866xfs_bmap_shift_update_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07005867 struct xfs_trans *tp,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005868 struct xfs_inode *ip,
5869 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005870 struct xfs_iext_cursor *icur,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005871 struct xfs_bmbt_irec *got,
5872 struct xfs_btree_cur *cur,
5873 int *logflags,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005874 xfs_fileoff_t startoff)
Brian Fostera979bdf2014-09-23 15:39:04 +10005875{
Christoph Hellwigbf806282017-10-19 11:07:34 -07005876 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005877 struct xfs_bmbt_irec prev = *got;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005878 int error, i;
Brian Fostera979bdf2014-09-23 15:39:04 +10005879
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005880 *logflags |= XFS_ILOG_CORE;
5881
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005882 got->br_startoff = startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005883
5884 if (cur) {
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005885 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005886 if (error)
5887 return error;
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08005888 if (XFS_IS_CORRUPT(mp, i != 1)) {
5889 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005890 return -EFSCORRUPTED;
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08005891 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005892
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005893 error = xfs_bmbt_update(cur, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005894 if (error)
5895 return error;
5896 } else {
5897 *logflags |= XFS_ILOG_DEXT;
5898 }
5899
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005900 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5901 got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005902
Darrick J. Wong9c194642016-08-03 12:16:05 +10005903 /* update reverse mapping */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07005904 xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
5905 xfs_rmap_map_extent(tp, ip, whichfork, got);
5906 return 0;
Brian Fostera979bdf2014-09-23 15:39:04 +10005907}
5908
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005909int
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005910xfs_bmap_collapse_extents(
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005911 struct xfs_trans *tp,
5912 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005913 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005914 xfs_fileoff_t offset_shift_fsb,
Brian Foster333f9502018-07-11 22:26:27 -07005915 bool *done)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005916{
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005917 int whichfork = XFS_DATA_FORK;
5918 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wong732436e2022-07-09 10:56:05 -07005919 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005920 struct xfs_btree_cur *cur = NULL;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005921 struct xfs_bmbt_irec got, prev;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005922 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005923 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005924 int error = 0;
5925 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005926
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005927 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
Darrick J. Wonga71895c2019-11-11 12:53:22 -08005928 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Darrick J. Wong1196f3f2024-02-22 12:31:51 -08005929 xfs_bmap_mark_sick(ip, whichfork);
Dave Chinner24513372014-06-25 14:58:08 +10005930 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005931 }
5932
Dave Chinner75c8c50f2021-08-18 18:46:53 -07005933 if (xfs_is_shutdown(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005934 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005935
Matthew Wilcox (Oracle)3fed24f2024-02-19 15:41:12 +00005936 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005937
Christoph Hellwig862a8042021-04-13 11:15:09 -07005938 error = xfs_iread_extents(tp, ip, whichfork);
5939 if (error)
5940 return error;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005941
Christoph Hellwige9e66df2024-02-22 12:35:13 -08005942 if (ifp->if_format == XFS_DINODE_FMT_BTREE)
Brian Fosterddb19e32014-09-23 15:38:09 +10005943 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Brian Fosterddb19e32014-09-23 15:38:09 +10005944
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005945 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005946 *done = true;
5947 goto del_cursor;
5948 }
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005949 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08005950 xfs_bmap_mark_sick(ip, whichfork);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005951 error = -EFSCORRUPTED;
5952 goto del_cursor;
5953 }
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005954
Christoph Hellwigbf806282017-10-19 11:07:34 -07005955 new_startoff = got.br_startoff - offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005956 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005957 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5958 error = -EINVAL;
5959 goto del_cursor;
5960 }
5961
Christoph Hellwigbf806282017-10-19 11:07:34 -07005962 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
Brian Foster0f37d172018-08-01 07:20:34 -07005963 error = xfs_bmse_merge(tp, ip, whichfork,
5964 offset_shift_fsb, &icur, &got, &prev,
5965 cur, &logflags);
Christoph Hellwigbf806282017-10-19 11:07:34 -07005966 if (error)
5967 goto del_cursor;
5968 goto done;
5969 }
5970 } else {
5971 if (got.br_startoff < offset_shift_fsb) {
5972 error = -EINVAL;
5973 goto del_cursor;
5974 }
5975 }
5976
Brian Foster0f37d172018-08-01 07:20:34 -07005977 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5978 cur, &logflags, new_startoff);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005979 if (error)
5980 goto del_cursor;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005981
Christoph Hellwig42630362017-11-03 10:34:41 -07005982done:
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005983 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5984 *done = true;
5985 goto del_cursor;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005986 }
5987
Christoph Hellwigbf806282017-10-19 11:07:34 -07005988 *next_fsb = got.br_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005989del_cursor:
5990 if (cur)
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07005991 xfs_btree_del_cursor(cur, error);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005992 if (logflags)
5993 xfs_trans_log_inode(tp, ip, logflags);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005994 return error;
5995}
5996
Darrick J. Wongf62cb482018-06-21 23:26:57 -07005997/* Make sure we won't be right-shifting an extent past the maximum bound. */
5998int
5999xfs_bmap_can_insert_extents(
6000 struct xfs_inode *ip,
6001 xfs_fileoff_t off,
6002 xfs_fileoff_t shift)
6003{
6004 struct xfs_bmbt_irec got;
6005 int is_empty;
6006 int error = 0;
6007
Matthew Wilcox (Oracle)3fed24f2024-02-19 15:41:12 +00006008 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL);
Darrick J. Wongf62cb482018-06-21 23:26:57 -07006009
Dave Chinner75c8c50f2021-08-18 18:46:53 -07006010 if (xfs_is_shutdown(ip->i_mount))
Darrick J. Wongf62cb482018-06-21 23:26:57 -07006011 return -EIO;
6012
6013 xfs_ilock(ip, XFS_ILOCK_EXCL);
6014 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
6015 if (!error && !is_empty && got.br_startoff >= off &&
6016 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
6017 error = -EINVAL;
6018 xfs_iunlock(ip, XFS_ILOCK_EXCL);
6019
6020 return error;
6021}
6022
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07006023int
6024xfs_bmap_insert_extents(
6025 struct xfs_trans *tp,
6026 struct xfs_inode *ip,
6027 xfs_fileoff_t *next_fsb,
6028 xfs_fileoff_t offset_shift_fsb,
6029 bool *done,
Brian Foster333f9502018-07-11 22:26:27 -07006030 xfs_fileoff_t stop_fsb)
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07006031{
6032 int whichfork = XFS_DATA_FORK;
6033 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wong732436e2022-07-09 10:56:05 -07006034 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07006035 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07006036 struct xfs_bmbt_irec got, next;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006037 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07006038 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07006039 int error = 0;
6040 int logflags = 0;
6041
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07006042 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
Darrick J. Wonga71895c2019-11-11 12:53:22 -08006043 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Darrick J. Wong1196f3f2024-02-22 12:31:51 -08006044 xfs_bmap_mark_sick(ip, whichfork);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07006045 return -EFSCORRUPTED;
6046 }
6047
Dave Chinner75c8c50f2021-08-18 18:46:53 -07006048 if (xfs_is_shutdown(mp))
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07006049 return -EIO;
6050
Matthew Wilcox (Oracle)3fed24f2024-02-19 15:41:12 +00006051 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07006052
Christoph Hellwig862a8042021-04-13 11:15:09 -07006053 error = xfs_iread_extents(tp, ip, whichfork);
6054 if (error)
6055 return error;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07006056
Christoph Hellwige9e66df2024-02-22 12:35:13 -08006057 if (ifp->if_format == XFS_DINODE_FMT_BTREE)
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07006058 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07006059
Namjae Jeona904b1c2015-03-25 15:08:56 +11006060 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006061 xfs_iext_last(ifp, &icur);
6062 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07006063 stop_fsb > got.br_startoff) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07006064 *done = true;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006065 goto del_cursor;
6066 }
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006067 } else {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006068 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07006069 *done = true;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006070 goto del_cursor;
6071 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006072 }
Darrick J. Wongf9e03702019-11-11 12:52:18 -08006073 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08006074 xfs_bmap_mark_sick(ip, whichfork);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08006075 error = -EFSCORRUPTED;
6076 goto del_cursor;
6077 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006078
Brian Fosterd0c22042019-12-11 13:18:38 -08006079 if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08006080 xfs_bmap_mark_sick(ip, whichfork);
Darrick J. Wongc2414ad2019-10-28 16:12:34 -07006081 error = -EFSCORRUPTED;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07006082 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006083 }
6084
Christoph Hellwigbf806282017-10-19 11:07:34 -07006085 new_startoff = got.br_startoff + offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006086 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07006087 if (new_startoff + got.br_blockcount > next.br_startoff) {
6088 error = -EINVAL;
6089 goto del_cursor;
6090 }
6091
6092 /*
6093 * Unlike a left shift (which involves a hole punch), a right
6094 * shift does not modify extent neighbors in any way. We should
6095 * never find mergeable extents in this scenario. Check anyways
6096 * and warn if we encounter two extents that could be one.
6097 */
6098 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
6099 WARN_ON_ONCE(1);
6100 }
6101
Brian Foster0f37d172018-08-01 07:20:34 -07006102 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
6103 cur, &logflags, new_startoff);
Christoph Hellwig6b18af02017-10-19 11:07:10 -07006104 if (error)
6105 goto del_cursor;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07006106
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006107 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07006108 stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07006109 *done = true;
Christoph Hellwig6b18af02017-10-19 11:07:10 -07006110 goto del_cursor;
6111 }
Christoph Hellwig6b18af02017-10-19 11:07:10 -07006112
6113 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006114del_cursor:
6115 if (cur)
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07006116 xfs_btree_del_cursor(cur, error);
Brian Fosterca446d82014-09-02 12:12:53 +10006117 if (logflags)
6118 xfs_trans_log_inode(tp, ip, logflags);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006119 return error;
6120}
Namjae Jeona904b1c2015-03-25 15:08:56 +11006121
6122/*
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006123 * Splits an extent into two extents at split_fsb block such that it is the
6124 * first block of the current_ext. @ext is a target extent to be split.
6125 * @split_fsb is a block where the extents is split. If split_fsb lies in a
6126 * hole or the first block of extents, just return 0.
Namjae Jeona904b1c2015-03-25 15:08:56 +11006127 */
Brian Fosterb73df172020-02-26 09:43:15 -08006128int
6129xfs_bmap_split_extent(
Namjae Jeona904b1c2015-03-25 15:08:56 +11006130 struct xfs_trans *tp,
6131 struct xfs_inode *ip,
Brian Foster4b77a082018-07-11 22:26:27 -07006132 xfs_fileoff_t split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006133{
6134 int whichfork = XFS_DATA_FORK;
Darrick J. Wong732436e2022-07-09 10:56:05 -07006135 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006136 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006137 struct xfs_bmbt_irec got;
6138 struct xfs_bmbt_irec new; /* split extent */
6139 struct xfs_mount *mp = ip->i_mount;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006140 xfs_fsblock_t gotblkcnt; /* new block count for got */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006141 struct xfs_iext_cursor icur;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006142 int error = 0;
6143 int logflags = 0;
6144 int i = 0;
6145
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07006146 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
Darrick J. Wonga71895c2019-11-11 12:53:22 -08006147 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Darrick J. Wong1196f3f2024-02-22 12:31:51 -08006148 xfs_bmap_mark_sick(ip, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006149 return -EFSCORRUPTED;
6150 }
6151
Dave Chinner75c8c50f2021-08-18 18:46:53 -07006152 if (xfs_is_shutdown(mp))
Namjae Jeona904b1c2015-03-25 15:08:56 +11006153 return -EIO;
6154
Christoph Hellwig862a8042021-04-13 11:15:09 -07006155 /* Read in all the extents */
6156 error = xfs_iread_extents(tp, ip, whichfork);
6157 if (error)
6158 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006159
6160 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006161 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11006162 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006163 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006164 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006165 return 0;
6166
6167 gotblkcnt = split_fsb - got.br_startoff;
6168 new.br_startoff = split_fsb;
6169 new.br_startblock = got.br_startblock + gotblkcnt;
6170 new.br_blockcount = got.br_blockcount - gotblkcnt;
6171 new.br_state = got.br_state;
6172
Christoph Hellwigac1e0672021-04-13 11:15:11 -07006173 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006174 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07006175 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006176 if (error)
6177 goto del_cursor;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08006178 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08006179 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08006180 error = -EFSCORRUPTED;
6181 goto del_cursor;
6182 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006183 }
6184
Namjae Jeona904b1c2015-03-25 15:08:56 +11006185 got.br_blockcount = gotblkcnt;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006186 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
6187 &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006188
6189 logflags = XFS_ILOG_CORE;
6190 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07006191 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006192 if (error)
6193 goto del_cursor;
6194 } else
6195 logflags |= XFS_ILOG_DEXT;
6196
6197 /* Add new extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006198 xfs_iext_next(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07006199 xfs_iext_insert(ip, &icur, &new, 0);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07006200 ifp->if_nextents++;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006201
6202 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07006203 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006204 if (error)
6205 goto del_cursor;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08006206 if (XFS_IS_CORRUPT(mp, i != 0)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08006207 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08006208 error = -EFSCORRUPTED;
6209 goto del_cursor;
6210 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006211 error = xfs_btree_insert(cur, &i);
6212 if (error)
6213 goto del_cursor;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08006214 if (XFS_IS_CORRUPT(mp, i != 1)) {
Darrick J. Wong989d5ec2024-02-22 12:32:55 -08006215 xfs_btree_mark_sick(cur);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08006216 error = -EFSCORRUPTED;
6217 goto del_cursor;
6218 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006219 }
6220
6221 /*
6222 * Convert to a btree if necessary.
6223 */
6224 if (xfs_bmap_needs_btree(ip, whichfork)) {
6225 int tmp_logflags; /* partial log flag return val */
6226
6227 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07006228 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
6229 &tmp_logflags, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006230 logflags |= tmp_logflags;
6231 }
6232
6233del_cursor:
6234 if (cur) {
Christoph Hellwig88ee2f42024-02-22 12:37:03 -08006235 cur->bc_bmap.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07006236 xfs_btree_del_cursor(cur, error);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006237 }
6238
6239 if (logflags)
6240 xfs_trans_log_inode(tp, ip, logflags);
6241 return error;
6242}
6243
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006244/* Record a bmap intent. */
Darrick J. Wong52f80702024-02-22 12:44:32 -08006245static inline void
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006246__xfs_bmap_add(
Brian Foster0f37d172018-08-01 07:20:34 -07006247 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006248 enum xfs_bmap_intent_type type,
6249 struct xfs_inode *ip,
6250 int whichfork,
6251 struct xfs_bmbt_irec *bmap)
6252{
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006253 struct xfs_bmap_intent *bi;
6254
Darrick J. Wong52f80702024-02-22 12:44:32 -08006255 if ((whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK) ||
6256 bmap->br_startblock == HOLESTARTBLOCK ||
6257 bmap->br_startblock == DELAYSTARTBLOCK)
6258 return;
6259
Dave Chinner0b3a76e2024-01-16 09:59:46 +11006260 bi = kmem_cache_alloc(xfs_bmap_intent_cache, GFP_KERNEL | __GFP_NOFAIL);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006261 INIT_LIST_HEAD(&bi->bi_list);
6262 bi->bi_type = type;
6263 bi->bi_owner = ip;
6264 bi->bi_whichfork = whichfork;
6265 bi->bi_bmap = *bmap;
6266
Darrick J. Wong80284112024-02-22 12:44:21 -08006267 xfs_bmap_defer_add(tp, bi);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006268}
6269
6270/* Map an extent into a file. */
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006271void
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006272xfs_bmap_map_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07006273 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006274 struct xfs_inode *ip,
Darrick J. Wong52f80702024-02-22 12:44:32 -08006275 int whichfork,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006276 struct xfs_bmbt_irec *PREV)
6277{
Darrick J. Wong52f80702024-02-22 12:44:32 -08006278 __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, whichfork, PREV);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006279}
6280
6281/* Unmap an extent out of a file. */
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006282void
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006283xfs_bmap_unmap_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07006284 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006285 struct xfs_inode *ip,
Darrick J. Wong52f80702024-02-22 12:44:32 -08006286 int whichfork,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006287 struct xfs_bmbt_irec *PREV)
6288{
Darrick J. Wong52f80702024-02-22 12:44:32 -08006289 __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, whichfork, PREV);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006290}
6291
6292/*
6293 * Process one of the deferred bmap operations. We pass back the
6294 * btree cursor to maintain our lock on the bmapbt between calls.
6295 */
6296int
6297xfs_bmap_finish_one(
6298 struct xfs_trans *tp,
Darrick J. Wongddccb812023-02-01 09:50:53 -08006299 struct xfs_bmap_intent *bi)
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006300{
Darrick J. Wongddccb812023-02-01 09:50:53 -08006301 struct xfs_bmbt_irec *bmap = &bi->bi_bmap;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006302 int error = 0;
Darrick J. Wong52f80702024-02-22 12:44:32 -08006303 int flags = 0;
6304
6305 if (bi->bi_whichfork == XFS_ATTR_FORK)
6306 flags |= XFS_BMAPI_ATTRFORK;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006307
Dave Chinner692b6cd2023-02-11 04:11:06 +11006308 ASSERT(tp->t_highest_agno == NULLAGNUMBER);
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006309
Darrick J. Wong2a15e762024-02-22 12:43:53 -08006310 trace_xfs_bmap_deferred(bi);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006311
Darrick J. Wong52f80702024-02-22 12:44:32 -08006312 if (XFS_TEST_ERROR(false, tp->t_mountp, XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006313 return -EIO;
6314
Darrick J. Wongddccb812023-02-01 09:50:53 -08006315 switch (bi->bi_type) {
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006316 case XFS_BMAP_MAP:
Darrick J. Wong6c8127e2024-02-22 12:45:00 -08006317 if (bi->bi_bmap.br_state == XFS_EXT_UNWRITTEN)
6318 flags |= XFS_BMAPI_PREALLOC;
Darrick J. Wongddccb812023-02-01 09:50:53 -08006319 error = xfs_bmapi_remap(tp, bi->bi_owner, bmap->br_startoff,
Darrick J. Wong52f80702024-02-22 12:44:32 -08006320 bmap->br_blockcount, bmap->br_startblock,
6321 flags);
Darrick J. Wongddccb812023-02-01 09:50:53 -08006322 bmap->br_blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006323 break;
6324 case XFS_BMAP_UNMAP:
Darrick J. Wongddccb812023-02-01 09:50:53 -08006325 error = __xfs_bunmapi(tp, bi->bi_owner, bmap->br_startoff,
Darrick J. Wong52f80702024-02-22 12:44:32 -08006326 &bmap->br_blockcount, flags | XFS_BMAPI_REMAP,
6327 1);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006328 break;
6329 default:
6330 ASSERT(0);
Darrick J. Wong1196f3f2024-02-22 12:31:51 -08006331 xfs_bmap_mark_sick(bi->bi_owner, bi->bi_whichfork);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006332 error = -EFSCORRUPTED;
6333 }
6334
6335 return error;
6336}
Darrick J. Wong30b09842018-03-23 10:06:52 -07006337
Darrick J. Wonge744cef2023-12-15 10:03:37 -08006338/* Check that an extent does not have invalid flags or bad ranges. */
Darrick J. Wong30b09842018-03-23 10:06:52 -07006339xfs_failaddr_t
Darrick J. Wonge744cef2023-12-15 10:03:37 -08006340xfs_bmap_validate_extent_raw(
6341 struct xfs_mount *mp,
6342 bool rtfile,
Darrick J. Wong30b09842018-03-23 10:06:52 -07006343 int whichfork,
6344 struct xfs_bmbt_irec *irec)
6345{
Darrick J. Wong33005fd2020-12-04 13:28:35 -08006346 if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
Darrick J. Wongacf104c2020-12-02 12:25:43 -08006347 return __this_address;
6348
Darrick J. Wonge744cef2023-12-15 10:03:37 -08006349 if (rtfile && whichfork == XFS_DATA_FORK) {
Darrick J. Wong3d2b6d032023-10-16 09:31:22 -07006350 if (!xfs_verify_rtbext(mp, irec->br_startblock,
6351 irec->br_blockcount))
Darrick J. Wong30b09842018-03-23 10:06:52 -07006352 return __this_address;
6353 } else {
Darrick J. Wong67457eb2020-12-04 13:20:00 -08006354 if (!xfs_verify_fsbext(mp, irec->br_startblock,
6355 irec->br_blockcount))
Darrick J. Wong30b09842018-03-23 10:06:52 -07006356 return __this_address;
6357 }
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11006358 if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6359 return __this_address;
Darrick J. Wong30b09842018-03-23 10:06:52 -07006360 return NULL;
6361}
Darrick J. Wongf3c799c2021-10-12 14:11:01 -07006362
6363int __init
6364xfs_bmap_intent_init_cache(void)
6365{
6366 xfs_bmap_intent_cache = kmem_cache_create("xfs_bmap_intent",
6367 sizeof(struct xfs_bmap_intent),
6368 0, 0, NULL);
6369
6370 return xfs_bmap_intent_cache != NULL ? 0 : -ENOMEM;
6371}
6372
6373void
6374xfs_bmap_intent_destroy_cache(void)
6375{
6376 kmem_cache_destroy(xfs_bmap_intent_cache);
6377 xfs_bmap_intent_cache = NULL;
6378}
Darrick J. Wonge744cef2023-12-15 10:03:37 -08006379
6380/* Check that an inode's extent does not have invalid flags or bad ranges. */
6381xfs_failaddr_t
6382xfs_bmap_validate_extent(
6383 struct xfs_inode *ip,
6384 int whichfork,
6385 struct xfs_bmbt_irec *irec)
6386{
6387 return xfs_bmap_validate_extent_raw(ip->i_mount,
6388 XFS_IS_REALTIME_INODE(ip), whichfork, irec);
6389}
Darrick J. Wonga59eb5f2023-12-15 10:03:43 -08006390
6391/*
6392 * Used in xfs_itruncate_extents(). This is the maximum number of extents
6393 * freed from a file in a single transaction.
6394 */
6395#define XFS_ITRUNC_MAX_EXTENTS 2
6396
6397/*
6398 * Unmap every extent in part of an inode's fork. We don't do any higher level
6399 * invalidation work at all.
6400 */
6401int
6402xfs_bunmapi_range(
6403 struct xfs_trans **tpp,
6404 struct xfs_inode *ip,
6405 uint32_t flags,
6406 xfs_fileoff_t startoff,
6407 xfs_fileoff_t endoff)
6408{
6409 xfs_filblks_t unmap_len = endoff - startoff + 1;
6410 int error = 0;
6411
Matthew Wilcox (Oracle)3fed24f2024-02-19 15:41:12 +00006412 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
Darrick J. Wonga59eb5f2023-12-15 10:03:43 -08006413
6414 while (unmap_len > 0) {
6415 ASSERT((*tpp)->t_highest_agno == NULLAGNUMBER);
6416 error = __xfs_bunmapi(*tpp, ip, startoff, &unmap_len, flags,
6417 XFS_ITRUNC_MAX_EXTENTS);
6418 if (error)
6419 goto out;
6420
6421 /* free the just unmapped extents */
6422 error = xfs_defer_finish(tpp);
6423 if (error)
6424 goto out;
Ritesh Harjani (IBM)b0c6bcd2024-05-07 15:06:55 +05306425 cond_resched();
Darrick J. Wonga59eb5f2023-12-15 10:03:43 -08006426 }
6427out:
6428 return error;
6429}
Darrick J. Wong32080a92024-02-22 12:43:38 -08006430
6431struct xfs_bmap_query_range {
6432 xfs_bmap_query_range_fn fn;
6433 void *priv;
6434};
6435
6436/* Format btree record and pass to our callback. */
6437STATIC int
6438xfs_bmap_query_range_helper(
6439 struct xfs_btree_cur *cur,
6440 const union xfs_btree_rec *rec,
6441 void *priv)
6442{
6443 struct xfs_bmap_query_range *query = priv;
6444 struct xfs_bmbt_irec irec;
6445 xfs_failaddr_t fa;
6446
6447 xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
6448 fa = xfs_bmap_validate_extent(cur->bc_ino.ip, cur->bc_ino.whichfork,
6449 &irec);
6450 if (fa) {
6451 xfs_btree_mark_sick(cur);
6452 return xfs_bmap_complain_bad_rec(cur->bc_ino.ip,
6453 cur->bc_ino.whichfork, fa, &irec);
6454 }
6455
6456 return query->fn(cur, &irec, query->priv);
6457}
6458
6459/* Find all bmaps. */
6460int
6461xfs_bmap_query_all(
6462 struct xfs_btree_cur *cur,
6463 xfs_bmap_query_range_fn fn,
6464 void *priv)
6465{
6466 struct xfs_bmap_query_range query = {
6467 .priv = priv,
6468 .fn = fn,
6469 };
6470
6471 return xfs_btree_query_all(cur, xfs_bmap_query_range_helper, &query);
6472}
Darrick J. Wongacdddbe2024-07-02 11:22:31 -07006473
6474/* Helper function to extract extent size hint from inode */
6475xfs_extlen_t
6476xfs_get_extsz_hint(
6477 struct xfs_inode *ip)
6478{
6479 /*
6480 * No point in aligning allocations if we need to COW to actually
6481 * write to them.
6482 */
6483 if (xfs_is_always_cow_inode(ip))
6484 return 0;
6485 if ((ip->i_diflags & XFS_DIFLAG_EXTSIZE) && ip->i_extsize)
6486 return ip->i_extsize;
6487 if (XFS_IS_REALTIME_INODE(ip) &&
6488 ip->i_mount->m_sb.sb_rextsize > 1)
6489 return ip->i_mount->m_sb.sb_rextsize;
6490 return 0;
6491}
6492
6493/*
6494 * Helper function to extract CoW extent size hint from inode.
6495 * Between the extent size hint and the CoW extent size hint, we
6496 * return the greater of the two. If the value is zero (automatic),
6497 * use the default size.
6498 */
6499xfs_extlen_t
6500xfs_get_cowextsz_hint(
6501 struct xfs_inode *ip)
6502{
6503 xfs_extlen_t a, b;
6504
6505 a = 0;
6506 if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
6507 a = ip->i_cowextsize;
6508 b = xfs_get_extsz_hint(ip);
6509
6510 a = max(a, b);
6511 if (a == 0)
6512 return XFS_DEFAULT_COWEXTSZ_HINT;
6513 return a;
6514}