blob: fd5c0d669d0d7a0544a7179c6b198308f29a787f [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);
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07001102 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, 0);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001103 ip->i_afp->if_format = XFS_DINODE_FMT_EXTENTS;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001104 ip->i_afp->if_flags = XFS_IFEXTENTS;
1105 logflags = 0;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001106 switch (ip->i_df.if_format) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001107 case XFS_DINODE_FMT_LOCAL:
Brian Foster825d75cd2018-07-11 22:26:21 -07001108 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001109 break;
1110 case XFS_DINODE_FMT_EXTENTS:
Brian Foster825d75cd2018-07-11 22:26:21 -07001111 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001112 break;
1113 case XFS_DINODE_FMT_BTREE:
Brian Foster825d75cd2018-07-11 22:26:21 -07001114 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001115 break;
1116 default:
1117 error = 0;
1118 break;
1119 }
1120 if (logflags)
1121 xfs_trans_log_inode(tp, ip, logflags);
1122 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07001123 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001124 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1125 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001126 bool log_sb = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001127
1128 spin_lock(&mp->m_sb_lock);
1129 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1130 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001131 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001132 }
1133 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1134 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001135 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001136 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001137 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001138 if (log_sb)
1139 xfs_log_sb(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001140 }
1141
Christoph Hellwig70393312015-06-04 13:48:08 +10001142 error = xfs_trans_commit(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001143 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001144 return error;
1145
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001146trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001147 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001148 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001149 return error;
1150}
1151
1152/*
1153 * Internal and external extent tree search functions.
1154 */
1155
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001156struct xfs_iread_state {
1157 struct xfs_iext_cursor icur;
1158 xfs_extnum_t loaded;
1159};
1160
1161/* Stuff every bmbt record from this block into the incore extent map. */
1162static int
1163xfs_iread_bmbt_block(
1164 struct xfs_btree_cur *cur,
1165 int level,
1166 void *priv)
1167{
1168 struct xfs_iread_state *ir = priv;
1169 struct xfs_mount *mp = cur->bc_mp;
Dave Chinner92219c22020-03-10 17:52:53 -07001170 struct xfs_inode *ip = cur->bc_ino.ip;
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001171 struct xfs_btree_block *block;
1172 struct xfs_buf *bp;
1173 struct xfs_bmbt_rec *frp;
1174 xfs_extnum_t num_recs;
1175 xfs_extnum_t j;
Dave Chinner92219c22020-03-10 17:52:53 -07001176 int whichfork = cur->bc_ino.whichfork;
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001177 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001178
1179 block = xfs_btree_get_block(cur, level, &bp);
1180
1181 /* Abort if we find more records than nextents. */
1182 num_recs = xfs_btree_get_numrecs(block);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001183 if (unlikely(ir->loaded + num_recs > ifp->if_nextents)) {
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001184 xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).",
1185 (unsigned long long)ip->i_ino);
1186 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block,
1187 sizeof(*block), __this_address);
1188 return -EFSCORRUPTED;
1189 }
1190
1191 /* Copy records into the incore cache. */
1192 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1193 for (j = 0; j < num_recs; j++, frp++, ir->loaded++) {
1194 struct xfs_bmbt_irec new;
1195 xfs_failaddr_t fa;
1196
1197 xfs_bmbt_disk_get_all(frp, &new);
1198 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1199 if (fa) {
1200 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1201 "xfs_iread_extents(2)", frp,
1202 sizeof(*frp), fa);
1203 return -EFSCORRUPTED;
1204 }
1205 xfs_iext_insert(ip, &ir->icur, &new,
1206 xfs_bmap_fork_to_state(whichfork));
1207 trace_xfs_read_extent(ip, &ir->icur,
1208 xfs_bmap_fork_to_state(whichfork), _THIS_IP_);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001209 xfs_iext_next(ifp, &ir->icur);
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001210 }
1211
1212 return 0;
1213}
1214
Dave Chinner9e5987a72013-02-25 12:31:26 +11001215/*
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001216 * Read in extents from a btree-format inode.
Dave Chinner9e5987a72013-02-25 12:31:26 +11001217 */
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001218int
1219xfs_iread_extents(
1220 struct xfs_trans *tp,
1221 struct xfs_inode *ip,
1222 int whichfork)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001223{
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001224 struct xfs_iread_state ir;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001225 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001226 struct xfs_mount *mp = ip->i_mount;
1227 struct xfs_btree_cur *cur;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001228 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001229
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001230 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1231
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001232 if (XFS_IS_CORRUPT(mp, ifp->if_format != XFS_DINODE_FMT_BTREE)) {
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001233 error = -EFSCORRUPTED;
1234 goto out;
1235 }
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001236
1237 ir.loaded = 0;
1238 xfs_iext_first(ifp, &ir.icur);
1239 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1240 error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block,
1241 XFS_BTREE_VISIT_RECORDS, &ir);
1242 xfs_btree_del_cursor(cur, error);
1243 if (error)
1244 goto out;
1245
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001246 if (XFS_IS_CORRUPT(mp, ir.loaded != ifp->if_nextents)) {
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001247 error = -EFSCORRUPTED;
1248 goto out;
1249 }
1250 ASSERT(ir.loaded == xfs_iext_count(ifp));
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001251
1252 ifp->if_flags |= XFS_IFEXTENTS;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001253 return 0;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001254out:
1255 xfs_iext_destroy(ifp);
1256 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001257}
1258
Dave Chinner9e5987a72013-02-25 12:31:26 +11001259/*
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001260 * Returns the relative block number of the first unused block(s) in the given
1261 * fork with at least "len" logically contiguous blocks free. This is the
1262 * lowest-address hole if the fork has holes, else the first block past the end
1263 * of fork. Return 0 if the fork is currently local (in-inode).
Dave Chinner9e5987a72013-02-25 12:31:26 +11001264 */
1265int /* error */
1266xfs_bmap_first_unused(
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001267 struct xfs_trans *tp, /* transaction pointer */
1268 struct xfs_inode *ip, /* incore inode */
1269 xfs_extlen_t len, /* size of hole to find */
1270 xfs_fileoff_t *first_unused, /* unused block */
1271 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001272{
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001273 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1274 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001275 struct xfs_iext_cursor icur;
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001276 xfs_fileoff_t lastaddr = 0;
1277 xfs_fileoff_t lowest, max;
1278 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001279
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001280 if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001281 *first_unused = 0;
1282 return 0;
1283 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001284
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001285 ASSERT(xfs_ifork_has_extents(ifp));
1286
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001287 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1288 error = xfs_iread_extents(tp, ip, whichfork);
1289 if (error)
1290 return error;
1291 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001292
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001293 lowest = max = *first_unused;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001294 for_each_xfs_iext(ifp, &icur, &got) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001295 /*
1296 * See if the hole before this extent will work.
1297 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001298 if (got.br_startoff >= lowest + len &&
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001299 got.br_startoff - max >= len)
1300 break;
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001301 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001302 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1303 }
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001304
Dave Chinner9e5987a72013-02-25 12:31:26 +11001305 *first_unused = max;
1306 return 0;
1307}
1308
1309/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001310 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a72013-02-25 12:31:26 +11001311 * last_block (input value) in the file.
1312 * This is not based on i_size, it is based on the extent records.
1313 * Returns 0 for local files, as they do not have extent records.
1314 */
1315int /* error */
1316xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001317 struct xfs_trans *tp, /* transaction pointer */
1318 struct xfs_inode *ip, /* incore inode */
1319 xfs_fileoff_t *last_block, /* last block */
1320 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001321{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001322 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1323 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001324 struct xfs_iext_cursor icur;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001325 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001326
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001327 switch (ifp->if_format) {
Christoph Hellwig86685f72016-11-24 11:39:38 +11001328 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001329 *last_block = 0;
1330 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001331 case XFS_DINODE_FMT_BTREE:
1332 case XFS_DINODE_FMT_EXTENTS:
1333 break;
1334 default:
Darrick J. Wonga5155b82019-11-02 09:40:53 -07001335 ASSERT(0);
Darrick J. Wongc2414ad2019-10-28 16:12:34 -07001336 return -EFSCORRUPTED;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001337 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001338
1339 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1340 error = xfs_iread_extents(tp, ip, whichfork);
1341 if (error)
1342 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001343 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001344
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001345 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
Christoph Hellwigdc560152017-10-23 16:32:39 -07001346 *last_block = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001347 return 0;
1348}
1349
Dave Chinner68988112013-08-12 20:49:42 +10001350int
Dave Chinner9e5987a72013-02-25 12:31:26 +11001351xfs_bmap_last_extent(
1352 struct xfs_trans *tp,
1353 struct xfs_inode *ip,
1354 int whichfork,
1355 struct xfs_bmbt_irec *rec,
1356 int *is_empty)
1357{
1358 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001359 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001360 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001361
1362 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1363 error = xfs_iread_extents(tp, ip, whichfork);
1364 if (error)
1365 return error;
1366 }
1367
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001368 xfs_iext_last(ifp, &icur);
1369 if (!xfs_iext_get_extent(ifp, &icur, rec))
Dave Chinner9e5987a72013-02-25 12:31:26 +11001370 *is_empty = 1;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001371 else
1372 *is_empty = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001373 return 0;
1374}
1375
1376/*
1377 * Check the last inode extent to determine whether this allocation will result
1378 * in blocks being allocated at the end of the file. When we allocate new data
1379 * blocks at the end of the file which do not start at the previous data block,
1380 * we will try to align the new blocks at stripe unit boundaries.
1381 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001382 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a72013-02-25 12:31:26 +11001383 * at, or past the EOF.
1384 */
1385STATIC int
1386xfs_bmap_isaeof(
1387 struct xfs_bmalloca *bma,
1388 int whichfork)
1389{
1390 struct xfs_bmbt_irec rec;
1391 int is_empty;
1392 int error;
1393
Thomas Meyer749f24f2017-10-09 11:38:54 -07001394 bma->aeof = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001395 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1396 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001397 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001398 return error;
1399
Dave Chinner6e708bc2013-11-22 10:41:16 +11001400 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001401 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001402 return 0;
1403 }
1404
Dave Chinner9e5987a72013-02-25 12:31:26 +11001405 /*
1406 * Check if we are allocation or past the last extent, or at least into
1407 * the last delayed allocated extent.
1408 */
1409 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1410 (bma->offset >= rec.br_startoff &&
1411 isnullstartblock(rec.br_startblock));
1412 return 0;
1413}
1414
1415/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001416 * Returns the file-relative block number of the first block past eof in
1417 * the file. This is not based on i_size, it is based on the extent records.
1418 * Returns 0 for local files, as they do not have extent records.
1419 */
1420int
1421xfs_bmap_last_offset(
Dave Chinner9e5987a72013-02-25 12:31:26 +11001422 struct xfs_inode *ip,
1423 xfs_fileoff_t *last_block,
1424 int whichfork)
1425{
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001426 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001427 struct xfs_bmbt_irec rec;
1428 int is_empty;
1429 int error;
1430
1431 *last_block = 0;
1432
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001433 if (ifp->if_format == XFS_DINODE_FMT_LOCAL)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001434 return 0;
1435
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001436 if (XFS_IS_CORRUPT(ip->i_mount, !xfs_ifork_has_extents(ifp)))
Darrick J. Wongc2414ad2019-10-28 16:12:34 -07001437 return -EFSCORRUPTED;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001438
1439 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1440 if (error || is_empty)
1441 return error;
1442
1443 *last_block = rec.br_startoff + rec.br_blockcount;
1444 return 0;
1445}
1446
1447/*
1448 * Returns whether the selected fork of the inode has exactly one
1449 * block or not. For the data fork we check this matches di_size,
1450 * implying the file's range is 0..bsize-1.
1451 */
1452int /* 1=>1 block, 0=>otherwise */
1453xfs_bmap_one_block(
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001454 struct xfs_inode *ip, /* incore inode */
1455 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001456{
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001457 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1458 int rval; /* return value */
1459 struct xfs_bmbt_irec s; /* internal version of extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001460 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001461
1462#ifndef DEBUG
1463 if (whichfork == XFS_DATA_FORK)
1464 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1465#endif /* !DEBUG */
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001466 if (ifp->if_nextents != 1)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001467 return 0;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07001468 if (ifp->if_format != XFS_DINODE_FMT_EXTENTS)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001469 return 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001470 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001471 xfs_iext_first(ifp, &icur);
1472 xfs_iext_get_extent(ifp, &icur, &s);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001473 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1474 if (rval && whichfork == XFS_DATA_FORK)
1475 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1476 return rval;
1477}
1478
1479/*
1480 * Extent tree manipulation functions used during allocation.
1481 */
1482
1483/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001484 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 */
1486STATIC int /* error */
1487xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001488 struct xfs_bmalloca *bma,
1489 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001491 struct xfs_mount *mp = bma->ip->i_mount;
1492 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001493 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 int i; /* temp state */
1496 xfs_fileoff_t new_endoff; /* end offset of new entry */
1497 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1498 /* left is 0, right is 1, prev is 2 */
1499 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07001500 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001501 xfs_filblks_t da_new; /* new count del alloc blocks used */
1502 xfs_filblks_t da_old; /* old count del alloc blocks used */
1503 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 int tmp_rval; /* partial logging flags */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001505 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Darrick J. Wong60b49842016-10-03 09:11:34 -07001507 ASSERT(whichfork != XFS_ATTR_FORK);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001508 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001509 ASSERT(!bma->cur ||
Dave Chinner8ef54792020-03-10 17:54:38 -07001510 (bma->cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001511
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001512 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514#define LEFT r[0]
1515#define RIGHT r[1]
1516#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 /*
1519 * Set up a bunch of variables to make the tests simpler.
1520 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001521 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001523 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 ASSERT(PREV.br_startoff <= new->br_startoff);
1525 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001526
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001527 da_old = startblockval(PREV.br_startblock);
1528 da_new = 0;
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 /*
1531 * Set flags determining what part of the previous delayed allocation
1532 * extent is being replaced by a real allocation.
1533 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001534 if (PREV.br_startoff == new->br_startoff)
1535 state |= BMAP_LEFT_FILLING;
1536 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1537 state |= BMAP_RIGHT_FILLING;
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 /*
1540 * Check and set flags if this segment has a left neighbor.
1541 * Don't set contiguous if the combined extent would be too large.
1542 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001543 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001544 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001545 if (isnullstartblock(LEFT.br_startblock))
1546 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001548
1549 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1550 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1551 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1552 LEFT.br_state == new->br_state &&
1553 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1554 state |= BMAP_LEFT_CONTIG;
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 /*
1557 * Check and set flags if this segment has a right neighbor.
1558 * Don't set contiguous if the combined extent would be too large.
1559 * Also check for all-three-contiguous being too large.
1560 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001561 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001562 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001563 if (isnullstartblock(RIGHT.br_startblock))
1564 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001566
1567 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1568 new_endoff == RIGHT.br_startoff &&
1569 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1570 new->br_state == RIGHT.br_state &&
1571 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1572 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1573 BMAP_RIGHT_FILLING)) !=
1574 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1575 BMAP_RIGHT_FILLING) ||
1576 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1577 <= MAXEXTLEN))
1578 state |= BMAP_RIGHT_CONTIG;
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 error = 0;
1581 /*
1582 * Switch out based on the FILLING and CONTIG state bits.
1583 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001584 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1585 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1586 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1587 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 /*
1589 * Filling in all of a previously delayed allocation extent.
1590 * The left and right neighbors are both contiguous with new.
1591 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001592 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001593
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001594 xfs_iext_remove(bma->ip, &bma->icur, state);
1595 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001596 xfs_iext_prev(ifp, &bma->icur);
1597 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001598 ifp->if_nextents--;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001599
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001600 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1602 else {
1603 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001604 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001605 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001607 if (XFS_IS_CORRUPT(mp, i != 1)) {
1608 error = -EFSCORRUPTED;
1609 goto done;
1610 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001611 error = xfs_btree_delete(bma->cur, &i);
1612 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001614 if (XFS_IS_CORRUPT(mp, i != 1)) {
1615 error = -EFSCORRUPTED;
1616 goto done;
1617 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001618 error = xfs_btree_decrement(bma->cur, 0, &i);
1619 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001621 if (XFS_IS_CORRUPT(mp, i != 1)) {
1622 error = -EFSCORRUPTED;
1623 goto done;
1624 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001625 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001626 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 goto done;
1628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 break;
1630
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001631 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 /*
1633 * Filling in all of a previously delayed allocation extent.
1634 * The left neighbor is contiguous, the right is not.
1635 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001636 old = LEFT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001637 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001638
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001639 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001640 xfs_iext_prev(ifp, &bma->icur);
1641 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001642
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001643 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 rval = XFS_ILOG_DEXT;
1645 else {
1646 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001647 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001648 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001650 if (XFS_IS_CORRUPT(mp, i != 1)) {
1651 error = -EFSCORRUPTED;
1652 goto done;
1653 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001654 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001655 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 goto done;
1657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 break;
1659
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001660 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 /*
1662 * Filling in all of a previously delayed allocation extent.
Dave Chinner9230a0b2018-11-19 22:50:08 -08001663 * The right neighbor is contiguous, the left is not. Take care
1664 * with delay -> unwritten extent allocation here because the
1665 * delalloc record we are overwriting is always written.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001667 PREV.br_startblock = new->br_startblock;
1668 PREV.br_blockcount += RIGHT.br_blockcount;
Dave Chinner9230a0b2018-11-19 22:50:08 -08001669 PREV.br_state = new->br_state;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001670
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001671 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001672 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001673 xfs_iext_prev(ifp, &bma->icur);
1674 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001675
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001676 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 rval = XFS_ILOG_DEXT;
1678 else {
1679 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001680 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001681 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001683 if (XFS_IS_CORRUPT(mp, i != 1)) {
1684 error = -EFSCORRUPTED;
1685 goto done;
1686 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001687 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001688 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 goto done;
1690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 break;
1692
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001693 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 /*
1695 * Filling in all of a previously delayed allocation extent.
1696 * Neither the left nor right neighbors are contiguous with
1697 * the new one.
1698 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001699 PREV.br_startblock = new->br_startblock;
1700 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001701 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001702 ifp->if_nextents++;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001703
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001704 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1706 else {
1707 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001708 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001709 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001711 if (XFS_IS_CORRUPT(mp, i != 0)) {
1712 error = -EFSCORRUPTED;
1713 goto done;
1714 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001715 error = xfs_btree_insert(bma->cur, &i);
1716 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001718 if (XFS_IS_CORRUPT(mp, i != 1)) {
1719 error = -EFSCORRUPTED;
1720 goto done;
1721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 break;
1724
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001725 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 /*
1727 * Filling in the first part of a previous delayed allocation.
1728 * The left neighbor is contiguous.
1729 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001730 old = LEFT;
1731 temp = PREV.br_blockcount - new->br_blockcount;
1732 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1733 startblockval(PREV.br_startblock));
1734
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001735 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001736
Christoph Hellwigbf999712017-11-03 10:34:38 -07001737 PREV.br_blockcount = temp;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001738 PREV.br_startoff += new->br_blockcount;
1739 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwig0d045542017-11-03 10:34:39 -07001740
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001741 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1742 xfs_iext_prev(ifp, &bma->icur);
1743 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001744
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001745 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 rval = XFS_ILOG_DEXT;
1747 else {
1748 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001749 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001750 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001752 if (XFS_IS_CORRUPT(mp, i != 1)) {
1753 error = -EFSCORRUPTED;
1754 goto done;
1755 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001756 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001757 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 goto done;
1759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 break;
1761
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001762 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 /*
1764 * Filling in the first part of a previous delayed allocation.
1765 * The left neighbor is not contiguous.
1766 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001767 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001768 ifp->if_nextents++;
1769
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001770 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1772 else {
1773 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001774 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001775 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001777 if (XFS_IS_CORRUPT(mp, i != 0)) {
1778 error = -EFSCORRUPTED;
1779 goto done;
1780 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001781 error = xfs_btree_insert(bma->cur, &i);
1782 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001784 if (XFS_IS_CORRUPT(mp, i != 1)) {
1785 error = -EFSCORRUPTED;
1786 goto done;
1787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001789
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001790 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001791 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001792 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 rval |= tmp_rval;
1794 if (error)
1795 goto done;
1796 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001797
1798 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001799 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001800 startblockval(PREV.br_startblock) -
Dave Chinner92219c22020-03-10 17:52:53 -07001801 (bma->cur ? bma->cur->bc_ino.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001802
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001803 PREV.br_startoff = new_endoff;
1804 PREV.br_blockcount = temp;
1805 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001806 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001807 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001808 xfs_iext_prev(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 break;
1810
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001811 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 /*
1813 * Filling in the last part of a previous delayed allocation.
1814 * The right neighbor is contiguous with the new allocation.
1815 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001816 old = RIGHT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001817 RIGHT.br_startoff = new->br_startoff;
1818 RIGHT.br_startblock = new->br_startblock;
1819 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001820
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001821 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 rval = XFS_ILOG_DEXT;
1823 else {
1824 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001825 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001826 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001828 if (XFS_IS_CORRUPT(mp, i != 1)) {
1829 error = -EFSCORRUPTED;
1830 goto done;
1831 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001832 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001833 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 goto done;
1835 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001836
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001837 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001838 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001839 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001840
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001841 PREV.br_blockcount = temp;
1842 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001843
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001844 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1845 xfs_iext_next(ifp, &bma->icur);
1846 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 break;
1848
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001849 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 /*
1851 * Filling in the last part of a previous delayed allocation.
1852 * The right neighbor is not contiguous.
1853 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001854 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001855 ifp->if_nextents++;
1856
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001857 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1859 else {
1860 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001861 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001862 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001864 if (XFS_IS_CORRUPT(mp, i != 0)) {
1865 error = -EFSCORRUPTED;
1866 goto done;
1867 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001868 error = xfs_btree_insert(bma->cur, &i);
1869 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001871 if (XFS_IS_CORRUPT(mp, i != 1)) {
1872 error = -EFSCORRUPTED;
1873 goto done;
1874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001876
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001877 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001878 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001879 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 rval |= tmp_rval;
1881 if (error)
1882 goto done;
1883 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001884
1885 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001886 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001887 startblockval(PREV.br_startblock) -
Dave Chinner92219c22020-03-10 17:52:53 -07001888 (bma->cur ? bma->cur->bc_ino.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001889
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001890 PREV.br_startblock = nullstartblock(da_new);
1891 PREV.br_blockcount = temp;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001892 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001893 xfs_iext_next(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 break;
1895
1896 case 0:
1897 /*
1898 * Filling in the middle part of a previous delayed allocation.
1899 * Contiguity is impossible here.
1900 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001901 *
1902 * We start with a delayed allocation:
1903 *
1904 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1905 * PREV @ idx
1906 *
1907 * and we are allocating:
1908 * +rrrrrrrrrrrrrrrrr+
1909 * new
1910 *
1911 * and we set it up for insertion as:
1912 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1913 * new
1914 * PREV @ idx LEFT RIGHT
1915 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001917 old = PREV;
1918
1919 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001920 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001921
1922 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001923 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001924 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001925 RIGHT.br_blockcount =
1926 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1927 RIGHT.br_startblock =
1928 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1929 RIGHT.br_blockcount));
1930
1931 /* truncate PREV */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001932 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1933 PREV.br_startblock =
1934 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1935 PREV.br_blockcount));
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001936 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001937
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001938 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001939 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1940 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001941 ifp->if_nextents++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001942
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001943 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1945 else {
1946 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001947 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001948 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001950 if (XFS_IS_CORRUPT(mp, i != 0)) {
1951 error = -EFSCORRUPTED;
1952 goto done;
1953 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001954 error = xfs_btree_insert(bma->cur, &i);
1955 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001957 if (XFS_IS_CORRUPT(mp, i != 1)) {
1958 error = -EFSCORRUPTED;
1959 goto done;
1960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001962
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001963 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001964 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001965 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 rval |= tmp_rval;
1967 if (error)
1968 goto done;
1969 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001970
1971 da_new = startblockval(PREV.br_startblock) +
1972 startblockval(RIGHT.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 break;
1974
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001975 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1976 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1977 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1978 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1979 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1980 case BMAP_LEFT_CONTIG:
1981 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 /*
1983 * These cases are all impossible.
1984 */
1985 ASSERT(0);
1986 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001987
Darrick J. Wong95eb3082018-05-09 10:02:32 -07001988 /* add reverse mapping unless caller opted out */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07001989 if (!(bma->flags & XFS_BMAPI_NORMAP))
1990 xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10001991
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001992 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001993 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001994 int tmp_logflags; /* partial log flag return val */
1995
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001996 ASSERT(bma->cur == NULL);
1997 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001998 &bma->cur, da_old > 0, &tmp_logflags,
1999 whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002000 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002001 if (error)
2002 goto done;
2003 }
2004
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07002005 if (da_new != da_old)
2006 xfs_mod_delalloc(mp, (int64_t)da_new - da_old);
2007
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002008 if (bma->cur) {
Dave Chinner92219c22020-03-10 17:52:53 -07002009 da_new += bma->cur->bc_ino.allocated;
2010 bma->cur->bc_ino.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002011 }
2012
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002013 /* adjust for changes in reserved delayed indirect blocks */
2014 if (da_new != da_old) {
2015 ASSERT(state == 0 || da_new < da_old);
2016 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2017 false);
2018 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002019
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002020 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002022 if (whichfork != XFS_COW_FORK)
2023 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 return error;
2025#undef LEFT
2026#undef RIGHT
2027#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
2030/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002031 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 */
Christoph Hellwig26b91c72019-02-18 09:38:48 -08002033int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002035 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002037 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002038 struct xfs_iext_cursor *icur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002040 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002041 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 int i; /* temp state */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07002046 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2049 /* left is 0, right is 1, prev is 2 */
2050 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002051 int state = xfs_bmap_fork_to_state(whichfork);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002052 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002053 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002055 *logflagsp = 0;
2056
2057 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002058 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002059
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002060 ASSERT(!isnullstartblock(new->br_startblock));
2061
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002062 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064#define LEFT r[0]
2065#define RIGHT r[1]
2066#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 /*
2069 * Set up a bunch of variables to make the tests simpler.
2070 */
2071 error = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002072 xfs_iext_get_extent(ifp, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002073 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 new_endoff = new->br_startoff + new->br_blockcount;
2075 ASSERT(PREV.br_startoff <= new->br_startoff);
2076 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 /*
2079 * Set flags determining what part of the previous oldext allocation
2080 * extent is being replaced by a newext allocation.
2081 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002082 if (PREV.br_startoff == new->br_startoff)
2083 state |= BMAP_LEFT_FILLING;
2084 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2085 state |= BMAP_RIGHT_FILLING;
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 /*
2088 * Check and set flags if this segment has a left neighbor.
2089 * Don't set contiguous if the combined extent would be too large.
2090 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002091 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002092 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002093 if (isnullstartblock(LEFT.br_startblock))
2094 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002096
2097 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2098 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2099 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002100 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002101 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2102 state |= BMAP_LEFT_CONTIG;
2103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 /*
2105 * Check and set flags if this segment has a right neighbor.
2106 * Don't set contiguous if the combined extent would be too large.
2107 * Also check for all-three-contiguous being too large.
2108 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002109 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002110 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002111 if (isnullstartblock(RIGHT.br_startblock))
2112 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002114
2115 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2116 new_endoff == RIGHT.br_startoff &&
2117 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002118 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002119 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2120 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2121 BMAP_RIGHT_FILLING)) !=
2122 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2123 BMAP_RIGHT_FILLING) ||
2124 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2125 <= MAXEXTLEN))
2126 state |= BMAP_RIGHT_CONTIG;
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 /*
2129 * Switch out based on the FILLING and CONTIG state bits.
2130 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002131 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2132 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2133 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2134 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 /*
2136 * Setting all of a previous oldext extent to newext.
2137 * The left and right neighbors are both contiguous with new.
2138 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002139 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002140
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002141 xfs_iext_remove(ip, icur, state);
2142 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002143 xfs_iext_prev(ifp, icur);
2144 xfs_iext_update_extent(ip, state, icur, &LEFT);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002145 ifp->if_nextents -= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 if (cur == NULL)
2147 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2148 else {
2149 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002150 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2151 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002153 if (XFS_IS_CORRUPT(mp, i != 1)) {
2154 error = -EFSCORRUPTED;
2155 goto done;
2156 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002157 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002159 if (XFS_IS_CORRUPT(mp, i != 1)) {
2160 error = -EFSCORRUPTED;
2161 goto done;
2162 }
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002163 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002165 if (XFS_IS_CORRUPT(mp, i != 1)) {
2166 error = -EFSCORRUPTED;
2167 goto done;
2168 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002169 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002171 if (XFS_IS_CORRUPT(mp, i != 1)) {
2172 error = -EFSCORRUPTED;
2173 goto done;
2174 }
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002175 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002177 if (XFS_IS_CORRUPT(mp, i != 1)) {
2178 error = -EFSCORRUPTED;
2179 goto done;
2180 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002181 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002182 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 goto done;
2184 }
2185 break;
2186
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002187 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 /*
2189 * Setting all of a previous oldext extent to newext.
2190 * The left neighbor is contiguous, the right is not.
2191 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002192 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002193
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002194 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002195 xfs_iext_prev(ifp, icur);
2196 xfs_iext_update_extent(ip, state, icur, &LEFT);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002197 ifp->if_nextents--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 if (cur == NULL)
2199 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2200 else {
2201 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002202 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2203 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002205 if (XFS_IS_CORRUPT(mp, i != 1)) {
2206 error = -EFSCORRUPTED;
2207 goto done;
2208 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002209 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002211 if (XFS_IS_CORRUPT(mp, i != 1)) {
2212 error = -EFSCORRUPTED;
2213 goto done;
2214 }
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002215 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002217 if (XFS_IS_CORRUPT(mp, i != 1)) {
2218 error = -EFSCORRUPTED;
2219 goto done;
2220 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002221 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002222 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 goto done;
2224 }
2225 break;
2226
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002227 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 /*
2229 * Setting all of a previous oldext extent to newext.
2230 * The right neighbor is contiguous, the left is not.
2231 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002232 PREV.br_blockcount += RIGHT.br_blockcount;
2233 PREV.br_state = new->br_state;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002234
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002235 xfs_iext_next(ifp, icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002236 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002237 xfs_iext_prev(ifp, icur);
2238 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002239 ifp->if_nextents--;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 if (cur == NULL)
2242 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2243 else {
2244 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002245 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2246 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002248 if (XFS_IS_CORRUPT(mp, i != 1)) {
2249 error = -EFSCORRUPTED;
2250 goto done;
2251 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002252 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002254 if (XFS_IS_CORRUPT(mp, i != 1)) {
2255 error = -EFSCORRUPTED;
2256 goto done;
2257 }
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002258 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002260 if (XFS_IS_CORRUPT(mp, i != 1)) {
2261 error = -EFSCORRUPTED;
2262 goto done;
2263 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002264 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002265 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 goto done;
2267 }
2268 break;
2269
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002270 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 /*
2272 * Setting all of a previous oldext extent to newext.
2273 * Neither the left nor right neighbors are contiguous with
2274 * the new one.
2275 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002276 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002277 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (cur == NULL)
2280 rval = XFS_ILOG_DEXT;
2281 else {
2282 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002283 error = xfs_bmbt_lookup_eq(cur, new, &i);
2284 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002286 if (XFS_IS_CORRUPT(mp, i != 1)) {
2287 error = -EFSCORRUPTED;
2288 goto done;
2289 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002290 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002291 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 goto done;
2293 }
2294 break;
2295
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002296 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 /*
2298 * Setting the first part of a previous oldext extent to newext.
2299 * The left neighbor is contiguous.
2300 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002301 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002302
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002303 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002304 PREV.br_startoff += new->br_blockcount;
2305 PREV.br_startblock += new->br_blockcount;
2306 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002307
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002308 xfs_iext_update_extent(ip, state, icur, &PREV);
2309 xfs_iext_prev(ifp, icur);
2310 xfs_iext_update_extent(ip, state, icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if (cur == NULL)
2313 rval = XFS_ILOG_DEXT;
2314 else {
2315 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002316 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002317 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002319 if (XFS_IS_CORRUPT(mp, i != 1)) {
2320 error = -EFSCORRUPTED;
2321 goto done;
2322 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002323 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002324 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002326 error = xfs_btree_decrement(cur, 0, &i);
2327 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002329 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002330 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 goto done;
2332 }
2333 break;
2334
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002335 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 /*
2337 * Setting the first part of a previous oldext extent to newext.
2338 * The left neighbor is not contiguous.
2339 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002340 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002341 PREV.br_startoff += new->br_blockcount;
2342 PREV.br_startblock += new->br_blockcount;
2343 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002344
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002345 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002346 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002347 ifp->if_nextents++;
2348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (cur == NULL)
2350 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2351 else {
2352 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002353 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002354 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002356 if (XFS_IS_CORRUPT(mp, i != 1)) {
2357 error = -EFSCORRUPTED;
2358 goto done;
2359 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002360 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002361 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 goto done;
2363 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002364 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002366 if (XFS_IS_CORRUPT(mp, i != 1)) {
2367 error = -EFSCORRUPTED;
2368 goto done;
2369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 }
2371 break;
2372
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002373 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 /*
2375 * Setting the last part of a previous oldext extent to newext.
2376 * The right neighbor is contiguous with the new allocation.
2377 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002378 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002379 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002380
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002381 RIGHT.br_startoff = new->br_startoff;
2382 RIGHT.br_startblock = new->br_startblock;
2383 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002384
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002385 xfs_iext_update_extent(ip, state, icur, &PREV);
2386 xfs_iext_next(ifp, icur);
2387 xfs_iext_update_extent(ip, state, icur, &RIGHT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 if (cur == NULL)
2390 rval = XFS_ILOG_DEXT;
2391 else {
2392 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002393 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002394 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002396 if (XFS_IS_CORRUPT(mp, i != 1)) {
2397 error = -EFSCORRUPTED;
2398 goto done;
2399 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002400 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002401 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002403 error = xfs_btree_increment(cur, 0, &i);
2404 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002406 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002407 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 goto done;
2409 }
2410 break;
2411
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002412 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 /*
2414 * Setting the last part of a previous oldext extent to newext.
2415 * The right neighbor is not contiguous.
2416 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002417 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002418 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002419
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002420 xfs_iext_update_extent(ip, state, icur, &PREV);
2421 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002422 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002423 ifp->if_nextents++;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002424
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 if (cur == NULL)
2426 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2427 else {
2428 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002429 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002430 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002432 if (XFS_IS_CORRUPT(mp, i != 1)) {
2433 error = -EFSCORRUPTED;
2434 goto done;
2435 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002436 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002437 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002439 error = xfs_bmbt_lookup_eq(cur, new, &i);
2440 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002442 if (XFS_IS_CORRUPT(mp, i != 0)) {
2443 error = -EFSCORRUPTED;
2444 goto done;
2445 }
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002446 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002448 if (XFS_IS_CORRUPT(mp, i != 1)) {
2449 error = -EFSCORRUPTED;
2450 goto done;
2451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 }
2453 break;
2454
2455 case 0:
2456 /*
2457 * Setting the middle part of a previous oldext extent to
2458 * newext. Contiguity is impossible here.
2459 * One extent becomes three extents.
2460 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002461 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002462 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 r[0] = *new;
2465 r[1].br_startoff = new_endoff;
2466 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002467 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002469 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002470
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002471 xfs_iext_update_extent(ip, state, icur, &PREV);
2472 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002473 xfs_iext_insert(ip, icur, &r[1], state);
2474 xfs_iext_insert(ip, icur, &r[0], state);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002475 ifp->if_nextents += 2;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 if (cur == NULL)
2478 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2479 else {
2480 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002481 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002482 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002484 if (XFS_IS_CORRUPT(mp, i != 1)) {
2485 error = -EFSCORRUPTED;
2486 goto done;
2487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002489 error = xfs_bmbt_update(cur, &r[1]);
2490 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 goto done;
2492 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002494 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002496 if (XFS_IS_CORRUPT(mp, i != 1)) {
2497 error = -EFSCORRUPTED;
2498 goto done;
2499 }
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002500 /*
2501 * Reset the cursor to the position of the new extent
2502 * we are about to insert as we can't trust it after
2503 * the previous insert.
2504 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002505 error = xfs_bmbt_lookup_eq(cur, new, &i);
2506 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002508 if (XFS_IS_CORRUPT(mp, i != 0)) {
2509 error = -EFSCORRUPTED;
2510 goto done;
2511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002513 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002515 if (XFS_IS_CORRUPT(mp, i != 1)) {
2516 error = -EFSCORRUPTED;
2517 goto done;
2518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
2520 break;
2521
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002522 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2523 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2524 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2525 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2526 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2527 case BMAP_LEFT_CONTIG:
2528 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 /*
2530 * These cases are all impossible.
2531 */
2532 ASSERT(0);
2533 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002534
Darrick J. Wong9c194642016-08-03 12:16:05 +10002535 /* update reverse mappings */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07002536 xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002537
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002538 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002539 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002540 int tmp_logflags; /* partial log flag return val */
2541
2542 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07002543 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2544 &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002545 *logflagsp |= tmp_logflags;
2546 if (error)
2547 goto done;
2548 }
2549
2550 /* clear out the allocated field, done with it now in any case. */
2551 if (cur) {
Dave Chinner92219c22020-03-10 17:52:53 -07002552 cur->bc_ino.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002553 *curp = cur;
2554 }
2555
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002556 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002558 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 return error;
2560#undef LEFT
2561#undef RIGHT
2562#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563}
2564
2565/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002566 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002568STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569xfs_bmap_add_extent_hole_delay(
2570 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002571 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002572 struct xfs_iext_cursor *icur,
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002573 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574{
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07002575 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2577 xfs_filblks_t newlen=0; /* new indirect size */
2578 xfs_filblks_t oldlen=0; /* old indirect size */
2579 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002580 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002581 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002583 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002584 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 /*
2587 * Check and set flags if this segment has a left neighbor
2588 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002589 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002590 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002591 if (isnullstartblock(left.br_startblock))
2592 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002594
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 /*
2596 * Check and set flags if the current (right) segment exists.
2597 * If it doesn't exist, we're converting the hole at end-of-file.
2598 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002599 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002600 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002601 if (isnullstartblock(right.br_startblock))
2602 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002604
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 /*
2606 * Set contiguity flags on the left and right neighbors.
2607 * Don't let extents get too large, even if the pieces are contiguous.
2608 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002609 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2610 left.br_startoff + left.br_blockcount == new->br_startoff &&
2611 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2612 state |= BMAP_LEFT_CONTIG;
2613
2614 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2615 new->br_startoff + new->br_blockcount == right.br_startoff &&
2616 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2617 (!(state & BMAP_LEFT_CONTIG) ||
2618 (left.br_blockcount + new->br_blockcount +
2619 right.br_blockcount <= MAXEXTLEN)))
2620 state |= BMAP_RIGHT_CONTIG;
2621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 /*
2623 * Switch out based on the contiguity flags.
2624 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002625 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2626 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 /*
2628 * New allocation is contiguous with delayed allocations
2629 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002630 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 */
2632 temp = left.br_blockcount + new->br_blockcount +
2633 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002634
Eric Sandeen9d87c312009-01-14 23:22:07 -06002635 oldlen = startblockval(left.br_startblock) +
2636 startblockval(new->br_startblock) +
2637 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002638 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2639 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002640 left.br_startblock = nullstartblock(newlen);
2641 left.br_blockcount = temp;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002642
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002643 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002644 xfs_iext_prev(ifp, icur);
2645 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 break;
2647
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002648 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 /*
2650 * New allocation is contiguous with a delayed allocation
2651 * on the left.
2652 * Merge the new allocation with the left neighbor.
2653 */
2654 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002655
Eric Sandeen9d87c312009-01-14 23:22:07 -06002656 oldlen = startblockval(left.br_startblock) +
2657 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002658 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2659 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002660 left.br_blockcount = temp;
2661 left.br_startblock = nullstartblock(newlen);
Christoph Hellwig41d196f2017-11-03 10:34:39 -07002662
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002663 xfs_iext_prev(ifp, icur);
2664 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 break;
2666
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002667 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 /*
2669 * New allocation is contiguous with a delayed allocation
2670 * on the right.
2671 * Merge the new allocation with the right neighbor.
2672 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002674 oldlen = startblockval(new->br_startblock) +
2675 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002676 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2677 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002678 right.br_startoff = new->br_startoff;
2679 right.br_startblock = nullstartblock(newlen);
2680 right.br_blockcount = temp;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002681 xfs_iext_update_extent(ip, state, icur, &right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 break;
2683
2684 case 0:
2685 /*
2686 * New allocation is not contiguous with another
2687 * delayed allocation.
2688 * Insert a new entry.
2689 */
2690 oldlen = newlen = 0;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002691 xfs_iext_insert(ip, icur, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 break;
2693 }
2694 if (oldlen != newlen) {
2695 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002696 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2697 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 /*
2699 * Nothing to do for disk quota accounting here.
2700 */
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07002701 xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703}
2704
2705/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002706 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 */
2708STATIC int /* error */
2709xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002710 struct xfs_trans *tp,
2711 struct xfs_inode *ip,
2712 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002713 struct xfs_iext_cursor *icur,
Christoph Hellwig6d045582017-04-11 16:45:54 -07002714 struct xfs_btree_cur **curp,
2715 struct xfs_bmbt_irec *new,
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002716 int *logflagsp,
2717 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002719 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2720 struct xfs_mount *mp = ip->i_mount;
2721 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2725 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002726 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002727 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002728 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002730 ASSERT(!isnullstartblock(new->br_startblock));
Dave Chinner8ef54792020-03-10 17:54:38 -07002731 ASSERT(!cur || !(cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002732
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002733 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 /*
2736 * Check and set flags if this segment has a left neighbor.
2737 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002738 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002739 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002740 if (isnullstartblock(left.br_startblock))
2741 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 /*
2745 * Check and set flags if this segment has a current value.
2746 * Not true if we're inserting into the "hole" at eof.
2747 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002748 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002749 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002750 if (isnullstartblock(right.br_startblock))
2751 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 /*
2755 * We're inserting a real allocation between "left" and "right".
2756 * Set the contiguity flags. Don't let extents get too large.
2757 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002758 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2759 left.br_startoff + left.br_blockcount == new->br_startoff &&
2760 left.br_startblock + left.br_blockcount == new->br_startblock &&
2761 left.br_state == new->br_state &&
2762 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2763 state |= BMAP_LEFT_CONTIG;
2764
2765 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2766 new->br_startoff + new->br_blockcount == right.br_startoff &&
2767 new->br_startblock + new->br_blockcount == right.br_startblock &&
2768 new->br_state == right.br_state &&
2769 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2770 (!(state & BMAP_LEFT_CONTIG) ||
2771 left.br_blockcount + new->br_blockcount +
2772 right.br_blockcount <= MAXEXTLEN))
2773 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002775 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 /*
2777 * Select which case we're in here, and implement it.
2778 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002779 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2780 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 /*
2782 * New allocation is contiguous with real allocations on the
2783 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002784 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002786 left.br_blockcount += new->br_blockcount + right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002787
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002788 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002789 xfs_iext_prev(ifp, icur);
2790 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002791 ifp->if_nextents--;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002792
Christoph Hellwig6d045582017-04-11 16:45:54 -07002793 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002794 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002795 } else {
2796 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002797 error = xfs_bmbt_lookup_eq(cur, &right, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002798 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002799 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002800 if (XFS_IS_CORRUPT(mp, i != 1)) {
2801 error = -EFSCORRUPTED;
2802 goto done;
2803 }
Christoph Hellwig6d045582017-04-11 16:45:54 -07002804 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002805 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002806 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002807 if (XFS_IS_CORRUPT(mp, i != 1)) {
2808 error = -EFSCORRUPTED;
2809 goto done;
2810 }
Christoph Hellwig6d045582017-04-11 16:45:54 -07002811 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002812 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002813 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002814 if (XFS_IS_CORRUPT(mp, i != 1)) {
2815 error = -EFSCORRUPTED;
2816 goto done;
2817 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002818 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002819 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002820 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002822 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002824 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 /*
2826 * New allocation is contiguous with a real allocation
2827 * on the left.
2828 * Merge the new allocation with the left neighbor.
2829 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002830 old = left;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002831 left.br_blockcount += new->br_blockcount;
Christoph Hellwig1d2e0082017-11-03 10:34:40 -07002832
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002833 xfs_iext_prev(ifp, icur);
2834 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002835
Christoph Hellwig6d045582017-04-11 16:45:54 -07002836 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002837 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002838 } else {
2839 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002840 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002841 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002842 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002843 if (XFS_IS_CORRUPT(mp, i != 1)) {
2844 error = -EFSCORRUPTED;
2845 goto done;
2846 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002847 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002848 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002849 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002851 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002853 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 /*
2855 * New allocation is contiguous with a real allocation
2856 * on the right.
2857 * Merge the new allocation with the right neighbor.
2858 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002859 old = right;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07002860
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002861 right.br_startoff = new->br_startoff;
2862 right.br_startblock = new->br_startblock;
2863 right.br_blockcount += new->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002864 xfs_iext_update_extent(ip, state, icur, &right);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002865
Christoph Hellwig6d045582017-04-11 16:45:54 -07002866 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002867 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002868 } else {
2869 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002870 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002871 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002872 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002873 if (XFS_IS_CORRUPT(mp, i != 1)) {
2874 error = -EFSCORRUPTED;
2875 goto done;
2876 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002877 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002878 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002879 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002881 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 case 0:
2884 /*
2885 * New allocation is not contiguous with another
2886 * real allocation.
2887 * Insert a new entry.
2888 */
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002889 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002890 ifp->if_nextents++;
2891
Christoph Hellwig6d045582017-04-11 16:45:54 -07002892 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002893 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002894 } else {
2895 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002896 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002897 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002898 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002899 if (XFS_IS_CORRUPT(mp, i != 0)) {
2900 error = -EFSCORRUPTED;
2901 goto done;
2902 }
Christoph Hellwig6d045582017-04-11 16:45:54 -07002903 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002904 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002905 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002906 if (XFS_IS_CORRUPT(mp, i != 1)) {
2907 error = -EFSCORRUPTED;
2908 goto done;
2909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002911 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002913
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002914 /* add reverse mapping unless caller opted out */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07002915 if (!(flags & XFS_BMAPI_NORMAP))
2916 xfs_rmap_map_extent(tp, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002917
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002918 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002919 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002920 int tmp_logflags; /* partial log flag return val */
2921
Christoph Hellwig6d045582017-04-11 16:45:54 -07002922 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07002923 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2924 &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002925 *logflagsp |= tmp_logflags;
2926 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002927 if (error)
2928 goto done;
2929 }
2930
2931 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002932 if (cur)
Dave Chinner92219c22020-03-10 17:52:53 -07002933 cur->bc_ino.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002934
Christoph Hellwig6d045582017-04-11 16:45:54 -07002935 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002936done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002937 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002938 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939}
2940
Nathan Scottdd9f4382006-01-11 15:28:28 +11002941/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11002942 * Functions used in the extent read, allocate and remove paths
2943 */
2944
2945/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002946 * Adjust the size of the new extent based on di_extsize and rt extsize.
2947 */
Dave Chinner68988112013-08-12 20:49:42 +10002948int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002949xfs_bmap_extsize_align(
2950 xfs_mount_t *mp,
2951 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2952 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2953 xfs_extlen_t extsz, /* align to this extent size */
2954 int rt, /* is this a realtime inode? */
2955 int eof, /* is extent at end-of-file? */
2956 int delay, /* creating delalloc extent? */
2957 int convert, /* overwriting unwritten extent? */
2958 xfs_fileoff_t *offp, /* in/out: aligned offset */
2959 xfs_extlen_t *lenp) /* in/out: aligned length */
2960{
2961 xfs_fileoff_t orig_off; /* original offset */
2962 xfs_extlen_t orig_alen; /* original length */
2963 xfs_fileoff_t orig_end; /* original off+len */
2964 xfs_fileoff_t nexto; /* next file offset */
2965 xfs_fileoff_t prevo; /* previous file offset */
2966 xfs_fileoff_t align_off; /* temp for offset */
2967 xfs_extlen_t align_alen; /* temp for length */
2968 xfs_extlen_t temp; /* temp for calculations */
2969
2970 if (convert)
2971 return 0;
2972
2973 orig_off = align_off = *offp;
2974 orig_alen = align_alen = *lenp;
2975 orig_end = orig_off + orig_alen;
2976
2977 /*
2978 * If this request overlaps an existing extent, then don't
2979 * attempt to perform any additional alignment.
2980 */
2981 if (!delay && !eof &&
2982 (orig_off >= gotp->br_startoff) &&
2983 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2984 return 0;
2985 }
2986
2987 /*
2988 * If the file offset is unaligned vs. the extent size
2989 * we need to align it. This will be possible unless
2990 * the file was previously written with a kernel that didn't
2991 * perform this alignment, or if a truncate shot us in the
2992 * foot.
2993 */
Dave Chinner0703a8e2018-06-08 09:54:22 -07002994 div_u64_rem(orig_off, extsz, &temp);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002995 if (temp) {
2996 align_alen += temp;
2997 align_off -= temp;
2998 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10002999
3000 /* Same adjustment for the end of the requested area. */
3001 temp = (align_alen % extsz);
3002 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003003 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003004
3005 /*
3006 * For large extent hint sizes, the aligned extent might be larger than
3007 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3008 * the length back under MAXEXTLEN. The outer allocation loops handle
3009 * short allocation just fine, so it is safe to do this. We only want to
3010 * do it when we are forced to, though, because it means more allocation
3011 * operations are required.
3012 */
3013 while (align_alen > MAXEXTLEN)
3014 align_alen -= extsz;
3015 ASSERT(align_alen <= MAXEXTLEN);
3016
Nathan Scottdd9f4382006-01-11 15:28:28 +11003017 /*
3018 * If the previous block overlaps with this proposed allocation
3019 * then move the start forward without adjusting the length.
3020 */
3021 if (prevp->br_startoff != NULLFILEOFF) {
3022 if (prevp->br_startblock == HOLESTARTBLOCK)
3023 prevo = prevp->br_startoff;
3024 else
3025 prevo = prevp->br_startoff + prevp->br_blockcount;
3026 } else
3027 prevo = 0;
3028 if (align_off != orig_off && align_off < prevo)
3029 align_off = prevo;
3030 /*
3031 * If the next block overlaps with this proposed allocation
3032 * then move the start back without adjusting the length,
3033 * but not before offset 0.
3034 * This may of course make the start overlap previous block,
3035 * and if we hit the offset 0 limit then the next block
3036 * can still overlap too.
3037 */
3038 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3039 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3040 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3041 nexto = gotp->br_startoff + gotp->br_blockcount;
3042 else
3043 nexto = gotp->br_startoff;
3044 } else
3045 nexto = NULLFILEOFF;
3046 if (!eof &&
3047 align_off + align_alen != orig_end &&
3048 align_off + align_alen > nexto)
3049 align_off = nexto > align_alen ? nexto - align_alen : 0;
3050 /*
3051 * If we're now overlapping the next or previous extent that
3052 * means we can't fit an extsz piece in this hole. Just move
3053 * the start forward to the first valid spot and set
3054 * the length so we hit the end.
3055 */
3056 if (align_off != orig_off && align_off < prevo)
3057 align_off = prevo;
3058 if (align_off + align_alen != orig_end &&
3059 align_off + align_alen > nexto &&
3060 nexto != NULLFILEOFF) {
3061 ASSERT(nexto > prevo);
3062 align_alen = nexto - align_off;
3063 }
3064
3065 /*
3066 * If realtime, and the result isn't a multiple of the realtime
3067 * extent size we need to remove blocks until it is.
3068 */
3069 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3070 /*
3071 * We're not covering the original request, or
3072 * we won't be able to once we fix the length.
3073 */
3074 if (orig_off < align_off ||
3075 orig_end > align_off + align_alen ||
3076 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003077 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003078 /*
3079 * Try to fix it by moving the start up.
3080 */
3081 if (align_off + temp <= orig_off) {
3082 align_alen -= temp;
3083 align_off += temp;
3084 }
3085 /*
3086 * Try to fix it by moving the end in.
3087 */
3088 else if (align_off + align_alen - temp >= orig_end)
3089 align_alen -= temp;
3090 /*
3091 * Set the start to the minimum then trim the length.
3092 */
3093 else {
3094 align_alen -= orig_off - align_off;
3095 align_off = orig_off;
3096 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3097 }
3098 /*
3099 * Result doesn't cover the request, fail it.
3100 */
3101 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003102 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003103 } else {
3104 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003105 /* see MAXEXTLEN handling above */
3106 ASSERT(orig_end <= align_off + align_alen ||
3107 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003108 }
3109
3110#ifdef DEBUG
3111 if (!eof && gotp->br_startoff != NULLFILEOFF)
3112 ASSERT(align_off + align_alen <= gotp->br_startoff);
3113 if (prevp->br_startoff != NULLFILEOFF)
3114 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3115#endif
3116
3117 *lenp = align_alen;
3118 *offp = align_off;
3119 return 0;
3120}
3121
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122#define XFS_ALLOC_GAP_UNITS 4
3123
Dave Chinner68988112013-08-12 20:49:42 +10003124void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003125xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003126 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
3128 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3130 xfs_mount_t *mp; /* mount point structure */
3131 int nullfb; /* true if ap->firstblock isn't set */
3132 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
3134#define ISVALID(x,y) \
3135 (rt ? \
3136 (x) < mp->m_sb.sb_rblocks : \
3137 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3138 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3139 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3140
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 mp = ap->ip->i_mount;
Brian Foster94c07b42018-07-11 22:26:28 -07003142 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003143 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
Christoph Hellwigc34d5702019-10-30 12:25:00 -07003144 (ap->datatype & XFS_ALLOC_USERDATA);
Brian Foster94c07b42018-07-11 22:26:28 -07003145 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3146 ap->tp->t_firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 /*
3148 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003149 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003151 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3152 !isnullstartblock(ap->prev.br_startblock) &&
3153 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3154 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003155 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 /*
3157 * Adjust for the gap between prevp and us.
3158 */
Dave Chinner3a756672011-09-18 20:40:58 +00003159 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003160 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003162 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3163 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 }
3165 /*
3166 * If not at eof, then compare the two neighbor blocks.
3167 * Figure out whether either one gives us a good starting point,
3168 * and pick the better one.
3169 */
3170 else if (!ap->eof) {
3171 xfs_fsblock_t gotbno; /* right side block number */
3172 xfs_fsblock_t gotdiff=0; /* right side difference */
3173 xfs_fsblock_t prevbno; /* left side block number */
3174 xfs_fsblock_t prevdiff=0; /* left side difference */
3175
3176 /*
3177 * If there's a previous (left) block, select a requested
3178 * start block based on it.
3179 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003180 if (ap->prev.br_startoff != NULLFILEOFF &&
3181 !isnullstartblock(ap->prev.br_startblock) &&
3182 (prevbno = ap->prev.br_startblock +
3183 ap->prev.br_blockcount) &&
3184 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 /*
3186 * Calculate gap to end of previous block.
3187 */
Dave Chinner3a756672011-09-18 20:40:58 +00003188 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003189 (ap->prev.br_startoff +
3190 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 /*
3192 * Figure the startblock based on the previous block's
3193 * end and the gap size.
3194 * Heuristic!
3195 * If the gap is large relative to the piece we're
3196 * allocating, or using it gives us an invalid block
3197 * number, then just use the end of the previous block.
3198 */
Dave Chinner3a756672011-09-18 20:40:58 +00003199 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003201 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 prevbno += adjust;
3203 else
3204 prevdiff += adjust;
3205 /*
3206 * If the firstblock forbids it, can't use it,
3207 * must use default.
3208 */
3209 if (!rt && !nullfb &&
3210 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3211 prevbno = NULLFSBLOCK;
3212 }
3213 /*
3214 * No previous block or can't follow it, just default.
3215 */
3216 else
3217 prevbno = NULLFSBLOCK;
3218 /*
3219 * If there's a following (right) block, select a requested
3220 * start block based on it.
3221 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003222 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 /*
3224 * Calculate gap to start of next block.
3225 */
Dave Chinner3a756672011-09-18 20:40:58 +00003226 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 /*
3228 * Figure the startblock based on the next block's
3229 * start and the gap size.
3230 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003231 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 /*
3233 * Heuristic!
3234 * If the gap is large relative to the piece we're
3235 * allocating, or using it gives us an invalid block
3236 * number, then just use the start of the next block
3237 * offset by our length.
3238 */
Dave Chinner3a756672011-09-18 20:40:58 +00003239 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 ISVALID(gotbno - gotdiff, gotbno))
3241 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003242 else if (ISVALID(gotbno - ap->length, gotbno)) {
3243 gotbno -= ap->length;
3244 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 } else
3246 gotdiff += adjust;
3247 /*
3248 * If the firstblock forbids it, can't use it,
3249 * must use default.
3250 */
3251 if (!rt && !nullfb &&
3252 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3253 gotbno = NULLFSBLOCK;
3254 }
3255 /*
3256 * No next block, just default.
3257 */
3258 else
3259 gotbno = NULLFSBLOCK;
3260 /*
3261 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003262 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 */
3264 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003265 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003267 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003269 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003271#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003272}
3273
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003274static int
3275xfs_bmap_longest_free_extent(
3276 struct xfs_trans *tp,
3277 xfs_agnumber_t ag,
3278 xfs_extlen_t *blen,
3279 int *notinit)
3280{
3281 struct xfs_mount *mp = tp->t_mountp;
3282 struct xfs_perag *pag;
3283 xfs_extlen_t longest;
3284 int error = 0;
3285
3286 pag = xfs_perag_get(mp, ag);
3287 if (!pag->pagf_init) {
3288 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
Darrick J. Wongf48e2df2020-01-23 17:01:19 -08003289 if (error) {
3290 /* Couldn't lock the AGF, so skip this AG. */
3291 if (error == -EAGAIN) {
3292 *notinit = 1;
3293 error = 0;
3294 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003295 goto out;
3296 }
3297 }
3298
Eric Sandeena1f69412018-04-06 10:09:42 -07003299 longest = xfs_alloc_longest_free_extent(pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003300 xfs_alloc_min_freelist(mp, pag),
3301 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003302 if (*blen < longest)
3303 *blen = longest;
3304
3305out:
3306 xfs_perag_put(pag);
3307 return error;
3308}
3309
3310static void
3311xfs_bmap_select_minlen(
3312 struct xfs_bmalloca *ap,
3313 struct xfs_alloc_arg *args,
3314 xfs_extlen_t *blen,
3315 int notinit)
3316{
3317 if (notinit || *blen < ap->minlen) {
3318 /*
3319 * Since we did a BUF_TRYLOCK above, it is possible that
3320 * there is space for this request.
3321 */
3322 args->minlen = ap->minlen;
3323 } else if (*blen < args->maxlen) {
3324 /*
3325 * If the best seen length is less than the request length,
3326 * use the best as the minimum.
3327 */
3328 args->minlen = *blen;
3329 } else {
3330 /*
3331 * Otherwise we've seen an extent as big as maxlen, use that
3332 * as the minimum.
3333 */
3334 args->minlen = args->maxlen;
3335 }
3336}
3337
Nathan Scotta365bdd2006-03-14 13:34:16 +11003338STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003339xfs_bmap_btalloc_nullfb(
3340 struct xfs_bmalloca *ap,
3341 struct xfs_alloc_arg *args,
3342 xfs_extlen_t *blen)
3343{
3344 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003345 xfs_agnumber_t ag, startag;
3346 int notinit = 0;
3347 int error;
3348
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003349 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003350 args->total = ap->total;
3351
Christoph Hellwigc467c042010-02-15 23:34:42 +00003352 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3353 if (startag == NULLAGNUMBER)
3354 startag = ag = 0;
3355
Dave Chinner14b064c2011-01-27 12:16:28 +11003356 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003357 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3358 &notinit);
3359 if (error)
3360 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003361
Christoph Hellwigc467c042010-02-15 23:34:42 +00003362 if (++ag == mp->m_sb.sb_agcount)
3363 ag = 0;
3364 if (ag == startag)
3365 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003366 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003367
3368 xfs_bmap_select_minlen(ap, args, blen, notinit);
3369 return 0;
3370}
3371
3372STATIC int
3373xfs_bmap_btalloc_filestreams(
3374 struct xfs_bmalloca *ap,
3375 struct xfs_alloc_arg *args,
3376 xfs_extlen_t *blen)
3377{
3378 struct xfs_mount *mp = ap->ip->i_mount;
3379 xfs_agnumber_t ag;
3380 int notinit = 0;
3381 int error;
3382
3383 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3384 args->total = ap->total;
3385
3386 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3387 if (ag == NULLAGNUMBER)
3388 ag = 0;
3389
3390 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3391 if (error)
3392 return error;
3393
3394 if (*blen < args->maxlen) {
3395 error = xfs_filestream_new_ag(ap, &ag);
3396 if (error)
3397 return error;
3398
3399 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3400 &notinit);
3401 if (error)
3402 return error;
3403
3404 }
3405
3406 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003407
3408 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003409 * Set the failure fallback case to look in the selected AG as stream
3410 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003411 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003412 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003413 return 0;
3414}
3415
Darrick J. Wong751f3762018-01-25 13:58:13 -08003416/* Update all inode and quota accounting for the allocation we just did. */
3417static void
3418xfs_bmap_btalloc_accounting(
3419 struct xfs_bmalloca *ap,
3420 struct xfs_alloc_arg *args)
3421{
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003422 if (ap->flags & XFS_BMAPI_COWFORK) {
3423 /*
3424 * COW fork blocks are in-core only and thus are treated as
3425 * in-core quota reservation (like delalloc blocks) even when
3426 * converted to real blocks. The quota reservation is not
3427 * accounted to disk until blocks are remapped to the data
3428 * fork. So if these blocks were previously delalloc, we
3429 * already have quota reservation and there's nothing to do
3430 * yet.
3431 */
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003432 if (ap->wasdel) {
3433 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003434 return;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003435 }
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003436
3437 /*
3438 * Otherwise, we've allocated blocks in a hole. The transaction
3439 * has acquired in-core quota reservation for this extent.
3440 * Rather than account these as real blocks, however, we reduce
3441 * the transaction quota reservation based on the allocation.
3442 * This essentially transfers the transaction quota reservation
3443 * to that of a delalloc extent.
3444 */
3445 ap->ip->i_delayed_blks += args->len;
3446 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3447 -(long)args->len);
3448 return;
3449 }
3450
3451 /* data/attr fork only */
3452 ap->ip->i_d.di_nblocks += args->len;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003453 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003454 if (ap->wasdel) {
Darrick J. Wong751f3762018-01-25 13:58:13 -08003455 ap->ip->i_delayed_blks -= args->len;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003456 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3457 }
Darrick J. Wong751f3762018-01-25 13:58:13 -08003458 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3459 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3460 args->len);
3461}
3462
Christoph Hellwigc467c042010-02-15 23:34:42 +00003463STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003464xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003465 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003466{
3467 xfs_mount_t *mp; /* mount point structure */
3468 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003469 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003470 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003471 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003472 xfs_alloc_arg_t args;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003473 xfs_fileoff_t orig_offset;
3474 xfs_extlen_t orig_length;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003475 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003476 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003477 int nullfb; /* true if ap->firstblock isn't set */
3478 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003479 int tryagain;
3480 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003481 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003482
Dave Chinnera99ebf42011-12-01 11:24:20 +00003483 ASSERT(ap->length);
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003484 orig_offset = ap->offset;
3485 orig_length = ap->length;
Dave Chinnera99ebf42011-12-01 11:24:20 +00003486
Nathan Scotta365bdd2006-03-14 13:34:16 +11003487 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003488
3489 /* stripe alignment for allocation is determined by mount parameters */
3490 stripe_align = 0;
3491 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3492 stripe_align = mp->m_swidth;
3493 else if (mp->m_dalign)
3494 stripe_align = mp->m_dalign;
3495
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003496 if (ap->flags & XFS_BMAPI_COWFORK)
3497 align = xfs_get_cowextsz_hint(ap->ip);
Christoph Hellwigc34d5702019-10-30 12:25:00 -07003498 else if (ap->datatype & XFS_ALLOC_USERDATA)
Dave Chinner292378e2016-09-26 08:21:28 +10003499 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003500 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003501 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003502 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003503 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003504 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003505 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003506 }
Dave Chinner33177f052013-12-12 16:34:36 +11003507
3508
Brian Foster94c07b42018-07-11 22:26:28 -07003509 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3510 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3511 ap->tp->t_firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003512 if (nullfb) {
Christoph Hellwigc34d5702019-10-30 12:25:00 -07003513 if ((ap->datatype & XFS_ALLOC_USERDATA) &&
Dave Chinner292378e2016-09-26 08:21:28 +10003514 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003515 ag = xfs_filestream_lookup_ag(ap->ip);
3516 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003517 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003518 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003519 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003520 }
3521 } else
Brian Foster94c07b42018-07-11 22:26:28 -07003522 ap->blkno = ap->tp->t_firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003523
3524 xfs_bmap_adjacent(ap);
3525
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003527 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 * it's in the right allocation group.
3529 */
Dave Chinner3a756672011-09-18 20:40:58 +00003530 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 ;
3532 else
Brian Foster94c07b42018-07-11 22:26:28 -07003533 ap->blkno = ap->tp->t_firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 * Normal allocation, done through xfs_alloc_vextent.
3536 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003537 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003538 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003539 args.tp = ap->tp;
3540 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003541 args.fsbno = ap->blkno;
Darrick J. Wong7280fed2018-12-12 08:46:23 -08003542 args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
Dave Chinner14b064c2011-01-27 12:16:28 +11003543
3544 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner9bb54cb2018-06-07 07:54:02 -07003545 args.maxlen = min(ap->length, mp->m_ag_max_usable);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003546 blen = 0;
3547 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003548 /*
3549 * Search for an allocation group with a single extent large
3550 * enough for the request. If one isn't found, then adjust
3551 * the minimum allocation size to the largest space found.
3552 */
Christoph Hellwigc34d5702019-10-30 12:25:00 -07003553 if ((ap->datatype & XFS_ALLOC_USERDATA) &&
Dave Chinner292378e2016-09-26 08:21:28 +10003554 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003555 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3556 else
3557 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003558 if (error)
3559 return error;
Brian Foster1214f1c2018-08-01 07:20:31 -07003560 } else if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003561 if (xfs_inode_is_filestream(ap->ip))
3562 args.type = XFS_ALLOCTYPE_FIRST_AG;
3563 else
3564 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003565 args.total = args.minlen = ap->minlen;
3566 } else {
3567 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3568 args.total = ap->total;
3569 args.minlen = ap->minlen;
3570 }
David Chinner957d0eb2007-06-18 16:50:37 +10003571 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003572 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003573 args.prod = align;
Dave Chinner0703a8e2018-06-08 09:54:22 -07003574 div_u64_rem(ap->offset, args.prod, &args.mod);
3575 if (args.mod)
3576 args.mod = args.prod - args.mod;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003577 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003578 args.prod = 1;
3579 args.mod = 0;
3580 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003581 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner0703a8e2018-06-08 09:54:22 -07003582 div_u64_rem(ap->offset, args.prod, &args.mod);
3583 if (args.mod)
3584 args.mod = args.prod - args.mod;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003585 }
3586 /*
Dave Chinner1c743572019-10-21 09:26:34 -07003587 * If we are not low on available data blocks, and the underlying
3588 * logical volume manager is a stripe, and the file offset is zero then
3589 * try to allocate data blocks on stripe unit boundary. NOTE: ap->aeof
3590 * is only set if the allocation length is >= the stripe unit and the
3591 * allocation offset is at the end of file.
Nathan Scotta365bdd2006-03-14 13:34:16 +11003592 */
Brian Foster1214f1c2018-08-01 07:20:31 -07003593 if (!(ap->tp->t_flags & XFS_TRANS_LOWMODE) && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003594 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003595 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003596 atype = args.type;
3597 isaligned = 1;
3598 /*
Dave Chinner1c743572019-10-21 09:26:34 -07003599 * Adjust minlen to try and preserve alignment if we
3600 * can't guarantee an aligned maxlen extent.
Nathan Scotta365bdd2006-03-14 13:34:16 +11003601 */
Dave Chinner1c743572019-10-21 09:26:34 -07003602 if (blen > args.alignment &&
3603 blen <= args.maxlen + args.alignment)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003604 args.minlen = blen - args.alignment;
3605 args.minalignslop = 0;
3606 } else {
3607 /*
3608 * First try an exact bno allocation.
3609 * If it fails then do a near or start bno
3610 * allocation with alignment turned on.
3611 */
3612 atype = args.type;
3613 tryagain = 1;
3614 args.type = XFS_ALLOCTYPE_THIS_BNO;
3615 args.alignment = 1;
3616 /*
3617 * Compute the minlen+alignment for the
3618 * next case. Set slop so that the value
3619 * of minlen+alignment+slop doesn't go up
3620 * between the calls.
3621 */
Dave Chinner33177f052013-12-12 16:34:36 +11003622 if (blen > stripe_align && blen <= args.maxlen)
3623 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003624 else
3625 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003626 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003627 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003628 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003629 args.minlen - 1;
3630 else
3631 args.minalignslop = 0;
3632 }
3633 } else {
3634 args.alignment = 1;
3635 args.minalignslop = 0;
3636 }
3637 args.minleft = ap->minleft;
3638 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003639 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003640 args.datatype = ap->datatype;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003641
3642 error = xfs_alloc_vextent(&args);
3643 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003644 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003645
Nathan Scotta365bdd2006-03-14 13:34:16 +11003646 if (tryagain && args.fsbno == NULLFSBLOCK) {
3647 /*
3648 * Exact allocation failed. Now try with alignment
3649 * turned on.
3650 */
3651 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003652 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003653 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003654 args.minlen = nextminlen;
3655 args.minalignslop = 0;
3656 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 if ((error = xfs_alloc_vextent(&args)))
3658 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003659 }
3660 if (isaligned && args.fsbno == NULLFSBLOCK) {
3661 /*
3662 * allocation failed, so turn off alignment and
3663 * try again.
3664 */
3665 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003666 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003667 args.alignment = 0;
3668 if ((error = xfs_alloc_vextent(&args)))
3669 return error;
3670 }
3671 if (args.fsbno == NULLFSBLOCK && nullfb &&
3672 args.minlen > ap->minlen) {
3673 args.minlen = ap->minlen;
3674 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003675 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003676 if ((error = xfs_alloc_vextent(&args)))
3677 return error;
3678 }
3679 if (args.fsbno == NULLFSBLOCK && nullfb) {
3680 args.fsbno = 0;
3681 args.type = XFS_ALLOCTYPE_FIRST_AG;
3682 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003683 if ((error = xfs_alloc_vextent(&args)))
3684 return error;
Brian Foster1214f1c2018-08-01 07:20:31 -07003685 ap->tp->t_flags |= XFS_TRANS_LOWMODE;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003686 }
3687 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003688 /*
3689 * check the allocation happened at the same or higher AG than
3690 * the first block that was allocated.
3691 */
Brian Foster94c07b42018-07-11 22:26:28 -07003692 ASSERT(ap->tp->t_firstblock == NULLFSBLOCK ||
3693 XFS_FSB_TO_AGNO(mp, ap->tp->t_firstblock) <=
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003694 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003695
Dave Chinner3a756672011-09-18 20:40:58 +00003696 ap->blkno = args.fsbno;
Brian Foster94c07b42018-07-11 22:26:28 -07003697 if (ap->tp->t_firstblock == NULLFSBLOCK)
3698 ap->tp->t_firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003699 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003700 ap->length = args.len;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003701 /*
3702 * If the extent size hint is active, we tried to round the
3703 * caller's allocation request offset down to extsz and the
3704 * length up to another extsz boundary. If we found a free
3705 * extent we mapped it in starting at this new offset. If the
3706 * newly mapped space isn't long enough to cover any of the
3707 * range of offsets that was originally requested, move the
3708 * mapping up so that we can fill as much of the caller's
3709 * original request as possible. Free space is apparently
3710 * very fragmented so we're unlikely to be able to satisfy the
3711 * hints anyway.
3712 */
3713 if (ap->length <= orig_length)
3714 ap->offset = orig_offset;
3715 else if (ap->offset + ap->length < orig_offset + orig_length)
3716 ap->offset = orig_offset + orig_length - ap->length;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003717 xfs_bmap_btalloc_accounting(ap, &args);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003718 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003719 ap->blkno = NULLFSBLOCK;
3720 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 }
3722 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003723}
3724
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003725/* Trim extent to fit a logical block range. */
3726void
3727xfs_trim_extent(
3728 struct xfs_bmbt_irec *irec,
3729 xfs_fileoff_t bno,
3730 xfs_filblks_t len)
3731{
3732 xfs_fileoff_t distance;
3733 xfs_fileoff_t end = bno + len;
3734
3735 if (irec->br_startoff + irec->br_blockcount <= bno ||
3736 irec->br_startoff >= end) {
3737 irec->br_blockcount = 0;
3738 return;
3739 }
3740
3741 if (irec->br_startoff < bno) {
3742 distance = bno - irec->br_startoff;
3743 if (isnullstartblock(irec->br_startblock))
3744 irec->br_startblock = DELAYSTARTBLOCK;
3745 if (irec->br_startblock != DELAYSTARTBLOCK &&
3746 irec->br_startblock != HOLESTARTBLOCK)
3747 irec->br_startblock += distance;
3748 irec->br_startoff += distance;
3749 irec->br_blockcount -= distance;
3750 }
3751
3752 if (end < irec->br_startoff + irec->br_blockcount) {
3753 distance = irec->br_startoff + irec->br_blockcount - end;
3754 irec->br_blockcount -= distance;
3755 }
3756}
3757
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003759 * Trim the returned map to the required bounds
3760 */
3761STATIC void
3762xfs_bmapi_trim_map(
3763 struct xfs_bmbt_irec *mval,
3764 struct xfs_bmbt_irec *got,
3765 xfs_fileoff_t *bno,
3766 xfs_filblks_t len,
3767 xfs_fileoff_t obno,
3768 xfs_fileoff_t end,
3769 int n,
3770 int flags)
3771{
3772 if ((flags & XFS_BMAPI_ENTIRE) ||
3773 got->br_startoff + got->br_blockcount <= obno) {
3774 *mval = *got;
3775 if (isnullstartblock(got->br_startblock))
3776 mval->br_startblock = DELAYSTARTBLOCK;
3777 return;
3778 }
3779
3780 if (obno > *bno)
3781 *bno = obno;
3782 ASSERT((*bno >= obno) || (n == 0));
3783 ASSERT(*bno < end);
3784 mval->br_startoff = *bno;
3785 if (isnullstartblock(got->br_startblock))
3786 mval->br_startblock = DELAYSTARTBLOCK;
3787 else
3788 mval->br_startblock = got->br_startblock +
3789 (*bno - got->br_startoff);
3790 /*
3791 * Return the minimum of what we got and what we asked for for
3792 * the length. We can use the len variable here because it is
3793 * modified below and we could have been there before coming
3794 * here if the first part of the allocation didn't overlap what
3795 * was asked for.
3796 */
3797 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3798 got->br_blockcount - (*bno - got->br_startoff));
3799 mval->br_state = got->br_state;
3800 ASSERT(mval->br_blockcount <= len);
3801 return;
3802}
3803
3804/*
3805 * Update and validate the extent map to return
3806 */
3807STATIC void
3808xfs_bmapi_update_map(
3809 struct xfs_bmbt_irec **map,
3810 xfs_fileoff_t *bno,
3811 xfs_filblks_t *len,
3812 xfs_fileoff_t obno,
3813 xfs_fileoff_t end,
3814 int *n,
3815 int flags)
3816{
3817 xfs_bmbt_irec_t *mval = *map;
3818
3819 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3820 ((mval->br_startoff + mval->br_blockcount) <= end));
3821 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3822 (mval->br_startoff < obno));
3823
3824 *bno = mval->br_startoff + mval->br_blockcount;
3825 *len = end - *bno;
3826 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3827 /* update previous map with new information */
3828 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3829 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3830 ASSERT(mval->br_state == mval[-1].br_state);
3831 mval[-1].br_blockcount = mval->br_blockcount;
3832 mval[-1].br_state = mval->br_state;
3833 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3834 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3835 mval[-1].br_startblock != HOLESTARTBLOCK &&
3836 mval->br_startblock == mval[-1].br_startblock +
3837 mval[-1].br_blockcount &&
Christoph Hellwigc3a2f9f2018-07-11 22:26:01 -07003838 mval[-1].br_state == mval->br_state) {
Dave Chinneraef9a892011-09-18 20:40:44 +00003839 ASSERT(mval->br_startoff ==
3840 mval[-1].br_startoff + mval[-1].br_blockcount);
3841 mval[-1].br_blockcount += mval->br_blockcount;
3842 } else if (*n > 0 &&
3843 mval->br_startblock == DELAYSTARTBLOCK &&
3844 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3845 mval->br_startoff ==
3846 mval[-1].br_startoff + mval[-1].br_blockcount) {
3847 mval[-1].br_blockcount += mval->br_blockcount;
3848 mval[-1].br_state = mval->br_state;
3849 } else if (!((*n == 0) &&
3850 ((mval->br_startoff + mval->br_blockcount) <=
3851 obno))) {
3852 mval++;
3853 (*n)++;
3854 }
3855 *map = mval;
3856}
3857
3858/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003859 * Map file blocks to filesystem blocks without allocation.
3860 */
3861int
3862xfs_bmapi_read(
3863 struct xfs_inode *ip,
3864 xfs_fileoff_t bno,
3865 xfs_filblks_t len,
3866 struct xfs_bmbt_irec *mval,
3867 int *nmap,
3868 int flags)
3869{
3870 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig4b516ff2020-05-14 14:06:41 -07003871 int whichfork = xfs_bmapi_whichfork(flags);
3872 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003873 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003874 xfs_fileoff_t obno;
3875 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003876 struct xfs_iext_cursor icur;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003877 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003878 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003879 int n = 0;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003880
3881 ASSERT(*nmap >= 1);
Christoph Hellwig1a1c57b2020-05-14 14:06:40 -07003882 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003883 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003884
Christoph Hellwig4b516ff2020-05-14 14:06:41 -07003885 if (WARN_ON_ONCE(!ifp))
3886 return -EFSCORRUPTED;
3887
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07003888 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
3889 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT))
Dave Chinner24513372014-06-25 14:58:08 +10003890 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003891
3892 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003893 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003894
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003895 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003896
Dave Chinner5c8ed202011-09-18 20:40:45 +00003897 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3898 error = xfs_iread_extents(NULL, ip, whichfork);
3899 if (error)
3900 return error;
3901 }
3902
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003903 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003904 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003905 end = bno + len;
3906 obno = bno;
3907
3908 while (bno < end && n < *nmap) {
3909 /* Reading past eof, act as though there's a hole up to end. */
3910 if (eof)
3911 got.br_startoff = end;
3912 if (got.br_startoff > bno) {
3913 /* Reading in a hole. */
3914 mval->br_startoff = bno;
3915 mval->br_startblock = HOLESTARTBLOCK;
3916 mval->br_blockcount =
3917 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3918 mval->br_state = XFS_EXT_NORM;
3919 bno += mval->br_blockcount;
3920 len -= mval->br_blockcount;
3921 mval++;
3922 n++;
3923 continue;
3924 }
3925
3926 /* set up the extent map to return. */
3927 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3928 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3929
3930 /* If we're done, stop now. */
3931 if (bno >= end || n >= *nmap)
3932 break;
3933
3934 /* Else go on to the next record. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003935 if (!xfs_iext_next_extent(ifp, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003936 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003937 }
3938 *nmap = n;
3939 return 0;
3940}
3941
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003942/*
3943 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3944 * global pool and the extent inserted into the inode in-core extent tree.
3945 *
3946 * On entry, got refers to the first extent beyond the offset of the extent to
3947 * allocate or eof is specified if no such extent exists. On return, got refers
3948 * to the extent record that was inserted to the inode fork.
3949 *
3950 * Note that the allocated extent may have been merged with contiguous extents
3951 * during insertion into the inode fork. Thus, got does not reflect the current
3952 * state of the inode fork on return. If necessary, the caller can use lastx to
3953 * look up the updated record in the inode fork.
3954 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10003955int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003956xfs_bmapi_reserve_delalloc(
3957 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003958 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11003959 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003960 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11003961 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003962 struct xfs_bmbt_irec *got,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003963 struct xfs_iext_cursor *icur,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003964 int eof)
3965{
3966 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003967 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003968 xfs_extlen_t alen;
3969 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003970 int error;
Brian Foster974ae922016-11-28 14:57:42 +11003971 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003972
Brian Foster974ae922016-11-28 14:57:42 +11003973 /*
3974 * Cap the alloc length. Keep track of prealloc so we know whether to
3975 * tag the inode before we return.
3976 */
3977 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003978 if (!eof)
3979 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11003980 if (prealloc && alen >= len)
3981 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003982
3983 /* Figure out the extent size, adjust alen */
Shan Hai6ca30722018-01-23 13:56:11 -08003984 if (whichfork == XFS_COW_FORK) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003985 struct xfs_bmbt_irec prev;
Shan Hai6ca30722018-01-23 13:56:11 -08003986 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003987
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003988 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003989 prev.br_startoff = NULLFILEOFF;
3990
Shan Hai6ca30722018-01-23 13:56:11 -08003991 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003992 1, 0, &aoff, &alen);
3993 ASSERT(!error);
3994 }
3995
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003996 /*
3997 * Make a transaction-less quota reservation for delayed allocation
3998 * blocks. This number gets adjusted later. We return if we haven't
3999 * allocated blocks already inside this loop.
4000 */
4001 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
Shan Hai6ca30722018-01-23 13:56:11 -08004002 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004003 if (error)
4004 return error;
4005
4006 /*
4007 * Split changing sb for alen and indlen since they could be coming
4008 * from different places.
4009 */
4010 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4011 ASSERT(indlen > 0);
4012
Shan Hai6ca30722018-01-23 13:56:11 -08004013 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004014 if (error)
4015 goto out_unreserve_quota;
4016
Dave Chinner0d485ad2015-02-23 21:22:03 +11004017 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004018 if (error)
4019 goto out_unreserve_blocks;
4020
4021
4022 ip->i_delayed_blks += alen;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07004023 xfs_mod_delalloc(ip->i_mount, alen + indlen);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004024
4025 got->br_startoff = aoff;
4026 got->br_startblock = nullstartblock(indlen);
4027 got->br_blockcount = alen;
4028 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004029
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004030 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004031
Brian Foster974ae922016-11-28 14:57:42 +11004032 /*
4033 * Tag the inode if blocks were preallocated. Note that COW fork
4034 * preallocation can occur at the start or end of the extent, even when
4035 * prealloc == 0, so we must also check the aligned offset and length.
4036 */
4037 if (whichfork == XFS_DATA_FORK && prealloc)
4038 xfs_inode_set_eofblocks_tag(ip);
4039 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4040 xfs_inode_set_cowblocks_tag(ip);
4041
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004042 return 0;
4043
4044out_unreserve_blocks:
Shan Hai6ca30722018-01-23 13:56:11 -08004045 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004046out_unreserve_quota:
4047 if (XFS_IS_QUOTA_ON(mp))
Shan Hai6ca30722018-01-23 13:56:11 -08004048 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0,
4049 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004050 return error;
4051}
4052
Dave Chinnercf11da92014-07-15 07:08:24 +10004053static int
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004054xfs_bmap_alloc_userdata(
4055 struct xfs_bmalloca *bma)
4056{
4057 struct xfs_mount *mp = bma->ip->i_mount;
4058 int whichfork = xfs_bmapi_whichfork(bma->flags);
4059 int error;
4060
4061 /*
4062 * Set the data type being allocated. For the data fork, the first data
4063 * in the file is treated differently to all other allocations. For the
4064 * attribute fork, we only need to ensure the allocated range is not on
4065 * the busy list.
4066 */
4067 bma->datatype = XFS_ALLOC_NOBUSY;
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004068 if (whichfork == XFS_DATA_FORK) {
Christoph Hellwigc34d5702019-10-30 12:25:00 -07004069 bma->datatype |= XFS_ALLOC_USERDATA;
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004070 if (bma->offset == 0)
4071 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004072
4073 if (mp->m_dalign && bma->length >= mp->m_dalign) {
4074 error = xfs_bmap_isaeof(bma, whichfork);
4075 if (error)
4076 return error;
4077 }
4078
4079 if (XFS_IS_REALTIME_INODE(bma->ip))
4080 return xfs_bmap_rtalloc(bma);
4081 }
4082
4083 return xfs_bmap_btalloc(bma);
4084}
4085
4086static int
Dave Chinnercf11da92014-07-15 07:08:24 +10004087xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004088 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004089{
4090 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004091 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004092 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004093 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004094 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004095
Dave Chinnera99ebf42011-12-01 11:24:20 +00004096 ASSERT(bma->length > 0);
4097
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004098 /*
4099 * For the wasdelay case, we could also just allocate the stuff asked
4100 * for in this bmap call but that wouldn't be as good.
4101 */
4102 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004103 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4104 bma->offset = bma->got.br_startoff;
Darrick J. Wongf5be0842019-11-06 08:53:54 -08004105 if (!xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev))
4106 bma->prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004107 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004108 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004109 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004110 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004111 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004112 }
4113
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004114 if (bma->flags & XFS_BMAPI_CONTIG)
4115 bma->minlen = bma->length;
4116 else
4117 bma->minlen = 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004118
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004119 if (bma->flags & XFS_BMAPI_METADATA)
4120 error = xfs_bmap_btalloc(bma);
4121 else
4122 error = xfs_bmap_alloc_userdata(bma);
4123 if (error || bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004124 return error;
4125
Christoph Hellwigfd638f12019-10-30 12:25:00 -07004126 if (bma->flags & XFS_BMAPI_ZERO) {
4127 error = xfs_zero_extent(bma->ip, bma->blkno, bma->length);
4128 if (error)
4129 return error;
4130 }
4131
Brian Fostercf612de2018-07-11 22:26:29 -07004132 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur)
Dave Chinner29c8d172011-09-18 20:41:00 +00004133 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004134 /*
4135 * Bump the number of extents we've allocated
4136 * in this call.
4137 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004138 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004139
Dave Chinner29c8d172011-09-18 20:41:00 +00004140 if (bma->cur)
Dave Chinner92219c22020-03-10 17:52:53 -07004141 bma->cur->bc_ino.flags =
Dave Chinner8ef54792020-03-10 17:54:38 -07004142 bma->wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004143
Dave Chinner963c30c2011-09-18 20:40:59 +00004144 bma->got.br_startoff = bma->offset;
4145 bma->got.br_startblock = bma->blkno;
4146 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004147 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004148
Darrick J. Wonga5949d32020-05-23 09:43:31 -07004149 if (bma->flags & XFS_BMAPI_PREALLOC)
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004150 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004151
Christoph Hellwigc6534242011-09-18 20:41:05 +00004152 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004153 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004154 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004155 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004156 whichfork, &bma->icur, &bma->cur, &bma->got,
Brian Foster92f9da32018-07-11 22:26:28 -07004157 &bma->logflags, bma->flags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004158
Christoph Hellwigc315c902011-09-18 20:41:02 +00004159 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004160 if (error)
4161 return error;
4162
4163 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004164 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4165 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4166 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004167 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004168 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004169
Dave Chinner963c30c2011-09-18 20:40:59 +00004170 ASSERT(bma->got.br_startoff <= bma->offset);
4171 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4172 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004173 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4174 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004175 return 0;
4176}
4177
Dave Chinnerb447fe52011-09-18 20:40:51 +00004178STATIC int
4179xfs_bmapi_convert_unwritten(
4180 struct xfs_bmalloca *bma,
4181 struct xfs_bmbt_irec *mval,
4182 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004183 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004184{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004185 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004186 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004187 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004188 int error;
4189
Dave Chinnerb447fe52011-09-18 20:40:51 +00004190 /* check if we need to do unwritten->real conversion */
4191 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4192 (flags & XFS_BMAPI_PREALLOC))
4193 return 0;
4194
4195 /* check if we need to do real->unwritten conversion */
4196 if (mval->br_state == XFS_EXT_NORM &&
4197 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4198 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4199 return 0;
4200
4201 /*
4202 * Modify (by adding) the state flag, if writing.
4203 */
4204 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004205 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4206 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004207 bma->ip, whichfork);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004208 }
4209 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4210 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4211
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004212 /*
4213 * Before insertion into the bmbt, zero the range being converted
4214 * if required.
4215 */
4216 if (flags & XFS_BMAPI_ZERO) {
4217 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4218 mval->br_blockcount);
4219 if (error)
4220 return error;
4221 }
4222
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004223 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
Brian Foster92f9da32018-07-11 22:26:28 -07004224 &bma->icur, &bma->cur, mval, &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004225 /*
4226 * Log the inode core unconditionally in the unwritten extent conversion
4227 * path because the conversion might not have done so (e.g., if the
4228 * extent count hasn't changed). We need to make sure the inode is dirty
4229 * in the transaction for the sake of fsync(), even if nothing has
4230 * changed, because fsync() will not force the log for this transaction
4231 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004232 *
4233 * Note: If we're only converting cow fork extents, there aren't
4234 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004235 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004236 if (whichfork != XFS_COW_FORK)
4237 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004238 if (error)
4239 return error;
4240
4241 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004242 * Update our extent pointer, given that
4243 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4244 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004245 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004246 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004247
4248 /*
4249 * We may have combined previously unwritten space with written space,
4250 * so generate another request.
4251 */
4252 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004253 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004254 return 0;
4255}
4256
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004257static inline xfs_extlen_t
4258xfs_bmapi_minleft(
4259 struct xfs_trans *tp,
4260 struct xfs_inode *ip,
4261 int fork)
4262{
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004263 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, fork);
4264
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004265 if (tp && tp->t_firstblock != NULLFSBLOCK)
4266 return 0;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004267 if (ifp->if_format != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004268 return 1;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004269 return be16_to_cpu(ifp->if_broot->bb_level) + 1;
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004270}
4271
4272/*
4273 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4274 * a case where the data is changed, there's an error, and it's not logged so we
4275 * don't shutdown when we should. Don't bother logging extents/btree changes if
4276 * we converted to the other format.
4277 */
4278static void
4279xfs_bmapi_finish(
4280 struct xfs_bmalloca *bma,
4281 int whichfork,
4282 int error)
4283{
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004284 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4285
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004286 if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004287 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004288 bma->logflags &= ~xfs_ilog_fext(whichfork);
4289 else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004290 ifp->if_format != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004291 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4292
4293 if (bma->logflags)
4294 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4295 if (bma->cur)
4296 xfs_btree_del_cursor(bma->cur, error);
4297}
4298
Christoph Hellwig44032802011-09-18 20:40:48 +00004299/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004300 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4301 * extent state if necessary. Details behaviour is controlled by the flags
4302 * parameter. Only allocates blocks from a single allocation group, to avoid
4303 * locking problems.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004305int
4306xfs_bmapi_write(
4307 struct xfs_trans *tp, /* transaction pointer */
4308 struct xfs_inode *ip, /* incore inode */
4309 xfs_fileoff_t bno, /* starting file offs. mapped */
4310 xfs_filblks_t len, /* length to map in file */
4311 int flags, /* XFS_BMAPI_... */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004312 xfs_extlen_t total, /* total blocks needed */
4313 struct xfs_bmbt_irec *mval, /* output: map values */
Brian Foster6e702a52018-07-11 22:26:12 -07004314 int *nmap) /* i/o: mval size/count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315{
Darrick J. Wong4b0bce32019-03-19 08:16:22 -07004316 struct xfs_bmalloca bma = {
4317 .tp = tp,
4318 .ip = ip,
4319 .total = total,
4320 };
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004321 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004322 int whichfork = xfs_bmapi_whichfork(flags);
4323 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004324 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004325 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004326 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004327 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004328 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004329
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004331 xfs_fileoff_t orig_bno; /* original block number value */
4332 int orig_flags; /* original flags arg value */
4333 xfs_filblks_t orig_len; /* original value of len arg */
4334 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4335 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
4337 orig_bno = bno;
4338 orig_len = len;
4339 orig_flags = flags;
4340 orig_mval = mval;
4341 orig_nmap = *nmap;
4342#endif
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004343
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004345 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
Christoph Hellwig26b91c72019-02-18 09:38:48 -08004346 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004347 ASSERT(len > 0);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004348 ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004349 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004350 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004351
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004352 /* zeroing is for currently only for data extents, not metadata */
4353 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4354 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4355 /*
4356 * we can allocate unwritten extents or pre-zero allocated blocks,
4357 * but it makes no sense to do both at once. This would result in
4358 * zeroing the unwritten extent twice, but it still being an
4359 * unwritten extent....
4360 */
4361 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4362 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4363
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004364 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
Darrick J. Wonga71895c2019-11-11 12:53:22 -08004365 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Dave Chinner24513372014-06-25 14:58:08 +10004366 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004368
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004370 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004371
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004372 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004373
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004374 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4375 error = xfs_iread_extents(tp, ip, whichfork);
4376 if (error)
4377 goto error0;
4378 }
4379
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004380 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004381 eof = true;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004382 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004383 bma.prev.br_startoff = NULLFILEOFF;
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004384 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004385
Brian Foster627209f2019-02-01 09:14:23 -08004386 n = 0;
4387 end = bno + len;
4388 obno = bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004390 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004391
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004392 /* in hole or beyond EOF? */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004393 if (eof || bma.got.br_startoff > bno) {
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004394 /*
4395 * CoW fork conversions should /never/ hit EOF or
4396 * holes. There should always be something for us
4397 * to work on.
4398 */
4399 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4400 (flags & XFS_BMAPI_COWFORK)));
4401
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004402 need_alloc = true;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004403 } else if (isnullstartblock(bma.got.br_startblock)) {
4404 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004405 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004406
4407 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 * First, deal with the hole before the allocated space
4409 * that we found, if any.
4410 */
Christoph Hellwig26b91c72019-02-18 09:38:48 -08004411 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004412 bma.eof = eof;
4413 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4414 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004415 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004416 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004417
Dave Chinnera99ebf42011-12-01 11:24:20 +00004418 /*
4419 * There's a 32/64 bit type mismatch between the
4420 * allocation length request (which can be 64 bits in
4421 * length) and the bma length request, which is
4422 * xfs_extlen_t and therefore 32 bits. Hence we have to
4423 * check for 32-bit overflows and handle them here.
4424 */
4425 if (len > (xfs_filblks_t)MAXEXTLEN)
4426 bma.length = MAXEXTLEN;
4427 else
4428 bma.length = len;
4429
4430 ASSERT(len > 0);
4431 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004432 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 if (error)
4434 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004435 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004436 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004437
4438 /*
4439 * If this is a CoW allocation, record the data in
4440 * the refcount btree for orphan recovery.
4441 */
Darrick J. Wong74b4c5d2019-08-26 17:06:04 -07004442 if (whichfork == XFS_COW_FORK)
4443 xfs_refcount_alloc_cow_extent(tp, bma.blkno,
4444 bma.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004446
Dave Chinneraef9a892011-09-18 20:40:44 +00004447 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004448 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4449 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450
Dave Chinnerb447fe52011-09-18 20:40:51 +00004451 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004452 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004453 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004454 continue;
4455 if (error)
4456 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
Dave Chinneraef9a892011-09-18 20:40:44 +00004458 /* update the extent map to return */
4459 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4460
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 /*
4462 * If we're done, stop now. Stop when we've allocated
4463 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4464 * the transaction may get too big.
4465 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004466 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004468
4469 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004470 bma.prev = bma.got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004471 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004472 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004475
Christoph Hellwigb101e332019-02-15 08:02:47 -08004476 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4477 whichfork);
4478 if (error)
4479 goto error0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004480
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004481 ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwigdaf83962020-05-18 10:27:22 -07004482 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork));
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004483 xfs_bmapi_finish(&bma, whichfork, 0);
4484 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4485 orig_nmap, *nmap);
4486 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487error0:
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004488 xfs_bmapi_finish(&bma, whichfork, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 return error;
4490}
4491
Brian Foster627209f2019-02-01 09:14:23 -08004492/*
4493 * Convert an existing delalloc extent to real blocks based on file offset. This
4494 * attempts to allocate the entire delalloc extent and may require multiple
4495 * invocations to allocate the target offset if a large enough physical extent
4496 * is not available.
4497 */
4498int
4499xfs_bmapi_convert_delalloc(
Brian Foster627209f2019-02-01 09:14:23 -08004500 struct xfs_inode *ip,
Brian Foster627209f2019-02-01 09:14:23 -08004501 int whichfork,
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004502 xfs_off_t offset,
4503 struct iomap *iomap,
Christoph Hellwig491ce612019-02-15 08:02:49 -08004504 unsigned int *seq)
Brian Foster627209f2019-02-01 09:14:23 -08004505{
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004506 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004507 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004508 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004509 struct xfs_bmalloca bma = { NULL };
Darrick J. Wongaf952ae2019-12-16 11:14:09 -08004510 uint16_t flags = 0;
Christoph Hellwig491ce612019-02-15 08:02:49 -08004511 struct xfs_trans *tp;
Brian Foster627209f2019-02-01 09:14:23 -08004512 int error;
Brian Foster627209f2019-02-01 09:14:23 -08004513
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004514 if (whichfork == XFS_COW_FORK)
4515 flags |= IOMAP_F_SHARED;
4516
Christoph Hellwig491ce612019-02-15 08:02:49 -08004517 /*
4518 * Space for the extent and indirect blocks was reserved when the
4519 * delalloc extent was created so there's no need to do so here.
4520 */
4521 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4522 XFS_TRANS_RESERVE, &tp);
4523 if (error)
4524 return error;
4525
4526 xfs_ilock(ip, XFS_ILOCK_EXCL);
4527 xfs_trans_ijoin(tp, ip, 0);
4528
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004529 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4530 bma.got.br_startoff > offset_fsb) {
4531 /*
4532 * No extent found in the range we are trying to convert. This
4533 * should only happen for the COW fork, where another thread
4534 * might have moved the extent to the data fork in the meantime.
4535 */
4536 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004537 error = -EAGAIN;
4538 goto out_trans_cancel;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004539 }
Brian Foster627209f2019-02-01 09:14:23 -08004540
4541 /*
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004542 * If we find a real extent here we raced with another thread converting
4543 * the extent. Just return the real extent at this offset.
Brian Foster627209f2019-02-01 09:14:23 -08004544 */
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004545 if (!isnullstartblock(bma.got.br_startblock)) {
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004546 xfs_bmbt_to_iomap(ip, iomap, &bma.got, flags);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004547 *seq = READ_ONCE(ifp->if_seq);
4548 goto out_trans_cancel;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004549 }
4550
4551 bma.tp = tp;
4552 bma.ip = ip;
4553 bma.wasdel = true;
4554 bma.offset = bma.got.br_startoff;
4555 bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount, MAXEXTLEN);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004556 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
Darrick J. Wonga5949d32020-05-23 09:43:31 -07004557
4558 /*
4559 * When we're converting the delalloc reservations backing dirty pages
4560 * in the page cache, we must be careful about how we create the new
4561 * extents:
4562 *
4563 * New CoW fork extents are created unwritten, turned into real extents
4564 * when we're about to write the data to disk, and mapped into the data
4565 * fork after the write finishes. End of story.
4566 *
4567 * New data fork extents must be mapped in as unwritten and converted
4568 * to real extents after the write succeeds to avoid exposing stale
4569 * disk contents if we crash.
4570 */
4571 bma.flags = XFS_BMAPI_PREALLOC;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004572 if (whichfork == XFS_COW_FORK)
Darrick J. Wonga5949d32020-05-23 09:43:31 -07004573 bma.flags |= XFS_BMAPI_COWFORK;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004574
4575 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4576 bma.prev.br_startoff = NULLFILEOFF;
4577
4578 error = xfs_bmapi_allocate(&bma);
4579 if (error)
4580 goto out_finish;
4581
4582 error = -ENOSPC;
4583 if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK))
4584 goto out_finish;
4585 error = -EFSCORRUPTED;
Christoph Hellwigeb77b232019-09-03 08:13:13 -07004586 if (WARN_ON_ONCE(!xfs_valid_startblock(ip, bma.got.br_startblock)))
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004587 goto out_finish;
4588
Christoph Hellwig125851a2019-02-15 08:02:49 -08004589 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4590 XFS_STATS_INC(mp, xs_xstrat_quick);
4591
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004592 ASSERT(!isnullstartblock(bma.got.br_startblock));
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004593 xfs_bmbt_to_iomap(ip, iomap, &bma.got, flags);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004594 *seq = READ_ONCE(ifp->if_seq);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004595
Darrick J. Wong74b4c5d2019-08-26 17:06:04 -07004596 if (whichfork == XFS_COW_FORK)
4597 xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004598
4599 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4600 whichfork);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004601 if (error)
4602 goto out_finish;
4603
4604 xfs_bmapi_finish(&bma, whichfork, 0);
4605 error = xfs_trans_commit(tp);
4606 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4607 return error;
4608
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004609out_finish:
4610 xfs_bmapi_finish(&bma, whichfork, error);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004611out_trans_cancel:
4612 xfs_trans_cancel(tp);
4613 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Brian Foster627209f2019-02-01 09:14:23 -08004614 return error;
4615}
4616
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004617int
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004618xfs_bmapi_remap(
4619 struct xfs_trans *tp,
4620 struct xfs_inode *ip,
4621 xfs_fileoff_t bno,
4622 xfs_filblks_t len,
4623 xfs_fsblock_t startblock,
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004624 int flags)
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004625{
4626 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004627 struct xfs_ifork *ifp;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004628 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004629 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004630 struct xfs_iext_cursor icur;
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004631 int whichfork = xfs_bmapi_whichfork(flags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004632 int logflags = 0, error;
4633
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004634 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004635 ASSERT(len > 0);
4636 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4637 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Darrick J. Wong7644bd92018-05-14 06:34:35 -07004638 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4639 XFS_BMAPI_NORMAP)));
4640 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4641 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004642
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004643 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
Darrick J. Wonga71895c2019-11-11 12:53:22 -08004644 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004645 return -EFSCORRUPTED;
4646 }
4647
4648 if (XFS_FORCED_SHUTDOWN(mp))
4649 return -EIO;
4650
4651 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004652 error = xfs_iread_extents(tp, ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004653 if (error)
4654 return error;
4655 }
4656
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004657 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004658 /* make sure we only reflink into a hole. */
4659 ASSERT(got.br_startoff > bno);
4660 ASSERT(got.br_startoff - bno >= len);
4661 }
4662
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004663 ip->i_d.di_nblocks += len;
4664 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004665
4666 if (ifp->if_flags & XFS_IFBROOT) {
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004667 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner92219c22020-03-10 17:52:53 -07004668 cur->bc_ino.flags = 0;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004669 }
4670
4671 got.br_startoff = bno;
4672 got.br_startblock = startblock;
4673 got.br_blockcount = len;
Darrick J. Wong7644bd92018-05-14 06:34:35 -07004674 if (flags & XFS_BMAPI_PREALLOC)
4675 got.br_state = XFS_EXT_UNWRITTEN;
4676 else
4677 got.br_state = XFS_EXT_NORM;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004678
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004679 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
Brian Foster92f9da32018-07-11 22:26:28 -07004680 &cur, &got, &logflags, flags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004681 if (error)
4682 goto error0;
4683
Christoph Hellwigb101e332019-02-15 08:02:47 -08004684 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004685
4686error0:
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004687 if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004688 logflags &= ~XFS_ILOG_DEXT;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07004689 else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE)
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004690 logflags &= ~XFS_ILOG_DBROOT;
4691
4692 if (logflags)
4693 xfs_trans_log_inode(tp, ip, logflags);
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07004694 if (cur)
4695 xfs_btree_del_cursor(cur, error);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004696 return error;
4697}
4698
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004700 * When a delalloc extent is split (e.g., due to a hole punch), the original
4701 * indlen reservation must be shared across the two new extents that are left
4702 * behind.
4703 *
4704 * Given the original reservation and the worst case indlen for the two new
4705 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004706 * reservation fairly across the two new extents. If necessary, steal available
4707 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4708 * ores == 1). The number of stolen blocks is returned. The availability and
4709 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004710 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004711static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004712xfs_bmap_split_indlen(
4713 xfs_filblks_t ores, /* original res. */
4714 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004715 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4716 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004717{
4718 xfs_filblks_t len1 = *indlen1;
4719 xfs_filblks_t len2 = *indlen2;
4720 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004721 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004722 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004723
4724 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004725 * Steal as many blocks as we can to try and satisfy the worst case
4726 * indlen for both new extents.
4727 */
Brian Foster75d65362017-02-13 22:48:30 -08004728 if (ores < nres && avail)
4729 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4730 ores += stolen;
4731
4732 /* nothing else to do if we've satisfied the new reservation */
4733 if (ores >= nres)
4734 return stolen;
Brian Fosterd34999c2016-03-15 11:42:47 +11004735
4736 /*
Brian Foster75d65362017-02-13 22:48:30 -08004737 * We can't meet the total required reservation for the two extents.
4738 * Calculate the percent of the overall shortage between both extents
4739 * and apply this percentage to each of the requested indlen values.
4740 * This distributes the shortage fairly and reduces the chances that one
4741 * of the two extents is left with nothing when extents are repeatedly
4742 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004743 */
Brian Foster75d65362017-02-13 22:48:30 -08004744 resfactor = (ores * 100);
4745 do_div(resfactor, nres);
4746 len1 *= resfactor;
4747 do_div(len1, 100);
4748 len2 *= resfactor;
4749 do_div(len2, 100);
4750 ASSERT(len1 + len2 <= ores);
4751 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4752
4753 /*
4754 * Hand out the remainder to each extent. If one of the two reservations
4755 * is zero, we want to make sure that one gets a block first. The loop
4756 * below starts with len1, so hand len2 a block right off the bat if it
4757 * is zero.
4758 */
4759 ores -= (len1 + len2);
4760 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4761 if (ores && !len2 && *indlen2) {
4762 len2++;
4763 ores--;
4764 }
4765 while (ores) {
4766 if (len1 < *indlen1) {
4767 len1++;
4768 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004769 }
Brian Foster75d65362017-02-13 22:48:30 -08004770 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004771 break;
Brian Foster75d65362017-02-13 22:48:30 -08004772 if (len2 < *indlen2) {
4773 len2++;
4774 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004775 }
4776 }
4777
4778 *indlen1 = len1;
4779 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004780
4781 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004782}
4783
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004784int
4785xfs_bmap_del_extent_delay(
4786 struct xfs_inode *ip,
4787 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004788 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004789 struct xfs_bmbt_irec *got,
4790 struct xfs_bmbt_irec *del)
4791{
4792 struct xfs_mount *mp = ip->i_mount;
4793 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4794 struct xfs_bmbt_irec new;
4795 int64_t da_old, da_new, da_diff = 0;
4796 xfs_fileoff_t del_endoff, got_endoff;
4797 xfs_filblks_t got_indlen, new_indlen, stolen;
Christoph Hellwig060ea652017-10-19 11:02:29 -07004798 int state = xfs_bmap_fork_to_state(whichfork);
4799 int error = 0;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004800 bool isrt;
4801
4802 XFS_STATS_INC(mp, xs_del_exlist);
4803
4804 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4805 del_endoff = del->br_startoff + del->br_blockcount;
4806 got_endoff = got->br_startoff + got->br_blockcount;
4807 da_old = startblockval(got->br_startblock);
4808 da_new = 0;
4809
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004810 ASSERT(del->br_blockcount > 0);
4811 ASSERT(got->br_startoff <= del->br_startoff);
4812 ASSERT(got_endoff >= del_endoff);
4813
4814 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004815 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004816
4817 do_div(rtexts, mp->m_sb.sb_rextsize);
4818 xfs_mod_frextents(mp, rtexts);
4819 }
4820
4821 /*
4822 * Update the inode delalloc counter now and wait to update the
4823 * sb counters as we might have to borrow some blocks for the
4824 * indirect block accounting.
4825 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004826 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4827 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004828 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004829 if (error)
4830 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004831 ip->i_delayed_blks -= del->br_blockcount;
4832
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004833 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004834 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004835 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004836 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004837
Christoph Hellwig0173c682017-10-17 14:16:22 -07004838 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4839 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004840 /*
4841 * Matches the whole extent. Delete the entry.
4842 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004843 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004844 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004845 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004846 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004847 /*
4848 * Deleting the first part of the extent.
4849 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004850 got->br_startoff = del_endoff;
4851 got->br_blockcount -= del->br_blockcount;
4852 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4853 got->br_blockcount), da_old);
4854 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004855 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004856 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004857 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004858 /*
4859 * Deleting the last part of the extent.
4860 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004861 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4862 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4863 got->br_blockcount), da_old);
4864 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004865 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004866 break;
4867 case 0:
4868 /*
4869 * Deleting the middle of the extent.
4870 *
4871 * Distribute the original indlen reservation across the two new
4872 * extents. Steal blocks from the deleted extent if necessary.
4873 * Stealing blocks simply fudges the fdblocks accounting below.
4874 * Warn if either of the new indlen reservations is zero as this
4875 * can lead to delalloc problems.
4876 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004877 got->br_blockcount = del->br_startoff - got->br_startoff;
4878 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4879
4880 new.br_blockcount = got_endoff - del_endoff;
4881 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4882
4883 WARN_ON_ONCE(!got_indlen || !new_indlen);
4884 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4885 del->br_blockcount);
4886
4887 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004888
4889 new.br_startoff = del_endoff;
4890 new.br_state = got->br_state;
4891 new.br_startblock = nullstartblock((int)new_indlen);
4892
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004893 xfs_iext_update_extent(ip, state, icur, got);
4894 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004895 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004896
4897 da_new = got_indlen + new_indlen - stolen;
4898 del->br_blockcount -= stolen;
4899 break;
4900 }
4901
4902 ASSERT(da_old >= da_new);
4903 da_diff = da_old - da_new;
4904 if (!isrt)
4905 da_diff += del->br_blockcount;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07004906 if (da_diff) {
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004907 xfs_mod_fdblocks(mp, da_diff, false);
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07004908 xfs_mod_delalloc(mp, -da_diff);
4909 }
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004910 return error;
4911}
4912
4913void
4914xfs_bmap_del_extent_cow(
4915 struct xfs_inode *ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004916 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004917 struct xfs_bmbt_irec *got,
4918 struct xfs_bmbt_irec *del)
4919{
4920 struct xfs_mount *mp = ip->i_mount;
4921 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4922 struct xfs_bmbt_irec new;
4923 xfs_fileoff_t del_endoff, got_endoff;
4924 int state = BMAP_COWFORK;
4925
4926 XFS_STATS_INC(mp, xs_del_exlist);
4927
4928 del_endoff = del->br_startoff + del->br_blockcount;
4929 got_endoff = got->br_startoff + got->br_blockcount;
4930
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004931 ASSERT(del->br_blockcount > 0);
4932 ASSERT(got->br_startoff <= del->br_startoff);
4933 ASSERT(got_endoff >= del_endoff);
4934 ASSERT(!isnullstartblock(got->br_startblock));
4935
4936 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004937 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004938 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004939 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004940
Christoph Hellwig0173c682017-10-17 14:16:22 -07004941 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4942 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004943 /*
4944 * Matches the whole extent. Delete the entry.
4945 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004946 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004947 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004948 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004949 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004950 /*
4951 * Deleting the first part of the extent.
4952 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004953 got->br_startoff = del_endoff;
4954 got->br_blockcount -= del->br_blockcount;
4955 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004956 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004957 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004958 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004959 /*
4960 * Deleting the last part of the extent.
4961 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004962 got->br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004963 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004964 break;
4965 case 0:
4966 /*
4967 * Deleting the middle of the extent.
4968 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004969 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004970
4971 new.br_startoff = del_endoff;
4972 new.br_blockcount = got_endoff - del_endoff;
4973 new.br_state = got->br_state;
4974 new.br_startblock = del->br_startblock + del->br_blockcount;
4975
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004976 xfs_iext_update_extent(ip, state, icur, got);
4977 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004978 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004979 break;
4980 }
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08004981 ip->i_delayed_blks -= del->br_blockcount;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004982}
4983
Brian Fostera9bd24a2016-03-15 11:42:46 +11004984/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11004985 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07004986 * after removing space.
Dave Chinner9e5987a72013-02-25 12:31:26 +11004987 */
4988STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07004989xfs_bmap_del_extent_real(
Dave Chinner9e5987a72013-02-25 12:31:26 +11004990 xfs_inode_t *ip, /* incore inode pointer */
4991 xfs_trans_t *tp, /* current transaction pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004992 struct xfs_iext_cursor *icur,
Dave Chinner9e5987a72013-02-25 12:31:26 +11004993 xfs_btree_cur_t *cur, /* if null, not a btree */
4994 xfs_bmbt_irec_t *del, /* data to remove from extents */
4995 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07004996 int whichfork, /* data or attr fork */
4997 int bflags) /* bmapi flags */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004998{
Dave Chinner9e5987a72013-02-25 12:31:26 +11004999 xfs_fsblock_t del_endblock=0; /* first block past del */
5000 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005001 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005002 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07005003 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005004 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005005 xfs_fileoff_t got_endoff; /* first offset past got */
5006 int i; /* temp state */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07005007 struct xfs_ifork *ifp; /* inode fork pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005008 xfs_mount_t *mp; /* mount structure */
5009 xfs_filblks_t nblks; /* quota/sb block count */
5010 xfs_bmbt_irec_t new; /* new record to be inserted */
5011 /* REFERENCED */
5012 uint qfield; /* quota field to update */
Christoph Hellwig060ea652017-10-19 11:02:29 -07005013 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005014 struct xfs_bmbt_irec old;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005015
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005016 mp = ip->i_mount;
5017 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005018
Dave Chinner9e5987a72013-02-25 12:31:26 +11005019 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005020 ASSERT(del->br_blockcount > 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005021 xfs_iext_get_extent(ifp, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005022 ASSERT(got.br_startoff <= del->br_startoff);
5023 del_endoff = del->br_startoff + del->br_blockcount;
5024 got_endoff = got.br_startoff + got.br_blockcount;
5025 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005026 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a72013-02-25 12:31:26 +11005027 qfield = 0;
5028 error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005029
Christoph Hellwig1b24b632017-10-17 14:16:22 -07005030 /*
5031 * If it's the case where the directory code is running with no block
5032 * reservation, and the deleted block is in the middle of its extent,
5033 * and the resulting insert of an extent would cause transformation to
5034 * btree format, then reject it. The calling code will then swap blocks
5035 * around instead. We have to do this now, rather than waiting for the
5036 * conversion to btree format, since the transaction will be dirty then.
5037 */
5038 if (tp->t_blk_res == 0 &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005039 ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005040 ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) &&
Christoph Hellwig1b24b632017-10-17 14:16:22 -07005041 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
5042 return -ENOSPC;
5043
5044 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005045 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5046 xfs_fsblock_t bno;
5047 xfs_filblks_t len;
Dave Chinner0703a8e2018-06-08 09:54:22 -07005048 xfs_extlen_t mod;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005049
Dave Chinner0703a8e2018-06-08 09:54:22 -07005050 bno = div_u64_rem(del->br_startblock, mp->m_sb.sb_rextsize,
5051 &mod);
5052 ASSERT(mod == 0);
5053 len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize,
5054 &mod);
5055 ASSERT(mod == 0);
5056
Christoph Hellwige1d75532017-10-17 14:16:21 -07005057 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5058 if (error)
5059 goto done;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005060 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005061 nblks = len * mp->m_sb.sb_rextsize;
5062 qfield = XFS_TRANS_DQ_RTBCOUNT;
5063 } else {
5064 do_fx = 1;
5065 nblks = del->br_blockcount;
5066 qfield = XFS_TRANS_DQ_BCOUNT;
5067 }
5068
5069 del_endblock = del->br_startblock + del->br_blockcount;
5070 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005071 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005072 if (error)
5073 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005074 if (XFS_IS_CORRUPT(mp, i != 1)) {
5075 error = -EFSCORRUPTED;
5076 goto done;
5077 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11005078 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005079
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005080 if (got.br_startoff == del->br_startoff)
5081 state |= BMAP_LEFT_FILLING;
5082 if (got_endoff == del_endoff)
5083 state |= BMAP_RIGHT_FILLING;
5084
5085 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5086 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005087 /*
5088 * Matches the whole extent. Delete the entry.
5089 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005090 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005091 xfs_iext_prev(ifp, icur);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005092 ifp->if_nextents--;
5093
Dave Chinner9e5987a72013-02-25 12:31:26 +11005094 flags |= XFS_ILOG_CORE;
5095 if (!cur) {
5096 flags |= xfs_ilog_fext(whichfork);
5097 break;
5098 }
5099 if ((error = xfs_btree_delete(cur, &i)))
5100 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005101 if (XFS_IS_CORRUPT(mp, i != 1)) {
5102 error = -EFSCORRUPTED;
5103 goto done;
5104 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11005105 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005106 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005107 /*
5108 * Deleting the first part of the extent.
5109 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005110 got.br_startoff = del_endoff;
5111 got.br_startblock = del_endblock;
5112 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005113 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005114 if (!cur) {
5115 flags |= xfs_ilog_fext(whichfork);
5116 break;
5117 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005118 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005119 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005120 goto done;
5121 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005122 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005123 /*
5124 * Deleting the last part of the extent.
5125 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005126 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005127 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005128 if (!cur) {
5129 flags |= xfs_ilog_fext(whichfork);
5130 break;
5131 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005132 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005133 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005134 goto done;
5135 break;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005136 case 0:
5137 /*
5138 * Deleting the middle of the extent.
5139 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005140 old = got;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07005141
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005142 got.br_blockcount = del->br_startoff - got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005143 xfs_iext_update_extent(ip, state, icur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005144
Dave Chinner9e5987a72013-02-25 12:31:26 +11005145 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005146 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005147 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005148 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005149
Christoph Hellwige1d75532017-10-17 14:16:21 -07005150 flags |= XFS_ILOG_CORE;
5151 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005152 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005153 if (error)
5154 goto done;
5155 error = xfs_btree_increment(cur, 0, &i);
5156 if (error)
5157 goto done;
5158 cur->bc_rec.b = new;
5159 error = xfs_btree_insert(cur, &i);
5160 if (error && error != -ENOSPC)
5161 goto done;
5162 /*
5163 * If get no-space back from btree insert, it tried a
5164 * split, and we have a zero block reservation. Fix up
5165 * our state and return the error.
5166 */
5167 if (error == -ENOSPC) {
5168 /*
5169 * Reset the cursor, don't trust it after any
5170 * insert operation.
5171 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005172 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005173 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005174 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005175 if (XFS_IS_CORRUPT(mp, i != 1)) {
5176 error = -EFSCORRUPTED;
5177 goto done;
5178 }
Christoph Hellwige1d75532017-10-17 14:16:21 -07005179 /*
5180 * Update the btree record back
5181 * to the original value.
5182 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005183 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005184 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005185 goto done;
5186 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005187 * Reset the extent record back
5188 * to the original value.
Dave Chinner9e5987a72013-02-25 12:31:26 +11005189 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005190 xfs_iext_update_extent(ip, state, icur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005191 flags = 0;
5192 error = -ENOSPC;
5193 goto done;
5194 }
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005195 if (XFS_IS_CORRUPT(mp, i != 1)) {
5196 error = -EFSCORRUPTED;
5197 goto done;
5198 }
Christoph Hellwige1d75532017-10-17 14:16:21 -07005199 } else
5200 flags |= xfs_ilog_fext(whichfork);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005201
5202 ifp->if_nextents++;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005203 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005204 xfs_iext_insert(ip, icur, &new, state);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005205 break;
5206 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005207
5208 /* remove reverse mapping */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07005209 xfs_rmap_unmap_extent(tp, ip, whichfork, del);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005210
Dave Chinner9e5987a72013-02-25 12:31:26 +11005211 /*
5212 * If we need to, add to list of extents to delete.
5213 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005214 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005215 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
Darrick J. Wong74b4c5d2019-08-26 17:06:04 -07005216 xfs_refcount_decrease_extent(tp, del);
Brian Fosterfcb762f2018-05-09 08:45:04 -07005217 } else {
Brian Foster0f37d172018-08-01 07:20:34 -07005218 __xfs_bmap_add_free(tp, del->br_startblock,
Brian Foster4e529332018-05-10 09:35:42 -07005219 del->br_blockcount, NULL,
5220 (bflags & XFS_BMAPI_NODISCARD) ||
5221 del->br_state == XFS_EXT_UNWRITTEN);
Brian Fosterfcb762f2018-05-09 08:45:04 -07005222 }
Darrick J. Wong62aab202016-10-03 09:11:23 -07005223 }
5224
Dave Chinner9e5987a72013-02-25 12:31:26 +11005225 /*
5226 * Adjust inode # blocks in the file.
5227 */
5228 if (nblks)
5229 ip->i_d.di_nblocks -= nblks;
5230 /*
5231 * Adjust quota data.
5232 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005233 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a72013-02-25 12:31:26 +11005234 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5235
Dave Chinner9e5987a72013-02-25 12:31:26 +11005236done:
5237 *logflagsp = flags;
5238 return error;
5239}
5240
5241/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 * Unmap (remove) blocks from a file.
5243 * If nexts is nonzero then the number of extents to remove is limited to
5244 * that value. If not all extents in the block range can be removed then
5245 * *done is set.
5246 */
5247int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005248__xfs_bunmapi(
Brian Fosterccd9d912018-07-11 22:26:13 -07005249 struct xfs_trans *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005251 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005252 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 int flags, /* misc flags */
Brian Foster2af52842018-07-11 22:26:25 -07005254 xfs_extnum_t nexts) /* number of extents max */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255{
Brian Fosterccd9d912018-07-11 22:26:13 -07005256 struct xfs_btree_cur *cur; /* bmap btree cursor */
5257 struct xfs_bmbt_irec del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 int error; /* error return value */
5259 xfs_extnum_t extno; /* extent number in list */
Brian Fosterccd9d912018-07-11 22:26:13 -07005260 struct xfs_bmbt_irec got; /* current extent record */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07005261 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 int isrt; /* freeing in rt area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 int logflags; /* transaction logging flags */
5264 xfs_extlen_t mod; /* rt extent offset */
Darrick J. Wonga71895c2019-11-11 12:53:22 -08005265 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 int tmp_logflags; /* partial logging flags */
5267 int wasdel; /* was a delayed alloc extent */
5268 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005270 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005271 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005272 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005273 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005274 struct xfs_iext_cursor icur;
5275 bool done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005277 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005278
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005279 whichfork = xfs_bmapi_whichfork(flags);
5280 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005282 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)))
Dave Chinner24513372014-06-25 14:58:08 +10005283 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005285 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005286
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005287 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 ASSERT(len > 0);
5289 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005290
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005291 /*
5292 * Guesstimate how many blocks we can unmap without running the risk of
5293 * blowing out the transaction with a mix of EFIs and reflink
5294 * adjustments.
5295 */
Darrick J. Wong8c57b882017-12-10 18:03:53 -08005296 if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005297 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5298 else
5299 max_len = len;
5300
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5302 (error = xfs_iread_extents(tp, ip, whichfork)))
5303 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005304 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005305 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 return 0;
5307 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005308 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005309 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwigdc560152017-10-23 16:32:39 -07005310 end = start + len;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005311
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005312 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
Christoph Hellwigdc560152017-10-23 16:32:39 -07005313 *rlen = 0;
5314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 }
Christoph Hellwigdc560152017-10-23 16:32:39 -07005316 end--;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005317
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 logflags = 0;
5319 if (ifp->if_flags & XFS_IFBROOT) {
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005320 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005321 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner92219c22020-03-10 17:52:53 -07005322 cur->bc_ino.flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 } else
5324 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005325
5326 if (isrt) {
5327 /*
5328 * Synchronize by locking the bitmap inode.
5329 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005330 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005331 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005332 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5333 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005334 }
5335
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 extno = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005337 while (end != (xfs_fileoff_t)-1 && end >= start &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005338 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005340 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 * Just back up to the previous extent, if so.
5342 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005343 if (got.br_startoff > end &&
5344 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5345 done = true;
5346 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 }
5348 /*
5349 * Is the last block of this extent before the range
5350 * we're supposed to delete? If so, we're done.
5351 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005352 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005354 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 break;
5356 /*
5357 * Then deal with the (possibly delayed) allocated space
5358 * we found.
5359 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005361 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005362
5363 /*
5364 * Make sure we don't touch multiple AGF headers out of order
5365 * in a single transaction, as that could cause AB-BA deadlocks.
5366 */
Omar Sandoval69ffe592019-11-26 16:58:08 -08005367 if (!wasdel && !isrt) {
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005368 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5369 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5370 break;
5371 prev_agno = agno;
5372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 if (got.br_startoff < start) {
5374 del.br_startoff = start;
5375 del.br_blockcount -= start - got.br_startoff;
5376 if (!wasdel)
5377 del.br_startblock += start - got.br_startoff;
5378 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005379 if (del.br_startoff + del.br_blockcount > end + 1)
5380 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005381
5382 /* How much can we safely unmap? */
5383 if (max_len < del.br_blockcount) {
5384 del.br_startoff += del.br_blockcount - max_len;
5385 if (!wasdel)
5386 del.br_startblock += del.br_blockcount - max_len;
5387 del.br_blockcount = max_len;
5388 }
5389
Dave Chinner0703a8e2018-06-08 09:54:22 -07005390 if (!isrt)
5391 goto delete;
5392
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 sum = del.br_startblock + del.br_blockcount;
Dave Chinner0703a8e2018-06-08 09:54:22 -07005394 div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod);
5395 if (mod) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 /*
5397 * Realtime extent not lined up at the end.
5398 * The extent could have been split into written
5399 * and unwritten pieces, or we could just be
5400 * unmapping part of it. But we can't really
5401 * get rid of part of a realtime extent.
5402 */
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11005403 if (del.br_state == XFS_EXT_UNWRITTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 /*
5405 * This piece is unwritten, or we're not
5406 * using unwritten extents. Skip over it.
5407 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005408 ASSERT(end >= mod);
5409 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 del.br_blockcount : mod;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005411 if (end < got.br_startoff &&
5412 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5413 done = true;
5414 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 }
5416 continue;
5417 }
5418 /*
5419 * It's written, turn it unwritten.
5420 * This is better than zeroing it.
5421 */
5422 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005423 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 /*
5425 * If this spans a realtime extent boundary,
5426 * chop it back to the start of the one we end at.
5427 */
5428 if (del.br_blockcount > mod) {
5429 del.br_startoff += del.br_blockcount - mod;
5430 del.br_startblock += del.br_blockcount - mod;
5431 del.br_blockcount = mod;
5432 }
5433 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005434 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005435 whichfork, &icur, &cur, &del,
Brian Foster92f9da32018-07-11 22:26:28 -07005436 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 if (error)
5438 goto error0;
5439 goto nodelete;
5440 }
Dave Chinner0703a8e2018-06-08 09:54:22 -07005441 div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod);
5442 if (mod) {
Omar Sandoval0c4da702019-11-26 16:58:07 -08005443 xfs_extlen_t off = mp->m_sb.sb_rextsize - mod;
5444
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 /*
5446 * Realtime extent is lined up at the end but not
5447 * at the front. We'll get rid of full extents if
5448 * we can.
5449 */
Omar Sandoval0c4da702019-11-26 16:58:07 -08005450 if (del.br_blockcount > off) {
5451 del.br_blockcount -= off;
5452 del.br_startoff += off;
5453 del.br_startblock += off;
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11005454 } else if (del.br_startoff == start &&
5455 (del.br_state == XFS_EXT_UNWRITTEN ||
5456 tp->t_blk_res == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 /*
5458 * Can't make it unwritten. There isn't
5459 * a full extent here so just skip it.
5460 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005461 ASSERT(end >= del.br_blockcount);
5462 end -= del.br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005463 if (got.br_startoff > end &&
5464 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5465 done = true;
5466 break;
5467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 continue;
5469 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005470 struct xfs_bmbt_irec prev;
Omar Sandoval0c4da702019-11-26 16:58:07 -08005471 xfs_fileoff_t unwrite_start;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005472
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 /*
5474 * This one is already unwritten.
5475 * It must have a written left neighbor.
5476 * Unwrite the killed part of that one and
5477 * try again.
5478 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005479 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5480 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005482 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 ASSERT(del.br_startblock ==
5484 prev.br_startblock + prev.br_blockcount);
Omar Sandoval0c4da702019-11-26 16:58:07 -08005485 unwrite_start = max3(start,
5486 del.br_startoff - mod,
5487 prev.br_startoff);
5488 mod = unwrite_start - prev.br_startoff;
5489 prev.br_startoff = unwrite_start;
5490 prev.br_startblock += mod;
5491 prev.br_blockcount -= mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005493 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005494 ip, whichfork, &icur, &cur,
Brian Foster92f9da32018-07-11 22:26:28 -07005495 &prev, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 if (error)
5497 goto error0;
5498 goto nodelete;
5499 } else {
5500 ASSERT(del.br_state == XFS_EXT_NORM);
5501 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005502 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005503 ip, whichfork, &icur, &cur,
Brian Foster92f9da32018-07-11 22:26:28 -07005504 &del, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 if (error)
5506 goto error0;
5507 goto nodelete;
5508 }
5509 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005510
Dave Chinner0703a8e2018-06-08 09:54:22 -07005511delete:
Brian Fosterb2706a02016-03-15 11:42:46 +11005512 if (wasdel) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005513 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005514 &got, &del);
5515 } else {
Brian Foster81ba8f32018-07-11 22:26:16 -07005516 error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5517 &del, &tmp_logflags, whichfork,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005518 flags);
5519 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005520 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005521
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 if (error)
5523 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005524
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005525 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005526 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 /*
5529 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005531 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005532 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5533 (got.br_startoff > end &&
5534 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5535 done = true;
5536 break;
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 extno++;
5539 }
5540 }
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005541 if (done || end == (xfs_fileoff_t)-1 || end < start)
Darrick J. Wong44535932016-10-03 09:11:29 -07005542 *rlen = 0;
5543 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005544 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005545
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 /*
5547 * Convert to a btree if necessary.
5548 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005549 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07005551 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5552 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 logflags |= tmp_logflags;
Christoph Hellwigb101e332019-02-15 08:02:47 -08005554 } else {
5555 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 }
Christoph Hellwigb101e332019-02-15 08:02:47 -08005558
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559error0:
5560 /*
5561 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005562 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005564 if ((logflags & xfs_ilog_fext(whichfork)) &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005565 ifp->if_format != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005566 logflags &= ~xfs_ilog_fext(whichfork);
5567 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005568 ifp->if_format != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005569 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 /*
5571 * Log inode even in the error case, if the transaction
5572 * is dirty we'll need to shut down the filesystem.
5573 */
5574 if (logflags)
5575 xfs_trans_log_inode(tp, ip, logflags);
5576 if (cur) {
Brian Fostercf612de2018-07-11 22:26:29 -07005577 if (!error)
Dave Chinner92219c22020-03-10 17:52:53 -07005578 cur->bc_ino.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07005579 xfs_btree_del_cursor(cur, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 }
5581 return error;
5582}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005583
Darrick J. Wong44535932016-10-03 09:11:29 -07005584/* Unmap a range of a file. */
5585int
5586xfs_bunmapi(
5587 xfs_trans_t *tp,
5588 struct xfs_inode *ip,
5589 xfs_fileoff_t bno,
5590 xfs_filblks_t len,
5591 int flags,
5592 xfs_extnum_t nexts,
Darrick J. Wong44535932016-10-03 09:11:29 -07005593 int *done)
5594{
5595 int error;
5596
Brian Foster2af52842018-07-11 22:26:25 -07005597 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
Darrick J. Wong44535932016-10-03 09:11:29 -07005598 *done = (len == 0);
5599 return error;
5600}
5601
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005602/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005603 * Determine whether an extent shift can be accomplished by a merge with the
5604 * extent that precedes the target hole of the shift.
5605 */
5606STATIC bool
5607xfs_bmse_can_merge(
5608 struct xfs_bmbt_irec *left, /* preceding extent */
5609 struct xfs_bmbt_irec *got, /* current extent to shift */
5610 xfs_fileoff_t shift) /* shift fsb */
5611{
5612 xfs_fileoff_t startoff;
5613
5614 startoff = got->br_startoff - shift;
5615
5616 /*
5617 * The extent, once shifted, must be adjacent in-file and on-disk with
5618 * the preceding extent.
5619 */
5620 if ((left->br_startoff + left->br_blockcount != startoff) ||
5621 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5622 (left->br_state != got->br_state) ||
5623 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5624 return false;
5625
5626 return true;
5627}
5628
5629/*
5630 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5631 * hole in the file. If an extent shift would result in the extent being fully
5632 * adjacent to the extent that currently precedes the hole, we can merge with
5633 * the preceding extent rather than do the shift.
5634 *
5635 * This function assumes the caller has verified a shift-by-merge is possible
5636 * with the provided extents via xfs_bmse_can_merge().
5637 */
5638STATIC int
5639xfs_bmse_merge(
Brian Foster0f37d172018-08-01 07:20:34 -07005640 struct xfs_trans *tp,
Brian Fosterddb19e32014-09-23 15:38:09 +10005641 struct xfs_inode *ip,
5642 int whichfork,
5643 xfs_fileoff_t shift, /* shift fsb */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005644 struct xfs_iext_cursor *icur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005645 struct xfs_bmbt_irec *got, /* extent to shift */
5646 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005647 struct xfs_btree_cur *cur,
Brian Foster0f37d172018-08-01 07:20:34 -07005648 int *logflags) /* output */
Brian Fosterddb19e32014-09-23 15:38:09 +10005649{
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005650 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005651 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005652 xfs_filblks_t blockcount;
5653 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005654 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005655
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005656 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005657
5658 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5659 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005660 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005661
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005662 new = *left;
5663 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005664
5665 /*
5666 * Update the on-disk extent count, the btree if necessary and log the
5667 * inode.
5668 */
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005669 ifp->if_nextents--;
Brian Fosterddb19e32014-09-23 15:38:09 +10005670 *logflags |= XFS_ILOG_CORE;
5671 if (!cur) {
5672 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005673 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005674 }
5675
5676 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005677 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005678 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005679 return error;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005680 if (XFS_IS_CORRUPT(mp, i != 1))
5681 return -EFSCORRUPTED;
Brian Fosterddb19e32014-09-23 15:38:09 +10005682
5683 error = xfs_btree_delete(cur, &i);
5684 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005685 return error;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005686 if (XFS_IS_CORRUPT(mp, i != 1))
5687 return -EFSCORRUPTED;
Brian Fosterddb19e32014-09-23 15:38:09 +10005688
5689 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005690 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005691 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005692 return error;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005693 if (XFS_IS_CORRUPT(mp, i != 1))
5694 return -EFSCORRUPTED;
Brian Fosterddb19e32014-09-23 15:38:09 +10005695
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005696 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005697 if (error)
5698 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005699
Brian Fostere20e1742019-09-18 17:50:45 -07005700 /* change to extent format if required after extent removal */
5701 error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork);
5702 if (error)
5703 return error;
5704
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005705done:
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005706 xfs_iext_remove(ip, icur, 0);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07005707 xfs_iext_prev(ifp, icur);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005708 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5709 &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005710
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005711 /* update reverse mapping. rmap functions merge the rmaps for us */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07005712 xfs_rmap_unmap_extent(tp, ip, whichfork, got);
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005713 memcpy(&new, got, sizeof(new));
5714 new.br_startoff = left->br_startoff + left->br_blockcount;
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07005715 xfs_rmap_map_extent(tp, ip, whichfork, &new);
5716 return 0;
Brian Fosterddb19e32014-09-23 15:38:09 +10005717}
5718
Christoph Hellwigbf806282017-10-19 11:07:34 -07005719static int
5720xfs_bmap_shift_update_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07005721 struct xfs_trans *tp,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005722 struct xfs_inode *ip,
5723 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005724 struct xfs_iext_cursor *icur,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005725 struct xfs_bmbt_irec *got,
5726 struct xfs_btree_cur *cur,
5727 int *logflags,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005728 xfs_fileoff_t startoff)
Brian Fostera979bdf2014-09-23 15:39:04 +10005729{
Christoph Hellwigbf806282017-10-19 11:07:34 -07005730 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005731 struct xfs_bmbt_irec prev = *got;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005732 int error, i;
Brian Fostera979bdf2014-09-23 15:39:04 +10005733
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005734 *logflags |= XFS_ILOG_CORE;
5735
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005736 got->br_startoff = startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005737
5738 if (cur) {
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005739 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005740 if (error)
5741 return error;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005742 if (XFS_IS_CORRUPT(mp, i != 1))
5743 return -EFSCORRUPTED;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005744
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005745 error = xfs_bmbt_update(cur, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005746 if (error)
5747 return error;
5748 } else {
5749 *logflags |= XFS_ILOG_DEXT;
5750 }
5751
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005752 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5753 got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005754
Darrick J. Wong9c194642016-08-03 12:16:05 +10005755 /* update reverse mapping */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07005756 xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
5757 xfs_rmap_map_extent(tp, ip, whichfork, got);
5758 return 0;
Brian Fostera979bdf2014-09-23 15:39:04 +10005759}
5760
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005761int
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005762xfs_bmap_collapse_extents(
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005763 struct xfs_trans *tp,
5764 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005765 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005766 xfs_fileoff_t offset_shift_fsb,
Brian Foster333f9502018-07-11 22:26:27 -07005767 bool *done)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005768{
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005769 int whichfork = XFS_DATA_FORK;
5770 struct xfs_mount *mp = ip->i_mount;
5771 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5772 struct xfs_btree_cur *cur = NULL;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005773 struct xfs_bmbt_irec got, prev;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005774 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005775 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005776 int error = 0;
5777 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005778
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005779 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
Darrick J. Wonga71895c2019-11-11 12:53:22 -08005780 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Dave Chinner24513372014-06-25 14:58:08 +10005781 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005782 }
5783
5784 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005785 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005786
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005787 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005788
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005789 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005790 error = xfs_iread_extents(tp, ip, whichfork);
5791 if (error)
5792 return error;
5793 }
5794
Brian Fosterddb19e32014-09-23 15:38:09 +10005795 if (ifp->if_flags & XFS_IFBROOT) {
5796 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner92219c22020-03-10 17:52:53 -07005797 cur->bc_ino.flags = 0;
Brian Fosterddb19e32014-09-23 15:38:09 +10005798 }
5799
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005800 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005801 *done = true;
5802 goto del_cursor;
5803 }
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005804 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5805 error = -EFSCORRUPTED;
5806 goto del_cursor;
5807 }
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005808
Christoph Hellwigbf806282017-10-19 11:07:34 -07005809 new_startoff = got.br_startoff - offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005810 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005811 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5812 error = -EINVAL;
5813 goto del_cursor;
5814 }
5815
Christoph Hellwigbf806282017-10-19 11:07:34 -07005816 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
Brian Foster0f37d172018-08-01 07:20:34 -07005817 error = xfs_bmse_merge(tp, ip, whichfork,
5818 offset_shift_fsb, &icur, &got, &prev,
5819 cur, &logflags);
Christoph Hellwigbf806282017-10-19 11:07:34 -07005820 if (error)
5821 goto del_cursor;
5822 goto done;
5823 }
5824 } else {
5825 if (got.br_startoff < offset_shift_fsb) {
5826 error = -EINVAL;
5827 goto del_cursor;
5828 }
5829 }
5830
Brian Foster0f37d172018-08-01 07:20:34 -07005831 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5832 cur, &logflags, new_startoff);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005833 if (error)
5834 goto del_cursor;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005835
Christoph Hellwig42630362017-11-03 10:34:41 -07005836done:
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005837 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5838 *done = true;
5839 goto del_cursor;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005840 }
5841
Christoph Hellwigbf806282017-10-19 11:07:34 -07005842 *next_fsb = got.br_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005843del_cursor:
5844 if (cur)
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07005845 xfs_btree_del_cursor(cur, error);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005846 if (logflags)
5847 xfs_trans_log_inode(tp, ip, logflags);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005848 return error;
5849}
5850
Darrick J. Wongf62cb482018-06-21 23:26:57 -07005851/* Make sure we won't be right-shifting an extent past the maximum bound. */
5852int
5853xfs_bmap_can_insert_extents(
5854 struct xfs_inode *ip,
5855 xfs_fileoff_t off,
5856 xfs_fileoff_t shift)
5857{
5858 struct xfs_bmbt_irec got;
5859 int is_empty;
5860 int error = 0;
5861
5862 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5863
5864 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5865 return -EIO;
5866
5867 xfs_ilock(ip, XFS_ILOCK_EXCL);
5868 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
5869 if (!error && !is_empty && got.br_startoff >= off &&
5870 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
5871 error = -EINVAL;
5872 xfs_iunlock(ip, XFS_ILOCK_EXCL);
5873
5874 return error;
5875}
5876
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005877int
5878xfs_bmap_insert_extents(
5879 struct xfs_trans *tp,
5880 struct xfs_inode *ip,
5881 xfs_fileoff_t *next_fsb,
5882 xfs_fileoff_t offset_shift_fsb,
5883 bool *done,
Brian Foster333f9502018-07-11 22:26:27 -07005884 xfs_fileoff_t stop_fsb)
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005885{
5886 int whichfork = XFS_DATA_FORK;
5887 struct xfs_mount *mp = ip->i_mount;
5888 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5889 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005890 struct xfs_bmbt_irec got, next;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005891 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005892 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005893 int error = 0;
5894 int logflags = 0;
5895
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005896 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
Darrick J. Wonga71895c2019-11-11 12:53:22 -08005897 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005898 return -EFSCORRUPTED;
5899 }
5900
5901 if (XFS_FORCED_SHUTDOWN(mp))
5902 return -EIO;
5903
5904 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5905
5906 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5907 error = xfs_iread_extents(tp, ip, whichfork);
5908 if (error)
5909 return error;
5910 }
5911
5912 if (ifp->if_flags & XFS_IFBROOT) {
5913 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner92219c22020-03-10 17:52:53 -07005914 cur->bc_ino.flags = 0;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005915 }
5916
Namjae Jeona904b1c2015-03-25 15:08:56 +11005917 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005918 xfs_iext_last(ifp, &icur);
5919 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005920 stop_fsb > got.br_startoff) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005921 *done = true;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005922 goto del_cursor;
5923 }
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005924 } else {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005925 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005926 *done = true;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005927 goto del_cursor;
5928 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005929 }
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005930 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5931 error = -EFSCORRUPTED;
5932 goto del_cursor;
5933 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005934
Brian Fosterd0c22042019-12-11 13:18:38 -08005935 if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) {
Darrick J. Wongc2414ad2019-10-28 16:12:34 -07005936 error = -EFSCORRUPTED;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005937 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005938 }
5939
Christoph Hellwigbf806282017-10-19 11:07:34 -07005940 new_startoff = got.br_startoff + offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005941 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005942 if (new_startoff + got.br_blockcount > next.br_startoff) {
5943 error = -EINVAL;
5944 goto del_cursor;
5945 }
5946
5947 /*
5948 * Unlike a left shift (which involves a hole punch), a right
5949 * shift does not modify extent neighbors in any way. We should
5950 * never find mergeable extents in this scenario. Check anyways
5951 * and warn if we encounter two extents that could be one.
5952 */
5953 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5954 WARN_ON_ONCE(1);
5955 }
5956
Brian Foster0f37d172018-08-01 07:20:34 -07005957 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5958 cur, &logflags, new_startoff);
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005959 if (error)
5960 goto del_cursor;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005961
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005962 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005963 stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005964 *done = true;
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005965 goto del_cursor;
5966 }
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005967
5968 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005969del_cursor:
5970 if (cur)
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07005971 xfs_btree_del_cursor(cur, error);
Brian Fosterca446d82014-09-02 12:12:53 +10005972 if (logflags)
5973 xfs_trans_log_inode(tp, ip, logflags);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005974 return error;
5975}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005976
5977/*
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005978 * Splits an extent into two extents at split_fsb block such that it is the
5979 * first block of the current_ext. @ext is a target extent to be split.
5980 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5981 * hole or the first block of extents, just return 0.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005982 */
Brian Fosterb73df172020-02-26 09:43:15 -08005983int
5984xfs_bmap_split_extent(
Namjae Jeona904b1c2015-03-25 15:08:56 +11005985 struct xfs_trans *tp,
5986 struct xfs_inode *ip,
Brian Foster4b77a082018-07-11 22:26:27 -07005987 xfs_fileoff_t split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005988{
5989 int whichfork = XFS_DATA_FORK;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07005990 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005991 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005992 struct xfs_bmbt_irec got;
5993 struct xfs_bmbt_irec new; /* split extent */
5994 struct xfs_mount *mp = ip->i_mount;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005995 xfs_fsblock_t gotblkcnt; /* new block count for got */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005996 struct xfs_iext_cursor icur;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005997 int error = 0;
5998 int logflags = 0;
5999 int i = 0;
6000
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07006001 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
Darrick J. Wonga71895c2019-11-11 12:53:22 -08006002 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006003 return -EFSCORRUPTED;
6004 }
6005
6006 if (XFS_FORCED_SHUTDOWN(mp))
6007 return -EIO;
6008
Namjae Jeona904b1c2015-03-25 15:08:56 +11006009 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6010 /* Read in all the extents */
6011 error = xfs_iread_extents(tp, ip, whichfork);
6012 if (error)
6013 return error;
6014 }
6015
6016 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006017 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11006018 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006019 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006020 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006021 return 0;
6022
6023 gotblkcnt = split_fsb - got.br_startoff;
6024 new.br_startoff = split_fsb;
6025 new.br_startblock = got.br_startblock + gotblkcnt;
6026 new.br_blockcount = got.br_blockcount - gotblkcnt;
6027 new.br_state = got.br_state;
6028
6029 if (ifp->if_flags & XFS_IFBROOT) {
6030 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner92219c22020-03-10 17:52:53 -07006031 cur->bc_ino.flags = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07006032 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006033 if (error)
6034 goto del_cursor;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08006035 if (XFS_IS_CORRUPT(mp, i != 1)) {
6036 error = -EFSCORRUPTED;
6037 goto del_cursor;
6038 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006039 }
6040
Namjae Jeona904b1c2015-03-25 15:08:56 +11006041 got.br_blockcount = gotblkcnt;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006042 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
6043 &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006044
6045 logflags = XFS_ILOG_CORE;
6046 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07006047 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006048 if (error)
6049 goto del_cursor;
6050 } else
6051 logflags |= XFS_ILOG_DEXT;
6052
6053 /* Add new extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006054 xfs_iext_next(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07006055 xfs_iext_insert(ip, &icur, &new, 0);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07006056 ifp->if_nextents++;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006057
6058 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07006059 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006060 if (error)
6061 goto del_cursor;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08006062 if (XFS_IS_CORRUPT(mp, i != 0)) {
6063 error = -EFSCORRUPTED;
6064 goto del_cursor;
6065 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006066 error = xfs_btree_insert(cur, &i);
6067 if (error)
6068 goto del_cursor;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08006069 if (XFS_IS_CORRUPT(mp, i != 1)) {
6070 error = -EFSCORRUPTED;
6071 goto del_cursor;
6072 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006073 }
6074
6075 /*
6076 * Convert to a btree if necessary.
6077 */
6078 if (xfs_bmap_needs_btree(ip, whichfork)) {
6079 int tmp_logflags; /* partial log flag return val */
6080
6081 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07006082 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
6083 &tmp_logflags, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006084 logflags |= tmp_logflags;
6085 }
6086
6087del_cursor:
6088 if (cur) {
Dave Chinner92219c22020-03-10 17:52:53 -07006089 cur->bc_ino.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07006090 xfs_btree_del_cursor(cur, error);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006091 }
6092
6093 if (logflags)
6094 xfs_trans_log_inode(tp, ip, logflags);
6095 return error;
6096}
6097
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006098/* Deferred mapping is only for real extents in the data fork. */
6099static bool
6100xfs_bmap_is_update_needed(
6101 struct xfs_bmbt_irec *bmap)
6102{
6103 return bmap->br_startblock != HOLESTARTBLOCK &&
6104 bmap->br_startblock != DELAYSTARTBLOCK;
6105}
6106
6107/* Record a bmap intent. */
6108static int
6109__xfs_bmap_add(
Brian Foster0f37d172018-08-01 07:20:34 -07006110 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006111 enum xfs_bmap_intent_type type,
6112 struct xfs_inode *ip,
6113 int whichfork,
6114 struct xfs_bmbt_irec *bmap)
6115{
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006116 struct xfs_bmap_intent *bi;
6117
Brian Foster0f37d172018-08-01 07:20:34 -07006118 trace_xfs_bmap_defer(tp->t_mountp,
6119 XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock),
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006120 type,
Brian Foster0f37d172018-08-01 07:20:34 -07006121 XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock),
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006122 ip->i_ino, whichfork,
6123 bmap->br_startoff,
6124 bmap->br_blockcount,
6125 bmap->br_state);
6126
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07006127 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_NOFS);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006128 INIT_LIST_HEAD(&bi->bi_list);
6129 bi->bi_type = type;
6130 bi->bi_owner = ip;
6131 bi->bi_whichfork = whichfork;
6132 bi->bi_bmap = *bmap;
6133
Brian Foster0f37d172018-08-01 07:20:34 -07006134 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006135 return 0;
6136}
6137
6138/* Map an extent into a file. */
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006139void
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006140xfs_bmap_map_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07006141 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006142 struct xfs_inode *ip,
6143 struct xfs_bmbt_irec *PREV)
6144{
6145 if (!xfs_bmap_is_update_needed(PREV))
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006146 return;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006147
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006148 __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, XFS_DATA_FORK, PREV);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006149}
6150
6151/* Unmap an extent out of a file. */
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006152void
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006153xfs_bmap_unmap_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07006154 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006155 struct xfs_inode *ip,
6156 struct xfs_bmbt_irec *PREV)
6157{
6158 if (!xfs_bmap_is_update_needed(PREV))
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006159 return;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006160
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006161 __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, XFS_DATA_FORK, PREV);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006162}
6163
6164/*
6165 * Process one of the deferred bmap operations. We pass back the
6166 * btree cursor to maintain our lock on the bmapbt between calls.
6167 */
6168int
6169xfs_bmap_finish_one(
6170 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006171 struct xfs_inode *ip,
6172 enum xfs_bmap_intent_type type,
6173 int whichfork,
6174 xfs_fileoff_t startoff,
6175 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006176 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006177 xfs_exntst_t state)
6178{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006179 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006180
Brian Foster37283792018-07-11 22:26:23 -07006181 ASSERT(tp->t_firstblock == NULLFSBLOCK);
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006182
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006183 trace_xfs_bmap_deferred(tp->t_mountp,
6184 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6185 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006186 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006187
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006188 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006189 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006190
6191 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006192 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006193 return -EIO;
6194
6195 switch (type) {
6196 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006197 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Brian Fosterff3edf22018-07-11 22:26:14 -07006198 startblock, 0);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006199 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006200 break;
6201 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006202 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
Brian Foster2af52842018-07-11 22:26:25 -07006203 XFS_BMAPI_REMAP, 1);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006204 break;
6205 default:
6206 ASSERT(0);
6207 error = -EFSCORRUPTED;
6208 }
6209
6210 return error;
6211}
Darrick J. Wong30b09842018-03-23 10:06:52 -07006212
6213/* Check that an inode's extent does not have invalid flags or bad ranges. */
6214xfs_failaddr_t
6215xfs_bmap_validate_extent(
6216 struct xfs_inode *ip,
6217 int whichfork,
6218 struct xfs_bmbt_irec *irec)
6219{
6220 struct xfs_mount *mp = ip->i_mount;
6221 xfs_fsblock_t endfsb;
6222 bool isrt;
6223
6224 isrt = XFS_IS_REALTIME_INODE(ip);
6225 endfsb = irec->br_startblock + irec->br_blockcount - 1;
6226 if (isrt) {
6227 if (!xfs_verify_rtbno(mp, irec->br_startblock))
6228 return __this_address;
6229 if (!xfs_verify_rtbno(mp, endfsb))
6230 return __this_address;
6231 } else {
6232 if (!xfs_verify_fsbno(mp, irec->br_startblock))
6233 return __this_address;
6234 if (!xfs_verify_fsbno(mp, endfsb))
6235 return __this_address;
6236 if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) !=
6237 XFS_FSB_TO_AGNO(mp, endfsb))
6238 return __this_address;
6239 }
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11006240 if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6241 return __this_address;
Darrick J. Wong30b09842018-03-23 10:06:52 -07006242 return NULL;
6243}