blob: 9c40d597103570d834450a9da177cd8cd4dd4635 [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 *
69 * Note that we can no longer assume that if we are in ATTR1 that
70 * the fork offset of all the inodes will be
71 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
72 * with ATTR2 and then mounted back with ATTR1, keeping the
73 * di_forkoff's fixed but probably at various positions. Therefore,
74 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
75 * of a minimum size available.
76 */
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
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110097STATIC int /* error */
98xfs_bmbt_lookup_eq(
99 struct xfs_btree_cur *cur,
Christoph Hellwige16cf9b2017-10-17 14:16:26 -0700100 struct xfs_bmbt_irec *irec,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100101 int *stat) /* success/failure */
102{
Christoph Hellwige16cf9b2017-10-17 14:16:26 -0700103 cur->bc_rec.b = *irec;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100104 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
105}
106
107STATIC int /* error */
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700108xfs_bmbt_lookup_first(
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100109 struct xfs_btree_cur *cur,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100110 int *stat) /* success/failure */
111{
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700112 cur->bc_rec.b.br_startoff = 0;
113 cur->bc_rec.b.br_startblock = 0;
114 cur->bc_rec.b.br_blockcount = 0;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100115 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
116}
117
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100118/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000119 * Check if the inode needs to be converted to btree format.
120 */
121static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
122{
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700123 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
124
Darrick J. Wong60b49842016-10-03 09:11:34 -0700125 return whichfork != XFS_COW_FORK &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700126 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700127 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000128}
129
130/*
131 * Check if the inode should be converted to extent format.
132 */
133static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
134{
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700135 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
136
Darrick J. Wong60b49842016-10-03 09:11:34 -0700137 return whichfork != XFS_COW_FORK &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700138 ifp->if_format == XFS_DINODE_FMT_BTREE &&
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700139 ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000140}
141
142/*
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700143 * Update the record referred to by cur to the value given by irec
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100144 * This either works (return 0) or gets an EFSCORRUPTED error.
145 */
146STATIC int
147xfs_bmbt_update(
148 struct xfs_btree_cur *cur,
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700149 struct xfs_bmbt_irec *irec)
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100150{
151 union xfs_btree_rec rec;
152
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700153 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100154 return xfs_btree_update(cur, &rec);
155}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/*
Dave Chinner9e5987a72013-02-25 12:31:26 +1100158 * Compute the worst-case number of indirect blocks that will be used
159 * for ip's delayed extent of length "len".
160 */
161STATIC xfs_filblks_t
162xfs_bmap_worst_indlen(
163 xfs_inode_t *ip, /* incore inode pointer */
164 xfs_filblks_t len) /* delayed extent length */
165{
166 int level; /* btree level number */
167 int maxrecs; /* maximum record count at this level */
168 xfs_mount_t *mp; /* mount structure */
169 xfs_filblks_t rval; /* return value */
170
171 mp = ip->i_mount;
172 maxrecs = mp->m_bmap_dmxr[0];
173 for (level = 0, rval = 0;
174 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
175 level++) {
176 len += maxrecs - 1;
177 do_div(len, maxrecs);
178 rval += len;
Darrick J. Wong5e5c9432017-09-18 09:41:17 -0700179 if (len == 1)
180 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
Dave Chinner9e5987a72013-02-25 12:31:26 +1100181 level - 1;
182 if (level == 0)
183 maxrecs = mp->m_bmap_dmxr[1];
184 }
185 return rval;
186}
187
188/*
189 * Calculate the default attribute fork offset for newly created inodes.
190 */
191uint
192xfs_default_attroffset(
193 struct xfs_inode *ip)
194{
195 struct xfs_mount *mp = ip->i_mount;
196 uint offset;
197
Christoph Hellwige9e2eae2020-03-18 08:15:10 -0700198 if (mp->m_sb.sb_inodesize == 256)
199 offset = XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
200 else
Dave Chinner9e5987a72013-02-25 12:31:26 +1100201 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100202
Christoph Hellwige9e2eae2020-03-18 08:15:10 -0700203 ASSERT(offset < XFS_LITINO(mp));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100204 return offset;
205}
206
207/*
208 * Helper routine to reset inode di_forkoff field when switching
209 * attribute fork from local to extent format - we reset it where
210 * possible to make space available for inline data fork extents.
211 */
212STATIC void
213xfs_bmap_forkoff_reset(
Dave Chinner9e5987a72013-02-25 12:31:26 +1100214 xfs_inode_t *ip,
215 int whichfork)
216{
217 if (whichfork == XFS_ATTR_FORK &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700218 ip->i_df.if_format != XFS_DINODE_FMT_DEV &&
219 ip->i_df.if_format != XFS_DINODE_FMT_BTREE) {
Dave Chinner9e5987a72013-02-25 12:31:26 +1100220 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
221
222 if (dfl_forkoff > ip->i_d.di_forkoff)
223 ip->i_d.di_forkoff = dfl_forkoff;
224 }
225}
226
Dave Chinner9e5987a72013-02-25 12:31:26 +1100227#ifdef DEBUG
228STATIC struct xfs_buf *
229xfs_bmap_get_bp(
230 struct xfs_btree_cur *cur,
231 xfs_fsblock_t bno)
232{
Dave Chinnere6631f82018-05-09 07:49:37 -0700233 struct xfs_log_item *lip;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100234 int i;
235
236 if (!cur)
237 return NULL;
238
239 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
240 if (!cur->bc_bufs[i])
241 break;
242 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
243 return cur->bc_bufs[i];
244 }
245
246 /* Chase down all the log items to see if the bp is there */
Dave Chinnere6631f82018-05-09 07:49:37 -0700247 list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
248 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
249
Dave Chinner9e5987a72013-02-25 12:31:26 +1100250 if (bip->bli_item.li_type == XFS_LI_BUF &&
251 XFS_BUF_ADDR(bip->bli_buf) == bno)
252 return bip->bli_buf;
253 }
254
255 return NULL;
256}
257
258STATIC void
259xfs_check_block(
260 struct xfs_btree_block *block,
261 xfs_mount_t *mp,
262 int root,
263 short sz)
264{
265 int i, j, dmxr;
266 __be64 *pp, *thispa; /* pointer to block address */
267 xfs_bmbt_key_t *prevp, *keyp;
268
269 ASSERT(be16_to_cpu(block->bb_level) > 0);
270
271 prevp = NULL;
272 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
273 dmxr = mp->m_bmap_dmxr[0];
274 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
275
276 if (prevp) {
277 ASSERT(be64_to_cpu(prevp->br_startoff) <
278 be64_to_cpu(keyp->br_startoff));
279 }
280 prevp = keyp;
281
282 /*
283 * Compare the block numbers to see if there are dups.
284 */
285 if (root)
286 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
287 else
288 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
289
290 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
291 if (root)
292 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
293 else
294 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
295 if (*thispa == *pp) {
296 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
297 __func__, j, i,
298 (unsigned long long)be64_to_cpu(*thispa));
Darrick J. Wongcec57252018-05-04 15:31:21 -0700299 xfs_err(mp, "%s: ptrs are equal in node\n",
Dave Chinner9e5987a72013-02-25 12:31:26 +1100300 __func__);
Darrick J. Wongcec57252018-05-04 15:31:21 -0700301 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100302 }
303 }
304 }
305}
306
307/*
308 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100309 * btree leaves. THis becomes prohibitively expensive for large extent count
310 * files, so don't bother with inodes that have more than 10,000 extents in
311 * them. The btree record ordering checks will still be done, so for such large
312 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100313 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100314STATIC void
315xfs_bmap_check_leaf_extents(
316 xfs_btree_cur_t *cur, /* btree cursor or null */
317 xfs_inode_t *ip, /* incore inode pointer */
318 int whichfork) /* data or attr fork */
319{
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700320 struct xfs_mount *mp = ip->i_mount;
321 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100322 struct xfs_btree_block *block; /* current btree block */
323 xfs_fsblock_t bno; /* block # of "block" */
324 xfs_buf_t *bp; /* buffer for "block" */
325 int error; /* error return value */
326 xfs_extnum_t i=0, j; /* index into the extents list */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100327 int level; /* btree level, for checking */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100328 __be64 *pp; /* pointer to block address */
329 xfs_bmbt_rec_t *ep; /* pointer to current extent */
330 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
331 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
332 int bp_release = 0;
333
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700334 if (ifp->if_format != XFS_DINODE_FMT_BTREE)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100335 return;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100336
Dave Chinnere3543812016-01-08 11:28:49 +1100337 /* skip large extent count inodes */
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700338 if (ip->i_df.if_nextents > 10000)
Dave Chinnere3543812016-01-08 11:28:49 +1100339 return;
340
Dave Chinner9e5987a72013-02-25 12:31:26 +1100341 bno = NULLFSBLOCK;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100342 block = ifp->if_broot;
343 /*
344 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
345 */
346 level = be16_to_cpu(block->bb_level);
347 ASSERT(level > 0);
348 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
349 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
350 bno = be64_to_cpu(*pp);
351
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000352 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100353 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
354 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
355
356 /*
357 * Go down the tree until leaf level is reached, following the first
358 * pointer (leftmost) at each level.
359 */
360 while (level-- > 0) {
361 /* See if buf is in cur first */
362 bp_release = 0;
363 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
364 if (!bp) {
365 bp_release = 1;
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700366 error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100367 XFS_BMAP_BTREE_REF,
368 &xfs_bmbt_buf_ops);
369 if (error)
370 goto error_norelse;
371 }
372 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100373 if (level == 0)
374 break;
375
376 /*
377 * Check this block for basic sanity (increasing keys and
378 * no duplicate blocks).
379 */
380
381 xfs_check_block(block, mp, 0, 0);
382 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
383 bno = be64_to_cpu(*pp);
Darrick J. Wongf9e03702019-11-11 12:52:18 -0800384 if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbno(mp, bno))) {
385 error = -EFSCORRUPTED;
386 goto error0;
387 }
Dave Chinner9e5987a72013-02-25 12:31:26 +1100388 if (bp_release) {
389 bp_release = 0;
390 xfs_trans_brelse(NULL, bp);
391 }
392 }
393
394 /*
395 * Here with bp and block set to the leftmost leaf node in the tree.
396 */
397 i = 0;
398
399 /*
400 * Loop over all leaf nodes checking that all extents are in the right order.
401 */
402 for (;;) {
403 xfs_fsblock_t nextbno;
404 xfs_extnum_t num_recs;
405
406
407 num_recs = xfs_btree_get_numrecs(block);
408
409 /*
410 * Read-ahead the next leaf block, if any.
411 */
412
413 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
414
415 /*
416 * Check all the extents to make sure they are OK.
417 * If we had a previous block, the last entry should
418 * conform with the first entry in this one.
419 */
420
421 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
422 if (i) {
423 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
424 xfs_bmbt_disk_get_blockcount(&last) <=
425 xfs_bmbt_disk_get_startoff(ep));
426 }
427 for (j = 1; j < num_recs; j++) {
428 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
429 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
430 xfs_bmbt_disk_get_blockcount(ep) <=
431 xfs_bmbt_disk_get_startoff(nextp));
432 ep = nextp;
433 }
434
435 last = *ep;
436 i += num_recs;
437 if (bp_release) {
438 bp_release = 0;
439 xfs_trans_brelse(NULL, bp);
440 }
441 bno = nextbno;
442 /*
443 * If we've reached the end, stop.
444 */
445 if (bno == NULLFSBLOCK)
446 break;
447
448 bp_release = 0;
449 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
450 if (!bp) {
451 bp_release = 1;
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700452 error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100453 XFS_BMAP_BTREE_REF,
454 &xfs_bmbt_buf_ops);
455 if (error)
456 goto error_norelse;
457 }
458 block = XFS_BUF_TO_BLOCK(bp);
459 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100460
Dave Chinner9e5987a72013-02-25 12:31:26 +1100461 return;
462
463error0:
464 xfs_warn(mp, "%s: at error0", __func__);
465 if (bp_release)
466 xfs_trans_brelse(NULL, bp);
467error_norelse:
468 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
469 __func__, i);
Darrick J. Wongcec57252018-05-04 15:31:21 -0700470 xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
471 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100472 return;
473}
474
475/*
Dave Chinner9e5987a72013-02-25 12:31:26 +1100476 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000477 * given the caller's original parameters. Specifically check the
478 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a72013-02-25 12:31:26 +1100479 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
480 */
481STATIC void
482xfs_bmap_validate_ret(
483 xfs_fileoff_t bno,
484 xfs_filblks_t len,
485 int flags,
486 xfs_bmbt_irec_t *mval,
487 int nmap,
488 int ret_nmap)
489{
490 int i; /* index to map values */
491
492 ASSERT(ret_nmap <= nmap);
493
494 for (i = 0; i < ret_nmap; i++) {
495 ASSERT(mval[i].br_blockcount > 0);
496 if (!(flags & XFS_BMAPI_ENTIRE)) {
497 ASSERT(mval[i].br_startoff >= bno);
498 ASSERT(mval[i].br_blockcount <= len);
499 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
500 bno + len);
501 } else {
502 ASSERT(mval[i].br_startoff < bno + len);
503 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
504 bno);
505 }
506 ASSERT(i == 0 ||
507 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
508 mval[i].br_startoff);
509 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
510 mval[i].br_startblock != HOLESTARTBLOCK);
511 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
512 mval[i].br_state == XFS_EXT_UNWRITTEN);
513 }
514}
515
516#else
517#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
Darrick J. Wong7bf7a192017-08-31 15:11:06 -0700518#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100519#endif /* DEBUG */
520
521/*
522 * bmap free list manipulation functions
523 */
524
525/*
526 * Add the extent to the list of extents to be free at transaction end.
527 * The list is maintained sorted (by block number).
528 */
529void
Brian Fosterfcb762f2018-05-09 08:45:04 -0700530__xfs_bmap_add_free(
Brian Foster0f37d172018-08-01 07:20:34 -0700531 struct xfs_trans *tp,
Darrick J. Wong340785c2016-08-03 11:33:42 +1000532 xfs_fsblock_t bno,
533 xfs_filblks_t len,
Darrick J. Wong66e32372018-12-12 08:46:23 -0800534 const struct xfs_owner_info *oinfo,
Brian Fosterfcb762f2018-05-09 08:45:04 -0700535 bool skip_discard)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100536{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000537 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100538#ifdef DEBUG
Brian Foster0f37d172018-08-01 07:20:34 -0700539 struct xfs_mount *mp = tp->t_mountp;
540 xfs_agnumber_t agno;
541 xfs_agblock_t agbno;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100542
543 ASSERT(bno != NULLFSBLOCK);
544 ASSERT(len > 0);
545 ASSERT(len <= MAXEXTLEN);
546 ASSERT(!isnullstartblock(bno));
547 agno = XFS_FSB_TO_AGNO(mp, bno);
548 agbno = XFS_FSB_TO_AGBNO(mp, bno);
549 ASSERT(agno < mp->m_sb.sb_agcount);
550 ASSERT(agbno < mp->m_sb.sb_agblocks);
551 ASSERT(len < mp->m_sb.sb_agblocks);
552 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
553#endif
554 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000555
Carlos Maiolino3050bd02020-07-22 09:23:04 -0700556 new = kmem_cache_alloc(xfs_bmap_free_item_zone,
557 GFP_KERNEL | __GFP_NOFAIL);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000558 new->xefi_startblock = bno;
559 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000560 if (oinfo)
561 new->xefi_oinfo = *oinfo;
562 else
Darrick J. Wong7280fed2018-12-12 08:46:23 -0800563 new->xefi_oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
Brian Fosterfcb762f2018-05-09 08:45:04 -0700564 new->xefi_skip_discard = skip_discard;
Brian Foster0f37d172018-08-01 07:20:34 -0700565 trace_xfs_bmap_free_defer(tp->t_mountp,
566 XFS_FSB_TO_AGNO(tp->t_mountp, bno), 0,
567 XFS_FSB_TO_AGBNO(tp->t_mountp, bno), len);
568 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100569}
570
571/*
572 * Inode fork format manipulation functions
573 */
574
575/*
Christoph Hellwigb101e332019-02-15 08:02:47 -0800576 * Convert the inode format to extent format if it currently is in btree format,
577 * but the extent list is small enough that it fits into the extent format.
578 *
579 * Since the extents are already in-core, all we have to do is give up the space
580 * for the btree root and pitch the leaf block.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100581 */
582STATIC int /* error */
583xfs_bmap_btree_to_extents(
Christoph Hellwigb101e332019-02-15 08:02:47 -0800584 struct xfs_trans *tp, /* transaction pointer */
585 struct xfs_inode *ip, /* incore inode pointer */
586 struct xfs_btree_cur *cur, /* btree cursor */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100587 int *logflagsp, /* inode logging flags */
588 int whichfork) /* data or attr fork */
589{
Christoph Hellwigb101e332019-02-15 08:02:47 -0800590 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
591 struct xfs_mount *mp = ip->i_mount;
592 struct xfs_btree_block *rblock = ifp->if_broot;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100593 struct xfs_btree_block *cblock;/* child btree block */
594 xfs_fsblock_t cbno; /* child block number */
595 xfs_buf_t *cbp; /* child block's buffer */
596 int error; /* error return value */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100597 __be64 *pp; /* ptr to block address */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000598 struct xfs_owner_info oinfo;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100599
Christoph Hellwigb101e332019-02-15 08:02:47 -0800600 /* check if we actually need the extent format first: */
601 if (!xfs_bmap_wants_extents(ip, whichfork))
602 return 0;
603
604 ASSERT(cur);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700605 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100606 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700607 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100608 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
609 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
610 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
Christoph Hellwigb101e332019-02-15 08:02:47 -0800611
Dave Chinner9e5987a72013-02-25 12:31:26 +1100612 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
613 cbno = be64_to_cpu(*pp);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100614#ifdef DEBUG
Darrick J. Wongf9e03702019-11-11 12:52:18 -0800615 if (XFS_IS_CORRUPT(cur->bc_mp, !xfs_btree_check_lptr(cur, cbno, 1)))
616 return -EFSCORRUPTED;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100617#endif
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700618 error = xfs_btree_read_bufl(mp, tp, cbno, &cbp, XFS_BMAP_BTREE_REF,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100619 &xfs_bmbt_buf_ops);
620 if (error)
621 return error;
622 cblock = XFS_BUF_TO_BLOCK(cbp);
623 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
624 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000625 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
Brian Foster0f37d172018-08-01 07:20:34 -0700626 xfs_bmap_add_free(cur->bc_tp, cbno, 1, &oinfo);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100627 ip->i_d.di_nblocks--;
628 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
629 xfs_trans_binval(tp, cbp);
630 if (cur->bc_bufs[0] == cbp)
631 cur->bc_bufs[0] = NULL;
632 xfs_iroot_realloc(ip, -1, whichfork);
633 ASSERT(ifp->if_broot == NULL);
634 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
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);
678 ifp->if_flags |= XFS_IFBROOT;
679
680 /*
681 * Fill in the root.
682 */
683 block = ifp->if_broot;
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800684 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
685 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
Eric Sandeenf88ae462017-01-27 23:16:37 -0800686 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100687 /*
688 * Need a cursor. Can't allocate until bb_level is filled in.
689 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100690 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner8ef54792020-03-10 17:54:38 -0700691 cur->bc_ino.flags = wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100692 /*
693 * Convert to a btree with two levels, one record in root.
694 */
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700695 ifp->if_format = XFS_DINODE_FMT_BTREE;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100696 memset(&args, 0, sizeof(args));
697 args.tp = tp;
698 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000699 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Brian Foster280253d2018-07-11 22:26:29 -0700700 if (tp->t_firstblock == NULLFSBLOCK) {
Dave Chinner9e5987a72013-02-25 12:31:26 +1100701 args.type = XFS_ALLOCTYPE_START_BNO;
702 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Brian Foster1214f1c2018-08-01 07:20:31 -0700703 } else if (tp->t_flags & XFS_TRANS_LOWMODE) {
Dave Chinner9e5987a72013-02-25 12:31:26 +1100704 args.type = XFS_ALLOCTYPE_START_BNO;
Brian Foster280253d2018-07-11 22:26:29 -0700705 args.fsbno = tp->t_firstblock;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100706 } else {
707 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Brian Foster280253d2018-07-11 22:26:29 -0700708 args.fsbno = tp->t_firstblock;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100709 }
710 args.minlen = args.maxlen = args.prod = 1;
711 args.wasdel = wasdel;
712 *logflagsp = 0;
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000713 error = xfs_alloc_vextent(&args);
714 if (error)
715 goto out_root_realloc;
Darrick J. Wong90e20562016-10-03 09:11:45 -0700716
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800717 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
Shan Hai01239d772018-08-10 17:55:55 -0700718 error = -ENOSPC;
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000719 goto out_root_realloc;
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800720 }
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000721
Dave Chinner9e5987a72013-02-25 12:31:26 +1100722 /*
723 * Allocation can't fail, the space was reserved.
724 */
Brian Foster280253d2018-07-11 22:26:29 -0700725 ASSERT(tp->t_firstblock == NULLFSBLOCK ||
726 args.agno >= XFS_FSB_TO_AGNO(mp, tp->t_firstblock));
Brian Fostercf612de2018-07-11 22:26:29 -0700727 tp->t_firstblock = args.fsbno;
Dave Chinner92219c22020-03-10 17:52:53 -0700728 cur->bc_ino.allocated++;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100729 ip->i_d.di_nblocks++;
730 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
Darrick J. Wongee647f82020-01-23 17:01:19 -0800731 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
732 XFS_FSB_TO_DADDR(mp, args.fsbno),
733 mp->m_bsize, 0, &abp);
734 if (error)
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000735 goto out_unreserve_dquot;
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000736
Dave Chinner9e5987a72013-02-25 12:31:26 +1100737 /*
738 * Fill in the child block.
739 */
740 abp->b_ops = &xfs_bmbt_buf_ops;
741 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800742 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
743 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500744 XFS_BTREE_LONG_PTRS);
745
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700746 for_each_xfs_iext(ifp, &icur, &rec) {
Christoph Hellwig906abed2017-11-03 10:34:43 -0700747 if (isnullstartblock(rec.br_startblock))
748 continue;
749 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
750 xfs_bmbt_disk_set_all(arp, &rec);
751 cnt++;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100752 }
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700753 ASSERT(cnt == ifp->if_nextents);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100754 xfs_btree_set_numrecs(ablock, cnt);
755
756 /*
757 * Fill in the root key and pointer.
758 */
759 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
760 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
761 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
762 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
763 be16_to_cpu(block->bb_level)));
764 *pp = cpu_to_be64(args.fsbno);
765
766 /*
767 * Do all this logging at the end so that
768 * the root is at the right level.
769 */
770 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
771 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
772 ASSERT(*curp == NULL);
773 *curp = cur;
774 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
775 return 0;
Shan Hai01239d772018-08-10 17:55:55 -0700776
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000777out_unreserve_dquot:
Shan Hai01239d772018-08-10 17:55:55 -0700778 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000779out_root_realloc:
Shan Hai01239d772018-08-10 17:55:55 -0700780 xfs_iroot_realloc(ip, -1, whichfork);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700781 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000782 ASSERT(ifp->if_broot == NULL);
Shan Hai01239d772018-08-10 17:55:55 -0700783 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
784
785 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100786}
787
788/*
789 * Convert a local file to an extents file.
790 * This code is out of bounds for data forks of regular files,
791 * since the file data needs to get logged so things will stay consistent.
792 * (The bmap-level manipulations are ok, though).
793 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000794void
795xfs_bmap_local_to_extents_empty(
Brian Fosteraeea4b72019-10-07 12:54:16 -0700796 struct xfs_trans *tp,
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000797 struct xfs_inode *ip,
798 int whichfork)
799{
800 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
801
Darrick J. Wong60b49842016-10-03 09:11:34 -0700802 ASSERT(whichfork != XFS_COW_FORK);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700803 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000804 ASSERT(ifp->if_bytes == 0);
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700805 ASSERT(ifp->if_nextents == 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000806
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000807 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000808 ifp->if_flags &= ~XFS_IFINLINE;
809 ifp->if_flags |= XFS_IFEXTENTS;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700810 ifp->if_u1.if_root = NULL;
811 ifp->if_height = 0;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700812 ifp->if_format = XFS_DINODE_FMT_EXTENTS;
Brian Fosteraeea4b72019-10-07 12:54:16 -0700813 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000814}
815
816
Dave Chinner9e5987a72013-02-25 12:31:26 +1100817STATIC int /* error */
818xfs_bmap_local_to_extents(
819 xfs_trans_t *tp, /* transaction pointer */
820 xfs_inode_t *ip, /* incore inode pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100821 xfs_extlen_t total, /* total blocks needed by transaction */
822 int *logflagsp, /* inode logging flags */
823 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500824 void (*init_fn)(struct xfs_trans *tp,
825 struct xfs_buf *bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100826 struct xfs_inode *ip,
827 struct xfs_ifork *ifp))
828{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000829 int error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100830 int flags; /* logging flags returned */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -0700831 struct xfs_ifork *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000832 xfs_alloc_arg_t args; /* allocation arguments */
833 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700834 struct xfs_bmbt_irec rec;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700835 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100836
837 /*
838 * We don't want to deal with the case of keeping inode data inline yet.
839 * So sending the data fork of a regular inode is invalid.
840 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100841 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100842 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700843 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000844
845 if (!ifp->if_bytes) {
Brian Fosteraeea4b72019-10-07 12:54:16 -0700846 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000847 flags = XFS_ILOG_CORE;
848 goto done;
849 }
850
Dave Chinner9e5987a72013-02-25 12:31:26 +1100851 flags = 0;
852 error = 0;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700853 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000854 memset(&args, 0, sizeof(args));
855 args.tp = tp;
856 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000857 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000858 /*
859 * Allocate a block. We know we need only one, since the
860 * file currently fits in an inode.
861 */
Brian Foster280253d2018-07-11 22:26:29 -0700862 if (tp->t_firstblock == NULLFSBLOCK) {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000863 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
864 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100865 } else {
Brian Foster280253d2018-07-11 22:26:29 -0700866 args.fsbno = tp->t_firstblock;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000867 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100868 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000869 args.total = total;
870 args.minlen = args.maxlen = args.prod = 1;
871 error = xfs_alloc_vextent(&args);
872 if (error)
873 goto done;
874
875 /* Can't fail, the space was reserved. */
876 ASSERT(args.fsbno != NULLFSBLOCK);
877 ASSERT(args.len == 1);
Brian Foster280253d2018-07-11 22:26:29 -0700878 tp->t_firstblock = args.fsbno;
Darrick J. Wongee647f82020-01-23 17:01:19 -0800879 error = xfs_trans_get_buf(tp, args.mp->m_ddev_targp,
880 XFS_FSB_TO_DADDR(args.mp, args.fsbno),
881 args.mp->m_bsize, 0, &bp);
882 if (error)
883 goto done;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000884
Dave Chinnerfe22d552015-01-22 09:30:06 +1100885 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100886 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100887 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100888 * The callout is responsible for logging because the remote format
889 * might differ from the local format and thus we don't know how much to
890 * log here. Note that init_fn must also set the buffer log item type
891 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100892 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000893 init_fn(tp, bp, ip, ifp);
894
Brian Fosterb7cdc662015-10-12 15:40:24 +1100895 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000896 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
Brian Fosteraeea4b72019-10-07 12:54:16 -0700897 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100898 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000899
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700900 ifp->if_u1.if_root = NULL;
901 ifp->if_height = 0;
902
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700903 rec.br_startoff = 0;
904 rec.br_startblock = args.fsbno;
905 rec.br_blockcount = 1;
906 rec.br_state = XFS_EXT_NORM;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700907 xfs_iext_first(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -0700908 xfs_iext_insert(ip, &icur, &rec, 0);
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700909
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700910 ifp->if_nextents = 1;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000911 ip->i_d.di_nblocks = 1;
912 xfs_trans_mod_dquot_byino(tp, ip,
913 XFS_TRANS_DQ_BCOUNT, 1L);
914 flags |= xfs_ilog_fext(whichfork);
915
Dave Chinner9e5987a72013-02-25 12:31:26 +1100916done:
917 *logflagsp = flags;
918 return error;
919}
920
921/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 * Called from xfs_bmap_add_attrfork to handle btree format files.
923 */
924STATIC int /* error */
925xfs_bmap_add_attrfork_btree(
926 xfs_trans_t *tp, /* transaction pointer */
927 xfs_inode_t *ip, /* incore inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 int *flags) /* inode logging flags */
929{
930 xfs_btree_cur_t *cur; /* btree cursor */
931 int error; /* error return value */
932 xfs_mount_t *mp; /* file system mount struct */
933 int stat; /* newroot status */
934
935 mp = ip->i_mount;
936 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
937 *flags |= XFS_ILOG_DBROOT;
938 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100939 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700940 error = xfs_bmbt_lookup_first(cur, &stat);
941 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000943 /* must be at least one entry */
Darrick J. Wongf9e03702019-11-11 12:52:18 -0800944 if (XFS_IS_CORRUPT(mp, stat != 1)) {
945 error = -EFSCORRUPTED;
946 goto error0;
947 }
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100948 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 goto error0;
950 if (stat == 0) {
951 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000952 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
Dave Chinner92219c22020-03-10 17:52:53 -0700954 cur->bc_ino.allocated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
956 }
957 return 0;
958error0:
959 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
960 return error;
961}
962
963/*
964 * Called from xfs_bmap_add_attrfork to handle extents format files.
965 */
966STATIC int /* error */
967xfs_bmap_add_attrfork_extents(
Brian Foster81ba8f32018-07-11 22:26:16 -0700968 struct xfs_trans *tp, /* transaction pointer */
969 struct xfs_inode *ip, /* incore inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 int *flags) /* inode logging flags */
971{
972 xfs_btree_cur_t *cur; /* bmap btree cursor */
973 int error; /* error return value */
974
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700975 if (ip->i_df.if_nextents * sizeof(struct xfs_bmbt_rec) <=
976 XFS_IFORK_DSIZE(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return 0;
978 cur = NULL;
Brian Foster280253d2018-07-11 22:26:29 -0700979 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
980 XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (cur) {
Dave Chinner92219c22020-03-10 17:52:53 -0700982 cur->bc_ino.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -0700983 xfs_btree_del_cursor(cur, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985 return error;
986}
987
988/*
Dave Chinner1e823792013-02-11 15:58:13 +1100989 * Called from xfs_bmap_add_attrfork to handle local format files. Each
990 * different data fork content type needs a different callout to do the
991 * conversion. Some are basic and only require special block initialisation
992 * callouts for the data formating, others (directories) are so specialised they
993 * handle everything themselves.
994 *
995 * XXX (dgc): investigate whether directory conversion can use the generic
996 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500997 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 */
999STATIC int /* error */
1000xfs_bmap_add_attrfork_local(
Brian Foster825d75cd2018-07-11 22:26:21 -07001001 struct xfs_trans *tp, /* transaction pointer */
1002 struct xfs_inode *ip, /* incore inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 int *flags) /* inode logging flags */
1004{
Brian Foster825d75cd2018-07-11 22:26:21 -07001005 struct xfs_da_args dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1008 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001009
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001010 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001012 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 dargs.dp = ip;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001014 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 dargs.whichfork = XFS_DATA_FORK;
1016 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001017 return xfs_dir2_sf_to_block(&dargs);
1018 }
1019
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001020 if (S_ISLNK(VFS_I(ip)->i_mode))
Brian Foster280253d2018-07-11 22:26:29 -07001021 return xfs_bmap_local_to_extents(tp, ip, 1, flags,
1022 XFS_DATA_FORK,
Dave Chinner1e823792013-02-11 15:58:13 +11001023 xfs_symlink_local_to_remote);
1024
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001025 /* should only be called for types that support local format data */
1026 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001027 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
Allison Henderson2f3cd802018-10-18 17:21:16 +11001030/* Set an inode attr fork off based on the format */
1031int
1032xfs_bmap_set_attrforkoff(
1033 struct xfs_inode *ip,
1034 int size,
1035 int *version)
1036{
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001037 switch (ip->i_df.if_format) {
Allison Henderson2f3cd802018-10-18 17:21:16 +11001038 case XFS_DINODE_FMT_DEV:
1039 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1040 break;
1041 case XFS_DINODE_FMT_LOCAL:
1042 case XFS_DINODE_FMT_EXTENTS:
1043 case XFS_DINODE_FMT_BTREE:
1044 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1045 if (!ip->i_d.di_forkoff)
1046 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1047 else if ((ip->i_mount->m_flags & XFS_MOUNT_ATTR2) && version)
1048 *version = 2;
1049 break;
1050 default:
1051 ASSERT(0);
1052 return -EINVAL;
1053 }
1054
1055 return 0;
1056}
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001059 * Convert inode from non-attributed to attributed.
1060 * Must not be in a transaction, ip must not be locked.
1061 */
1062int /* error code */
1063xfs_bmap_add_attrfork(
1064 xfs_inode_t *ip, /* incore inode pointer */
1065 int size, /* space new attribute needs */
1066 int rsvd) /* xact may use reserved blks */
1067{
Dave Chinner9e5987a72013-02-25 12:31:26 +11001068 xfs_mount_t *mp; /* mount structure */
1069 xfs_trans_t *tp; /* transaction pointer */
1070 int blks; /* space reservation */
1071 int version = 1; /* superblock attr version */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001072 int logflags; /* logging flags */
1073 int error; /* error return value */
1074
1075 ASSERT(XFS_IFORK_Q(ip) == 0);
1076
1077 mp = ip->i_mount;
1078 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001079
Dave Chinner9e5987a72013-02-25 12:31:26 +11001080 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001081
1082 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1083 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1084 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001085 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001086
Dave Chinner9e5987a72013-02-25 12:31:26 +11001087 xfs_ilock(ip, XFS_ILOCK_EXCL);
1088 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1089 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1090 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001091 if (error)
1092 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001093 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001094 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001095
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001096 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001097 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Allison Henderson2f3cd802018-10-18 17:21:16 +11001098 error = xfs_bmap_set_attrforkoff(ip, size, &version);
1099 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001100 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001101 ASSERT(ip->i_afp == NULL);
Carlos Maiolino32a2b112020-07-22 09:23:10 -07001102
1103 ip->i_afp = kmem_cache_zalloc(xfs_ifork_zone,
1104 GFP_KERNEL | __GFP_NOFAIL);
1105
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001106 ip->i_afp->if_format = XFS_DINODE_FMT_EXTENTS;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001107 ip->i_afp->if_flags = XFS_IFEXTENTS;
1108 logflags = 0;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001109 switch (ip->i_df.if_format) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001110 case XFS_DINODE_FMT_LOCAL:
Brian Foster825d75cd2018-07-11 22:26:21 -07001111 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001112 break;
1113 case XFS_DINODE_FMT_EXTENTS:
Brian Foster825d75cd2018-07-11 22:26:21 -07001114 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001115 break;
1116 case XFS_DINODE_FMT_BTREE:
Brian Foster825d75cd2018-07-11 22:26:21 -07001117 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001118 break;
1119 default:
1120 error = 0;
1121 break;
1122 }
1123 if (logflags)
1124 xfs_trans_log_inode(tp, ip, logflags);
1125 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07001126 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001127 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1128 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001129 bool log_sb = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001130
1131 spin_lock(&mp->m_sb_lock);
1132 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1133 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001134 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001135 }
1136 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1137 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001138 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001139 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001140 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001141 if (log_sb)
1142 xfs_log_sb(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001143 }
1144
Christoph Hellwig70393312015-06-04 13:48:08 +10001145 error = xfs_trans_commit(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001146 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001147 return error;
1148
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001149trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001150 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001151 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001152 return error;
1153}
1154
1155/*
1156 * Internal and external extent tree search functions.
1157 */
1158
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001159struct xfs_iread_state {
1160 struct xfs_iext_cursor icur;
1161 xfs_extnum_t loaded;
1162};
1163
1164/* Stuff every bmbt record from this block into the incore extent map. */
1165static int
1166xfs_iread_bmbt_block(
1167 struct xfs_btree_cur *cur,
1168 int level,
1169 void *priv)
1170{
1171 struct xfs_iread_state *ir = priv;
1172 struct xfs_mount *mp = cur->bc_mp;
Dave Chinner92219c22020-03-10 17:52:53 -07001173 struct xfs_inode *ip = cur->bc_ino.ip;
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001174 struct xfs_btree_block *block;
1175 struct xfs_buf *bp;
1176 struct xfs_bmbt_rec *frp;
1177 xfs_extnum_t num_recs;
1178 xfs_extnum_t j;
Dave Chinner92219c22020-03-10 17:52:53 -07001179 int whichfork = cur->bc_ino.whichfork;
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001180 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001181
1182 block = xfs_btree_get_block(cur, level, &bp);
1183
1184 /* Abort if we find more records than nextents. */
1185 num_recs = xfs_btree_get_numrecs(block);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001186 if (unlikely(ir->loaded + num_recs > ifp->if_nextents)) {
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001187 xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).",
1188 (unsigned long long)ip->i_ino);
1189 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block,
1190 sizeof(*block), __this_address);
1191 return -EFSCORRUPTED;
1192 }
1193
1194 /* Copy records into the incore cache. */
1195 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1196 for (j = 0; j < num_recs; j++, frp++, ir->loaded++) {
1197 struct xfs_bmbt_irec new;
1198 xfs_failaddr_t fa;
1199
1200 xfs_bmbt_disk_get_all(frp, &new);
1201 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1202 if (fa) {
1203 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1204 "xfs_iread_extents(2)", frp,
1205 sizeof(*frp), fa);
1206 return -EFSCORRUPTED;
1207 }
1208 xfs_iext_insert(ip, &ir->icur, &new,
1209 xfs_bmap_fork_to_state(whichfork));
1210 trace_xfs_read_extent(ip, &ir->icur,
1211 xfs_bmap_fork_to_state(whichfork), _THIS_IP_);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001212 xfs_iext_next(ifp, &ir->icur);
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001213 }
1214
1215 return 0;
1216}
1217
Dave Chinner9e5987a72013-02-25 12:31:26 +11001218/*
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001219 * Read in extents from a btree-format inode.
Dave Chinner9e5987a72013-02-25 12:31:26 +11001220 */
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001221int
1222xfs_iread_extents(
1223 struct xfs_trans *tp,
1224 struct xfs_inode *ip,
1225 int whichfork)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001226{
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001227 struct xfs_iread_state ir;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001228 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001229 struct xfs_mount *mp = ip->i_mount;
1230 struct xfs_btree_cur *cur;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001231 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001232
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001233 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1234
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001235 if (XFS_IS_CORRUPT(mp, ifp->if_format != XFS_DINODE_FMT_BTREE)) {
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001236 error = -EFSCORRUPTED;
1237 goto out;
1238 }
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001239
1240 ir.loaded = 0;
1241 xfs_iext_first(ifp, &ir.icur);
1242 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1243 error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block,
1244 XFS_BTREE_VISIT_RECORDS, &ir);
1245 xfs_btree_del_cursor(cur, error);
1246 if (error)
1247 goto out;
1248
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001249 if (XFS_IS_CORRUPT(mp, ir.loaded != ifp->if_nextents)) {
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001250 error = -EFSCORRUPTED;
1251 goto out;
1252 }
1253 ASSERT(ir.loaded == xfs_iext_count(ifp));
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001254
1255 ifp->if_flags |= XFS_IFEXTENTS;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001256 return 0;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001257out:
1258 xfs_iext_destroy(ifp);
1259 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001260}
1261
Dave Chinner9e5987a72013-02-25 12:31:26 +11001262/*
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001263 * Returns the relative block number of the first unused block(s) in the given
1264 * fork with at least "len" logically contiguous blocks free. This is the
1265 * lowest-address hole if the fork has holes, else the first block past the end
1266 * of fork. Return 0 if the fork is currently local (in-inode).
Dave Chinner9e5987a72013-02-25 12:31:26 +11001267 */
1268int /* error */
1269xfs_bmap_first_unused(
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001270 struct xfs_trans *tp, /* transaction pointer */
1271 struct xfs_inode *ip, /* incore inode */
1272 xfs_extlen_t len, /* size of hole to find */
1273 xfs_fileoff_t *first_unused, /* unused block */
1274 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001275{
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001276 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1277 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001278 struct xfs_iext_cursor icur;
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001279 xfs_fileoff_t lastaddr = 0;
1280 xfs_fileoff_t lowest, max;
1281 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001282
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001283 if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001284 *first_unused = 0;
1285 return 0;
1286 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001287
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001288 ASSERT(xfs_ifork_has_extents(ifp));
1289
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001290 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1291 error = xfs_iread_extents(tp, ip, whichfork);
1292 if (error)
1293 return error;
1294 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001295
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001296 lowest = max = *first_unused;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001297 for_each_xfs_iext(ifp, &icur, &got) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001298 /*
1299 * See if the hole before this extent will work.
1300 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001301 if (got.br_startoff >= lowest + len &&
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001302 got.br_startoff - max >= len)
1303 break;
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001304 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001305 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1306 }
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001307
Dave Chinner9e5987a72013-02-25 12:31:26 +11001308 *first_unused = max;
1309 return 0;
1310}
1311
1312/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001313 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a72013-02-25 12:31:26 +11001314 * last_block (input value) in the file.
1315 * This is not based on i_size, it is based on the extent records.
1316 * Returns 0 for local files, as they do not have extent records.
1317 */
1318int /* error */
1319xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001320 struct xfs_trans *tp, /* transaction pointer */
1321 struct xfs_inode *ip, /* incore inode */
1322 xfs_fileoff_t *last_block, /* last block */
1323 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001324{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001325 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1326 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001327 struct xfs_iext_cursor icur;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001328 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001329
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001330 switch (ifp->if_format) {
Christoph Hellwig86685f72016-11-24 11:39:38 +11001331 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001332 *last_block = 0;
1333 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001334 case XFS_DINODE_FMT_BTREE:
1335 case XFS_DINODE_FMT_EXTENTS:
1336 break;
1337 default:
Darrick J. Wonga5155b82019-11-02 09:40:53 -07001338 ASSERT(0);
Darrick J. Wongc2414ad2019-10-28 16:12:34 -07001339 return -EFSCORRUPTED;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001340 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001341
1342 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1343 error = xfs_iread_extents(tp, ip, whichfork);
1344 if (error)
1345 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001346 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001347
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001348 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
Christoph Hellwigdc560152017-10-23 16:32:39 -07001349 *last_block = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001350 return 0;
1351}
1352
Dave Chinner68988112013-08-12 20:49:42 +10001353int
Dave Chinner9e5987a72013-02-25 12:31:26 +11001354xfs_bmap_last_extent(
1355 struct xfs_trans *tp,
1356 struct xfs_inode *ip,
1357 int whichfork,
1358 struct xfs_bmbt_irec *rec,
1359 int *is_empty)
1360{
1361 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001362 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001363 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001364
1365 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1366 error = xfs_iread_extents(tp, ip, whichfork);
1367 if (error)
1368 return error;
1369 }
1370
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001371 xfs_iext_last(ifp, &icur);
1372 if (!xfs_iext_get_extent(ifp, &icur, rec))
Dave Chinner9e5987a72013-02-25 12:31:26 +11001373 *is_empty = 1;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001374 else
1375 *is_empty = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001376 return 0;
1377}
1378
1379/*
1380 * Check the last inode extent to determine whether this allocation will result
1381 * in blocks being allocated at the end of the file. When we allocate new data
1382 * blocks at the end of the file which do not start at the previous data block,
1383 * we will try to align the new blocks at stripe unit boundaries.
1384 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001385 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a72013-02-25 12:31:26 +11001386 * at, or past the EOF.
1387 */
1388STATIC int
1389xfs_bmap_isaeof(
1390 struct xfs_bmalloca *bma,
1391 int whichfork)
1392{
1393 struct xfs_bmbt_irec rec;
1394 int is_empty;
1395 int error;
1396
Thomas Meyer749f24f2017-10-09 11:38:54 -07001397 bma->aeof = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001398 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1399 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001400 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001401 return error;
1402
Dave Chinner6e708bc2013-11-22 10:41:16 +11001403 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001404 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001405 return 0;
1406 }
1407
Dave Chinner9e5987a72013-02-25 12:31:26 +11001408 /*
1409 * Check if we are allocation or past the last extent, or at least into
1410 * the last delayed allocated extent.
1411 */
1412 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1413 (bma->offset >= rec.br_startoff &&
1414 isnullstartblock(rec.br_startblock));
1415 return 0;
1416}
1417
1418/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001419 * Returns the file-relative block number of the first block past eof in
1420 * the file. This is not based on i_size, it is based on the extent records.
1421 * Returns 0 for local files, as they do not have extent records.
1422 */
1423int
1424xfs_bmap_last_offset(
Dave Chinner9e5987a72013-02-25 12:31:26 +11001425 struct xfs_inode *ip,
1426 xfs_fileoff_t *last_block,
1427 int whichfork)
1428{
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001429 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001430 struct xfs_bmbt_irec rec;
1431 int is_empty;
1432 int error;
1433
1434 *last_block = 0;
1435
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001436 if (ifp->if_format == XFS_DINODE_FMT_LOCAL)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001437 return 0;
1438
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001439 if (XFS_IS_CORRUPT(ip->i_mount, !xfs_ifork_has_extents(ifp)))
Darrick J. Wongc2414ad2019-10-28 16:12:34 -07001440 return -EFSCORRUPTED;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001441
1442 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1443 if (error || is_empty)
1444 return error;
1445
1446 *last_block = rec.br_startoff + rec.br_blockcount;
1447 return 0;
1448}
1449
1450/*
1451 * Returns whether the selected fork of the inode has exactly one
1452 * block or not. For the data fork we check this matches di_size,
1453 * implying the file's range is 0..bsize-1.
1454 */
1455int /* 1=>1 block, 0=>otherwise */
1456xfs_bmap_one_block(
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001457 struct xfs_inode *ip, /* incore inode */
1458 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001459{
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001460 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1461 int rval; /* return value */
1462 struct xfs_bmbt_irec s; /* internal version of extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001463 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001464
1465#ifndef DEBUG
1466 if (whichfork == XFS_DATA_FORK)
1467 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1468#endif /* !DEBUG */
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001469 if (ifp->if_nextents != 1)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001470 return 0;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001471 if (ifp->if_format != XFS_DINODE_FMT_EXTENTS)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001472 return 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001473 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001474 xfs_iext_first(ifp, &icur);
1475 xfs_iext_get_extent(ifp, &icur, &s);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001476 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1477 if (rval && whichfork == XFS_DATA_FORK)
1478 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1479 return rval;
1480}
1481
1482/*
1483 * Extent tree manipulation functions used during allocation.
1484 */
1485
1486/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001487 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 */
1489STATIC int /* error */
1490xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001491 struct xfs_bmalloca *bma,
1492 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001494 struct xfs_mount *mp = bma->ip->i_mount;
1495 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001496 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 int i; /* temp state */
1499 xfs_fileoff_t new_endoff; /* end offset of new entry */
1500 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1501 /* left is 0, right is 1, prev is 2 */
1502 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07001503 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001504 xfs_filblks_t da_new; /* new count del alloc blocks used */
1505 xfs_filblks_t da_old; /* old count del alloc blocks used */
1506 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 int tmp_rval; /* partial logging flags */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001508 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Darrick J. Wong60b49842016-10-03 09:11:34 -07001510 ASSERT(whichfork != XFS_ATTR_FORK);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001511 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001512 ASSERT(!bma->cur ||
Dave Chinner8ef54792020-03-10 17:54:38 -07001513 (bma->cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001514
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001515 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517#define LEFT r[0]
1518#define RIGHT r[1]
1519#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 /*
1522 * Set up a bunch of variables to make the tests simpler.
1523 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001524 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001526 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 ASSERT(PREV.br_startoff <= new->br_startoff);
1528 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001529
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001530 da_old = startblockval(PREV.br_startblock);
1531 da_new = 0;
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 /*
1534 * Set flags determining what part of the previous delayed allocation
1535 * extent is being replaced by a real allocation.
1536 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001537 if (PREV.br_startoff == new->br_startoff)
1538 state |= BMAP_LEFT_FILLING;
1539 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1540 state |= BMAP_RIGHT_FILLING;
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 /*
1543 * Check and set flags if this segment has a left neighbor.
1544 * Don't set contiguous if the combined extent would be too large.
1545 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001546 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001547 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001548 if (isnullstartblock(LEFT.br_startblock))
1549 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001551
1552 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1553 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1554 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1555 LEFT.br_state == new->br_state &&
1556 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1557 state |= BMAP_LEFT_CONTIG;
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 /*
1560 * Check and set flags if this segment has a right neighbor.
1561 * Don't set contiguous if the combined extent would be too large.
1562 * Also check for all-three-contiguous being too large.
1563 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001564 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001565 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001566 if (isnullstartblock(RIGHT.br_startblock))
1567 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001569
1570 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1571 new_endoff == RIGHT.br_startoff &&
1572 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1573 new->br_state == RIGHT.br_state &&
1574 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1575 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1576 BMAP_RIGHT_FILLING)) !=
1577 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1578 BMAP_RIGHT_FILLING) ||
1579 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1580 <= MAXEXTLEN))
1581 state |= BMAP_RIGHT_CONTIG;
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 error = 0;
1584 /*
1585 * Switch out based on the FILLING and CONTIG state bits.
1586 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001587 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1588 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1589 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1590 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 /*
1592 * Filling in all of a previously delayed allocation extent.
1593 * The left and right neighbors are both contiguous with new.
1594 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001595 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001596
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001597 xfs_iext_remove(bma->ip, &bma->icur, state);
1598 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001599 xfs_iext_prev(ifp, &bma->icur);
1600 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001601 ifp->if_nextents--;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001602
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001603 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1605 else {
1606 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001607 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001608 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001610 if (XFS_IS_CORRUPT(mp, i != 1)) {
1611 error = -EFSCORRUPTED;
1612 goto done;
1613 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001614 error = xfs_btree_delete(bma->cur, &i);
1615 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001617 if (XFS_IS_CORRUPT(mp, i != 1)) {
1618 error = -EFSCORRUPTED;
1619 goto done;
1620 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001621 error = xfs_btree_decrement(bma->cur, 0, &i);
1622 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001624 if (XFS_IS_CORRUPT(mp, i != 1)) {
1625 error = -EFSCORRUPTED;
1626 goto done;
1627 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001628 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001629 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 goto done;
1631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 break;
1633
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001634 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 /*
1636 * Filling in all of a previously delayed allocation extent.
1637 * The left neighbor is contiguous, the right is not.
1638 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001639 old = LEFT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001640 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001641
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001642 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001643 xfs_iext_prev(ifp, &bma->icur);
1644 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001645
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001646 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 rval = XFS_ILOG_DEXT;
1648 else {
1649 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001650 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001651 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001653 if (XFS_IS_CORRUPT(mp, i != 1)) {
1654 error = -EFSCORRUPTED;
1655 goto done;
1656 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001657 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001658 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 goto done;
1660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 break;
1662
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001663 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 /*
1665 * Filling in all of a previously delayed allocation extent.
Dave Chinner9230a0b2018-11-19 22:50:08 -08001666 * The right neighbor is contiguous, the left is not. Take care
1667 * with delay -> unwritten extent allocation here because the
1668 * delalloc record we are overwriting is always written.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001670 PREV.br_startblock = new->br_startblock;
1671 PREV.br_blockcount += RIGHT.br_blockcount;
Dave Chinner9230a0b2018-11-19 22:50:08 -08001672 PREV.br_state = new->br_state;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001673
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001674 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001675 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001676 xfs_iext_prev(ifp, &bma->icur);
1677 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001678
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001679 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 rval = XFS_ILOG_DEXT;
1681 else {
1682 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001683 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001684 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001686 if (XFS_IS_CORRUPT(mp, i != 1)) {
1687 error = -EFSCORRUPTED;
1688 goto done;
1689 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001690 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001691 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 goto done;
1693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 break;
1695
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001696 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 /*
1698 * Filling in all of a previously delayed allocation extent.
1699 * Neither the left nor right neighbors are contiguous with
1700 * the new one.
1701 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001702 PREV.br_startblock = new->br_startblock;
1703 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001704 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001705 ifp->if_nextents++;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001706
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001707 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1709 else {
1710 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001711 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001712 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001714 if (XFS_IS_CORRUPT(mp, i != 0)) {
1715 error = -EFSCORRUPTED;
1716 goto done;
1717 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001718 error = xfs_btree_insert(bma->cur, &i);
1719 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001721 if (XFS_IS_CORRUPT(mp, i != 1)) {
1722 error = -EFSCORRUPTED;
1723 goto done;
1724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 break;
1727
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001728 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 /*
1730 * Filling in the first part of a previous delayed allocation.
1731 * The left neighbor is contiguous.
1732 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001733 old = LEFT;
1734 temp = PREV.br_blockcount - new->br_blockcount;
1735 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1736 startblockval(PREV.br_startblock));
1737
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001738 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001739
Christoph Hellwigbf999712017-11-03 10:34:38 -07001740 PREV.br_blockcount = temp;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001741 PREV.br_startoff += new->br_blockcount;
1742 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwig0d045542017-11-03 10:34:39 -07001743
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001744 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1745 xfs_iext_prev(ifp, &bma->icur);
1746 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001747
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001748 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 rval = XFS_ILOG_DEXT;
1750 else {
1751 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001752 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001753 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001755 if (XFS_IS_CORRUPT(mp, i != 1)) {
1756 error = -EFSCORRUPTED;
1757 goto done;
1758 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001759 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001760 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 goto done;
1762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 break;
1764
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001765 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 /*
1767 * Filling in the first part of a previous delayed allocation.
1768 * The left neighbor is not contiguous.
1769 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001770 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001771 ifp->if_nextents++;
1772
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001773 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1775 else {
1776 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001777 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001778 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001780 if (XFS_IS_CORRUPT(mp, i != 0)) {
1781 error = -EFSCORRUPTED;
1782 goto done;
1783 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001784 error = xfs_btree_insert(bma->cur, &i);
1785 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001787 if (XFS_IS_CORRUPT(mp, i != 1)) {
1788 error = -EFSCORRUPTED;
1789 goto done;
1790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001792
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001793 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001794 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001795 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 rval |= tmp_rval;
1797 if (error)
1798 goto done;
1799 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001800
1801 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001802 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001803 startblockval(PREV.br_startblock) -
Dave Chinner92219c22020-03-10 17:52:53 -07001804 (bma->cur ? bma->cur->bc_ino.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001805
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001806 PREV.br_startoff = new_endoff;
1807 PREV.br_blockcount = temp;
1808 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001809 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001810 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001811 xfs_iext_prev(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 break;
1813
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001814 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 /*
1816 * Filling in the last part of a previous delayed allocation.
1817 * The right neighbor is contiguous with the new allocation.
1818 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001819 old = RIGHT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001820 RIGHT.br_startoff = new->br_startoff;
1821 RIGHT.br_startblock = new->br_startblock;
1822 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001823
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001824 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 rval = XFS_ILOG_DEXT;
1826 else {
1827 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001828 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001829 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001831 if (XFS_IS_CORRUPT(mp, i != 1)) {
1832 error = -EFSCORRUPTED;
1833 goto done;
1834 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001835 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001836 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 goto done;
1838 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001839
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001840 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001841 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001842 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001843
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001844 PREV.br_blockcount = temp;
1845 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001846
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001847 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1848 xfs_iext_next(ifp, &bma->icur);
1849 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 break;
1851
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001852 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 /*
1854 * Filling in the last part of a previous delayed allocation.
1855 * The right neighbor is not contiguous.
1856 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001857 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001858 ifp->if_nextents++;
1859
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001860 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1862 else {
1863 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001864 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001865 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001867 if (XFS_IS_CORRUPT(mp, i != 0)) {
1868 error = -EFSCORRUPTED;
1869 goto done;
1870 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001871 error = xfs_btree_insert(bma->cur, &i);
1872 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001874 if (XFS_IS_CORRUPT(mp, i != 1)) {
1875 error = -EFSCORRUPTED;
1876 goto done;
1877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001879
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001880 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001881 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001882 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 rval |= tmp_rval;
1884 if (error)
1885 goto done;
1886 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001887
1888 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001889 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001890 startblockval(PREV.br_startblock) -
Dave Chinner92219c22020-03-10 17:52:53 -07001891 (bma->cur ? bma->cur->bc_ino.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001892
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001893 PREV.br_startblock = nullstartblock(da_new);
1894 PREV.br_blockcount = temp;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001895 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001896 xfs_iext_next(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 break;
1898
1899 case 0:
1900 /*
1901 * Filling in the middle part of a previous delayed allocation.
1902 * Contiguity is impossible here.
1903 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001904 *
1905 * We start with a delayed allocation:
1906 *
1907 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1908 * PREV @ idx
1909 *
1910 * and we are allocating:
1911 * +rrrrrrrrrrrrrrrrr+
1912 * new
1913 *
1914 * and we set it up for insertion as:
1915 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1916 * new
1917 * PREV @ idx LEFT RIGHT
1918 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001920 old = PREV;
1921
1922 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001923 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001924
1925 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001926 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001927 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001928 RIGHT.br_blockcount =
1929 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1930 RIGHT.br_startblock =
1931 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1932 RIGHT.br_blockcount));
1933
1934 /* truncate PREV */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001935 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1936 PREV.br_startblock =
1937 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1938 PREV.br_blockcount));
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001939 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001940
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001941 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001942 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1943 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001944 ifp->if_nextents++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001945
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001946 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1948 else {
1949 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001950 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001951 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001953 if (XFS_IS_CORRUPT(mp, i != 0)) {
1954 error = -EFSCORRUPTED;
1955 goto done;
1956 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001957 error = xfs_btree_insert(bma->cur, &i);
1958 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001960 if (XFS_IS_CORRUPT(mp, i != 1)) {
1961 error = -EFSCORRUPTED;
1962 goto done;
1963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001965
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001966 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001967 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001968 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 rval |= tmp_rval;
1970 if (error)
1971 goto done;
1972 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001973
1974 da_new = startblockval(PREV.br_startblock) +
1975 startblockval(RIGHT.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 break;
1977
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001978 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1979 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1980 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1981 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1982 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1983 case BMAP_LEFT_CONTIG:
1984 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 /*
1986 * These cases are all impossible.
1987 */
1988 ASSERT(0);
1989 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001990
Darrick J. Wong95eb3082018-05-09 10:02:32 -07001991 /* add reverse mapping unless caller opted out */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07001992 if (!(bma->flags & XFS_BMAPI_NORMAP))
1993 xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10001994
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001995 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001996 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001997 int tmp_logflags; /* partial log flag return val */
1998
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001999 ASSERT(bma->cur == NULL);
2000 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07002001 &bma->cur, da_old > 0, &tmp_logflags,
2002 whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002003 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002004 if (error)
2005 goto done;
2006 }
2007
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07002008 if (da_new != da_old)
2009 xfs_mod_delalloc(mp, (int64_t)da_new - da_old);
2010
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002011 if (bma->cur) {
Dave Chinner92219c22020-03-10 17:52:53 -07002012 da_new += bma->cur->bc_ino.allocated;
2013 bma->cur->bc_ino.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002014 }
2015
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002016 /* adjust for changes in reserved delayed indirect blocks */
2017 if (da_new != da_old) {
2018 ASSERT(state == 0 || da_new < da_old);
2019 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2020 false);
2021 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002022
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002023 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002025 if (whichfork != XFS_COW_FORK)
2026 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 return error;
2028#undef LEFT
2029#undef RIGHT
2030#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
2033/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002034 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 */
Christoph Hellwig26b91c72019-02-18 09:38:48 -08002036int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002038 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002040 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002041 struct xfs_iext_cursor *icur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002043 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002044 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 int i; /* temp state */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07002049 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2052 /* left is 0, right is 1, prev is 2 */
2053 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002054 int state = xfs_bmap_fork_to_state(whichfork);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002055 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002056 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002058 *logflagsp = 0;
2059
2060 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002061 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002062
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002063 ASSERT(!isnullstartblock(new->br_startblock));
2064
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002065 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067#define LEFT r[0]
2068#define RIGHT r[1]
2069#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 /*
2072 * Set up a bunch of variables to make the tests simpler.
2073 */
2074 error = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002075 xfs_iext_get_extent(ifp, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002076 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 new_endoff = new->br_startoff + new->br_blockcount;
2078 ASSERT(PREV.br_startoff <= new->br_startoff);
2079 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 /*
2082 * Set flags determining what part of the previous oldext allocation
2083 * extent is being replaced by a newext allocation.
2084 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002085 if (PREV.br_startoff == new->br_startoff)
2086 state |= BMAP_LEFT_FILLING;
2087 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2088 state |= BMAP_RIGHT_FILLING;
2089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 /*
2091 * Check and set flags if this segment has a left neighbor.
2092 * Don't set contiguous if the combined extent would be too large.
2093 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002094 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002095 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002096 if (isnullstartblock(LEFT.br_startblock))
2097 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002099
2100 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2101 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2102 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002103 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002104 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2105 state |= BMAP_LEFT_CONTIG;
2106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 /*
2108 * Check and set flags if this segment has a right neighbor.
2109 * Don't set contiguous if the combined extent would be too large.
2110 * Also check for all-three-contiguous being too large.
2111 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002112 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002113 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002114 if (isnullstartblock(RIGHT.br_startblock))
2115 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002117
2118 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2119 new_endoff == RIGHT.br_startoff &&
2120 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002121 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002122 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2123 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2124 BMAP_RIGHT_FILLING)) !=
2125 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2126 BMAP_RIGHT_FILLING) ||
2127 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2128 <= MAXEXTLEN))
2129 state |= BMAP_RIGHT_CONTIG;
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 /*
2132 * Switch out based on the FILLING and CONTIG state bits.
2133 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002134 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2135 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2136 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2137 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 /*
2139 * Setting all of a previous oldext extent to newext.
2140 * The left and right neighbors are both contiguous with new.
2141 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002142 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002143
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002144 xfs_iext_remove(ip, icur, state);
2145 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 -= 2;
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, &RIGHT, &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 Hellwig91cca5df2008-10-30 16:58:01 +11002172 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002174 if (XFS_IS_CORRUPT(mp, i != 1)) {
2175 error = -EFSCORRUPTED;
2176 goto done;
2177 }
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002178 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002180 if (XFS_IS_CORRUPT(mp, i != 1)) {
2181 error = -EFSCORRUPTED;
2182 goto done;
2183 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002184 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002185 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 goto done;
2187 }
2188 break;
2189
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002190 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 /*
2192 * Setting all of a previous oldext extent to newext.
2193 * The left neighbor is contiguous, the right is not.
2194 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002195 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002196
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002197 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002198 xfs_iext_prev(ifp, icur);
2199 xfs_iext_update_extent(ip, state, icur, &LEFT);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002200 ifp->if_nextents--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 if (cur == NULL)
2202 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2203 else {
2204 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002205 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2206 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002208 if (XFS_IS_CORRUPT(mp, i != 1)) {
2209 error = -EFSCORRUPTED;
2210 goto done;
2211 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002212 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002214 if (XFS_IS_CORRUPT(mp, i != 1)) {
2215 error = -EFSCORRUPTED;
2216 goto done;
2217 }
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002218 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002220 if (XFS_IS_CORRUPT(mp, i != 1)) {
2221 error = -EFSCORRUPTED;
2222 goto done;
2223 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002224 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002225 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 goto done;
2227 }
2228 break;
2229
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002230 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 /*
2232 * Setting all of a previous oldext extent to newext.
2233 * The right neighbor is contiguous, the left is not.
2234 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002235 PREV.br_blockcount += RIGHT.br_blockcount;
2236 PREV.br_state = new->br_state;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002237
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002238 xfs_iext_next(ifp, icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002239 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002240 xfs_iext_prev(ifp, icur);
2241 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002242 ifp->if_nextents--;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (cur == NULL)
2245 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2246 else {
2247 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002248 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2249 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002251 if (XFS_IS_CORRUPT(mp, i != 1)) {
2252 error = -EFSCORRUPTED;
2253 goto done;
2254 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002255 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002257 if (XFS_IS_CORRUPT(mp, i != 1)) {
2258 error = -EFSCORRUPTED;
2259 goto done;
2260 }
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002261 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002263 if (XFS_IS_CORRUPT(mp, i != 1)) {
2264 error = -EFSCORRUPTED;
2265 goto done;
2266 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002267 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002268 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 goto done;
2270 }
2271 break;
2272
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002273 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 /*
2275 * Setting all of a previous oldext extent to newext.
2276 * Neither the left nor right neighbors are contiguous with
2277 * the new one.
2278 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002279 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002280 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (cur == NULL)
2283 rval = XFS_ILOG_DEXT;
2284 else {
2285 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002286 error = xfs_bmbt_lookup_eq(cur, new, &i);
2287 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002289 if (XFS_IS_CORRUPT(mp, i != 1)) {
2290 error = -EFSCORRUPTED;
2291 goto done;
2292 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002293 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002294 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 goto done;
2296 }
2297 break;
2298
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002299 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 /*
2301 * Setting the first part of a previous oldext extent to newext.
2302 * The left neighbor is contiguous.
2303 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002304 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002305
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002306 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002307 PREV.br_startoff += new->br_blockcount;
2308 PREV.br_startblock += new->br_blockcount;
2309 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002310
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002311 xfs_iext_update_extent(ip, state, icur, &PREV);
2312 xfs_iext_prev(ifp, icur);
2313 xfs_iext_update_extent(ip, state, icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 if (cur == NULL)
2316 rval = XFS_ILOG_DEXT;
2317 else {
2318 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002319 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002320 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002322 if (XFS_IS_CORRUPT(mp, i != 1)) {
2323 error = -EFSCORRUPTED;
2324 goto done;
2325 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002326 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002327 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002329 error = xfs_btree_decrement(cur, 0, &i);
2330 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002332 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002333 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 goto done;
2335 }
2336 break;
2337
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002338 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 /*
2340 * Setting the first part of a previous oldext extent to newext.
2341 * The left neighbor is not contiguous.
2342 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002343 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002344 PREV.br_startoff += new->br_blockcount;
2345 PREV.br_startblock += new->br_blockcount;
2346 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002347
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002348 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002349 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002350 ifp->if_nextents++;
2351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 if (cur == NULL)
2353 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2354 else {
2355 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002356 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002357 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002359 if (XFS_IS_CORRUPT(mp, i != 1)) {
2360 error = -EFSCORRUPTED;
2361 goto done;
2362 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002363 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002364 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 goto done;
2366 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002367 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002369 if (XFS_IS_CORRUPT(mp, i != 1)) {
2370 error = -EFSCORRUPTED;
2371 goto done;
2372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
2374 break;
2375
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002376 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 /*
2378 * Setting the last part of a previous oldext extent to newext.
2379 * The right neighbor is contiguous with the new allocation.
2380 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002381 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002382 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002383
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002384 RIGHT.br_startoff = new->br_startoff;
2385 RIGHT.br_startblock = new->br_startblock;
2386 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002387
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002388 xfs_iext_update_extent(ip, state, icur, &PREV);
2389 xfs_iext_next(ifp, icur);
2390 xfs_iext_update_extent(ip, state, icur, &RIGHT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (cur == NULL)
2393 rval = XFS_ILOG_DEXT;
2394 else {
2395 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002396 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002397 if (error)
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 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002403 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002404 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002406 error = xfs_btree_increment(cur, 0, &i);
2407 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002409 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002410 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 goto done;
2412 }
2413 break;
2414
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002415 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 /*
2417 * Setting the last part of a previous oldext extent to newext.
2418 * The right neighbor is not contiguous.
2419 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002420 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002421 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002422
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002423 xfs_iext_update_extent(ip, state, icur, &PREV);
2424 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002425 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002426 ifp->if_nextents++;
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 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002439 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002440 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002442 error = xfs_bmbt_lookup_eq(cur, new, &i);
2443 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002445 if (XFS_IS_CORRUPT(mp, i != 0)) {
2446 error = -EFSCORRUPTED;
2447 goto done;
2448 }
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002449 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002451 if (XFS_IS_CORRUPT(mp, i != 1)) {
2452 error = -EFSCORRUPTED;
2453 goto done;
2454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 }
2456 break;
2457
2458 case 0:
2459 /*
2460 * Setting the middle part of a previous oldext extent to
2461 * newext. Contiguity is impossible here.
2462 * One extent becomes three extents.
2463 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002464 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002465 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 r[0] = *new;
2468 r[1].br_startoff = new_endoff;
2469 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002470 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002472 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002473
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002474 xfs_iext_update_extent(ip, state, icur, &PREV);
2475 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002476 xfs_iext_insert(ip, icur, &r[1], state);
2477 xfs_iext_insert(ip, icur, &r[0], state);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002478 ifp->if_nextents += 2;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002479
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 if (cur == NULL)
2481 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2482 else {
2483 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002484 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002485 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002487 if (XFS_IS_CORRUPT(mp, i != 1)) {
2488 error = -EFSCORRUPTED;
2489 goto done;
2490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002492 error = xfs_bmbt_update(cur, &r[1]);
2493 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 goto done;
2495 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002497 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002499 if (XFS_IS_CORRUPT(mp, i != 1)) {
2500 error = -EFSCORRUPTED;
2501 goto done;
2502 }
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002503 /*
2504 * Reset the cursor to the position of the new extent
2505 * we are about to insert as we can't trust it after
2506 * the previous insert.
2507 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002508 error = xfs_bmbt_lookup_eq(cur, new, &i);
2509 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002511 if (XFS_IS_CORRUPT(mp, i != 0)) {
2512 error = -EFSCORRUPTED;
2513 goto done;
2514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002516 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002518 if (XFS_IS_CORRUPT(mp, i != 1)) {
2519 error = -EFSCORRUPTED;
2520 goto done;
2521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 }
2523 break;
2524
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002525 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2526 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2527 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2528 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2529 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2530 case BMAP_LEFT_CONTIG:
2531 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 /*
2533 * These cases are all impossible.
2534 */
2535 ASSERT(0);
2536 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002537
Darrick J. Wong9c194642016-08-03 12:16:05 +10002538 /* update reverse mappings */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07002539 xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002540
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002541 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002542 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002543 int tmp_logflags; /* partial log flag return val */
2544
2545 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07002546 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2547 &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002548 *logflagsp |= tmp_logflags;
2549 if (error)
2550 goto done;
2551 }
2552
2553 /* clear out the allocated field, done with it now in any case. */
2554 if (cur) {
Dave Chinner92219c22020-03-10 17:52:53 -07002555 cur->bc_ino.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002556 *curp = cur;
2557 }
2558
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002559 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002561 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 return error;
2563#undef LEFT
2564#undef RIGHT
2565#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566}
2567
2568/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002569 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002571STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572xfs_bmap_add_extent_hole_delay(
2573 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002574 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002575 struct xfs_iext_cursor *icur,
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002576 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577{
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07002578 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2580 xfs_filblks_t newlen=0; /* new indirect size */
2581 xfs_filblks_t oldlen=0; /* old indirect size */
2582 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002583 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002584 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002586 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002587 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002588
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 /*
2590 * Check and set flags if this segment has a left neighbor
2591 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002592 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002593 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002594 if (isnullstartblock(left.br_startblock))
2595 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 /*
2599 * Check and set flags if the current (right) segment exists.
2600 * If it doesn't exist, we're converting the hole at end-of-file.
2601 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002602 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002603 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002604 if (isnullstartblock(right.br_startblock))
2605 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 /*
2609 * Set contiguity flags on the left and right neighbors.
2610 * Don't let extents get too large, even if the pieces are contiguous.
2611 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002612 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2613 left.br_startoff + left.br_blockcount == new->br_startoff &&
2614 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2615 state |= BMAP_LEFT_CONTIG;
2616
2617 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2618 new->br_startoff + new->br_blockcount == right.br_startoff &&
2619 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2620 (!(state & BMAP_LEFT_CONTIG) ||
2621 (left.br_blockcount + new->br_blockcount +
2622 right.br_blockcount <= MAXEXTLEN)))
2623 state |= BMAP_RIGHT_CONTIG;
2624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 /*
2626 * Switch out based on the contiguity flags.
2627 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002628 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2629 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 /*
2631 * New allocation is contiguous with delayed allocations
2632 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002633 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 */
2635 temp = left.br_blockcount + new->br_blockcount +
2636 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002637
Eric Sandeen9d87c312009-01-14 23:22:07 -06002638 oldlen = startblockval(left.br_startblock) +
2639 startblockval(new->br_startblock) +
2640 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002641 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2642 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002643 left.br_startblock = nullstartblock(newlen);
2644 left.br_blockcount = temp;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002645
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002646 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002647 xfs_iext_prev(ifp, icur);
2648 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 break;
2650
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002651 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 /*
2653 * New allocation is contiguous with a delayed allocation
2654 * on the left.
2655 * Merge the new allocation with the left neighbor.
2656 */
2657 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002658
Eric Sandeen9d87c312009-01-14 23:22:07 -06002659 oldlen = startblockval(left.br_startblock) +
2660 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002661 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2662 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002663 left.br_blockcount = temp;
2664 left.br_startblock = nullstartblock(newlen);
Christoph Hellwig41d196f2017-11-03 10:34:39 -07002665
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002666 xfs_iext_prev(ifp, icur);
2667 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 break;
2669
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002670 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 /*
2672 * New allocation is contiguous with a delayed allocation
2673 * on the right.
2674 * Merge the new allocation with the right neighbor.
2675 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002677 oldlen = startblockval(new->br_startblock) +
2678 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002679 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2680 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002681 right.br_startoff = new->br_startoff;
2682 right.br_startblock = nullstartblock(newlen);
2683 right.br_blockcount = temp;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002684 xfs_iext_update_extent(ip, state, icur, &right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 break;
2686
2687 case 0:
2688 /*
2689 * New allocation is not contiguous with another
2690 * delayed allocation.
2691 * Insert a new entry.
2692 */
2693 oldlen = newlen = 0;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002694 xfs_iext_insert(ip, icur, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 break;
2696 }
2697 if (oldlen != newlen) {
2698 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002699 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2700 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 /*
2702 * Nothing to do for disk quota accounting here.
2703 */
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07002704 xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706}
2707
2708/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002709 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 */
2711STATIC int /* error */
2712xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002713 struct xfs_trans *tp,
2714 struct xfs_inode *ip,
2715 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002716 struct xfs_iext_cursor *icur,
Christoph Hellwig6d045582017-04-11 16:45:54 -07002717 struct xfs_btree_cur **curp,
2718 struct xfs_bmbt_irec *new,
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002719 int *logflagsp,
2720 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002722 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2723 struct xfs_mount *mp = ip->i_mount;
2724 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2728 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002729 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002730 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002731 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002733 ASSERT(!isnullstartblock(new->br_startblock));
Dave Chinner8ef54792020-03-10 17:54:38 -07002734 ASSERT(!cur || !(cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002735
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002736 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 /*
2739 * Check and set flags if this segment has a left neighbor.
2740 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002741 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002742 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002743 if (isnullstartblock(left.br_startblock))
2744 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 /*
2748 * Check and set flags if this segment has a current value.
2749 * Not true if we're inserting into the "hole" at eof.
2750 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002751 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002752 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002753 if (isnullstartblock(right.br_startblock))
2754 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002756
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 /*
2758 * We're inserting a real allocation between "left" and "right".
2759 * Set the contiguity flags. Don't let extents get too large.
2760 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002761 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2762 left.br_startoff + left.br_blockcount == new->br_startoff &&
2763 left.br_startblock + left.br_blockcount == new->br_startblock &&
2764 left.br_state == new->br_state &&
2765 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2766 state |= BMAP_LEFT_CONTIG;
2767
2768 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2769 new->br_startoff + new->br_blockcount == right.br_startoff &&
2770 new->br_startblock + new->br_blockcount == right.br_startblock &&
2771 new->br_state == right.br_state &&
2772 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2773 (!(state & BMAP_LEFT_CONTIG) ||
2774 left.br_blockcount + new->br_blockcount +
2775 right.br_blockcount <= MAXEXTLEN))
2776 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002778 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 /*
2780 * Select which case we're in here, and implement it.
2781 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002782 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2783 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 /*
2785 * New allocation is contiguous with real allocations on the
2786 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002787 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002789 left.br_blockcount += new->br_blockcount + right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002790
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002791 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002792 xfs_iext_prev(ifp, icur);
2793 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002794 ifp->if_nextents--;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002795
Christoph Hellwig6d045582017-04-11 16:45:54 -07002796 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002797 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002798 } else {
2799 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002800 error = xfs_bmbt_lookup_eq(cur, &right, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002801 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002802 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002803 if (XFS_IS_CORRUPT(mp, i != 1)) {
2804 error = -EFSCORRUPTED;
2805 goto done;
2806 }
Christoph Hellwig6d045582017-04-11 16:45:54 -07002807 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002808 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002809 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002810 if (XFS_IS_CORRUPT(mp, i != 1)) {
2811 error = -EFSCORRUPTED;
2812 goto done;
2813 }
Christoph Hellwig6d045582017-04-11 16:45:54 -07002814 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002815 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002816 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002817 if (XFS_IS_CORRUPT(mp, i != 1)) {
2818 error = -EFSCORRUPTED;
2819 goto done;
2820 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002821 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002822 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002823 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002825 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002827 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 /*
2829 * New allocation is contiguous with a real allocation
2830 * on the left.
2831 * Merge the new allocation with the left neighbor.
2832 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002833 old = left;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002834 left.br_blockcount += new->br_blockcount;
Christoph Hellwig1d2e0082017-11-03 10:34:40 -07002835
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002836 xfs_iext_prev(ifp, icur);
2837 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002838
Christoph Hellwig6d045582017-04-11 16:45:54 -07002839 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002840 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002841 } else {
2842 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002843 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002844 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002845 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002846 if (XFS_IS_CORRUPT(mp, i != 1)) {
2847 error = -EFSCORRUPTED;
2848 goto done;
2849 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002850 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002851 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002852 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002854 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002856 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 /*
2858 * New allocation is contiguous with a real allocation
2859 * on the right.
2860 * Merge the new allocation with the right neighbor.
2861 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002862 old = right;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07002863
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002864 right.br_startoff = new->br_startoff;
2865 right.br_startblock = new->br_startblock;
2866 right.br_blockcount += new->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002867 xfs_iext_update_extent(ip, state, icur, &right);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002868
Christoph Hellwig6d045582017-04-11 16:45:54 -07002869 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002870 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002871 } else {
2872 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002873 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002874 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002875 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002876 if (XFS_IS_CORRUPT(mp, i != 1)) {
2877 error = -EFSCORRUPTED;
2878 goto done;
2879 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002880 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002881 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002882 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002884 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
2886 case 0:
2887 /*
2888 * New allocation is not contiguous with another
2889 * real allocation.
2890 * Insert a new entry.
2891 */
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002892 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002893 ifp->if_nextents++;
2894
Christoph Hellwig6d045582017-04-11 16:45:54 -07002895 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002896 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002897 } else {
2898 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002899 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002900 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002901 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002902 if (XFS_IS_CORRUPT(mp, i != 0)) {
2903 error = -EFSCORRUPTED;
2904 goto done;
2905 }
Christoph Hellwig6d045582017-04-11 16:45:54 -07002906 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002907 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002908 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002909 if (XFS_IS_CORRUPT(mp, i != 1)) {
2910 error = -EFSCORRUPTED;
2911 goto done;
2912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002914 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002916
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002917 /* add reverse mapping unless caller opted out */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07002918 if (!(flags & XFS_BMAPI_NORMAP))
2919 xfs_rmap_map_extent(tp, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002920
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002921 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002922 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002923 int tmp_logflags; /* partial log flag return val */
2924
Christoph Hellwig6d045582017-04-11 16:45:54 -07002925 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07002926 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2927 &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002928 *logflagsp |= tmp_logflags;
2929 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002930 if (error)
2931 goto done;
2932 }
2933
2934 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002935 if (cur)
Dave Chinner92219c22020-03-10 17:52:53 -07002936 cur->bc_ino.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002937
Christoph Hellwig6d045582017-04-11 16:45:54 -07002938 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002939done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002940 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002941 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942}
2943
Nathan Scottdd9f4382006-01-11 15:28:28 +11002944/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11002945 * Functions used in the extent read, allocate and remove paths
2946 */
2947
2948/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002949 * Adjust the size of the new extent based on di_extsize and rt extsize.
2950 */
Dave Chinner68988112013-08-12 20:49:42 +10002951int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002952xfs_bmap_extsize_align(
2953 xfs_mount_t *mp,
2954 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2955 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2956 xfs_extlen_t extsz, /* align to this extent size */
2957 int rt, /* is this a realtime inode? */
2958 int eof, /* is extent at end-of-file? */
2959 int delay, /* creating delalloc extent? */
2960 int convert, /* overwriting unwritten extent? */
2961 xfs_fileoff_t *offp, /* in/out: aligned offset */
2962 xfs_extlen_t *lenp) /* in/out: aligned length */
2963{
2964 xfs_fileoff_t orig_off; /* original offset */
2965 xfs_extlen_t orig_alen; /* original length */
2966 xfs_fileoff_t orig_end; /* original off+len */
2967 xfs_fileoff_t nexto; /* next file offset */
2968 xfs_fileoff_t prevo; /* previous file offset */
2969 xfs_fileoff_t align_off; /* temp for offset */
2970 xfs_extlen_t align_alen; /* temp for length */
2971 xfs_extlen_t temp; /* temp for calculations */
2972
2973 if (convert)
2974 return 0;
2975
2976 orig_off = align_off = *offp;
2977 orig_alen = align_alen = *lenp;
2978 orig_end = orig_off + orig_alen;
2979
2980 /*
2981 * If this request overlaps an existing extent, then don't
2982 * attempt to perform any additional alignment.
2983 */
2984 if (!delay && !eof &&
2985 (orig_off >= gotp->br_startoff) &&
2986 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2987 return 0;
2988 }
2989
2990 /*
2991 * If the file offset is unaligned vs. the extent size
2992 * we need to align it. This will be possible unless
2993 * the file was previously written with a kernel that didn't
2994 * perform this alignment, or if a truncate shot us in the
2995 * foot.
2996 */
Dave Chinner0703a8e2018-06-08 09:54:22 -07002997 div_u64_rem(orig_off, extsz, &temp);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002998 if (temp) {
2999 align_alen += temp;
3000 align_off -= temp;
3001 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003002
3003 /* Same adjustment for the end of the requested area. */
3004 temp = (align_alen % extsz);
3005 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003006 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003007
3008 /*
3009 * For large extent hint sizes, the aligned extent might be larger than
3010 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3011 * the length back under MAXEXTLEN. The outer allocation loops handle
3012 * short allocation just fine, so it is safe to do this. We only want to
3013 * do it when we are forced to, though, because it means more allocation
3014 * operations are required.
3015 */
3016 while (align_alen > MAXEXTLEN)
3017 align_alen -= extsz;
3018 ASSERT(align_alen <= MAXEXTLEN);
3019
Nathan Scottdd9f4382006-01-11 15:28:28 +11003020 /*
3021 * If the previous block overlaps with this proposed allocation
3022 * then move the start forward without adjusting the length.
3023 */
3024 if (prevp->br_startoff != NULLFILEOFF) {
3025 if (prevp->br_startblock == HOLESTARTBLOCK)
3026 prevo = prevp->br_startoff;
3027 else
3028 prevo = prevp->br_startoff + prevp->br_blockcount;
3029 } else
3030 prevo = 0;
3031 if (align_off != orig_off && align_off < prevo)
3032 align_off = prevo;
3033 /*
3034 * If the next block overlaps with this proposed allocation
3035 * then move the start back without adjusting the length,
3036 * but not before offset 0.
3037 * This may of course make the start overlap previous block,
3038 * and if we hit the offset 0 limit then the next block
3039 * can still overlap too.
3040 */
3041 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3042 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3043 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3044 nexto = gotp->br_startoff + gotp->br_blockcount;
3045 else
3046 nexto = gotp->br_startoff;
3047 } else
3048 nexto = NULLFILEOFF;
3049 if (!eof &&
3050 align_off + align_alen != orig_end &&
3051 align_off + align_alen > nexto)
3052 align_off = nexto > align_alen ? nexto - align_alen : 0;
3053 /*
3054 * If we're now overlapping the next or previous extent that
3055 * means we can't fit an extsz piece in this hole. Just move
3056 * the start forward to the first valid spot and set
3057 * the length so we hit the end.
3058 */
3059 if (align_off != orig_off && align_off < prevo)
3060 align_off = prevo;
3061 if (align_off + align_alen != orig_end &&
3062 align_off + align_alen > nexto &&
3063 nexto != NULLFILEOFF) {
3064 ASSERT(nexto > prevo);
3065 align_alen = nexto - align_off;
3066 }
3067
3068 /*
3069 * If realtime, and the result isn't a multiple of the realtime
3070 * extent size we need to remove blocks until it is.
3071 */
3072 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3073 /*
3074 * We're not covering the original request, or
3075 * we won't be able to once we fix the length.
3076 */
3077 if (orig_off < align_off ||
3078 orig_end > align_off + align_alen ||
3079 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003080 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003081 /*
3082 * Try to fix it by moving the start up.
3083 */
3084 if (align_off + temp <= orig_off) {
3085 align_alen -= temp;
3086 align_off += temp;
3087 }
3088 /*
3089 * Try to fix it by moving the end in.
3090 */
3091 else if (align_off + align_alen - temp >= orig_end)
3092 align_alen -= temp;
3093 /*
3094 * Set the start to the minimum then trim the length.
3095 */
3096 else {
3097 align_alen -= orig_off - align_off;
3098 align_off = orig_off;
3099 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3100 }
3101 /*
3102 * Result doesn't cover the request, fail it.
3103 */
3104 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003105 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003106 } else {
3107 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003108 /* see MAXEXTLEN handling above */
3109 ASSERT(orig_end <= align_off + align_alen ||
3110 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003111 }
3112
3113#ifdef DEBUG
3114 if (!eof && gotp->br_startoff != NULLFILEOFF)
3115 ASSERT(align_off + align_alen <= gotp->br_startoff);
3116 if (prevp->br_startoff != NULLFILEOFF)
3117 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3118#endif
3119
3120 *lenp = align_alen;
3121 *offp = align_off;
3122 return 0;
3123}
3124
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125#define XFS_ALLOC_GAP_UNITS 4
3126
Dave Chinner68988112013-08-12 20:49:42 +10003127void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003128xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003129 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130{
3131 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3133 xfs_mount_t *mp; /* mount point structure */
3134 int nullfb; /* true if ap->firstblock isn't set */
3135 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
3137#define ISVALID(x,y) \
3138 (rt ? \
3139 (x) < mp->m_sb.sb_rblocks : \
3140 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3141 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3142 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3143
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 mp = ap->ip->i_mount;
Brian Foster94c07b42018-07-11 22:26:28 -07003145 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003146 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
Christoph Hellwigc34d5702019-10-30 12:25:00 -07003147 (ap->datatype & XFS_ALLOC_USERDATA);
Brian Foster94c07b42018-07-11 22:26:28 -07003148 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3149 ap->tp->t_firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 /*
3151 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003152 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003154 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3155 !isnullstartblock(ap->prev.br_startblock) &&
3156 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3157 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003158 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 /*
3160 * Adjust for the gap between prevp and us.
3161 */
Dave Chinner3a756672011-09-18 20:40:58 +00003162 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003163 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003165 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3166 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 }
3168 /*
3169 * If not at eof, then compare the two neighbor blocks.
3170 * Figure out whether either one gives us a good starting point,
3171 * and pick the better one.
3172 */
3173 else if (!ap->eof) {
3174 xfs_fsblock_t gotbno; /* right side block number */
3175 xfs_fsblock_t gotdiff=0; /* right side difference */
3176 xfs_fsblock_t prevbno; /* left side block number */
3177 xfs_fsblock_t prevdiff=0; /* left side difference */
3178
3179 /*
3180 * If there's a previous (left) block, select a requested
3181 * start block based on it.
3182 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003183 if (ap->prev.br_startoff != NULLFILEOFF &&
3184 !isnullstartblock(ap->prev.br_startblock) &&
3185 (prevbno = ap->prev.br_startblock +
3186 ap->prev.br_blockcount) &&
3187 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 /*
3189 * Calculate gap to end of previous block.
3190 */
Dave Chinner3a756672011-09-18 20:40:58 +00003191 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003192 (ap->prev.br_startoff +
3193 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 /*
3195 * Figure the startblock based on the previous block's
3196 * end and the gap size.
3197 * Heuristic!
3198 * If the gap is large relative to the piece we're
3199 * allocating, or using it gives us an invalid block
3200 * number, then just use the end of the previous block.
3201 */
Dave Chinner3a756672011-09-18 20:40:58 +00003202 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003204 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 prevbno += adjust;
3206 else
3207 prevdiff += adjust;
3208 /*
3209 * If the firstblock forbids it, can't use it,
3210 * must use default.
3211 */
3212 if (!rt && !nullfb &&
3213 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3214 prevbno = NULLFSBLOCK;
3215 }
3216 /*
3217 * No previous block or can't follow it, just default.
3218 */
3219 else
3220 prevbno = NULLFSBLOCK;
3221 /*
3222 * If there's a following (right) block, select a requested
3223 * start block based on it.
3224 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003225 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 /*
3227 * Calculate gap to start of next block.
3228 */
Dave Chinner3a756672011-09-18 20:40:58 +00003229 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 /*
3231 * Figure the startblock based on the next block's
3232 * start and the gap size.
3233 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003234 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 /*
3236 * Heuristic!
3237 * If the gap is large relative to the piece we're
3238 * allocating, or using it gives us an invalid block
3239 * number, then just use the start of the next block
3240 * offset by our length.
3241 */
Dave Chinner3a756672011-09-18 20:40:58 +00003242 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 ISVALID(gotbno - gotdiff, gotbno))
3244 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003245 else if (ISVALID(gotbno - ap->length, gotbno)) {
3246 gotbno -= ap->length;
3247 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 } else
3249 gotdiff += adjust;
3250 /*
3251 * If the firstblock forbids it, can't use it,
3252 * must use default.
3253 */
3254 if (!rt && !nullfb &&
3255 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3256 gotbno = NULLFSBLOCK;
3257 }
3258 /*
3259 * No next block, just default.
3260 */
3261 else
3262 gotbno = NULLFSBLOCK;
3263 /*
3264 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003265 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 */
3267 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003268 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003270 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003272 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003274#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003275}
3276
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003277static int
3278xfs_bmap_longest_free_extent(
3279 struct xfs_trans *tp,
3280 xfs_agnumber_t ag,
3281 xfs_extlen_t *blen,
3282 int *notinit)
3283{
3284 struct xfs_mount *mp = tp->t_mountp;
3285 struct xfs_perag *pag;
3286 xfs_extlen_t longest;
3287 int error = 0;
3288
3289 pag = xfs_perag_get(mp, ag);
3290 if (!pag->pagf_init) {
3291 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
Darrick J. Wongf48e2df2020-01-23 17:01:19 -08003292 if (error) {
3293 /* Couldn't lock the AGF, so skip this AG. */
3294 if (error == -EAGAIN) {
3295 *notinit = 1;
3296 error = 0;
3297 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003298 goto out;
3299 }
3300 }
3301
Eric Sandeena1f69412018-04-06 10:09:42 -07003302 longest = xfs_alloc_longest_free_extent(pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003303 xfs_alloc_min_freelist(mp, pag),
3304 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003305 if (*blen < longest)
3306 *blen = longest;
3307
3308out:
3309 xfs_perag_put(pag);
3310 return error;
3311}
3312
3313static void
3314xfs_bmap_select_minlen(
3315 struct xfs_bmalloca *ap,
3316 struct xfs_alloc_arg *args,
3317 xfs_extlen_t *blen,
3318 int notinit)
3319{
3320 if (notinit || *blen < ap->minlen) {
3321 /*
3322 * Since we did a BUF_TRYLOCK above, it is possible that
3323 * there is space for this request.
3324 */
3325 args->minlen = ap->minlen;
3326 } else if (*blen < args->maxlen) {
3327 /*
3328 * If the best seen length is less than the request length,
3329 * use the best as the minimum.
3330 */
3331 args->minlen = *blen;
3332 } else {
3333 /*
3334 * Otherwise we've seen an extent as big as maxlen, use that
3335 * as the minimum.
3336 */
3337 args->minlen = args->maxlen;
3338 }
3339}
3340
Nathan Scotta365bdd2006-03-14 13:34:16 +11003341STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003342xfs_bmap_btalloc_nullfb(
3343 struct xfs_bmalloca *ap,
3344 struct xfs_alloc_arg *args,
3345 xfs_extlen_t *blen)
3346{
3347 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003348 xfs_agnumber_t ag, startag;
3349 int notinit = 0;
3350 int error;
3351
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003352 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003353 args->total = ap->total;
3354
Christoph Hellwigc467c042010-02-15 23:34:42 +00003355 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3356 if (startag == NULLAGNUMBER)
3357 startag = ag = 0;
3358
Dave Chinner14b064c2011-01-27 12:16:28 +11003359 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003360 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3361 &notinit);
3362 if (error)
3363 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003364
Christoph Hellwigc467c042010-02-15 23:34:42 +00003365 if (++ag == mp->m_sb.sb_agcount)
3366 ag = 0;
3367 if (ag == startag)
3368 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003369 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003370
3371 xfs_bmap_select_minlen(ap, args, blen, notinit);
3372 return 0;
3373}
3374
3375STATIC int
3376xfs_bmap_btalloc_filestreams(
3377 struct xfs_bmalloca *ap,
3378 struct xfs_alloc_arg *args,
3379 xfs_extlen_t *blen)
3380{
3381 struct xfs_mount *mp = ap->ip->i_mount;
3382 xfs_agnumber_t ag;
3383 int notinit = 0;
3384 int error;
3385
3386 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3387 args->total = ap->total;
3388
3389 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3390 if (ag == NULLAGNUMBER)
3391 ag = 0;
3392
3393 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3394 if (error)
3395 return error;
3396
3397 if (*blen < args->maxlen) {
3398 error = xfs_filestream_new_ag(ap, &ag);
3399 if (error)
3400 return error;
3401
3402 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3403 &notinit);
3404 if (error)
3405 return error;
3406
3407 }
3408
3409 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003410
3411 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003412 * Set the failure fallback case to look in the selected AG as stream
3413 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003414 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003415 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003416 return 0;
3417}
3418
Darrick J. Wong751f3762018-01-25 13:58:13 -08003419/* Update all inode and quota accounting for the allocation we just did. */
3420static void
3421xfs_bmap_btalloc_accounting(
3422 struct xfs_bmalloca *ap,
3423 struct xfs_alloc_arg *args)
3424{
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003425 if (ap->flags & XFS_BMAPI_COWFORK) {
3426 /*
3427 * COW fork blocks are in-core only and thus are treated as
3428 * in-core quota reservation (like delalloc blocks) even when
3429 * converted to real blocks. The quota reservation is not
3430 * accounted to disk until blocks are remapped to the data
3431 * fork. So if these blocks were previously delalloc, we
3432 * already have quota reservation and there's nothing to do
3433 * yet.
3434 */
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003435 if (ap->wasdel) {
3436 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003437 return;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003438 }
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003439
3440 /*
3441 * Otherwise, we've allocated blocks in a hole. The transaction
3442 * has acquired in-core quota reservation for this extent.
3443 * Rather than account these as real blocks, however, we reduce
3444 * the transaction quota reservation based on the allocation.
3445 * This essentially transfers the transaction quota reservation
3446 * to that of a delalloc extent.
3447 */
3448 ap->ip->i_delayed_blks += args->len;
3449 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3450 -(long)args->len);
3451 return;
3452 }
3453
3454 /* data/attr fork only */
3455 ap->ip->i_d.di_nblocks += args->len;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003456 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003457 if (ap->wasdel) {
Darrick J. Wong751f3762018-01-25 13:58:13 -08003458 ap->ip->i_delayed_blks -= args->len;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003459 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3460 }
Darrick J. Wong751f3762018-01-25 13:58:13 -08003461 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3462 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3463 args->len);
3464}
3465
Christoph Hellwigc467c042010-02-15 23:34:42 +00003466STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003467xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003468 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003469{
3470 xfs_mount_t *mp; /* mount point structure */
3471 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003472 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003473 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003474 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003475 xfs_alloc_arg_t args;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003476 xfs_fileoff_t orig_offset;
3477 xfs_extlen_t orig_length;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003478 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003479 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003480 int nullfb; /* true if ap->firstblock isn't set */
3481 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003482 int tryagain;
3483 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003484 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003485
Dave Chinnera99ebf42011-12-01 11:24:20 +00003486 ASSERT(ap->length);
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003487 orig_offset = ap->offset;
3488 orig_length = ap->length;
Dave Chinnera99ebf42011-12-01 11:24:20 +00003489
Nathan Scotta365bdd2006-03-14 13:34:16 +11003490 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003491
3492 /* stripe alignment for allocation is determined by mount parameters */
3493 stripe_align = 0;
3494 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3495 stripe_align = mp->m_swidth;
3496 else if (mp->m_dalign)
3497 stripe_align = mp->m_dalign;
3498
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003499 if (ap->flags & XFS_BMAPI_COWFORK)
3500 align = xfs_get_cowextsz_hint(ap->ip);
Christoph Hellwigc34d5702019-10-30 12:25:00 -07003501 else if (ap->datatype & XFS_ALLOC_USERDATA)
Dave Chinner292378e2016-09-26 08:21:28 +10003502 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003503 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003504 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003505 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003506 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003507 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003508 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003509 }
Dave Chinner33177f052013-12-12 16:34:36 +11003510
3511
Brian Foster94c07b42018-07-11 22:26:28 -07003512 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3513 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3514 ap->tp->t_firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003515 if (nullfb) {
Christoph Hellwigc34d5702019-10-30 12:25:00 -07003516 if ((ap->datatype & XFS_ALLOC_USERDATA) &&
Dave Chinner292378e2016-09-26 08:21:28 +10003517 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003518 ag = xfs_filestream_lookup_ag(ap->ip);
3519 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003520 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003521 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003522 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003523 }
3524 } else
Brian Foster94c07b42018-07-11 22:26:28 -07003525 ap->blkno = ap->tp->t_firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003526
3527 xfs_bmap_adjacent(ap);
3528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003530 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 * it's in the right allocation group.
3532 */
Dave Chinner3a756672011-09-18 20:40:58 +00003533 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 ;
3535 else
Brian Foster94c07b42018-07-11 22:26:28 -07003536 ap->blkno = ap->tp->t_firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 * Normal allocation, done through xfs_alloc_vextent.
3539 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003540 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003541 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003542 args.tp = ap->tp;
3543 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003544 args.fsbno = ap->blkno;
Darrick J. Wong7280fed2018-12-12 08:46:23 -08003545 args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
Dave Chinner14b064c2011-01-27 12:16:28 +11003546
3547 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner9bb54cb2018-06-07 07:54:02 -07003548 args.maxlen = min(ap->length, mp->m_ag_max_usable);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003549 blen = 0;
3550 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003551 /*
3552 * Search for an allocation group with a single extent large
3553 * enough for the request. If one isn't found, then adjust
3554 * the minimum allocation size to the largest space found.
3555 */
Christoph Hellwigc34d5702019-10-30 12:25:00 -07003556 if ((ap->datatype & XFS_ALLOC_USERDATA) &&
Dave Chinner292378e2016-09-26 08:21:28 +10003557 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003558 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3559 else
3560 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003561 if (error)
3562 return error;
Brian Foster1214f1c2018-08-01 07:20:31 -07003563 } else if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003564 if (xfs_inode_is_filestream(ap->ip))
3565 args.type = XFS_ALLOCTYPE_FIRST_AG;
3566 else
3567 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003568 args.total = args.minlen = ap->minlen;
3569 } else {
3570 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3571 args.total = ap->total;
3572 args.minlen = ap->minlen;
3573 }
David Chinner957d0eb2007-06-18 16:50:37 +10003574 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003575 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003576 args.prod = align;
Dave Chinner0703a8e2018-06-08 09:54:22 -07003577 div_u64_rem(ap->offset, args.prod, &args.mod);
3578 if (args.mod)
3579 args.mod = args.prod - args.mod;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003580 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003581 args.prod = 1;
3582 args.mod = 0;
3583 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003584 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner0703a8e2018-06-08 09:54:22 -07003585 div_u64_rem(ap->offset, args.prod, &args.mod);
3586 if (args.mod)
3587 args.mod = args.prod - args.mod;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003588 }
3589 /*
Dave Chinner1c743572019-10-21 09:26:34 -07003590 * If we are not low on available data blocks, and the underlying
3591 * logical volume manager is a stripe, and the file offset is zero then
3592 * try to allocate data blocks on stripe unit boundary. NOTE: ap->aeof
3593 * is only set if the allocation length is >= the stripe unit and the
3594 * allocation offset is at the end of file.
Nathan Scotta365bdd2006-03-14 13:34:16 +11003595 */
Brian Foster1214f1c2018-08-01 07:20:31 -07003596 if (!(ap->tp->t_flags & XFS_TRANS_LOWMODE) && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003597 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003598 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003599 atype = args.type;
3600 isaligned = 1;
3601 /*
Dave Chinner1c743572019-10-21 09:26:34 -07003602 * Adjust minlen to try and preserve alignment if we
3603 * can't guarantee an aligned maxlen extent.
Nathan Scotta365bdd2006-03-14 13:34:16 +11003604 */
Dave Chinner1c743572019-10-21 09:26:34 -07003605 if (blen > args.alignment &&
3606 blen <= args.maxlen + args.alignment)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003607 args.minlen = blen - args.alignment;
3608 args.minalignslop = 0;
3609 } else {
3610 /*
3611 * First try an exact bno allocation.
3612 * If it fails then do a near or start bno
3613 * allocation with alignment turned on.
3614 */
3615 atype = args.type;
3616 tryagain = 1;
3617 args.type = XFS_ALLOCTYPE_THIS_BNO;
3618 args.alignment = 1;
3619 /*
3620 * Compute the minlen+alignment for the
3621 * next case. Set slop so that the value
3622 * of minlen+alignment+slop doesn't go up
3623 * between the calls.
3624 */
Dave Chinner33177f052013-12-12 16:34:36 +11003625 if (blen > stripe_align && blen <= args.maxlen)
3626 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003627 else
3628 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003629 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003630 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003631 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003632 args.minlen - 1;
3633 else
3634 args.minalignslop = 0;
3635 }
3636 } else {
3637 args.alignment = 1;
3638 args.minalignslop = 0;
3639 }
3640 args.minleft = ap->minleft;
3641 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003642 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003643 args.datatype = ap->datatype;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003644
3645 error = xfs_alloc_vextent(&args);
3646 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003647 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003648
Nathan Scotta365bdd2006-03-14 13:34:16 +11003649 if (tryagain && args.fsbno == NULLFSBLOCK) {
3650 /*
3651 * Exact allocation failed. Now try with alignment
3652 * turned on.
3653 */
3654 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003655 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003656 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003657 args.minlen = nextminlen;
3658 args.minalignslop = 0;
3659 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 if ((error = xfs_alloc_vextent(&args)))
3661 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003662 }
3663 if (isaligned && args.fsbno == NULLFSBLOCK) {
3664 /*
3665 * allocation failed, so turn off alignment and
3666 * try again.
3667 */
3668 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003669 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003670 args.alignment = 0;
3671 if ((error = xfs_alloc_vextent(&args)))
3672 return error;
3673 }
3674 if (args.fsbno == NULLFSBLOCK && nullfb &&
3675 args.minlen > ap->minlen) {
3676 args.minlen = ap->minlen;
3677 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003678 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003679 if ((error = xfs_alloc_vextent(&args)))
3680 return error;
3681 }
3682 if (args.fsbno == NULLFSBLOCK && nullfb) {
3683 args.fsbno = 0;
3684 args.type = XFS_ALLOCTYPE_FIRST_AG;
3685 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003686 if ((error = xfs_alloc_vextent(&args)))
3687 return error;
Brian Foster1214f1c2018-08-01 07:20:31 -07003688 ap->tp->t_flags |= XFS_TRANS_LOWMODE;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003689 }
3690 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003691 /*
3692 * check the allocation happened at the same or higher AG than
3693 * the first block that was allocated.
3694 */
Brian Foster94c07b42018-07-11 22:26:28 -07003695 ASSERT(ap->tp->t_firstblock == NULLFSBLOCK ||
3696 XFS_FSB_TO_AGNO(mp, ap->tp->t_firstblock) <=
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003697 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003698
Dave Chinner3a756672011-09-18 20:40:58 +00003699 ap->blkno = args.fsbno;
Brian Foster94c07b42018-07-11 22:26:28 -07003700 if (ap->tp->t_firstblock == NULLFSBLOCK)
3701 ap->tp->t_firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003702 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003703 ap->length = args.len;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003704 /*
3705 * If the extent size hint is active, we tried to round the
3706 * caller's allocation request offset down to extsz and the
3707 * length up to another extsz boundary. If we found a free
3708 * extent we mapped it in starting at this new offset. If the
3709 * newly mapped space isn't long enough to cover any of the
3710 * range of offsets that was originally requested, move the
3711 * mapping up so that we can fill as much of the caller's
3712 * original request as possible. Free space is apparently
3713 * very fragmented so we're unlikely to be able to satisfy the
3714 * hints anyway.
3715 */
3716 if (ap->length <= orig_length)
3717 ap->offset = orig_offset;
3718 else if (ap->offset + ap->length < orig_offset + orig_length)
3719 ap->offset = orig_offset + orig_length - ap->length;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003720 xfs_bmap_btalloc_accounting(ap, &args);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003721 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003722 ap->blkno = NULLFSBLOCK;
3723 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 }
3725 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003726}
3727
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003728/* Trim extent to fit a logical block range. */
3729void
3730xfs_trim_extent(
3731 struct xfs_bmbt_irec *irec,
3732 xfs_fileoff_t bno,
3733 xfs_filblks_t len)
3734{
3735 xfs_fileoff_t distance;
3736 xfs_fileoff_t end = bno + len;
3737
3738 if (irec->br_startoff + irec->br_blockcount <= bno ||
3739 irec->br_startoff >= end) {
3740 irec->br_blockcount = 0;
3741 return;
3742 }
3743
3744 if (irec->br_startoff < bno) {
3745 distance = bno - irec->br_startoff;
3746 if (isnullstartblock(irec->br_startblock))
3747 irec->br_startblock = DELAYSTARTBLOCK;
3748 if (irec->br_startblock != DELAYSTARTBLOCK &&
3749 irec->br_startblock != HOLESTARTBLOCK)
3750 irec->br_startblock += distance;
3751 irec->br_startoff += distance;
3752 irec->br_blockcount -= distance;
3753 }
3754
3755 if (end < irec->br_startoff + irec->br_blockcount) {
3756 distance = irec->br_startoff + irec->br_blockcount - end;
3757 irec->br_blockcount -= distance;
3758 }
3759}
3760
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003762 * Trim the returned map to the required bounds
3763 */
3764STATIC void
3765xfs_bmapi_trim_map(
3766 struct xfs_bmbt_irec *mval,
3767 struct xfs_bmbt_irec *got,
3768 xfs_fileoff_t *bno,
3769 xfs_filblks_t len,
3770 xfs_fileoff_t obno,
3771 xfs_fileoff_t end,
3772 int n,
3773 int flags)
3774{
3775 if ((flags & XFS_BMAPI_ENTIRE) ||
3776 got->br_startoff + got->br_blockcount <= obno) {
3777 *mval = *got;
3778 if (isnullstartblock(got->br_startblock))
3779 mval->br_startblock = DELAYSTARTBLOCK;
3780 return;
3781 }
3782
3783 if (obno > *bno)
3784 *bno = obno;
3785 ASSERT((*bno >= obno) || (n == 0));
3786 ASSERT(*bno < end);
3787 mval->br_startoff = *bno;
3788 if (isnullstartblock(got->br_startblock))
3789 mval->br_startblock = DELAYSTARTBLOCK;
3790 else
3791 mval->br_startblock = got->br_startblock +
3792 (*bno - got->br_startoff);
3793 /*
3794 * Return the minimum of what we got and what we asked for for
3795 * the length. We can use the len variable here because it is
3796 * modified below and we could have been there before coming
3797 * here if the first part of the allocation didn't overlap what
3798 * was asked for.
3799 */
3800 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3801 got->br_blockcount - (*bno - got->br_startoff));
3802 mval->br_state = got->br_state;
3803 ASSERT(mval->br_blockcount <= len);
3804 return;
3805}
3806
3807/*
3808 * Update and validate the extent map to return
3809 */
3810STATIC void
3811xfs_bmapi_update_map(
3812 struct xfs_bmbt_irec **map,
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 xfs_bmbt_irec_t *mval = *map;
3821
3822 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3823 ((mval->br_startoff + mval->br_blockcount) <= end));
3824 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3825 (mval->br_startoff < obno));
3826
3827 *bno = mval->br_startoff + mval->br_blockcount;
3828 *len = end - *bno;
3829 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3830 /* update previous map with new information */
3831 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3832 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3833 ASSERT(mval->br_state == mval[-1].br_state);
3834 mval[-1].br_blockcount = mval->br_blockcount;
3835 mval[-1].br_state = mval->br_state;
3836 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3837 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3838 mval[-1].br_startblock != HOLESTARTBLOCK &&
3839 mval->br_startblock == mval[-1].br_startblock +
3840 mval[-1].br_blockcount &&
Christoph Hellwigc3a2f9f2018-07-11 22:26:01 -07003841 mval[-1].br_state == mval->br_state) {
Dave Chinneraef9a892011-09-18 20:40:44 +00003842 ASSERT(mval->br_startoff ==
3843 mval[-1].br_startoff + mval[-1].br_blockcount);
3844 mval[-1].br_blockcount += mval->br_blockcount;
3845 } else if (*n > 0 &&
3846 mval->br_startblock == DELAYSTARTBLOCK &&
3847 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3848 mval->br_startoff ==
3849 mval[-1].br_startoff + mval[-1].br_blockcount) {
3850 mval[-1].br_blockcount += mval->br_blockcount;
3851 mval[-1].br_state = mval->br_state;
3852 } else if (!((*n == 0) &&
3853 ((mval->br_startoff + mval->br_blockcount) <=
3854 obno))) {
3855 mval++;
3856 (*n)++;
3857 }
3858 *map = mval;
3859}
3860
3861/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003862 * Map file blocks to filesystem blocks without allocation.
3863 */
3864int
3865xfs_bmapi_read(
3866 struct xfs_inode *ip,
3867 xfs_fileoff_t bno,
3868 xfs_filblks_t len,
3869 struct xfs_bmbt_irec *mval,
3870 int *nmap,
3871 int flags)
3872{
3873 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig4b516ff2020-05-14 14:06:41 -07003874 int whichfork = xfs_bmapi_whichfork(flags);
3875 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003876 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003877 xfs_fileoff_t obno;
3878 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003879 struct xfs_iext_cursor icur;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003880 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003881 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003882 int n = 0;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003883
3884 ASSERT(*nmap >= 1);
Christoph Hellwig1a1c57b2020-05-14 14:06:40 -07003885 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003886 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003887
Christoph Hellwig4b516ff2020-05-14 14:06:41 -07003888 if (WARN_ON_ONCE(!ifp))
3889 return -EFSCORRUPTED;
3890
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07003891 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
3892 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT))
Dave Chinner24513372014-06-25 14:58:08 +10003893 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003894
3895 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003896 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003897
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003898 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003899
Dave Chinner5c8ed202011-09-18 20:40:45 +00003900 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3901 error = xfs_iread_extents(NULL, ip, whichfork);
3902 if (error)
3903 return error;
3904 }
3905
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003906 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003907 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003908 end = bno + len;
3909 obno = bno;
3910
3911 while (bno < end && n < *nmap) {
3912 /* Reading past eof, act as though there's a hole up to end. */
3913 if (eof)
3914 got.br_startoff = end;
3915 if (got.br_startoff > bno) {
3916 /* Reading in a hole. */
3917 mval->br_startoff = bno;
3918 mval->br_startblock = HOLESTARTBLOCK;
3919 mval->br_blockcount =
3920 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3921 mval->br_state = XFS_EXT_NORM;
3922 bno += mval->br_blockcount;
3923 len -= mval->br_blockcount;
3924 mval++;
3925 n++;
3926 continue;
3927 }
3928
3929 /* set up the extent map to return. */
3930 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3931 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3932
3933 /* If we're done, stop now. */
3934 if (bno >= end || n >= *nmap)
3935 break;
3936
3937 /* Else go on to the next record. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003938 if (!xfs_iext_next_extent(ifp, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003939 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003940 }
3941 *nmap = n;
3942 return 0;
3943}
3944
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003945/*
3946 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3947 * global pool and the extent inserted into the inode in-core extent tree.
3948 *
3949 * On entry, got refers to the first extent beyond the offset of the extent to
3950 * allocate or eof is specified if no such extent exists. On return, got refers
3951 * to the extent record that was inserted to the inode fork.
3952 *
3953 * Note that the allocated extent may have been merged with contiguous extents
3954 * during insertion into the inode fork. Thus, got does not reflect the current
3955 * state of the inode fork on return. If necessary, the caller can use lastx to
3956 * look up the updated record in the inode fork.
3957 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10003958int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003959xfs_bmapi_reserve_delalloc(
3960 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003961 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11003962 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003963 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11003964 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003965 struct xfs_bmbt_irec *got,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003966 struct xfs_iext_cursor *icur,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003967 int eof)
3968{
3969 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003970 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003971 xfs_extlen_t alen;
3972 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003973 int error;
Brian Foster974ae922016-11-28 14:57:42 +11003974 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003975
Brian Foster974ae922016-11-28 14:57:42 +11003976 /*
3977 * Cap the alloc length. Keep track of prealloc so we know whether to
3978 * tag the inode before we return.
3979 */
3980 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003981 if (!eof)
3982 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11003983 if (prealloc && alen >= len)
3984 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003985
3986 /* Figure out the extent size, adjust alen */
Shan Hai6ca30722018-01-23 13:56:11 -08003987 if (whichfork == XFS_COW_FORK) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003988 struct xfs_bmbt_irec prev;
Shan Hai6ca30722018-01-23 13:56:11 -08003989 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003990
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003991 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003992 prev.br_startoff = NULLFILEOFF;
3993
Shan Hai6ca30722018-01-23 13:56:11 -08003994 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003995 1, 0, &aoff, &alen);
3996 ASSERT(!error);
3997 }
3998
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003999 /*
4000 * Make a transaction-less quota reservation for delayed allocation
4001 * blocks. This number gets adjusted later. We return if we haven't
4002 * allocated blocks already inside this loop.
4003 */
4004 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
Shan Hai6ca30722018-01-23 13:56:11 -08004005 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004006 if (error)
4007 return error;
4008
4009 /*
4010 * Split changing sb for alen and indlen since they could be coming
4011 * from different places.
4012 */
4013 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4014 ASSERT(indlen > 0);
4015
Shan Hai6ca30722018-01-23 13:56:11 -08004016 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004017 if (error)
4018 goto out_unreserve_quota;
4019
Dave Chinner0d485ad2015-02-23 21:22:03 +11004020 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004021 if (error)
4022 goto out_unreserve_blocks;
4023
4024
4025 ip->i_delayed_blks += alen;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07004026 xfs_mod_delalloc(ip->i_mount, alen + indlen);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004027
4028 got->br_startoff = aoff;
4029 got->br_startblock = nullstartblock(indlen);
4030 got->br_blockcount = alen;
4031 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004032
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004033 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004034
Brian Foster974ae922016-11-28 14:57:42 +11004035 /*
4036 * Tag the inode if blocks were preallocated. Note that COW fork
4037 * preallocation can occur at the start or end of the extent, even when
4038 * prealloc == 0, so we must also check the aligned offset and length.
4039 */
4040 if (whichfork == XFS_DATA_FORK && prealloc)
4041 xfs_inode_set_eofblocks_tag(ip);
4042 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4043 xfs_inode_set_cowblocks_tag(ip);
4044
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004045 return 0;
4046
4047out_unreserve_blocks:
Shan Hai6ca30722018-01-23 13:56:11 -08004048 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004049out_unreserve_quota:
4050 if (XFS_IS_QUOTA_ON(mp))
Shan Hai6ca30722018-01-23 13:56:11 -08004051 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0,
4052 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004053 return error;
4054}
4055
Dave Chinnercf11da92014-07-15 07:08:24 +10004056static int
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004057xfs_bmap_alloc_userdata(
4058 struct xfs_bmalloca *bma)
4059{
4060 struct xfs_mount *mp = bma->ip->i_mount;
4061 int whichfork = xfs_bmapi_whichfork(bma->flags);
4062 int error;
4063
4064 /*
4065 * Set the data type being allocated. For the data fork, the first data
4066 * in the file is treated differently to all other allocations. For the
4067 * attribute fork, we only need to ensure the allocated range is not on
4068 * the busy list.
4069 */
4070 bma->datatype = XFS_ALLOC_NOBUSY;
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004071 if (whichfork == XFS_DATA_FORK) {
Christoph Hellwigc34d5702019-10-30 12:25:00 -07004072 bma->datatype |= XFS_ALLOC_USERDATA;
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004073 if (bma->offset == 0)
4074 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004075
4076 if (mp->m_dalign && bma->length >= mp->m_dalign) {
4077 error = xfs_bmap_isaeof(bma, whichfork);
4078 if (error)
4079 return error;
4080 }
4081
4082 if (XFS_IS_REALTIME_INODE(bma->ip))
4083 return xfs_bmap_rtalloc(bma);
4084 }
4085
4086 return xfs_bmap_btalloc(bma);
4087}
4088
4089static int
Dave Chinnercf11da92014-07-15 07:08:24 +10004090xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004091 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004092{
4093 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004094 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004095 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004096 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004097 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004098
Dave Chinnera99ebf42011-12-01 11:24:20 +00004099 ASSERT(bma->length > 0);
4100
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004101 /*
4102 * For the wasdelay case, we could also just allocate the stuff asked
4103 * for in this bmap call but that wouldn't be as good.
4104 */
4105 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004106 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4107 bma->offset = bma->got.br_startoff;
Darrick J. Wongf5be0842019-11-06 08:53:54 -08004108 if (!xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev))
4109 bma->prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004110 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004111 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004112 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004113 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004114 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004115 }
4116
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004117 if (bma->flags & XFS_BMAPI_CONTIG)
4118 bma->minlen = bma->length;
4119 else
4120 bma->minlen = 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004121
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004122 if (bma->flags & XFS_BMAPI_METADATA)
4123 error = xfs_bmap_btalloc(bma);
4124 else
4125 error = xfs_bmap_alloc_userdata(bma);
4126 if (error || bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004127 return error;
4128
Christoph Hellwigfd638f12019-10-30 12:25:00 -07004129 if (bma->flags & XFS_BMAPI_ZERO) {
4130 error = xfs_zero_extent(bma->ip, bma->blkno, bma->length);
4131 if (error)
4132 return error;
4133 }
4134
Brian Fostercf612de2018-07-11 22:26:29 -07004135 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur)
Dave Chinner29c8d172011-09-18 20:41:00 +00004136 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004137 /*
4138 * Bump the number of extents we've allocated
4139 * in this call.
4140 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004141 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004142
Dave Chinner29c8d172011-09-18 20:41:00 +00004143 if (bma->cur)
Dave Chinner92219c22020-03-10 17:52:53 -07004144 bma->cur->bc_ino.flags =
Dave Chinner8ef54792020-03-10 17:54:38 -07004145 bma->wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004146
Dave Chinner963c30c2011-09-18 20:40:59 +00004147 bma->got.br_startoff = bma->offset;
4148 bma->got.br_startblock = bma->blkno;
4149 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004150 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004151
Darrick J. Wonga5949d32020-05-23 09:43:31 -07004152 if (bma->flags & XFS_BMAPI_PREALLOC)
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004153 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004154
Christoph Hellwigc6534242011-09-18 20:41:05 +00004155 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004156 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004157 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004158 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004159 whichfork, &bma->icur, &bma->cur, &bma->got,
Brian Foster92f9da32018-07-11 22:26:28 -07004160 &bma->logflags, bma->flags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004161
Christoph Hellwigc315c902011-09-18 20:41:02 +00004162 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004163 if (error)
4164 return error;
4165
4166 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004167 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4168 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4169 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004170 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004171 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004172
Dave Chinner963c30c2011-09-18 20:40:59 +00004173 ASSERT(bma->got.br_startoff <= bma->offset);
4174 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4175 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004176 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4177 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004178 return 0;
4179}
4180
Dave Chinnerb447fe52011-09-18 20:40:51 +00004181STATIC int
4182xfs_bmapi_convert_unwritten(
4183 struct xfs_bmalloca *bma,
4184 struct xfs_bmbt_irec *mval,
4185 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004186 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004187{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004188 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004189 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004190 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004191 int error;
4192
Dave Chinnerb447fe52011-09-18 20:40:51 +00004193 /* check if we need to do unwritten->real conversion */
4194 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4195 (flags & XFS_BMAPI_PREALLOC))
4196 return 0;
4197
4198 /* check if we need to do real->unwritten conversion */
4199 if (mval->br_state == XFS_EXT_NORM &&
4200 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4201 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4202 return 0;
4203
4204 /*
4205 * Modify (by adding) the state flag, if writing.
4206 */
4207 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004208 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4209 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004210 bma->ip, whichfork);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004211 }
4212 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4213 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4214
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004215 /*
4216 * Before insertion into the bmbt, zero the range being converted
4217 * if required.
4218 */
4219 if (flags & XFS_BMAPI_ZERO) {
4220 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4221 mval->br_blockcount);
4222 if (error)
4223 return error;
4224 }
4225
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004226 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
Brian Foster92f9da32018-07-11 22:26:28 -07004227 &bma->icur, &bma->cur, mval, &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004228 /*
4229 * Log the inode core unconditionally in the unwritten extent conversion
4230 * path because the conversion might not have done so (e.g., if the
4231 * extent count hasn't changed). We need to make sure the inode is dirty
4232 * in the transaction for the sake of fsync(), even if nothing has
4233 * changed, because fsync() will not force the log for this transaction
4234 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004235 *
4236 * Note: If we're only converting cow fork extents, there aren't
4237 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004238 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004239 if (whichfork != XFS_COW_FORK)
4240 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004241 if (error)
4242 return error;
4243
4244 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004245 * Update our extent pointer, given that
4246 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4247 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004248 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004249 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004250
4251 /*
4252 * We may have combined previously unwritten space with written space,
4253 * so generate another request.
4254 */
4255 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004256 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004257 return 0;
4258}
4259
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004260static inline xfs_extlen_t
4261xfs_bmapi_minleft(
4262 struct xfs_trans *tp,
4263 struct xfs_inode *ip,
4264 int fork)
4265{
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004266 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, fork);
4267
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004268 if (tp && tp->t_firstblock != NULLFSBLOCK)
4269 return 0;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004270 if (ifp->if_format != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004271 return 1;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004272 return be16_to_cpu(ifp->if_broot->bb_level) + 1;
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004273}
4274
4275/*
4276 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4277 * a case where the data is changed, there's an error, and it's not logged so we
4278 * don't shutdown when we should. Don't bother logging extents/btree changes if
4279 * we converted to the other format.
4280 */
4281static void
4282xfs_bmapi_finish(
4283 struct xfs_bmalloca *bma,
4284 int whichfork,
4285 int error)
4286{
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004287 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4288
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004289 if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004290 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004291 bma->logflags &= ~xfs_ilog_fext(whichfork);
4292 else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004293 ifp->if_format != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004294 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4295
4296 if (bma->logflags)
4297 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4298 if (bma->cur)
4299 xfs_btree_del_cursor(bma->cur, error);
4300}
4301
Christoph Hellwig44032802011-09-18 20:40:48 +00004302/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004303 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4304 * extent state if necessary. Details behaviour is controlled by the flags
4305 * parameter. Only allocates blocks from a single allocation group, to avoid
4306 * locking problems.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004308int
4309xfs_bmapi_write(
4310 struct xfs_trans *tp, /* transaction pointer */
4311 struct xfs_inode *ip, /* incore inode */
4312 xfs_fileoff_t bno, /* starting file offs. mapped */
4313 xfs_filblks_t len, /* length to map in file */
4314 int flags, /* XFS_BMAPI_... */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004315 xfs_extlen_t total, /* total blocks needed */
4316 struct xfs_bmbt_irec *mval, /* output: map values */
Brian Foster6e702a52018-07-11 22:26:12 -07004317 int *nmap) /* i/o: mval size/count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318{
Darrick J. Wong4b0bce32019-03-19 08:16:22 -07004319 struct xfs_bmalloca bma = {
4320 .tp = tp,
4321 .ip = ip,
4322 .total = total,
4323 };
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004324 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004325 int whichfork = xfs_bmapi_whichfork(flags);
4326 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004327 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004328 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004329 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004330 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004331 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004332
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004334 xfs_fileoff_t orig_bno; /* original block number value */
4335 int orig_flags; /* original flags arg value */
4336 xfs_filblks_t orig_len; /* original value of len arg */
4337 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4338 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
4340 orig_bno = bno;
4341 orig_len = len;
4342 orig_flags = flags;
4343 orig_mval = mval;
4344 orig_nmap = *nmap;
4345#endif
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004346
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004348 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
Christoph Hellwig26b91c72019-02-18 09:38:48 -08004349 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004350 ASSERT(len > 0);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004351 ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004352 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004353 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004354
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004355 /* zeroing is for currently only for data extents, not metadata */
4356 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4357 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4358 /*
4359 * we can allocate unwritten extents or pre-zero allocated blocks,
4360 * but it makes no sense to do both at once. This would result in
4361 * zeroing the unwritten extent twice, but it still being an
4362 * unwritten extent....
4363 */
4364 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4365 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4366
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004367 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
Darrick J. Wonga71895c2019-11-11 12:53:22 -08004368 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Dave Chinner24513372014-06-25 14:58:08 +10004369 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004371
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004373 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004374
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004375 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004376
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004377 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4378 error = xfs_iread_extents(tp, ip, whichfork);
4379 if (error)
4380 goto error0;
4381 }
4382
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004383 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004384 eof = true;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004385 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004386 bma.prev.br_startoff = NULLFILEOFF;
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004387 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004388
Brian Foster627209f2019-02-01 09:14:23 -08004389 n = 0;
4390 end = bno + len;
4391 obno = bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004393 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004394
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004395 /* in hole or beyond EOF? */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004396 if (eof || bma.got.br_startoff > bno) {
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004397 /*
4398 * CoW fork conversions should /never/ hit EOF or
4399 * holes. There should always be something for us
4400 * to work on.
4401 */
4402 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4403 (flags & XFS_BMAPI_COWFORK)));
4404
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004405 need_alloc = true;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004406 } else if (isnullstartblock(bma.got.br_startblock)) {
4407 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004408 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004409
4410 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 * First, deal with the hole before the allocated space
4412 * that we found, if any.
4413 */
Christoph Hellwig26b91c72019-02-18 09:38:48 -08004414 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004415 bma.eof = eof;
4416 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4417 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004418 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004419 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004420
Dave Chinnera99ebf42011-12-01 11:24:20 +00004421 /*
4422 * There's a 32/64 bit type mismatch between the
4423 * allocation length request (which can be 64 bits in
4424 * length) and the bma length request, which is
4425 * xfs_extlen_t and therefore 32 bits. Hence we have to
4426 * check for 32-bit overflows and handle them here.
4427 */
4428 if (len > (xfs_filblks_t)MAXEXTLEN)
4429 bma.length = MAXEXTLEN;
4430 else
4431 bma.length = len;
4432
4433 ASSERT(len > 0);
4434 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004435 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 if (error)
4437 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004438 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004439 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004440
4441 /*
4442 * If this is a CoW allocation, record the data in
4443 * the refcount btree for orphan recovery.
4444 */
Darrick J. Wong74b4c5d2019-08-26 17:06:04 -07004445 if (whichfork == XFS_COW_FORK)
4446 xfs_refcount_alloc_cow_extent(tp, bma.blkno,
4447 bma.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004449
Dave Chinneraef9a892011-09-18 20:40:44 +00004450 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004451 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4452 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453
Dave Chinnerb447fe52011-09-18 20:40:51 +00004454 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004455 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004456 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004457 continue;
4458 if (error)
4459 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
Dave Chinneraef9a892011-09-18 20:40:44 +00004461 /* update the extent map to return */
4462 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4463
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 /*
4465 * If we're done, stop now. Stop when we've allocated
4466 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4467 * the transaction may get too big.
4468 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004469 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004471
4472 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004473 bma.prev = bma.got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004474 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004475 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004478
Christoph Hellwigb101e332019-02-15 08:02:47 -08004479 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4480 whichfork);
4481 if (error)
4482 goto error0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004483
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004484 ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwigdaf83962020-05-18 10:27:22 -07004485 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork));
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004486 xfs_bmapi_finish(&bma, whichfork, 0);
4487 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4488 orig_nmap, *nmap);
4489 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490error0:
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004491 xfs_bmapi_finish(&bma, whichfork, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 return error;
4493}
4494
Brian Foster627209f2019-02-01 09:14:23 -08004495/*
4496 * Convert an existing delalloc extent to real blocks based on file offset. This
4497 * attempts to allocate the entire delalloc extent and may require multiple
4498 * invocations to allocate the target offset if a large enough physical extent
4499 * is not available.
4500 */
4501int
4502xfs_bmapi_convert_delalloc(
Brian Foster627209f2019-02-01 09:14:23 -08004503 struct xfs_inode *ip,
Brian Foster627209f2019-02-01 09:14:23 -08004504 int whichfork,
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004505 xfs_off_t offset,
4506 struct iomap *iomap,
Christoph Hellwig491ce612019-02-15 08:02:49 -08004507 unsigned int *seq)
Brian Foster627209f2019-02-01 09:14:23 -08004508{
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004509 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004510 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004511 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004512 struct xfs_bmalloca bma = { NULL };
Darrick J. Wongaf952ae2019-12-16 11:14:09 -08004513 uint16_t flags = 0;
Christoph Hellwig491ce612019-02-15 08:02:49 -08004514 struct xfs_trans *tp;
Brian Foster627209f2019-02-01 09:14:23 -08004515 int error;
Brian Foster627209f2019-02-01 09:14:23 -08004516
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004517 if (whichfork == XFS_COW_FORK)
4518 flags |= IOMAP_F_SHARED;
4519
Christoph Hellwig491ce612019-02-15 08:02:49 -08004520 /*
4521 * Space for the extent and indirect blocks was reserved when the
4522 * delalloc extent was created so there's no need to do so here.
4523 */
4524 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4525 XFS_TRANS_RESERVE, &tp);
4526 if (error)
4527 return error;
4528
4529 xfs_ilock(ip, XFS_ILOCK_EXCL);
4530 xfs_trans_ijoin(tp, ip, 0);
4531
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004532 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4533 bma.got.br_startoff > offset_fsb) {
4534 /*
4535 * No extent found in the range we are trying to convert. This
4536 * should only happen for the COW fork, where another thread
4537 * might have moved the extent to the data fork in the meantime.
4538 */
4539 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004540 error = -EAGAIN;
4541 goto out_trans_cancel;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004542 }
Brian Foster627209f2019-02-01 09:14:23 -08004543
4544 /*
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004545 * If we find a real extent here we raced with another thread converting
4546 * the extent. Just return the real extent at this offset.
Brian Foster627209f2019-02-01 09:14:23 -08004547 */
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004548 if (!isnullstartblock(bma.got.br_startblock)) {
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004549 xfs_bmbt_to_iomap(ip, iomap, &bma.got, flags);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004550 *seq = READ_ONCE(ifp->if_seq);
4551 goto out_trans_cancel;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004552 }
4553
4554 bma.tp = tp;
4555 bma.ip = ip;
4556 bma.wasdel = true;
4557 bma.offset = bma.got.br_startoff;
4558 bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount, MAXEXTLEN);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004559 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
Darrick J. Wonga5949d32020-05-23 09:43:31 -07004560
4561 /*
4562 * When we're converting the delalloc reservations backing dirty pages
4563 * in the page cache, we must be careful about how we create the new
4564 * extents:
4565 *
4566 * New CoW fork extents are created unwritten, turned into real extents
4567 * when we're about to write the data to disk, and mapped into the data
4568 * fork after the write finishes. End of story.
4569 *
4570 * New data fork extents must be mapped in as unwritten and converted
4571 * to real extents after the write succeeds to avoid exposing stale
4572 * disk contents if we crash.
4573 */
4574 bma.flags = XFS_BMAPI_PREALLOC;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004575 if (whichfork == XFS_COW_FORK)
Darrick J. Wonga5949d32020-05-23 09:43:31 -07004576 bma.flags |= XFS_BMAPI_COWFORK;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004577
4578 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4579 bma.prev.br_startoff = NULLFILEOFF;
4580
4581 error = xfs_bmapi_allocate(&bma);
4582 if (error)
4583 goto out_finish;
4584
4585 error = -ENOSPC;
4586 if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK))
4587 goto out_finish;
4588 error = -EFSCORRUPTED;
Christoph Hellwigeb77b232019-09-03 08:13:13 -07004589 if (WARN_ON_ONCE(!xfs_valid_startblock(ip, bma.got.br_startblock)))
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004590 goto out_finish;
4591
Christoph Hellwig125851a2019-02-15 08:02:49 -08004592 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4593 XFS_STATS_INC(mp, xs_xstrat_quick);
4594
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004595 ASSERT(!isnullstartblock(bma.got.br_startblock));
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004596 xfs_bmbt_to_iomap(ip, iomap, &bma.got, flags);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004597 *seq = READ_ONCE(ifp->if_seq);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004598
Darrick J. Wong74b4c5d2019-08-26 17:06:04 -07004599 if (whichfork == XFS_COW_FORK)
4600 xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004601
4602 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4603 whichfork);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004604 if (error)
4605 goto out_finish;
4606
4607 xfs_bmapi_finish(&bma, whichfork, 0);
4608 error = xfs_trans_commit(tp);
4609 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4610 return error;
4611
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004612out_finish:
4613 xfs_bmapi_finish(&bma, whichfork, error);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004614out_trans_cancel:
4615 xfs_trans_cancel(tp);
4616 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Brian Foster627209f2019-02-01 09:14:23 -08004617 return error;
4618}
4619
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004620int
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004621xfs_bmapi_remap(
4622 struct xfs_trans *tp,
4623 struct xfs_inode *ip,
4624 xfs_fileoff_t bno,
4625 xfs_filblks_t len,
4626 xfs_fsblock_t startblock,
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004627 int flags)
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004628{
4629 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004630 struct xfs_ifork *ifp;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004631 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004632 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004633 struct xfs_iext_cursor icur;
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004634 int whichfork = xfs_bmapi_whichfork(flags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004635 int logflags = 0, error;
4636
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004637 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004638 ASSERT(len > 0);
4639 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4640 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Darrick J. Wong7644bd92018-05-14 06:34:35 -07004641 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4642 XFS_BMAPI_NORMAP)));
4643 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4644 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004645
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004646 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
Darrick J. Wonga71895c2019-11-11 12:53:22 -08004647 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004648 return -EFSCORRUPTED;
4649 }
4650
4651 if (XFS_FORCED_SHUTDOWN(mp))
4652 return -EIO;
4653
4654 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004655 error = xfs_iread_extents(tp, ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004656 if (error)
4657 return error;
4658 }
4659
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004660 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004661 /* make sure we only reflink into a hole. */
4662 ASSERT(got.br_startoff > bno);
4663 ASSERT(got.br_startoff - bno >= len);
4664 }
4665
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004666 ip->i_d.di_nblocks += len;
4667 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004668
4669 if (ifp->if_flags & XFS_IFBROOT) {
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004670 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner92219c22020-03-10 17:52:53 -07004671 cur->bc_ino.flags = 0;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004672 }
4673
4674 got.br_startoff = bno;
4675 got.br_startblock = startblock;
4676 got.br_blockcount = len;
Darrick J. Wong7644bd92018-05-14 06:34:35 -07004677 if (flags & XFS_BMAPI_PREALLOC)
4678 got.br_state = XFS_EXT_UNWRITTEN;
4679 else
4680 got.br_state = XFS_EXT_NORM;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004681
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004682 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
Brian Foster92f9da32018-07-11 22:26:28 -07004683 &cur, &got, &logflags, flags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004684 if (error)
4685 goto error0;
4686
Christoph Hellwigb101e332019-02-15 08:02:47 -08004687 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004688
4689error0:
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004690 if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004691 logflags &= ~XFS_ILOG_DEXT;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004692 else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE)
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004693 logflags &= ~XFS_ILOG_DBROOT;
4694
4695 if (logflags)
4696 xfs_trans_log_inode(tp, ip, logflags);
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07004697 if (cur)
4698 xfs_btree_del_cursor(cur, error);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004699 return error;
4700}
4701
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004703 * When a delalloc extent is split (e.g., due to a hole punch), the original
4704 * indlen reservation must be shared across the two new extents that are left
4705 * behind.
4706 *
4707 * Given the original reservation and the worst case indlen for the two new
4708 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004709 * reservation fairly across the two new extents. If necessary, steal available
4710 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4711 * ores == 1). The number of stolen blocks is returned. The availability and
4712 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004713 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004714static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004715xfs_bmap_split_indlen(
4716 xfs_filblks_t ores, /* original res. */
4717 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004718 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4719 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004720{
4721 xfs_filblks_t len1 = *indlen1;
4722 xfs_filblks_t len2 = *indlen2;
4723 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004724 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004725 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004726
4727 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004728 * Steal as many blocks as we can to try and satisfy the worst case
4729 * indlen for both new extents.
4730 */
Brian Foster75d65362017-02-13 22:48:30 -08004731 if (ores < nres && avail)
4732 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4733 ores += stolen;
4734
4735 /* nothing else to do if we've satisfied the new reservation */
4736 if (ores >= nres)
4737 return stolen;
Brian Fosterd34999c2016-03-15 11:42:47 +11004738
4739 /*
Brian Foster75d65362017-02-13 22:48:30 -08004740 * We can't meet the total required reservation for the two extents.
4741 * Calculate the percent of the overall shortage between both extents
4742 * and apply this percentage to each of the requested indlen values.
4743 * This distributes the shortage fairly and reduces the chances that one
4744 * of the two extents is left with nothing when extents are repeatedly
4745 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004746 */
Brian Foster75d65362017-02-13 22:48:30 -08004747 resfactor = (ores * 100);
4748 do_div(resfactor, nres);
4749 len1 *= resfactor;
4750 do_div(len1, 100);
4751 len2 *= resfactor;
4752 do_div(len2, 100);
4753 ASSERT(len1 + len2 <= ores);
4754 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4755
4756 /*
4757 * Hand out the remainder to each extent. If one of the two reservations
4758 * is zero, we want to make sure that one gets a block first. The loop
4759 * below starts with len1, so hand len2 a block right off the bat if it
4760 * is zero.
4761 */
4762 ores -= (len1 + len2);
4763 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4764 if (ores && !len2 && *indlen2) {
4765 len2++;
4766 ores--;
4767 }
4768 while (ores) {
4769 if (len1 < *indlen1) {
4770 len1++;
4771 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004772 }
Brian Foster75d65362017-02-13 22:48:30 -08004773 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004774 break;
Brian Foster75d65362017-02-13 22:48:30 -08004775 if (len2 < *indlen2) {
4776 len2++;
4777 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004778 }
4779 }
4780
4781 *indlen1 = len1;
4782 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004783
4784 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004785}
4786
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004787int
4788xfs_bmap_del_extent_delay(
4789 struct xfs_inode *ip,
4790 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004791 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004792 struct xfs_bmbt_irec *got,
4793 struct xfs_bmbt_irec *del)
4794{
4795 struct xfs_mount *mp = ip->i_mount;
4796 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4797 struct xfs_bmbt_irec new;
4798 int64_t da_old, da_new, da_diff = 0;
4799 xfs_fileoff_t del_endoff, got_endoff;
4800 xfs_filblks_t got_indlen, new_indlen, stolen;
Christoph Hellwig060ea652017-10-19 11:02:29 -07004801 int state = xfs_bmap_fork_to_state(whichfork);
4802 int error = 0;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004803 bool isrt;
4804
4805 XFS_STATS_INC(mp, xs_del_exlist);
4806
4807 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4808 del_endoff = del->br_startoff + del->br_blockcount;
4809 got_endoff = got->br_startoff + got->br_blockcount;
4810 da_old = startblockval(got->br_startblock);
4811 da_new = 0;
4812
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004813 ASSERT(del->br_blockcount > 0);
4814 ASSERT(got->br_startoff <= del->br_startoff);
4815 ASSERT(got_endoff >= del_endoff);
4816
4817 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004818 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004819
4820 do_div(rtexts, mp->m_sb.sb_rextsize);
4821 xfs_mod_frextents(mp, rtexts);
4822 }
4823
4824 /*
4825 * Update the inode delalloc counter now and wait to update the
4826 * sb counters as we might have to borrow some blocks for the
4827 * indirect block accounting.
4828 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004829 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4830 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004831 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004832 if (error)
4833 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004834 ip->i_delayed_blks -= del->br_blockcount;
4835
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004836 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004837 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004838 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004839 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004840
Christoph Hellwig0173c682017-10-17 14:16:22 -07004841 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4842 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004843 /*
4844 * Matches the whole extent. Delete the entry.
4845 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004846 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004847 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004848 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004849 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004850 /*
4851 * Deleting the first part of the extent.
4852 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004853 got->br_startoff = del_endoff;
4854 got->br_blockcount -= del->br_blockcount;
4855 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4856 got->br_blockcount), da_old);
4857 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004858 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004859 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004860 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004861 /*
4862 * Deleting the last part of the extent.
4863 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004864 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4865 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4866 got->br_blockcount), da_old);
4867 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004868 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004869 break;
4870 case 0:
4871 /*
4872 * Deleting the middle of the extent.
4873 *
4874 * Distribute the original indlen reservation across the two new
4875 * extents. Steal blocks from the deleted extent if necessary.
4876 * Stealing blocks simply fudges the fdblocks accounting below.
4877 * Warn if either of the new indlen reservations is zero as this
4878 * can lead to delalloc problems.
4879 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004880 got->br_blockcount = del->br_startoff - got->br_startoff;
4881 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4882
4883 new.br_blockcount = got_endoff - del_endoff;
4884 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4885
4886 WARN_ON_ONCE(!got_indlen || !new_indlen);
4887 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4888 del->br_blockcount);
4889
4890 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004891
4892 new.br_startoff = del_endoff;
4893 new.br_state = got->br_state;
4894 new.br_startblock = nullstartblock((int)new_indlen);
4895
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004896 xfs_iext_update_extent(ip, state, icur, got);
4897 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004898 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004899
4900 da_new = got_indlen + new_indlen - stolen;
4901 del->br_blockcount -= stolen;
4902 break;
4903 }
4904
4905 ASSERT(da_old >= da_new);
4906 da_diff = da_old - da_new;
4907 if (!isrt)
4908 da_diff += del->br_blockcount;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07004909 if (da_diff) {
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004910 xfs_mod_fdblocks(mp, da_diff, false);
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07004911 xfs_mod_delalloc(mp, -da_diff);
4912 }
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004913 return error;
4914}
4915
4916void
4917xfs_bmap_del_extent_cow(
4918 struct xfs_inode *ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004919 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004920 struct xfs_bmbt_irec *got,
4921 struct xfs_bmbt_irec *del)
4922{
4923 struct xfs_mount *mp = ip->i_mount;
4924 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4925 struct xfs_bmbt_irec new;
4926 xfs_fileoff_t del_endoff, got_endoff;
4927 int state = BMAP_COWFORK;
4928
4929 XFS_STATS_INC(mp, xs_del_exlist);
4930
4931 del_endoff = del->br_startoff + del->br_blockcount;
4932 got_endoff = got->br_startoff + got->br_blockcount;
4933
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004934 ASSERT(del->br_blockcount > 0);
4935 ASSERT(got->br_startoff <= del->br_startoff);
4936 ASSERT(got_endoff >= del_endoff);
4937 ASSERT(!isnullstartblock(got->br_startblock));
4938
4939 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004940 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004941 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004942 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004943
Christoph Hellwig0173c682017-10-17 14:16:22 -07004944 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4945 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004946 /*
4947 * Matches the whole extent. Delete the entry.
4948 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004949 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004950 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004951 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004952 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004953 /*
4954 * Deleting the first part of the extent.
4955 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004956 got->br_startoff = del_endoff;
4957 got->br_blockcount -= del->br_blockcount;
4958 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004959 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004960 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004961 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004962 /*
4963 * Deleting the last part of the extent.
4964 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004965 got->br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004966 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004967 break;
4968 case 0:
4969 /*
4970 * Deleting the middle of the extent.
4971 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004972 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004973
4974 new.br_startoff = del_endoff;
4975 new.br_blockcount = got_endoff - del_endoff;
4976 new.br_state = got->br_state;
4977 new.br_startblock = del->br_startblock + del->br_blockcount;
4978
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004979 xfs_iext_update_extent(ip, state, icur, got);
4980 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004981 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004982 break;
4983 }
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08004984 ip->i_delayed_blks -= del->br_blockcount;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004985}
4986
Brian Fostera9bd24a2016-03-15 11:42:46 +11004987/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11004988 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07004989 * after removing space.
Dave Chinner9e5987a72013-02-25 12:31:26 +11004990 */
4991STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07004992xfs_bmap_del_extent_real(
Dave Chinner9e5987a72013-02-25 12:31:26 +11004993 xfs_inode_t *ip, /* incore inode pointer */
4994 xfs_trans_t *tp, /* current transaction pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004995 struct xfs_iext_cursor *icur,
Dave Chinner9e5987a72013-02-25 12:31:26 +11004996 xfs_btree_cur_t *cur, /* if null, not a btree */
4997 xfs_bmbt_irec_t *del, /* data to remove from extents */
4998 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07004999 int whichfork, /* data or attr fork */
5000 int bflags) /* bmapi flags */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005001{
Dave Chinner9e5987a72013-02-25 12:31:26 +11005002 xfs_fsblock_t del_endblock=0; /* first block past del */
5003 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005004 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005005 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07005006 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005007 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005008 xfs_fileoff_t got_endoff; /* first offset past got */
5009 int i; /* temp state */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07005010 struct xfs_ifork *ifp; /* inode fork pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005011 xfs_mount_t *mp; /* mount structure */
5012 xfs_filblks_t nblks; /* quota/sb block count */
5013 xfs_bmbt_irec_t new; /* new record to be inserted */
5014 /* REFERENCED */
5015 uint qfield; /* quota field to update */
Christoph Hellwig060ea652017-10-19 11:02:29 -07005016 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005017 struct xfs_bmbt_irec old;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005018
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005019 mp = ip->i_mount;
5020 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005021
Dave Chinner9e5987a72013-02-25 12:31:26 +11005022 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005023 ASSERT(del->br_blockcount > 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005024 xfs_iext_get_extent(ifp, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005025 ASSERT(got.br_startoff <= del->br_startoff);
5026 del_endoff = del->br_startoff + del->br_blockcount;
5027 got_endoff = got.br_startoff + got.br_blockcount;
5028 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005029 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a72013-02-25 12:31:26 +11005030 qfield = 0;
5031 error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005032
Christoph Hellwig1b24b632017-10-17 14:16:22 -07005033 /*
5034 * If it's the case where the directory code is running with no block
5035 * reservation, and the deleted block is in the middle of its extent,
5036 * and the resulting insert of an extent would cause transformation to
5037 * btree format, then reject it. The calling code will then swap blocks
5038 * around instead. We have to do this now, rather than waiting for the
5039 * conversion to btree format, since the transaction will be dirty then.
5040 */
5041 if (tp->t_blk_res == 0 &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005042 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005043 ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) &&
Christoph Hellwig1b24b632017-10-17 14:16:22 -07005044 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
5045 return -ENOSPC;
5046
5047 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005048 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5049 xfs_fsblock_t bno;
5050 xfs_filblks_t len;
Dave Chinner0703a8e2018-06-08 09:54:22 -07005051 xfs_extlen_t mod;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005052
Dave Chinner0703a8e2018-06-08 09:54:22 -07005053 bno = div_u64_rem(del->br_startblock, mp->m_sb.sb_rextsize,
5054 &mod);
5055 ASSERT(mod == 0);
5056 len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize,
5057 &mod);
5058 ASSERT(mod == 0);
5059
Christoph Hellwige1d75532017-10-17 14:16:21 -07005060 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5061 if (error)
5062 goto done;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005063 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005064 nblks = len * mp->m_sb.sb_rextsize;
5065 qfield = XFS_TRANS_DQ_RTBCOUNT;
5066 } else {
5067 do_fx = 1;
5068 nblks = del->br_blockcount;
5069 qfield = XFS_TRANS_DQ_BCOUNT;
5070 }
5071
5072 del_endblock = del->br_startblock + del->br_blockcount;
5073 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005074 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005075 if (error)
5076 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005077 if (XFS_IS_CORRUPT(mp, i != 1)) {
5078 error = -EFSCORRUPTED;
5079 goto done;
5080 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11005081 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005082
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005083 if (got.br_startoff == del->br_startoff)
5084 state |= BMAP_LEFT_FILLING;
5085 if (got_endoff == del_endoff)
5086 state |= BMAP_RIGHT_FILLING;
5087
5088 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5089 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005090 /*
5091 * Matches the whole extent. Delete the entry.
5092 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005093 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005094 xfs_iext_prev(ifp, icur);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005095 ifp->if_nextents--;
5096
Dave Chinner9e5987a72013-02-25 12:31:26 +11005097 flags |= XFS_ILOG_CORE;
5098 if (!cur) {
5099 flags |= xfs_ilog_fext(whichfork);
5100 break;
5101 }
5102 if ((error = xfs_btree_delete(cur, &i)))
5103 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005104 if (XFS_IS_CORRUPT(mp, i != 1)) {
5105 error = -EFSCORRUPTED;
5106 goto done;
5107 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11005108 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005109 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005110 /*
5111 * Deleting the first part of the extent.
5112 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005113 got.br_startoff = del_endoff;
5114 got.br_startblock = del_endblock;
5115 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005116 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005117 if (!cur) {
5118 flags |= xfs_ilog_fext(whichfork);
5119 break;
5120 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005121 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005122 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005123 goto done;
5124 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005125 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005126 /*
5127 * Deleting the last part of the extent.
5128 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005129 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005130 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005131 if (!cur) {
5132 flags |= xfs_ilog_fext(whichfork);
5133 break;
5134 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005135 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005136 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005137 goto done;
5138 break;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005139 case 0:
5140 /*
5141 * Deleting the middle of the extent.
5142 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005143 old = got;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07005144
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005145 got.br_blockcount = del->br_startoff - got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005146 xfs_iext_update_extent(ip, state, icur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005147
Dave Chinner9e5987a72013-02-25 12:31:26 +11005148 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005149 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005150 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005151 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005152
Christoph Hellwige1d75532017-10-17 14:16:21 -07005153 flags |= XFS_ILOG_CORE;
5154 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005155 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005156 if (error)
5157 goto done;
5158 error = xfs_btree_increment(cur, 0, &i);
5159 if (error)
5160 goto done;
5161 cur->bc_rec.b = new;
5162 error = xfs_btree_insert(cur, &i);
5163 if (error && error != -ENOSPC)
5164 goto done;
5165 /*
5166 * If get no-space back from btree insert, it tried a
5167 * split, and we have a zero block reservation. Fix up
5168 * our state and return the error.
5169 */
5170 if (error == -ENOSPC) {
5171 /*
5172 * Reset the cursor, don't trust it after any
5173 * insert operation.
5174 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005175 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005176 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005177 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005178 if (XFS_IS_CORRUPT(mp, i != 1)) {
5179 error = -EFSCORRUPTED;
5180 goto done;
5181 }
Christoph Hellwige1d75532017-10-17 14:16:21 -07005182 /*
5183 * Update the btree record back
5184 * to the original value.
5185 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005186 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005187 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005188 goto done;
5189 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005190 * Reset the extent record back
5191 * to the original value.
Dave Chinner9e5987a72013-02-25 12:31:26 +11005192 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005193 xfs_iext_update_extent(ip, state, icur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005194 flags = 0;
5195 error = -ENOSPC;
5196 goto done;
5197 }
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005198 if (XFS_IS_CORRUPT(mp, i != 1)) {
5199 error = -EFSCORRUPTED;
5200 goto done;
5201 }
Christoph Hellwige1d75532017-10-17 14:16:21 -07005202 } else
5203 flags |= xfs_ilog_fext(whichfork);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005204
5205 ifp->if_nextents++;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005206 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005207 xfs_iext_insert(ip, icur, &new, state);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005208 break;
5209 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005210
5211 /* remove reverse mapping */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07005212 xfs_rmap_unmap_extent(tp, ip, whichfork, del);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005213
Dave Chinner9e5987a72013-02-25 12:31:26 +11005214 /*
5215 * If we need to, add to list of extents to delete.
5216 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005217 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005218 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
Darrick J. Wong74b4c5d2019-08-26 17:06:04 -07005219 xfs_refcount_decrease_extent(tp, del);
Brian Fosterfcb762f2018-05-09 08:45:04 -07005220 } else {
Brian Foster0f37d172018-08-01 07:20:34 -07005221 __xfs_bmap_add_free(tp, del->br_startblock,
Brian Foster4e529332018-05-10 09:35:42 -07005222 del->br_blockcount, NULL,
5223 (bflags & XFS_BMAPI_NODISCARD) ||
5224 del->br_state == XFS_EXT_UNWRITTEN);
Brian Fosterfcb762f2018-05-09 08:45:04 -07005225 }
Darrick J. Wong62aab202016-10-03 09:11:23 -07005226 }
5227
Dave Chinner9e5987a72013-02-25 12:31:26 +11005228 /*
5229 * Adjust inode # blocks in the file.
5230 */
5231 if (nblks)
5232 ip->i_d.di_nblocks -= nblks;
5233 /*
5234 * Adjust quota data.
5235 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005236 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a72013-02-25 12:31:26 +11005237 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5238
Dave Chinner9e5987a72013-02-25 12:31:26 +11005239done:
5240 *logflagsp = flags;
5241 return error;
5242}
5243
5244/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 * Unmap (remove) blocks from a file.
5246 * If nexts is nonzero then the number of extents to remove is limited to
5247 * that value. If not all extents in the block range can be removed then
5248 * *done is set.
5249 */
5250int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005251__xfs_bunmapi(
Brian Fosterccd9d912018-07-11 22:26:13 -07005252 struct xfs_trans *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005254 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005255 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 int flags, /* misc flags */
Brian Foster2af52842018-07-11 22:26:25 -07005257 xfs_extnum_t nexts) /* number of extents max */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258{
Brian Fosterccd9d912018-07-11 22:26:13 -07005259 struct xfs_btree_cur *cur; /* bmap btree cursor */
5260 struct xfs_bmbt_irec del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 int error; /* error return value */
5262 xfs_extnum_t extno; /* extent number in list */
Brian Fosterccd9d912018-07-11 22:26:13 -07005263 struct xfs_bmbt_irec got; /* current extent record */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07005264 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 int isrt; /* freeing in rt area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 int logflags; /* transaction logging flags */
5267 xfs_extlen_t mod; /* rt extent offset */
Darrick J. Wonga71895c2019-11-11 12:53:22 -08005268 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 int tmp_logflags; /* partial logging flags */
5270 int wasdel; /* was a delayed alloc extent */
5271 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005273 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005274 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005275 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005276 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005277 struct xfs_iext_cursor icur;
5278 bool done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005280 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005281
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005282 whichfork = xfs_bmapi_whichfork(flags);
5283 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005285 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)))
Dave Chinner24513372014-06-25 14:58:08 +10005286 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005288 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005289
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005290 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 ASSERT(len > 0);
5292 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005293
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005294 /*
5295 * Guesstimate how many blocks we can unmap without running the risk of
5296 * blowing out the transaction with a mix of EFIs and reflink
5297 * adjustments.
5298 */
Darrick J. Wong8c57b882017-12-10 18:03:53 -08005299 if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005300 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5301 else
5302 max_len = len;
5303
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5305 (error = xfs_iread_extents(tp, ip, whichfork)))
5306 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005307 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005308 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 return 0;
5310 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005311 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005312 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwigdc560152017-10-23 16:32:39 -07005313 end = start + len;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005314
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005315 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
Christoph Hellwigdc560152017-10-23 16:32:39 -07005316 *rlen = 0;
5317 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 }
Christoph Hellwigdc560152017-10-23 16:32:39 -07005319 end--;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005320
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 logflags = 0;
5322 if (ifp->if_flags & XFS_IFBROOT) {
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005323 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005324 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner92219c22020-03-10 17:52:53 -07005325 cur->bc_ino.flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 } else
5327 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005328
5329 if (isrt) {
5330 /*
5331 * Synchronize by locking the bitmap inode.
5332 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005333 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005334 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005335 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5336 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005337 }
5338
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 extno = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005340 while (end != (xfs_fileoff_t)-1 && end >= start &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005341 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005343 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 * Just back up to the previous extent, if so.
5345 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005346 if (got.br_startoff > end &&
5347 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5348 done = true;
5349 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 }
5351 /*
5352 * Is the last block of this extent before the range
5353 * we're supposed to delete? If so, we're done.
5354 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005355 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005357 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 break;
5359 /*
5360 * Then deal with the (possibly delayed) allocated space
5361 * we found.
5362 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005364 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005365
5366 /*
5367 * Make sure we don't touch multiple AGF headers out of order
5368 * in a single transaction, as that could cause AB-BA deadlocks.
5369 */
Omar Sandoval69ffe592019-11-26 16:58:08 -08005370 if (!wasdel && !isrt) {
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005371 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5372 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5373 break;
5374 prev_agno = agno;
5375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 if (got.br_startoff < start) {
5377 del.br_startoff = start;
5378 del.br_blockcount -= start - got.br_startoff;
5379 if (!wasdel)
5380 del.br_startblock += start - got.br_startoff;
5381 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005382 if (del.br_startoff + del.br_blockcount > end + 1)
5383 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005384
5385 /* How much can we safely unmap? */
5386 if (max_len < del.br_blockcount) {
5387 del.br_startoff += del.br_blockcount - max_len;
5388 if (!wasdel)
5389 del.br_startblock += del.br_blockcount - max_len;
5390 del.br_blockcount = max_len;
5391 }
5392
Dave Chinner0703a8e2018-06-08 09:54:22 -07005393 if (!isrt)
5394 goto delete;
5395
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 sum = del.br_startblock + del.br_blockcount;
Dave Chinner0703a8e2018-06-08 09:54:22 -07005397 div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod);
5398 if (mod) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 /*
5400 * Realtime extent not lined up at the end.
5401 * The extent could have been split into written
5402 * and unwritten pieces, or we could just be
5403 * unmapping part of it. But we can't really
5404 * get rid of part of a realtime extent.
5405 */
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11005406 if (del.br_state == XFS_EXT_UNWRITTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 /*
5408 * This piece is unwritten, or we're not
5409 * using unwritten extents. Skip over it.
5410 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005411 ASSERT(end >= mod);
5412 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 del.br_blockcount : mod;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005414 if (end < got.br_startoff &&
5415 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5416 done = true;
5417 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 }
5419 continue;
5420 }
5421 /*
5422 * It's written, turn it unwritten.
5423 * This is better than zeroing it.
5424 */
5425 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005426 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 /*
5428 * If this spans a realtime extent boundary,
5429 * chop it back to the start of the one we end at.
5430 */
5431 if (del.br_blockcount > mod) {
5432 del.br_startoff += del.br_blockcount - mod;
5433 del.br_startblock += del.br_blockcount - mod;
5434 del.br_blockcount = mod;
5435 }
5436 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005437 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005438 whichfork, &icur, &cur, &del,
Brian Foster92f9da32018-07-11 22:26:28 -07005439 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 if (error)
5441 goto error0;
5442 goto nodelete;
5443 }
Dave Chinner0703a8e2018-06-08 09:54:22 -07005444 div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod);
5445 if (mod) {
Omar Sandoval0c4da702019-11-26 16:58:07 -08005446 xfs_extlen_t off = mp->m_sb.sb_rextsize - mod;
5447
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 /*
5449 * Realtime extent is lined up at the end but not
5450 * at the front. We'll get rid of full extents if
5451 * we can.
5452 */
Omar Sandoval0c4da702019-11-26 16:58:07 -08005453 if (del.br_blockcount > off) {
5454 del.br_blockcount -= off;
5455 del.br_startoff += off;
5456 del.br_startblock += off;
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11005457 } else if (del.br_startoff == start &&
5458 (del.br_state == XFS_EXT_UNWRITTEN ||
5459 tp->t_blk_res == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 /*
5461 * Can't make it unwritten. There isn't
5462 * a full extent here so just skip it.
5463 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005464 ASSERT(end >= del.br_blockcount);
5465 end -= del.br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005466 if (got.br_startoff > end &&
5467 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5468 done = true;
5469 break;
5470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 continue;
5472 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005473 struct xfs_bmbt_irec prev;
Omar Sandoval0c4da702019-11-26 16:58:07 -08005474 xfs_fileoff_t unwrite_start;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005475
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 /*
5477 * This one is already unwritten.
5478 * It must have a written left neighbor.
5479 * Unwrite the killed part of that one and
5480 * try again.
5481 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005482 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5483 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005485 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 ASSERT(del.br_startblock ==
5487 prev.br_startblock + prev.br_blockcount);
Omar Sandoval0c4da702019-11-26 16:58:07 -08005488 unwrite_start = max3(start,
5489 del.br_startoff - mod,
5490 prev.br_startoff);
5491 mod = unwrite_start - prev.br_startoff;
5492 prev.br_startoff = unwrite_start;
5493 prev.br_startblock += mod;
5494 prev.br_blockcount -= mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005496 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005497 ip, whichfork, &icur, &cur,
Brian Foster92f9da32018-07-11 22:26:28 -07005498 &prev, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 if (error)
5500 goto error0;
5501 goto nodelete;
5502 } else {
5503 ASSERT(del.br_state == XFS_EXT_NORM);
5504 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005505 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005506 ip, whichfork, &icur, &cur,
Brian Foster92f9da32018-07-11 22:26:28 -07005507 &del, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 if (error)
5509 goto error0;
5510 goto nodelete;
5511 }
5512 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005513
Dave Chinner0703a8e2018-06-08 09:54:22 -07005514delete:
Brian Fosterb2706a02016-03-15 11:42:46 +11005515 if (wasdel) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005516 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005517 &got, &del);
5518 } else {
Brian Foster81ba8f32018-07-11 22:26:16 -07005519 error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5520 &del, &tmp_logflags, whichfork,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005521 flags);
5522 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005523 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005524
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 if (error)
5526 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005527
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005528 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005529 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 /*
5532 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005534 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005535 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5536 (got.br_startoff > end &&
5537 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5538 done = true;
5539 break;
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 extno++;
5542 }
5543 }
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005544 if (done || end == (xfs_fileoff_t)-1 || end < start)
Darrick J. Wong44535932016-10-03 09:11:29 -07005545 *rlen = 0;
5546 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005547 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005548
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 /*
5550 * Convert to a btree if necessary.
5551 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005552 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07005554 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5555 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 logflags |= tmp_logflags;
Christoph Hellwigb101e332019-02-15 08:02:47 -08005557 } else {
5558 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 }
Christoph Hellwigb101e332019-02-15 08:02:47 -08005561
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562error0:
5563 /*
5564 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005565 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005567 if ((logflags & xfs_ilog_fext(whichfork)) &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005568 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005569 logflags &= ~xfs_ilog_fext(whichfork);
5570 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005571 ifp->if_format != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005572 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 /*
5574 * Log inode even in the error case, if the transaction
5575 * is dirty we'll need to shut down the filesystem.
5576 */
5577 if (logflags)
5578 xfs_trans_log_inode(tp, ip, logflags);
5579 if (cur) {
Brian Fostercf612de2018-07-11 22:26:29 -07005580 if (!error)
Dave Chinner92219c22020-03-10 17:52:53 -07005581 cur->bc_ino.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07005582 xfs_btree_del_cursor(cur, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 }
5584 return error;
5585}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005586
Darrick J. Wong44535932016-10-03 09:11:29 -07005587/* Unmap a range of a file. */
5588int
5589xfs_bunmapi(
5590 xfs_trans_t *tp,
5591 struct xfs_inode *ip,
5592 xfs_fileoff_t bno,
5593 xfs_filblks_t len,
5594 int flags,
5595 xfs_extnum_t nexts,
Darrick J. Wong44535932016-10-03 09:11:29 -07005596 int *done)
5597{
5598 int error;
5599
Brian Foster2af52842018-07-11 22:26:25 -07005600 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
Darrick J. Wong44535932016-10-03 09:11:29 -07005601 *done = (len == 0);
5602 return error;
5603}
5604
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005605/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005606 * Determine whether an extent shift can be accomplished by a merge with the
5607 * extent that precedes the target hole of the shift.
5608 */
5609STATIC bool
5610xfs_bmse_can_merge(
5611 struct xfs_bmbt_irec *left, /* preceding extent */
5612 struct xfs_bmbt_irec *got, /* current extent to shift */
5613 xfs_fileoff_t shift) /* shift fsb */
5614{
5615 xfs_fileoff_t startoff;
5616
5617 startoff = got->br_startoff - shift;
5618
5619 /*
5620 * The extent, once shifted, must be adjacent in-file and on-disk with
5621 * the preceding extent.
5622 */
5623 if ((left->br_startoff + left->br_blockcount != startoff) ||
5624 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5625 (left->br_state != got->br_state) ||
5626 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5627 return false;
5628
5629 return true;
5630}
5631
5632/*
5633 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5634 * hole in the file. If an extent shift would result in the extent being fully
5635 * adjacent to the extent that currently precedes the hole, we can merge with
5636 * the preceding extent rather than do the shift.
5637 *
5638 * This function assumes the caller has verified a shift-by-merge is possible
5639 * with the provided extents via xfs_bmse_can_merge().
5640 */
5641STATIC int
5642xfs_bmse_merge(
Brian Foster0f37d172018-08-01 07:20:34 -07005643 struct xfs_trans *tp,
Brian Fosterddb19e32014-09-23 15:38:09 +10005644 struct xfs_inode *ip,
5645 int whichfork,
5646 xfs_fileoff_t shift, /* shift fsb */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005647 struct xfs_iext_cursor *icur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005648 struct xfs_bmbt_irec *got, /* extent to shift */
5649 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005650 struct xfs_btree_cur *cur,
Brian Foster0f37d172018-08-01 07:20:34 -07005651 int *logflags) /* output */
Brian Fosterddb19e32014-09-23 15:38:09 +10005652{
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005653 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005654 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005655 xfs_filblks_t blockcount;
5656 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005657 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005658
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005659 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005660
5661 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5662 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005663 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005664
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005665 new = *left;
5666 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005667
5668 /*
5669 * Update the on-disk extent count, the btree if necessary and log the
5670 * inode.
5671 */
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005672 ifp->if_nextents--;
Brian Fosterddb19e32014-09-23 15:38:09 +10005673 *logflags |= XFS_ILOG_CORE;
5674 if (!cur) {
5675 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005676 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005677 }
5678
5679 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005680 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005681 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005682 return error;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005683 if (XFS_IS_CORRUPT(mp, i != 1))
5684 return -EFSCORRUPTED;
Brian Fosterddb19e32014-09-23 15:38:09 +10005685
5686 error = xfs_btree_delete(cur, &i);
5687 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005688 return error;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005689 if (XFS_IS_CORRUPT(mp, i != 1))
5690 return -EFSCORRUPTED;
Brian Fosterddb19e32014-09-23 15:38:09 +10005691
5692 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005693 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005694 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005695 return error;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005696 if (XFS_IS_CORRUPT(mp, i != 1))
5697 return -EFSCORRUPTED;
Brian Fosterddb19e32014-09-23 15:38:09 +10005698
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005699 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005700 if (error)
5701 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005702
Brian Fostere20e1742019-09-18 17:50:45 -07005703 /* change to extent format if required after extent removal */
5704 error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork);
5705 if (error)
5706 return error;
5707
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005708done:
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005709 xfs_iext_remove(ip, icur, 0);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005710 xfs_iext_prev(ifp, icur);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005711 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5712 &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005713
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005714 /* update reverse mapping. rmap functions merge the rmaps for us */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07005715 xfs_rmap_unmap_extent(tp, ip, whichfork, got);
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005716 memcpy(&new, got, sizeof(new));
5717 new.br_startoff = left->br_startoff + left->br_blockcount;
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07005718 xfs_rmap_map_extent(tp, ip, whichfork, &new);
5719 return 0;
Brian Fosterddb19e32014-09-23 15:38:09 +10005720}
5721
Christoph Hellwigbf806282017-10-19 11:07:34 -07005722static int
5723xfs_bmap_shift_update_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07005724 struct xfs_trans *tp,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005725 struct xfs_inode *ip,
5726 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005727 struct xfs_iext_cursor *icur,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005728 struct xfs_bmbt_irec *got,
5729 struct xfs_btree_cur *cur,
5730 int *logflags,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005731 xfs_fileoff_t startoff)
Brian Fostera979bdf2014-09-23 15:39:04 +10005732{
Christoph Hellwigbf806282017-10-19 11:07:34 -07005733 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005734 struct xfs_bmbt_irec prev = *got;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005735 int error, i;
Brian Fostera979bdf2014-09-23 15:39:04 +10005736
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005737 *logflags |= XFS_ILOG_CORE;
5738
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005739 got->br_startoff = startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005740
5741 if (cur) {
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005742 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005743 if (error)
5744 return error;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005745 if (XFS_IS_CORRUPT(mp, i != 1))
5746 return -EFSCORRUPTED;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005747
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005748 error = xfs_bmbt_update(cur, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005749 if (error)
5750 return error;
5751 } else {
5752 *logflags |= XFS_ILOG_DEXT;
5753 }
5754
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005755 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5756 got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005757
Darrick J. Wong9c194642016-08-03 12:16:05 +10005758 /* update reverse mapping */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07005759 xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
5760 xfs_rmap_map_extent(tp, ip, whichfork, got);
5761 return 0;
Brian Fostera979bdf2014-09-23 15:39:04 +10005762}
5763
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005764int
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005765xfs_bmap_collapse_extents(
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005766 struct xfs_trans *tp,
5767 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005768 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005769 xfs_fileoff_t offset_shift_fsb,
Brian Foster333f9502018-07-11 22:26:27 -07005770 bool *done)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005771{
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005772 int whichfork = XFS_DATA_FORK;
5773 struct xfs_mount *mp = ip->i_mount;
5774 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5775 struct xfs_btree_cur *cur = NULL;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005776 struct xfs_bmbt_irec got, prev;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005777 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005778 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005779 int error = 0;
5780 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005781
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005782 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
Darrick J. Wonga71895c2019-11-11 12:53:22 -08005783 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Dave Chinner24513372014-06-25 14:58:08 +10005784 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005785 }
5786
5787 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005788 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005789
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005790 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005791
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005792 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005793 error = xfs_iread_extents(tp, ip, whichfork);
5794 if (error)
5795 return error;
5796 }
5797
Brian Fosterddb19e32014-09-23 15:38:09 +10005798 if (ifp->if_flags & XFS_IFBROOT) {
5799 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner92219c22020-03-10 17:52:53 -07005800 cur->bc_ino.flags = 0;
Brian Fosterddb19e32014-09-23 15:38:09 +10005801 }
5802
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005803 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005804 *done = true;
5805 goto del_cursor;
5806 }
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005807 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5808 error = -EFSCORRUPTED;
5809 goto del_cursor;
5810 }
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005811
Christoph Hellwigbf806282017-10-19 11:07:34 -07005812 new_startoff = got.br_startoff - offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005813 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005814 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5815 error = -EINVAL;
5816 goto del_cursor;
5817 }
5818
Christoph Hellwigbf806282017-10-19 11:07:34 -07005819 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
Brian Foster0f37d172018-08-01 07:20:34 -07005820 error = xfs_bmse_merge(tp, ip, whichfork,
5821 offset_shift_fsb, &icur, &got, &prev,
5822 cur, &logflags);
Christoph Hellwigbf806282017-10-19 11:07:34 -07005823 if (error)
5824 goto del_cursor;
5825 goto done;
5826 }
5827 } else {
5828 if (got.br_startoff < offset_shift_fsb) {
5829 error = -EINVAL;
5830 goto del_cursor;
5831 }
5832 }
5833
Brian Foster0f37d172018-08-01 07:20:34 -07005834 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5835 cur, &logflags, new_startoff);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005836 if (error)
5837 goto del_cursor;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005838
Christoph Hellwig42630362017-11-03 10:34:41 -07005839done:
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005840 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5841 *done = true;
5842 goto del_cursor;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005843 }
5844
Christoph Hellwigbf806282017-10-19 11:07:34 -07005845 *next_fsb = got.br_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005846del_cursor:
5847 if (cur)
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07005848 xfs_btree_del_cursor(cur, error);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005849 if (logflags)
5850 xfs_trans_log_inode(tp, ip, logflags);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005851 return error;
5852}
5853
Darrick J. Wongf62cb482018-06-21 23:26:57 -07005854/* Make sure we won't be right-shifting an extent past the maximum bound. */
5855int
5856xfs_bmap_can_insert_extents(
5857 struct xfs_inode *ip,
5858 xfs_fileoff_t off,
5859 xfs_fileoff_t shift)
5860{
5861 struct xfs_bmbt_irec got;
5862 int is_empty;
5863 int error = 0;
5864
5865 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5866
5867 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5868 return -EIO;
5869
5870 xfs_ilock(ip, XFS_ILOCK_EXCL);
5871 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
5872 if (!error && !is_empty && got.br_startoff >= off &&
5873 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
5874 error = -EINVAL;
5875 xfs_iunlock(ip, XFS_ILOCK_EXCL);
5876
5877 return error;
5878}
5879
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005880int
5881xfs_bmap_insert_extents(
5882 struct xfs_trans *tp,
5883 struct xfs_inode *ip,
5884 xfs_fileoff_t *next_fsb,
5885 xfs_fileoff_t offset_shift_fsb,
5886 bool *done,
Brian Foster333f9502018-07-11 22:26:27 -07005887 xfs_fileoff_t stop_fsb)
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005888{
5889 int whichfork = XFS_DATA_FORK;
5890 struct xfs_mount *mp = ip->i_mount;
5891 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5892 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005893 struct xfs_bmbt_irec got, next;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005894 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005895 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005896 int error = 0;
5897 int logflags = 0;
5898
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005899 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
Darrick J. Wonga71895c2019-11-11 12:53:22 -08005900 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005901 return -EFSCORRUPTED;
5902 }
5903
5904 if (XFS_FORCED_SHUTDOWN(mp))
5905 return -EIO;
5906
5907 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5908
5909 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5910 error = xfs_iread_extents(tp, ip, whichfork);
5911 if (error)
5912 return error;
5913 }
5914
5915 if (ifp->if_flags & XFS_IFBROOT) {
5916 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner92219c22020-03-10 17:52:53 -07005917 cur->bc_ino.flags = 0;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005918 }
5919
Namjae Jeona904b1c2015-03-25 15:08:56 +11005920 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005921 xfs_iext_last(ifp, &icur);
5922 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005923 stop_fsb > got.br_startoff) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005924 *done = true;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005925 goto del_cursor;
5926 }
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005927 } else {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005928 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005929 *done = true;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005930 goto del_cursor;
5931 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005932 }
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005933 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5934 error = -EFSCORRUPTED;
5935 goto del_cursor;
5936 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005937
Brian Fosterd0c22042019-12-11 13:18:38 -08005938 if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) {
Darrick J. Wongc2414ad2019-10-28 16:12:34 -07005939 error = -EFSCORRUPTED;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005940 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005941 }
5942
Christoph Hellwigbf806282017-10-19 11:07:34 -07005943 new_startoff = got.br_startoff + offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005944 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005945 if (new_startoff + got.br_blockcount > next.br_startoff) {
5946 error = -EINVAL;
5947 goto del_cursor;
5948 }
5949
5950 /*
5951 * Unlike a left shift (which involves a hole punch), a right
5952 * shift does not modify extent neighbors in any way. We should
5953 * never find mergeable extents in this scenario. Check anyways
5954 * and warn if we encounter two extents that could be one.
5955 */
5956 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5957 WARN_ON_ONCE(1);
5958 }
5959
Brian Foster0f37d172018-08-01 07:20:34 -07005960 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5961 cur, &logflags, new_startoff);
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005962 if (error)
5963 goto del_cursor;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005964
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005965 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005966 stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005967 *done = true;
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005968 goto del_cursor;
5969 }
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005970
5971 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005972del_cursor:
5973 if (cur)
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07005974 xfs_btree_del_cursor(cur, error);
Brian Fosterca446d82014-09-02 12:12:53 +10005975 if (logflags)
5976 xfs_trans_log_inode(tp, ip, logflags);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005977 return error;
5978}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005979
5980/*
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005981 * Splits an extent into two extents at split_fsb block such that it is the
5982 * first block of the current_ext. @ext is a target extent to be split.
5983 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5984 * hole or the first block of extents, just return 0.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005985 */
Brian Fosterb73df172020-02-26 09:43:15 -08005986int
5987xfs_bmap_split_extent(
Namjae Jeona904b1c2015-03-25 15:08:56 +11005988 struct xfs_trans *tp,
5989 struct xfs_inode *ip,
Brian Foster4b77a082018-07-11 22:26:27 -07005990 xfs_fileoff_t split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005991{
5992 int whichfork = XFS_DATA_FORK;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005993 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005994 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005995 struct xfs_bmbt_irec got;
5996 struct xfs_bmbt_irec new; /* split extent */
5997 struct xfs_mount *mp = ip->i_mount;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005998 xfs_fsblock_t gotblkcnt; /* new block count for got */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005999 struct xfs_iext_cursor icur;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006000 int error = 0;
6001 int logflags = 0;
6002 int i = 0;
6003
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07006004 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
Darrick J. Wonga71895c2019-11-11 12:53:22 -08006005 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006006 return -EFSCORRUPTED;
6007 }
6008
6009 if (XFS_FORCED_SHUTDOWN(mp))
6010 return -EIO;
6011
Namjae Jeona904b1c2015-03-25 15:08:56 +11006012 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6013 /* Read in all the extents */
6014 error = xfs_iread_extents(tp, ip, whichfork);
6015 if (error)
6016 return error;
6017 }
6018
6019 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006020 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11006021 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006022 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006023 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006024 return 0;
6025
6026 gotblkcnt = split_fsb - got.br_startoff;
6027 new.br_startoff = split_fsb;
6028 new.br_startblock = got.br_startblock + gotblkcnt;
6029 new.br_blockcount = got.br_blockcount - gotblkcnt;
6030 new.br_state = got.br_state;
6031
6032 if (ifp->if_flags & XFS_IFBROOT) {
6033 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner92219c22020-03-10 17:52:53 -07006034 cur->bc_ino.flags = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07006035 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006036 if (error)
6037 goto del_cursor;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08006038 if (XFS_IS_CORRUPT(mp, i != 1)) {
6039 error = -EFSCORRUPTED;
6040 goto del_cursor;
6041 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006042 }
6043
Namjae Jeona904b1c2015-03-25 15:08:56 +11006044 got.br_blockcount = gotblkcnt;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006045 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
6046 &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006047
6048 logflags = XFS_ILOG_CORE;
6049 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07006050 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006051 if (error)
6052 goto del_cursor;
6053 } else
6054 logflags |= XFS_ILOG_DEXT;
6055
6056 /* Add new extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006057 xfs_iext_next(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07006058 xfs_iext_insert(ip, &icur, &new, 0);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07006059 ifp->if_nextents++;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006060
6061 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07006062 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006063 if (error)
6064 goto del_cursor;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08006065 if (XFS_IS_CORRUPT(mp, i != 0)) {
6066 error = -EFSCORRUPTED;
6067 goto del_cursor;
6068 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006069 error = xfs_btree_insert(cur, &i);
6070 if (error)
6071 goto del_cursor;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08006072 if (XFS_IS_CORRUPT(mp, i != 1)) {
6073 error = -EFSCORRUPTED;
6074 goto del_cursor;
6075 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006076 }
6077
6078 /*
6079 * Convert to a btree if necessary.
6080 */
6081 if (xfs_bmap_needs_btree(ip, whichfork)) {
6082 int tmp_logflags; /* partial log flag return val */
6083
6084 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07006085 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
6086 &tmp_logflags, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006087 logflags |= tmp_logflags;
6088 }
6089
6090del_cursor:
6091 if (cur) {
Dave Chinner92219c22020-03-10 17:52:53 -07006092 cur->bc_ino.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07006093 xfs_btree_del_cursor(cur, error);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006094 }
6095
6096 if (logflags)
6097 xfs_trans_log_inode(tp, ip, logflags);
6098 return error;
6099}
6100
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006101/* Deferred mapping is only for real extents in the data fork. */
6102static bool
6103xfs_bmap_is_update_needed(
6104 struct xfs_bmbt_irec *bmap)
6105{
6106 return bmap->br_startblock != HOLESTARTBLOCK &&
6107 bmap->br_startblock != DELAYSTARTBLOCK;
6108}
6109
6110/* Record a bmap intent. */
6111static int
6112__xfs_bmap_add(
Brian Foster0f37d172018-08-01 07:20:34 -07006113 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006114 enum xfs_bmap_intent_type type,
6115 struct xfs_inode *ip,
6116 int whichfork,
6117 struct xfs_bmbt_irec *bmap)
6118{
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006119 struct xfs_bmap_intent *bi;
6120
Brian Foster0f37d172018-08-01 07:20:34 -07006121 trace_xfs_bmap_defer(tp->t_mountp,
6122 XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock),
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006123 type,
Brian Foster0f37d172018-08-01 07:20:34 -07006124 XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock),
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006125 ip->i_ino, whichfork,
6126 bmap->br_startoff,
6127 bmap->br_blockcount,
6128 bmap->br_state);
6129
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07006130 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_NOFS);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006131 INIT_LIST_HEAD(&bi->bi_list);
6132 bi->bi_type = type;
6133 bi->bi_owner = ip;
6134 bi->bi_whichfork = whichfork;
6135 bi->bi_bmap = *bmap;
6136
Brian Foster0f37d172018-08-01 07:20:34 -07006137 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006138 return 0;
6139}
6140
6141/* Map an extent into a file. */
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006142void
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006143xfs_bmap_map_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07006144 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006145 struct xfs_inode *ip,
6146 struct xfs_bmbt_irec *PREV)
6147{
6148 if (!xfs_bmap_is_update_needed(PREV))
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006149 return;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006150
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006151 __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, XFS_DATA_FORK, PREV);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006152}
6153
6154/* Unmap an extent out of a file. */
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006155void
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006156xfs_bmap_unmap_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07006157 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006158 struct xfs_inode *ip,
6159 struct xfs_bmbt_irec *PREV)
6160{
6161 if (!xfs_bmap_is_update_needed(PREV))
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006162 return;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006163
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006164 __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, XFS_DATA_FORK, PREV);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006165}
6166
6167/*
6168 * Process one of the deferred bmap operations. We pass back the
6169 * btree cursor to maintain our lock on the bmapbt between calls.
6170 */
6171int
6172xfs_bmap_finish_one(
6173 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006174 struct xfs_inode *ip,
6175 enum xfs_bmap_intent_type type,
6176 int whichfork,
6177 xfs_fileoff_t startoff,
6178 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006179 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006180 xfs_exntst_t state)
6181{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006182 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006183
Brian Foster37283792018-07-11 22:26:23 -07006184 ASSERT(tp->t_firstblock == NULLFSBLOCK);
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006185
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006186 trace_xfs_bmap_deferred(tp->t_mountp,
6187 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6188 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006189 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006190
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006191 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006192 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006193
6194 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006195 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006196 return -EIO;
6197
6198 switch (type) {
6199 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006200 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Brian Fosterff3edf22018-07-11 22:26:14 -07006201 startblock, 0);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006202 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006203 break;
6204 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006205 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
Brian Foster2af52842018-07-11 22:26:25 -07006206 XFS_BMAPI_REMAP, 1);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006207 break;
6208 default:
6209 ASSERT(0);
6210 error = -EFSCORRUPTED;
6211 }
6212
6213 return error;
6214}
Darrick J. Wong30b09842018-03-23 10:06:52 -07006215
6216/* Check that an inode's extent does not have invalid flags or bad ranges. */
6217xfs_failaddr_t
6218xfs_bmap_validate_extent(
6219 struct xfs_inode *ip,
6220 int whichfork,
6221 struct xfs_bmbt_irec *irec)
6222{
6223 struct xfs_mount *mp = ip->i_mount;
6224 xfs_fsblock_t endfsb;
6225 bool isrt;
6226
6227 isrt = XFS_IS_REALTIME_INODE(ip);
6228 endfsb = irec->br_startblock + irec->br_blockcount - 1;
6229 if (isrt) {
6230 if (!xfs_verify_rtbno(mp, irec->br_startblock))
6231 return __this_address;
6232 if (!xfs_verify_rtbno(mp, endfsb))
6233 return __this_address;
6234 } else {
6235 if (!xfs_verify_fsbno(mp, irec->br_startblock))
6236 return __this_address;
6237 if (!xfs_verify_fsbno(mp, endfsb))
6238 return __this_address;
6239 if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) !=
6240 XFS_FSB_TO_AGNO(mp, endfsb))
6241 return __this_address;
6242 }
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11006243 if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6244 return __this_address;
Darrick J. Wong30b09842018-03-23 10:06:52 -07006245 return NULL;
6246}