blob: 7eb6b28a4c30a8aa9b68cda2c49a87ddffbc94ed [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_rtalloc.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"
Darrick J. Wong3fd129b2016-09-19 10:30:52 +100034#include "xfs_ag_resv.h"
Darrick J. Wong62aab202016-10-03 09:11:23 -070035#include "xfs_refcount.h"
Brian Foster974ae922016-11-28 14:57:42 +110036#include "xfs_icache.h"
Christoph Hellwig4e087a32019-10-17 13:12:06 -070037#include "xfs_iomap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040kmem_zone_t *xfs_bmap_free_item_zone;
41
42/*
Dave Chinner9e5987a72013-02-25 12:31:26 +110043 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 */
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
Dave Chinner9e5987a72013-02-25 12:31:26 +110047 * Compute and fill in the value of the maximum depth of a bmap btree
48 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 */
Dave Chinner9e5987a72013-02-25 12:31:26 +110050void
51xfs_bmap_compute_maxlevels(
52 xfs_mount_t *mp, /* file system mount structure */
53 int whichfork) /* data or attr fork */
54{
55 int level; /* btree level */
56 uint maxblocks; /* max blocks at this level */
57 uint maxleafents; /* max leaf entries possible */
58 int maxrootrecs; /* max records in root block */
59 int minleafrecs; /* min records in leaf block */
60 int minnoderecs; /* min records in node block */
61 int sz; /* root block size */
62
63 /*
Christoph Hellwigdaf83962020-05-18 10:27:22 -070064 * The maximum number of extents in a file, hence the maximum number of
65 * leaf entries, is controlled by the size of the on-disk extent count,
66 * either a signed 32-bit number for the data fork, or a signed 16-bit
67 * number for the attr fork.
Dave Chinner9e5987a72013-02-25 12:31:26 +110068 *
Christoph Hellwig7821ea32021-03-29 11:11:44 -070069 * Note that we can no longer assume that if we are in ATTR1 that the
70 * fork offset of all the inodes will be
71 * (xfs_default_attroffset(ip) >> 3) because we could have mounted with
72 * ATTR2 and then mounted back with ATTR1, keeping the i_forkoff's fixed
73 * but probably at various positions. Therefore, for both ATTR1 and
74 * ATTR2 we have to assume the worst case scenario of a minimum size
75 * available.
Dave Chinner9e5987a72013-02-25 12:31:26 +110076 */
77 if (whichfork == XFS_DATA_FORK) {
78 maxleafents = MAXEXTNUM;
79 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
80 } else {
81 maxleafents = MAXAEXTNUM;
82 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
83 }
Eric Sandeen152d93b2014-04-14 18:58:51 +100084 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +110085 minleafrecs = mp->m_bmap_dmnr[0];
86 minnoderecs = mp->m_bmap_dmnr[1];
87 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
88 for (level = 1; maxblocks > 1; level++) {
89 if (maxblocks <= maxrootrecs)
90 maxblocks = 1;
91 else
92 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
93 }
94 mp->m_bm_maxlevels[whichfork] = level;
95}
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Dave Chinnerb2941042021-04-06 07:03:24 -070097unsigned int
98xfs_bmap_compute_attr_offset(
99 struct xfs_mount *mp)
100{
101 if (mp->m_sb.sb_inodesize == 256)
102 return XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
103 return XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
104}
105
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100106STATIC int /* error */
107xfs_bmbt_lookup_eq(
108 struct xfs_btree_cur *cur,
Christoph Hellwige16cf9b2017-10-17 14:16:26 -0700109 struct xfs_bmbt_irec *irec,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100110 int *stat) /* success/failure */
111{
Christoph Hellwige16cf9b2017-10-17 14:16:26 -0700112 cur->bc_rec.b = *irec;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100113 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
114}
115
116STATIC int /* error */
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700117xfs_bmbt_lookup_first(
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100118 struct xfs_btree_cur *cur,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100119 int *stat) /* success/failure */
120{
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700121 cur->bc_rec.b.br_startoff = 0;
122 cur->bc_rec.b.br_startblock = 0;
123 cur->bc_rec.b.br_blockcount = 0;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100124 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
125}
126
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100127/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000128 * Check if the inode needs to be converted to btree format.
129 */
130static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
131{
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700132 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
133
Darrick J. Wong60b49842016-10-03 09:11:34 -0700134 return whichfork != XFS_COW_FORK &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700135 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700136 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000137}
138
139/*
140 * Check if the inode should be converted to extent format.
141 */
142static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
143{
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700144 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
145
Darrick J. Wong60b49842016-10-03 09:11:34 -0700146 return whichfork != XFS_COW_FORK &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700147 ifp->if_format == XFS_DINODE_FMT_BTREE &&
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700148 ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000149}
150
151/*
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700152 * Update the record referred to by cur to the value given by irec
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100153 * This either works (return 0) or gets an EFSCORRUPTED error.
154 */
155STATIC int
156xfs_bmbt_update(
157 struct xfs_btree_cur *cur,
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700158 struct xfs_bmbt_irec *irec)
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100159{
160 union xfs_btree_rec rec;
161
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700162 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100163 return xfs_btree_update(cur, &rec);
164}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/*
Dave Chinner9e5987a72013-02-25 12:31:26 +1100167 * Compute the worst-case number of indirect blocks that will be used
168 * for ip's delayed extent of length "len".
169 */
170STATIC xfs_filblks_t
171xfs_bmap_worst_indlen(
172 xfs_inode_t *ip, /* incore inode pointer */
173 xfs_filblks_t len) /* delayed extent length */
174{
175 int level; /* btree level number */
176 int maxrecs; /* maximum record count at this level */
177 xfs_mount_t *mp; /* mount structure */
178 xfs_filblks_t rval; /* return value */
179
180 mp = ip->i_mount;
181 maxrecs = mp->m_bmap_dmxr[0];
182 for (level = 0, rval = 0;
183 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
184 level++) {
185 len += maxrecs - 1;
186 do_div(len, maxrecs);
187 rval += len;
Darrick J. Wong5e5c9432017-09-18 09:41:17 -0700188 if (len == 1)
189 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
Dave Chinner9e5987a72013-02-25 12:31:26 +1100190 level - 1;
191 if (level == 0)
192 maxrecs = mp->m_bmap_dmxr[1];
193 }
194 return rval;
195}
196
197/*
198 * Calculate the default attribute fork offset for newly created inodes.
199 */
200uint
201xfs_default_attroffset(
202 struct xfs_inode *ip)
203{
Dave Chinner683ec9b2021-04-06 07:02:04 -0700204 if (ip->i_df.if_format == XFS_DINODE_FMT_DEV)
205 return roundup(sizeof(xfs_dev_t), 8);
Dave Chinnerb2941042021-04-06 07:03:24 -0700206 return M_IGEO(ip->i_mount)->attr_fork_offset;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100207}
208
209/*
Christoph Hellwig7821ea32021-03-29 11:11:44 -0700210 * Helper routine to reset inode i_forkoff field when switching attribute fork
211 * from local to extent format - we reset it where possible to make space
212 * available for inline data fork extents.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100213 */
214STATIC void
215xfs_bmap_forkoff_reset(
Dave Chinner9e5987a72013-02-25 12:31:26 +1100216 xfs_inode_t *ip,
217 int whichfork)
218{
219 if (whichfork == XFS_ATTR_FORK &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700220 ip->i_df.if_format != XFS_DINODE_FMT_DEV &&
221 ip->i_df.if_format != XFS_DINODE_FMT_BTREE) {
Dave Chinner9e5987a72013-02-25 12:31:26 +1100222 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
223
Christoph Hellwig7821ea32021-03-29 11:11:44 -0700224 if (dfl_forkoff > ip->i_forkoff)
225 ip->i_forkoff = dfl_forkoff;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100226 }
227}
228
Dave Chinner9e5987a72013-02-25 12:31:26 +1100229#ifdef DEBUG
230STATIC struct xfs_buf *
231xfs_bmap_get_bp(
232 struct xfs_btree_cur *cur,
233 xfs_fsblock_t bno)
234{
Dave Chinnere6631f82018-05-09 07:49:37 -0700235 struct xfs_log_item *lip;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100236 int i;
237
238 if (!cur)
239 return NULL;
240
241 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
242 if (!cur->bc_bufs[i])
243 break;
244 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
245 return cur->bc_bufs[i];
246 }
247
248 /* Chase down all the log items to see if the bp is there */
Dave Chinnere6631f82018-05-09 07:49:37 -0700249 list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
250 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
251
Dave Chinner9e5987a72013-02-25 12:31:26 +1100252 if (bip->bli_item.li_type == XFS_LI_BUF &&
253 XFS_BUF_ADDR(bip->bli_buf) == bno)
254 return bip->bli_buf;
255 }
256
257 return NULL;
258}
259
260STATIC void
261xfs_check_block(
262 struct xfs_btree_block *block,
263 xfs_mount_t *mp,
264 int root,
265 short sz)
266{
267 int i, j, dmxr;
268 __be64 *pp, *thispa; /* pointer to block address */
269 xfs_bmbt_key_t *prevp, *keyp;
270
271 ASSERT(be16_to_cpu(block->bb_level) > 0);
272
273 prevp = NULL;
274 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
275 dmxr = mp->m_bmap_dmxr[0];
276 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
277
278 if (prevp) {
279 ASSERT(be64_to_cpu(prevp->br_startoff) <
280 be64_to_cpu(keyp->br_startoff));
281 }
282 prevp = keyp;
283
284 /*
285 * Compare the block numbers to see if there are dups.
286 */
287 if (root)
288 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
289 else
290 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
291
292 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
293 if (root)
294 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
295 else
296 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
297 if (*thispa == *pp) {
298 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
299 __func__, j, i,
300 (unsigned long long)be64_to_cpu(*thispa));
Darrick J. Wongcec57252018-05-04 15:31:21 -0700301 xfs_err(mp, "%s: ptrs are equal in node\n",
Dave Chinner9e5987a72013-02-25 12:31:26 +1100302 __func__);
Darrick J. Wongcec57252018-05-04 15:31:21 -0700303 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100304 }
305 }
306 }
307}
308
309/*
310 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100311 * btree leaves. THis becomes prohibitively expensive for large extent count
312 * files, so don't bother with inodes that have more than 10,000 extents in
313 * them. The btree record ordering checks will still be done, so for such large
314 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100315 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100316STATIC void
317xfs_bmap_check_leaf_extents(
318 xfs_btree_cur_t *cur, /* btree cursor or null */
319 xfs_inode_t *ip, /* incore inode pointer */
320 int whichfork) /* data or attr fork */
321{
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700322 struct xfs_mount *mp = ip->i_mount;
323 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100324 struct xfs_btree_block *block; /* current btree block */
325 xfs_fsblock_t bno; /* block # of "block" */
Dave Chinnere8222612020-12-16 16:07:34 -0800326 struct xfs_buf *bp; /* buffer for "block" */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100327 int error; /* error return value */
328 xfs_extnum_t i=0, j; /* index into the extents list */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100329 int level; /* btree level, for checking */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100330 __be64 *pp; /* pointer to block address */
331 xfs_bmbt_rec_t *ep; /* pointer to current extent */
332 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
333 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
334 int bp_release = 0;
335
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700336 if (ifp->if_format != XFS_DINODE_FMT_BTREE)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100337 return;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100338
Dave Chinnere3543812016-01-08 11:28:49 +1100339 /* skip large extent count inodes */
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700340 if (ip->i_df.if_nextents > 10000)
Dave Chinnere3543812016-01-08 11:28:49 +1100341 return;
342
Dave Chinner9e5987a72013-02-25 12:31:26 +1100343 bno = NULLFSBLOCK;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100344 block = ifp->if_broot;
345 /*
346 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
347 */
348 level = be16_to_cpu(block->bb_level);
349 ASSERT(level > 0);
350 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
351 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
352 bno = be64_to_cpu(*pp);
353
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000354 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100355 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
356 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
357
358 /*
359 * Go down the tree until leaf level is reached, following the first
360 * pointer (leftmost) at each level.
361 */
362 while (level-- > 0) {
363 /* See if buf is in cur first */
364 bp_release = 0;
365 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
366 if (!bp) {
367 bp_release = 1;
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700368 error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100369 XFS_BMAP_BTREE_REF,
370 &xfs_bmbt_buf_ops);
371 if (error)
372 goto error_norelse;
373 }
374 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100375 if (level == 0)
376 break;
377
378 /*
379 * Check this block for basic sanity (increasing keys and
380 * no duplicate blocks).
381 */
382
383 xfs_check_block(block, mp, 0, 0);
384 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
385 bno = be64_to_cpu(*pp);
Darrick J. Wongf9e03702019-11-11 12:52:18 -0800386 if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbno(mp, bno))) {
387 error = -EFSCORRUPTED;
388 goto error0;
389 }
Dave Chinner9e5987a72013-02-25 12:31:26 +1100390 if (bp_release) {
391 bp_release = 0;
392 xfs_trans_brelse(NULL, bp);
393 }
394 }
395
396 /*
397 * Here with bp and block set to the leftmost leaf node in the tree.
398 */
399 i = 0;
400
401 /*
402 * Loop over all leaf nodes checking that all extents are in the right order.
403 */
404 for (;;) {
405 xfs_fsblock_t nextbno;
406 xfs_extnum_t num_recs;
407
408
409 num_recs = xfs_btree_get_numrecs(block);
410
411 /*
412 * Read-ahead the next leaf block, if any.
413 */
414
415 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
416
417 /*
418 * Check all the extents to make sure they are OK.
419 * If we had a previous block, the last entry should
420 * conform with the first entry in this one.
421 */
422
423 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
424 if (i) {
425 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
426 xfs_bmbt_disk_get_blockcount(&last) <=
427 xfs_bmbt_disk_get_startoff(ep));
428 }
429 for (j = 1; j < num_recs; j++) {
430 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
431 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
432 xfs_bmbt_disk_get_blockcount(ep) <=
433 xfs_bmbt_disk_get_startoff(nextp));
434 ep = nextp;
435 }
436
437 last = *ep;
438 i += num_recs;
439 if (bp_release) {
440 bp_release = 0;
441 xfs_trans_brelse(NULL, bp);
442 }
443 bno = nextbno;
444 /*
445 * If we've reached the end, stop.
446 */
447 if (bno == NULLFSBLOCK)
448 break;
449
450 bp_release = 0;
451 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
452 if (!bp) {
453 bp_release = 1;
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700454 error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100455 XFS_BMAP_BTREE_REF,
456 &xfs_bmbt_buf_ops);
457 if (error)
458 goto error_norelse;
459 }
460 block = XFS_BUF_TO_BLOCK(bp);
461 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100462
Dave Chinner9e5987a72013-02-25 12:31:26 +1100463 return;
464
465error0:
466 xfs_warn(mp, "%s: at error0", __func__);
467 if (bp_release)
468 xfs_trans_brelse(NULL, bp);
469error_norelse:
470 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
471 __func__, i);
Darrick J. Wongcec57252018-05-04 15:31:21 -0700472 xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
473 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100474 return;
475}
476
477/*
Dave Chinner9e5987a72013-02-25 12:31:26 +1100478 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000479 * given the caller's original parameters. Specifically check the
480 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a72013-02-25 12:31:26 +1100481 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
482 */
483STATIC void
484xfs_bmap_validate_ret(
485 xfs_fileoff_t bno,
486 xfs_filblks_t len,
487 int flags,
488 xfs_bmbt_irec_t *mval,
489 int nmap,
490 int ret_nmap)
491{
492 int i; /* index to map values */
493
494 ASSERT(ret_nmap <= nmap);
495
496 for (i = 0; i < ret_nmap; i++) {
497 ASSERT(mval[i].br_blockcount > 0);
498 if (!(flags & XFS_BMAPI_ENTIRE)) {
499 ASSERT(mval[i].br_startoff >= bno);
500 ASSERT(mval[i].br_blockcount <= len);
501 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
502 bno + len);
503 } else {
504 ASSERT(mval[i].br_startoff < bno + len);
505 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
506 bno);
507 }
508 ASSERT(i == 0 ||
509 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
510 mval[i].br_startoff);
511 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
512 mval[i].br_startblock != HOLESTARTBLOCK);
513 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
514 mval[i].br_state == XFS_EXT_UNWRITTEN);
515 }
516}
517
518#else
519#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
Darrick J. Wong7bf7a192017-08-31 15:11:06 -0700520#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100521#endif /* DEBUG */
522
523/*
524 * bmap free list manipulation functions
525 */
526
527/*
528 * Add the extent to the list of extents to be free at transaction end.
529 * The list is maintained sorted (by block number).
530 */
531void
Brian Fosterfcb762f2018-05-09 08:45:04 -0700532__xfs_bmap_add_free(
Brian Foster0f37d172018-08-01 07:20:34 -0700533 struct xfs_trans *tp,
Darrick J. Wong340785c2016-08-03 11:33:42 +1000534 xfs_fsblock_t bno,
535 xfs_filblks_t len,
Darrick J. Wong66e32372018-12-12 08:46:23 -0800536 const struct xfs_owner_info *oinfo,
Brian Fosterfcb762f2018-05-09 08:45:04 -0700537 bool skip_discard)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100538{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000539 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100540#ifdef DEBUG
Brian Foster0f37d172018-08-01 07:20:34 -0700541 struct xfs_mount *mp = tp->t_mountp;
542 xfs_agnumber_t agno;
543 xfs_agblock_t agbno;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100544
545 ASSERT(bno != NULLFSBLOCK);
546 ASSERT(len > 0);
547 ASSERT(len <= MAXEXTLEN);
548 ASSERT(!isnullstartblock(bno));
549 agno = XFS_FSB_TO_AGNO(mp, bno);
550 agbno = XFS_FSB_TO_AGBNO(mp, bno);
551 ASSERT(agno < mp->m_sb.sb_agcount);
552 ASSERT(agbno < mp->m_sb.sb_agblocks);
553 ASSERT(len < mp->m_sb.sb_agblocks);
554 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
555#endif
556 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000557
Carlos Maiolino3050bd02020-07-22 09:23:04 -0700558 new = kmem_cache_alloc(xfs_bmap_free_item_zone,
559 GFP_KERNEL | __GFP_NOFAIL);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000560 new->xefi_startblock = bno;
561 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000562 if (oinfo)
563 new->xefi_oinfo = *oinfo;
564 else
Darrick J. Wong7280fed2018-12-12 08:46:23 -0800565 new->xefi_oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
Brian Fosterfcb762f2018-05-09 08:45:04 -0700566 new->xefi_skip_discard = skip_discard;
Brian Foster0f37d172018-08-01 07:20:34 -0700567 trace_xfs_bmap_free_defer(tp->t_mountp,
568 XFS_FSB_TO_AGNO(tp->t_mountp, bno), 0,
569 XFS_FSB_TO_AGBNO(tp->t_mountp, bno), len);
570 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100571}
572
573/*
574 * Inode fork format manipulation functions
575 */
576
577/*
Christoph Hellwigb101e332019-02-15 08:02:47 -0800578 * Convert the inode format to extent format if it currently is in btree format,
579 * but the extent list is small enough that it fits into the extent format.
580 *
581 * Since the extents are already in-core, all we have to do is give up the space
582 * for the btree root and pitch the leaf block.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100583 */
584STATIC int /* error */
585xfs_bmap_btree_to_extents(
Christoph Hellwigb101e332019-02-15 08:02:47 -0800586 struct xfs_trans *tp, /* transaction pointer */
587 struct xfs_inode *ip, /* incore inode pointer */
588 struct xfs_btree_cur *cur, /* btree cursor */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100589 int *logflagsp, /* inode logging flags */
590 int whichfork) /* data or attr fork */
591{
Christoph Hellwigb101e332019-02-15 08:02:47 -0800592 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
593 struct xfs_mount *mp = ip->i_mount;
594 struct xfs_btree_block *rblock = ifp->if_broot;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100595 struct xfs_btree_block *cblock;/* child btree block */
596 xfs_fsblock_t cbno; /* child block number */
Dave Chinnere8222612020-12-16 16:07:34 -0800597 struct xfs_buf *cbp; /* child block's buffer */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100598 int error; /* error return value */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100599 __be64 *pp; /* ptr to block address */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000600 struct xfs_owner_info oinfo;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100601
Christoph Hellwigb101e332019-02-15 08:02:47 -0800602 /* check if we actually need the extent format first: */
603 if (!xfs_bmap_wants_extents(ip, whichfork))
604 return 0;
605
606 ASSERT(cur);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700607 ASSERT(whichfork != XFS_COW_FORK);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700608 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100609 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
610 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
611 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
Christoph Hellwigb101e332019-02-15 08:02:47 -0800612
Dave Chinner9e5987a72013-02-25 12:31:26 +1100613 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
614 cbno = be64_to_cpu(*pp);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100615#ifdef DEBUG
Darrick J. Wongf9e03702019-11-11 12:52:18 -0800616 if (XFS_IS_CORRUPT(cur->bc_mp, !xfs_btree_check_lptr(cur, cbno, 1)))
617 return -EFSCORRUPTED;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100618#endif
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700619 error = xfs_btree_read_bufl(mp, tp, cbno, &cbp, XFS_BMAP_BTREE_REF,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100620 &xfs_bmbt_buf_ops);
621 if (error)
622 return error;
623 cblock = XFS_BUF_TO_BLOCK(cbp);
624 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
625 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000626 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
Brian Foster0f37d172018-08-01 07:20:34 -0700627 xfs_bmap_add_free(cur->bc_tp, cbno, 1, &oinfo);
Christoph Hellwig6e73a542021-03-29 11:11:40 -0700628 ip->i_nblocks--;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100629 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
630 xfs_trans_binval(tp, cbp);
631 if (cur->bc_bufs[0] == cbp)
632 cur->bc_bufs[0] = NULL;
633 xfs_iroot_realloc(ip, -1, whichfork);
634 ASSERT(ifp->if_broot == NULL);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700635 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
Christoph Hellwigb101e332019-02-15 08:02:47 -0800636 *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100637 return 0;
638}
639
640/*
641 * Convert an extents-format file into a btree-format file.
642 * The new file will have a root block (in the inode) and a single child block.
643 */
644STATIC int /* error */
645xfs_bmap_extents_to_btree(
Brian Foster81ba8f32018-07-11 22:26:16 -0700646 struct xfs_trans *tp, /* transaction pointer */
647 struct xfs_inode *ip, /* incore inode pointer */
Brian Foster81ba8f32018-07-11 22:26:16 -0700648 struct xfs_btree_cur **curp, /* cursor returned to caller */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100649 int wasdel, /* converting a delayed alloc */
650 int *logflagsp, /* inode logging flags */
651 int whichfork) /* data or attr fork */
652{
653 struct xfs_btree_block *ablock; /* allocated (child) bt block */
Brian Foster81ba8f32018-07-11 22:26:16 -0700654 struct xfs_buf *abp; /* buffer for ablock */
655 struct xfs_alloc_arg args; /* allocation arguments */
656 struct xfs_bmbt_rec *arp; /* child record pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100657 struct xfs_btree_block *block; /* btree root block */
Brian Foster81ba8f32018-07-11 22:26:16 -0700658 struct xfs_btree_cur *cur; /* bmap btree cursor */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100659 int error; /* error return value */
Brian Foster81ba8f32018-07-11 22:26:16 -0700660 struct xfs_ifork *ifp; /* inode fork pointer */
661 struct xfs_bmbt_key *kp; /* root block key pointer */
662 struct xfs_mount *mp; /* mount structure */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100663 xfs_bmbt_ptr_t *pp; /* root block address pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700664 struct xfs_iext_cursor icur;
Christoph Hellwig906abed2017-11-03 10:34:43 -0700665 struct xfs_bmbt_irec rec;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700666 xfs_extnum_t cnt = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100667
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500668 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700669 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100670 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700671 ASSERT(ifp->if_format == XFS_DINODE_FMT_EXTENTS);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100672
673 /*
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000674 * Make space in the inode incore. This needs to be undone if we fail
675 * to expand the root.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100676 */
677 xfs_iroot_realloc(ip, 1, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100678
679 /*
680 * Fill in the root.
681 */
682 block = ifp->if_broot;
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800683 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
684 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
Eric Sandeenf88ae462017-01-27 23:16:37 -0800685 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100686 /*
687 * Need a cursor. Can't allocate until bb_level is filled in.
688 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100689 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner8ef54792020-03-10 17:54:38 -0700690 cur->bc_ino.flags = wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100691 /*
692 * Convert to a btree with two levels, one record in root.
693 */
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700694 ifp->if_format = XFS_DINODE_FMT_BTREE;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100695 memset(&args, 0, sizeof(args));
696 args.tp = tp;
697 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000698 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Brian Foster280253d2018-07-11 22:26:29 -0700699 if (tp->t_firstblock == NULLFSBLOCK) {
Dave Chinner9e5987a72013-02-25 12:31:26 +1100700 args.type = XFS_ALLOCTYPE_START_BNO;
701 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Brian Foster1214f1c2018-08-01 07:20:31 -0700702 } else if (tp->t_flags & XFS_TRANS_LOWMODE) {
Dave Chinner9e5987a72013-02-25 12:31:26 +1100703 args.type = XFS_ALLOCTYPE_START_BNO;
Brian Foster280253d2018-07-11 22:26:29 -0700704 args.fsbno = tp->t_firstblock;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100705 } else {
706 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Brian Foster280253d2018-07-11 22:26:29 -0700707 args.fsbno = tp->t_firstblock;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100708 }
709 args.minlen = args.maxlen = args.prod = 1;
710 args.wasdel = wasdel;
711 *logflagsp = 0;
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000712 error = xfs_alloc_vextent(&args);
713 if (error)
714 goto out_root_realloc;
Darrick J. Wong90e20562016-10-03 09:11:45 -0700715
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800716 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
Shan Hai01239d772018-08-10 17:55:55 -0700717 error = -ENOSPC;
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000718 goto out_root_realloc;
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800719 }
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000720
Dave Chinner9e5987a72013-02-25 12:31:26 +1100721 /*
722 * Allocation can't fail, the space was reserved.
723 */
Brian Foster280253d2018-07-11 22:26:29 -0700724 ASSERT(tp->t_firstblock == NULLFSBLOCK ||
725 args.agno >= XFS_FSB_TO_AGNO(mp, tp->t_firstblock));
Brian Fostercf612de2018-07-11 22:26:29 -0700726 tp->t_firstblock = args.fsbno;
Dave Chinner92219c22020-03-10 17:52:53 -0700727 cur->bc_ino.allocated++;
Christoph Hellwig6e73a542021-03-29 11:11:40 -0700728 ip->i_nblocks++;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100729 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
Darrick J. Wongee647f82020-01-23 17:01:19 -0800730 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
731 XFS_FSB_TO_DADDR(mp, args.fsbno),
732 mp->m_bsize, 0, &abp);
733 if (error)
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000734 goto out_unreserve_dquot;
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000735
Dave Chinner9e5987a72013-02-25 12:31:26 +1100736 /*
737 * Fill in the child block.
738 */
739 abp->b_ops = &xfs_bmbt_buf_ops;
740 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800741 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
742 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500743 XFS_BTREE_LONG_PTRS);
744
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700745 for_each_xfs_iext(ifp, &icur, &rec) {
Christoph Hellwig906abed2017-11-03 10:34:43 -0700746 if (isnullstartblock(rec.br_startblock))
747 continue;
748 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
749 xfs_bmbt_disk_set_all(arp, &rec);
750 cnt++;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100751 }
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700752 ASSERT(cnt == ifp->if_nextents);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100753 xfs_btree_set_numrecs(ablock, cnt);
754
755 /*
756 * Fill in the root key and pointer.
757 */
758 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
759 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
760 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
761 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
762 be16_to_cpu(block->bb_level)));
763 *pp = cpu_to_be64(args.fsbno);
764
765 /*
766 * Do all this logging at the end so that
767 * the root is at the right level.
768 */
769 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
770 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
771 ASSERT(*curp == NULL);
772 *curp = cur;
773 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
774 return 0;
Shan Hai01239d772018-08-10 17:55:55 -0700775
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000776out_unreserve_dquot:
Shan Hai01239d772018-08-10 17:55:55 -0700777 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000778out_root_realloc:
Shan Hai01239d772018-08-10 17:55:55 -0700779 xfs_iroot_realloc(ip, -1, whichfork);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700780 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000781 ASSERT(ifp->if_broot == NULL);
Shan Hai01239d772018-08-10 17:55:55 -0700782 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
783
784 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100785}
786
787/*
788 * Convert a local file to an extents file.
789 * This code is out of bounds for data forks of regular files,
790 * since the file data needs to get logged so things will stay consistent.
791 * (The bmap-level manipulations are ok, though).
792 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000793void
794xfs_bmap_local_to_extents_empty(
Brian Fosteraeea4b72019-10-07 12:54:16 -0700795 struct xfs_trans *tp,
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000796 struct xfs_inode *ip,
797 int whichfork)
798{
799 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
800
Darrick J. Wong60b49842016-10-03 09:11:34 -0700801 ASSERT(whichfork != XFS_COW_FORK);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700802 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000803 ASSERT(ifp->if_bytes == 0);
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700804 ASSERT(ifp->if_nextents == 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000805
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000806 xfs_bmap_forkoff_reset(ip, whichfork);
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700807 ifp->if_u1.if_root = NULL;
808 ifp->if_height = 0;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700809 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
Brian Fosteraeea4b72019-10-07 12:54:16 -0700810 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000811}
812
813
Dave Chinner9e5987a72013-02-25 12:31:26 +1100814STATIC int /* error */
815xfs_bmap_local_to_extents(
816 xfs_trans_t *tp, /* transaction pointer */
817 xfs_inode_t *ip, /* incore inode pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100818 xfs_extlen_t total, /* total blocks needed by transaction */
819 int *logflagsp, /* inode logging flags */
820 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500821 void (*init_fn)(struct xfs_trans *tp,
822 struct xfs_buf *bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100823 struct xfs_inode *ip,
824 struct xfs_ifork *ifp))
825{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000826 int error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100827 int flags; /* logging flags returned */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -0700828 struct xfs_ifork *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000829 xfs_alloc_arg_t args; /* allocation arguments */
Dave Chinnere8222612020-12-16 16:07:34 -0800830 struct xfs_buf *bp; /* buffer for extent block */
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700831 struct xfs_bmbt_irec rec;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700832 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100833
834 /*
835 * We don't want to deal with the case of keeping inode data inline yet.
836 * So sending the data fork of a regular inode is invalid.
837 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100838 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100839 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700840 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000841
842 if (!ifp->if_bytes) {
Brian Fosteraeea4b72019-10-07 12:54:16 -0700843 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000844 flags = XFS_ILOG_CORE;
845 goto done;
846 }
847
Dave Chinner9e5987a72013-02-25 12:31:26 +1100848 flags = 0;
849 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000850 memset(&args, 0, sizeof(args));
851 args.tp = tp;
852 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000853 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000854 /*
855 * Allocate a block. We know we need only one, since the
856 * file currently fits in an inode.
857 */
Brian Foster280253d2018-07-11 22:26:29 -0700858 if (tp->t_firstblock == NULLFSBLOCK) {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000859 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
860 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100861 } else {
Brian Foster280253d2018-07-11 22:26:29 -0700862 args.fsbno = tp->t_firstblock;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000863 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100864 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000865 args.total = total;
866 args.minlen = args.maxlen = args.prod = 1;
867 error = xfs_alloc_vextent(&args);
868 if (error)
869 goto done;
870
871 /* Can't fail, the space was reserved. */
872 ASSERT(args.fsbno != NULLFSBLOCK);
873 ASSERT(args.len == 1);
Brian Foster280253d2018-07-11 22:26:29 -0700874 tp->t_firstblock = args.fsbno;
Darrick J. Wongee647f82020-01-23 17:01:19 -0800875 error = xfs_trans_get_buf(tp, args.mp->m_ddev_targp,
876 XFS_FSB_TO_DADDR(args.mp, args.fsbno),
877 args.mp->m_bsize, 0, &bp);
878 if (error)
879 goto done;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000880
Dave Chinnerfe22d552015-01-22 09:30:06 +1100881 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100882 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100883 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100884 * The callout is responsible for logging because the remote format
885 * might differ from the local format and thus we don't know how much to
886 * log here. Note that init_fn must also set the buffer log item type
887 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100888 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000889 init_fn(tp, bp, ip, ifp);
890
Brian Fosterb7cdc662015-10-12 15:40:24 +1100891 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000892 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
Brian Fosteraeea4b72019-10-07 12:54:16 -0700893 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100894 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000895
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700896 ifp->if_u1.if_root = NULL;
897 ifp->if_height = 0;
898
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700899 rec.br_startoff = 0;
900 rec.br_startblock = args.fsbno;
901 rec.br_blockcount = 1;
902 rec.br_state = XFS_EXT_NORM;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700903 xfs_iext_first(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -0700904 xfs_iext_insert(ip, &icur, &rec, 0);
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700905
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700906 ifp->if_nextents = 1;
Christoph Hellwig6e73a542021-03-29 11:11:40 -0700907 ip->i_nblocks = 1;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000908 xfs_trans_mod_dquot_byino(tp, ip,
909 XFS_TRANS_DQ_BCOUNT, 1L);
910 flags |= xfs_ilog_fext(whichfork);
911
Dave Chinner9e5987a72013-02-25 12:31:26 +1100912done:
913 *logflagsp = flags;
914 return error;
915}
916
917/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 * Called from xfs_bmap_add_attrfork to handle btree format files.
919 */
920STATIC int /* error */
921xfs_bmap_add_attrfork_btree(
922 xfs_trans_t *tp, /* transaction pointer */
923 xfs_inode_t *ip, /* incore inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 int *flags) /* inode logging flags */
925{
Chandan Babu Rb6785e22021-04-02 15:07:33 -0700926 struct xfs_btree_block *block = ip->i_df.if_broot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 xfs_btree_cur_t *cur; /* btree cursor */
928 int error; /* error return value */
929 xfs_mount_t *mp; /* file system mount struct */
930 int stat; /* newroot status */
931
932 mp = ip->i_mount;
Chandan Babu Rb6785e22021-04-02 15:07:33 -0700933
934 if (XFS_BMAP_BMDR_SPACE(block) <= XFS_IFORK_DSIZE(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 *flags |= XFS_ILOG_DBROOT;
936 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100937 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700938 error = xfs_bmbt_lookup_first(cur, &stat);
939 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000941 /* must be at least one entry */
Darrick J. Wongf9e03702019-11-11 12:52:18 -0800942 if (XFS_IS_CORRUPT(mp, stat != 1)) {
943 error = -EFSCORRUPTED;
944 goto error0;
945 }
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100946 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 goto error0;
948 if (stat == 0) {
949 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000950 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
Dave Chinner92219c22020-03-10 17:52:53 -0700952 cur->bc_ino.allocated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
954 }
955 return 0;
956error0:
957 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
958 return error;
959}
960
961/*
962 * Called from xfs_bmap_add_attrfork to handle extents format files.
963 */
964STATIC int /* error */
965xfs_bmap_add_attrfork_extents(
Brian Foster81ba8f32018-07-11 22:26:16 -0700966 struct xfs_trans *tp, /* transaction pointer */
967 struct xfs_inode *ip, /* incore inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 int *flags) /* inode logging flags */
969{
970 xfs_btree_cur_t *cur; /* bmap btree cursor */
971 int error; /* error return value */
972
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700973 if (ip->i_df.if_nextents * sizeof(struct xfs_bmbt_rec) <=
974 XFS_IFORK_DSIZE(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return 0;
976 cur = NULL;
Brian Foster280253d2018-07-11 22:26:29 -0700977 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
978 XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (cur) {
Dave Chinner92219c22020-03-10 17:52:53 -0700980 cur->bc_ino.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -0700981 xfs_btree_del_cursor(cur, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
983 return error;
984}
985
986/*
Dave Chinner1e823792013-02-11 15:58:13 +1100987 * Called from xfs_bmap_add_attrfork to handle local format files. Each
988 * different data fork content type needs a different callout to do the
989 * conversion. Some are basic and only require special block initialisation
990 * callouts for the data formating, others (directories) are so specialised they
991 * handle everything themselves.
992 *
993 * XXX (dgc): investigate whether directory conversion can use the generic
994 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500995 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 */
997STATIC int /* error */
998xfs_bmap_add_attrfork_local(
Brian Foster825d75cd2018-07-11 22:26:21 -0700999 struct xfs_trans *tp, /* transaction pointer */
1000 struct xfs_inode *ip, /* incore inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 int *flags) /* inode logging flags */
1002{
Brian Foster825d75cd2018-07-11 22:26:21 -07001003 struct xfs_da_args dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1006 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001007
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001008 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001010 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 dargs.dp = ip;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001012 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 dargs.whichfork = XFS_DATA_FORK;
1014 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001015 return xfs_dir2_sf_to_block(&dargs);
1016 }
1017
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001018 if (S_ISLNK(VFS_I(ip)->i_mode))
Brian Foster280253d2018-07-11 22:26:29 -07001019 return xfs_bmap_local_to_extents(tp, ip, 1, flags,
1020 XFS_DATA_FORK,
Dave Chinner1e823792013-02-11 15:58:13 +11001021 xfs_symlink_local_to_remote);
1022
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001023 /* should only be called for types that support local format data */
1024 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001025 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
Dave Chinnere6a688c2021-03-22 09:52:03 -07001028/*
1029 * Set an inode attr fork offset based on the format of the data fork.
1030 */
Christoph Hellwig5a981e42021-06-02 14:58:59 -07001031static int
Allison Henderson2f3cd802018-10-18 17:21:16 +11001032xfs_bmap_set_attrforkoff(
1033 struct xfs_inode *ip,
1034 int size,
1035 int *version)
1036{
Dave Chinner683ec9b2021-04-06 07:02:04 -07001037 int default_size = xfs_default_attroffset(ip) >> 3;
1038
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001039 switch (ip->i_df.if_format) {
Allison Henderson2f3cd802018-10-18 17:21:16 +11001040 case XFS_DINODE_FMT_DEV:
Dave Chinner683ec9b2021-04-06 07:02:04 -07001041 ip->i_forkoff = default_size;
Allison Henderson2f3cd802018-10-18 17:21:16 +11001042 break;
1043 case XFS_DINODE_FMT_LOCAL:
1044 case XFS_DINODE_FMT_EXTENTS:
1045 case XFS_DINODE_FMT_BTREE:
Christoph Hellwig7821ea32021-03-29 11:11:44 -07001046 ip->i_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1047 if (!ip->i_forkoff)
Dave Chinner683ec9b2021-04-06 07:02:04 -07001048 ip->i_forkoff = default_size;
Allison Henderson2f3cd802018-10-18 17:21:16 +11001049 else if ((ip->i_mount->m_flags & XFS_MOUNT_ATTR2) && version)
1050 *version = 2;
1051 break;
1052 default:
1053 ASSERT(0);
1054 return -EINVAL;
1055 }
1056
1057 return 0;
1058}
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001061 * Convert inode from non-attributed to attributed.
1062 * Must not be in a transaction, ip must not be locked.
1063 */
1064int /* error code */
1065xfs_bmap_add_attrfork(
1066 xfs_inode_t *ip, /* incore inode pointer */
1067 int size, /* space new attribute needs */
1068 int rsvd) /* xact may use reserved blks */
1069{
Dave Chinner9e5987a72013-02-25 12:31:26 +11001070 xfs_mount_t *mp; /* mount structure */
1071 xfs_trans_t *tp; /* transaction pointer */
1072 int blks; /* space reservation */
1073 int version = 1; /* superblock attr version */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001074 int logflags; /* logging flags */
1075 int error; /* error return value */
1076
1077 ASSERT(XFS_IFORK_Q(ip) == 0);
1078
1079 mp = ip->i_mount;
1080 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001081
Dave Chinner9e5987a72013-02-25 12:31:26 +11001082 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001083
Darrick J. Wong3de4eb12021-01-26 16:44:07 -08001084 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_addafork, blks, 0,
Darrick J. Wong3a1af6c2021-01-26 16:33:29 -08001085 rsvd, &tp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001086 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001087 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001088 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001089 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001090
Dave Chinner9e5987a72013-02-25 12:31:26 +11001091 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Allison Henderson2f3cd802018-10-18 17:21:16 +11001092 error = xfs_bmap_set_attrforkoff(ip, size, &version);
1093 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001094 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001095 ASSERT(ip->i_afp == NULL);
Carlos Maiolino32a2b112020-07-22 09:23:10 -07001096
Dave Chinnere6a688c2021-03-22 09:52:03 -07001097 ip->i_afp = xfs_ifork_alloc(XFS_DINODE_FMT_EXTENTS, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001098 logflags = 0;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001099 switch (ip->i_df.if_format) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001100 case XFS_DINODE_FMT_LOCAL:
Brian Foster825d75cd2018-07-11 22:26:21 -07001101 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001102 break;
1103 case XFS_DINODE_FMT_EXTENTS:
Brian Foster825d75cd2018-07-11 22:26:21 -07001104 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001105 break;
1106 case XFS_DINODE_FMT_BTREE:
Brian Foster825d75cd2018-07-11 22:26:21 -07001107 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001108 break;
1109 default:
1110 error = 0;
1111 break;
1112 }
1113 if (logflags)
1114 xfs_trans_log_inode(tp, ip, logflags);
1115 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07001116 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001117 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1118 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001119 bool log_sb = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001120
1121 spin_lock(&mp->m_sb_lock);
1122 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1123 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001124 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001125 }
1126 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1127 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001128 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001129 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001130 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001131 if (log_sb)
1132 xfs_log_sb(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001133 }
1134
Christoph Hellwig70393312015-06-04 13:48:08 +10001135 error = xfs_trans_commit(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001136 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001137 return error;
1138
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001139trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001140 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001141 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001142 return error;
1143}
1144
1145/*
1146 * Internal and external extent tree search functions.
1147 */
1148
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001149struct xfs_iread_state {
1150 struct xfs_iext_cursor icur;
1151 xfs_extnum_t loaded;
1152};
1153
1154/* Stuff every bmbt record from this block into the incore extent map. */
1155static int
1156xfs_iread_bmbt_block(
1157 struct xfs_btree_cur *cur,
1158 int level,
1159 void *priv)
1160{
1161 struct xfs_iread_state *ir = priv;
1162 struct xfs_mount *mp = cur->bc_mp;
Dave Chinner92219c22020-03-10 17:52:53 -07001163 struct xfs_inode *ip = cur->bc_ino.ip;
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001164 struct xfs_btree_block *block;
1165 struct xfs_buf *bp;
1166 struct xfs_bmbt_rec *frp;
1167 xfs_extnum_t num_recs;
1168 xfs_extnum_t j;
Dave Chinner92219c22020-03-10 17:52:53 -07001169 int whichfork = cur->bc_ino.whichfork;
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001170 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001171
1172 block = xfs_btree_get_block(cur, level, &bp);
1173
1174 /* Abort if we find more records than nextents. */
1175 num_recs = xfs_btree_get_numrecs(block);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001176 if (unlikely(ir->loaded + num_recs > ifp->if_nextents)) {
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001177 xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).",
1178 (unsigned long long)ip->i_ino);
1179 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block,
1180 sizeof(*block), __this_address);
1181 return -EFSCORRUPTED;
1182 }
1183
1184 /* Copy records into the incore cache. */
1185 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1186 for (j = 0; j < num_recs; j++, frp++, ir->loaded++) {
1187 struct xfs_bmbt_irec new;
1188 xfs_failaddr_t fa;
1189
1190 xfs_bmbt_disk_get_all(frp, &new);
1191 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1192 if (fa) {
1193 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1194 "xfs_iread_extents(2)", frp,
1195 sizeof(*frp), fa);
1196 return -EFSCORRUPTED;
1197 }
1198 xfs_iext_insert(ip, &ir->icur, &new,
1199 xfs_bmap_fork_to_state(whichfork));
1200 trace_xfs_read_extent(ip, &ir->icur,
1201 xfs_bmap_fork_to_state(whichfork), _THIS_IP_);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001202 xfs_iext_next(ifp, &ir->icur);
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001203 }
1204
1205 return 0;
1206}
1207
Dave Chinner9e5987a72013-02-25 12:31:26 +11001208/*
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001209 * Read in extents from a btree-format inode.
Dave Chinner9e5987a72013-02-25 12:31:26 +11001210 */
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001211int
1212xfs_iread_extents(
1213 struct xfs_trans *tp,
1214 struct xfs_inode *ip,
1215 int whichfork)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001216{
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001217 struct xfs_iread_state ir;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001218 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001219 struct xfs_mount *mp = ip->i_mount;
1220 struct xfs_btree_cur *cur;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001221 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001222
Christoph Hellwigb2197a32021-04-13 11:15:12 -07001223 if (!xfs_need_iread_extents(ifp))
Christoph Hellwig862a8042021-04-13 11:15:09 -07001224 return 0;
1225
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001226 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1227
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001228 ir.loaded = 0;
1229 xfs_iext_first(ifp, &ir.icur);
1230 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1231 error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block,
1232 XFS_BTREE_VISIT_RECORDS, &ir);
1233 xfs_btree_del_cursor(cur, error);
1234 if (error)
1235 goto out;
1236
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001237 if (XFS_IS_CORRUPT(mp, ir.loaded != ifp->if_nextents)) {
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001238 error = -EFSCORRUPTED;
1239 goto out;
1240 }
1241 ASSERT(ir.loaded == xfs_iext_count(ifp));
Dave Chinner9e5987a72013-02-25 12:31:26 +11001242 return 0;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001243out:
1244 xfs_iext_destroy(ifp);
1245 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001246}
1247
Dave Chinner9e5987a72013-02-25 12:31:26 +11001248/*
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001249 * Returns the relative block number of the first unused block(s) in the given
1250 * fork with at least "len" logically contiguous blocks free. This is the
1251 * lowest-address hole if the fork has holes, else the first block past the end
1252 * of fork. Return 0 if the fork is currently local (in-inode).
Dave Chinner9e5987a72013-02-25 12:31:26 +11001253 */
1254int /* error */
1255xfs_bmap_first_unused(
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001256 struct xfs_trans *tp, /* transaction pointer */
1257 struct xfs_inode *ip, /* incore inode */
1258 xfs_extlen_t len, /* size of hole to find */
1259 xfs_fileoff_t *first_unused, /* unused block */
1260 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001261{
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001262 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1263 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001264 struct xfs_iext_cursor icur;
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001265 xfs_fileoff_t lastaddr = 0;
1266 xfs_fileoff_t lowest, max;
1267 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001268
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001269 if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001270 *first_unused = 0;
1271 return 0;
1272 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001273
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001274 ASSERT(xfs_ifork_has_extents(ifp));
1275
Christoph Hellwig862a8042021-04-13 11:15:09 -07001276 error = xfs_iread_extents(tp, ip, whichfork);
1277 if (error)
1278 return error;
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001279
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001280 lowest = max = *first_unused;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001281 for_each_xfs_iext(ifp, &icur, &got) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001282 /*
1283 * See if the hole before this extent will work.
1284 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001285 if (got.br_startoff >= lowest + len &&
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001286 got.br_startoff - max >= len)
1287 break;
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001288 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001289 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1290 }
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001291
Dave Chinner9e5987a72013-02-25 12:31:26 +11001292 *first_unused = max;
1293 return 0;
1294}
1295
1296/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001297 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a72013-02-25 12:31:26 +11001298 * last_block (input value) in the file.
1299 * This is not based on i_size, it is based on the extent records.
1300 * Returns 0 for local files, as they do not have extent records.
1301 */
1302int /* error */
1303xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001304 struct xfs_trans *tp, /* transaction pointer */
1305 struct xfs_inode *ip, /* incore inode */
1306 xfs_fileoff_t *last_block, /* last block */
1307 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001308{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001309 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1310 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001311 struct xfs_iext_cursor icur;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001312 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001313
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001314 switch (ifp->if_format) {
Christoph Hellwig86685f72016-11-24 11:39:38 +11001315 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001316 *last_block = 0;
1317 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001318 case XFS_DINODE_FMT_BTREE:
1319 case XFS_DINODE_FMT_EXTENTS:
1320 break;
1321 default:
Darrick J. Wonga5155b82019-11-02 09:40:53 -07001322 ASSERT(0);
Darrick J. Wongc2414ad2019-10-28 16:12:34 -07001323 return -EFSCORRUPTED;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001324 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001325
Christoph Hellwig862a8042021-04-13 11:15:09 -07001326 error = xfs_iread_extents(tp, ip, whichfork);
1327 if (error)
1328 return error;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001329
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001330 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
Christoph Hellwigdc560152017-10-23 16:32:39 -07001331 *last_block = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001332 return 0;
1333}
1334
Dave Chinner68988112013-08-12 20:49:42 +10001335int
Dave Chinner9e5987a72013-02-25 12:31:26 +11001336xfs_bmap_last_extent(
1337 struct xfs_trans *tp,
1338 struct xfs_inode *ip,
1339 int whichfork,
1340 struct xfs_bmbt_irec *rec,
1341 int *is_empty)
1342{
1343 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001344 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001345 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001346
Christoph Hellwig862a8042021-04-13 11:15:09 -07001347 error = xfs_iread_extents(tp, ip, whichfork);
1348 if (error)
1349 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001350
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001351 xfs_iext_last(ifp, &icur);
1352 if (!xfs_iext_get_extent(ifp, &icur, rec))
Dave Chinner9e5987a72013-02-25 12:31:26 +11001353 *is_empty = 1;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001354 else
1355 *is_empty = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001356 return 0;
1357}
1358
1359/*
1360 * Check the last inode extent to determine whether this allocation will result
1361 * in blocks being allocated at the end of the file. When we allocate new data
1362 * blocks at the end of the file which do not start at the previous data block,
1363 * we will try to align the new blocks at stripe unit boundaries.
1364 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001365 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a72013-02-25 12:31:26 +11001366 * at, or past the EOF.
1367 */
1368STATIC int
1369xfs_bmap_isaeof(
1370 struct xfs_bmalloca *bma,
1371 int whichfork)
1372{
1373 struct xfs_bmbt_irec rec;
1374 int is_empty;
1375 int error;
1376
Thomas Meyer749f24f2017-10-09 11:38:54 -07001377 bma->aeof = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001378 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1379 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001380 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001381 return error;
1382
Dave Chinner6e708bc2013-11-22 10:41:16 +11001383 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001384 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001385 return 0;
1386 }
1387
Dave Chinner9e5987a72013-02-25 12:31:26 +11001388 /*
1389 * Check if we are allocation or past the last extent, or at least into
1390 * the last delayed allocated extent.
1391 */
1392 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1393 (bma->offset >= rec.br_startoff &&
1394 isnullstartblock(rec.br_startblock));
1395 return 0;
1396}
1397
1398/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001399 * Returns the file-relative block number of the first block past eof in
1400 * the file. This is not based on i_size, it is based on the extent records.
1401 * Returns 0 for local files, as they do not have extent records.
1402 */
1403int
1404xfs_bmap_last_offset(
Dave Chinner9e5987a72013-02-25 12:31:26 +11001405 struct xfs_inode *ip,
1406 xfs_fileoff_t *last_block,
1407 int whichfork)
1408{
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001409 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001410 struct xfs_bmbt_irec rec;
1411 int is_empty;
1412 int error;
1413
1414 *last_block = 0;
1415
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001416 if (ifp->if_format == XFS_DINODE_FMT_LOCAL)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001417 return 0;
1418
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001419 if (XFS_IS_CORRUPT(ip->i_mount, !xfs_ifork_has_extents(ifp)))
Darrick J. Wongc2414ad2019-10-28 16:12:34 -07001420 return -EFSCORRUPTED;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001421
1422 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1423 if (error || is_empty)
1424 return error;
1425
1426 *last_block = rec.br_startoff + rec.br_blockcount;
1427 return 0;
1428}
1429
1430/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001431 * Extent tree manipulation functions used during allocation.
1432 */
1433
1434/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001435 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 */
1437STATIC int /* error */
1438xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001439 struct xfs_bmalloca *bma,
1440 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001442 struct xfs_mount *mp = bma->ip->i_mount;
1443 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001444 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 int i; /* temp state */
1447 xfs_fileoff_t new_endoff; /* end offset of new entry */
1448 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1449 /* left is 0, right is 1, prev is 2 */
1450 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07001451 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001452 xfs_filblks_t da_new; /* new count del alloc blocks used */
1453 xfs_filblks_t da_old; /* old count del alloc blocks used */
1454 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 int tmp_rval; /* partial logging flags */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001456 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Darrick J. Wong60b49842016-10-03 09:11:34 -07001458 ASSERT(whichfork != XFS_ATTR_FORK);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001459 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001460 ASSERT(!bma->cur ||
Dave Chinner8ef54792020-03-10 17:54:38 -07001461 (bma->cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001462
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001463 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465#define LEFT r[0]
1466#define RIGHT r[1]
1467#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 /*
1470 * Set up a bunch of variables to make the tests simpler.
1471 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001472 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001474 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 ASSERT(PREV.br_startoff <= new->br_startoff);
1476 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001477
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001478 da_old = startblockval(PREV.br_startblock);
1479 da_new = 0;
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 /*
1482 * Set flags determining what part of the previous delayed allocation
1483 * extent is being replaced by a real allocation.
1484 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001485 if (PREV.br_startoff == new->br_startoff)
1486 state |= BMAP_LEFT_FILLING;
1487 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1488 state |= BMAP_RIGHT_FILLING;
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 /*
1491 * Check and set flags if this segment has a left neighbor.
1492 * Don't set contiguous if the combined extent would be too large.
1493 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001494 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001495 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001496 if (isnullstartblock(LEFT.br_startblock))
1497 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001499
1500 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1501 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1502 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1503 LEFT.br_state == new->br_state &&
1504 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1505 state |= BMAP_LEFT_CONTIG;
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 /*
1508 * Check and set flags if this segment has a right neighbor.
1509 * Don't set contiguous if the combined extent would be too large.
1510 * Also check for all-three-contiguous being too large.
1511 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001512 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001513 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001514 if (isnullstartblock(RIGHT.br_startblock))
1515 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001517
1518 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1519 new_endoff == RIGHT.br_startoff &&
1520 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1521 new->br_state == RIGHT.br_state &&
1522 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1523 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1524 BMAP_RIGHT_FILLING)) !=
1525 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1526 BMAP_RIGHT_FILLING) ||
1527 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1528 <= MAXEXTLEN))
1529 state |= BMAP_RIGHT_CONTIG;
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 error = 0;
1532 /*
1533 * Switch out based on the FILLING and CONTIG state bits.
1534 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001535 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1536 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1537 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1538 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 /*
1540 * Filling in all of a previously delayed allocation extent.
1541 * The left and right neighbors are both contiguous with new.
1542 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001543 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001544
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001545 xfs_iext_remove(bma->ip, &bma->icur, state);
1546 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001547 xfs_iext_prev(ifp, &bma->icur);
1548 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001549 ifp->if_nextents--;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001550
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001551 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1553 else {
1554 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001555 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001556 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001558 if (XFS_IS_CORRUPT(mp, i != 1)) {
1559 error = -EFSCORRUPTED;
1560 goto done;
1561 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001562 error = xfs_btree_delete(bma->cur, &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)) {
1566 error = -EFSCORRUPTED;
1567 goto done;
1568 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001569 error = xfs_btree_decrement(bma->cur, 0, &i);
1570 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001572 if (XFS_IS_CORRUPT(mp, i != 1)) {
1573 error = -EFSCORRUPTED;
1574 goto done;
1575 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001576 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001577 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 goto done;
1579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 break;
1581
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001582 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 /*
1584 * Filling in all of a previously delayed allocation extent.
1585 * The left neighbor is contiguous, the right is not.
1586 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001587 old = LEFT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001588 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001589
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001590 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001591 xfs_iext_prev(ifp, &bma->icur);
1592 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001593
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001594 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 rval = XFS_ILOG_DEXT;
1596 else {
1597 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001598 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001599 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001601 if (XFS_IS_CORRUPT(mp, i != 1)) {
1602 error = -EFSCORRUPTED;
1603 goto done;
1604 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001605 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001606 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 goto done;
1608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 break;
1610
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001611 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 /*
1613 * Filling in all of a previously delayed allocation extent.
Dave Chinner9230a0b2018-11-19 22:50:08 -08001614 * The right neighbor is contiguous, the left is not. Take care
1615 * with delay -> unwritten extent allocation here because the
1616 * delalloc record we are overwriting is always written.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001618 PREV.br_startblock = new->br_startblock;
1619 PREV.br_blockcount += RIGHT.br_blockcount;
Dave Chinner9230a0b2018-11-19 22:50:08 -08001620 PREV.br_state = new->br_state;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001621
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001622 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001623 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001624 xfs_iext_prev(ifp, &bma->icur);
1625 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001626
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001627 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 rval = XFS_ILOG_DEXT;
1629 else {
1630 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001631 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001632 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001634 if (XFS_IS_CORRUPT(mp, i != 1)) {
1635 error = -EFSCORRUPTED;
1636 goto done;
1637 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001638 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001639 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 goto done;
1641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 break;
1643
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001644 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 /*
1646 * Filling in all of a previously delayed allocation extent.
1647 * Neither the left nor right neighbors are contiguous with
1648 * the new one.
1649 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001650 PREV.br_startblock = new->br_startblock;
1651 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001652 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001653 ifp->if_nextents++;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001654
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001655 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1657 else {
1658 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001659 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001660 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001662 if (XFS_IS_CORRUPT(mp, i != 0)) {
1663 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)) {
1670 error = -EFSCORRUPTED;
1671 goto done;
1672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 break;
1675
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001676 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 /*
1678 * Filling in the first part of a previous delayed allocation.
1679 * The left neighbor is contiguous.
1680 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001681 old = LEFT;
1682 temp = PREV.br_blockcount - new->br_blockcount;
1683 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1684 startblockval(PREV.br_startblock));
1685
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001686 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001687
Christoph Hellwigbf999712017-11-03 10:34:38 -07001688 PREV.br_blockcount = temp;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001689 PREV.br_startoff += new->br_blockcount;
1690 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwig0d045542017-11-03 10:34:39 -07001691
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001692 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1693 xfs_iext_prev(ifp, &bma->icur);
1694 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001695
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001696 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 rval = XFS_ILOG_DEXT;
1698 else {
1699 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001700 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001701 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001703 if (XFS_IS_CORRUPT(mp, i != 1)) {
1704 error = -EFSCORRUPTED;
1705 goto done;
1706 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001707 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001708 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 goto done;
1710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 break;
1712
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001713 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 /*
1715 * Filling in the first part of a previous delayed allocation.
1716 * The left neighbor is not contiguous.
1717 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001718 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001719 ifp->if_nextents++;
1720
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001721 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1723 else {
1724 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001725 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001726 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001728 if (XFS_IS_CORRUPT(mp, i != 0)) {
1729 error = -EFSCORRUPTED;
1730 goto done;
1731 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001732 error = xfs_btree_insert(bma->cur, &i);
1733 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001735 if (XFS_IS_CORRUPT(mp, i != 1)) {
1736 error = -EFSCORRUPTED;
1737 goto done;
1738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001740
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001741 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001742 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001743 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 rval |= tmp_rval;
1745 if (error)
1746 goto done;
1747 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001748
1749 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001750 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001751 startblockval(PREV.br_startblock) -
Dave Chinner92219c22020-03-10 17:52:53 -07001752 (bma->cur ? bma->cur->bc_ino.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001753
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001754 PREV.br_startoff = new_endoff;
1755 PREV.br_blockcount = temp;
1756 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001757 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001758 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001759 xfs_iext_prev(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 break;
1761
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001762 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 /*
1764 * Filling in the last part of a previous delayed allocation.
1765 * The right neighbor is contiguous with the new allocation.
1766 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001767 old = RIGHT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001768 RIGHT.br_startoff = new->br_startoff;
1769 RIGHT.br_startblock = new->br_startblock;
1770 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001771
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001772 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 rval = XFS_ILOG_DEXT;
1774 else {
1775 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001776 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001777 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001779 if (XFS_IS_CORRUPT(mp, i != 1)) {
1780 error = -EFSCORRUPTED;
1781 goto done;
1782 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001783 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001784 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 goto done;
1786 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001787
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001788 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001789 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001790 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001791
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001792 PREV.br_blockcount = temp;
1793 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001794
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001795 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1796 xfs_iext_next(ifp, &bma->icur);
1797 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 break;
1799
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001800 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 /*
1802 * Filling in the last part of a previous delayed allocation.
1803 * The right neighbor is not contiguous.
1804 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001805 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001806 ifp->if_nextents++;
1807
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001808 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1810 else {
1811 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001812 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001813 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001815 if (XFS_IS_CORRUPT(mp, i != 0)) {
1816 error = -EFSCORRUPTED;
1817 goto done;
1818 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001819 error = xfs_btree_insert(bma->cur, &i);
1820 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001822 if (XFS_IS_CORRUPT(mp, i != 1)) {
1823 error = -EFSCORRUPTED;
1824 goto done;
1825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001827
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001828 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001829 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001830 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 rval |= tmp_rval;
1832 if (error)
1833 goto done;
1834 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001835
1836 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001837 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001838 startblockval(PREV.br_startblock) -
Dave Chinner92219c22020-03-10 17:52:53 -07001839 (bma->cur ? bma->cur->bc_ino.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001840
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001841 PREV.br_startblock = nullstartblock(da_new);
1842 PREV.br_blockcount = temp;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001843 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001844 xfs_iext_next(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 break;
1846
1847 case 0:
1848 /*
1849 * Filling in the middle part of a previous delayed allocation.
1850 * Contiguity is impossible here.
1851 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001852 *
1853 * We start with a delayed allocation:
1854 *
1855 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1856 * PREV @ idx
1857 *
1858 * and we are allocating:
1859 * +rrrrrrrrrrrrrrrrr+
1860 * new
1861 *
1862 * and we set it up for insertion as:
1863 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1864 * new
1865 * PREV @ idx LEFT RIGHT
1866 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001868 old = PREV;
1869
1870 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001871 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001872
1873 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001874 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001875 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001876 RIGHT.br_blockcount =
1877 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1878 RIGHT.br_startblock =
1879 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1880 RIGHT.br_blockcount));
1881
1882 /* truncate PREV */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001883 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1884 PREV.br_startblock =
1885 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1886 PREV.br_blockcount));
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001887 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001888
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001889 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001890 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1891 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001892 ifp->if_nextents++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001893
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001894 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1896 else {
1897 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001898 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001899 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001901 if (XFS_IS_CORRUPT(mp, i != 0)) {
1902 error = -EFSCORRUPTED;
1903 goto done;
1904 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001905 error = xfs_btree_insert(bma->cur, &i);
1906 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001908 if (XFS_IS_CORRUPT(mp, i != 1)) {
1909 error = -EFSCORRUPTED;
1910 goto done;
1911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001913
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001914 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001915 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001916 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 rval |= tmp_rval;
1918 if (error)
1919 goto done;
1920 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001921
1922 da_new = startblockval(PREV.br_startblock) +
1923 startblockval(RIGHT.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 break;
1925
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001926 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1927 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1928 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1929 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1930 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1931 case BMAP_LEFT_CONTIG:
1932 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 /*
1934 * These cases are all impossible.
1935 */
1936 ASSERT(0);
1937 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001938
Darrick J. Wong95eb3082018-05-09 10:02:32 -07001939 /* add reverse mapping unless caller opted out */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07001940 if (!(bma->flags & XFS_BMAPI_NORMAP))
1941 xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10001942
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001943 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001944 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001945 int tmp_logflags; /* partial log flag return val */
1946
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001947 ASSERT(bma->cur == NULL);
1948 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001949 &bma->cur, da_old > 0, &tmp_logflags,
1950 whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001951 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001952 if (error)
1953 goto done;
1954 }
1955
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07001956 if (da_new != da_old)
1957 xfs_mod_delalloc(mp, (int64_t)da_new - da_old);
1958
Christoph Hellwigca1862b2017-10-17 14:16:25 -07001959 if (bma->cur) {
Dave Chinner92219c22020-03-10 17:52:53 -07001960 da_new += bma->cur->bc_ino.allocated;
1961 bma->cur->bc_ino.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001962 }
1963
Christoph Hellwigca1862b2017-10-17 14:16:25 -07001964 /* adjust for changes in reserved delayed indirect blocks */
1965 if (da_new != da_old) {
1966 ASSERT(state == 0 || da_new < da_old);
1967 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
1968 false);
1969 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001970
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001971 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07001973 if (whichfork != XFS_COW_FORK)
1974 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return error;
1976#undef LEFT
1977#undef RIGHT
1978#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
1981/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001982 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 */
Christoph Hellwig26b91c72019-02-18 09:38:48 -08001984int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001986 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08001988 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001989 struct xfs_iext_cursor *icur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001991 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001992 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 int i; /* temp state */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07001997 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2000 /* left is 0, right is 1, prev is 2 */
2001 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002002 int state = xfs_bmap_fork_to_state(whichfork);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002003 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002004 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002006 *logflagsp = 0;
2007
2008 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002009 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002010
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002011 ASSERT(!isnullstartblock(new->br_startblock));
2012
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002013 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015#define LEFT r[0]
2016#define RIGHT r[1]
2017#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 /*
2020 * Set up a bunch of variables to make the tests simpler.
2021 */
2022 error = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002023 xfs_iext_get_extent(ifp, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002024 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 new_endoff = new->br_startoff + new->br_blockcount;
2026 ASSERT(PREV.br_startoff <= new->br_startoff);
2027 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 /*
2030 * Set flags determining what part of the previous oldext allocation
2031 * extent is being replaced by a newext allocation.
2032 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002033 if (PREV.br_startoff == new->br_startoff)
2034 state |= BMAP_LEFT_FILLING;
2035 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2036 state |= BMAP_RIGHT_FILLING;
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 /*
2039 * Check and set flags if this segment has a left neighbor.
2040 * Don't set contiguous if the combined extent would be too large.
2041 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002042 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002043 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002044 if (isnullstartblock(LEFT.br_startblock))
2045 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002047
2048 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2049 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2050 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002051 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002052 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2053 state |= BMAP_LEFT_CONTIG;
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 /*
2056 * Check and set flags if this segment has a right neighbor.
2057 * Don't set contiguous if the combined extent would be too large.
2058 * Also check for all-three-contiguous being too large.
2059 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002060 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002061 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002062 if (isnullstartblock(RIGHT.br_startblock))
2063 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002065
2066 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2067 new_endoff == RIGHT.br_startoff &&
2068 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002069 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002070 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2071 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2072 BMAP_RIGHT_FILLING)) !=
2073 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2074 BMAP_RIGHT_FILLING) ||
2075 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2076 <= MAXEXTLEN))
2077 state |= BMAP_RIGHT_CONTIG;
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 /*
2080 * Switch out based on the FILLING and CONTIG state bits.
2081 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002082 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2083 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2084 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2085 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 /*
2087 * Setting all of a previous oldext extent to newext.
2088 * The left and right neighbors are both contiguous with new.
2089 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002090 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002091
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002092 xfs_iext_remove(ip, icur, state);
2093 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002094 xfs_iext_prev(ifp, icur);
2095 xfs_iext_update_extent(ip, state, icur, &LEFT);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002096 ifp->if_nextents -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 if (cur == NULL)
2098 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2099 else {
2100 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002101 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2102 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002104 if (XFS_IS_CORRUPT(mp, i != 1)) {
2105 error = -EFSCORRUPTED;
2106 goto done;
2107 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002108 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002110 if (XFS_IS_CORRUPT(mp, i != 1)) {
2111 error = -EFSCORRUPTED;
2112 goto done;
2113 }
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002114 if ((error = xfs_btree_decrement(cur, 0, &i)))
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)) {
2117 error = -EFSCORRUPTED;
2118 goto done;
2119 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002120 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002122 if (XFS_IS_CORRUPT(mp, i != 1)) {
2123 error = -EFSCORRUPTED;
2124 goto done;
2125 }
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002126 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002128 if (XFS_IS_CORRUPT(mp, i != 1)) {
2129 error = -EFSCORRUPTED;
2130 goto done;
2131 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002132 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002133 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 goto done;
2135 }
2136 break;
2137
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002138 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 /*
2140 * Setting all of a previous oldext extent to newext.
2141 * The left neighbor is contiguous, the right is not.
2142 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002143 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002144
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002145 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002146 xfs_iext_prev(ifp, icur);
2147 xfs_iext_update_extent(ip, state, icur, &LEFT);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002148 ifp->if_nextents--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (cur == NULL)
2150 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2151 else {
2152 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002153 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2154 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002156 if (XFS_IS_CORRUPT(mp, i != 1)) {
2157 error = -EFSCORRUPTED;
2158 goto done;
2159 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002160 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002162 if (XFS_IS_CORRUPT(mp, i != 1)) {
2163 error = -EFSCORRUPTED;
2164 goto done;
2165 }
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002166 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002168 if (XFS_IS_CORRUPT(mp, i != 1)) {
2169 error = -EFSCORRUPTED;
2170 goto done;
2171 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002172 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002173 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 goto done;
2175 }
2176 break;
2177
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002178 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 /*
2180 * Setting all of a previous oldext extent to newext.
2181 * The right neighbor is contiguous, the left is not.
2182 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002183 PREV.br_blockcount += RIGHT.br_blockcount;
2184 PREV.br_state = new->br_state;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002185
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002186 xfs_iext_next(ifp, icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002187 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002188 xfs_iext_prev(ifp, icur);
2189 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002190 ifp->if_nextents--;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 if (cur == NULL)
2193 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2194 else {
2195 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002196 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2197 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002199 if (XFS_IS_CORRUPT(mp, i != 1)) {
2200 error = -EFSCORRUPTED;
2201 goto done;
2202 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002203 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002205 if (XFS_IS_CORRUPT(mp, i != 1)) {
2206 error = -EFSCORRUPTED;
2207 goto done;
2208 }
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002209 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002211 if (XFS_IS_CORRUPT(mp, i != 1)) {
2212 error = -EFSCORRUPTED;
2213 goto done;
2214 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002215 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002216 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 goto done;
2218 }
2219 break;
2220
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002221 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 /*
2223 * Setting all of a previous oldext extent to newext.
2224 * Neither the left nor right neighbors are contiguous with
2225 * the new one.
2226 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002227 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002228 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 if (cur == NULL)
2231 rval = XFS_ILOG_DEXT;
2232 else {
2233 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002234 error = xfs_bmbt_lookup_eq(cur, new, &i);
2235 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002237 if (XFS_IS_CORRUPT(mp, i != 1)) {
2238 error = -EFSCORRUPTED;
2239 goto done;
2240 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002241 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002242 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 goto done;
2244 }
2245 break;
2246
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002247 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 /*
2249 * Setting the first part of a previous oldext extent to newext.
2250 * The left neighbor is contiguous.
2251 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002252 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002253
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002254 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002255 PREV.br_startoff += new->br_blockcount;
2256 PREV.br_startblock += new->br_blockcount;
2257 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002258
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002259 xfs_iext_update_extent(ip, state, icur, &PREV);
2260 xfs_iext_prev(ifp, icur);
2261 xfs_iext_update_extent(ip, state, icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 if (cur == NULL)
2264 rval = XFS_ILOG_DEXT;
2265 else {
2266 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002267 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002268 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002270 if (XFS_IS_CORRUPT(mp, i != 1)) {
2271 error = -EFSCORRUPTED;
2272 goto done;
2273 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002274 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002275 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002277 error = xfs_btree_decrement(cur, 0, &i);
2278 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002280 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002281 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 goto done;
2283 }
2284 break;
2285
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002286 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 /*
2288 * Setting the first part of a previous oldext extent to newext.
2289 * The left neighbor is not contiguous.
2290 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002291 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002292 PREV.br_startoff += new->br_blockcount;
2293 PREV.br_startblock += new->br_blockcount;
2294 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002295
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002296 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002297 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002298 ifp->if_nextents++;
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 if (cur == NULL)
2301 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2302 else {
2303 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002304 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002305 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002307 if (XFS_IS_CORRUPT(mp, i != 1)) {
2308 error = -EFSCORRUPTED;
2309 goto done;
2310 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002311 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002312 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 goto done;
2314 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002315 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002317 if (XFS_IS_CORRUPT(mp, i != 1)) {
2318 error = -EFSCORRUPTED;
2319 goto done;
2320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 }
2322 break;
2323
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002324 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 /*
2326 * Setting the last part of a previous oldext extent to newext.
2327 * The right neighbor is contiguous with the new allocation.
2328 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002329 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002330 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002331
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002332 RIGHT.br_startoff = new->br_startoff;
2333 RIGHT.br_startblock = new->br_startblock;
2334 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002335
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002336 xfs_iext_update_extent(ip, state, icur, &PREV);
2337 xfs_iext_next(ifp, icur);
2338 xfs_iext_update_extent(ip, state, icur, &RIGHT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 if (cur == NULL)
2341 rval = XFS_ILOG_DEXT;
2342 else {
2343 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002344 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002345 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002347 if (XFS_IS_CORRUPT(mp, i != 1)) {
2348 error = -EFSCORRUPTED;
2349 goto done;
2350 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002351 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002352 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002354 error = xfs_btree_increment(cur, 0, &i);
2355 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002357 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002358 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 goto done;
2360 }
2361 break;
2362
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002363 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 /*
2365 * Setting the last part of a previous oldext extent to newext.
2366 * The right neighbor is not contiguous.
2367 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002368 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002369 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002370
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002371 xfs_iext_update_extent(ip, state, icur, &PREV);
2372 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002373 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002374 ifp->if_nextents++;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (cur == NULL)
2377 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2378 else {
2379 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002380 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002381 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002383 if (XFS_IS_CORRUPT(mp, i != 1)) {
2384 error = -EFSCORRUPTED;
2385 goto done;
2386 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002387 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002388 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002390 error = xfs_bmbt_lookup_eq(cur, new, &i);
2391 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002393 if (XFS_IS_CORRUPT(mp, i != 0)) {
2394 error = -EFSCORRUPTED;
2395 goto done;
2396 }
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002397 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002399 if (XFS_IS_CORRUPT(mp, i != 1)) {
2400 error = -EFSCORRUPTED;
2401 goto done;
2402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 }
2404 break;
2405
2406 case 0:
2407 /*
2408 * Setting the middle part of a previous oldext extent to
2409 * newext. Contiguity is impossible here.
2410 * One extent becomes three extents.
2411 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002412 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002413 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 r[0] = *new;
2416 r[1].br_startoff = new_endoff;
2417 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002418 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002420 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002421
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002422 xfs_iext_update_extent(ip, state, icur, &PREV);
2423 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002424 xfs_iext_insert(ip, icur, &r[1], state);
2425 xfs_iext_insert(ip, icur, &r[0], state);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002426 ifp->if_nextents += 2;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002427
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 if (cur == NULL)
2429 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2430 else {
2431 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002432 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002433 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002435 if (XFS_IS_CORRUPT(mp, i != 1)) {
2436 error = -EFSCORRUPTED;
2437 goto done;
2438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002440 error = xfs_bmbt_update(cur, &r[1]);
2441 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 goto done;
2443 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002445 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002447 if (XFS_IS_CORRUPT(mp, i != 1)) {
2448 error = -EFSCORRUPTED;
2449 goto done;
2450 }
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002451 /*
2452 * Reset the cursor to the position of the new extent
2453 * we are about to insert as we can't trust it after
2454 * the previous insert.
2455 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002456 error = xfs_bmbt_lookup_eq(cur, new, &i);
2457 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002459 if (XFS_IS_CORRUPT(mp, i != 0)) {
2460 error = -EFSCORRUPTED;
2461 goto done;
2462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002464 if ((error = xfs_btree_insert(cur, &i)))
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)) {
2467 error = -EFSCORRUPTED;
2468 goto done;
2469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 }
2471 break;
2472
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002473 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2474 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2475 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2476 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2477 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2478 case BMAP_LEFT_CONTIG:
2479 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 /*
2481 * These cases are all impossible.
2482 */
2483 ASSERT(0);
2484 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002485
Darrick J. Wong9c194642016-08-03 12:16:05 +10002486 /* update reverse mappings */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07002487 xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002488
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002489 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002490 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002491 int tmp_logflags; /* partial log flag return val */
2492
2493 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07002494 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2495 &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002496 *logflagsp |= tmp_logflags;
2497 if (error)
2498 goto done;
2499 }
2500
2501 /* clear out the allocated field, done with it now in any case. */
2502 if (cur) {
Dave Chinner92219c22020-03-10 17:52:53 -07002503 cur->bc_ino.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002504 *curp = cur;
2505 }
2506
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002507 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002509 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 return error;
2511#undef LEFT
2512#undef RIGHT
2513#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514}
2515
2516/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002517 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002519STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520xfs_bmap_add_extent_hole_delay(
2521 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002522 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002523 struct xfs_iext_cursor *icur,
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002524 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07002526 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2528 xfs_filblks_t newlen=0; /* new indirect size */
2529 xfs_filblks_t oldlen=0; /* old indirect size */
2530 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002531 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002532 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002534 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002535 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 /*
2538 * Check and set flags if this segment has a left neighbor
2539 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002540 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002541 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002542 if (isnullstartblock(left.br_startblock))
2543 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 /*
2547 * Check and set flags if the current (right) segment exists.
2548 * If it doesn't exist, we're converting the hole at end-of-file.
2549 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002550 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002551 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002552 if (isnullstartblock(right.br_startblock))
2553 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 /*
2557 * Set contiguity flags on the left and right neighbors.
2558 * Don't let extents get too large, even if the pieces are contiguous.
2559 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002560 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2561 left.br_startoff + left.br_blockcount == new->br_startoff &&
2562 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2563 state |= BMAP_LEFT_CONTIG;
2564
2565 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2566 new->br_startoff + new->br_blockcount == right.br_startoff &&
2567 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2568 (!(state & BMAP_LEFT_CONTIG) ||
2569 (left.br_blockcount + new->br_blockcount +
2570 right.br_blockcount <= MAXEXTLEN)))
2571 state |= BMAP_RIGHT_CONTIG;
2572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 /*
2574 * Switch out based on the contiguity flags.
2575 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002576 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2577 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 /*
2579 * New allocation is contiguous with delayed allocations
2580 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002581 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 */
2583 temp = left.br_blockcount + new->br_blockcount +
2584 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002585
Eric Sandeen9d87c312009-01-14 23:22:07 -06002586 oldlen = startblockval(left.br_startblock) +
2587 startblockval(new->br_startblock) +
2588 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002589 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2590 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002591 left.br_startblock = nullstartblock(newlen);
2592 left.br_blockcount = temp;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002593
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002594 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002595 xfs_iext_prev(ifp, icur);
2596 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 break;
2598
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002599 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 /*
2601 * New allocation is contiguous with a delayed allocation
2602 * on the left.
2603 * Merge the new allocation with the left neighbor.
2604 */
2605 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002606
Eric Sandeen9d87c312009-01-14 23:22:07 -06002607 oldlen = startblockval(left.br_startblock) +
2608 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002609 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2610 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002611 left.br_blockcount = temp;
2612 left.br_startblock = nullstartblock(newlen);
Christoph Hellwig41d196f2017-11-03 10:34:39 -07002613
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002614 xfs_iext_prev(ifp, icur);
2615 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 break;
2617
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002618 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 /*
2620 * New allocation is contiguous with a delayed allocation
2621 * on the right.
2622 * Merge the new allocation with the right neighbor.
2623 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002625 oldlen = startblockval(new->br_startblock) +
2626 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002627 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2628 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002629 right.br_startoff = new->br_startoff;
2630 right.br_startblock = nullstartblock(newlen);
2631 right.br_blockcount = temp;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002632 xfs_iext_update_extent(ip, state, icur, &right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 break;
2634
2635 case 0:
2636 /*
2637 * New allocation is not contiguous with another
2638 * delayed allocation.
2639 * Insert a new entry.
2640 */
2641 oldlen = newlen = 0;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002642 xfs_iext_insert(ip, icur, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 break;
2644 }
2645 if (oldlen != newlen) {
2646 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002647 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2648 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 /*
2650 * Nothing to do for disk quota accounting here.
2651 */
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07002652 xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654}
2655
2656/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002657 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 */
2659STATIC int /* error */
2660xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002661 struct xfs_trans *tp,
2662 struct xfs_inode *ip,
2663 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002664 struct xfs_iext_cursor *icur,
Christoph Hellwig6d045582017-04-11 16:45:54 -07002665 struct xfs_btree_cur **curp,
2666 struct xfs_bmbt_irec *new,
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002667 int *logflagsp,
2668 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002670 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2671 struct xfs_mount *mp = ip->i_mount;
2672 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2676 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002677 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002678 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002679 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002681 ASSERT(!isnullstartblock(new->br_startblock));
Dave Chinner8ef54792020-03-10 17:54:38 -07002682 ASSERT(!cur || !(cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002683
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002684 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002685
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 /*
2687 * Check and set flags if this segment has a left neighbor.
2688 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002689 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002690 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002691 if (isnullstartblock(left.br_startblock))
2692 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 /*
2696 * Check and set flags if this segment has a current value.
2697 * Not true if we're inserting into the "hole" at eof.
2698 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002699 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002700 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002701 if (isnullstartblock(right.br_startblock))
2702 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 /*
2706 * We're inserting a real allocation between "left" and "right".
2707 * Set the contiguity flags. Don't let extents get too large.
2708 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002709 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2710 left.br_startoff + left.br_blockcount == new->br_startoff &&
2711 left.br_startblock + left.br_blockcount == new->br_startblock &&
2712 left.br_state == new->br_state &&
2713 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2714 state |= BMAP_LEFT_CONTIG;
2715
2716 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2717 new->br_startoff + new->br_blockcount == right.br_startoff &&
2718 new->br_startblock + new->br_blockcount == right.br_startblock &&
2719 new->br_state == right.br_state &&
2720 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2721 (!(state & BMAP_LEFT_CONTIG) ||
2722 left.br_blockcount + new->br_blockcount +
2723 right.br_blockcount <= MAXEXTLEN))
2724 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002726 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 /*
2728 * Select which case we're in here, and implement it.
2729 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002730 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2731 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 /*
2733 * New allocation is contiguous with real allocations on the
2734 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002735 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002737 left.br_blockcount += new->br_blockcount + right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002738
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002739 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002740 xfs_iext_prev(ifp, icur);
2741 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002742 ifp->if_nextents--;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002743
Christoph Hellwig6d045582017-04-11 16:45:54 -07002744 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002745 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002746 } else {
2747 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002748 error = xfs_bmbt_lookup_eq(cur, &right, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002749 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002750 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002751 if (XFS_IS_CORRUPT(mp, i != 1)) {
2752 error = -EFSCORRUPTED;
2753 goto done;
2754 }
Christoph Hellwig6d045582017-04-11 16:45:54 -07002755 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002756 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002757 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002758 if (XFS_IS_CORRUPT(mp, i != 1)) {
2759 error = -EFSCORRUPTED;
2760 goto done;
2761 }
Christoph Hellwig6d045582017-04-11 16:45:54 -07002762 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002763 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002764 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002765 if (XFS_IS_CORRUPT(mp, i != 1)) {
2766 error = -EFSCORRUPTED;
2767 goto done;
2768 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002769 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002770 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002771 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002773 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002775 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 /*
2777 * New allocation is contiguous with a real allocation
2778 * on the left.
2779 * Merge the new allocation with the left neighbor.
2780 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002781 old = left;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002782 left.br_blockcount += new->br_blockcount;
Christoph Hellwig1d2e0082017-11-03 10:34:40 -07002783
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002784 xfs_iext_prev(ifp, icur);
2785 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002786
Christoph Hellwig6d045582017-04-11 16:45:54 -07002787 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002788 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002789 } else {
2790 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002791 error = xfs_bmbt_lookup_eq(cur, &old, &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)) {
2795 error = -EFSCORRUPTED;
2796 goto done;
2797 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002798 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002799 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002800 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002802 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002804 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 /*
2806 * New allocation is contiguous with a real allocation
2807 * on the right.
2808 * Merge the new allocation with the right neighbor.
2809 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002810 old = right;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07002811
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002812 right.br_startoff = new->br_startoff;
2813 right.br_startblock = new->br_startblock;
2814 right.br_blockcount += new->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002815 xfs_iext_update_extent(ip, state, icur, &right);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002816
Christoph Hellwig6d045582017-04-11 16:45:54 -07002817 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002818 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002819 } else {
2820 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002821 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002822 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002823 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002824 if (XFS_IS_CORRUPT(mp, i != 1)) {
2825 error = -EFSCORRUPTED;
2826 goto done;
2827 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002828 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002829 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002830 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002832 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
2834 case 0:
2835 /*
2836 * New allocation is not contiguous with another
2837 * real allocation.
2838 * Insert a new entry.
2839 */
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002840 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002841 ifp->if_nextents++;
2842
Christoph Hellwig6d045582017-04-11 16:45:54 -07002843 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002844 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002845 } else {
2846 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002847 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002848 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002849 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002850 if (XFS_IS_CORRUPT(mp, i != 0)) {
2851 error = -EFSCORRUPTED;
2852 goto done;
2853 }
Christoph Hellwig6d045582017-04-11 16:45:54 -07002854 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002855 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002856 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002857 if (XFS_IS_CORRUPT(mp, i != 1)) {
2858 error = -EFSCORRUPTED;
2859 goto done;
2860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002862 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002864
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002865 /* add reverse mapping unless caller opted out */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07002866 if (!(flags & XFS_BMAPI_NORMAP))
2867 xfs_rmap_map_extent(tp, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002868
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002869 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002870 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002871 int tmp_logflags; /* partial log flag return val */
2872
Christoph Hellwig6d045582017-04-11 16:45:54 -07002873 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07002874 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2875 &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002876 *logflagsp |= tmp_logflags;
2877 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002878 if (error)
2879 goto done;
2880 }
2881
2882 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002883 if (cur)
Dave Chinner92219c22020-03-10 17:52:53 -07002884 cur->bc_ino.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002885
Christoph Hellwig6d045582017-04-11 16:45:54 -07002886 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002887done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002888 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002889 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890}
2891
Nathan Scottdd9f4382006-01-11 15:28:28 +11002892/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11002893 * Functions used in the extent read, allocate and remove paths
2894 */
2895
2896/*
Christoph Hellwig031474c2021-03-29 11:11:41 -07002897 * Adjust the size of the new extent based on i_extsize and rt extsize.
Nathan Scottdd9f4382006-01-11 15:28:28 +11002898 */
Dave Chinner68988112013-08-12 20:49:42 +10002899int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002900xfs_bmap_extsize_align(
2901 xfs_mount_t *mp,
2902 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2903 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2904 xfs_extlen_t extsz, /* align to this extent size */
2905 int rt, /* is this a realtime inode? */
2906 int eof, /* is extent at end-of-file? */
2907 int delay, /* creating delalloc extent? */
2908 int convert, /* overwriting unwritten extent? */
2909 xfs_fileoff_t *offp, /* in/out: aligned offset */
2910 xfs_extlen_t *lenp) /* in/out: aligned length */
2911{
2912 xfs_fileoff_t orig_off; /* original offset */
2913 xfs_extlen_t orig_alen; /* original length */
2914 xfs_fileoff_t orig_end; /* original off+len */
2915 xfs_fileoff_t nexto; /* next file offset */
2916 xfs_fileoff_t prevo; /* previous file offset */
2917 xfs_fileoff_t align_off; /* temp for offset */
2918 xfs_extlen_t align_alen; /* temp for length */
2919 xfs_extlen_t temp; /* temp for calculations */
2920
2921 if (convert)
2922 return 0;
2923
2924 orig_off = align_off = *offp;
2925 orig_alen = align_alen = *lenp;
2926 orig_end = orig_off + orig_alen;
2927
2928 /*
2929 * If this request overlaps an existing extent, then don't
2930 * attempt to perform any additional alignment.
2931 */
2932 if (!delay && !eof &&
2933 (orig_off >= gotp->br_startoff) &&
2934 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2935 return 0;
2936 }
2937
2938 /*
2939 * If the file offset is unaligned vs. the extent size
2940 * we need to align it. This will be possible unless
2941 * the file was previously written with a kernel that didn't
2942 * perform this alignment, or if a truncate shot us in the
2943 * foot.
2944 */
Dave Chinner0703a8e2018-06-08 09:54:22 -07002945 div_u64_rem(orig_off, extsz, &temp);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002946 if (temp) {
2947 align_alen += temp;
2948 align_off -= temp;
2949 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10002950
2951 /* Same adjustment for the end of the requested area. */
2952 temp = (align_alen % extsz);
2953 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11002954 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10002955
2956 /*
2957 * For large extent hint sizes, the aligned extent might be larger than
2958 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2959 * the length back under MAXEXTLEN. The outer allocation loops handle
2960 * short allocation just fine, so it is safe to do this. We only want to
2961 * do it when we are forced to, though, because it means more allocation
2962 * operations are required.
2963 */
2964 while (align_alen > MAXEXTLEN)
2965 align_alen -= extsz;
2966 ASSERT(align_alen <= MAXEXTLEN);
2967
Nathan Scottdd9f4382006-01-11 15:28:28 +11002968 /*
2969 * If the previous block overlaps with this proposed allocation
2970 * then move the start forward without adjusting the length.
2971 */
2972 if (prevp->br_startoff != NULLFILEOFF) {
2973 if (prevp->br_startblock == HOLESTARTBLOCK)
2974 prevo = prevp->br_startoff;
2975 else
2976 prevo = prevp->br_startoff + prevp->br_blockcount;
2977 } else
2978 prevo = 0;
2979 if (align_off != orig_off && align_off < prevo)
2980 align_off = prevo;
2981 /*
2982 * If the next block overlaps with this proposed allocation
2983 * then move the start back without adjusting the length,
2984 * but not before offset 0.
2985 * This may of course make the start overlap previous block,
2986 * and if we hit the offset 0 limit then the next block
2987 * can still overlap too.
2988 */
2989 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2990 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2991 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2992 nexto = gotp->br_startoff + gotp->br_blockcount;
2993 else
2994 nexto = gotp->br_startoff;
2995 } else
2996 nexto = NULLFILEOFF;
2997 if (!eof &&
2998 align_off + align_alen != orig_end &&
2999 align_off + align_alen > nexto)
3000 align_off = nexto > align_alen ? nexto - align_alen : 0;
3001 /*
3002 * If we're now overlapping the next or previous extent that
3003 * means we can't fit an extsz piece in this hole. Just move
3004 * the start forward to the first valid spot and set
3005 * the length so we hit the end.
3006 */
3007 if (align_off != orig_off && align_off < prevo)
3008 align_off = prevo;
3009 if (align_off + align_alen != orig_end &&
3010 align_off + align_alen > nexto &&
3011 nexto != NULLFILEOFF) {
3012 ASSERT(nexto > prevo);
3013 align_alen = nexto - align_off;
3014 }
3015
3016 /*
3017 * If realtime, and the result isn't a multiple of the realtime
3018 * extent size we need to remove blocks until it is.
3019 */
3020 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3021 /*
3022 * We're not covering the original request, or
3023 * we won't be able to once we fix the length.
3024 */
3025 if (orig_off < align_off ||
3026 orig_end > align_off + align_alen ||
3027 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003028 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003029 /*
3030 * Try to fix it by moving the start up.
3031 */
3032 if (align_off + temp <= orig_off) {
3033 align_alen -= temp;
3034 align_off += temp;
3035 }
3036 /*
3037 * Try to fix it by moving the end in.
3038 */
3039 else if (align_off + align_alen - temp >= orig_end)
3040 align_alen -= temp;
3041 /*
3042 * Set the start to the minimum then trim the length.
3043 */
3044 else {
3045 align_alen -= orig_off - align_off;
3046 align_off = orig_off;
3047 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3048 }
3049 /*
3050 * Result doesn't cover the request, fail it.
3051 */
3052 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003053 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003054 } else {
3055 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003056 /* see MAXEXTLEN handling above */
3057 ASSERT(orig_end <= align_off + align_alen ||
3058 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003059 }
3060
3061#ifdef DEBUG
3062 if (!eof && gotp->br_startoff != NULLFILEOFF)
3063 ASSERT(align_off + align_alen <= gotp->br_startoff);
3064 if (prevp->br_startoff != NULLFILEOFF)
3065 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3066#endif
3067
3068 *lenp = align_alen;
3069 *offp = align_off;
3070 return 0;
3071}
3072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073#define XFS_ALLOC_GAP_UNITS 4
3074
Dave Chinner68988112013-08-12 20:49:42 +10003075void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003076xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003077 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078{
3079 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3081 xfs_mount_t *mp; /* mount point structure */
3082 int nullfb; /* true if ap->firstblock isn't set */
3083 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
3085#define ISVALID(x,y) \
3086 (rt ? \
3087 (x) < mp->m_sb.sb_rblocks : \
3088 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3089 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3090 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3091
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 mp = ap->ip->i_mount;
Brian Foster94c07b42018-07-11 22:26:28 -07003093 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003094 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
Christoph Hellwigc34d5702019-10-30 12:25:00 -07003095 (ap->datatype & XFS_ALLOC_USERDATA);
Brian Foster94c07b42018-07-11 22:26:28 -07003096 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3097 ap->tp->t_firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 /*
3099 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003100 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003102 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3103 !isnullstartblock(ap->prev.br_startblock) &&
3104 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3105 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003106 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 /*
3108 * Adjust for the gap between prevp and us.
3109 */
Dave Chinner3a756672011-09-18 20:40:58 +00003110 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003111 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003113 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3114 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 }
3116 /*
3117 * If not at eof, then compare the two neighbor blocks.
3118 * Figure out whether either one gives us a good starting point,
3119 * and pick the better one.
3120 */
3121 else if (!ap->eof) {
3122 xfs_fsblock_t gotbno; /* right side block number */
3123 xfs_fsblock_t gotdiff=0; /* right side difference */
3124 xfs_fsblock_t prevbno; /* left side block number */
3125 xfs_fsblock_t prevdiff=0; /* left side difference */
3126
3127 /*
3128 * If there's a previous (left) block, select a requested
3129 * start block based on it.
3130 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003131 if (ap->prev.br_startoff != NULLFILEOFF &&
3132 !isnullstartblock(ap->prev.br_startblock) &&
3133 (prevbno = ap->prev.br_startblock +
3134 ap->prev.br_blockcount) &&
3135 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 /*
3137 * Calculate gap to end of previous block.
3138 */
Dave Chinner3a756672011-09-18 20:40:58 +00003139 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003140 (ap->prev.br_startoff +
3141 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 /*
3143 * Figure the startblock based on the previous block's
3144 * end and the gap size.
3145 * Heuristic!
3146 * If the gap is large relative to the piece we're
3147 * allocating, or using it gives us an invalid block
3148 * number, then just use the end of the previous block.
3149 */
Dave Chinner3a756672011-09-18 20:40:58 +00003150 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003152 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 prevbno += adjust;
3154 else
3155 prevdiff += adjust;
3156 /*
3157 * If the firstblock forbids it, can't use it,
3158 * must use default.
3159 */
3160 if (!rt && !nullfb &&
3161 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3162 prevbno = NULLFSBLOCK;
3163 }
3164 /*
3165 * No previous block or can't follow it, just default.
3166 */
3167 else
3168 prevbno = NULLFSBLOCK;
3169 /*
3170 * If there's a following (right) block, select a requested
3171 * start block based on it.
3172 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003173 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 /*
3175 * Calculate gap to start of next block.
3176 */
Dave Chinner3a756672011-09-18 20:40:58 +00003177 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 /*
3179 * Figure the startblock based on the next block's
3180 * start and the gap size.
3181 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003182 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 /*
3184 * Heuristic!
3185 * If the gap is large relative to the piece we're
3186 * allocating, or using it gives us an invalid block
3187 * number, then just use the start of the next block
3188 * offset by our length.
3189 */
Dave Chinner3a756672011-09-18 20:40:58 +00003190 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 ISVALID(gotbno - gotdiff, gotbno))
3192 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003193 else if (ISVALID(gotbno - ap->length, gotbno)) {
3194 gotbno -= ap->length;
3195 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 } else
3197 gotdiff += adjust;
3198 /*
3199 * If the firstblock forbids it, can't use it,
3200 * must use default.
3201 */
3202 if (!rt && !nullfb &&
3203 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3204 gotbno = NULLFSBLOCK;
3205 }
3206 /*
3207 * No next block, just default.
3208 */
3209 else
3210 gotbno = NULLFSBLOCK;
3211 /*
3212 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003213 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 */
3215 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003216 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003218 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003220 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003222#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003223}
3224
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003225static int
3226xfs_bmap_longest_free_extent(
3227 struct xfs_trans *tp,
3228 xfs_agnumber_t ag,
3229 xfs_extlen_t *blen,
3230 int *notinit)
3231{
3232 struct xfs_mount *mp = tp->t_mountp;
3233 struct xfs_perag *pag;
3234 xfs_extlen_t longest;
3235 int error = 0;
3236
3237 pag = xfs_perag_get(mp, ag);
3238 if (!pag->pagf_init) {
3239 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
Darrick J. Wongf48e2df2020-01-23 17:01:19 -08003240 if (error) {
3241 /* Couldn't lock the AGF, so skip this AG. */
3242 if (error == -EAGAIN) {
3243 *notinit = 1;
3244 error = 0;
3245 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003246 goto out;
3247 }
3248 }
3249
Eric Sandeena1f69412018-04-06 10:09:42 -07003250 longest = xfs_alloc_longest_free_extent(pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003251 xfs_alloc_min_freelist(mp, pag),
3252 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003253 if (*blen < longest)
3254 *blen = longest;
3255
3256out:
3257 xfs_perag_put(pag);
3258 return error;
3259}
3260
3261static void
3262xfs_bmap_select_minlen(
3263 struct xfs_bmalloca *ap,
3264 struct xfs_alloc_arg *args,
3265 xfs_extlen_t *blen,
3266 int notinit)
3267{
3268 if (notinit || *blen < ap->minlen) {
3269 /*
3270 * Since we did a BUF_TRYLOCK above, it is possible that
3271 * there is space for this request.
3272 */
3273 args->minlen = ap->minlen;
3274 } else if (*blen < args->maxlen) {
3275 /*
3276 * If the best seen length is less than the request length,
3277 * use the best as the minimum.
3278 */
3279 args->minlen = *blen;
3280 } else {
3281 /*
3282 * Otherwise we've seen an extent as big as maxlen, use that
3283 * as the minimum.
3284 */
3285 args->minlen = args->maxlen;
3286 }
3287}
3288
Nathan Scotta365bdd2006-03-14 13:34:16 +11003289STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003290xfs_bmap_btalloc_nullfb(
3291 struct xfs_bmalloca *ap,
3292 struct xfs_alloc_arg *args,
3293 xfs_extlen_t *blen)
3294{
3295 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003296 xfs_agnumber_t ag, startag;
3297 int notinit = 0;
3298 int error;
3299
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003300 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003301 args->total = ap->total;
3302
Christoph Hellwigc467c042010-02-15 23:34:42 +00003303 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3304 if (startag == NULLAGNUMBER)
3305 startag = ag = 0;
3306
Dave Chinner14b064c2011-01-27 12:16:28 +11003307 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003308 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3309 &notinit);
3310 if (error)
3311 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003312
Christoph Hellwigc467c042010-02-15 23:34:42 +00003313 if (++ag == mp->m_sb.sb_agcount)
3314 ag = 0;
3315 if (ag == startag)
3316 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003317 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003318
3319 xfs_bmap_select_minlen(ap, args, blen, notinit);
3320 return 0;
3321}
3322
3323STATIC int
3324xfs_bmap_btalloc_filestreams(
3325 struct xfs_bmalloca *ap,
3326 struct xfs_alloc_arg *args,
3327 xfs_extlen_t *blen)
3328{
3329 struct xfs_mount *mp = ap->ip->i_mount;
3330 xfs_agnumber_t ag;
3331 int notinit = 0;
3332 int error;
3333
3334 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3335 args->total = ap->total;
3336
3337 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3338 if (ag == NULLAGNUMBER)
3339 ag = 0;
3340
3341 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3342 if (error)
3343 return error;
3344
3345 if (*blen < args->maxlen) {
3346 error = xfs_filestream_new_ag(ap, &ag);
3347 if (error)
3348 return error;
3349
3350 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3351 &notinit);
3352 if (error)
3353 return error;
3354
3355 }
3356
3357 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003358
3359 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003360 * Set the failure fallback case to look in the selected AG as stream
3361 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003362 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003363 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003364 return 0;
3365}
3366
Darrick J. Wong751f3762018-01-25 13:58:13 -08003367/* Update all inode and quota accounting for the allocation we just did. */
3368static void
3369xfs_bmap_btalloc_accounting(
3370 struct xfs_bmalloca *ap,
3371 struct xfs_alloc_arg *args)
3372{
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003373 if (ap->flags & XFS_BMAPI_COWFORK) {
3374 /*
3375 * COW fork blocks are in-core only and thus are treated as
3376 * in-core quota reservation (like delalloc blocks) even when
3377 * converted to real blocks. The quota reservation is not
3378 * accounted to disk until blocks are remapped to the data
3379 * fork. So if these blocks were previously delalloc, we
3380 * already have quota reservation and there's nothing to do
3381 * yet.
3382 */
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003383 if (ap->wasdel) {
3384 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003385 return;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003386 }
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003387
3388 /*
3389 * Otherwise, we've allocated blocks in a hole. The transaction
3390 * has acquired in-core quota reservation for this extent.
3391 * Rather than account these as real blocks, however, we reduce
3392 * the transaction quota reservation based on the allocation.
3393 * This essentially transfers the transaction quota reservation
3394 * to that of a delalloc extent.
3395 */
3396 ap->ip->i_delayed_blks += args->len;
3397 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3398 -(long)args->len);
3399 return;
3400 }
3401
3402 /* data/attr fork only */
Christoph Hellwig6e73a542021-03-29 11:11:40 -07003403 ap->ip->i_nblocks += args->len;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003404 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003405 if (ap->wasdel) {
Darrick J. Wong751f3762018-01-25 13:58:13 -08003406 ap->ip->i_delayed_blks -= args->len;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003407 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3408 }
Darrick J. Wong751f3762018-01-25 13:58:13 -08003409 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3410 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3411 args->len);
3412}
3413
Chandan Babu R0961fdd2021-01-22 16:48:16 -08003414static int
3415xfs_bmap_compute_alignments(
3416 struct xfs_bmalloca *ap,
3417 struct xfs_alloc_arg *args)
3418{
3419 struct xfs_mount *mp = args->mp;
3420 xfs_extlen_t align = 0; /* minimum allocation alignment */
3421 int stripe_align = 0;
Chandan Babu R0961fdd2021-01-22 16:48:16 -08003422
3423 /* stripe alignment for allocation is determined by mount parameters */
3424 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3425 stripe_align = mp->m_swidth;
3426 else if (mp->m_dalign)
3427 stripe_align = mp->m_dalign;
3428
3429 if (ap->flags & XFS_BMAPI_COWFORK)
3430 align = xfs_get_cowextsz_hint(ap->ip);
3431 else if (ap->datatype & XFS_ALLOC_USERDATA)
3432 align = xfs_get_extsz_hint(ap->ip);
3433 if (align) {
Chandan Babu R560ab6c2021-01-27 08:35:14 -08003434 if (xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, align, 0,
3435 ap->eof, 0, ap->conv, &ap->offset,
3436 &ap->length))
3437 ASSERT(0);
Chandan Babu R0961fdd2021-01-22 16:48:16 -08003438 ASSERT(ap->length);
3439 }
3440
3441 /* apply extent size hints if obtained earlier */
3442 if (align) {
3443 args->prod = align;
3444 div_u64_rem(ap->offset, args->prod, &args->mod);
3445 if (args->mod)
3446 args->mod = args->prod - args->mod;
3447 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
3448 args->prod = 1;
3449 args->mod = 0;
3450 } else {
3451 args->prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
3452 div_u64_rem(ap->offset, args->prod, &args->mod);
3453 if (args->mod)
3454 args->mod = args->prod - args->mod;
3455 }
3456
3457 return stripe_align;
3458}
3459
Chandan Babu R07c72e52021-01-22 16:48:17 -08003460static void
3461xfs_bmap_process_allocated_extent(
3462 struct xfs_bmalloca *ap,
3463 struct xfs_alloc_arg *args,
3464 xfs_fileoff_t orig_offset,
3465 xfs_extlen_t orig_length)
3466{
3467 int nullfb;
3468
3469 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3470
3471 /*
3472 * check the allocation happened at the same or higher AG than
3473 * the first block that was allocated.
3474 */
3475 ASSERT(nullfb ||
3476 XFS_FSB_TO_AGNO(args->mp, ap->tp->t_firstblock) <=
3477 XFS_FSB_TO_AGNO(args->mp, args->fsbno));
3478
3479 ap->blkno = args->fsbno;
3480 if (nullfb)
3481 ap->tp->t_firstblock = args->fsbno;
3482 ap->length = args->len;
3483 /*
3484 * If the extent size hint is active, we tried to round the
3485 * caller's allocation request offset down to extsz and the
3486 * length up to another extsz boundary. If we found a free
3487 * extent we mapped it in starting at this new offset. If the
3488 * newly mapped space isn't long enough to cover any of the
3489 * range of offsets that was originally requested, move the
3490 * mapping up so that we can fill as much of the caller's
3491 * original request as possible. Free space is apparently
3492 * very fragmented so we're unlikely to be able to satisfy the
3493 * hints anyway.
3494 */
3495 if (ap->length <= orig_length)
3496 ap->offset = orig_offset;
3497 else if (ap->offset + ap->length < orig_offset + orig_length)
3498 ap->offset = orig_offset + orig_length - ap->length;
3499 xfs_bmap_btalloc_accounting(ap, args);
3500}
3501
Chandan Babu R30151962021-01-22 16:48:17 -08003502#ifdef DEBUG
3503static int
3504xfs_bmap_exact_minlen_extent_alloc(
3505 struct xfs_bmalloca *ap)
3506{
3507 struct xfs_mount *mp = ap->ip->i_mount;
3508 struct xfs_alloc_arg args = { .tp = ap->tp, .mp = mp };
3509 xfs_fileoff_t orig_offset;
3510 xfs_extlen_t orig_length;
3511 int error;
3512
3513 ASSERT(ap->length);
3514
3515 if (ap->minlen != 1) {
3516 ap->blkno = NULLFSBLOCK;
3517 ap->length = 0;
3518 return 0;
3519 }
3520
3521 orig_offset = ap->offset;
3522 orig_length = ap->length;
3523
3524 args.alloc_minlen_only = 1;
3525
3526 xfs_bmap_compute_alignments(ap, &args);
3527
3528 if (ap->tp->t_firstblock == NULLFSBLOCK) {
3529 /*
3530 * Unlike the longest extent available in an AG, we don't track
3531 * the length of an AG's shortest extent.
3532 * XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT is a debug only knob and
3533 * hence we can afford to start traversing from the 0th AG since
3534 * we need not be concerned about a drop in performance in
3535 * "debug only" code paths.
3536 */
3537 ap->blkno = XFS_AGB_TO_FSB(mp, 0, 0);
3538 } else {
3539 ap->blkno = ap->tp->t_firstblock;
3540 }
3541
3542 args.fsbno = ap->blkno;
3543 args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
3544 args.type = XFS_ALLOCTYPE_FIRST_AG;
Chandan Babu R6e8bd392021-03-25 11:55:10 -07003545 args.minlen = args.maxlen = ap->minlen;
3546 args.total = ap->total;
Chandan Babu R30151962021-01-22 16:48:17 -08003547
3548 args.alignment = 1;
3549 args.minalignslop = 0;
3550
3551 args.minleft = ap->minleft;
3552 args.wasdel = ap->wasdel;
3553 args.resv = XFS_AG_RESV_NONE;
3554 args.datatype = ap->datatype;
3555
3556 error = xfs_alloc_vextent(&args);
3557 if (error)
3558 return error;
3559
3560 if (args.fsbno != NULLFSBLOCK) {
3561 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3562 orig_length);
3563 } else {
3564 ap->blkno = NULLFSBLOCK;
3565 ap->length = 0;
3566 }
3567
3568 return 0;
3569}
3570#else
3571
3572#define xfs_bmap_exact_minlen_extent_alloc(bma) (-EFSCORRUPTED)
3573
3574#endif
3575
Christoph Hellwigc467c042010-02-15 23:34:42 +00003576STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003577xfs_bmap_btalloc(
Chandan Babu R30151962021-01-22 16:48:17 -08003578 struct xfs_bmalloca *ap)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003579{
Chandan Babu R30151962021-01-22 16:48:17 -08003580 struct xfs_mount *mp = ap->ip->i_mount;
3581 struct xfs_alloc_arg args = { .tp = ap->tp, .mp = mp };
3582 xfs_alloctype_t atype = 0;
3583 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3584 xfs_agnumber_t ag;
3585 xfs_fileoff_t orig_offset;
3586 xfs_extlen_t orig_length;
3587 xfs_extlen_t blen;
3588 xfs_extlen_t nextminlen = 0;
3589 int nullfb; /* true if ap->firstblock isn't set */
3590 int isaligned;
3591 int tryagain;
3592 int error;
3593 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003594
Dave Chinnera99ebf42011-12-01 11:24:20 +00003595 ASSERT(ap->length);
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003596 orig_offset = ap->offset;
3597 orig_length = ap->length;
Dave Chinnera99ebf42011-12-01 11:24:20 +00003598
Chandan Babu R0961fdd2021-01-22 16:48:16 -08003599 stripe_align = xfs_bmap_compute_alignments(ap, &args);
Dave Chinner33177f052013-12-12 16:34:36 +11003600
Brian Foster94c07b42018-07-11 22:26:28 -07003601 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3602 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3603 ap->tp->t_firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003604 if (nullfb) {
Christoph Hellwigc34d5702019-10-30 12:25:00 -07003605 if ((ap->datatype & XFS_ALLOC_USERDATA) &&
Dave Chinner292378e2016-09-26 08:21:28 +10003606 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003607 ag = xfs_filestream_lookup_ag(ap->ip);
3608 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003609 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003610 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003611 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003612 }
3613 } else
Brian Foster94c07b42018-07-11 22:26:28 -07003614 ap->blkno = ap->tp->t_firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003615
3616 xfs_bmap_adjacent(ap);
3617
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003619 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 * it's in the right allocation group.
3621 */
Dave Chinner3a756672011-09-18 20:40:58 +00003622 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 ;
3624 else
Brian Foster94c07b42018-07-11 22:26:28 -07003625 ap->blkno = ap->tp->t_firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 * Normal allocation, done through xfs_alloc_vextent.
3628 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003629 tryagain = isaligned = 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003630 args.fsbno = ap->blkno;
Darrick J. Wong7280fed2018-12-12 08:46:23 -08003631 args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
Dave Chinner14b064c2011-01-27 12:16:28 +11003632
3633 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner9bb54cb2018-06-07 07:54:02 -07003634 args.maxlen = min(ap->length, mp->m_ag_max_usable);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003635 blen = 0;
3636 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003637 /*
3638 * Search for an allocation group with a single extent large
3639 * enough for the request. If one isn't found, then adjust
3640 * the minimum allocation size to the largest space found.
3641 */
Christoph Hellwigc34d5702019-10-30 12:25:00 -07003642 if ((ap->datatype & XFS_ALLOC_USERDATA) &&
Dave Chinner292378e2016-09-26 08:21:28 +10003643 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003644 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3645 else
3646 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003647 if (error)
3648 return error;
Brian Foster1214f1c2018-08-01 07:20:31 -07003649 } else if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003650 if (xfs_inode_is_filestream(ap->ip))
3651 args.type = XFS_ALLOCTYPE_FIRST_AG;
3652 else
3653 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003654 args.total = args.minlen = ap->minlen;
3655 } else {
3656 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3657 args.total = ap->total;
3658 args.minlen = ap->minlen;
3659 }
Chandan Babu R0961fdd2021-01-22 16:48:16 -08003660
Nathan Scotta365bdd2006-03-14 13:34:16 +11003661 /*
Dave Chinner1c743572019-10-21 09:26:34 -07003662 * If we are not low on available data blocks, and the underlying
3663 * logical volume manager is a stripe, and the file offset is zero then
3664 * try to allocate data blocks on stripe unit boundary. NOTE: ap->aeof
3665 * is only set if the allocation length is >= the stripe unit and the
3666 * allocation offset is at the end of file.
Nathan Scotta365bdd2006-03-14 13:34:16 +11003667 */
Brian Foster1214f1c2018-08-01 07:20:31 -07003668 if (!(ap->tp->t_flags & XFS_TRANS_LOWMODE) && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003669 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003670 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003671 atype = args.type;
3672 isaligned = 1;
3673 /*
Dave Chinner1c743572019-10-21 09:26:34 -07003674 * Adjust minlen to try and preserve alignment if we
3675 * can't guarantee an aligned maxlen extent.
Nathan Scotta365bdd2006-03-14 13:34:16 +11003676 */
Dave Chinner1c743572019-10-21 09:26:34 -07003677 if (blen > args.alignment &&
3678 blen <= args.maxlen + args.alignment)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003679 args.minlen = blen - args.alignment;
3680 args.minalignslop = 0;
3681 } else {
3682 /*
3683 * First try an exact bno allocation.
3684 * If it fails then do a near or start bno
3685 * allocation with alignment turned on.
3686 */
3687 atype = args.type;
3688 tryagain = 1;
3689 args.type = XFS_ALLOCTYPE_THIS_BNO;
3690 args.alignment = 1;
3691 /*
3692 * Compute the minlen+alignment for the
3693 * next case. Set slop so that the value
3694 * of minlen+alignment+slop doesn't go up
3695 * between the calls.
3696 */
Dave Chinner33177f052013-12-12 16:34:36 +11003697 if (blen > stripe_align && blen <= args.maxlen)
3698 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003699 else
3700 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003701 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003702 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003703 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003704 args.minlen - 1;
3705 else
3706 args.minalignslop = 0;
3707 }
3708 } else {
3709 args.alignment = 1;
3710 args.minalignslop = 0;
3711 }
3712 args.minleft = ap->minleft;
3713 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003714 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003715 args.datatype = ap->datatype;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003716
3717 error = xfs_alloc_vextent(&args);
3718 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003719 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003720
Nathan Scotta365bdd2006-03-14 13:34:16 +11003721 if (tryagain && args.fsbno == NULLFSBLOCK) {
3722 /*
3723 * Exact allocation failed. Now try with alignment
3724 * turned on.
3725 */
3726 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003727 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003728 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003729 args.minlen = nextminlen;
3730 args.minalignslop = 0;
3731 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 if ((error = xfs_alloc_vextent(&args)))
3733 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003734 }
3735 if (isaligned && args.fsbno == NULLFSBLOCK) {
3736 /*
3737 * allocation failed, so turn off alignment and
3738 * try again.
3739 */
3740 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003741 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003742 args.alignment = 0;
3743 if ((error = xfs_alloc_vextent(&args)))
3744 return error;
3745 }
3746 if (args.fsbno == NULLFSBLOCK && nullfb &&
3747 args.minlen > ap->minlen) {
3748 args.minlen = ap->minlen;
3749 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003750 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003751 if ((error = xfs_alloc_vextent(&args)))
3752 return error;
3753 }
3754 if (args.fsbno == NULLFSBLOCK && nullfb) {
3755 args.fsbno = 0;
3756 args.type = XFS_ALLOCTYPE_FIRST_AG;
3757 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003758 if ((error = xfs_alloc_vextent(&args)))
3759 return error;
Brian Foster1214f1c2018-08-01 07:20:31 -07003760 ap->tp->t_flags |= XFS_TRANS_LOWMODE;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003761 }
Dave Chinner0937e0f2011-09-18 20:40:57 +00003762
Chandan Babu R07c72e52021-01-22 16:48:17 -08003763 if (args.fsbno != NULLFSBLOCK) {
3764 xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3765 orig_length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003766 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003767 ap->blkno = NULLFSBLOCK;
3768 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 }
3770 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003771}
3772
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003773/* Trim extent to fit a logical block range. */
3774void
3775xfs_trim_extent(
3776 struct xfs_bmbt_irec *irec,
3777 xfs_fileoff_t bno,
3778 xfs_filblks_t len)
3779{
3780 xfs_fileoff_t distance;
3781 xfs_fileoff_t end = bno + len;
3782
3783 if (irec->br_startoff + irec->br_blockcount <= bno ||
3784 irec->br_startoff >= end) {
3785 irec->br_blockcount = 0;
3786 return;
3787 }
3788
3789 if (irec->br_startoff < bno) {
3790 distance = bno - irec->br_startoff;
3791 if (isnullstartblock(irec->br_startblock))
3792 irec->br_startblock = DELAYSTARTBLOCK;
3793 if (irec->br_startblock != DELAYSTARTBLOCK &&
3794 irec->br_startblock != HOLESTARTBLOCK)
3795 irec->br_startblock += distance;
3796 irec->br_startoff += distance;
3797 irec->br_blockcount -= distance;
3798 }
3799
3800 if (end < irec->br_startoff + irec->br_blockcount) {
3801 distance = irec->br_startoff + irec->br_blockcount - end;
3802 irec->br_blockcount -= distance;
3803 }
3804}
3805
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003807 * Trim the returned map to the required bounds
3808 */
3809STATIC void
3810xfs_bmapi_trim_map(
3811 struct xfs_bmbt_irec *mval,
3812 struct xfs_bmbt_irec *got,
3813 xfs_fileoff_t *bno,
3814 xfs_filblks_t len,
3815 xfs_fileoff_t obno,
3816 xfs_fileoff_t end,
3817 int n,
3818 int flags)
3819{
3820 if ((flags & XFS_BMAPI_ENTIRE) ||
3821 got->br_startoff + got->br_blockcount <= obno) {
3822 *mval = *got;
3823 if (isnullstartblock(got->br_startblock))
3824 mval->br_startblock = DELAYSTARTBLOCK;
3825 return;
3826 }
3827
3828 if (obno > *bno)
3829 *bno = obno;
3830 ASSERT((*bno >= obno) || (n == 0));
3831 ASSERT(*bno < end);
3832 mval->br_startoff = *bno;
3833 if (isnullstartblock(got->br_startblock))
3834 mval->br_startblock = DELAYSTARTBLOCK;
3835 else
3836 mval->br_startblock = got->br_startblock +
3837 (*bno - got->br_startoff);
3838 /*
3839 * Return the minimum of what we got and what we asked for for
3840 * the length. We can use the len variable here because it is
3841 * modified below and we could have been there before coming
3842 * here if the first part of the allocation didn't overlap what
3843 * was asked for.
3844 */
3845 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3846 got->br_blockcount - (*bno - got->br_startoff));
3847 mval->br_state = got->br_state;
3848 ASSERT(mval->br_blockcount <= len);
3849 return;
3850}
3851
3852/*
3853 * Update and validate the extent map to return
3854 */
3855STATIC void
3856xfs_bmapi_update_map(
3857 struct xfs_bmbt_irec **map,
3858 xfs_fileoff_t *bno,
3859 xfs_filblks_t *len,
3860 xfs_fileoff_t obno,
3861 xfs_fileoff_t end,
3862 int *n,
3863 int flags)
3864{
3865 xfs_bmbt_irec_t *mval = *map;
3866
3867 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3868 ((mval->br_startoff + mval->br_blockcount) <= end));
3869 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3870 (mval->br_startoff < obno));
3871
3872 *bno = mval->br_startoff + mval->br_blockcount;
3873 *len = end - *bno;
3874 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3875 /* update previous map with new information */
3876 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3877 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3878 ASSERT(mval->br_state == mval[-1].br_state);
3879 mval[-1].br_blockcount = mval->br_blockcount;
3880 mval[-1].br_state = mval->br_state;
3881 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3882 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3883 mval[-1].br_startblock != HOLESTARTBLOCK &&
3884 mval->br_startblock == mval[-1].br_startblock +
3885 mval[-1].br_blockcount &&
Christoph Hellwigc3a2f9f2018-07-11 22:26:01 -07003886 mval[-1].br_state == mval->br_state) {
Dave Chinneraef9a892011-09-18 20:40:44 +00003887 ASSERT(mval->br_startoff ==
3888 mval[-1].br_startoff + mval[-1].br_blockcount);
3889 mval[-1].br_blockcount += mval->br_blockcount;
3890 } else if (*n > 0 &&
3891 mval->br_startblock == DELAYSTARTBLOCK &&
3892 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3893 mval->br_startoff ==
3894 mval[-1].br_startoff + mval[-1].br_blockcount) {
3895 mval[-1].br_blockcount += mval->br_blockcount;
3896 mval[-1].br_state = mval->br_state;
3897 } else if (!((*n == 0) &&
3898 ((mval->br_startoff + mval->br_blockcount) <=
3899 obno))) {
3900 mval++;
3901 (*n)++;
3902 }
3903 *map = mval;
3904}
3905
3906/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003907 * Map file blocks to filesystem blocks without allocation.
3908 */
3909int
3910xfs_bmapi_read(
3911 struct xfs_inode *ip,
3912 xfs_fileoff_t bno,
3913 xfs_filblks_t len,
3914 struct xfs_bmbt_irec *mval,
3915 int *nmap,
3916 int flags)
3917{
3918 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig4b516ff2020-05-14 14:06:41 -07003919 int whichfork = xfs_bmapi_whichfork(flags);
3920 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003921 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003922 xfs_fileoff_t obno;
3923 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003924 struct xfs_iext_cursor icur;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003925 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003926 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003927 int n = 0;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003928
3929 ASSERT(*nmap >= 1);
Christoph Hellwig1a1c57b2020-05-14 14:06:40 -07003930 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003931 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003932
Christoph Hellwig4b516ff2020-05-14 14:06:41 -07003933 if (WARN_ON_ONCE(!ifp))
3934 return -EFSCORRUPTED;
3935
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07003936 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
3937 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT))
Dave Chinner24513372014-06-25 14:58:08 +10003938 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003939
3940 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003941 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003942
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003943 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003944
Christoph Hellwig862a8042021-04-13 11:15:09 -07003945 error = xfs_iread_extents(NULL, ip, whichfork);
3946 if (error)
3947 return error;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003948
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003949 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003950 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003951 end = bno + len;
3952 obno = bno;
3953
3954 while (bno < end && n < *nmap) {
3955 /* Reading past eof, act as though there's a hole up to end. */
3956 if (eof)
3957 got.br_startoff = end;
3958 if (got.br_startoff > bno) {
3959 /* Reading in a hole. */
3960 mval->br_startoff = bno;
3961 mval->br_startblock = HOLESTARTBLOCK;
3962 mval->br_blockcount =
3963 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3964 mval->br_state = XFS_EXT_NORM;
3965 bno += mval->br_blockcount;
3966 len -= mval->br_blockcount;
3967 mval++;
3968 n++;
3969 continue;
3970 }
3971
3972 /* set up the extent map to return. */
3973 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3974 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3975
3976 /* If we're done, stop now. */
3977 if (bno >= end || n >= *nmap)
3978 break;
3979
3980 /* Else go on to the next record. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003981 if (!xfs_iext_next_extent(ifp, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003982 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003983 }
3984 *nmap = n;
3985 return 0;
3986}
3987
Brian Fosterf65e6fad2017-03-08 09:58:08 -08003988/*
3989 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3990 * global pool and the extent inserted into the inode in-core extent tree.
3991 *
3992 * On entry, got refers to the first extent beyond the offset of the extent to
3993 * allocate or eof is specified if no such extent exists. On return, got refers
3994 * to the extent record that was inserted to the inode fork.
3995 *
3996 * Note that the allocated extent may have been merged with contiguous extents
3997 * during insertion into the inode fork. Thus, got does not reflect the current
3998 * state of the inode fork on return. If necessary, the caller can use lastx to
3999 * look up the updated record in the inode fork.
4000 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10004001int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004002xfs_bmapi_reserve_delalloc(
4003 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004004 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11004005 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004006 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11004007 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004008 struct xfs_bmbt_irec *got,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004009 struct xfs_iext_cursor *icur,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004010 int eof)
4011{
4012 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004013 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004014 xfs_extlen_t alen;
4015 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004016 int error;
Brian Foster974ae922016-11-28 14:57:42 +11004017 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004018
Brian Foster974ae922016-11-28 14:57:42 +11004019 /*
4020 * Cap the alloc length. Keep track of prealloc so we know whether to
4021 * tag the inode before we return.
4022 */
4023 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004024 if (!eof)
4025 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11004026 if (prealloc && alen >= len)
4027 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004028
4029 /* Figure out the extent size, adjust alen */
Shan Hai6ca30722018-01-23 13:56:11 -08004030 if (whichfork == XFS_COW_FORK) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004031 struct xfs_bmbt_irec prev;
Shan Hai6ca30722018-01-23 13:56:11 -08004032 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004033
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004034 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004035 prev.br_startoff = NULLFILEOFF;
4036
Shan Hai6ca30722018-01-23 13:56:11 -08004037 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004038 1, 0, &aoff, &alen);
4039 ASSERT(!error);
4040 }
4041
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004042 /*
4043 * Make a transaction-less quota reservation for delayed allocation
4044 * blocks. This number gets adjusted later. We return if we haven't
4045 * allocated blocks already inside this loop.
4046 */
Darrick J. Wong85546502021-01-22 16:48:34 -08004047 error = xfs_quota_reserve_blkres(ip, alen);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004048 if (error)
4049 return error;
4050
4051 /*
4052 * Split changing sb for alen and indlen since they could be coming
4053 * from different places.
4054 */
4055 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4056 ASSERT(indlen > 0);
4057
Shan Hai6ca30722018-01-23 13:56:11 -08004058 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004059 if (error)
4060 goto out_unreserve_quota;
4061
Dave Chinner0d485ad2015-02-23 21:22:03 +11004062 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004063 if (error)
4064 goto out_unreserve_blocks;
4065
4066
4067 ip->i_delayed_blks += alen;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07004068 xfs_mod_delalloc(ip->i_mount, alen + indlen);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004069
4070 got->br_startoff = aoff;
4071 got->br_startblock = nullstartblock(indlen);
4072 got->br_blockcount = alen;
4073 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004074
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004075 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004076
Brian Foster974ae922016-11-28 14:57:42 +11004077 /*
4078 * Tag the inode if blocks were preallocated. Note that COW fork
4079 * preallocation can occur at the start or end of the extent, even when
4080 * prealloc == 0, so we must also check the aligned offset and length.
4081 */
4082 if (whichfork == XFS_DATA_FORK && prealloc)
4083 xfs_inode_set_eofblocks_tag(ip);
4084 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4085 xfs_inode_set_cowblocks_tag(ip);
4086
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004087 return 0;
4088
4089out_unreserve_blocks:
Shan Hai6ca30722018-01-23 13:56:11 -08004090 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004091out_unreserve_quota:
4092 if (XFS_IS_QUOTA_ON(mp))
Darrick J. Wong85546502021-01-22 16:48:34 -08004093 xfs_quota_unreserve_blkres(ip, alen);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004094 return error;
4095}
4096
Dave Chinnercf11da92014-07-15 07:08:24 +10004097static int
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004098xfs_bmap_alloc_userdata(
4099 struct xfs_bmalloca *bma)
4100{
4101 struct xfs_mount *mp = bma->ip->i_mount;
4102 int whichfork = xfs_bmapi_whichfork(bma->flags);
4103 int error;
4104
4105 /*
4106 * Set the data type being allocated. For the data fork, the first data
4107 * in the file is treated differently to all other allocations. For the
4108 * attribute fork, we only need to ensure the allocated range is not on
4109 * the busy list.
4110 */
4111 bma->datatype = XFS_ALLOC_NOBUSY;
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004112 if (whichfork == XFS_DATA_FORK) {
Christoph Hellwigc34d5702019-10-30 12:25:00 -07004113 bma->datatype |= XFS_ALLOC_USERDATA;
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004114 if (bma->offset == 0)
4115 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004116
4117 if (mp->m_dalign && bma->length >= mp->m_dalign) {
4118 error = xfs_bmap_isaeof(bma, whichfork);
4119 if (error)
4120 return error;
4121 }
4122
4123 if (XFS_IS_REALTIME_INODE(bma->ip))
4124 return xfs_bmap_rtalloc(bma);
4125 }
4126
Chandan Babu R30151962021-01-22 16:48:17 -08004127 if (unlikely(XFS_TEST_ERROR(false, mp,
4128 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
4129 return xfs_bmap_exact_minlen_extent_alloc(bma);
4130
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004131 return xfs_bmap_btalloc(bma);
4132}
4133
4134static int
Dave Chinnercf11da92014-07-15 07:08:24 +10004135xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004136 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004137{
4138 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004139 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004140 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004141 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004142 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004143
Dave Chinnera99ebf42011-12-01 11:24:20 +00004144 ASSERT(bma->length > 0);
4145
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004146 /*
4147 * For the wasdelay case, we could also just allocate the stuff asked
4148 * for in this bmap call but that wouldn't be as good.
4149 */
4150 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004151 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4152 bma->offset = bma->got.br_startoff;
Darrick J. Wongf5be0842019-11-06 08:53:54 -08004153 if (!xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev))
4154 bma->prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004155 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004156 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004157 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004158 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004159 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004160 }
4161
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004162 if (bma->flags & XFS_BMAPI_CONTIG)
4163 bma->minlen = bma->length;
4164 else
4165 bma->minlen = 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004166
Chandan Babu R30151962021-01-22 16:48:17 -08004167 if (bma->flags & XFS_BMAPI_METADATA) {
4168 if (unlikely(XFS_TEST_ERROR(false, mp,
4169 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
4170 error = xfs_bmap_exact_minlen_extent_alloc(bma);
4171 else
4172 error = xfs_bmap_btalloc(bma);
4173 } else {
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004174 error = xfs_bmap_alloc_userdata(bma);
Chandan Babu R30151962021-01-22 16:48:17 -08004175 }
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004176 if (error || bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004177 return error;
4178
Christoph Hellwigfd638f12019-10-30 12:25:00 -07004179 if (bma->flags & XFS_BMAPI_ZERO) {
4180 error = xfs_zero_extent(bma->ip, bma->blkno, bma->length);
4181 if (error)
4182 return error;
4183 }
4184
Christoph Hellwigac1e0672021-04-13 11:15:11 -07004185 if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur)
Dave Chinner29c8d17a2011-09-18 20:41:00 +00004186 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004187 /*
4188 * Bump the number of extents we've allocated
4189 * in this call.
4190 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004191 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004192
Dave Chinner29c8d17a2011-09-18 20:41:00 +00004193 if (bma->cur)
Dave Chinner92219c22020-03-10 17:52:53 -07004194 bma->cur->bc_ino.flags =
Dave Chinner8ef54792020-03-10 17:54:38 -07004195 bma->wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004196
Dave Chinner963c30c2011-09-18 20:40:59 +00004197 bma->got.br_startoff = bma->offset;
4198 bma->got.br_startblock = bma->blkno;
4199 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004200 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004201
Darrick J. Wonga5949d32020-05-23 09:43:31 -07004202 if (bma->flags & XFS_BMAPI_PREALLOC)
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004203 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004204
Christoph Hellwigc6534242011-09-18 20:41:05 +00004205 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004206 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004207 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004208 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004209 whichfork, &bma->icur, &bma->cur, &bma->got,
Brian Foster92f9da32018-07-11 22:26:28 -07004210 &bma->logflags, bma->flags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004211
Christoph Hellwigc315c902011-09-18 20:41:02 +00004212 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004213 if (error)
4214 return error;
4215
4216 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004217 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4218 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4219 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004220 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004221 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004222
Dave Chinner963c30c2011-09-18 20:40:59 +00004223 ASSERT(bma->got.br_startoff <= bma->offset);
4224 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4225 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004226 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4227 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004228 return 0;
4229}
4230
Dave Chinnerb447fe52011-09-18 20:40:51 +00004231STATIC int
4232xfs_bmapi_convert_unwritten(
4233 struct xfs_bmalloca *bma,
4234 struct xfs_bmbt_irec *mval,
4235 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004236 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004237{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004238 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004239 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004240 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004241 int error;
4242
Dave Chinnerb447fe52011-09-18 20:40:51 +00004243 /* check if we need to do unwritten->real conversion */
4244 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4245 (flags & XFS_BMAPI_PREALLOC))
4246 return 0;
4247
4248 /* check if we need to do real->unwritten conversion */
4249 if (mval->br_state == XFS_EXT_NORM &&
4250 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4251 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4252 return 0;
4253
4254 /*
4255 * Modify (by adding) the state flag, if writing.
4256 */
4257 ASSERT(mval->br_blockcount <= len);
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(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004260 bma->ip, whichfork);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004261 }
4262 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4263 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4264
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004265 /*
4266 * Before insertion into the bmbt, zero the range being converted
4267 * if required.
4268 */
4269 if (flags & XFS_BMAPI_ZERO) {
4270 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4271 mval->br_blockcount);
4272 if (error)
4273 return error;
4274 }
4275
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004276 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
Brian Foster92f9da32018-07-11 22:26:28 -07004277 &bma->icur, &bma->cur, mval, &tmp_logflags);
Brian Foster2e588a462015-06-01 07:15:23 +10004278 /*
4279 * Log the inode core unconditionally in the unwritten extent conversion
4280 * path because the conversion might not have done so (e.g., if the
4281 * extent count hasn't changed). We need to make sure the inode is dirty
4282 * in the transaction for the sake of fsync(), even if nothing has
4283 * changed, because fsync() will not force the log for this transaction
4284 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004285 *
4286 * Note: If we're only converting cow fork extents, there aren't
4287 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a462015-06-01 07:15:23 +10004288 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004289 if (whichfork != XFS_COW_FORK)
4290 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004291 if (error)
4292 return error;
4293
4294 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004295 * Update our extent pointer, given that
4296 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4297 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004298 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004299 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004300
4301 /*
4302 * We may have combined previously unwritten space with written space,
4303 * so generate another request.
4304 */
4305 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004306 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004307 return 0;
4308}
4309
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004310static inline xfs_extlen_t
4311xfs_bmapi_minleft(
4312 struct xfs_trans *tp,
4313 struct xfs_inode *ip,
4314 int fork)
4315{
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004316 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, fork);
4317
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004318 if (tp && tp->t_firstblock != NULLFSBLOCK)
4319 return 0;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004320 if (ifp->if_format != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004321 return 1;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004322 return be16_to_cpu(ifp->if_broot->bb_level) + 1;
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004323}
4324
4325/*
4326 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4327 * a case where the data is changed, there's an error, and it's not logged so we
4328 * don't shutdown when we should. Don't bother logging extents/btree changes if
4329 * we converted to the other format.
4330 */
4331static void
4332xfs_bmapi_finish(
4333 struct xfs_bmalloca *bma,
4334 int whichfork,
4335 int error)
4336{
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004337 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4338
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004339 if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004340 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004341 bma->logflags &= ~xfs_ilog_fext(whichfork);
4342 else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004343 ifp->if_format != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004344 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4345
4346 if (bma->logflags)
4347 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4348 if (bma->cur)
4349 xfs_btree_del_cursor(bma->cur, error);
4350}
4351
Christoph Hellwig44032802011-09-18 20:40:48 +00004352/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004353 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4354 * extent state if necessary. Details behaviour is controlled by the flags
4355 * parameter. Only allocates blocks from a single allocation group, to avoid
4356 * locking problems.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004358int
4359xfs_bmapi_write(
4360 struct xfs_trans *tp, /* transaction pointer */
4361 struct xfs_inode *ip, /* incore inode */
4362 xfs_fileoff_t bno, /* starting file offs. mapped */
4363 xfs_filblks_t len, /* length to map in file */
4364 int flags, /* XFS_BMAPI_... */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004365 xfs_extlen_t total, /* total blocks needed */
4366 struct xfs_bmbt_irec *mval, /* output: map values */
Brian Foster6e702a52018-07-11 22:26:12 -07004367 int *nmap) /* i/o: mval size/count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368{
Darrick J. Wong4b0bce32019-03-19 08:16:22 -07004369 struct xfs_bmalloca bma = {
4370 .tp = tp,
4371 .ip = ip,
4372 .total = total,
4373 };
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004374 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004375 int whichfork = xfs_bmapi_whichfork(flags);
4376 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004377 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004378 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004379 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004380 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004381 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004382
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004384 xfs_fileoff_t orig_bno; /* original block number value */
4385 int orig_flags; /* original flags arg value */
4386 xfs_filblks_t orig_len; /* original value of len arg */
4387 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4388 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
4390 orig_bno = bno;
4391 orig_len = len;
4392 orig_flags = flags;
4393 orig_mval = mval;
4394 orig_nmap = *nmap;
4395#endif
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004396
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004398 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
Christoph Hellwig26b91c72019-02-18 09:38:48 -08004399 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004400 ASSERT(len > 0);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004401 ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004402 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004403 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004404
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004405 /* zeroing is for currently only for data extents, not metadata */
4406 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4407 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4408 /*
4409 * we can allocate unwritten extents or pre-zero allocated blocks,
4410 * but it makes no sense to do both at once. This would result in
4411 * zeroing the unwritten extent twice, but it still being an
4412 * unwritten extent....
4413 */
4414 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4415 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4416
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004417 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
Darrick J. Wonga71895c2019-11-11 12:53:22 -08004418 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Dave Chinner24513372014-06-25 14:58:08 +10004419 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004421
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004423 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004424
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004425 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004426
Christoph Hellwig862a8042021-04-13 11:15:09 -07004427 error = xfs_iread_extents(tp, ip, whichfork);
4428 if (error)
4429 goto error0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004430
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004431 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004432 eof = true;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004433 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004434 bma.prev.br_startoff = NULLFILEOFF;
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004435 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004436
Brian Foster627209f2019-02-01 09:14:23 -08004437 n = 0;
4438 end = bno + len;
4439 obno = bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004441 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004442
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004443 /* in hole or beyond EOF? */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004444 if (eof || bma.got.br_startoff > bno) {
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004445 /*
4446 * CoW fork conversions should /never/ hit EOF or
4447 * holes. There should always be something for us
4448 * to work on.
4449 */
4450 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4451 (flags & XFS_BMAPI_COWFORK)));
4452
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004453 need_alloc = true;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004454 } else if (isnullstartblock(bma.got.br_startblock)) {
4455 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004456 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004457
4458 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 * First, deal with the hole before the allocated space
4460 * that we found, if any.
4461 */
Christoph Hellwig26b91c72019-02-18 09:38:48 -08004462 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004463 bma.eof = eof;
4464 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4465 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004466 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004467 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004468
Dave Chinnera99ebf42011-12-01 11:24:20 +00004469 /*
4470 * There's a 32/64 bit type mismatch between the
4471 * allocation length request (which can be 64 bits in
4472 * length) and the bma length request, which is
4473 * xfs_extlen_t and therefore 32 bits. Hence we have to
4474 * check for 32-bit overflows and handle them here.
4475 */
4476 if (len > (xfs_filblks_t)MAXEXTLEN)
4477 bma.length = MAXEXTLEN;
4478 else
4479 bma.length = len;
4480
4481 ASSERT(len > 0);
4482 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004483 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 if (error)
4485 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004486 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004487 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004488
4489 /*
4490 * If this is a CoW allocation, record the data in
4491 * the refcount btree for orphan recovery.
4492 */
Darrick J. Wong74b4c5d2019-08-26 17:06:04 -07004493 if (whichfork == XFS_COW_FORK)
4494 xfs_refcount_alloc_cow_extent(tp, bma.blkno,
4495 bma.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004497
Dave Chinneraef9a892011-09-18 20:40:44 +00004498 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004499 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4500 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
Dave Chinnerb447fe52011-09-18 20:40:51 +00004502 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004503 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004504 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004505 continue;
4506 if (error)
4507 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
Dave Chinneraef9a892011-09-18 20:40:44 +00004509 /* update the extent map to return */
4510 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4511
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 /*
4513 * If we're done, stop now. Stop when we've allocated
4514 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4515 * the transaction may get too big.
4516 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004517 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004519
4520 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004521 bma.prev = bma.got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004522 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004523 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004526
Christoph Hellwigb101e332019-02-15 08:02:47 -08004527 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4528 whichfork);
4529 if (error)
4530 goto error0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004531
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004532 ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwigdaf83962020-05-18 10:27:22 -07004533 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork));
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004534 xfs_bmapi_finish(&bma, whichfork, 0);
4535 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4536 orig_nmap, *nmap);
4537 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538error0:
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004539 xfs_bmapi_finish(&bma, whichfork, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 return error;
4541}
4542
Brian Foster627209f2019-02-01 09:14:23 -08004543/*
4544 * Convert an existing delalloc extent to real blocks based on file offset. This
4545 * attempts to allocate the entire delalloc extent and may require multiple
4546 * invocations to allocate the target offset if a large enough physical extent
4547 * is not available.
4548 */
4549int
4550xfs_bmapi_convert_delalloc(
Brian Foster627209f2019-02-01 09:14:23 -08004551 struct xfs_inode *ip,
Brian Foster627209f2019-02-01 09:14:23 -08004552 int whichfork,
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004553 xfs_off_t offset,
4554 struct iomap *iomap,
Christoph Hellwig491ce612019-02-15 08:02:49 -08004555 unsigned int *seq)
Brian Foster627209f2019-02-01 09:14:23 -08004556{
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004557 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004558 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004559 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004560 struct xfs_bmalloca bma = { NULL };
Darrick J. Wongaf952ae2019-12-16 11:14:09 -08004561 uint16_t flags = 0;
Christoph Hellwig491ce612019-02-15 08:02:49 -08004562 struct xfs_trans *tp;
Brian Foster627209f2019-02-01 09:14:23 -08004563 int error;
Brian Foster627209f2019-02-01 09:14:23 -08004564
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004565 if (whichfork == XFS_COW_FORK)
4566 flags |= IOMAP_F_SHARED;
4567
Christoph Hellwig491ce612019-02-15 08:02:49 -08004568 /*
4569 * Space for the extent and indirect blocks was reserved when the
4570 * delalloc extent was created so there's no need to do so here.
4571 */
4572 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4573 XFS_TRANS_RESERVE, &tp);
4574 if (error)
4575 return error;
4576
4577 xfs_ilock(ip, XFS_ILOCK_EXCL);
Chandan Babu R727e1ac2021-01-22 16:48:11 -08004578
4579 error = xfs_iext_count_may_overflow(ip, whichfork,
4580 XFS_IEXT_ADD_NOSPLIT_CNT);
4581 if (error)
4582 goto out_trans_cancel;
4583
Christoph Hellwig491ce612019-02-15 08:02:49 -08004584 xfs_trans_ijoin(tp, ip, 0);
4585
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004586 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4587 bma.got.br_startoff > offset_fsb) {
4588 /*
4589 * No extent found in the range we are trying to convert. This
4590 * should only happen for the COW fork, where another thread
4591 * might have moved the extent to the data fork in the meantime.
4592 */
4593 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004594 error = -EAGAIN;
4595 goto out_trans_cancel;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004596 }
Brian Foster627209f2019-02-01 09:14:23 -08004597
4598 /*
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004599 * If we find a real extent here we raced with another thread converting
4600 * the extent. Just return the real extent at this offset.
Brian Foster627209f2019-02-01 09:14:23 -08004601 */
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004602 if (!isnullstartblock(bma.got.br_startblock)) {
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004603 xfs_bmbt_to_iomap(ip, iomap, &bma.got, flags);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004604 *seq = READ_ONCE(ifp->if_seq);
4605 goto out_trans_cancel;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004606 }
4607
4608 bma.tp = tp;
4609 bma.ip = ip;
4610 bma.wasdel = true;
4611 bma.offset = bma.got.br_startoff;
4612 bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount, MAXEXTLEN);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004613 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
Darrick J. Wonga5949d32020-05-23 09:43:31 -07004614
4615 /*
4616 * When we're converting the delalloc reservations backing dirty pages
4617 * in the page cache, we must be careful about how we create the new
4618 * extents:
4619 *
4620 * New CoW fork extents are created unwritten, turned into real extents
4621 * when we're about to write the data to disk, and mapped into the data
4622 * fork after the write finishes. End of story.
4623 *
4624 * New data fork extents must be mapped in as unwritten and converted
4625 * to real extents after the write succeeds to avoid exposing stale
4626 * disk contents if we crash.
4627 */
4628 bma.flags = XFS_BMAPI_PREALLOC;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004629 if (whichfork == XFS_COW_FORK)
Darrick J. Wonga5949d32020-05-23 09:43:31 -07004630 bma.flags |= XFS_BMAPI_COWFORK;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004631
4632 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4633 bma.prev.br_startoff = NULLFILEOFF;
4634
4635 error = xfs_bmapi_allocate(&bma);
4636 if (error)
4637 goto out_finish;
4638
4639 error = -ENOSPC;
4640 if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK))
4641 goto out_finish;
4642 error = -EFSCORRUPTED;
Christoph Hellwigeb77b232019-09-03 08:13:13 -07004643 if (WARN_ON_ONCE(!xfs_valid_startblock(ip, bma.got.br_startblock)))
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004644 goto out_finish;
4645
Christoph Hellwig125851a2019-02-15 08:02:49 -08004646 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4647 XFS_STATS_INC(mp, xs_xstrat_quick);
4648
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004649 ASSERT(!isnullstartblock(bma.got.br_startblock));
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004650 xfs_bmbt_to_iomap(ip, iomap, &bma.got, flags);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004651 *seq = READ_ONCE(ifp->if_seq);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004652
Darrick J. Wong74b4c5d2019-08-26 17:06:04 -07004653 if (whichfork == XFS_COW_FORK)
4654 xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004655
4656 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4657 whichfork);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004658 if (error)
4659 goto out_finish;
4660
4661 xfs_bmapi_finish(&bma, whichfork, 0);
4662 error = xfs_trans_commit(tp);
4663 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4664 return error;
4665
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004666out_finish:
4667 xfs_bmapi_finish(&bma, whichfork, error);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004668out_trans_cancel:
4669 xfs_trans_cancel(tp);
4670 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Brian Foster627209f2019-02-01 09:14:23 -08004671 return error;
4672}
4673
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004674int
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004675xfs_bmapi_remap(
4676 struct xfs_trans *tp,
4677 struct xfs_inode *ip,
4678 xfs_fileoff_t bno,
4679 xfs_filblks_t len,
4680 xfs_fsblock_t startblock,
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004681 int flags)
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004682{
4683 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004684 struct xfs_ifork *ifp;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004685 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004686 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004687 struct xfs_iext_cursor icur;
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004688 int whichfork = xfs_bmapi_whichfork(flags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004689 int logflags = 0, error;
4690
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004691 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004692 ASSERT(len > 0);
4693 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4694 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Darrick J. Wong7644bd92018-05-14 06:34:35 -07004695 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4696 XFS_BMAPI_NORMAP)));
4697 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4698 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004699
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004700 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
Darrick J. Wonga71895c2019-11-11 12:53:22 -08004701 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004702 return -EFSCORRUPTED;
4703 }
4704
4705 if (XFS_FORCED_SHUTDOWN(mp))
4706 return -EIO;
4707
Christoph Hellwig862a8042021-04-13 11:15:09 -07004708 error = xfs_iread_extents(tp, ip, whichfork);
4709 if (error)
4710 return error;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004711
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004712 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004713 /* make sure we only reflink into a hole. */
4714 ASSERT(got.br_startoff > bno);
4715 ASSERT(got.br_startoff - bno >= len);
4716 }
4717
Christoph Hellwig6e73a542021-03-29 11:11:40 -07004718 ip->i_nblocks += len;
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004719 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004720
Christoph Hellwigac1e0672021-04-13 11:15:11 -07004721 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004722 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner92219c22020-03-10 17:52:53 -07004723 cur->bc_ino.flags = 0;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004724 }
4725
4726 got.br_startoff = bno;
4727 got.br_startblock = startblock;
4728 got.br_blockcount = len;
Darrick J. Wong7644bd92018-05-14 06:34:35 -07004729 if (flags & XFS_BMAPI_PREALLOC)
4730 got.br_state = XFS_EXT_UNWRITTEN;
4731 else
4732 got.br_state = XFS_EXT_NORM;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004733
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004734 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
Brian Foster92f9da32018-07-11 22:26:28 -07004735 &cur, &got, &logflags, flags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004736 if (error)
4737 goto error0;
4738
Christoph Hellwigb101e332019-02-15 08:02:47 -08004739 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004740
4741error0:
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004742 if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004743 logflags &= ~XFS_ILOG_DEXT;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004744 else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE)
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004745 logflags &= ~XFS_ILOG_DBROOT;
4746
4747 if (logflags)
4748 xfs_trans_log_inode(tp, ip, logflags);
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07004749 if (cur)
4750 xfs_btree_del_cursor(cur, error);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004751 return error;
4752}
4753
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004755 * When a delalloc extent is split (e.g., due to a hole punch), the original
4756 * indlen reservation must be shared across the two new extents that are left
4757 * behind.
4758 *
4759 * Given the original reservation and the worst case indlen for the two new
4760 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004761 * reservation fairly across the two new extents. If necessary, steal available
4762 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4763 * ores == 1). The number of stolen blocks is returned. The availability and
4764 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004765 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004766static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004767xfs_bmap_split_indlen(
4768 xfs_filblks_t ores, /* original res. */
4769 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004770 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4771 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004772{
4773 xfs_filblks_t len1 = *indlen1;
4774 xfs_filblks_t len2 = *indlen2;
4775 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004776 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004777 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004778
4779 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004780 * Steal as many blocks as we can to try and satisfy the worst case
4781 * indlen for both new extents.
4782 */
Brian Foster75d65362017-02-13 22:48:30 -08004783 if (ores < nres && avail)
4784 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4785 ores += stolen;
4786
4787 /* nothing else to do if we've satisfied the new reservation */
4788 if (ores >= nres)
4789 return stolen;
Brian Fosterd34999c2016-03-15 11:42:47 +11004790
4791 /*
Brian Foster75d65362017-02-13 22:48:30 -08004792 * We can't meet the total required reservation for the two extents.
4793 * Calculate the percent of the overall shortage between both extents
4794 * and apply this percentage to each of the requested indlen values.
4795 * This distributes the shortage fairly and reduces the chances that one
4796 * of the two extents is left with nothing when extents are repeatedly
4797 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004798 */
Brian Foster75d65362017-02-13 22:48:30 -08004799 resfactor = (ores * 100);
4800 do_div(resfactor, nres);
4801 len1 *= resfactor;
4802 do_div(len1, 100);
4803 len2 *= resfactor;
4804 do_div(len2, 100);
4805 ASSERT(len1 + len2 <= ores);
4806 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4807
4808 /*
4809 * Hand out the remainder to each extent. If one of the two reservations
4810 * is zero, we want to make sure that one gets a block first. The loop
4811 * below starts with len1, so hand len2 a block right off the bat if it
4812 * is zero.
4813 */
4814 ores -= (len1 + len2);
4815 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4816 if (ores && !len2 && *indlen2) {
4817 len2++;
4818 ores--;
4819 }
4820 while (ores) {
4821 if (len1 < *indlen1) {
4822 len1++;
4823 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004824 }
Brian Foster75d65362017-02-13 22:48:30 -08004825 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004826 break;
Brian Foster75d65362017-02-13 22:48:30 -08004827 if (len2 < *indlen2) {
4828 len2++;
4829 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004830 }
4831 }
4832
4833 *indlen1 = len1;
4834 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004835
4836 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004837}
4838
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004839int
4840xfs_bmap_del_extent_delay(
4841 struct xfs_inode *ip,
4842 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004843 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004844 struct xfs_bmbt_irec *got,
4845 struct xfs_bmbt_irec *del)
4846{
4847 struct xfs_mount *mp = ip->i_mount;
4848 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4849 struct xfs_bmbt_irec new;
4850 int64_t da_old, da_new, da_diff = 0;
4851 xfs_fileoff_t del_endoff, got_endoff;
4852 xfs_filblks_t got_indlen, new_indlen, stolen;
Christoph Hellwig060ea652017-10-19 11:02:29 -07004853 int state = xfs_bmap_fork_to_state(whichfork);
4854 int error = 0;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004855 bool isrt;
4856
4857 XFS_STATS_INC(mp, xs_del_exlist);
4858
4859 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4860 del_endoff = del->br_startoff + del->br_blockcount;
4861 got_endoff = got->br_startoff + got->br_blockcount;
4862 da_old = startblockval(got->br_startblock);
4863 da_new = 0;
4864
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004865 ASSERT(del->br_blockcount > 0);
4866 ASSERT(got->br_startoff <= del->br_startoff);
4867 ASSERT(got_endoff >= del_endoff);
4868
4869 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004870 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004871
4872 do_div(rtexts, mp->m_sb.sb_rextsize);
4873 xfs_mod_frextents(mp, rtexts);
4874 }
4875
4876 /*
4877 * Update the inode delalloc counter now and wait to update the
4878 * sb counters as we might have to borrow some blocks for the
4879 * indirect block accounting.
4880 */
Darrick J. Wong85546502021-01-22 16:48:34 -08004881 ASSERT(!isrt);
4882 error = xfs_quota_unreserve_blkres(ip, del->br_blockcount);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004883 if (error)
4884 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004885 ip->i_delayed_blks -= del->br_blockcount;
4886
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004887 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004888 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004889 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004890 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004891
Christoph Hellwig0173c682017-10-17 14:16:22 -07004892 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4893 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004894 /*
4895 * Matches the whole extent. Delete the entry.
4896 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004897 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004898 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004899 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004900 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004901 /*
4902 * Deleting the first part of the extent.
4903 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004904 got->br_startoff = del_endoff;
4905 got->br_blockcount -= del->br_blockcount;
4906 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4907 got->br_blockcount), da_old);
4908 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004909 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004910 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004911 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004912 /*
4913 * Deleting the last part of the extent.
4914 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004915 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4916 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4917 got->br_blockcount), da_old);
4918 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004919 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004920 break;
4921 case 0:
4922 /*
4923 * Deleting the middle of the extent.
4924 *
4925 * Distribute the original indlen reservation across the two new
4926 * extents. Steal blocks from the deleted extent if necessary.
4927 * Stealing blocks simply fudges the fdblocks accounting below.
4928 * Warn if either of the new indlen reservations is zero as this
4929 * can lead to delalloc problems.
4930 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004931 got->br_blockcount = del->br_startoff - got->br_startoff;
4932 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4933
4934 new.br_blockcount = got_endoff - del_endoff;
4935 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4936
4937 WARN_ON_ONCE(!got_indlen || !new_indlen);
4938 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4939 del->br_blockcount);
4940
4941 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004942
4943 new.br_startoff = del_endoff;
4944 new.br_state = got->br_state;
4945 new.br_startblock = nullstartblock((int)new_indlen);
4946
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004947 xfs_iext_update_extent(ip, state, icur, got);
4948 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004949 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004950
4951 da_new = got_indlen + new_indlen - stolen;
4952 del->br_blockcount -= stolen;
4953 break;
4954 }
4955
4956 ASSERT(da_old >= da_new);
4957 da_diff = da_old - da_new;
4958 if (!isrt)
4959 da_diff += del->br_blockcount;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07004960 if (da_diff) {
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004961 xfs_mod_fdblocks(mp, da_diff, false);
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07004962 xfs_mod_delalloc(mp, -da_diff);
4963 }
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004964 return error;
4965}
4966
4967void
4968xfs_bmap_del_extent_cow(
4969 struct xfs_inode *ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004970 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004971 struct xfs_bmbt_irec *got,
4972 struct xfs_bmbt_irec *del)
4973{
4974 struct xfs_mount *mp = ip->i_mount;
4975 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4976 struct xfs_bmbt_irec new;
4977 xfs_fileoff_t del_endoff, got_endoff;
4978 int state = BMAP_COWFORK;
4979
4980 XFS_STATS_INC(mp, xs_del_exlist);
4981
4982 del_endoff = del->br_startoff + del->br_blockcount;
4983 got_endoff = got->br_startoff + got->br_blockcount;
4984
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004985 ASSERT(del->br_blockcount > 0);
4986 ASSERT(got->br_startoff <= del->br_startoff);
4987 ASSERT(got_endoff >= del_endoff);
4988 ASSERT(!isnullstartblock(got->br_startblock));
4989
4990 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004991 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004992 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004993 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004994
Christoph Hellwig0173c682017-10-17 14:16:22 -07004995 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4996 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004997 /*
4998 * Matches the whole extent. Delete the entry.
4999 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005000 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005001 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005002 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07005003 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005004 /*
5005 * Deleting the first part of the extent.
5006 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005007 got->br_startoff = del_endoff;
5008 got->br_blockcount -= del->br_blockcount;
5009 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005010 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005011 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07005012 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005013 /*
5014 * Deleting the last part of the extent.
5015 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005016 got->br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005017 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005018 break;
5019 case 0:
5020 /*
5021 * Deleting the middle of the extent.
5022 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005023 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005024
5025 new.br_startoff = del_endoff;
5026 new.br_blockcount = got_endoff - del_endoff;
5027 new.br_state = got->br_state;
5028 new.br_startblock = del->br_startblock + del->br_blockcount;
5029
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005030 xfs_iext_update_extent(ip, state, icur, got);
5031 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005032 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005033 break;
5034 }
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08005035 ip->i_delayed_blks -= del->br_blockcount;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005036}
5037
Brian Fostera9bd24a2016-03-15 11:42:46 +11005038/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11005039 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07005040 * after removing space.
Dave Chinner9e5987a72013-02-25 12:31:26 +11005041 */
5042STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005043xfs_bmap_del_extent_real(
Dave Chinner9e5987a72013-02-25 12:31:26 +11005044 xfs_inode_t *ip, /* incore inode pointer */
5045 xfs_trans_t *tp, /* current transaction pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005046 struct xfs_iext_cursor *icur,
Dave Chinner9e5987a72013-02-25 12:31:26 +11005047 xfs_btree_cur_t *cur, /* if null, not a btree */
5048 xfs_bmbt_irec_t *del, /* data to remove from extents */
5049 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005050 int whichfork, /* data or attr fork */
5051 int bflags) /* bmapi flags */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005052{
Dave Chinner9e5987a72013-02-25 12:31:26 +11005053 xfs_fsblock_t del_endblock=0; /* first block past del */
5054 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005055 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005056 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07005057 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005058 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005059 xfs_fileoff_t got_endoff; /* first offset past got */
5060 int i; /* temp state */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07005061 struct xfs_ifork *ifp; /* inode fork pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005062 xfs_mount_t *mp; /* mount structure */
5063 xfs_filblks_t nblks; /* quota/sb block count */
5064 xfs_bmbt_irec_t new; /* new record to be inserted */
5065 /* REFERENCED */
5066 uint qfield; /* quota field to update */
Christoph Hellwig060ea652017-10-19 11:02:29 -07005067 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005068 struct xfs_bmbt_irec old;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005069
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005070 mp = ip->i_mount;
5071 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005072
Dave Chinner9e5987a72013-02-25 12:31:26 +11005073 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005074 ASSERT(del->br_blockcount > 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005075 xfs_iext_get_extent(ifp, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005076 ASSERT(got.br_startoff <= del->br_startoff);
5077 del_endoff = del->br_startoff + del->br_blockcount;
5078 got_endoff = got.br_startoff + got.br_blockcount;
5079 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005080 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a72013-02-25 12:31:26 +11005081 qfield = 0;
5082 error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005083
Christoph Hellwig1b24b632017-10-17 14:16:22 -07005084 /*
5085 * If it's the case where the directory code is running with no block
5086 * reservation, and the deleted block is in the middle of its extent,
5087 * and the resulting insert of an extent would cause transformation to
5088 * btree format, then reject it. The calling code will then swap blocks
5089 * around instead. We have to do this now, rather than waiting for the
5090 * conversion to btree format, since the transaction will be dirty then.
5091 */
5092 if (tp->t_blk_res == 0 &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005093 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005094 ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) &&
Christoph Hellwig1b24b632017-10-17 14:16:22 -07005095 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
5096 return -ENOSPC;
5097
5098 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005099 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005100 xfs_filblks_t len;
Dave Chinner0703a8e2018-06-08 09:54:22 -07005101 xfs_extlen_t mod;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005102
Dave Chinner0703a8e2018-06-08 09:54:22 -07005103 len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize,
5104 &mod);
5105 ASSERT(mod == 0);
5106
Darrick J. Wong8df0fa32020-09-21 09:15:08 -07005107 if (!(bflags & XFS_BMAPI_REMAP)) {
5108 xfs_fsblock_t bno;
5109
5110 bno = div_u64_rem(del->br_startblock,
5111 mp->m_sb.sb_rextsize, &mod);
5112 ASSERT(mod == 0);
5113
5114 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5115 if (error)
5116 goto done;
5117 }
5118
Dave Chinner9e5987a72013-02-25 12:31:26 +11005119 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005120 nblks = len * mp->m_sb.sb_rextsize;
5121 qfield = XFS_TRANS_DQ_RTBCOUNT;
5122 } else {
5123 do_fx = 1;
5124 nblks = del->br_blockcount;
5125 qfield = XFS_TRANS_DQ_BCOUNT;
5126 }
5127
5128 del_endblock = del->br_startblock + del->br_blockcount;
5129 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005130 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005131 if (error)
5132 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005133 if (XFS_IS_CORRUPT(mp, i != 1)) {
5134 error = -EFSCORRUPTED;
5135 goto done;
5136 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11005137 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005138
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005139 if (got.br_startoff == del->br_startoff)
5140 state |= BMAP_LEFT_FILLING;
5141 if (got_endoff == del_endoff)
5142 state |= BMAP_RIGHT_FILLING;
5143
5144 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5145 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005146 /*
5147 * Matches the whole extent. Delete the entry.
5148 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005149 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005150 xfs_iext_prev(ifp, icur);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005151 ifp->if_nextents--;
5152
Dave Chinner9e5987a72013-02-25 12:31:26 +11005153 flags |= XFS_ILOG_CORE;
5154 if (!cur) {
5155 flags |= xfs_ilog_fext(whichfork);
5156 break;
5157 }
5158 if ((error = xfs_btree_delete(cur, &i)))
5159 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005160 if (XFS_IS_CORRUPT(mp, i != 1)) {
5161 error = -EFSCORRUPTED;
5162 goto done;
5163 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11005164 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005165 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005166 /*
5167 * Deleting the first part of the extent.
5168 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005169 got.br_startoff = del_endoff;
5170 got.br_startblock = del_endblock;
5171 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005172 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005173 if (!cur) {
5174 flags |= xfs_ilog_fext(whichfork);
5175 break;
5176 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005177 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005178 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005179 goto done;
5180 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005181 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005182 /*
5183 * Deleting the last part of the extent.
5184 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005185 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005186 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005187 if (!cur) {
5188 flags |= xfs_ilog_fext(whichfork);
5189 break;
5190 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005191 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005192 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005193 goto done;
5194 break;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005195 case 0:
5196 /*
5197 * Deleting the middle of the extent.
5198 */
Chandan Babu R0dbc5cb2021-01-22 16:48:12 -08005199
5200 /*
5201 * For directories, -ENOSPC is returned since a directory entry
5202 * remove operation must not fail due to low extent count
5203 * availability. -ENOSPC will be handled by higher layers of XFS
5204 * by letting the corresponding empty Data/Free blocks to linger
5205 * until a future remove operation. Dabtree blocks would be
5206 * swapped with the last block in the leaf space and then the
5207 * new last block will be unmapped.
Chandan Babu R02092a22021-01-22 16:48:13 -08005208 *
5209 * The above logic also applies to the source directory entry of
5210 * a rename operation.
Chandan Babu R0dbc5cb2021-01-22 16:48:12 -08005211 */
5212 error = xfs_iext_count_may_overflow(ip, whichfork, 1);
5213 if (error) {
5214 ASSERT(S_ISDIR(VFS_I(ip)->i_mode) &&
5215 whichfork == XFS_DATA_FORK);
5216 error = -ENOSPC;
5217 goto done;
5218 }
5219
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005220 old = got;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07005221
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005222 got.br_blockcount = del->br_startoff - got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005223 xfs_iext_update_extent(ip, state, icur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005224
Dave Chinner9e5987a72013-02-25 12:31:26 +11005225 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005226 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005227 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005228 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005229
Christoph Hellwige1d75532017-10-17 14:16:21 -07005230 flags |= XFS_ILOG_CORE;
5231 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005232 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005233 if (error)
5234 goto done;
5235 error = xfs_btree_increment(cur, 0, &i);
5236 if (error)
5237 goto done;
5238 cur->bc_rec.b = new;
5239 error = xfs_btree_insert(cur, &i);
5240 if (error && error != -ENOSPC)
5241 goto done;
5242 /*
5243 * If get no-space back from btree insert, it tried a
5244 * split, and we have a zero block reservation. Fix up
5245 * our state and return the error.
5246 */
5247 if (error == -ENOSPC) {
5248 /*
5249 * Reset the cursor, don't trust it after any
5250 * insert operation.
5251 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005252 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005253 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005254 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005255 if (XFS_IS_CORRUPT(mp, i != 1)) {
5256 error = -EFSCORRUPTED;
5257 goto done;
5258 }
Christoph Hellwige1d75532017-10-17 14:16:21 -07005259 /*
5260 * Update the btree record back
5261 * to the original value.
5262 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005263 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005264 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005265 goto done;
5266 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005267 * Reset the extent record back
5268 * to the original value.
Dave Chinner9e5987a72013-02-25 12:31:26 +11005269 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005270 xfs_iext_update_extent(ip, state, icur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005271 flags = 0;
5272 error = -ENOSPC;
5273 goto done;
5274 }
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005275 if (XFS_IS_CORRUPT(mp, i != 1)) {
5276 error = -EFSCORRUPTED;
5277 goto done;
5278 }
Christoph Hellwige1d75532017-10-17 14:16:21 -07005279 } else
5280 flags |= xfs_ilog_fext(whichfork);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005281
5282 ifp->if_nextents++;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005283 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005284 xfs_iext_insert(ip, icur, &new, state);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005285 break;
5286 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005287
5288 /* remove reverse mapping */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07005289 xfs_rmap_unmap_extent(tp, ip, whichfork, del);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005290
Dave Chinner9e5987a72013-02-25 12:31:26 +11005291 /*
5292 * If we need to, add to list of extents to delete.
5293 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005294 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005295 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
Darrick J. Wong74b4c5d2019-08-26 17:06:04 -07005296 xfs_refcount_decrease_extent(tp, del);
Brian Fosterfcb762f2018-05-09 08:45:04 -07005297 } else {
Brian Foster0f37d172018-08-01 07:20:34 -07005298 __xfs_bmap_add_free(tp, del->br_startblock,
Brian Foster4e529332018-05-10 09:35:42 -07005299 del->br_blockcount, NULL,
5300 (bflags & XFS_BMAPI_NODISCARD) ||
5301 del->br_state == XFS_EXT_UNWRITTEN);
Brian Fosterfcb762f2018-05-09 08:45:04 -07005302 }
Darrick J. Wong62aab202016-10-03 09:11:23 -07005303 }
5304
Dave Chinner9e5987a72013-02-25 12:31:26 +11005305 /*
5306 * Adjust inode # blocks in the file.
5307 */
5308 if (nblks)
Christoph Hellwig6e73a542021-03-29 11:11:40 -07005309 ip->i_nblocks -= nblks;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005310 /*
5311 * Adjust quota data.
5312 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005313 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a72013-02-25 12:31:26 +11005314 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5315
Dave Chinner9e5987a72013-02-25 12:31:26 +11005316done:
5317 *logflagsp = flags;
5318 return error;
5319}
5320
5321/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 * Unmap (remove) blocks from a file.
5323 * If nexts is nonzero then the number of extents to remove is limited to
5324 * that value. If not all extents in the block range can be removed then
5325 * *done is set.
5326 */
5327int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005328__xfs_bunmapi(
Brian Fosterccd9d912018-07-11 22:26:13 -07005329 struct xfs_trans *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005331 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005332 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 int flags, /* misc flags */
Brian Foster2af52842018-07-11 22:26:25 -07005334 xfs_extnum_t nexts) /* number of extents max */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335{
Brian Fosterccd9d912018-07-11 22:26:13 -07005336 struct xfs_btree_cur *cur; /* bmap btree cursor */
5337 struct xfs_bmbt_irec del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 int error; /* error return value */
5339 xfs_extnum_t extno; /* extent number in list */
Brian Fosterccd9d912018-07-11 22:26:13 -07005340 struct xfs_bmbt_irec got; /* current extent record */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07005341 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 int isrt; /* freeing in rt area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 int logflags; /* transaction logging flags */
5344 xfs_extlen_t mod; /* rt extent offset */
Darrick J. Wonga71895c2019-11-11 12:53:22 -08005345 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 int tmp_logflags; /* partial logging flags */
5347 int wasdel; /* was a delayed alloc extent */
5348 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005350 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005351 xfs_fileoff_t max_len;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005352 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005353 struct xfs_iext_cursor icur;
5354 bool done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005356 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005357
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005358 whichfork = xfs_bmapi_whichfork(flags);
5359 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005361 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)))
Dave Chinner24513372014-06-25 14:58:08 +10005362 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005364 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005365
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005366 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 ASSERT(len > 0);
5368 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005369
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005370 /*
5371 * Guesstimate how many blocks we can unmap without running the risk of
5372 * blowing out the transaction with a mix of EFIs and reflink
5373 * adjustments.
5374 */
Darrick J. Wong8c57b882017-12-10 18:03:53 -08005375 if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005376 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5377 else
5378 max_len = len;
5379
Christoph Hellwig862a8042021-04-13 11:15:09 -07005380 error = xfs_iread_extents(tp, ip, whichfork);
5381 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 return error;
Christoph Hellwig862a8042021-04-13 11:15:09 -07005383
Eric Sandeen5d829302016-11-08 12:59:42 +11005384 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005385 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 return 0;
5387 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005388 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005389 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwigdc560152017-10-23 16:32:39 -07005390 end = start + len;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005391
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005392 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
Christoph Hellwigdc560152017-10-23 16:32:39 -07005393 *rlen = 0;
5394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 }
Christoph Hellwigdc560152017-10-23 16:32:39 -07005396 end--;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005397
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 logflags = 0;
Christoph Hellwigac1e0672021-04-13 11:15:11 -07005399 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005400 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005401 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner92219c22020-03-10 17:52:53 -07005402 cur->bc_ino.flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 } else
5404 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005405
5406 if (isrt) {
5407 /*
5408 * Synchronize by locking the bitmap inode.
5409 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005410 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005411 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005412 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5413 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005414 }
5415
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 extno = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005417 while (end != (xfs_fileoff_t)-1 && end >= start &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005418 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005420 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 * Just back up to the previous extent, if so.
5422 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005423 if (got.br_startoff > end &&
5424 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5425 done = true;
5426 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 }
5428 /*
5429 * Is the last block of this extent before the range
5430 * we're supposed to delete? If so, we're done.
5431 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005432 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005434 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 break;
5436 /*
5437 * Then deal with the (possibly delayed) allocated space
5438 * we found.
5439 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005441 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005442
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 if (got.br_startoff < start) {
5444 del.br_startoff = start;
5445 del.br_blockcount -= start - got.br_startoff;
5446 if (!wasdel)
5447 del.br_startblock += start - got.br_startoff;
5448 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005449 if (del.br_startoff + del.br_blockcount > end + 1)
5450 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005451
5452 /* How much can we safely unmap? */
5453 if (max_len < del.br_blockcount) {
5454 del.br_startoff += del.br_blockcount - max_len;
5455 if (!wasdel)
5456 del.br_startblock += del.br_blockcount - max_len;
5457 del.br_blockcount = max_len;
5458 }
5459
Dave Chinner0703a8e2018-06-08 09:54:22 -07005460 if (!isrt)
5461 goto delete;
5462
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 sum = del.br_startblock + del.br_blockcount;
Dave Chinner0703a8e2018-06-08 09:54:22 -07005464 div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod);
5465 if (mod) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 /*
5467 * Realtime extent not lined up at the end.
5468 * The extent could have been split into written
5469 * and unwritten pieces, or we could just be
5470 * unmapping part of it. But we can't really
5471 * get rid of part of a realtime extent.
5472 */
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11005473 if (del.br_state == XFS_EXT_UNWRITTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 /*
5475 * This piece is unwritten, or we're not
5476 * using unwritten extents. Skip over it.
5477 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005478 ASSERT(end >= mod);
5479 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 del.br_blockcount : mod;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005481 if (end < got.br_startoff &&
5482 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5483 done = true;
5484 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 }
5486 continue;
5487 }
5488 /*
5489 * It's written, turn it unwritten.
5490 * This is better than zeroing it.
5491 */
5492 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005493 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 /*
5495 * If this spans a realtime extent boundary,
5496 * chop it back to the start of the one we end at.
5497 */
5498 if (del.br_blockcount > mod) {
5499 del.br_startoff += del.br_blockcount - mod;
5500 del.br_startblock += del.br_blockcount - mod;
5501 del.br_blockcount = mod;
5502 }
5503 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005504 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005505 whichfork, &icur, &cur, &del,
Brian Foster92f9da32018-07-11 22:26:28 -07005506 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 if (error)
5508 goto error0;
5509 goto nodelete;
5510 }
Dave Chinner0703a8e2018-06-08 09:54:22 -07005511 div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod);
5512 if (mod) {
Omar Sandoval0c4da702019-11-26 16:58:07 -08005513 xfs_extlen_t off = mp->m_sb.sb_rextsize - mod;
5514
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 /*
5516 * Realtime extent is lined up at the end but not
5517 * at the front. We'll get rid of full extents if
5518 * we can.
5519 */
Omar Sandoval0c4da702019-11-26 16:58:07 -08005520 if (del.br_blockcount > off) {
5521 del.br_blockcount -= off;
5522 del.br_startoff += off;
5523 del.br_startblock += off;
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11005524 } else if (del.br_startoff == start &&
5525 (del.br_state == XFS_EXT_UNWRITTEN ||
5526 tp->t_blk_res == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 /*
5528 * Can't make it unwritten. There isn't
5529 * a full extent here so just skip it.
5530 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005531 ASSERT(end >= del.br_blockcount);
5532 end -= del.br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005533 if (got.br_startoff > end &&
5534 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5535 done = true;
5536 break;
5537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 continue;
5539 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005540 struct xfs_bmbt_irec prev;
Omar Sandoval0c4da702019-11-26 16:58:07 -08005541 xfs_fileoff_t unwrite_start;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005542
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 /*
5544 * This one is already unwritten.
5545 * It must have a written left neighbor.
5546 * Unwrite the killed part of that one and
5547 * try again.
5548 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005549 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5550 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005552 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 ASSERT(del.br_startblock ==
5554 prev.br_startblock + prev.br_blockcount);
Omar Sandoval0c4da702019-11-26 16:58:07 -08005555 unwrite_start = max3(start,
5556 del.br_startoff - mod,
5557 prev.br_startoff);
5558 mod = unwrite_start - prev.br_startoff;
5559 prev.br_startoff = unwrite_start;
5560 prev.br_startblock += mod;
5561 prev.br_blockcount -= mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005563 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005564 ip, whichfork, &icur, &cur,
Brian Foster92f9da32018-07-11 22:26:28 -07005565 &prev, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 if (error)
5567 goto error0;
5568 goto nodelete;
5569 } else {
5570 ASSERT(del.br_state == XFS_EXT_NORM);
5571 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005572 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005573 ip, whichfork, &icur, &cur,
Brian Foster92f9da32018-07-11 22:26:28 -07005574 &del, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 if (error)
5576 goto error0;
5577 goto nodelete;
5578 }
5579 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005580
Dave Chinner0703a8e2018-06-08 09:54:22 -07005581delete:
Brian Fosterb2706a02016-03-15 11:42:46 +11005582 if (wasdel) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005583 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005584 &got, &del);
5585 } else {
Brian Foster81ba8f32018-07-11 22:26:16 -07005586 error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5587 &del, &tmp_logflags, whichfork,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005588 flags);
5589 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005590 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005591
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 if (error)
5593 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005594
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005595 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005596 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 /*
5599 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005601 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005602 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5603 (got.br_startoff > end &&
5604 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5605 done = true;
5606 break;
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 extno++;
5609 }
5610 }
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005611 if (done || end == (xfs_fileoff_t)-1 || end < start)
Darrick J. Wong44535932016-10-03 09:11:29 -07005612 *rlen = 0;
5613 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005614 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005615
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 /*
5617 * Convert to a btree if necessary.
5618 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005619 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07005621 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5622 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 logflags |= tmp_logflags;
Christoph Hellwigb101e332019-02-15 08:02:47 -08005624 } else {
5625 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 }
Christoph Hellwigb101e332019-02-15 08:02:47 -08005628
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629error0:
5630 /*
5631 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005632 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005634 if ((logflags & xfs_ilog_fext(whichfork)) &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005635 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005636 logflags &= ~xfs_ilog_fext(whichfork);
5637 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005638 ifp->if_format != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005639 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 /*
5641 * Log inode even in the error case, if the transaction
5642 * is dirty we'll need to shut down the filesystem.
5643 */
5644 if (logflags)
5645 xfs_trans_log_inode(tp, ip, logflags);
5646 if (cur) {
Brian Fostercf612de2018-07-11 22:26:29 -07005647 if (!error)
Dave Chinner92219c22020-03-10 17:52:53 -07005648 cur->bc_ino.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07005649 xfs_btree_del_cursor(cur, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 }
5651 return error;
5652}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005653
Darrick J. Wong44535932016-10-03 09:11:29 -07005654/* Unmap a range of a file. */
5655int
5656xfs_bunmapi(
5657 xfs_trans_t *tp,
5658 struct xfs_inode *ip,
5659 xfs_fileoff_t bno,
5660 xfs_filblks_t len,
5661 int flags,
5662 xfs_extnum_t nexts,
Darrick J. Wong44535932016-10-03 09:11:29 -07005663 int *done)
5664{
5665 int error;
5666
Brian Foster2af52842018-07-11 22:26:25 -07005667 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
Darrick J. Wong44535932016-10-03 09:11:29 -07005668 *done = (len == 0);
5669 return error;
5670}
5671
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005672/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005673 * Determine whether an extent shift can be accomplished by a merge with the
5674 * extent that precedes the target hole of the shift.
5675 */
5676STATIC bool
5677xfs_bmse_can_merge(
5678 struct xfs_bmbt_irec *left, /* preceding extent */
5679 struct xfs_bmbt_irec *got, /* current extent to shift */
5680 xfs_fileoff_t shift) /* shift fsb */
5681{
5682 xfs_fileoff_t startoff;
5683
5684 startoff = got->br_startoff - shift;
5685
5686 /*
5687 * The extent, once shifted, must be adjacent in-file and on-disk with
5688 * the preceding extent.
5689 */
5690 if ((left->br_startoff + left->br_blockcount != startoff) ||
5691 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5692 (left->br_state != got->br_state) ||
5693 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5694 return false;
5695
5696 return true;
5697}
5698
5699/*
5700 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5701 * hole in the file. If an extent shift would result in the extent being fully
5702 * adjacent to the extent that currently precedes the hole, we can merge with
5703 * the preceding extent rather than do the shift.
5704 *
5705 * This function assumes the caller has verified a shift-by-merge is possible
5706 * with the provided extents via xfs_bmse_can_merge().
5707 */
5708STATIC int
5709xfs_bmse_merge(
Brian Foster0f37d172018-08-01 07:20:34 -07005710 struct xfs_trans *tp,
Brian Fosterddb19e32014-09-23 15:38:09 +10005711 struct xfs_inode *ip,
5712 int whichfork,
5713 xfs_fileoff_t shift, /* shift fsb */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005714 struct xfs_iext_cursor *icur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005715 struct xfs_bmbt_irec *got, /* extent to shift */
5716 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005717 struct xfs_btree_cur *cur,
Brian Foster0f37d172018-08-01 07:20:34 -07005718 int *logflags) /* output */
Brian Fosterddb19e32014-09-23 15:38:09 +10005719{
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005720 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005721 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005722 xfs_filblks_t blockcount;
5723 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005724 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005725
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005726 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005727
5728 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5729 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005730 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005731
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005732 new = *left;
5733 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005734
5735 /*
5736 * Update the on-disk extent count, the btree if necessary and log the
5737 * inode.
5738 */
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005739 ifp->if_nextents--;
Brian Fosterddb19e32014-09-23 15:38:09 +10005740 *logflags |= XFS_ILOG_CORE;
5741 if (!cur) {
5742 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005743 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005744 }
5745
5746 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005747 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005748 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005749 return error;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005750 if (XFS_IS_CORRUPT(mp, i != 1))
5751 return -EFSCORRUPTED;
Brian Fosterddb19e32014-09-23 15:38:09 +10005752
5753 error = xfs_btree_delete(cur, &i);
5754 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005755 return error;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005756 if (XFS_IS_CORRUPT(mp, i != 1))
5757 return -EFSCORRUPTED;
Brian Fosterddb19e32014-09-23 15:38:09 +10005758
5759 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005760 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005761 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005762 return error;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005763 if (XFS_IS_CORRUPT(mp, i != 1))
5764 return -EFSCORRUPTED;
Brian Fosterddb19e32014-09-23 15:38:09 +10005765
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005766 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005767 if (error)
5768 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005769
Brian Fostere20e1742019-09-18 17:50:45 -07005770 /* change to extent format if required after extent removal */
5771 error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork);
5772 if (error)
5773 return error;
5774
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005775done:
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005776 xfs_iext_remove(ip, icur, 0);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005777 xfs_iext_prev(ifp, icur);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005778 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5779 &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005780
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005781 /* update reverse mapping. rmap functions merge the rmaps for us */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07005782 xfs_rmap_unmap_extent(tp, ip, whichfork, got);
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005783 memcpy(&new, got, sizeof(new));
5784 new.br_startoff = left->br_startoff + left->br_blockcount;
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07005785 xfs_rmap_map_extent(tp, ip, whichfork, &new);
5786 return 0;
Brian Fosterddb19e32014-09-23 15:38:09 +10005787}
5788
Christoph Hellwigbf806282017-10-19 11:07:34 -07005789static int
5790xfs_bmap_shift_update_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07005791 struct xfs_trans *tp,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005792 struct xfs_inode *ip,
5793 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005794 struct xfs_iext_cursor *icur,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005795 struct xfs_bmbt_irec *got,
5796 struct xfs_btree_cur *cur,
5797 int *logflags,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005798 xfs_fileoff_t startoff)
Brian Fostera979bdf2014-09-23 15:39:04 +10005799{
Christoph Hellwigbf806282017-10-19 11:07:34 -07005800 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005801 struct xfs_bmbt_irec prev = *got;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005802 int error, i;
Brian Fostera979bdf2014-09-23 15:39:04 +10005803
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005804 *logflags |= XFS_ILOG_CORE;
5805
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005806 got->br_startoff = startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005807
5808 if (cur) {
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005809 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005810 if (error)
5811 return error;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005812 if (XFS_IS_CORRUPT(mp, i != 1))
5813 return -EFSCORRUPTED;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005814
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005815 error = xfs_bmbt_update(cur, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005816 if (error)
5817 return error;
5818 } else {
5819 *logflags |= XFS_ILOG_DEXT;
5820 }
5821
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005822 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5823 got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005824
Darrick J. Wong9c194642016-08-03 12:16:05 +10005825 /* update reverse mapping */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07005826 xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
5827 xfs_rmap_map_extent(tp, ip, whichfork, got);
5828 return 0;
Brian Fostera979bdf2014-09-23 15:39:04 +10005829}
5830
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005831int
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005832xfs_bmap_collapse_extents(
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005833 struct xfs_trans *tp,
5834 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005835 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005836 xfs_fileoff_t offset_shift_fsb,
Brian Foster333f9502018-07-11 22:26:27 -07005837 bool *done)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005838{
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005839 int whichfork = XFS_DATA_FORK;
5840 struct xfs_mount *mp = ip->i_mount;
5841 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5842 struct xfs_btree_cur *cur = NULL;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005843 struct xfs_bmbt_irec got, prev;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005844 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005845 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005846 int error = 0;
5847 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005848
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005849 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
Darrick J. Wonga71895c2019-11-11 12:53:22 -08005850 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Dave Chinner24513372014-06-25 14:58:08 +10005851 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005852 }
5853
5854 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005855 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005856
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005857 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005858
Christoph Hellwig862a8042021-04-13 11:15:09 -07005859 error = xfs_iread_extents(tp, ip, whichfork);
5860 if (error)
5861 return error;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005862
Christoph Hellwigac1e0672021-04-13 11:15:11 -07005863 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
Brian Fosterddb19e32014-09-23 15:38:09 +10005864 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner92219c22020-03-10 17:52:53 -07005865 cur->bc_ino.flags = 0;
Brian Fosterddb19e32014-09-23 15:38:09 +10005866 }
5867
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005868 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005869 *done = true;
5870 goto del_cursor;
5871 }
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005872 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5873 error = -EFSCORRUPTED;
5874 goto del_cursor;
5875 }
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005876
Christoph Hellwigbf806282017-10-19 11:07:34 -07005877 new_startoff = got.br_startoff - offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005878 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005879 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5880 error = -EINVAL;
5881 goto del_cursor;
5882 }
5883
Christoph Hellwigbf806282017-10-19 11:07:34 -07005884 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
Brian Foster0f37d172018-08-01 07:20:34 -07005885 error = xfs_bmse_merge(tp, ip, whichfork,
5886 offset_shift_fsb, &icur, &got, &prev,
5887 cur, &logflags);
Christoph Hellwigbf806282017-10-19 11:07:34 -07005888 if (error)
5889 goto del_cursor;
5890 goto done;
5891 }
5892 } else {
5893 if (got.br_startoff < offset_shift_fsb) {
5894 error = -EINVAL;
5895 goto del_cursor;
5896 }
5897 }
5898
Brian Foster0f37d172018-08-01 07:20:34 -07005899 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5900 cur, &logflags, new_startoff);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005901 if (error)
5902 goto del_cursor;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005903
Christoph Hellwig42630362017-11-03 10:34:41 -07005904done:
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005905 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5906 *done = true;
5907 goto del_cursor;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005908 }
5909
Christoph Hellwigbf806282017-10-19 11:07:34 -07005910 *next_fsb = got.br_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005911del_cursor:
5912 if (cur)
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07005913 xfs_btree_del_cursor(cur, error);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005914 if (logflags)
5915 xfs_trans_log_inode(tp, ip, logflags);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005916 return error;
5917}
5918
Darrick J. Wongf62cb482018-06-21 23:26:57 -07005919/* Make sure we won't be right-shifting an extent past the maximum bound. */
5920int
5921xfs_bmap_can_insert_extents(
5922 struct xfs_inode *ip,
5923 xfs_fileoff_t off,
5924 xfs_fileoff_t shift)
5925{
5926 struct xfs_bmbt_irec got;
5927 int is_empty;
5928 int error = 0;
5929
5930 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5931
5932 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5933 return -EIO;
5934
5935 xfs_ilock(ip, XFS_ILOCK_EXCL);
5936 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
5937 if (!error && !is_empty && got.br_startoff >= off &&
5938 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
5939 error = -EINVAL;
5940 xfs_iunlock(ip, XFS_ILOCK_EXCL);
5941
5942 return error;
5943}
5944
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005945int
5946xfs_bmap_insert_extents(
5947 struct xfs_trans *tp,
5948 struct xfs_inode *ip,
5949 xfs_fileoff_t *next_fsb,
5950 xfs_fileoff_t offset_shift_fsb,
5951 bool *done,
Brian Foster333f9502018-07-11 22:26:27 -07005952 xfs_fileoff_t stop_fsb)
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005953{
5954 int whichfork = XFS_DATA_FORK;
5955 struct xfs_mount *mp = ip->i_mount;
5956 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5957 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005958 struct xfs_bmbt_irec got, next;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005959 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005960 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005961 int error = 0;
5962 int logflags = 0;
5963
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005964 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
Darrick J. Wonga71895c2019-11-11 12:53:22 -08005965 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005966 return -EFSCORRUPTED;
5967 }
5968
5969 if (XFS_FORCED_SHUTDOWN(mp))
5970 return -EIO;
5971
5972 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5973
Christoph Hellwig862a8042021-04-13 11:15:09 -07005974 error = xfs_iread_extents(tp, ip, whichfork);
5975 if (error)
5976 return error;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005977
Christoph Hellwigac1e0672021-04-13 11:15:11 -07005978 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005979 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner92219c22020-03-10 17:52:53 -07005980 cur->bc_ino.flags = 0;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005981 }
5982
Namjae Jeona904b1c2015-03-25 15:08:56 +11005983 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005984 xfs_iext_last(ifp, &icur);
5985 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005986 stop_fsb > got.br_startoff) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005987 *done = true;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005988 goto del_cursor;
5989 }
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005990 } else {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005991 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005992 *done = true;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005993 goto del_cursor;
5994 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005995 }
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005996 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5997 error = -EFSCORRUPTED;
5998 goto del_cursor;
5999 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006000
Brian Fosterd0c22042019-12-11 13:18:38 -08006001 if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) {
Darrick J. Wongc2414ad2019-10-28 16:12:34 -07006002 error = -EFSCORRUPTED;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07006003 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006004 }
6005
Christoph Hellwigbf806282017-10-19 11:07:34 -07006006 new_startoff = got.br_startoff + offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006007 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07006008 if (new_startoff + got.br_blockcount > next.br_startoff) {
6009 error = -EINVAL;
6010 goto del_cursor;
6011 }
6012
6013 /*
6014 * Unlike a left shift (which involves a hole punch), a right
6015 * shift does not modify extent neighbors in any way. We should
6016 * never find mergeable extents in this scenario. Check anyways
6017 * and warn if we encounter two extents that could be one.
6018 */
6019 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
6020 WARN_ON_ONCE(1);
6021 }
6022
Brian Foster0f37d172018-08-01 07:20:34 -07006023 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
6024 cur, &logflags, new_startoff);
Christoph Hellwig6b18af02017-10-19 11:07:10 -07006025 if (error)
6026 goto del_cursor;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07006027
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006028 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07006029 stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07006030 *done = true;
Christoph Hellwig6b18af02017-10-19 11:07:10 -07006031 goto del_cursor;
6032 }
Christoph Hellwig6b18af02017-10-19 11:07:10 -07006033
6034 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006035del_cursor:
6036 if (cur)
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07006037 xfs_btree_del_cursor(cur, error);
Brian Fosterca446d82014-09-02 12:12:53 +10006038 if (logflags)
6039 xfs_trans_log_inode(tp, ip, logflags);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006040 return error;
6041}
Namjae Jeona904b1c2015-03-25 15:08:56 +11006042
6043/*
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006044 * Splits an extent into two extents at split_fsb block such that it is the
6045 * first block of the current_ext. @ext is a target extent to be split.
6046 * @split_fsb is a block where the extents is split. If split_fsb lies in a
6047 * hole or the first block of extents, just return 0.
Namjae Jeona904b1c2015-03-25 15:08:56 +11006048 */
Brian Fosterb73df172020-02-26 09:43:15 -08006049int
6050xfs_bmap_split_extent(
Namjae Jeona904b1c2015-03-25 15:08:56 +11006051 struct xfs_trans *tp,
6052 struct xfs_inode *ip,
Brian Foster4b77a082018-07-11 22:26:27 -07006053 xfs_fileoff_t split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006054{
6055 int whichfork = XFS_DATA_FORK;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07006056 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006057 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006058 struct xfs_bmbt_irec got;
6059 struct xfs_bmbt_irec new; /* split extent */
6060 struct xfs_mount *mp = ip->i_mount;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006061 xfs_fsblock_t gotblkcnt; /* new block count for got */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006062 struct xfs_iext_cursor icur;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006063 int error = 0;
6064 int logflags = 0;
6065 int i = 0;
6066
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07006067 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
Darrick J. Wonga71895c2019-11-11 12:53:22 -08006068 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006069 return -EFSCORRUPTED;
6070 }
6071
6072 if (XFS_FORCED_SHUTDOWN(mp))
6073 return -EIO;
6074
Christoph Hellwig862a8042021-04-13 11:15:09 -07006075 /* Read in all the extents */
6076 error = xfs_iread_extents(tp, ip, whichfork);
6077 if (error)
6078 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006079
6080 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006081 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11006082 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006083 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006084 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006085 return 0;
6086
6087 gotblkcnt = split_fsb - got.br_startoff;
6088 new.br_startoff = split_fsb;
6089 new.br_startblock = got.br_startblock + gotblkcnt;
6090 new.br_blockcount = got.br_blockcount - gotblkcnt;
6091 new.br_state = got.br_state;
6092
Christoph Hellwigac1e0672021-04-13 11:15:11 -07006093 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006094 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner92219c22020-03-10 17:52:53 -07006095 cur->bc_ino.flags = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07006096 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006097 if (error)
6098 goto del_cursor;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08006099 if (XFS_IS_CORRUPT(mp, i != 1)) {
6100 error = -EFSCORRUPTED;
6101 goto del_cursor;
6102 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006103 }
6104
Namjae Jeona904b1c2015-03-25 15:08:56 +11006105 got.br_blockcount = gotblkcnt;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006106 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
6107 &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006108
6109 logflags = XFS_ILOG_CORE;
6110 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07006111 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006112 if (error)
6113 goto del_cursor;
6114 } else
6115 logflags |= XFS_ILOG_DEXT;
6116
6117 /* Add new extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006118 xfs_iext_next(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07006119 xfs_iext_insert(ip, &icur, &new, 0);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07006120 ifp->if_nextents++;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006121
6122 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07006123 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006124 if (error)
6125 goto del_cursor;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08006126 if (XFS_IS_CORRUPT(mp, i != 0)) {
6127 error = -EFSCORRUPTED;
6128 goto del_cursor;
6129 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006130 error = xfs_btree_insert(cur, &i);
6131 if (error)
6132 goto del_cursor;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08006133 if (XFS_IS_CORRUPT(mp, i != 1)) {
6134 error = -EFSCORRUPTED;
6135 goto del_cursor;
6136 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006137 }
6138
6139 /*
6140 * Convert to a btree if necessary.
6141 */
6142 if (xfs_bmap_needs_btree(ip, whichfork)) {
6143 int tmp_logflags; /* partial log flag return val */
6144
6145 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07006146 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
6147 &tmp_logflags, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006148 logflags |= tmp_logflags;
6149 }
6150
6151del_cursor:
6152 if (cur) {
Dave Chinner92219c22020-03-10 17:52:53 -07006153 cur->bc_ino.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07006154 xfs_btree_del_cursor(cur, error);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006155 }
6156
6157 if (logflags)
6158 xfs_trans_log_inode(tp, ip, logflags);
6159 return error;
6160}
6161
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006162/* Deferred mapping is only for real extents in the data fork. */
6163static bool
6164xfs_bmap_is_update_needed(
6165 struct xfs_bmbt_irec *bmap)
6166{
6167 return bmap->br_startblock != HOLESTARTBLOCK &&
6168 bmap->br_startblock != DELAYSTARTBLOCK;
6169}
6170
6171/* Record a bmap intent. */
6172static int
6173__xfs_bmap_add(
Brian Foster0f37d172018-08-01 07:20:34 -07006174 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006175 enum xfs_bmap_intent_type type,
6176 struct xfs_inode *ip,
6177 int whichfork,
6178 struct xfs_bmbt_irec *bmap)
6179{
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006180 struct xfs_bmap_intent *bi;
6181
Brian Foster0f37d172018-08-01 07:20:34 -07006182 trace_xfs_bmap_defer(tp->t_mountp,
6183 XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock),
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006184 type,
Brian Foster0f37d172018-08-01 07:20:34 -07006185 XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock),
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006186 ip->i_ino, whichfork,
6187 bmap->br_startoff,
6188 bmap->br_blockcount,
6189 bmap->br_state);
6190
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07006191 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_NOFS);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006192 INIT_LIST_HEAD(&bi->bi_list);
6193 bi->bi_type = type;
6194 bi->bi_owner = ip;
6195 bi->bi_whichfork = whichfork;
6196 bi->bi_bmap = *bmap;
6197
Brian Foster0f37d172018-08-01 07:20:34 -07006198 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006199 return 0;
6200}
6201
6202/* Map an extent into a file. */
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006203void
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006204xfs_bmap_map_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07006205 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006206 struct xfs_inode *ip,
6207 struct xfs_bmbt_irec *PREV)
6208{
6209 if (!xfs_bmap_is_update_needed(PREV))
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006210 return;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006211
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006212 __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, XFS_DATA_FORK, PREV);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006213}
6214
6215/* Unmap an extent out of a file. */
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006216void
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006217xfs_bmap_unmap_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07006218 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006219 struct xfs_inode *ip,
6220 struct xfs_bmbt_irec *PREV)
6221{
6222 if (!xfs_bmap_is_update_needed(PREV))
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006223 return;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006224
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006225 __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, XFS_DATA_FORK, PREV);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006226}
6227
6228/*
6229 * Process one of the deferred bmap operations. We pass back the
6230 * btree cursor to maintain our lock on the bmapbt between calls.
6231 */
6232int
6233xfs_bmap_finish_one(
6234 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006235 struct xfs_inode *ip,
6236 enum xfs_bmap_intent_type type,
6237 int whichfork,
6238 xfs_fileoff_t startoff,
6239 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006240 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006241 xfs_exntst_t state)
6242{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006243 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006244
Brian Foster37283792018-07-11 22:26:23 -07006245 ASSERT(tp->t_firstblock == NULLFSBLOCK);
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006246
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006247 trace_xfs_bmap_deferred(tp->t_mountp,
6248 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6249 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006250 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006251
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006252 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006253 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006254
6255 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006256 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006257 return -EIO;
6258
6259 switch (type) {
6260 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006261 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Brian Fosterff3edf22018-07-11 22:26:14 -07006262 startblock, 0);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006263 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006264 break;
6265 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006266 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
Brian Foster2af52842018-07-11 22:26:25 -07006267 XFS_BMAPI_REMAP, 1);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006268 break;
6269 default:
6270 ASSERT(0);
6271 error = -EFSCORRUPTED;
6272 }
6273
6274 return error;
6275}
Darrick J. Wong30b09842018-03-23 10:06:52 -07006276
6277/* Check that an inode's extent does not have invalid flags or bad ranges. */
6278xfs_failaddr_t
6279xfs_bmap_validate_extent(
6280 struct xfs_inode *ip,
6281 int whichfork,
6282 struct xfs_bmbt_irec *irec)
6283{
6284 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wong30b09842018-03-23 10:06:52 -07006285
Darrick J. Wong33005fd2020-12-04 13:28:35 -08006286 if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
Darrick J. Wongacf104c2020-12-02 12:25:43 -08006287 return __this_address;
6288
Darrick J. Wong18695ad2020-12-04 13:24:22 -08006289 if (XFS_IS_REALTIME_INODE(ip) && whichfork == XFS_DATA_FORK) {
6290 if (!xfs_verify_rtext(mp, irec->br_startblock,
6291 irec->br_blockcount))
Darrick J. Wong30b09842018-03-23 10:06:52 -07006292 return __this_address;
6293 } else {
Darrick J. Wong67457eb2020-12-04 13:20:00 -08006294 if (!xfs_verify_fsbext(mp, irec->br_startblock,
6295 irec->br_blockcount))
Darrick J. Wong30b09842018-03-23 10:06:52 -07006296 return __this_address;
6297 }
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11006298 if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6299 return __this_address;
Darrick J. Wong30b09842018-03-23 10:06:52 -07006300 return NULL;
6301}