blob: fc8f6d65576c16b4e736ee491ddbd0302d2723d3 [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 /*
64 * The maximum number of extents in a file, hence the maximum
65 * number of leaf entries, is controlled by the type of di_nextents
66 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
67 * (a signed 16-bit number, xfs_aextnum_t).
68 *
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{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700123 return whichfork != XFS_COW_FORK &&
124 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000125 XFS_IFORK_NEXTENTS(ip, whichfork) >
126 XFS_IFORK_MAXEXT(ip, whichfork);
127}
128
129/*
130 * Check if the inode should be converted to extent format.
131 */
132static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
133{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700134 return whichfork != XFS_COW_FORK &&
135 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000136 XFS_IFORK_NEXTENTS(ip, whichfork) <=
137 XFS_IFORK_MAXEXT(ip, whichfork);
138}
139
140/*
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700141 * Update the record referred to by cur to the value given by irec
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100142 * This either works (return 0) or gets an EFSCORRUPTED error.
143 */
144STATIC int
145xfs_bmbt_update(
146 struct xfs_btree_cur *cur,
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700147 struct xfs_bmbt_irec *irec)
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100148{
149 union xfs_btree_rec rec;
150
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700151 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100152 return xfs_btree_update(cur, &rec);
153}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/*
Dave Chinner9e5987a72013-02-25 12:31:26 +1100156 * Compute the worst-case number of indirect blocks that will be used
157 * for ip's delayed extent of length "len".
158 */
159STATIC xfs_filblks_t
160xfs_bmap_worst_indlen(
161 xfs_inode_t *ip, /* incore inode pointer */
162 xfs_filblks_t len) /* delayed extent length */
163{
164 int level; /* btree level number */
165 int maxrecs; /* maximum record count at this level */
166 xfs_mount_t *mp; /* mount structure */
167 xfs_filblks_t rval; /* return value */
168
169 mp = ip->i_mount;
170 maxrecs = mp->m_bmap_dmxr[0];
171 for (level = 0, rval = 0;
172 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
173 level++) {
174 len += maxrecs - 1;
175 do_div(len, maxrecs);
176 rval += len;
Darrick J. Wong5e5c9432017-09-18 09:41:17 -0700177 if (len == 1)
178 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
Dave Chinner9e5987a72013-02-25 12:31:26 +1100179 level - 1;
180 if (level == 0)
181 maxrecs = mp->m_bmap_dmxr[1];
182 }
183 return rval;
184}
185
186/*
187 * Calculate the default attribute fork offset for newly created inodes.
188 */
189uint
190xfs_default_attroffset(
191 struct xfs_inode *ip)
192{
193 struct xfs_mount *mp = ip->i_mount;
194 uint offset;
195
196 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100197 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a72013-02-25 12:31:26 +1100198 XFS_BMDR_SPACE_CALC(MINABTPTRS);
199 } else {
200 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
201 }
202
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100203 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
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 &&
218 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
Dave Chinner9e5987a72013-02-25 12:31:26 +1100219 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
220 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{
320 struct xfs_btree_block *block; /* current btree block */
321 xfs_fsblock_t bno; /* block # of "block" */
322 xfs_buf_t *bp; /* buffer for "block" */
323 int error; /* error return value */
324 xfs_extnum_t i=0, j; /* index into the extents list */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -0700325 struct xfs_ifork *ifp; /* fork structure */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100326 int level; /* btree level, for checking */
327 xfs_mount_t *mp; /* file system mount structure */
328 __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
334 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
335 return;
336 }
337
Dave Chinnere3543812016-01-08 11:28:49 +1100338 /* skip large extent count inodes */
339 if (ip->i_d.di_nextents > 10000)
340 return;
341
Dave Chinner9e5987a72013-02-25 12:31:26 +1100342 bno = NULLFSBLOCK;
343 mp = ip->i_mount;
344 ifp = XFS_IFORK_PTR(ip, whichfork);
345 block = ifp->if_broot;
346 /*
347 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
348 */
349 level = be16_to_cpu(block->bb_level);
350 ASSERT(level > 0);
351 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
352 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
353 bno = be64_to_cpu(*pp);
354
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000355 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100356 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
357 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
358
359 /*
360 * Go down the tree until leaf level is reached, following the first
361 * pointer (leftmost) at each level.
362 */
363 while (level-- > 0) {
364 /* See if buf is in cur first */
365 bp_release = 0;
366 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
367 if (!bp) {
368 bp_release = 1;
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700369 error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100370 XFS_BMAP_BTREE_REF,
371 &xfs_bmbt_buf_ops);
372 if (error)
373 goto error_norelse;
374 }
375 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100376 if (level == 0)
377 break;
378
379 /*
380 * Check this block for basic sanity (increasing keys and
381 * no duplicate blocks).
382 */
383
384 xfs_check_block(block, mp, 0, 0);
385 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
386 bno = be64_to_cpu(*pp);
Darrick J. Wongf9e03702019-11-11 12:52:18 -0800387 if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbno(mp, bno))) {
388 error = -EFSCORRUPTED;
389 goto error0;
390 }
Dave Chinner9e5987a72013-02-25 12:31:26 +1100391 if (bp_release) {
392 bp_release = 0;
393 xfs_trans_brelse(NULL, bp);
394 }
395 }
396
397 /*
398 * Here with bp and block set to the leftmost leaf node in the tree.
399 */
400 i = 0;
401
402 /*
403 * Loop over all leaf nodes checking that all extents are in the right order.
404 */
405 for (;;) {
406 xfs_fsblock_t nextbno;
407 xfs_extnum_t num_recs;
408
409
410 num_recs = xfs_btree_get_numrecs(block);
411
412 /*
413 * Read-ahead the next leaf block, if any.
414 */
415
416 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
417
418 /*
419 * Check all the extents to make sure they are OK.
420 * If we had a previous block, the last entry should
421 * conform with the first entry in this one.
422 */
423
424 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
425 if (i) {
426 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
427 xfs_bmbt_disk_get_blockcount(&last) <=
428 xfs_bmbt_disk_get_startoff(ep));
429 }
430 for (j = 1; j < num_recs; j++) {
431 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
432 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
433 xfs_bmbt_disk_get_blockcount(ep) <=
434 xfs_bmbt_disk_get_startoff(nextp));
435 ep = nextp;
436 }
437
438 last = *ep;
439 i += num_recs;
440 if (bp_release) {
441 bp_release = 0;
442 xfs_trans_brelse(NULL, bp);
443 }
444 bno = nextbno;
445 /*
446 * If we've reached the end, stop.
447 */
448 if (bno == NULLFSBLOCK)
449 break;
450
451 bp_release = 0;
452 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
453 if (!bp) {
454 bp_release = 1;
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700455 error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100456 XFS_BMAP_BTREE_REF,
457 &xfs_bmbt_buf_ops);
458 if (error)
459 goto error_norelse;
460 }
461 block = XFS_BUF_TO_BLOCK(bp);
462 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100463
Dave Chinner9e5987a72013-02-25 12:31:26 +1100464 return;
465
466error0:
467 xfs_warn(mp, "%s: at error0", __func__);
468 if (bp_release)
469 xfs_trans_brelse(NULL, bp);
470error_norelse:
471 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
472 __func__, i);
Darrick J. Wongcec57252018-05-04 15:31:21 -0700473 xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
474 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100475 return;
476}
477
478/*
Dave Chinner9e5987a72013-02-25 12:31:26 +1100479 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000480 * given the caller's original parameters. Specifically check the
481 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a72013-02-25 12:31:26 +1100482 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
483 */
484STATIC void
485xfs_bmap_validate_ret(
486 xfs_fileoff_t bno,
487 xfs_filblks_t len,
488 int flags,
489 xfs_bmbt_irec_t *mval,
490 int nmap,
491 int ret_nmap)
492{
493 int i; /* index to map values */
494
495 ASSERT(ret_nmap <= nmap);
496
497 for (i = 0; i < ret_nmap; i++) {
498 ASSERT(mval[i].br_blockcount > 0);
499 if (!(flags & XFS_BMAPI_ENTIRE)) {
500 ASSERT(mval[i].br_startoff >= bno);
501 ASSERT(mval[i].br_blockcount <= len);
502 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
503 bno + len);
504 } else {
505 ASSERT(mval[i].br_startoff < bno + len);
506 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
507 bno);
508 }
509 ASSERT(i == 0 ||
510 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
511 mval[i].br_startoff);
512 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
513 mval[i].br_startblock != HOLESTARTBLOCK);
514 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
515 mval[i].br_state == XFS_EXT_UNWRITTEN);
516 }
517}
518
519#else
520#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
Darrick J. Wong7bf7a192017-08-31 15:11:06 -0700521#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100522#endif /* DEBUG */
523
524/*
525 * bmap free list manipulation functions
526 */
527
528/*
529 * Add the extent to the list of extents to be free at transaction end.
530 * The list is maintained sorted (by block number).
531 */
532void
Brian Fosterfcb762f2018-05-09 08:45:04 -0700533__xfs_bmap_add_free(
Brian Foster0f37d172018-08-01 07:20:34 -0700534 struct xfs_trans *tp,
Darrick J. Wong340785c2016-08-03 11:33:42 +1000535 xfs_fsblock_t bno,
536 xfs_filblks_t len,
Darrick J. Wong66e32372018-12-12 08:46:23 -0800537 const struct xfs_owner_info *oinfo,
Brian Fosterfcb762f2018-05-09 08:45:04 -0700538 bool skip_discard)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100539{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000540 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100541#ifdef DEBUG
Brian Foster0f37d172018-08-01 07:20:34 -0700542 struct xfs_mount *mp = tp->t_mountp;
543 xfs_agnumber_t agno;
544 xfs_agblock_t agbno;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100545
546 ASSERT(bno != NULLFSBLOCK);
547 ASSERT(len > 0);
548 ASSERT(len <= MAXEXTLEN);
549 ASSERT(!isnullstartblock(bno));
550 agno = XFS_FSB_TO_AGNO(mp, bno);
551 agbno = XFS_FSB_TO_AGBNO(mp, bno);
552 ASSERT(agno < mp->m_sb.sb_agcount);
553 ASSERT(agbno < mp->m_sb.sb_agblocks);
554 ASSERT(len < mp->m_sb.sb_agblocks);
555 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
556#endif
557 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000558
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700559 new = kmem_zone_alloc(xfs_bmap_free_item_zone, 0);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000560 new->xefi_startblock = bno;
561 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000562 if (oinfo)
563 new->xefi_oinfo = *oinfo;
564 else
Darrick J. Wong7280fed2018-12-12 08:46:23 -0800565 new->xefi_oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
Brian Fosterfcb762f2018-05-09 08:45:04 -0700566 new->xefi_skip_discard = skip_discard;
Brian Foster0f37d172018-08-01 07:20:34 -0700567 trace_xfs_bmap_free_defer(tp->t_mountp,
568 XFS_FSB_TO_AGNO(tp->t_mountp, bno), 0,
569 XFS_FSB_TO_AGBNO(tp->t_mountp, bno), len);
570 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100571}
572
573/*
574 * Inode fork format manipulation functions
575 */
576
577/*
Christoph Hellwigb101e332019-02-15 08:02:47 -0800578 * Convert the inode format to extent format if it currently is in btree format,
579 * but the extent list is small enough that it fits into the extent format.
580 *
581 * Since the extents are already in-core, all we have to do is give up the space
582 * for the btree root and pitch the leaf block.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100583 */
584STATIC int /* error */
585xfs_bmap_btree_to_extents(
Christoph Hellwigb101e332019-02-15 08:02:47 -0800586 struct xfs_trans *tp, /* transaction pointer */
587 struct xfs_inode *ip, /* incore inode pointer */
588 struct xfs_btree_cur *cur, /* btree cursor */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100589 int *logflagsp, /* inode logging flags */
590 int whichfork) /* data or attr fork */
591{
Christoph Hellwigb101e332019-02-15 08:02:47 -0800592 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
593 struct xfs_mount *mp = ip->i_mount;
594 struct xfs_btree_block *rblock = ifp->if_broot;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100595 struct xfs_btree_block *cblock;/* child btree block */
596 xfs_fsblock_t cbno; /* child block number */
597 xfs_buf_t *cbp; /* child block's buffer */
598 int error; /* error return value */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100599 __be64 *pp; /* ptr to block address */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000600 struct xfs_owner_info oinfo;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100601
Christoph Hellwigb101e332019-02-15 08:02:47 -0800602 /* check if we actually need the extent format first: */
603 if (!xfs_bmap_wants_extents(ip, whichfork))
604 return 0;
605
606 ASSERT(cur);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700607 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100608 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
609 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100610 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
611 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
612 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
Christoph Hellwigb101e332019-02-15 08:02:47 -0800613
Dave Chinner9e5987a72013-02-25 12:31:26 +1100614 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
615 cbno = be64_to_cpu(*pp);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100616#ifdef DEBUG
Darrick J. Wongf9e03702019-11-11 12:52:18 -0800617 if (XFS_IS_CORRUPT(cur->bc_mp, !xfs_btree_check_lptr(cur, cbno, 1)))
618 return -EFSCORRUPTED;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100619#endif
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700620 error = xfs_btree_read_bufl(mp, tp, cbno, &cbp, XFS_BMAP_BTREE_REF,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100621 &xfs_bmbt_buf_ops);
622 if (error)
623 return error;
624 cblock = XFS_BUF_TO_BLOCK(cbp);
625 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
626 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000627 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
Brian Foster0f37d172018-08-01 07:20:34 -0700628 xfs_bmap_add_free(cur->bc_tp, cbno, 1, &oinfo);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100629 ip->i_d.di_nblocks--;
630 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
631 xfs_trans_binval(tp, cbp);
632 if (cur->bc_bufs[0] == cbp)
633 cur->bc_bufs[0] = NULL;
634 xfs_iroot_realloc(ip, -1, whichfork);
635 ASSERT(ifp->if_broot == NULL);
636 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
637 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Christoph Hellwigb101e332019-02-15 08:02:47 -0800638 *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100639 return 0;
640}
641
642/*
643 * Convert an extents-format file into a btree-format file.
644 * The new file will have a root block (in the inode) and a single child block.
645 */
646STATIC int /* error */
647xfs_bmap_extents_to_btree(
Brian Foster81ba8f32018-07-11 22:26:16 -0700648 struct xfs_trans *tp, /* transaction pointer */
649 struct xfs_inode *ip, /* incore inode pointer */
Brian Foster81ba8f32018-07-11 22:26:16 -0700650 struct xfs_btree_cur **curp, /* cursor returned to caller */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100651 int wasdel, /* converting a delayed alloc */
652 int *logflagsp, /* inode logging flags */
653 int whichfork) /* data or attr fork */
654{
655 struct xfs_btree_block *ablock; /* allocated (child) bt block */
Brian Foster81ba8f32018-07-11 22:26:16 -0700656 struct xfs_buf *abp; /* buffer for ablock */
657 struct xfs_alloc_arg args; /* allocation arguments */
658 struct xfs_bmbt_rec *arp; /* child record pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100659 struct xfs_btree_block *block; /* btree root block */
Brian Foster81ba8f32018-07-11 22:26:16 -0700660 struct xfs_btree_cur *cur; /* bmap btree cursor */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100661 int error; /* error return value */
Brian Foster81ba8f32018-07-11 22:26:16 -0700662 struct xfs_ifork *ifp; /* inode fork pointer */
663 struct xfs_bmbt_key *kp; /* root block key pointer */
664 struct xfs_mount *mp; /* mount structure */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100665 xfs_bmbt_ptr_t *pp; /* root block address pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700666 struct xfs_iext_cursor icur;
Christoph Hellwig906abed2017-11-03 10:34:43 -0700667 struct xfs_bmbt_irec rec;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700668 xfs_extnum_t cnt = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100669
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500670 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700671 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100672 ifp = XFS_IFORK_PTR(ip, whichfork);
673 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
674
675 /*
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000676 * Make space in the inode incore. This needs to be undone if we fail
677 * to expand the root.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100678 */
679 xfs_iroot_realloc(ip, 1, whichfork);
680 ifp->if_flags |= XFS_IFBROOT;
681
682 /*
683 * Fill in the root.
684 */
685 block = ifp->if_broot;
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800686 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
687 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
Eric Sandeenf88ae462017-01-27 23:16:37 -0800688 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100689 /*
690 * Need a cursor. Can't allocate until bb_level is filled in.
691 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100692 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner92219c22020-03-10 17:52:53 -0700693 cur->bc_ino.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100694 /*
695 * Convert to a btree with two levels, one record in root.
696 */
697 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
698 memset(&args, 0, sizeof(args));
699 args.tp = tp;
700 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000701 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Brian Foster280253d2018-07-11 22:26:29 -0700702 if (tp->t_firstblock == NULLFSBLOCK) {
Dave Chinner9e5987a72013-02-25 12:31:26 +1100703 args.type = XFS_ALLOCTYPE_START_BNO;
704 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Brian Foster1214f1c2018-08-01 07:20:31 -0700705 } else if (tp->t_flags & XFS_TRANS_LOWMODE) {
Dave Chinner9e5987a72013-02-25 12:31:26 +1100706 args.type = XFS_ALLOCTYPE_START_BNO;
Brian Foster280253d2018-07-11 22:26:29 -0700707 args.fsbno = tp->t_firstblock;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100708 } else {
709 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Brian Foster280253d2018-07-11 22:26:29 -0700710 args.fsbno = tp->t_firstblock;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100711 }
712 args.minlen = args.maxlen = args.prod = 1;
713 args.wasdel = wasdel;
714 *logflagsp = 0;
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000715 error = xfs_alloc_vextent(&args);
716 if (error)
717 goto out_root_realloc;
Darrick J. Wong90e20562016-10-03 09:11:45 -0700718
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800719 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
Shan Hai01239d772018-08-10 17:55:55 -0700720 error = -ENOSPC;
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000721 goto out_root_realloc;
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800722 }
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000723
Dave Chinner9e5987a72013-02-25 12:31:26 +1100724 /*
725 * Allocation can't fail, the space was reserved.
726 */
Brian Foster280253d2018-07-11 22:26:29 -0700727 ASSERT(tp->t_firstblock == NULLFSBLOCK ||
728 args.agno >= XFS_FSB_TO_AGNO(mp, tp->t_firstblock));
Brian Fostercf612de2018-07-11 22:26:29 -0700729 tp->t_firstblock = args.fsbno;
Dave Chinner92219c22020-03-10 17:52:53 -0700730 cur->bc_ino.allocated++;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100731 ip->i_d.di_nblocks++;
732 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
Darrick J. Wongee647f82020-01-23 17:01:19 -0800733 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
734 XFS_FSB_TO_DADDR(mp, args.fsbno),
735 mp->m_bsize, 0, &abp);
736 if (error)
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000737 goto out_unreserve_dquot;
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000738
Dave Chinner9e5987a72013-02-25 12:31:26 +1100739 /*
740 * Fill in the child block.
741 */
742 abp->b_ops = &xfs_bmbt_buf_ops;
743 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800744 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
745 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500746 XFS_BTREE_LONG_PTRS);
747
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700748 for_each_xfs_iext(ifp, &icur, &rec) {
Christoph Hellwig906abed2017-11-03 10:34:43 -0700749 if (isnullstartblock(rec.br_startblock))
750 continue;
751 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
752 xfs_bmbt_disk_set_all(arp, &rec);
753 cnt++;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100754 }
755 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
756 xfs_btree_set_numrecs(ablock, cnt);
757
758 /*
759 * Fill in the root key and pointer.
760 */
761 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
762 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
763 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
764 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
765 be16_to_cpu(block->bb_level)));
766 *pp = cpu_to_be64(args.fsbno);
767
768 /*
769 * Do all this logging at the end so that
770 * the root is at the right level.
771 */
772 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
773 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
774 ASSERT(*curp == NULL);
775 *curp = cur;
776 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
777 return 0;
Shan Hai01239d772018-08-10 17:55:55 -0700778
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000779out_unreserve_dquot:
Shan Hai01239d772018-08-10 17:55:55 -0700780 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000781out_root_realloc:
Shan Hai01239d772018-08-10 17:55:55 -0700782 xfs_iroot_realloc(ip, -1, whichfork);
783 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000784 ASSERT(ifp->if_broot == NULL);
Shan Hai01239d772018-08-10 17:55:55 -0700785 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
786
787 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100788}
789
790/*
791 * Convert a local file to an extents file.
792 * This code is out of bounds for data forks of regular files,
793 * since the file data needs to get logged so things will stay consistent.
794 * (The bmap-level manipulations are ok, though).
795 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000796void
797xfs_bmap_local_to_extents_empty(
Brian Fosteraeea4b72019-10-07 12:54:16 -0700798 struct xfs_trans *tp,
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000799 struct xfs_inode *ip,
800 int whichfork)
801{
802 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
803
Darrick J. Wong60b49842016-10-03 09:11:34 -0700804 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000805 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
806 ASSERT(ifp->if_bytes == 0);
807 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
808
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000809 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000810 ifp->if_flags &= ~XFS_IFINLINE;
811 ifp->if_flags |= XFS_IFEXTENTS;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700812 ifp->if_u1.if_root = NULL;
813 ifp->if_height = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000814 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Brian Fosteraeea4b72019-10-07 12:54:16 -0700815 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000816}
817
818
Dave Chinner9e5987a72013-02-25 12:31:26 +1100819STATIC int /* error */
820xfs_bmap_local_to_extents(
821 xfs_trans_t *tp, /* transaction pointer */
822 xfs_inode_t *ip, /* incore inode pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100823 xfs_extlen_t total, /* total blocks needed by transaction */
824 int *logflagsp, /* inode logging flags */
825 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500826 void (*init_fn)(struct xfs_trans *tp,
827 struct xfs_buf *bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100828 struct xfs_inode *ip,
829 struct xfs_ifork *ifp))
830{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000831 int error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100832 int flags; /* logging flags returned */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -0700833 struct xfs_ifork *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000834 xfs_alloc_arg_t args; /* allocation arguments */
835 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700836 struct xfs_bmbt_irec rec;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700837 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100838
839 /*
840 * We don't want to deal with the case of keeping inode data inline yet.
841 * So sending the data fork of a regular inode is invalid.
842 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100843 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100844 ifp = XFS_IFORK_PTR(ip, whichfork);
845 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000846
847 if (!ifp->if_bytes) {
Brian Fosteraeea4b72019-10-07 12:54:16 -0700848 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000849 flags = XFS_ILOG_CORE;
850 goto done;
851 }
852
Dave Chinner9e5987a72013-02-25 12:31:26 +1100853 flags = 0;
854 error = 0;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700855 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000856 memset(&args, 0, sizeof(args));
857 args.tp = tp;
858 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000859 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000860 /*
861 * Allocate a block. We know we need only one, since the
862 * file currently fits in an inode.
863 */
Brian Foster280253d2018-07-11 22:26:29 -0700864 if (tp->t_firstblock == NULLFSBLOCK) {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000865 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
866 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100867 } else {
Brian Foster280253d2018-07-11 22:26:29 -0700868 args.fsbno = tp->t_firstblock;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000869 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100870 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000871 args.total = total;
872 args.minlen = args.maxlen = args.prod = 1;
873 error = xfs_alloc_vextent(&args);
874 if (error)
875 goto done;
876
877 /* Can't fail, the space was reserved. */
878 ASSERT(args.fsbno != NULLFSBLOCK);
879 ASSERT(args.len == 1);
Brian Foster280253d2018-07-11 22:26:29 -0700880 tp->t_firstblock = args.fsbno;
Darrick J. Wongee647f82020-01-23 17:01:19 -0800881 error = xfs_trans_get_buf(tp, args.mp->m_ddev_targp,
882 XFS_FSB_TO_DADDR(args.mp, args.fsbno),
883 args.mp->m_bsize, 0, &bp);
884 if (error)
885 goto done;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000886
Dave Chinnerfe22d552015-01-22 09:30:06 +1100887 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100888 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100889 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100890 * The callout is responsible for logging because the remote format
891 * might differ from the local format and thus we don't know how much to
892 * log here. Note that init_fn must also set the buffer log item type
893 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100894 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000895 init_fn(tp, bp, ip, ifp);
896
Brian Fosterb7cdc662015-10-12 15:40:24 +1100897 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000898 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
Brian Fosteraeea4b72019-10-07 12:54:16 -0700899 xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100900 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000901
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700902 ifp->if_u1.if_root = NULL;
903 ifp->if_height = 0;
904
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700905 rec.br_startoff = 0;
906 rec.br_startblock = args.fsbno;
907 rec.br_blockcount = 1;
908 rec.br_state = XFS_EXT_NORM;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700909 xfs_iext_first(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -0700910 xfs_iext_insert(ip, &icur, &rec, 0);
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700911
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000912 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
913 ip->i_d.di_nblocks = 1;
914 xfs_trans_mod_dquot_byino(tp, ip,
915 XFS_TRANS_DQ_BCOUNT, 1L);
916 flags |= xfs_ilog_fext(whichfork);
917
Dave Chinner9e5987a72013-02-25 12:31:26 +1100918done:
919 *logflagsp = flags;
920 return error;
921}
922
923/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 * Called from xfs_bmap_add_attrfork to handle btree format files.
925 */
926STATIC int /* error */
927xfs_bmap_add_attrfork_btree(
928 xfs_trans_t *tp, /* transaction pointer */
929 xfs_inode_t *ip, /* incore inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 int *flags) /* inode logging flags */
931{
932 xfs_btree_cur_t *cur; /* btree cursor */
933 int error; /* error return value */
934 xfs_mount_t *mp; /* file system mount struct */
935 int stat; /* newroot status */
936
937 mp = ip->i_mount;
938 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
939 *flags |= XFS_ILOG_DBROOT;
940 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100941 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700942 error = xfs_bmbt_lookup_first(cur, &stat);
943 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000945 /* must be at least one entry */
Darrick J. Wongf9e03702019-11-11 12:52:18 -0800946 if (XFS_IS_CORRUPT(mp, stat != 1)) {
947 error = -EFSCORRUPTED;
948 goto error0;
949 }
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100950 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 goto error0;
952 if (stat == 0) {
953 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000954 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
Dave Chinner92219c22020-03-10 17:52:53 -0700956 cur->bc_ino.allocated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
958 }
959 return 0;
960error0:
961 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
962 return error;
963}
964
965/*
966 * Called from xfs_bmap_add_attrfork to handle extents format files.
967 */
968STATIC int /* error */
969xfs_bmap_add_attrfork_extents(
Brian Foster81ba8f32018-07-11 22:26:16 -0700970 struct xfs_trans *tp, /* transaction pointer */
971 struct xfs_inode *ip, /* incore inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 int *flags) /* inode logging flags */
973{
974 xfs_btree_cur_t *cur; /* bmap btree cursor */
975 int error; /* error return value */
976
977 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
978 return 0;
979 cur = NULL;
Brian Foster280253d2018-07-11 22:26:29 -0700980 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
981 XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (cur) {
Dave Chinner92219c22020-03-10 17:52:53 -0700983 cur->bc_ino.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -0700984 xfs_btree_del_cursor(cur, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986 return error;
987}
988
989/*
Dave Chinner1e823792013-02-11 15:58:13 +1100990 * Called from xfs_bmap_add_attrfork to handle local format files. Each
991 * different data fork content type needs a different callout to do the
992 * conversion. Some are basic and only require special block initialisation
993 * callouts for the data formating, others (directories) are so specialised they
994 * handle everything themselves.
995 *
996 * XXX (dgc): investigate whether directory conversion can use the generic
997 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500998 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 */
1000STATIC int /* error */
1001xfs_bmap_add_attrfork_local(
Brian Foster825d75cd2018-07-11 22:26:21 -07001002 struct xfs_trans *tp, /* transaction pointer */
1003 struct xfs_inode *ip, /* incore inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 int *flags) /* inode logging flags */
1005{
Brian Foster825d75cd2018-07-11 22:26:21 -07001006 struct xfs_da_args dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1009 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001010
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001011 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001013 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 dargs.dp = ip;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001015 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 dargs.whichfork = XFS_DATA_FORK;
1017 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001018 return xfs_dir2_sf_to_block(&dargs);
1019 }
1020
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001021 if (S_ISLNK(VFS_I(ip)->i_mode))
Brian Foster280253d2018-07-11 22:26:29 -07001022 return xfs_bmap_local_to_extents(tp, ip, 1, flags,
1023 XFS_DATA_FORK,
Dave Chinner1e823792013-02-11 15:58:13 +11001024 xfs_symlink_local_to_remote);
1025
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001026 /* should only be called for types that support local format data */
1027 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001028 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
Allison Henderson2f3cd802018-10-18 17:21:16 +11001031/* Set an inode attr fork off based on the format */
1032int
1033xfs_bmap_set_attrforkoff(
1034 struct xfs_inode *ip,
1035 int size,
1036 int *version)
1037{
1038 switch (ip->i_d.di_format) {
1039 case XFS_DINODE_FMT_DEV:
1040 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1041 break;
1042 case XFS_DINODE_FMT_LOCAL:
1043 case XFS_DINODE_FMT_EXTENTS:
1044 case XFS_DINODE_FMT_BTREE:
1045 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1046 if (!ip->i_d.di_forkoff)
1047 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1048 else if ((ip->i_mount->m_flags & XFS_MOUNT_ATTR2) && version)
1049 *version = 2;
1050 break;
1051 default:
1052 ASSERT(0);
1053 return -EINVAL;
1054 }
1055
1056 return 0;
1057}
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001060 * Convert inode from non-attributed to attributed.
1061 * Must not be in a transaction, ip must not be locked.
1062 */
1063int /* error code */
1064xfs_bmap_add_attrfork(
1065 xfs_inode_t *ip, /* incore inode pointer */
1066 int size, /* space new attribute needs */
1067 int rsvd) /* xact may use reserved blks */
1068{
Dave Chinner9e5987a72013-02-25 12:31:26 +11001069 xfs_mount_t *mp; /* mount structure */
1070 xfs_trans_t *tp; /* transaction pointer */
1071 int blks; /* space reservation */
1072 int version = 1; /* superblock attr version */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001073 int logflags; /* logging flags */
1074 int error; /* error return value */
1075
1076 ASSERT(XFS_IFORK_Q(ip) == 0);
1077
1078 mp = ip->i_mount;
1079 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001080
Dave Chinner9e5987a72013-02-25 12:31:26 +11001081 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001082
1083 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1084 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1085 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001086 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001087
Dave Chinner9e5987a72013-02-25 12:31:26 +11001088 xfs_ilock(ip, XFS_ILOCK_EXCL);
1089 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1090 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1091 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001092 if (error)
1093 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001094 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001095 goto trans_cancel;
Darrick J. Wonga71895c2019-11-11 12:53:22 -08001096 if (XFS_IS_CORRUPT(mp, ip->i_d.di_anextents != 0)) {
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001097 error = -EFSCORRUPTED;
1098 goto trans_cancel;
1099 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001100 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1101 /*
1102 * For inodes coming from pre-6.2 filesystems.
1103 */
1104 ASSERT(ip->i_d.di_aformat == 0);
1105 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1106 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001107
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001108 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001109 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Allison Henderson2f3cd802018-10-18 17:21:16 +11001110 error = xfs_bmap_set_attrforkoff(ip, size, &version);
1111 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001112 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001113 ASSERT(ip->i_afp == NULL);
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07001114 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001115 ip->i_afp->if_flags = XFS_IFEXTENTS;
1116 logflags = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001117 switch (ip->i_d.di_format) {
1118 case XFS_DINODE_FMT_LOCAL:
Brian Foster825d75cd2018-07-11 22:26:21 -07001119 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001120 break;
1121 case XFS_DINODE_FMT_EXTENTS:
Brian Foster825d75cd2018-07-11 22:26:21 -07001122 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001123 break;
1124 case XFS_DINODE_FMT_BTREE:
Brian Foster825d75cd2018-07-11 22:26:21 -07001125 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001126 break;
1127 default:
1128 error = 0;
1129 break;
1130 }
1131 if (logflags)
1132 xfs_trans_log_inode(tp, ip, logflags);
1133 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07001134 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001135 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1136 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001137 bool log_sb = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001138
1139 spin_lock(&mp->m_sb_lock);
1140 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1141 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001142 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001143 }
1144 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1145 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001146 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001147 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001148 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001149 if (log_sb)
1150 xfs_log_sb(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001151 }
1152
Christoph Hellwig70393312015-06-04 13:48:08 +10001153 error = xfs_trans_commit(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001154 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001155 return error;
1156
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001157trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001158 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001159 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001160 return error;
1161}
1162
1163/*
1164 * Internal and external extent tree search functions.
1165 */
1166
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001167struct xfs_iread_state {
1168 struct xfs_iext_cursor icur;
1169 xfs_extnum_t loaded;
1170};
1171
1172/* Stuff every bmbt record from this block into the incore extent map. */
1173static int
1174xfs_iread_bmbt_block(
1175 struct xfs_btree_cur *cur,
1176 int level,
1177 void *priv)
1178{
1179 struct xfs_iread_state *ir = priv;
1180 struct xfs_mount *mp = cur->bc_mp;
Dave Chinner92219c22020-03-10 17:52:53 -07001181 struct xfs_inode *ip = cur->bc_ino.ip;
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001182 struct xfs_btree_block *block;
1183 struct xfs_buf *bp;
1184 struct xfs_bmbt_rec *frp;
1185 xfs_extnum_t num_recs;
1186 xfs_extnum_t j;
Dave Chinner92219c22020-03-10 17:52:53 -07001187 int whichfork = cur->bc_ino.whichfork;
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001188
1189 block = xfs_btree_get_block(cur, level, &bp);
1190
1191 /* Abort if we find more records than nextents. */
1192 num_recs = xfs_btree_get_numrecs(block);
1193 if (unlikely(ir->loaded + num_recs >
1194 XFS_IFORK_NEXTENTS(ip, whichfork))) {
1195 xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).",
1196 (unsigned long long)ip->i_ino);
1197 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block,
1198 sizeof(*block), __this_address);
1199 return -EFSCORRUPTED;
1200 }
1201
1202 /* Copy records into the incore cache. */
1203 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1204 for (j = 0; j < num_recs; j++, frp++, ir->loaded++) {
1205 struct xfs_bmbt_irec new;
1206 xfs_failaddr_t fa;
1207
1208 xfs_bmbt_disk_get_all(frp, &new);
1209 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1210 if (fa) {
1211 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1212 "xfs_iread_extents(2)", frp,
1213 sizeof(*frp), fa);
1214 return -EFSCORRUPTED;
1215 }
1216 xfs_iext_insert(ip, &ir->icur, &new,
1217 xfs_bmap_fork_to_state(whichfork));
1218 trace_xfs_read_extent(ip, &ir->icur,
1219 xfs_bmap_fork_to_state(whichfork), _THIS_IP_);
1220 xfs_iext_next(XFS_IFORK_PTR(ip, whichfork), &ir->icur);
1221 }
1222
1223 return 0;
1224}
1225
Dave Chinner9e5987a72013-02-25 12:31:26 +11001226/*
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001227 * Read in extents from a btree-format inode.
Dave Chinner9e5987a72013-02-25 12:31:26 +11001228 */
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001229int
1230xfs_iread_extents(
1231 struct xfs_trans *tp,
1232 struct xfs_inode *ip,
1233 int whichfork)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001234{
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001235 struct xfs_iread_state ir;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001236 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001237 struct xfs_mount *mp = ip->i_mount;
1238 struct xfs_btree_cur *cur;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001239 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001240
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001241 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1242
Darrick J. Wonga71895c2019-11-11 12:53:22 -08001243 if (XFS_IS_CORRUPT(mp,
1244 XFS_IFORK_FORMAT(ip, whichfork) !=
1245 XFS_DINODE_FMT_BTREE)) {
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001246 error = -EFSCORRUPTED;
1247 goto out;
1248 }
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001249
1250 ir.loaded = 0;
1251 xfs_iext_first(ifp, &ir.icur);
1252 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1253 error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block,
1254 XFS_BTREE_VISIT_RECORDS, &ir);
1255 xfs_btree_del_cursor(cur, error);
1256 if (error)
1257 goto out;
1258
Darrick J. Wonga71895c2019-11-11 12:53:22 -08001259 if (XFS_IS_CORRUPT(mp,
1260 ir.loaded != XFS_IFORK_NEXTENTS(ip, whichfork))) {
Darrick J. Wonge992ae82019-10-28 16:12:35 -07001261 error = -EFSCORRUPTED;
1262 goto out;
1263 }
1264 ASSERT(ir.loaded == xfs_iext_count(ifp));
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001265
1266 ifp->if_flags |= XFS_IFEXTENTS;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001267 return 0;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001268out:
1269 xfs_iext_destroy(ifp);
1270 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001271}
1272
Dave Chinner9e5987a72013-02-25 12:31:26 +11001273/*
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001274 * Returns the relative block number of the first unused block(s) in the given
1275 * fork with at least "len" logically contiguous blocks free. This is the
1276 * lowest-address hole if the fork has holes, else the first block past the end
1277 * of fork. Return 0 if the fork is currently local (in-inode).
Dave Chinner9e5987a72013-02-25 12:31:26 +11001278 */
1279int /* error */
1280xfs_bmap_first_unused(
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001281 struct xfs_trans *tp, /* transaction pointer */
1282 struct xfs_inode *ip, /* incore inode */
1283 xfs_extlen_t len, /* size of hole to find */
1284 xfs_fileoff_t *first_unused, /* unused block */
1285 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001286{
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001287 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1288 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001289 struct xfs_iext_cursor icur;
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001290 xfs_fileoff_t lastaddr = 0;
1291 xfs_fileoff_t lowest, max;
1292 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001293
Darrick J. Wong2fe4f922019-11-07 15:05:21 -08001294 ASSERT(xfs_ifork_has_extents(ip, whichfork) ||
Dave Chinner9e5987a72013-02-25 12:31:26 +11001295 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001296
Dave Chinner9e5987a72013-02-25 12:31:26 +11001297 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1298 *first_unused = 0;
1299 return 0;
1300 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001301
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001302 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1303 error = xfs_iread_extents(tp, ip, whichfork);
1304 if (error)
1305 return error;
1306 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001307
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001308 lowest = max = *first_unused;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001309 for_each_xfs_iext(ifp, &icur, &got) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001310 /*
1311 * See if the hole before this extent will work.
1312 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001313 if (got.br_startoff >= lowest + len &&
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001314 got.br_startoff - max >= len)
1315 break;
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001316 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001317 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1318 }
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001319
Dave Chinner9e5987a72013-02-25 12:31:26 +11001320 *first_unused = max;
1321 return 0;
1322}
1323
1324/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001325 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a72013-02-25 12:31:26 +11001326 * last_block (input value) in the file.
1327 * This is not based on i_size, it is based on the extent records.
1328 * Returns 0 for local files, as they do not have extent records.
1329 */
1330int /* error */
1331xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001332 struct xfs_trans *tp, /* transaction pointer */
1333 struct xfs_inode *ip, /* incore inode */
1334 xfs_fileoff_t *last_block, /* last block */
1335 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001336{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001337 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1338 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001339 struct xfs_iext_cursor icur;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001340 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001341
Christoph Hellwig86685f72016-11-24 11:39:38 +11001342 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1343 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001344 *last_block = 0;
1345 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001346 case XFS_DINODE_FMT_BTREE:
1347 case XFS_DINODE_FMT_EXTENTS:
1348 break;
1349 default:
Darrick J. Wonga5155b82019-11-02 09:40:53 -07001350 ASSERT(0);
Darrick J. Wongc2414ad2019-10-28 16:12:34 -07001351 return -EFSCORRUPTED;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001352 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001353
1354 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1355 error = xfs_iread_extents(tp, ip, whichfork);
1356 if (error)
1357 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001358 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001359
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001360 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
Christoph Hellwigdc560152017-10-23 16:32:39 -07001361 *last_block = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001362 return 0;
1363}
1364
Dave Chinner68988112013-08-12 20:49:42 +10001365int
Dave Chinner9e5987a72013-02-25 12:31:26 +11001366xfs_bmap_last_extent(
1367 struct xfs_trans *tp,
1368 struct xfs_inode *ip,
1369 int whichfork,
1370 struct xfs_bmbt_irec *rec,
1371 int *is_empty)
1372{
1373 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001374 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001375 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001376
1377 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1378 error = xfs_iread_extents(tp, ip, whichfork);
1379 if (error)
1380 return error;
1381 }
1382
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001383 xfs_iext_last(ifp, &icur);
1384 if (!xfs_iext_get_extent(ifp, &icur, rec))
Dave Chinner9e5987a72013-02-25 12:31:26 +11001385 *is_empty = 1;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001386 else
1387 *is_empty = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001388 return 0;
1389}
1390
1391/*
1392 * Check the last inode extent to determine whether this allocation will result
1393 * in blocks being allocated at the end of the file. When we allocate new data
1394 * blocks at the end of the file which do not start at the previous data block,
1395 * we will try to align the new blocks at stripe unit boundaries.
1396 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001397 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a72013-02-25 12:31:26 +11001398 * at, or past the EOF.
1399 */
1400STATIC int
1401xfs_bmap_isaeof(
1402 struct xfs_bmalloca *bma,
1403 int whichfork)
1404{
1405 struct xfs_bmbt_irec rec;
1406 int is_empty;
1407 int error;
1408
Thomas Meyer749f24f2017-10-09 11:38:54 -07001409 bma->aeof = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001410 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1411 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001412 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001413 return error;
1414
Dave Chinner6e708bc2013-11-22 10:41:16 +11001415 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001416 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001417 return 0;
1418 }
1419
Dave Chinner9e5987a72013-02-25 12:31:26 +11001420 /*
1421 * Check if we are allocation or past the last extent, or at least into
1422 * the last delayed allocated extent.
1423 */
1424 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1425 (bma->offset >= rec.br_startoff &&
1426 isnullstartblock(rec.br_startblock));
1427 return 0;
1428}
1429
1430/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001431 * Returns the file-relative block number of the first block past eof in
1432 * the file. This is not based on i_size, it is based on the extent records.
1433 * Returns 0 for local files, as they do not have extent records.
1434 */
1435int
1436xfs_bmap_last_offset(
Dave Chinner9e5987a72013-02-25 12:31:26 +11001437 struct xfs_inode *ip,
1438 xfs_fileoff_t *last_block,
1439 int whichfork)
1440{
1441 struct xfs_bmbt_irec rec;
1442 int is_empty;
1443 int error;
1444
1445 *last_block = 0;
1446
1447 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1448 return 0;
1449
Darrick J. Wonga71895c2019-11-11 12:53:22 -08001450 if (XFS_IS_CORRUPT(ip->i_mount, !xfs_ifork_has_extents(ip, whichfork)))
Darrick J. Wongc2414ad2019-10-28 16:12:34 -07001451 return -EFSCORRUPTED;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001452
1453 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1454 if (error || is_empty)
1455 return error;
1456
1457 *last_block = rec.br_startoff + rec.br_blockcount;
1458 return 0;
1459}
1460
1461/*
1462 * Returns whether the selected fork of the inode has exactly one
1463 * block or not. For the data fork we check this matches di_size,
1464 * implying the file's range is 0..bsize-1.
1465 */
1466int /* 1=>1 block, 0=>otherwise */
1467xfs_bmap_one_block(
1468 xfs_inode_t *ip, /* incore inode */
1469 int whichfork) /* data or attr fork */
1470{
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07001471 struct xfs_ifork *ifp; /* inode fork pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001472 int rval; /* return value */
1473 xfs_bmbt_irec_t s; /* internal version of extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001474 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001475
1476#ifndef DEBUG
1477 if (whichfork == XFS_DATA_FORK)
1478 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1479#endif /* !DEBUG */
1480 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1481 return 0;
1482 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1483 return 0;
1484 ifp = XFS_IFORK_PTR(ip, whichfork);
1485 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001486 xfs_iext_first(ifp, &icur);
1487 xfs_iext_get_extent(ifp, &icur, &s);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001488 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1489 if (rval && whichfork == XFS_DATA_FORK)
1490 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1491 return rval;
1492}
1493
1494/*
1495 * Extent tree manipulation functions used during allocation.
1496 */
1497
1498/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001499 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 */
1501STATIC int /* error */
1502xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001503 struct xfs_bmalloca *bma,
1504 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001506 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 int i; /* temp state */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07001509 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 xfs_fileoff_t new_endoff; /* end offset of new entry */
1511 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1512 /* left is 0, right is 1, prev is 2 */
1513 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07001514 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001515 xfs_filblks_t da_new; /* new count del alloc blocks used */
1516 xfs_filblks_t da_old; /* old count del alloc blocks used */
1517 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001519 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001520 xfs_extnum_t *nextents;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001521 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001523 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001524 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001525 ASSERT(whichfork != XFS_ATTR_FORK);
1526 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1527 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001528
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001529 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001530 ASSERT(!bma->cur ||
Dave Chinner92219c22020-03-10 17:52:53 -07001531 (bma->cur->bc_ino.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001532
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001533 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535#define LEFT r[0]
1536#define RIGHT r[1]
1537#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 /*
1540 * Set up a bunch of variables to make the tests simpler.
1541 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001542 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001544 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 ASSERT(PREV.br_startoff <= new->br_startoff);
1546 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001547
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001548 da_old = startblockval(PREV.br_startblock);
1549 da_new = 0;
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 /*
1552 * Set flags determining what part of the previous delayed allocation
1553 * extent is being replaced by a real allocation.
1554 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001555 if (PREV.br_startoff == new->br_startoff)
1556 state |= BMAP_LEFT_FILLING;
1557 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1558 state |= BMAP_RIGHT_FILLING;
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 /*
1561 * Check and set flags if this segment has a left neighbor.
1562 * Don't set contiguous if the combined extent would be too large.
1563 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001564 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001565 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001566 if (isnullstartblock(LEFT.br_startblock))
1567 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001569
1570 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1571 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1572 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1573 LEFT.br_state == new->br_state &&
1574 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1575 state |= BMAP_LEFT_CONTIG;
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 /*
1578 * Check and set flags if this segment has a right neighbor.
1579 * Don't set contiguous if the combined extent would be too large.
1580 * Also check for all-three-contiguous being too large.
1581 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001582 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001583 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001584 if (isnullstartblock(RIGHT.br_startblock))
1585 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001587
1588 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1589 new_endoff == RIGHT.br_startoff &&
1590 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1591 new->br_state == RIGHT.br_state &&
1592 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1593 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1594 BMAP_RIGHT_FILLING)) !=
1595 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1596 BMAP_RIGHT_FILLING) ||
1597 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1598 <= MAXEXTLEN))
1599 state |= BMAP_RIGHT_CONTIG;
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 error = 0;
1602 /*
1603 * Switch out based on the FILLING and CONTIG state bits.
1604 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001605 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1606 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1607 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1608 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 /*
1610 * Filling in all of a previously delayed allocation extent.
1611 * The left and right neighbors are both contiguous with new.
1612 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001613 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001614
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001615 xfs_iext_remove(bma->ip, &bma->icur, state);
1616 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001617 xfs_iext_prev(ifp, &bma->icur);
1618 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001619 (*nextents)--;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001620
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001621 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1623 else {
1624 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001625 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001626 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001628 if (XFS_IS_CORRUPT(mp, i != 1)) {
1629 error = -EFSCORRUPTED;
1630 goto done;
1631 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001632 error = xfs_btree_delete(bma->cur, &i);
1633 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001635 if (XFS_IS_CORRUPT(mp, i != 1)) {
1636 error = -EFSCORRUPTED;
1637 goto done;
1638 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001639 error = xfs_btree_decrement(bma->cur, 0, &i);
1640 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001642 if (XFS_IS_CORRUPT(mp, i != 1)) {
1643 error = -EFSCORRUPTED;
1644 goto done;
1645 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001646 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001647 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 goto done;
1649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 break;
1651
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001652 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 /*
1654 * Filling in all of a previously delayed allocation extent.
1655 * The left neighbor is contiguous, the right is not.
1656 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001657 old = LEFT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001658 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001659
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001660 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001661 xfs_iext_prev(ifp, &bma->icur);
1662 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001663
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001664 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 rval = XFS_ILOG_DEXT;
1666 else {
1667 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001668 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001669 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001671 if (XFS_IS_CORRUPT(mp, i != 1)) {
1672 error = -EFSCORRUPTED;
1673 goto done;
1674 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001675 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001676 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 goto done;
1678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 break;
1680
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001681 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 /*
1683 * Filling in all of a previously delayed allocation extent.
Dave Chinner9230a0b2018-11-19 22:50:08 -08001684 * The right neighbor is contiguous, the left is not. Take care
1685 * with delay -> unwritten extent allocation here because the
1686 * delalloc record we are overwriting is always written.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001688 PREV.br_startblock = new->br_startblock;
1689 PREV.br_blockcount += RIGHT.br_blockcount;
Dave Chinner9230a0b2018-11-19 22:50:08 -08001690 PREV.br_state = new->br_state;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001691
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001692 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001693 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001694 xfs_iext_prev(ifp, &bma->icur);
1695 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001696
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001697 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 rval = XFS_ILOG_DEXT;
1699 else {
1700 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001701 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001702 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001704 if (XFS_IS_CORRUPT(mp, i != 1)) {
1705 error = -EFSCORRUPTED;
1706 goto done;
1707 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001708 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001709 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 goto done;
1711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 break;
1713
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001714 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 /*
1716 * Filling in all of a previously delayed allocation extent.
1717 * Neither the left nor right neighbors are contiguous with
1718 * the new one.
1719 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001720 PREV.br_startblock = new->br_startblock;
1721 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001722 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001723
Darrick J. Wong60b49842016-10-03 09:11:34 -07001724 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001725 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1727 else {
1728 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001729 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001730 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001732 if (XFS_IS_CORRUPT(mp, i != 0)) {
1733 error = -EFSCORRUPTED;
1734 goto done;
1735 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001736 error = xfs_btree_insert(bma->cur, &i);
1737 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001739 if (XFS_IS_CORRUPT(mp, i != 1)) {
1740 error = -EFSCORRUPTED;
1741 goto done;
1742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 break;
1745
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001746 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 /*
1748 * Filling in the first part of a previous delayed allocation.
1749 * The left neighbor is contiguous.
1750 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001751 old = LEFT;
1752 temp = PREV.br_blockcount - new->br_blockcount;
1753 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1754 startblockval(PREV.br_startblock));
1755
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001756 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001757
Christoph Hellwigbf999712017-11-03 10:34:38 -07001758 PREV.br_blockcount = temp;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001759 PREV.br_startoff += new->br_blockcount;
1760 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwig0d045542017-11-03 10:34:39 -07001761
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001762 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1763 xfs_iext_prev(ifp, &bma->icur);
1764 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001765
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001766 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 rval = XFS_ILOG_DEXT;
1768 else {
1769 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001770 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001771 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001773 if (XFS_IS_CORRUPT(mp, i != 1)) {
1774 error = -EFSCORRUPTED;
1775 goto done;
1776 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001777 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001778 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 goto done;
1780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 break;
1782
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001783 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 /*
1785 * Filling in the first part of a previous delayed allocation.
1786 * The left neighbor is not contiguous.
1787 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001788 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001789 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001790 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1792 else {
1793 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001794 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001795 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001797 if (XFS_IS_CORRUPT(mp, i != 0)) {
1798 error = -EFSCORRUPTED;
1799 goto done;
1800 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001801 error = xfs_btree_insert(bma->cur, &i);
1802 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001804 if (XFS_IS_CORRUPT(mp, i != 1)) {
1805 error = -EFSCORRUPTED;
1806 goto done;
1807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001809
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001810 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001811 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001812 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 rval |= tmp_rval;
1814 if (error)
1815 goto done;
1816 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001817
1818 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001819 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001820 startblockval(PREV.br_startblock) -
Dave Chinner92219c22020-03-10 17:52:53 -07001821 (bma->cur ? bma->cur->bc_ino.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001822
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001823 PREV.br_startoff = new_endoff;
1824 PREV.br_blockcount = temp;
1825 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001826 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001827 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001828 xfs_iext_prev(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 break;
1830
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001831 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 /*
1833 * Filling in the last part of a previous delayed allocation.
1834 * The right neighbor is contiguous with the new allocation.
1835 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001836 old = RIGHT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001837 RIGHT.br_startoff = new->br_startoff;
1838 RIGHT.br_startblock = new->br_startblock;
1839 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001840
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001841 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 rval = XFS_ILOG_DEXT;
1843 else {
1844 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001845 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001846 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001848 if (XFS_IS_CORRUPT(mp, i != 1)) {
1849 error = -EFSCORRUPTED;
1850 goto done;
1851 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001852 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001853 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 goto done;
1855 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001856
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001857 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001858 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001859 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001860
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001861 PREV.br_blockcount = temp;
1862 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001863
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001864 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1865 xfs_iext_next(ifp, &bma->icur);
1866 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 break;
1868
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001869 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 /*
1871 * Filling in the last part of a previous delayed allocation.
1872 * The right neighbor is not contiguous.
1873 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001874 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001875 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001876 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1878 else {
1879 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001880 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001881 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001883 if (XFS_IS_CORRUPT(mp, i != 0)) {
1884 error = -EFSCORRUPTED;
1885 goto done;
1886 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001887 error = xfs_btree_insert(bma->cur, &i);
1888 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001890 if (XFS_IS_CORRUPT(mp, i != 1)) {
1891 error = -EFSCORRUPTED;
1892 goto done;
1893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001895
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001896 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001897 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001898 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 rval |= tmp_rval;
1900 if (error)
1901 goto done;
1902 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001903
1904 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001905 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001906 startblockval(PREV.br_startblock) -
Dave Chinner92219c22020-03-10 17:52:53 -07001907 (bma->cur ? bma->cur->bc_ino.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001908
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001909 PREV.br_startblock = nullstartblock(da_new);
1910 PREV.br_blockcount = temp;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001911 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001912 xfs_iext_next(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 break;
1914
1915 case 0:
1916 /*
1917 * Filling in the middle part of a previous delayed allocation.
1918 * Contiguity is impossible here.
1919 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001920 *
1921 * We start with a delayed allocation:
1922 *
1923 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1924 * PREV @ idx
1925 *
1926 * and we are allocating:
1927 * +rrrrrrrrrrrrrrrrr+
1928 * new
1929 *
1930 * and we set it up for insertion as:
1931 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1932 * new
1933 * PREV @ idx LEFT RIGHT
1934 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001936 old = PREV;
1937
1938 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001939 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001940
1941 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001942 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001943 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001944 RIGHT.br_blockcount =
1945 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1946 RIGHT.br_startblock =
1947 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1948 RIGHT.br_blockcount));
1949
1950 /* truncate PREV */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001951 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1952 PREV.br_startblock =
1953 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1954 PREV.br_blockcount));
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001955 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001956
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001957 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001958 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1959 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001960 (*nextents)++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001961
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001962 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1964 else {
1965 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001966 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001967 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001969 if (XFS_IS_CORRUPT(mp, i != 0)) {
1970 error = -EFSCORRUPTED;
1971 goto done;
1972 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001973 error = xfs_btree_insert(bma->cur, &i);
1974 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001976 if (XFS_IS_CORRUPT(mp, i != 1)) {
1977 error = -EFSCORRUPTED;
1978 goto done;
1979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001981
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001982 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001983 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001984 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 rval |= tmp_rval;
1986 if (error)
1987 goto done;
1988 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001989
1990 da_new = startblockval(PREV.br_startblock) +
1991 startblockval(RIGHT.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 break;
1993
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001994 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1995 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1996 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1997 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1998 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1999 case BMAP_LEFT_CONTIG:
2000 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 /*
2002 * These cases are all impossible.
2003 */
2004 ASSERT(0);
2005 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002006
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002007 /* add reverse mapping unless caller opted out */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07002008 if (!(bma->flags & XFS_BMAPI_NORMAP))
2009 xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002010
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002011 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002012 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002013 int tmp_logflags; /* partial log flag return val */
2014
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002015 ASSERT(bma->cur == NULL);
2016 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07002017 &bma->cur, da_old > 0, &tmp_logflags,
2018 whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002019 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002020 if (error)
2021 goto done;
2022 }
2023
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07002024 if (da_new != da_old)
2025 xfs_mod_delalloc(mp, (int64_t)da_new - da_old);
2026
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002027 if (bma->cur) {
Dave Chinner92219c22020-03-10 17:52:53 -07002028 da_new += bma->cur->bc_ino.allocated;
2029 bma->cur->bc_ino.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002030 }
2031
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002032 /* adjust for changes in reserved delayed indirect blocks */
2033 if (da_new != da_old) {
2034 ASSERT(state == 0 || da_new < da_old);
2035 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2036 false);
2037 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002038
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002039 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002041 if (whichfork != XFS_COW_FORK)
2042 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return error;
2044#undef LEFT
2045#undef RIGHT
2046#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
2049/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002050 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 */
Christoph Hellwig26b91c72019-02-18 09:38:48 -08002052int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002054 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002056 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002057 struct xfs_iext_cursor *icur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002059 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002060 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 int i; /* temp state */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07002065 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2068 /* left is 0, right is 1, prev is 2 */
2069 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002070 int state = xfs_bmap_fork_to_state(whichfork);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002071 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002072 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002074 *logflagsp = 0;
2075
2076 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002077 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002078
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002079 ASSERT(!isnullstartblock(new->br_startblock));
2080
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002081 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083#define LEFT r[0]
2084#define RIGHT r[1]
2085#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 /*
2088 * Set up a bunch of variables to make the tests simpler.
2089 */
2090 error = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002091 xfs_iext_get_extent(ifp, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002092 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 new_endoff = new->br_startoff + new->br_blockcount;
2094 ASSERT(PREV.br_startoff <= new->br_startoff);
2095 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 /*
2098 * Set flags determining what part of the previous oldext allocation
2099 * extent is being replaced by a newext allocation.
2100 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002101 if (PREV.br_startoff == new->br_startoff)
2102 state |= BMAP_LEFT_FILLING;
2103 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2104 state |= BMAP_RIGHT_FILLING;
2105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 /*
2107 * Check and set flags if this segment has a left neighbor.
2108 * Don't set contiguous if the combined extent would be too large.
2109 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002110 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002111 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002112 if (isnullstartblock(LEFT.br_startblock))
2113 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002115
2116 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2117 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2118 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002119 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002120 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2121 state |= BMAP_LEFT_CONTIG;
2122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 /*
2124 * Check and set flags if this segment has a right neighbor.
2125 * Don't set contiguous if the combined extent would be too large.
2126 * Also check for all-three-contiguous being too large.
2127 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002128 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002129 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002130 if (isnullstartblock(RIGHT.br_startblock))
2131 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002133
2134 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2135 new_endoff == RIGHT.br_startoff &&
2136 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002137 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002138 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2139 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2140 BMAP_RIGHT_FILLING)) !=
2141 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2142 BMAP_RIGHT_FILLING) ||
2143 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2144 <= MAXEXTLEN))
2145 state |= BMAP_RIGHT_CONTIG;
2146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 /*
2148 * Switch out based on the FILLING and CONTIG state bits.
2149 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002150 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2151 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2152 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2153 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 /*
2155 * Setting all of a previous oldext extent to newext.
2156 * The left and right neighbors are both contiguous with new.
2157 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002158 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002159
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002160 xfs_iext_remove(ip, icur, state);
2161 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002162 xfs_iext_prev(ifp, icur);
2163 xfs_iext_update_extent(ip, state, icur, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002164 XFS_IFORK_NEXT_SET(ip, whichfork,
2165 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if (cur == NULL)
2167 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2168 else {
2169 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002170 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2171 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002173 if (XFS_IS_CORRUPT(mp, i != 1)) {
2174 error = -EFSCORRUPTED;
2175 goto done;
2176 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002177 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002179 if (XFS_IS_CORRUPT(mp, i != 1)) {
2180 error = -EFSCORRUPTED;
2181 goto done;
2182 }
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002183 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002185 if (XFS_IS_CORRUPT(mp, i != 1)) {
2186 error = -EFSCORRUPTED;
2187 goto done;
2188 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002189 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002191 if (XFS_IS_CORRUPT(mp, i != 1)) {
2192 error = -EFSCORRUPTED;
2193 goto done;
2194 }
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002195 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002197 if (XFS_IS_CORRUPT(mp, i != 1)) {
2198 error = -EFSCORRUPTED;
2199 goto done;
2200 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002201 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002202 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 goto done;
2204 }
2205 break;
2206
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002207 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 /*
2209 * Setting all of a previous oldext extent to newext.
2210 * The left neighbor is contiguous, the right is not.
2211 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002212 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002213
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002214 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002215 xfs_iext_prev(ifp, icur);
2216 xfs_iext_update_extent(ip, state, icur, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002217 XFS_IFORK_NEXT_SET(ip, whichfork,
2218 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 if (cur == NULL)
2220 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2221 else {
2222 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002223 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2224 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002226 if (XFS_IS_CORRUPT(mp, i != 1)) {
2227 error = -EFSCORRUPTED;
2228 goto done;
2229 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002230 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002232 if (XFS_IS_CORRUPT(mp, i != 1)) {
2233 error = -EFSCORRUPTED;
2234 goto done;
2235 }
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002236 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002238 if (XFS_IS_CORRUPT(mp, i != 1)) {
2239 error = -EFSCORRUPTED;
2240 goto done;
2241 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002242 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002243 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 goto done;
2245 }
2246 break;
2247
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002248 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 /*
2250 * Setting all of a previous oldext extent to newext.
2251 * The right neighbor is contiguous, the left is not.
2252 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002253 PREV.br_blockcount += RIGHT.br_blockcount;
2254 PREV.br_state = new->br_state;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002255
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002256 xfs_iext_next(ifp, icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002257 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002258 xfs_iext_prev(ifp, icur);
2259 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002260
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002261 XFS_IFORK_NEXT_SET(ip, whichfork,
2262 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 if (cur == NULL)
2264 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2265 else {
2266 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002267 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2268 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002270 if (XFS_IS_CORRUPT(mp, i != 1)) {
2271 error = -EFSCORRUPTED;
2272 goto done;
2273 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002274 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002276 if (XFS_IS_CORRUPT(mp, i != 1)) {
2277 error = -EFSCORRUPTED;
2278 goto done;
2279 }
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002280 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002282 if (XFS_IS_CORRUPT(mp, i != 1)) {
2283 error = -EFSCORRUPTED;
2284 goto done;
2285 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002286 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002287 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 goto done;
2289 }
2290 break;
2291
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002292 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 /*
2294 * Setting all of a previous oldext extent to newext.
2295 * Neither the left nor right neighbors are contiguous with
2296 * the new one.
2297 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002298 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002299 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 if (cur == NULL)
2302 rval = XFS_ILOG_DEXT;
2303 else {
2304 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002305 error = xfs_bmbt_lookup_eq(cur, new, &i);
2306 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002308 if (XFS_IS_CORRUPT(mp, i != 1)) {
2309 error = -EFSCORRUPTED;
2310 goto done;
2311 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002312 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002313 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 goto done;
2315 }
2316 break;
2317
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002318 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 /*
2320 * Setting the first part of a previous oldext extent to newext.
2321 * The left neighbor is contiguous.
2322 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002323 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002324
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002325 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002326 PREV.br_startoff += new->br_blockcount;
2327 PREV.br_startblock += new->br_blockcount;
2328 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002329
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002330 xfs_iext_update_extent(ip, state, icur, &PREV);
2331 xfs_iext_prev(ifp, icur);
2332 xfs_iext_update_extent(ip, state, icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 if (cur == NULL)
2335 rval = XFS_ILOG_DEXT;
2336 else {
2337 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002338 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002339 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002341 if (XFS_IS_CORRUPT(mp, i != 1)) {
2342 error = -EFSCORRUPTED;
2343 goto done;
2344 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002345 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002346 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002348 error = xfs_btree_decrement(cur, 0, &i);
2349 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002351 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002352 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 goto done;
2354 }
2355 break;
2356
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002357 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 /*
2359 * Setting the first part of a previous oldext extent to newext.
2360 * The left neighbor is not contiguous.
2361 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002362 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002363 PREV.br_startoff += new->br_blockcount;
2364 PREV.br_startblock += new->br_blockcount;
2365 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002366
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002367 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002368 xfs_iext_insert(ip, icur, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002369 XFS_IFORK_NEXT_SET(ip, whichfork,
2370 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 if (cur == NULL)
2372 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2373 else {
2374 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002375 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002376 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002378 if (XFS_IS_CORRUPT(mp, i != 1)) {
2379 error = -EFSCORRUPTED;
2380 goto done;
2381 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002382 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002383 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 goto done;
2385 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002386 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002388 if (XFS_IS_CORRUPT(mp, i != 1)) {
2389 error = -EFSCORRUPTED;
2390 goto done;
2391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393 break;
2394
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002395 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 /*
2397 * Setting the last part of a previous oldext extent to newext.
2398 * The right neighbor is contiguous with the new allocation.
2399 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002400 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002401 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002402
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002403 RIGHT.br_startoff = new->br_startoff;
2404 RIGHT.br_startblock = new->br_startblock;
2405 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002406
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002407 xfs_iext_update_extent(ip, state, icur, &PREV);
2408 xfs_iext_next(ifp, icur);
2409 xfs_iext_update_extent(ip, state, icur, &RIGHT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 if (cur == NULL)
2412 rval = XFS_ILOG_DEXT;
2413 else {
2414 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002415 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002416 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002418 if (XFS_IS_CORRUPT(mp, i != 1)) {
2419 error = -EFSCORRUPTED;
2420 goto done;
2421 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002422 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002423 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002425 error = xfs_btree_increment(cur, 0, &i);
2426 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002428 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002429 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 goto done;
2431 }
2432 break;
2433
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002434 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 /*
2436 * Setting the last part of a previous oldext extent to newext.
2437 * The right neighbor is not contiguous.
2438 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002439 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002440 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002441
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002442 xfs_iext_update_extent(ip, state, icur, &PREV);
2443 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002444 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002445
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002446 XFS_IFORK_NEXT_SET(ip, whichfork,
2447 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (cur == NULL)
2449 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2450 else {
2451 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002452 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002453 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002455 if (XFS_IS_CORRUPT(mp, i != 1)) {
2456 error = -EFSCORRUPTED;
2457 goto done;
2458 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002459 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002460 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002462 error = xfs_bmbt_lookup_eq(cur, new, &i);
2463 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002465 if (XFS_IS_CORRUPT(mp, i != 0)) {
2466 error = -EFSCORRUPTED;
2467 goto done;
2468 }
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002469 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002471 if (XFS_IS_CORRUPT(mp, i != 1)) {
2472 error = -EFSCORRUPTED;
2473 goto done;
2474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476 break;
2477
2478 case 0:
2479 /*
2480 * Setting the middle part of a previous oldext extent to
2481 * newext. Contiguity is impossible here.
2482 * One extent becomes three extents.
2483 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002484 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002485 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 r[0] = *new;
2488 r[1].br_startoff = new_endoff;
2489 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002490 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002492 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002493
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002494 xfs_iext_update_extent(ip, state, icur, &PREV);
2495 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002496 xfs_iext_insert(ip, icur, &r[1], state);
2497 xfs_iext_insert(ip, icur, &r[0], state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002498
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002499 XFS_IFORK_NEXT_SET(ip, whichfork,
2500 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 if (cur == NULL)
2502 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2503 else {
2504 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002505 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002506 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 != 1)) {
2509 error = -EFSCORRUPTED;
2510 goto done;
2511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002513 error = xfs_bmbt_update(cur, &r[1]);
2514 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 goto done;
2516 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002518 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002520 if (XFS_IS_CORRUPT(mp, i != 1)) {
2521 error = -EFSCORRUPTED;
2522 goto done;
2523 }
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002524 /*
2525 * Reset the cursor to the position of the new extent
2526 * we are about to insert as we can't trust it after
2527 * the previous insert.
2528 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002529 error = xfs_bmbt_lookup_eq(cur, new, &i);
2530 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002532 if (XFS_IS_CORRUPT(mp, i != 0)) {
2533 error = -EFSCORRUPTED;
2534 goto done;
2535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002537 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002539 if (XFS_IS_CORRUPT(mp, i != 1)) {
2540 error = -EFSCORRUPTED;
2541 goto done;
2542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
2544 break;
2545
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002546 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2547 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2548 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2549 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2550 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2551 case BMAP_LEFT_CONTIG:
2552 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 /*
2554 * These cases are all impossible.
2555 */
2556 ASSERT(0);
2557 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002558
Darrick J. Wong9c194642016-08-03 12:16:05 +10002559 /* update reverse mappings */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07002560 xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002561
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002562 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002563 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002564 int tmp_logflags; /* partial log flag return val */
2565
2566 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07002567 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2568 &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002569 *logflagsp |= tmp_logflags;
2570 if (error)
2571 goto done;
2572 }
2573
2574 /* clear out the allocated field, done with it now in any case. */
2575 if (cur) {
Dave Chinner92219c22020-03-10 17:52:53 -07002576 cur->bc_ino.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002577 *curp = cur;
2578 }
2579
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002580 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002582 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 return error;
2584#undef LEFT
2585#undef RIGHT
2586#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587}
2588
2589/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002590 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002592STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593xfs_bmap_add_extent_hole_delay(
2594 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002595 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002596 struct xfs_iext_cursor *icur,
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002597 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598{
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07002599 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2601 xfs_filblks_t newlen=0; /* new indirect size */
2602 xfs_filblks_t oldlen=0; /* old indirect size */
2603 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002604 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002605 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002607 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002608 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 /*
2611 * Check and set flags if this segment has a left neighbor
2612 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002613 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002614 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002615 if (isnullstartblock(left.br_startblock))
2616 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 /*
2620 * Check and set flags if the current (right) segment exists.
2621 * If it doesn't exist, we're converting the hole at end-of-file.
2622 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002623 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002624 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002625 if (isnullstartblock(right.br_startblock))
2626 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 /*
2630 * Set contiguity flags on the left and right neighbors.
2631 * Don't let extents get too large, even if the pieces are contiguous.
2632 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002633 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2634 left.br_startoff + left.br_blockcount == new->br_startoff &&
2635 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2636 state |= BMAP_LEFT_CONTIG;
2637
2638 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2639 new->br_startoff + new->br_blockcount == right.br_startoff &&
2640 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2641 (!(state & BMAP_LEFT_CONTIG) ||
2642 (left.br_blockcount + new->br_blockcount +
2643 right.br_blockcount <= MAXEXTLEN)))
2644 state |= BMAP_RIGHT_CONTIG;
2645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 /*
2647 * Switch out based on the contiguity flags.
2648 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002649 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2650 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 /*
2652 * New allocation is contiguous with delayed allocations
2653 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002654 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 */
2656 temp = left.br_blockcount + new->br_blockcount +
2657 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002658
Eric Sandeen9d87c312009-01-14 23:22:07 -06002659 oldlen = startblockval(left.br_startblock) +
2660 startblockval(new->br_startblock) +
2661 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002662 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2663 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002664 left.br_startblock = nullstartblock(newlen);
2665 left.br_blockcount = temp;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002666
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002667 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002668 xfs_iext_prev(ifp, icur);
2669 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 break;
2671
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002672 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 /*
2674 * New allocation is contiguous with a delayed allocation
2675 * on the left.
2676 * Merge the new allocation with the left neighbor.
2677 */
2678 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002679
Eric Sandeen9d87c312009-01-14 23:22:07 -06002680 oldlen = startblockval(left.br_startblock) +
2681 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002682 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2683 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002684 left.br_blockcount = temp;
2685 left.br_startblock = nullstartblock(newlen);
Christoph Hellwig41d196f2017-11-03 10:34:39 -07002686
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002687 xfs_iext_prev(ifp, icur);
2688 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 break;
2690
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002691 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 /*
2693 * New allocation is contiguous with a delayed allocation
2694 * on the right.
2695 * Merge the new allocation with the right neighbor.
2696 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002698 oldlen = startblockval(new->br_startblock) +
2699 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002700 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2701 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002702 right.br_startoff = new->br_startoff;
2703 right.br_startblock = nullstartblock(newlen);
2704 right.br_blockcount = temp;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002705 xfs_iext_update_extent(ip, state, icur, &right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 break;
2707
2708 case 0:
2709 /*
2710 * New allocation is not contiguous with another
2711 * delayed allocation.
2712 * Insert a new entry.
2713 */
2714 oldlen = newlen = 0;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002715 xfs_iext_insert(ip, icur, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 break;
2717 }
2718 if (oldlen != newlen) {
2719 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002720 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2721 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 /*
2723 * Nothing to do for disk quota accounting here.
2724 */
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07002725 xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727}
2728
2729/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002730 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 */
2732STATIC int /* error */
2733xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002734 struct xfs_trans *tp,
2735 struct xfs_inode *ip,
2736 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002737 struct xfs_iext_cursor *icur,
Christoph Hellwig6d045582017-04-11 16:45:54 -07002738 struct xfs_btree_cur **curp,
2739 struct xfs_bmbt_irec *new,
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002740 int *logflagsp,
2741 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002743 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2744 struct xfs_mount *mp = ip->i_mount;
2745 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2749 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002750 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002751 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002752 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002754 ASSERT(!isnullstartblock(new->br_startblock));
Dave Chinner92219c22020-03-10 17:52:53 -07002755 ASSERT(!cur || !(cur->bc_ino.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002756
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002757 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 /*
2760 * Check and set flags if this segment has a left neighbor.
2761 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002762 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002763 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002764 if (isnullstartblock(left.br_startblock))
2765 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002767
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 /*
2769 * Check and set flags if this segment has a current value.
2770 * Not true if we're inserting into the "hole" at eof.
2771 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002772 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002773 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002774 if (isnullstartblock(right.br_startblock))
2775 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002777
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 /*
2779 * We're inserting a real allocation between "left" and "right".
2780 * Set the contiguity flags. Don't let extents get too large.
2781 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002782 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2783 left.br_startoff + left.br_blockcount == new->br_startoff &&
2784 left.br_startblock + left.br_blockcount == new->br_startblock &&
2785 left.br_state == new->br_state &&
2786 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2787 state |= BMAP_LEFT_CONTIG;
2788
2789 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2790 new->br_startoff + new->br_blockcount == right.br_startoff &&
2791 new->br_startblock + new->br_blockcount == right.br_startblock &&
2792 new->br_state == right.br_state &&
2793 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2794 (!(state & BMAP_LEFT_CONTIG) ||
2795 left.br_blockcount + new->br_blockcount +
2796 right.br_blockcount <= MAXEXTLEN))
2797 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002799 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 /*
2801 * Select which case we're in here, and implement it.
2802 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002803 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2804 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 /*
2806 * New allocation is contiguous with real allocations on the
2807 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002808 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002810 left.br_blockcount += new->br_blockcount + right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002811
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002812 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002813 xfs_iext_prev(ifp, icur);
2814 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002815
Christoph Hellwig6d045582017-04-11 16:45:54 -07002816 XFS_IFORK_NEXT_SET(ip, whichfork,
2817 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2818 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002819 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002820 } else {
2821 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002822 error = xfs_bmbt_lookup_eq(cur, &right, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002823 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002824 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002825 if (XFS_IS_CORRUPT(mp, i != 1)) {
2826 error = -EFSCORRUPTED;
2827 goto done;
2828 }
Christoph Hellwig6d045582017-04-11 16:45:54 -07002829 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002830 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002831 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002832 if (XFS_IS_CORRUPT(mp, i != 1)) {
2833 error = -EFSCORRUPTED;
2834 goto done;
2835 }
Christoph Hellwig6d045582017-04-11 16:45:54 -07002836 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002837 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002838 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002839 if (XFS_IS_CORRUPT(mp, i != 1)) {
2840 error = -EFSCORRUPTED;
2841 goto done;
2842 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002843 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002844 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002845 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002847 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002849 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 /*
2851 * New allocation is contiguous with a real allocation
2852 * on the left.
2853 * Merge the new allocation with the left neighbor.
2854 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002855 old = left;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002856 left.br_blockcount += new->br_blockcount;
Christoph Hellwig1d2e0082017-11-03 10:34:40 -07002857
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002858 xfs_iext_prev(ifp, icur);
2859 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002860
Christoph Hellwig6d045582017-04-11 16:45:54 -07002861 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002862 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002863 } else {
2864 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002865 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002866 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002867 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002868 if (XFS_IS_CORRUPT(mp, i != 1)) {
2869 error = -EFSCORRUPTED;
2870 goto done;
2871 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002872 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002873 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002874 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002876 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002878 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 /*
2880 * New allocation is contiguous with a real allocation
2881 * on the right.
2882 * Merge the new allocation with the right neighbor.
2883 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002884 old = right;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07002885
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002886 right.br_startoff = new->br_startoff;
2887 right.br_startblock = new->br_startblock;
2888 right.br_blockcount += new->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002889 xfs_iext_update_extent(ip, state, icur, &right);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002890
Christoph Hellwig6d045582017-04-11 16:45:54 -07002891 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002892 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002893 } else {
2894 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002895 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002896 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002897 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002898 if (XFS_IS_CORRUPT(mp, i != 1)) {
2899 error = -EFSCORRUPTED;
2900 goto done;
2901 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002902 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002903 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002904 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002906 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
2908 case 0:
2909 /*
2910 * New allocation is not contiguous with another
2911 * real allocation.
2912 * Insert a new entry.
2913 */
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002914 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002915 XFS_IFORK_NEXT_SET(ip, whichfork,
2916 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2917 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002918 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002919 } else {
2920 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002921 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002922 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002923 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002924 if (XFS_IS_CORRUPT(mp, i != 0)) {
2925 error = -EFSCORRUPTED;
2926 goto done;
2927 }
Christoph Hellwig6d045582017-04-11 16:45:54 -07002928 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002929 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002930 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002931 if (XFS_IS_CORRUPT(mp, i != 1)) {
2932 error = -EFSCORRUPTED;
2933 goto done;
2934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002936 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002938
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002939 /* add reverse mapping unless caller opted out */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07002940 if (!(flags & XFS_BMAPI_NORMAP))
2941 xfs_rmap_map_extent(tp, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002942
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002943 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002944 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002945 int tmp_logflags; /* partial log flag return val */
2946
Christoph Hellwig6d045582017-04-11 16:45:54 -07002947 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07002948 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2949 &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002950 *logflagsp |= tmp_logflags;
2951 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002952 if (error)
2953 goto done;
2954 }
2955
2956 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002957 if (cur)
Dave Chinner92219c22020-03-10 17:52:53 -07002958 cur->bc_ino.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002959
Christoph Hellwig6d045582017-04-11 16:45:54 -07002960 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002961done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002962 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002963 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964}
2965
Nathan Scottdd9f4382006-01-11 15:28:28 +11002966/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11002967 * Functions used in the extent read, allocate and remove paths
2968 */
2969
2970/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002971 * Adjust the size of the new extent based on di_extsize and rt extsize.
2972 */
Dave Chinner68988112013-08-12 20:49:42 +10002973int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002974xfs_bmap_extsize_align(
2975 xfs_mount_t *mp,
2976 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2977 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2978 xfs_extlen_t extsz, /* align to this extent size */
2979 int rt, /* is this a realtime inode? */
2980 int eof, /* is extent at end-of-file? */
2981 int delay, /* creating delalloc extent? */
2982 int convert, /* overwriting unwritten extent? */
2983 xfs_fileoff_t *offp, /* in/out: aligned offset */
2984 xfs_extlen_t *lenp) /* in/out: aligned length */
2985{
2986 xfs_fileoff_t orig_off; /* original offset */
2987 xfs_extlen_t orig_alen; /* original length */
2988 xfs_fileoff_t orig_end; /* original off+len */
2989 xfs_fileoff_t nexto; /* next file offset */
2990 xfs_fileoff_t prevo; /* previous file offset */
2991 xfs_fileoff_t align_off; /* temp for offset */
2992 xfs_extlen_t align_alen; /* temp for length */
2993 xfs_extlen_t temp; /* temp for calculations */
2994
2995 if (convert)
2996 return 0;
2997
2998 orig_off = align_off = *offp;
2999 orig_alen = align_alen = *lenp;
3000 orig_end = orig_off + orig_alen;
3001
3002 /*
3003 * If this request overlaps an existing extent, then don't
3004 * attempt to perform any additional alignment.
3005 */
3006 if (!delay && !eof &&
3007 (orig_off >= gotp->br_startoff) &&
3008 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3009 return 0;
3010 }
3011
3012 /*
3013 * If the file offset is unaligned vs. the extent size
3014 * we need to align it. This will be possible unless
3015 * the file was previously written with a kernel that didn't
3016 * perform this alignment, or if a truncate shot us in the
3017 * foot.
3018 */
Dave Chinner0703a8e2018-06-08 09:54:22 -07003019 div_u64_rem(orig_off, extsz, &temp);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003020 if (temp) {
3021 align_alen += temp;
3022 align_off -= temp;
3023 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003024
3025 /* Same adjustment for the end of the requested area. */
3026 temp = (align_alen % extsz);
3027 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003028 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003029
3030 /*
3031 * For large extent hint sizes, the aligned extent might be larger than
3032 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3033 * the length back under MAXEXTLEN. The outer allocation loops handle
3034 * short allocation just fine, so it is safe to do this. We only want to
3035 * do it when we are forced to, though, because it means more allocation
3036 * operations are required.
3037 */
3038 while (align_alen > MAXEXTLEN)
3039 align_alen -= extsz;
3040 ASSERT(align_alen <= MAXEXTLEN);
3041
Nathan Scottdd9f4382006-01-11 15:28:28 +11003042 /*
3043 * If the previous block overlaps with this proposed allocation
3044 * then move the start forward without adjusting the length.
3045 */
3046 if (prevp->br_startoff != NULLFILEOFF) {
3047 if (prevp->br_startblock == HOLESTARTBLOCK)
3048 prevo = prevp->br_startoff;
3049 else
3050 prevo = prevp->br_startoff + prevp->br_blockcount;
3051 } else
3052 prevo = 0;
3053 if (align_off != orig_off && align_off < prevo)
3054 align_off = prevo;
3055 /*
3056 * If the next block overlaps with this proposed allocation
3057 * then move the start back without adjusting the length,
3058 * but not before offset 0.
3059 * This may of course make the start overlap previous block,
3060 * and if we hit the offset 0 limit then the next block
3061 * can still overlap too.
3062 */
3063 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3064 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3065 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3066 nexto = gotp->br_startoff + gotp->br_blockcount;
3067 else
3068 nexto = gotp->br_startoff;
3069 } else
3070 nexto = NULLFILEOFF;
3071 if (!eof &&
3072 align_off + align_alen != orig_end &&
3073 align_off + align_alen > nexto)
3074 align_off = nexto > align_alen ? nexto - align_alen : 0;
3075 /*
3076 * If we're now overlapping the next or previous extent that
3077 * means we can't fit an extsz piece in this hole. Just move
3078 * the start forward to the first valid spot and set
3079 * the length so we hit the end.
3080 */
3081 if (align_off != orig_off && align_off < prevo)
3082 align_off = prevo;
3083 if (align_off + align_alen != orig_end &&
3084 align_off + align_alen > nexto &&
3085 nexto != NULLFILEOFF) {
3086 ASSERT(nexto > prevo);
3087 align_alen = nexto - align_off;
3088 }
3089
3090 /*
3091 * If realtime, and the result isn't a multiple of the realtime
3092 * extent size we need to remove blocks until it is.
3093 */
3094 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3095 /*
3096 * We're not covering the original request, or
3097 * we won't be able to once we fix the length.
3098 */
3099 if (orig_off < align_off ||
3100 orig_end > align_off + align_alen ||
3101 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003102 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003103 /*
3104 * Try to fix it by moving the start up.
3105 */
3106 if (align_off + temp <= orig_off) {
3107 align_alen -= temp;
3108 align_off += temp;
3109 }
3110 /*
3111 * Try to fix it by moving the end in.
3112 */
3113 else if (align_off + align_alen - temp >= orig_end)
3114 align_alen -= temp;
3115 /*
3116 * Set the start to the minimum then trim the length.
3117 */
3118 else {
3119 align_alen -= orig_off - align_off;
3120 align_off = orig_off;
3121 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3122 }
3123 /*
3124 * Result doesn't cover the request, fail it.
3125 */
3126 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003127 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003128 } else {
3129 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003130 /* see MAXEXTLEN handling above */
3131 ASSERT(orig_end <= align_off + align_alen ||
3132 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003133 }
3134
3135#ifdef DEBUG
3136 if (!eof && gotp->br_startoff != NULLFILEOFF)
3137 ASSERT(align_off + align_alen <= gotp->br_startoff);
3138 if (prevp->br_startoff != NULLFILEOFF)
3139 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3140#endif
3141
3142 *lenp = align_alen;
3143 *offp = align_off;
3144 return 0;
3145}
3146
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147#define XFS_ALLOC_GAP_UNITS 4
3148
Dave Chinner68988112013-08-12 20:49:42 +10003149void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003150xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003151 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
3153 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3155 xfs_mount_t *mp; /* mount point structure */
3156 int nullfb; /* true if ap->firstblock isn't set */
3157 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
3159#define ISVALID(x,y) \
3160 (rt ? \
3161 (x) < mp->m_sb.sb_rblocks : \
3162 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3163 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3164 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3165
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 mp = ap->ip->i_mount;
Brian Foster94c07b42018-07-11 22:26:28 -07003167 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003168 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
Christoph Hellwigc34d5702019-10-30 12:25:00 -07003169 (ap->datatype & XFS_ALLOC_USERDATA);
Brian Foster94c07b42018-07-11 22:26:28 -07003170 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3171 ap->tp->t_firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 /*
3173 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003174 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003176 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3177 !isnullstartblock(ap->prev.br_startblock) &&
3178 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3179 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003180 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 /*
3182 * Adjust for the gap between prevp and us.
3183 */
Dave Chinner3a756672011-09-18 20:40:58 +00003184 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003185 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003187 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3188 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 }
3190 /*
3191 * If not at eof, then compare the two neighbor blocks.
3192 * Figure out whether either one gives us a good starting point,
3193 * and pick the better one.
3194 */
3195 else if (!ap->eof) {
3196 xfs_fsblock_t gotbno; /* right side block number */
3197 xfs_fsblock_t gotdiff=0; /* right side difference */
3198 xfs_fsblock_t prevbno; /* left side block number */
3199 xfs_fsblock_t prevdiff=0; /* left side difference */
3200
3201 /*
3202 * If there's a previous (left) block, select a requested
3203 * start block based on it.
3204 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003205 if (ap->prev.br_startoff != NULLFILEOFF &&
3206 !isnullstartblock(ap->prev.br_startblock) &&
3207 (prevbno = ap->prev.br_startblock +
3208 ap->prev.br_blockcount) &&
3209 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 /*
3211 * Calculate gap to end of previous block.
3212 */
Dave Chinner3a756672011-09-18 20:40:58 +00003213 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003214 (ap->prev.br_startoff +
3215 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 /*
3217 * Figure the startblock based on the previous block's
3218 * end and the gap size.
3219 * Heuristic!
3220 * If the gap is large relative to the piece we're
3221 * allocating, or using it gives us an invalid block
3222 * number, then just use the end of the previous block.
3223 */
Dave Chinner3a756672011-09-18 20:40:58 +00003224 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003226 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 prevbno += adjust;
3228 else
3229 prevdiff += adjust;
3230 /*
3231 * If the firstblock forbids it, can't use it,
3232 * must use default.
3233 */
3234 if (!rt && !nullfb &&
3235 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3236 prevbno = NULLFSBLOCK;
3237 }
3238 /*
3239 * No previous block or can't follow it, just default.
3240 */
3241 else
3242 prevbno = NULLFSBLOCK;
3243 /*
3244 * If there's a following (right) block, select a requested
3245 * start block based on it.
3246 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003247 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 /*
3249 * Calculate gap to start of next block.
3250 */
Dave Chinner3a756672011-09-18 20:40:58 +00003251 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 /*
3253 * Figure the startblock based on the next block's
3254 * start and the gap size.
3255 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003256 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 /*
3258 * Heuristic!
3259 * If the gap is large relative to the piece we're
3260 * allocating, or using it gives us an invalid block
3261 * number, then just use the start of the next block
3262 * offset by our length.
3263 */
Dave Chinner3a756672011-09-18 20:40:58 +00003264 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 ISVALID(gotbno - gotdiff, gotbno))
3266 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003267 else if (ISVALID(gotbno - ap->length, gotbno)) {
3268 gotbno -= ap->length;
3269 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 } else
3271 gotdiff += adjust;
3272 /*
3273 * If the firstblock forbids it, can't use it,
3274 * must use default.
3275 */
3276 if (!rt && !nullfb &&
3277 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3278 gotbno = NULLFSBLOCK;
3279 }
3280 /*
3281 * No next block, just default.
3282 */
3283 else
3284 gotbno = NULLFSBLOCK;
3285 /*
3286 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003287 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 */
3289 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003290 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003292 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003294 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003296#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003297}
3298
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003299static int
3300xfs_bmap_longest_free_extent(
3301 struct xfs_trans *tp,
3302 xfs_agnumber_t ag,
3303 xfs_extlen_t *blen,
3304 int *notinit)
3305{
3306 struct xfs_mount *mp = tp->t_mountp;
3307 struct xfs_perag *pag;
3308 xfs_extlen_t longest;
3309 int error = 0;
3310
3311 pag = xfs_perag_get(mp, ag);
3312 if (!pag->pagf_init) {
3313 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
Darrick J. Wongf48e2df2020-01-23 17:01:19 -08003314 if (error) {
3315 /* Couldn't lock the AGF, so skip this AG. */
3316 if (error == -EAGAIN) {
3317 *notinit = 1;
3318 error = 0;
3319 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003320 goto out;
3321 }
3322 }
3323
Eric Sandeena1f69412018-04-06 10:09:42 -07003324 longest = xfs_alloc_longest_free_extent(pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003325 xfs_alloc_min_freelist(mp, pag),
3326 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003327 if (*blen < longest)
3328 *blen = longest;
3329
3330out:
3331 xfs_perag_put(pag);
3332 return error;
3333}
3334
3335static void
3336xfs_bmap_select_minlen(
3337 struct xfs_bmalloca *ap,
3338 struct xfs_alloc_arg *args,
3339 xfs_extlen_t *blen,
3340 int notinit)
3341{
3342 if (notinit || *blen < ap->minlen) {
3343 /*
3344 * Since we did a BUF_TRYLOCK above, it is possible that
3345 * there is space for this request.
3346 */
3347 args->minlen = ap->minlen;
3348 } else if (*blen < args->maxlen) {
3349 /*
3350 * If the best seen length is less than the request length,
3351 * use the best as the minimum.
3352 */
3353 args->minlen = *blen;
3354 } else {
3355 /*
3356 * Otherwise we've seen an extent as big as maxlen, use that
3357 * as the minimum.
3358 */
3359 args->minlen = args->maxlen;
3360 }
3361}
3362
Nathan Scotta365bdd2006-03-14 13:34:16 +11003363STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003364xfs_bmap_btalloc_nullfb(
3365 struct xfs_bmalloca *ap,
3366 struct xfs_alloc_arg *args,
3367 xfs_extlen_t *blen)
3368{
3369 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003370 xfs_agnumber_t ag, startag;
3371 int notinit = 0;
3372 int error;
3373
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003374 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003375 args->total = ap->total;
3376
Christoph Hellwigc467c042010-02-15 23:34:42 +00003377 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3378 if (startag == NULLAGNUMBER)
3379 startag = ag = 0;
3380
Dave Chinner14b064c2011-01-27 12:16:28 +11003381 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003382 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3383 &notinit);
3384 if (error)
3385 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003386
Christoph Hellwigc467c042010-02-15 23:34:42 +00003387 if (++ag == mp->m_sb.sb_agcount)
3388 ag = 0;
3389 if (ag == startag)
3390 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003391 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003392
3393 xfs_bmap_select_minlen(ap, args, blen, notinit);
3394 return 0;
3395}
3396
3397STATIC int
3398xfs_bmap_btalloc_filestreams(
3399 struct xfs_bmalloca *ap,
3400 struct xfs_alloc_arg *args,
3401 xfs_extlen_t *blen)
3402{
3403 struct xfs_mount *mp = ap->ip->i_mount;
3404 xfs_agnumber_t ag;
3405 int notinit = 0;
3406 int error;
3407
3408 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3409 args->total = ap->total;
3410
3411 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3412 if (ag == NULLAGNUMBER)
3413 ag = 0;
3414
3415 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3416 if (error)
3417 return error;
3418
3419 if (*blen < args->maxlen) {
3420 error = xfs_filestream_new_ag(ap, &ag);
3421 if (error)
3422 return error;
3423
3424 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3425 &notinit);
3426 if (error)
3427 return error;
3428
3429 }
3430
3431 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003432
3433 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003434 * Set the failure fallback case to look in the selected AG as stream
3435 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003436 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003437 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003438 return 0;
3439}
3440
Darrick J. Wong751f3762018-01-25 13:58:13 -08003441/* Update all inode and quota accounting for the allocation we just did. */
3442static void
3443xfs_bmap_btalloc_accounting(
3444 struct xfs_bmalloca *ap,
3445 struct xfs_alloc_arg *args)
3446{
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003447 if (ap->flags & XFS_BMAPI_COWFORK) {
3448 /*
3449 * COW fork blocks are in-core only and thus are treated as
3450 * in-core quota reservation (like delalloc blocks) even when
3451 * converted to real blocks. The quota reservation is not
3452 * accounted to disk until blocks are remapped to the data
3453 * fork. So if these blocks were previously delalloc, we
3454 * already have quota reservation and there's nothing to do
3455 * yet.
3456 */
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003457 if (ap->wasdel) {
3458 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003459 return;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003460 }
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003461
3462 /*
3463 * Otherwise, we've allocated blocks in a hole. The transaction
3464 * has acquired in-core quota reservation for this extent.
3465 * Rather than account these as real blocks, however, we reduce
3466 * the transaction quota reservation based on the allocation.
3467 * This essentially transfers the transaction quota reservation
3468 * to that of a delalloc extent.
3469 */
3470 ap->ip->i_delayed_blks += args->len;
3471 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3472 -(long)args->len);
3473 return;
3474 }
3475
3476 /* data/attr fork only */
3477 ap->ip->i_d.di_nblocks += args->len;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003478 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003479 if (ap->wasdel) {
Darrick J. Wong751f3762018-01-25 13:58:13 -08003480 ap->ip->i_delayed_blks -= args->len;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003481 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3482 }
Darrick J. Wong751f3762018-01-25 13:58:13 -08003483 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3484 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3485 args->len);
3486}
3487
Christoph Hellwigc467c042010-02-15 23:34:42 +00003488STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003489xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003490 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003491{
3492 xfs_mount_t *mp; /* mount point structure */
3493 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003494 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003495 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003496 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003497 xfs_alloc_arg_t args;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003498 xfs_fileoff_t orig_offset;
3499 xfs_extlen_t orig_length;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003500 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003501 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003502 int nullfb; /* true if ap->firstblock isn't set */
3503 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003504 int tryagain;
3505 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003506 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003507
Dave Chinnera99ebf42011-12-01 11:24:20 +00003508 ASSERT(ap->length);
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003509 orig_offset = ap->offset;
3510 orig_length = ap->length;
Dave Chinnera99ebf42011-12-01 11:24:20 +00003511
Nathan Scotta365bdd2006-03-14 13:34:16 +11003512 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003513
3514 /* stripe alignment for allocation is determined by mount parameters */
3515 stripe_align = 0;
3516 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3517 stripe_align = mp->m_swidth;
3518 else if (mp->m_dalign)
3519 stripe_align = mp->m_dalign;
3520
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003521 if (ap->flags & XFS_BMAPI_COWFORK)
3522 align = xfs_get_cowextsz_hint(ap->ip);
Christoph Hellwigc34d5702019-10-30 12:25:00 -07003523 else if (ap->datatype & XFS_ALLOC_USERDATA)
Dave Chinner292378e2016-09-26 08:21:28 +10003524 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003525 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003526 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003527 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003528 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003529 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003530 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003531 }
Dave Chinner33177f052013-12-12 16:34:36 +11003532
3533
Brian Foster94c07b42018-07-11 22:26:28 -07003534 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3535 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3536 ap->tp->t_firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003537 if (nullfb) {
Christoph Hellwigc34d5702019-10-30 12:25:00 -07003538 if ((ap->datatype & XFS_ALLOC_USERDATA) &&
Dave Chinner292378e2016-09-26 08:21:28 +10003539 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003540 ag = xfs_filestream_lookup_ag(ap->ip);
3541 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003542 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003543 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003544 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003545 }
3546 } else
Brian Foster94c07b42018-07-11 22:26:28 -07003547 ap->blkno = ap->tp->t_firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003548
3549 xfs_bmap_adjacent(ap);
3550
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003552 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 * it's in the right allocation group.
3554 */
Dave Chinner3a756672011-09-18 20:40:58 +00003555 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 ;
3557 else
Brian Foster94c07b42018-07-11 22:26:28 -07003558 ap->blkno = ap->tp->t_firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 * Normal allocation, done through xfs_alloc_vextent.
3561 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003562 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003563 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003564 args.tp = ap->tp;
3565 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003566 args.fsbno = ap->blkno;
Darrick J. Wong7280fed2018-12-12 08:46:23 -08003567 args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
Dave Chinner14b064c2011-01-27 12:16:28 +11003568
3569 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner9bb54cb2018-06-07 07:54:02 -07003570 args.maxlen = min(ap->length, mp->m_ag_max_usable);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003571 blen = 0;
3572 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003573 /*
3574 * Search for an allocation group with a single extent large
3575 * enough for the request. If one isn't found, then adjust
3576 * the minimum allocation size to the largest space found.
3577 */
Christoph Hellwigc34d5702019-10-30 12:25:00 -07003578 if ((ap->datatype & XFS_ALLOC_USERDATA) &&
Dave Chinner292378e2016-09-26 08:21:28 +10003579 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003580 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3581 else
3582 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003583 if (error)
3584 return error;
Brian Foster1214f1c2018-08-01 07:20:31 -07003585 } else if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003586 if (xfs_inode_is_filestream(ap->ip))
3587 args.type = XFS_ALLOCTYPE_FIRST_AG;
3588 else
3589 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003590 args.total = args.minlen = ap->minlen;
3591 } else {
3592 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3593 args.total = ap->total;
3594 args.minlen = ap->minlen;
3595 }
David Chinner957d0eb2007-06-18 16:50:37 +10003596 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003597 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003598 args.prod = align;
Dave Chinner0703a8e2018-06-08 09:54:22 -07003599 div_u64_rem(ap->offset, args.prod, &args.mod);
3600 if (args.mod)
3601 args.mod = args.prod - args.mod;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003602 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003603 args.prod = 1;
3604 args.mod = 0;
3605 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003606 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner0703a8e2018-06-08 09:54:22 -07003607 div_u64_rem(ap->offset, args.prod, &args.mod);
3608 if (args.mod)
3609 args.mod = args.prod - args.mod;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003610 }
3611 /*
Dave Chinner1c743572019-10-21 09:26:34 -07003612 * If we are not low on available data blocks, and the underlying
3613 * logical volume manager is a stripe, and the file offset is zero then
3614 * try to allocate data blocks on stripe unit boundary. NOTE: ap->aeof
3615 * is only set if the allocation length is >= the stripe unit and the
3616 * allocation offset is at the end of file.
Nathan Scotta365bdd2006-03-14 13:34:16 +11003617 */
Brian Foster1214f1c2018-08-01 07:20:31 -07003618 if (!(ap->tp->t_flags & XFS_TRANS_LOWMODE) && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003619 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003620 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003621 atype = args.type;
3622 isaligned = 1;
3623 /*
Dave Chinner1c743572019-10-21 09:26:34 -07003624 * Adjust minlen to try and preserve alignment if we
3625 * can't guarantee an aligned maxlen extent.
Nathan Scotta365bdd2006-03-14 13:34:16 +11003626 */
Dave Chinner1c743572019-10-21 09:26:34 -07003627 if (blen > args.alignment &&
3628 blen <= args.maxlen + args.alignment)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003629 args.minlen = blen - args.alignment;
3630 args.minalignslop = 0;
3631 } else {
3632 /*
3633 * First try an exact bno allocation.
3634 * If it fails then do a near or start bno
3635 * allocation with alignment turned on.
3636 */
3637 atype = args.type;
3638 tryagain = 1;
3639 args.type = XFS_ALLOCTYPE_THIS_BNO;
3640 args.alignment = 1;
3641 /*
3642 * Compute the minlen+alignment for the
3643 * next case. Set slop so that the value
3644 * of minlen+alignment+slop doesn't go up
3645 * between the calls.
3646 */
Dave Chinner33177f052013-12-12 16:34:36 +11003647 if (blen > stripe_align && blen <= args.maxlen)
3648 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003649 else
3650 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003651 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003652 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003653 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003654 args.minlen - 1;
3655 else
3656 args.minalignslop = 0;
3657 }
3658 } else {
3659 args.alignment = 1;
3660 args.minalignslop = 0;
3661 }
3662 args.minleft = ap->minleft;
3663 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003664 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003665 args.datatype = ap->datatype;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003666
3667 error = xfs_alloc_vextent(&args);
3668 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003669 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003670
Nathan Scotta365bdd2006-03-14 13:34:16 +11003671 if (tryagain && args.fsbno == NULLFSBLOCK) {
3672 /*
3673 * Exact allocation failed. Now try with alignment
3674 * turned on.
3675 */
3676 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003677 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003678 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003679 args.minlen = nextminlen;
3680 args.minalignslop = 0;
3681 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 if ((error = xfs_alloc_vextent(&args)))
3683 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003684 }
3685 if (isaligned && args.fsbno == NULLFSBLOCK) {
3686 /*
3687 * allocation failed, so turn off alignment and
3688 * try again.
3689 */
3690 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003691 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003692 args.alignment = 0;
3693 if ((error = xfs_alloc_vextent(&args)))
3694 return error;
3695 }
3696 if (args.fsbno == NULLFSBLOCK && nullfb &&
3697 args.minlen > ap->minlen) {
3698 args.minlen = ap->minlen;
3699 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003700 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003701 if ((error = xfs_alloc_vextent(&args)))
3702 return error;
3703 }
3704 if (args.fsbno == NULLFSBLOCK && nullfb) {
3705 args.fsbno = 0;
3706 args.type = XFS_ALLOCTYPE_FIRST_AG;
3707 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003708 if ((error = xfs_alloc_vextent(&args)))
3709 return error;
Brian Foster1214f1c2018-08-01 07:20:31 -07003710 ap->tp->t_flags |= XFS_TRANS_LOWMODE;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003711 }
3712 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003713 /*
3714 * check the allocation happened at the same or higher AG than
3715 * the first block that was allocated.
3716 */
Brian Foster94c07b42018-07-11 22:26:28 -07003717 ASSERT(ap->tp->t_firstblock == NULLFSBLOCK ||
3718 XFS_FSB_TO_AGNO(mp, ap->tp->t_firstblock) <=
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003719 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003720
Dave Chinner3a756672011-09-18 20:40:58 +00003721 ap->blkno = args.fsbno;
Brian Foster94c07b42018-07-11 22:26:28 -07003722 if (ap->tp->t_firstblock == NULLFSBLOCK)
3723 ap->tp->t_firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003724 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003725 ap->length = args.len;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003726 /*
3727 * If the extent size hint is active, we tried to round the
3728 * caller's allocation request offset down to extsz and the
3729 * length up to another extsz boundary. If we found a free
3730 * extent we mapped it in starting at this new offset. If the
3731 * newly mapped space isn't long enough to cover any of the
3732 * range of offsets that was originally requested, move the
3733 * mapping up so that we can fill as much of the caller's
3734 * original request as possible. Free space is apparently
3735 * very fragmented so we're unlikely to be able to satisfy the
3736 * hints anyway.
3737 */
3738 if (ap->length <= orig_length)
3739 ap->offset = orig_offset;
3740 else if (ap->offset + ap->length < orig_offset + orig_length)
3741 ap->offset = orig_offset + orig_length - ap->length;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003742 xfs_bmap_btalloc_accounting(ap, &args);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003743 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003744 ap->blkno = NULLFSBLOCK;
3745 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 }
3747 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003748}
3749
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003750/* Trim extent to fit a logical block range. */
3751void
3752xfs_trim_extent(
3753 struct xfs_bmbt_irec *irec,
3754 xfs_fileoff_t bno,
3755 xfs_filblks_t len)
3756{
3757 xfs_fileoff_t distance;
3758 xfs_fileoff_t end = bno + len;
3759
3760 if (irec->br_startoff + irec->br_blockcount <= bno ||
3761 irec->br_startoff >= end) {
3762 irec->br_blockcount = 0;
3763 return;
3764 }
3765
3766 if (irec->br_startoff < bno) {
3767 distance = bno - irec->br_startoff;
3768 if (isnullstartblock(irec->br_startblock))
3769 irec->br_startblock = DELAYSTARTBLOCK;
3770 if (irec->br_startblock != DELAYSTARTBLOCK &&
3771 irec->br_startblock != HOLESTARTBLOCK)
3772 irec->br_startblock += distance;
3773 irec->br_startoff += distance;
3774 irec->br_blockcount -= distance;
3775 }
3776
3777 if (end < irec->br_startoff + irec->br_blockcount) {
3778 distance = irec->br_startoff + irec->br_blockcount - end;
3779 irec->br_blockcount -= distance;
3780 }
3781}
3782
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003784 * Trim the returned map to the required bounds
3785 */
3786STATIC void
3787xfs_bmapi_trim_map(
3788 struct xfs_bmbt_irec *mval,
3789 struct xfs_bmbt_irec *got,
3790 xfs_fileoff_t *bno,
3791 xfs_filblks_t len,
3792 xfs_fileoff_t obno,
3793 xfs_fileoff_t end,
3794 int n,
3795 int flags)
3796{
3797 if ((flags & XFS_BMAPI_ENTIRE) ||
3798 got->br_startoff + got->br_blockcount <= obno) {
3799 *mval = *got;
3800 if (isnullstartblock(got->br_startblock))
3801 mval->br_startblock = DELAYSTARTBLOCK;
3802 return;
3803 }
3804
3805 if (obno > *bno)
3806 *bno = obno;
3807 ASSERT((*bno >= obno) || (n == 0));
3808 ASSERT(*bno < end);
3809 mval->br_startoff = *bno;
3810 if (isnullstartblock(got->br_startblock))
3811 mval->br_startblock = DELAYSTARTBLOCK;
3812 else
3813 mval->br_startblock = got->br_startblock +
3814 (*bno - got->br_startoff);
3815 /*
3816 * Return the minimum of what we got and what we asked for for
3817 * the length. We can use the len variable here because it is
3818 * modified below and we could have been there before coming
3819 * here if the first part of the allocation didn't overlap what
3820 * was asked for.
3821 */
3822 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3823 got->br_blockcount - (*bno - got->br_startoff));
3824 mval->br_state = got->br_state;
3825 ASSERT(mval->br_blockcount <= len);
3826 return;
3827}
3828
3829/*
3830 * Update and validate the extent map to return
3831 */
3832STATIC void
3833xfs_bmapi_update_map(
3834 struct xfs_bmbt_irec **map,
3835 xfs_fileoff_t *bno,
3836 xfs_filblks_t *len,
3837 xfs_fileoff_t obno,
3838 xfs_fileoff_t end,
3839 int *n,
3840 int flags)
3841{
3842 xfs_bmbt_irec_t *mval = *map;
3843
3844 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3845 ((mval->br_startoff + mval->br_blockcount) <= end));
3846 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3847 (mval->br_startoff < obno));
3848
3849 *bno = mval->br_startoff + mval->br_blockcount;
3850 *len = end - *bno;
3851 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3852 /* update previous map with new information */
3853 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3854 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3855 ASSERT(mval->br_state == mval[-1].br_state);
3856 mval[-1].br_blockcount = mval->br_blockcount;
3857 mval[-1].br_state = mval->br_state;
3858 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3859 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3860 mval[-1].br_startblock != HOLESTARTBLOCK &&
3861 mval->br_startblock == mval[-1].br_startblock +
3862 mval[-1].br_blockcount &&
Christoph Hellwigc3a2f9f2018-07-11 22:26:01 -07003863 mval[-1].br_state == mval->br_state) {
Dave Chinneraef9a892011-09-18 20:40:44 +00003864 ASSERT(mval->br_startoff ==
3865 mval[-1].br_startoff + mval[-1].br_blockcount);
3866 mval[-1].br_blockcount += mval->br_blockcount;
3867 } else if (*n > 0 &&
3868 mval->br_startblock == DELAYSTARTBLOCK &&
3869 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3870 mval->br_startoff ==
3871 mval[-1].br_startoff + mval[-1].br_blockcount) {
3872 mval[-1].br_blockcount += mval->br_blockcount;
3873 mval[-1].br_state = mval->br_state;
3874 } else if (!((*n == 0) &&
3875 ((mval->br_startoff + mval->br_blockcount) <=
3876 obno))) {
3877 mval++;
3878 (*n)++;
3879 }
3880 *map = mval;
3881}
3882
3883/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003884 * Map file blocks to filesystem blocks without allocation.
3885 */
3886int
3887xfs_bmapi_read(
3888 struct xfs_inode *ip,
3889 xfs_fileoff_t bno,
3890 xfs_filblks_t len,
3891 struct xfs_bmbt_irec *mval,
3892 int *nmap,
3893 int flags)
3894{
3895 struct xfs_mount *mp = ip->i_mount;
3896 struct xfs_ifork *ifp;
3897 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003898 xfs_fileoff_t obno;
3899 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003900 struct xfs_iext_cursor icur;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003901 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003902 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003903 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003904 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003905
3906 ASSERT(*nmap >= 1);
3907 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Christoph Hellwigc3a2f9f2018-07-11 22:26:01 -07003908 XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003909 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003910
Darrick J. Wonga71895c2019-11-11 12:53:22 -08003911 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ip, whichfork)) ||
3912 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Dave Chinner24513372014-06-25 14:58:08 +10003913 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003914 }
3915
3916 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003917 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003918
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003919 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003920
3921 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong8612de32019-08-11 15:52:27 -07003922 if (!ifp) {
3923 /* No CoW fork? Return a hole. */
3924 if (whichfork == XFS_COW_FORK) {
3925 mval->br_startoff = bno;
3926 mval->br_startblock = HOLESTARTBLOCK;
3927 mval->br_blockcount = len;
3928 mval->br_state = XFS_EXT_NORM;
3929 *nmap = 1;
3930 return 0;
3931 }
Dave Chinner5c8ed202011-09-18 20:40:45 +00003932
Darrick J. Wong8612de32019-08-11 15:52:27 -07003933 /*
3934 * A missing attr ifork implies that the inode says we're in
3935 * extents or btree format but failed to pass the inode fork
3936 * verifier while trying to load it. Treat that as a file
3937 * corruption too.
3938 */
3939#ifdef DEBUG
3940 xfs_alert(mp, "%s: inode %llu missing fork %d",
3941 __func__, ip->i_ino, whichfork);
3942#endif /* DEBUG */
3943 return -EFSCORRUPTED;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003944 }
3945
Dave Chinner5c8ed202011-09-18 20:40:45 +00003946 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3947 error = xfs_iread_extents(NULL, ip, whichfork);
3948 if (error)
3949 return error;
3950 }
3951
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003952 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003953 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003954 end = bno + len;
3955 obno = bno;
3956
3957 while (bno < end && n < *nmap) {
3958 /* Reading past eof, act as though there's a hole up to end. */
3959 if (eof)
3960 got.br_startoff = end;
3961 if (got.br_startoff > bno) {
3962 /* Reading in a hole. */
3963 mval->br_startoff = bno;
3964 mval->br_startblock = HOLESTARTBLOCK;
3965 mval->br_blockcount =
3966 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3967 mval->br_state = XFS_EXT_NORM;
3968 bno += mval->br_blockcount;
3969 len -= mval->br_blockcount;
3970 mval++;
3971 n++;
3972 continue;
3973 }
3974
3975 /* set up the extent map to return. */
3976 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3977 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3978
3979 /* If we're done, stop now. */
3980 if (bno >= end || n >= *nmap)
3981 break;
3982
3983 /* Else go on to the next record. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003984 if (!xfs_iext_next_extent(ifp, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003985 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003986 }
3987 *nmap = n;
3988 return 0;
3989}
3990
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003991/*
3992 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3993 * global pool and the extent inserted into the inode in-core extent tree.
3994 *
3995 * On entry, got refers to the first extent beyond the offset of the extent to
3996 * allocate or eof is specified if no such extent exists. On return, got refers
3997 * to the extent record that was inserted to the inode fork.
3998 *
3999 * Note that the allocated extent may have been merged with contiguous extents
4000 * during insertion into the inode fork. Thus, got does not reflect the current
4001 * state of the inode fork on return. If necessary, the caller can use lastx to
4002 * look up the updated record in the inode fork.
4003 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10004004int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004005xfs_bmapi_reserve_delalloc(
4006 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004007 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11004008 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004009 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11004010 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004011 struct xfs_bmbt_irec *got,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004012 struct xfs_iext_cursor *icur,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004013 int eof)
4014{
4015 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004016 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004017 xfs_extlen_t alen;
4018 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004019 int error;
Brian Foster974ae922016-11-28 14:57:42 +11004020 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004021
Brian Foster974ae922016-11-28 14:57:42 +11004022 /*
4023 * Cap the alloc length. Keep track of prealloc so we know whether to
4024 * tag the inode before we return.
4025 */
4026 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004027 if (!eof)
4028 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11004029 if (prealloc && alen >= len)
4030 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004031
4032 /* Figure out the extent size, adjust alen */
Shan Hai6ca30722018-01-23 13:56:11 -08004033 if (whichfork == XFS_COW_FORK) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004034 struct xfs_bmbt_irec prev;
Shan Hai6ca30722018-01-23 13:56:11 -08004035 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004036
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004037 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004038 prev.br_startoff = NULLFILEOFF;
4039
Shan Hai6ca30722018-01-23 13:56:11 -08004040 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004041 1, 0, &aoff, &alen);
4042 ASSERT(!error);
4043 }
4044
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004045 /*
4046 * Make a transaction-less quota reservation for delayed allocation
4047 * blocks. This number gets adjusted later. We return if we haven't
4048 * allocated blocks already inside this loop.
4049 */
4050 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
Shan Hai6ca30722018-01-23 13:56:11 -08004051 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004052 if (error)
4053 return error;
4054
4055 /*
4056 * Split changing sb for alen and indlen since they could be coming
4057 * from different places.
4058 */
4059 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4060 ASSERT(indlen > 0);
4061
Shan Hai6ca30722018-01-23 13:56:11 -08004062 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004063 if (error)
4064 goto out_unreserve_quota;
4065
Dave Chinner0d485ad2015-02-23 21:22:03 +11004066 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004067 if (error)
4068 goto out_unreserve_blocks;
4069
4070
4071 ip->i_delayed_blks += alen;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07004072 xfs_mod_delalloc(ip->i_mount, alen + indlen);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004073
4074 got->br_startoff = aoff;
4075 got->br_startblock = nullstartblock(indlen);
4076 got->br_blockcount = alen;
4077 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004078
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004079 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004080
Brian Foster974ae922016-11-28 14:57:42 +11004081 /*
4082 * Tag the inode if blocks were preallocated. Note that COW fork
4083 * preallocation can occur at the start or end of the extent, even when
4084 * prealloc == 0, so we must also check the aligned offset and length.
4085 */
4086 if (whichfork == XFS_DATA_FORK && prealloc)
4087 xfs_inode_set_eofblocks_tag(ip);
4088 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4089 xfs_inode_set_cowblocks_tag(ip);
4090
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004091 return 0;
4092
4093out_unreserve_blocks:
Shan Hai6ca30722018-01-23 13:56:11 -08004094 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004095out_unreserve_quota:
4096 if (XFS_IS_QUOTA_ON(mp))
Shan Hai6ca30722018-01-23 13:56:11 -08004097 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0,
4098 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004099 return error;
4100}
4101
Dave Chinnercf11da92014-07-15 07:08:24 +10004102static int
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004103xfs_bmap_alloc_userdata(
4104 struct xfs_bmalloca *bma)
4105{
4106 struct xfs_mount *mp = bma->ip->i_mount;
4107 int whichfork = xfs_bmapi_whichfork(bma->flags);
4108 int error;
4109
4110 /*
4111 * Set the data type being allocated. For the data fork, the first data
4112 * in the file is treated differently to all other allocations. For the
4113 * attribute fork, we only need to ensure the allocated range is not on
4114 * the busy list.
4115 */
4116 bma->datatype = XFS_ALLOC_NOBUSY;
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004117 if (whichfork == XFS_DATA_FORK) {
Christoph Hellwigc34d5702019-10-30 12:25:00 -07004118 bma->datatype |= XFS_ALLOC_USERDATA;
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004119 if (bma->offset == 0)
4120 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004121
4122 if (mp->m_dalign && bma->length >= mp->m_dalign) {
4123 error = xfs_bmap_isaeof(bma, whichfork);
4124 if (error)
4125 return error;
4126 }
4127
4128 if (XFS_IS_REALTIME_INODE(bma->ip))
4129 return xfs_bmap_rtalloc(bma);
4130 }
4131
4132 return xfs_bmap_btalloc(bma);
4133}
4134
4135static int
Dave Chinnercf11da92014-07-15 07:08:24 +10004136xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004137 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004138{
4139 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004140 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004141 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004142 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004143 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004144
Dave Chinnera99ebf42011-12-01 11:24:20 +00004145 ASSERT(bma->length > 0);
4146
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004147 /*
4148 * For the wasdelay case, we could also just allocate the stuff asked
4149 * for in this bmap call but that wouldn't be as good.
4150 */
4151 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004152 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4153 bma->offset = bma->got.br_startoff;
Darrick J. Wongf5be0842019-11-06 08:53:54 -08004154 if (!xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev))
4155 bma->prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004156 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004157 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004158 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004159 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004160 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004161 }
4162
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004163 if (bma->flags & XFS_BMAPI_CONTIG)
4164 bma->minlen = bma->length;
4165 else
4166 bma->minlen = 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004167
Christoph Hellwigbe6cacb2019-10-30 12:24:59 -07004168 if (bma->flags & XFS_BMAPI_METADATA)
4169 error = xfs_bmap_btalloc(bma);
4170 else
4171 error = xfs_bmap_alloc_userdata(bma);
4172 if (error || bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004173 return error;
4174
Christoph Hellwigfd638f12019-10-30 12:25:00 -07004175 if (bma->flags & XFS_BMAPI_ZERO) {
4176 error = xfs_zero_extent(bma->ip, bma->blkno, bma->length);
4177 if (error)
4178 return error;
4179 }
4180
Brian Fostercf612de2018-07-11 22:26:29 -07004181 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur)
Dave Chinner29c8d172011-09-18 20:41:00 +00004182 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004183 /*
4184 * Bump the number of extents we've allocated
4185 * in this call.
4186 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004187 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004188
Dave Chinner29c8d172011-09-18 20:41:00 +00004189 if (bma->cur)
Dave Chinner92219c22020-03-10 17:52:53 -07004190 bma->cur->bc_ino.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004191 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4192
Dave Chinner963c30c2011-09-18 20:40:59 +00004193 bma->got.br_startoff = bma->offset;
4194 bma->got.br_startblock = bma->blkno;
4195 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004196 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004197
4198 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004199 * In the data fork, a wasdelay extent has been initialized, so
4200 * shouldn't be flagged as unwritten.
4201 *
4202 * For the cow fork, however, we convert delalloc reservations
4203 * (extents allocated for speculative preallocation) to
4204 * allocated unwritten extents, and only convert the unwritten
4205 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004206 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004207 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11004208 (bma->flags & XFS_BMAPI_PREALLOC))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004209 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004210
Christoph Hellwigc6534242011-09-18 20:41:05 +00004211 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004212 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004213 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004214 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004215 whichfork, &bma->icur, &bma->cur, &bma->got,
Brian Foster92f9da32018-07-11 22:26:28 -07004216 &bma->logflags, bma->flags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004217
Christoph Hellwigc315c902011-09-18 20:41:02 +00004218 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004219 if (error)
4220 return error;
4221
4222 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004223 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4224 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4225 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004226 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004227 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004228
Dave Chinner963c30c2011-09-18 20:40:59 +00004229 ASSERT(bma->got.br_startoff <= bma->offset);
4230 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4231 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004232 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4233 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004234 return 0;
4235}
4236
Dave Chinnerb447fe52011-09-18 20:40:51 +00004237STATIC int
4238xfs_bmapi_convert_unwritten(
4239 struct xfs_bmalloca *bma,
4240 struct xfs_bmbt_irec *mval,
4241 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004242 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004243{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004244 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004245 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004246 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004247 int error;
4248
Dave Chinnerb447fe52011-09-18 20:40:51 +00004249 /* check if we need to do unwritten->real conversion */
4250 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4251 (flags & XFS_BMAPI_PREALLOC))
4252 return 0;
4253
4254 /* check if we need to do real->unwritten conversion */
4255 if (mval->br_state == XFS_EXT_NORM &&
4256 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4257 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4258 return 0;
4259
4260 /*
4261 * Modify (by adding) the state flag, if writing.
4262 */
4263 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004264 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4265 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004266 bma->ip, whichfork);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004267 }
4268 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4269 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4270
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004271 /*
4272 * Before insertion into the bmbt, zero the range being converted
4273 * if required.
4274 */
4275 if (flags & XFS_BMAPI_ZERO) {
4276 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4277 mval->br_blockcount);
4278 if (error)
4279 return error;
4280 }
4281
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004282 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
Brian Foster92f9da32018-07-11 22:26:28 -07004283 &bma->icur, &bma->cur, mval, &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004284 /*
4285 * Log the inode core unconditionally in the unwritten extent conversion
4286 * path because the conversion might not have done so (e.g., if the
4287 * extent count hasn't changed). We need to make sure the inode is dirty
4288 * in the transaction for the sake of fsync(), even if nothing has
4289 * changed, because fsync() will not force the log for this transaction
4290 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004291 *
4292 * Note: If we're only converting cow fork extents, there aren't
4293 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004294 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004295 if (whichfork != XFS_COW_FORK)
4296 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004297 if (error)
4298 return error;
4299
4300 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004301 * Update our extent pointer, given that
4302 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4303 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004304 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004305 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004306
4307 /*
4308 * We may have combined previously unwritten space with written space,
4309 * so generate another request.
4310 */
4311 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004312 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004313 return 0;
4314}
4315
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004316static inline xfs_extlen_t
4317xfs_bmapi_minleft(
4318 struct xfs_trans *tp,
4319 struct xfs_inode *ip,
4320 int fork)
4321{
4322 if (tp && tp->t_firstblock != NULLFSBLOCK)
4323 return 0;
4324 if (XFS_IFORK_FORMAT(ip, fork) != XFS_DINODE_FMT_BTREE)
4325 return 1;
4326 return be16_to_cpu(XFS_IFORK_PTR(ip, fork)->if_broot->bb_level) + 1;
4327}
4328
4329/*
4330 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4331 * a case where the data is changed, there's an error, and it's not logged so we
4332 * don't shutdown when we should. Don't bother logging extents/btree changes if
4333 * we converted to the other format.
4334 */
4335static void
4336xfs_bmapi_finish(
4337 struct xfs_bmalloca *bma,
4338 int whichfork,
4339 int error)
4340{
4341 if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
4342 XFS_IFORK_FORMAT(bma->ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4343 bma->logflags &= ~xfs_ilog_fext(whichfork);
4344 else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
4345 XFS_IFORK_FORMAT(bma->ip, whichfork) != XFS_DINODE_FMT_BTREE)
4346 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4347
4348 if (bma->logflags)
4349 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4350 if (bma->cur)
4351 xfs_btree_del_cursor(bma->cur, error);
4352}
4353
Christoph Hellwig44032802011-09-18 20:40:48 +00004354/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004355 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4356 * extent state if necessary. Details behaviour is controlled by the flags
4357 * parameter. Only allocates blocks from a single allocation group, to avoid
4358 * locking problems.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004360int
4361xfs_bmapi_write(
4362 struct xfs_trans *tp, /* transaction pointer */
4363 struct xfs_inode *ip, /* incore inode */
4364 xfs_fileoff_t bno, /* starting file offs. mapped */
4365 xfs_filblks_t len, /* length to map in file */
4366 int flags, /* XFS_BMAPI_... */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004367 xfs_extlen_t total, /* total blocks needed */
4368 struct xfs_bmbt_irec *mval, /* output: map values */
Brian Foster6e702a52018-07-11 22:26:12 -07004369 int *nmap) /* i/o: mval size/count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370{
Darrick J. Wong4b0bce32019-03-19 08:16:22 -07004371 struct xfs_bmalloca bma = {
4372 .tp = tp,
4373 .ip = ip,
4374 .total = total,
4375 };
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004376 struct xfs_mount *mp = ip->i_mount;
4377 struct xfs_ifork *ifp;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004378 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004379 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004380 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004381 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004382 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004383 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004384
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004386 xfs_fileoff_t orig_bno; /* original block number value */
4387 int orig_flags; /* original flags arg value */
4388 xfs_filblks_t orig_len; /* original value of len arg */
4389 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4390 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391
4392 orig_bno = bno;
4393 orig_len = len;
4394 orig_flags = flags;
4395 orig_mval = mval;
4396 orig_nmap = *nmap;
4397#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004398 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004399
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004401 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
Christoph Hellwig26b91c72019-02-18 09:38:48 -08004402 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004403 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004404 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004405 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004406 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004407
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004408 /* zeroing is for currently only for data extents, not metadata */
4409 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4410 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4411 /*
4412 * we can allocate unwritten extents or pre-zero allocated blocks,
4413 * but it makes no sense to do both at once. This would result in
4414 * zeroing the unwritten extent twice, but it still being an
4415 * unwritten extent....
4416 */
4417 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4418 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4419
Darrick J. Wonga71895c2019-11-11 12:53:22 -08004420 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ip, whichfork)) ||
4421 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Dave Chinner24513372014-06-25 14:58:08 +10004422 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004424
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004426 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004427
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004429
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004430 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004431
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004432 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4433 error = xfs_iread_extents(tp, ip, whichfork);
4434 if (error)
4435 goto error0;
4436 }
4437
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004438 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004439 eof = true;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004440 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004441 bma.prev.br_startoff = NULLFILEOFF;
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004442 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004443
Brian Foster627209f2019-02-01 09:14:23 -08004444 n = 0;
4445 end = bno + len;
4446 obno = bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004448 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004449
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004450 /* in hole or beyond EOF? */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004451 if (eof || bma.got.br_startoff > bno) {
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004452 /*
4453 * CoW fork conversions should /never/ hit EOF or
4454 * holes. There should always be something for us
4455 * to work on.
4456 */
4457 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4458 (flags & XFS_BMAPI_COWFORK)));
4459
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004460 need_alloc = true;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004461 } else if (isnullstartblock(bma.got.br_startblock)) {
4462 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004463 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004464
4465 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 * First, deal with the hole before the allocated space
4467 * that we found, if any.
4468 */
Christoph Hellwig26b91c72019-02-18 09:38:48 -08004469 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004470 bma.eof = eof;
4471 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4472 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004473 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004474 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004475
Dave Chinnera99ebf42011-12-01 11:24:20 +00004476 /*
4477 * There's a 32/64 bit type mismatch between the
4478 * allocation length request (which can be 64 bits in
4479 * length) and the bma length request, which is
4480 * xfs_extlen_t and therefore 32 bits. Hence we have to
4481 * check for 32-bit overflows and handle them here.
4482 */
4483 if (len > (xfs_filblks_t)MAXEXTLEN)
4484 bma.length = MAXEXTLEN;
4485 else
4486 bma.length = len;
4487
4488 ASSERT(len > 0);
4489 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004490 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 if (error)
4492 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004493 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004494 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004495
4496 /*
4497 * If this is a CoW allocation, record the data in
4498 * the refcount btree for orphan recovery.
4499 */
Darrick J. Wong74b4c5d2019-08-26 17:06:04 -07004500 if (whichfork == XFS_COW_FORK)
4501 xfs_refcount_alloc_cow_extent(tp, bma.blkno,
4502 bma.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004504
Dave Chinneraef9a892011-09-18 20:40:44 +00004505 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004506 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4507 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
Dave Chinnerb447fe52011-09-18 20:40:51 +00004509 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004510 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004511 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004512 continue;
4513 if (error)
4514 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
Dave Chinneraef9a892011-09-18 20:40:44 +00004516 /* update the extent map to return */
4517 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4518
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 /*
4520 * If we're done, stop now. Stop when we've allocated
4521 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4522 * the transaction may get too big.
4523 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004524 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004526
4527 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004528 bma.prev = bma.got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004529 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004530 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004533
Christoph Hellwigb101e332019-02-15 08:02:47 -08004534 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4535 whichfork);
4536 if (error)
4537 goto error0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004538
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004540 XFS_IFORK_NEXTENTS(ip, whichfork) >
4541 XFS_IFORK_MAXEXT(ip, whichfork));
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004542 xfs_bmapi_finish(&bma, whichfork, 0);
4543 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4544 orig_nmap, *nmap);
4545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546error0:
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004547 xfs_bmapi_finish(&bma, whichfork, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 return error;
4549}
4550
Brian Foster627209f2019-02-01 09:14:23 -08004551/*
4552 * Convert an existing delalloc extent to real blocks based on file offset. This
4553 * attempts to allocate the entire delalloc extent and may require multiple
4554 * invocations to allocate the target offset if a large enough physical extent
4555 * is not available.
4556 */
4557int
4558xfs_bmapi_convert_delalloc(
Brian Foster627209f2019-02-01 09:14:23 -08004559 struct xfs_inode *ip,
Brian Foster627209f2019-02-01 09:14:23 -08004560 int whichfork,
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004561 xfs_off_t offset,
4562 struct iomap *iomap,
Christoph Hellwig491ce612019-02-15 08:02:49 -08004563 unsigned int *seq)
Brian Foster627209f2019-02-01 09:14:23 -08004564{
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004565 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004566 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004567 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004568 struct xfs_bmalloca bma = { NULL };
Darrick J. Wongaf952ae2019-12-16 11:14:09 -08004569 uint16_t flags = 0;
Christoph Hellwig491ce612019-02-15 08:02:49 -08004570 struct xfs_trans *tp;
Brian Foster627209f2019-02-01 09:14:23 -08004571 int error;
Brian Foster627209f2019-02-01 09:14:23 -08004572
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004573 if (whichfork == XFS_COW_FORK)
4574 flags |= IOMAP_F_SHARED;
4575
Christoph Hellwig491ce612019-02-15 08:02:49 -08004576 /*
4577 * Space for the extent and indirect blocks was reserved when the
4578 * delalloc extent was created so there's no need to do so here.
4579 */
4580 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4581 XFS_TRANS_RESERVE, &tp);
4582 if (error)
4583 return error;
4584
4585 xfs_ilock(ip, XFS_ILOCK_EXCL);
4586 xfs_trans_ijoin(tp, ip, 0);
4587
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004588 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4589 bma.got.br_startoff > offset_fsb) {
4590 /*
4591 * No extent found in the range we are trying to convert. This
4592 * should only happen for the COW fork, where another thread
4593 * might have moved the extent to the data fork in the meantime.
4594 */
4595 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004596 error = -EAGAIN;
4597 goto out_trans_cancel;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004598 }
Brian Foster627209f2019-02-01 09:14:23 -08004599
4600 /*
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004601 * If we find a real extent here we raced with another thread converting
4602 * the extent. Just return the real extent at this offset.
Brian Foster627209f2019-02-01 09:14:23 -08004603 */
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004604 if (!isnullstartblock(bma.got.br_startblock)) {
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004605 xfs_bmbt_to_iomap(ip, iomap, &bma.got, flags);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004606 *seq = READ_ONCE(ifp->if_seq);
4607 goto out_trans_cancel;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004608 }
4609
4610 bma.tp = tp;
4611 bma.ip = ip;
4612 bma.wasdel = true;
4613 bma.offset = bma.got.br_startoff;
4614 bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount, MAXEXTLEN);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004615 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4616 if (whichfork == XFS_COW_FORK)
4617 bma.flags = XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC;
4618
4619 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4620 bma.prev.br_startoff = NULLFILEOFF;
4621
4622 error = xfs_bmapi_allocate(&bma);
4623 if (error)
4624 goto out_finish;
4625
4626 error = -ENOSPC;
4627 if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK))
4628 goto out_finish;
4629 error = -EFSCORRUPTED;
Christoph Hellwigeb77b232019-09-03 08:13:13 -07004630 if (WARN_ON_ONCE(!xfs_valid_startblock(ip, bma.got.br_startblock)))
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004631 goto out_finish;
4632
Christoph Hellwig125851a2019-02-15 08:02:49 -08004633 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4634 XFS_STATS_INC(mp, xs_xstrat_quick);
4635
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004636 ASSERT(!isnullstartblock(bma.got.br_startblock));
Christoph Hellwig4e087a32019-10-17 13:12:06 -07004637 xfs_bmbt_to_iomap(ip, iomap, &bma.got, flags);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004638 *seq = READ_ONCE(ifp->if_seq);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004639
Darrick J. Wong74b4c5d2019-08-26 17:06:04 -07004640 if (whichfork == XFS_COW_FORK)
4641 xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004642
4643 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4644 whichfork);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004645 if (error)
4646 goto out_finish;
4647
4648 xfs_bmapi_finish(&bma, whichfork, 0);
4649 error = xfs_trans_commit(tp);
4650 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4651 return error;
4652
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004653out_finish:
4654 xfs_bmapi_finish(&bma, whichfork, error);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004655out_trans_cancel:
4656 xfs_trans_cancel(tp);
4657 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Brian Foster627209f2019-02-01 09:14:23 -08004658 return error;
4659}
4660
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004661int
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004662xfs_bmapi_remap(
4663 struct xfs_trans *tp,
4664 struct xfs_inode *ip,
4665 xfs_fileoff_t bno,
4666 xfs_filblks_t len,
4667 xfs_fsblock_t startblock,
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004668 int flags)
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004669{
4670 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004671 struct xfs_ifork *ifp;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004672 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004673 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004674 struct xfs_iext_cursor icur;
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004675 int whichfork = xfs_bmapi_whichfork(flags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004676 int logflags = 0, error;
4677
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004678 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004679 ASSERT(len > 0);
4680 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4681 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Darrick J. Wong7644bd92018-05-14 06:34:35 -07004682 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4683 XFS_BMAPI_NORMAP)));
4684 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4685 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004686
Darrick J. Wonga71895c2019-11-11 12:53:22 -08004687 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ip, whichfork)) ||
4688 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004689 return -EFSCORRUPTED;
4690 }
4691
4692 if (XFS_FORCED_SHUTDOWN(mp))
4693 return -EIO;
4694
4695 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004696 error = xfs_iread_extents(tp, ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004697 if (error)
4698 return error;
4699 }
4700
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004701 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004702 /* make sure we only reflink into a hole. */
4703 ASSERT(got.br_startoff > bno);
4704 ASSERT(got.br_startoff - bno >= len);
4705 }
4706
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004707 ip->i_d.di_nblocks += len;
4708 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004709
4710 if (ifp->if_flags & XFS_IFBROOT) {
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004711 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner92219c22020-03-10 17:52:53 -07004712 cur->bc_ino.flags = 0;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004713 }
4714
4715 got.br_startoff = bno;
4716 got.br_startblock = startblock;
4717 got.br_blockcount = len;
Darrick J. Wong7644bd92018-05-14 06:34:35 -07004718 if (flags & XFS_BMAPI_PREALLOC)
4719 got.br_state = XFS_EXT_UNWRITTEN;
4720 else
4721 got.br_state = XFS_EXT_NORM;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004722
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004723 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
Brian Foster92f9da32018-07-11 22:26:28 -07004724 &cur, &got, &logflags, flags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004725 if (error)
4726 goto error0;
4727
Christoph Hellwigb101e332019-02-15 08:02:47 -08004728 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004729
4730error0:
4731 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4732 logflags &= ~XFS_ILOG_DEXT;
4733 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4734 logflags &= ~XFS_ILOG_DBROOT;
4735
4736 if (logflags)
4737 xfs_trans_log_inode(tp, ip, logflags);
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07004738 if (cur)
4739 xfs_btree_del_cursor(cur, error);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004740 return error;
4741}
4742
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004744 * When a delalloc extent is split (e.g., due to a hole punch), the original
4745 * indlen reservation must be shared across the two new extents that are left
4746 * behind.
4747 *
4748 * Given the original reservation and the worst case indlen for the two new
4749 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004750 * reservation fairly across the two new extents. If necessary, steal available
4751 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4752 * ores == 1). The number of stolen blocks is returned. The availability and
4753 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004754 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004755static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004756xfs_bmap_split_indlen(
4757 xfs_filblks_t ores, /* original res. */
4758 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004759 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4760 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004761{
4762 xfs_filblks_t len1 = *indlen1;
4763 xfs_filblks_t len2 = *indlen2;
4764 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004765 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004766 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004767
4768 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004769 * Steal as many blocks as we can to try and satisfy the worst case
4770 * indlen for both new extents.
4771 */
Brian Foster75d65362017-02-13 22:48:30 -08004772 if (ores < nres && avail)
4773 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4774 ores += stolen;
4775
4776 /* nothing else to do if we've satisfied the new reservation */
4777 if (ores >= nres)
4778 return stolen;
Brian Fosterd34999c2016-03-15 11:42:47 +11004779
4780 /*
Brian Foster75d65362017-02-13 22:48:30 -08004781 * We can't meet the total required reservation for the two extents.
4782 * Calculate the percent of the overall shortage between both extents
4783 * and apply this percentage to each of the requested indlen values.
4784 * This distributes the shortage fairly and reduces the chances that one
4785 * of the two extents is left with nothing when extents are repeatedly
4786 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004787 */
Brian Foster75d65362017-02-13 22:48:30 -08004788 resfactor = (ores * 100);
4789 do_div(resfactor, nres);
4790 len1 *= resfactor;
4791 do_div(len1, 100);
4792 len2 *= resfactor;
4793 do_div(len2, 100);
4794 ASSERT(len1 + len2 <= ores);
4795 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4796
4797 /*
4798 * Hand out the remainder to each extent. If one of the two reservations
4799 * is zero, we want to make sure that one gets a block first. The loop
4800 * below starts with len1, so hand len2 a block right off the bat if it
4801 * is zero.
4802 */
4803 ores -= (len1 + len2);
4804 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4805 if (ores && !len2 && *indlen2) {
4806 len2++;
4807 ores--;
4808 }
4809 while (ores) {
4810 if (len1 < *indlen1) {
4811 len1++;
4812 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004813 }
Brian Foster75d65362017-02-13 22:48:30 -08004814 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004815 break;
Brian Foster75d65362017-02-13 22:48:30 -08004816 if (len2 < *indlen2) {
4817 len2++;
4818 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004819 }
4820 }
4821
4822 *indlen1 = len1;
4823 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004824
4825 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004826}
4827
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004828int
4829xfs_bmap_del_extent_delay(
4830 struct xfs_inode *ip,
4831 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004832 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004833 struct xfs_bmbt_irec *got,
4834 struct xfs_bmbt_irec *del)
4835{
4836 struct xfs_mount *mp = ip->i_mount;
4837 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4838 struct xfs_bmbt_irec new;
4839 int64_t da_old, da_new, da_diff = 0;
4840 xfs_fileoff_t del_endoff, got_endoff;
4841 xfs_filblks_t got_indlen, new_indlen, stolen;
Christoph Hellwig060ea652017-10-19 11:02:29 -07004842 int state = xfs_bmap_fork_to_state(whichfork);
4843 int error = 0;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004844 bool isrt;
4845
4846 XFS_STATS_INC(mp, xs_del_exlist);
4847
4848 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4849 del_endoff = del->br_startoff + del->br_blockcount;
4850 got_endoff = got->br_startoff + got->br_blockcount;
4851 da_old = startblockval(got->br_startblock);
4852 da_new = 0;
4853
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004854 ASSERT(del->br_blockcount > 0);
4855 ASSERT(got->br_startoff <= del->br_startoff);
4856 ASSERT(got_endoff >= del_endoff);
4857
4858 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004859 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004860
4861 do_div(rtexts, mp->m_sb.sb_rextsize);
4862 xfs_mod_frextents(mp, rtexts);
4863 }
4864
4865 /*
4866 * Update the inode delalloc counter now and wait to update the
4867 * sb counters as we might have to borrow some blocks for the
4868 * indirect block accounting.
4869 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004870 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4871 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004872 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004873 if (error)
4874 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004875 ip->i_delayed_blks -= del->br_blockcount;
4876
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004877 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004878 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004879 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004880 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004881
Christoph Hellwig0173c682017-10-17 14:16:22 -07004882 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4883 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004884 /*
4885 * Matches the whole extent. Delete the entry.
4886 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004887 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004888 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004889 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004890 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004891 /*
4892 * Deleting the first part of the extent.
4893 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004894 got->br_startoff = del_endoff;
4895 got->br_blockcount -= del->br_blockcount;
4896 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4897 got->br_blockcount), da_old);
4898 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004899 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004900 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004901 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004902 /*
4903 * Deleting the last part of the extent.
4904 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004905 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4906 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4907 got->br_blockcount), da_old);
4908 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004909 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004910 break;
4911 case 0:
4912 /*
4913 * Deleting the middle of the extent.
4914 *
4915 * Distribute the original indlen reservation across the two new
4916 * extents. Steal blocks from the deleted extent if necessary.
4917 * Stealing blocks simply fudges the fdblocks accounting below.
4918 * Warn if either of the new indlen reservations is zero as this
4919 * can lead to delalloc problems.
4920 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004921 got->br_blockcount = del->br_startoff - got->br_startoff;
4922 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4923
4924 new.br_blockcount = got_endoff - del_endoff;
4925 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4926
4927 WARN_ON_ONCE(!got_indlen || !new_indlen);
4928 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4929 del->br_blockcount);
4930
4931 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004932
4933 new.br_startoff = del_endoff;
4934 new.br_state = got->br_state;
4935 new.br_startblock = nullstartblock((int)new_indlen);
4936
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004937 xfs_iext_update_extent(ip, state, icur, got);
4938 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004939 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004940
4941 da_new = got_indlen + new_indlen - stolen;
4942 del->br_blockcount -= stolen;
4943 break;
4944 }
4945
4946 ASSERT(da_old >= da_new);
4947 da_diff = da_old - da_new;
4948 if (!isrt)
4949 da_diff += del->br_blockcount;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07004950 if (da_diff) {
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004951 xfs_mod_fdblocks(mp, da_diff, false);
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07004952 xfs_mod_delalloc(mp, -da_diff);
4953 }
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004954 return error;
4955}
4956
4957void
4958xfs_bmap_del_extent_cow(
4959 struct xfs_inode *ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004960 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004961 struct xfs_bmbt_irec *got,
4962 struct xfs_bmbt_irec *del)
4963{
4964 struct xfs_mount *mp = ip->i_mount;
4965 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4966 struct xfs_bmbt_irec new;
4967 xfs_fileoff_t del_endoff, got_endoff;
4968 int state = BMAP_COWFORK;
4969
4970 XFS_STATS_INC(mp, xs_del_exlist);
4971
4972 del_endoff = del->br_startoff + del->br_blockcount;
4973 got_endoff = got->br_startoff + got->br_blockcount;
4974
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004975 ASSERT(del->br_blockcount > 0);
4976 ASSERT(got->br_startoff <= del->br_startoff);
4977 ASSERT(got_endoff >= del_endoff);
4978 ASSERT(!isnullstartblock(got->br_startblock));
4979
4980 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004981 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004982 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004983 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004984
Christoph Hellwig0173c682017-10-17 14:16:22 -07004985 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4986 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004987 /*
4988 * Matches the whole extent. Delete the entry.
4989 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004990 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004991 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004992 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004993 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004994 /*
4995 * Deleting the first part of the extent.
4996 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004997 got->br_startoff = del_endoff;
4998 got->br_blockcount -= del->br_blockcount;
4999 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005000 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005001 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07005002 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005003 /*
5004 * Deleting the last part of the extent.
5005 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005006 got->br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005007 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005008 break;
5009 case 0:
5010 /*
5011 * Deleting the middle of the extent.
5012 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005013 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005014
5015 new.br_startoff = del_endoff;
5016 new.br_blockcount = got_endoff - del_endoff;
5017 new.br_state = got->br_state;
5018 new.br_startblock = del->br_startblock + del->br_blockcount;
5019
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005020 xfs_iext_update_extent(ip, state, icur, got);
5021 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005022 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005023 break;
5024 }
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08005025 ip->i_delayed_blks -= del->br_blockcount;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005026}
5027
Brian Fostera9bd24a2016-03-15 11:42:46 +11005028/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11005029 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07005030 * after removing space.
Dave Chinner9e5987a72013-02-25 12:31:26 +11005031 */
5032STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005033xfs_bmap_del_extent_real(
Dave Chinner9e5987a72013-02-25 12:31:26 +11005034 xfs_inode_t *ip, /* incore inode pointer */
5035 xfs_trans_t *tp, /* current transaction pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005036 struct xfs_iext_cursor *icur,
Dave Chinner9e5987a72013-02-25 12:31:26 +11005037 xfs_btree_cur_t *cur, /* if null, not a btree */
5038 xfs_bmbt_irec_t *del, /* data to remove from extents */
5039 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005040 int whichfork, /* data or attr fork */
5041 int bflags) /* bmapi flags */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005042{
Dave Chinner9e5987a72013-02-25 12:31:26 +11005043 xfs_fsblock_t del_endblock=0; /* first block past del */
5044 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005045 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005046 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07005047 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005048 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005049 xfs_fileoff_t got_endoff; /* first offset past got */
5050 int i; /* temp state */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07005051 struct xfs_ifork *ifp; /* inode fork pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005052 xfs_mount_t *mp; /* mount structure */
5053 xfs_filblks_t nblks; /* quota/sb block count */
5054 xfs_bmbt_irec_t new; /* new record to be inserted */
5055 /* REFERENCED */
5056 uint qfield; /* quota field to update */
Christoph Hellwig060ea652017-10-19 11:02:29 -07005057 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005058 struct xfs_bmbt_irec old;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005059
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005060 mp = ip->i_mount;
5061 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005062
Dave Chinner9e5987a72013-02-25 12:31:26 +11005063 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005064 ASSERT(del->br_blockcount > 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005065 xfs_iext_get_extent(ifp, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005066 ASSERT(got.br_startoff <= del->br_startoff);
5067 del_endoff = del->br_startoff + del->br_blockcount;
5068 got_endoff = got.br_startoff + got.br_blockcount;
5069 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005070 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a72013-02-25 12:31:26 +11005071 qfield = 0;
5072 error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005073
Christoph Hellwig1b24b632017-10-17 14:16:22 -07005074 /*
5075 * If it's the case where the directory code is running with no block
5076 * reservation, and the deleted block is in the middle of its extent,
5077 * and the resulting insert of an extent would cause transformation to
5078 * btree format, then reject it. The calling code will then swap blocks
5079 * around instead. We have to do this now, rather than waiting for the
5080 * conversion to btree format, since the transaction will be dirty then.
5081 */
5082 if (tp->t_blk_res == 0 &&
5083 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5084 XFS_IFORK_NEXTENTS(ip, whichfork) >=
5085 XFS_IFORK_MAXEXT(ip, whichfork) &&
5086 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
5087 return -ENOSPC;
5088
5089 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005090 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5091 xfs_fsblock_t bno;
5092 xfs_filblks_t len;
Dave Chinner0703a8e2018-06-08 09:54:22 -07005093 xfs_extlen_t mod;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005094
Dave Chinner0703a8e2018-06-08 09:54:22 -07005095 bno = div_u64_rem(del->br_startblock, mp->m_sb.sb_rextsize,
5096 &mod);
5097 ASSERT(mod == 0);
5098 len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize,
5099 &mod);
5100 ASSERT(mod == 0);
5101
Christoph Hellwige1d75532017-10-17 14:16:21 -07005102 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5103 if (error)
5104 goto done;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005105 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005106 nblks = len * mp->m_sb.sb_rextsize;
5107 qfield = XFS_TRANS_DQ_RTBCOUNT;
5108 } else {
5109 do_fx = 1;
5110 nblks = del->br_blockcount;
5111 qfield = XFS_TRANS_DQ_BCOUNT;
5112 }
5113
5114 del_endblock = del->br_startblock + del->br_blockcount;
5115 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005116 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005117 if (error)
5118 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005119 if (XFS_IS_CORRUPT(mp, i != 1)) {
5120 error = -EFSCORRUPTED;
5121 goto done;
5122 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11005123 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005124
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005125 if (got.br_startoff == del->br_startoff)
5126 state |= BMAP_LEFT_FILLING;
5127 if (got_endoff == del_endoff)
5128 state |= BMAP_RIGHT_FILLING;
5129
5130 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5131 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005132 /*
5133 * Matches the whole extent. Delete the entry.
5134 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005135 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005136 xfs_iext_prev(ifp, icur);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005137 XFS_IFORK_NEXT_SET(ip, whichfork,
5138 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5139 flags |= XFS_ILOG_CORE;
5140 if (!cur) {
5141 flags |= xfs_ilog_fext(whichfork);
5142 break;
5143 }
5144 if ((error = xfs_btree_delete(cur, &i)))
5145 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005146 if (XFS_IS_CORRUPT(mp, i != 1)) {
5147 error = -EFSCORRUPTED;
5148 goto done;
5149 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11005150 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005151 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005152 /*
5153 * Deleting the first part of the extent.
5154 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005155 got.br_startoff = del_endoff;
5156 got.br_startblock = del_endblock;
5157 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005158 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005159 if (!cur) {
5160 flags |= xfs_ilog_fext(whichfork);
5161 break;
5162 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005163 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005164 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005165 goto done;
5166 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005167 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005168 /*
5169 * Deleting the last part of the extent.
5170 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005171 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005172 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005173 if (!cur) {
5174 flags |= xfs_ilog_fext(whichfork);
5175 break;
5176 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005177 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005178 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005179 goto done;
5180 break;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005181 case 0:
5182 /*
5183 * Deleting the middle of the extent.
5184 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005185 old = got;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07005186
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005187 got.br_blockcount = del->br_startoff - got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005188 xfs_iext_update_extent(ip, state, icur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005189
Dave Chinner9e5987a72013-02-25 12:31:26 +11005190 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005191 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005192 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005193 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005194
Christoph Hellwige1d75532017-10-17 14:16:21 -07005195 flags |= XFS_ILOG_CORE;
5196 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005197 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005198 if (error)
5199 goto done;
5200 error = xfs_btree_increment(cur, 0, &i);
5201 if (error)
5202 goto done;
5203 cur->bc_rec.b = new;
5204 error = xfs_btree_insert(cur, &i);
5205 if (error && error != -ENOSPC)
5206 goto done;
5207 /*
5208 * If get no-space back from btree insert, it tried a
5209 * split, and we have a zero block reservation. Fix up
5210 * our state and return the error.
5211 */
5212 if (error == -ENOSPC) {
5213 /*
5214 * Reset the cursor, don't trust it after any
5215 * insert operation.
5216 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005217 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005218 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005219 goto done;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005220 if (XFS_IS_CORRUPT(mp, i != 1)) {
5221 error = -EFSCORRUPTED;
5222 goto done;
5223 }
Christoph Hellwige1d75532017-10-17 14:16:21 -07005224 /*
5225 * Update the btree record back
5226 * to the original value.
5227 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005228 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005229 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005230 goto done;
5231 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005232 * Reset the extent record back
5233 * to the original value.
Dave Chinner9e5987a72013-02-25 12:31:26 +11005234 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005235 xfs_iext_update_extent(ip, state, icur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005236 flags = 0;
5237 error = -ENOSPC;
5238 goto done;
5239 }
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005240 if (XFS_IS_CORRUPT(mp, i != 1)) {
5241 error = -EFSCORRUPTED;
5242 goto done;
5243 }
Christoph Hellwige1d75532017-10-17 14:16:21 -07005244 } else
5245 flags |= xfs_ilog_fext(whichfork);
5246 XFS_IFORK_NEXT_SET(ip, whichfork,
5247 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005248 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005249 xfs_iext_insert(ip, icur, &new, state);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005250 break;
5251 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005252
5253 /* remove reverse mapping */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07005254 xfs_rmap_unmap_extent(tp, ip, whichfork, del);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005255
Dave Chinner9e5987a72013-02-25 12:31:26 +11005256 /*
5257 * If we need to, add to list of extents to delete.
5258 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005259 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005260 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
Darrick J. Wong74b4c5d2019-08-26 17:06:04 -07005261 xfs_refcount_decrease_extent(tp, del);
Brian Fosterfcb762f2018-05-09 08:45:04 -07005262 } else {
Brian Foster0f37d172018-08-01 07:20:34 -07005263 __xfs_bmap_add_free(tp, del->br_startblock,
Brian Foster4e529332018-05-10 09:35:42 -07005264 del->br_blockcount, NULL,
5265 (bflags & XFS_BMAPI_NODISCARD) ||
5266 del->br_state == XFS_EXT_UNWRITTEN);
Brian Fosterfcb762f2018-05-09 08:45:04 -07005267 }
Darrick J. Wong62aab202016-10-03 09:11:23 -07005268 }
5269
Dave Chinner9e5987a72013-02-25 12:31:26 +11005270 /*
5271 * Adjust inode # blocks in the file.
5272 */
5273 if (nblks)
5274 ip->i_d.di_nblocks -= nblks;
5275 /*
5276 * Adjust quota data.
5277 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005278 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a72013-02-25 12:31:26 +11005279 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5280
Dave Chinner9e5987a72013-02-25 12:31:26 +11005281done:
5282 *logflagsp = flags;
5283 return error;
5284}
5285
5286/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 * Unmap (remove) blocks from a file.
5288 * If nexts is nonzero then the number of extents to remove is limited to
5289 * that value. If not all extents in the block range can be removed then
5290 * *done is set.
5291 */
5292int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005293__xfs_bunmapi(
Brian Fosterccd9d912018-07-11 22:26:13 -07005294 struct xfs_trans *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005296 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005297 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 int flags, /* misc flags */
Brian Foster2af52842018-07-11 22:26:25 -07005299 xfs_extnum_t nexts) /* number of extents max */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300{
Brian Fosterccd9d912018-07-11 22:26:13 -07005301 struct xfs_btree_cur *cur; /* bmap btree cursor */
5302 struct xfs_bmbt_irec del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 int error; /* error return value */
5304 xfs_extnum_t extno; /* extent number in list */
Brian Fosterccd9d912018-07-11 22:26:13 -07005305 struct xfs_bmbt_irec got; /* current extent record */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07005306 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 int isrt; /* freeing in rt area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 int logflags; /* transaction logging flags */
5309 xfs_extlen_t mod; /* rt extent offset */
Darrick J. Wonga71895c2019-11-11 12:53:22 -08005310 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 int tmp_logflags; /* partial logging flags */
5312 int wasdel; /* was a delayed alloc extent */
5313 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005315 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005316 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005317 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005318 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005319 struct xfs_iext_cursor icur;
5320 bool done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005322 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005323
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005324 whichfork = xfs_bmapi_whichfork(flags);
5325 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wonga71895c2019-11-11 12:53:22 -08005327 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ip, whichfork)))
Dave Chinner24513372014-06-25 14:58:08 +10005328 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005330 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005331
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005332 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 ASSERT(len > 0);
5334 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005335
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005336 /*
5337 * Guesstimate how many blocks we can unmap without running the risk of
5338 * blowing out the transaction with a mix of EFIs and reflink
5339 * adjustments.
5340 */
Darrick J. Wong8c57b882017-12-10 18:03:53 -08005341 if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005342 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5343 else
5344 max_len = len;
5345
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5347 (error = xfs_iread_extents(tp, ip, whichfork)))
5348 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005349 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005350 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 return 0;
5352 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005353 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005354 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwigdc560152017-10-23 16:32:39 -07005355 end = start + len;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005356
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005357 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
Christoph Hellwigdc560152017-10-23 16:32:39 -07005358 *rlen = 0;
5359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 }
Christoph Hellwigdc560152017-10-23 16:32:39 -07005361 end--;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005362
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 logflags = 0;
5364 if (ifp->if_flags & XFS_IFBROOT) {
5365 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005366 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner92219c22020-03-10 17:52:53 -07005367 cur->bc_ino.flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 } else
5369 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005370
5371 if (isrt) {
5372 /*
5373 * Synchronize by locking the bitmap inode.
5374 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005375 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005376 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005377 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5378 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005379 }
5380
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 extno = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005382 while (end != (xfs_fileoff_t)-1 && end >= start &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005383 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005385 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 * Just back up to the previous extent, if so.
5387 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005388 if (got.br_startoff > end &&
5389 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5390 done = true;
5391 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 }
5393 /*
5394 * Is the last block of this extent before the range
5395 * we're supposed to delete? If so, we're done.
5396 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005397 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005399 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 break;
5401 /*
5402 * Then deal with the (possibly delayed) allocated space
5403 * we found.
5404 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005406 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005407
5408 /*
5409 * Make sure we don't touch multiple AGF headers out of order
5410 * in a single transaction, as that could cause AB-BA deadlocks.
5411 */
Omar Sandoval69ffe592019-11-26 16:58:08 -08005412 if (!wasdel && !isrt) {
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005413 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5414 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5415 break;
5416 prev_agno = agno;
5417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 if (got.br_startoff < start) {
5419 del.br_startoff = start;
5420 del.br_blockcount -= start - got.br_startoff;
5421 if (!wasdel)
5422 del.br_startblock += start - got.br_startoff;
5423 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005424 if (del.br_startoff + del.br_blockcount > end + 1)
5425 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005426
5427 /* How much can we safely unmap? */
5428 if (max_len < del.br_blockcount) {
5429 del.br_startoff += del.br_blockcount - max_len;
5430 if (!wasdel)
5431 del.br_startblock += del.br_blockcount - max_len;
5432 del.br_blockcount = max_len;
5433 }
5434
Dave Chinner0703a8e2018-06-08 09:54:22 -07005435 if (!isrt)
5436 goto delete;
5437
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 sum = del.br_startblock + del.br_blockcount;
Dave Chinner0703a8e2018-06-08 09:54:22 -07005439 div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod);
5440 if (mod) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 /*
5442 * Realtime extent not lined up at the end.
5443 * The extent could have been split into written
5444 * and unwritten pieces, or we could just be
5445 * unmapping part of it. But we can't really
5446 * get rid of part of a realtime extent.
5447 */
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11005448 if (del.br_state == XFS_EXT_UNWRITTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 /*
5450 * This piece is unwritten, or we're not
5451 * using unwritten extents. Skip over it.
5452 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005453 ASSERT(end >= mod);
5454 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 del.br_blockcount : mod;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005456 if (end < got.br_startoff &&
5457 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5458 done = true;
5459 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 }
5461 continue;
5462 }
5463 /*
5464 * It's written, turn it unwritten.
5465 * This is better than zeroing it.
5466 */
5467 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005468 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 /*
5470 * If this spans a realtime extent boundary,
5471 * chop it back to the start of the one we end at.
5472 */
5473 if (del.br_blockcount > mod) {
5474 del.br_startoff += del.br_blockcount - mod;
5475 del.br_startblock += del.br_blockcount - mod;
5476 del.br_blockcount = mod;
5477 }
5478 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005479 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005480 whichfork, &icur, &cur, &del,
Brian Foster92f9da32018-07-11 22:26:28 -07005481 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 if (error)
5483 goto error0;
5484 goto nodelete;
5485 }
Dave Chinner0703a8e2018-06-08 09:54:22 -07005486 div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod);
5487 if (mod) {
Omar Sandoval0c4da702019-11-26 16:58:07 -08005488 xfs_extlen_t off = mp->m_sb.sb_rextsize - mod;
5489
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 /*
5491 * Realtime extent is lined up at the end but not
5492 * at the front. We'll get rid of full extents if
5493 * we can.
5494 */
Omar Sandoval0c4da702019-11-26 16:58:07 -08005495 if (del.br_blockcount > off) {
5496 del.br_blockcount -= off;
5497 del.br_startoff += off;
5498 del.br_startblock += off;
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11005499 } else if (del.br_startoff == start &&
5500 (del.br_state == XFS_EXT_UNWRITTEN ||
5501 tp->t_blk_res == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 /*
5503 * Can't make it unwritten. There isn't
5504 * a full extent here so just skip it.
5505 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005506 ASSERT(end >= del.br_blockcount);
5507 end -= del.br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005508 if (got.br_startoff > end &&
5509 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5510 done = true;
5511 break;
5512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 continue;
5514 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005515 struct xfs_bmbt_irec prev;
Omar Sandoval0c4da702019-11-26 16:58:07 -08005516 xfs_fileoff_t unwrite_start;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005517
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 /*
5519 * This one is already unwritten.
5520 * It must have a written left neighbor.
5521 * Unwrite the killed part of that one and
5522 * try again.
5523 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005524 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5525 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005527 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 ASSERT(del.br_startblock ==
5529 prev.br_startblock + prev.br_blockcount);
Omar Sandoval0c4da702019-11-26 16:58:07 -08005530 unwrite_start = max3(start,
5531 del.br_startoff - mod,
5532 prev.br_startoff);
5533 mod = unwrite_start - prev.br_startoff;
5534 prev.br_startoff = unwrite_start;
5535 prev.br_startblock += mod;
5536 prev.br_blockcount -= mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005538 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005539 ip, whichfork, &icur, &cur,
Brian Foster92f9da32018-07-11 22:26:28 -07005540 &prev, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 if (error)
5542 goto error0;
5543 goto nodelete;
5544 } else {
5545 ASSERT(del.br_state == XFS_EXT_NORM);
5546 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005547 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005548 ip, whichfork, &icur, &cur,
Brian Foster92f9da32018-07-11 22:26:28 -07005549 &del, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 if (error)
5551 goto error0;
5552 goto nodelete;
5553 }
5554 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005555
Dave Chinner0703a8e2018-06-08 09:54:22 -07005556delete:
Brian Fosterb2706a02016-03-15 11:42:46 +11005557 if (wasdel) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005558 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005559 &got, &del);
5560 } else {
Brian Foster81ba8f32018-07-11 22:26:16 -07005561 error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5562 &del, &tmp_logflags, whichfork,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005563 flags);
5564 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005565 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005566
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 if (error)
5568 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005569
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005570 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005571 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 /*
5574 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005576 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005577 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5578 (got.br_startoff > end &&
5579 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5580 done = true;
5581 break;
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 extno++;
5584 }
5585 }
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005586 if (done || end == (xfs_fileoff_t)-1 || end < start)
Darrick J. Wong44535932016-10-03 09:11:29 -07005587 *rlen = 0;
5588 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005589 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005590
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 /*
5592 * Convert to a btree if necessary.
5593 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005594 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07005596 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5597 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 logflags |= tmp_logflags;
Christoph Hellwigb101e332019-02-15 08:02:47 -08005599 } else {
5600 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 }
Christoph Hellwigb101e332019-02-15 08:02:47 -08005603
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604error0:
5605 /*
5606 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005607 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005609 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005611 logflags &= ~xfs_ilog_fext(whichfork);
5612 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005614 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 /*
5616 * Log inode even in the error case, if the transaction
5617 * is dirty we'll need to shut down the filesystem.
5618 */
5619 if (logflags)
5620 xfs_trans_log_inode(tp, ip, logflags);
5621 if (cur) {
Brian Fostercf612de2018-07-11 22:26:29 -07005622 if (!error)
Dave Chinner92219c22020-03-10 17:52:53 -07005623 cur->bc_ino.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07005624 xfs_btree_del_cursor(cur, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 }
5626 return error;
5627}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005628
Darrick J. Wong44535932016-10-03 09:11:29 -07005629/* Unmap a range of a file. */
5630int
5631xfs_bunmapi(
5632 xfs_trans_t *tp,
5633 struct xfs_inode *ip,
5634 xfs_fileoff_t bno,
5635 xfs_filblks_t len,
5636 int flags,
5637 xfs_extnum_t nexts,
Darrick J. Wong44535932016-10-03 09:11:29 -07005638 int *done)
5639{
5640 int error;
5641
Brian Foster2af52842018-07-11 22:26:25 -07005642 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
Darrick J. Wong44535932016-10-03 09:11:29 -07005643 *done = (len == 0);
5644 return error;
5645}
5646
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005647/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005648 * Determine whether an extent shift can be accomplished by a merge with the
5649 * extent that precedes the target hole of the shift.
5650 */
5651STATIC bool
5652xfs_bmse_can_merge(
5653 struct xfs_bmbt_irec *left, /* preceding extent */
5654 struct xfs_bmbt_irec *got, /* current extent to shift */
5655 xfs_fileoff_t shift) /* shift fsb */
5656{
5657 xfs_fileoff_t startoff;
5658
5659 startoff = got->br_startoff - shift;
5660
5661 /*
5662 * The extent, once shifted, must be adjacent in-file and on-disk with
5663 * the preceding extent.
5664 */
5665 if ((left->br_startoff + left->br_blockcount != startoff) ||
5666 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5667 (left->br_state != got->br_state) ||
5668 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5669 return false;
5670
5671 return true;
5672}
5673
5674/*
5675 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5676 * hole in the file. If an extent shift would result in the extent being fully
5677 * adjacent to the extent that currently precedes the hole, we can merge with
5678 * the preceding extent rather than do the shift.
5679 *
5680 * This function assumes the caller has verified a shift-by-merge is possible
5681 * with the provided extents via xfs_bmse_can_merge().
5682 */
5683STATIC int
5684xfs_bmse_merge(
Brian Foster0f37d172018-08-01 07:20:34 -07005685 struct xfs_trans *tp,
Brian Fosterddb19e32014-09-23 15:38:09 +10005686 struct xfs_inode *ip,
5687 int whichfork,
5688 xfs_fileoff_t shift, /* shift fsb */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005689 struct xfs_iext_cursor *icur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005690 struct xfs_bmbt_irec *got, /* extent to shift */
5691 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005692 struct xfs_btree_cur *cur,
Brian Foster0f37d172018-08-01 07:20:34 -07005693 int *logflags) /* output */
Brian Fosterddb19e32014-09-23 15:38:09 +10005694{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005695 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005696 xfs_filblks_t blockcount;
5697 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005698 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005699
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005700 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005701
5702 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5703 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005704 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005705
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005706 new = *left;
5707 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005708
5709 /*
5710 * Update the on-disk extent count, the btree if necessary and log the
5711 * inode.
5712 */
5713 XFS_IFORK_NEXT_SET(ip, whichfork,
5714 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5715 *logflags |= XFS_ILOG_CORE;
5716 if (!cur) {
5717 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005718 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005719 }
5720
5721 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005722 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005723 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005724 return error;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005725 if (XFS_IS_CORRUPT(mp, i != 1))
5726 return -EFSCORRUPTED;
Brian Fosterddb19e32014-09-23 15:38:09 +10005727
5728 error = xfs_btree_delete(cur, &i);
5729 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005730 return error;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005731 if (XFS_IS_CORRUPT(mp, i != 1))
5732 return -EFSCORRUPTED;
Brian Fosterddb19e32014-09-23 15:38:09 +10005733
5734 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005735 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005736 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005737 return error;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005738 if (XFS_IS_CORRUPT(mp, i != 1))
5739 return -EFSCORRUPTED;
Brian Fosterddb19e32014-09-23 15:38:09 +10005740
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005741 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005742 if (error)
5743 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005744
Brian Fostere20e1742019-09-18 17:50:45 -07005745 /* change to extent format if required after extent removal */
5746 error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork);
5747 if (error)
5748 return error;
5749
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005750done:
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005751 xfs_iext_remove(ip, icur, 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005752 xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur);
5753 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5754 &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005755
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005756 /* update reverse mapping. rmap functions merge the rmaps for us */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07005757 xfs_rmap_unmap_extent(tp, ip, whichfork, got);
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005758 memcpy(&new, got, sizeof(new));
5759 new.br_startoff = left->br_startoff + left->br_blockcount;
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07005760 xfs_rmap_map_extent(tp, ip, whichfork, &new);
5761 return 0;
Brian Fosterddb19e32014-09-23 15:38:09 +10005762}
5763
Christoph Hellwigbf806282017-10-19 11:07:34 -07005764static int
5765xfs_bmap_shift_update_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07005766 struct xfs_trans *tp,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005767 struct xfs_inode *ip,
5768 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005769 struct xfs_iext_cursor *icur,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005770 struct xfs_bmbt_irec *got,
5771 struct xfs_btree_cur *cur,
5772 int *logflags,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005773 xfs_fileoff_t startoff)
Brian Fostera979bdf2014-09-23 15:39:04 +10005774{
Christoph Hellwigbf806282017-10-19 11:07:34 -07005775 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005776 struct xfs_bmbt_irec prev = *got;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005777 int error, i;
Brian Fostera979bdf2014-09-23 15:39:04 +10005778
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005779 *logflags |= XFS_ILOG_CORE;
5780
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005781 got->br_startoff = startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005782
5783 if (cur) {
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005784 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005785 if (error)
5786 return error;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005787 if (XFS_IS_CORRUPT(mp, i != 1))
5788 return -EFSCORRUPTED;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005789
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005790 error = xfs_bmbt_update(cur, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005791 if (error)
5792 return error;
5793 } else {
5794 *logflags |= XFS_ILOG_DEXT;
5795 }
5796
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005797 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5798 got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005799
Darrick J. Wong9c194642016-08-03 12:16:05 +10005800 /* update reverse mapping */
Darrick J. Wongbc46ac62019-08-26 17:06:03 -07005801 xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
5802 xfs_rmap_map_extent(tp, ip, whichfork, got);
5803 return 0;
Brian Fostera979bdf2014-09-23 15:39:04 +10005804}
5805
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005806int
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005807xfs_bmap_collapse_extents(
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005808 struct xfs_trans *tp,
5809 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005810 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005811 xfs_fileoff_t offset_shift_fsb,
Brian Foster333f9502018-07-11 22:26:27 -07005812 bool *done)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005813{
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005814 int whichfork = XFS_DATA_FORK;
5815 struct xfs_mount *mp = ip->i_mount;
5816 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5817 struct xfs_btree_cur *cur = NULL;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005818 struct xfs_bmbt_irec got, prev;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005819 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005820 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005821 int error = 0;
5822 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005823
Darrick J. Wonga71895c2019-11-11 12:53:22 -08005824 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ip, whichfork)) ||
5825 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Dave Chinner24513372014-06-25 14:58:08 +10005826 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005827 }
5828
5829 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005830 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005831
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005832 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005833
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005834 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005835 error = xfs_iread_extents(tp, ip, whichfork);
5836 if (error)
5837 return error;
5838 }
5839
Brian Fosterddb19e32014-09-23 15:38:09 +10005840 if (ifp->if_flags & XFS_IFBROOT) {
5841 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner92219c22020-03-10 17:52:53 -07005842 cur->bc_ino.flags = 0;
Brian Fosterddb19e32014-09-23 15:38:09 +10005843 }
5844
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005845 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005846 *done = true;
5847 goto del_cursor;
5848 }
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005849 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5850 error = -EFSCORRUPTED;
5851 goto del_cursor;
5852 }
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005853
Christoph Hellwigbf806282017-10-19 11:07:34 -07005854 new_startoff = got.br_startoff - offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005855 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005856 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5857 error = -EINVAL;
5858 goto del_cursor;
5859 }
5860
Christoph Hellwigbf806282017-10-19 11:07:34 -07005861 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
Brian Foster0f37d172018-08-01 07:20:34 -07005862 error = xfs_bmse_merge(tp, ip, whichfork,
5863 offset_shift_fsb, &icur, &got, &prev,
5864 cur, &logflags);
Christoph Hellwigbf806282017-10-19 11:07:34 -07005865 if (error)
5866 goto del_cursor;
5867 goto done;
5868 }
5869 } else {
5870 if (got.br_startoff < offset_shift_fsb) {
5871 error = -EINVAL;
5872 goto del_cursor;
5873 }
5874 }
5875
Brian Foster0f37d172018-08-01 07:20:34 -07005876 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5877 cur, &logflags, new_startoff);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005878 if (error)
5879 goto del_cursor;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005880
Christoph Hellwig42630362017-11-03 10:34:41 -07005881done:
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005882 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5883 *done = true;
5884 goto del_cursor;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005885 }
5886
Christoph Hellwigbf806282017-10-19 11:07:34 -07005887 *next_fsb = got.br_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005888del_cursor:
5889 if (cur)
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07005890 xfs_btree_del_cursor(cur, error);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005891 if (logflags)
5892 xfs_trans_log_inode(tp, ip, logflags);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005893 return error;
5894}
5895
Darrick J. Wongf62cb482018-06-21 23:26:57 -07005896/* Make sure we won't be right-shifting an extent past the maximum bound. */
5897int
5898xfs_bmap_can_insert_extents(
5899 struct xfs_inode *ip,
5900 xfs_fileoff_t off,
5901 xfs_fileoff_t shift)
5902{
5903 struct xfs_bmbt_irec got;
5904 int is_empty;
5905 int error = 0;
5906
5907 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5908
5909 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5910 return -EIO;
5911
5912 xfs_ilock(ip, XFS_ILOCK_EXCL);
5913 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
5914 if (!error && !is_empty && got.br_startoff >= off &&
5915 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
5916 error = -EINVAL;
5917 xfs_iunlock(ip, XFS_ILOCK_EXCL);
5918
5919 return error;
5920}
5921
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005922int
5923xfs_bmap_insert_extents(
5924 struct xfs_trans *tp,
5925 struct xfs_inode *ip,
5926 xfs_fileoff_t *next_fsb,
5927 xfs_fileoff_t offset_shift_fsb,
5928 bool *done,
Brian Foster333f9502018-07-11 22:26:27 -07005929 xfs_fileoff_t stop_fsb)
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005930{
5931 int whichfork = XFS_DATA_FORK;
5932 struct xfs_mount *mp = ip->i_mount;
5933 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5934 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005935 struct xfs_bmbt_irec got, next;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005936 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005937 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005938 int error = 0;
5939 int logflags = 0;
5940
Darrick J. Wonga71895c2019-11-11 12:53:22 -08005941 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ip, whichfork)) ||
5942 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005943 return -EFSCORRUPTED;
5944 }
5945
5946 if (XFS_FORCED_SHUTDOWN(mp))
5947 return -EIO;
5948
5949 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5950
5951 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5952 error = xfs_iread_extents(tp, ip, whichfork);
5953 if (error)
5954 return error;
5955 }
5956
5957 if (ifp->if_flags & XFS_IFBROOT) {
5958 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner92219c22020-03-10 17:52:53 -07005959 cur->bc_ino.flags = 0;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005960 }
5961
Namjae Jeona904b1c2015-03-25 15:08:56 +11005962 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005963 xfs_iext_last(ifp, &icur);
5964 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005965 stop_fsb > got.br_startoff) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005966 *done = true;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005967 goto del_cursor;
5968 }
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005969 } else {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005970 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005971 *done = true;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005972 goto del_cursor;
5973 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005974 }
Darrick J. Wongf9e03702019-11-11 12:52:18 -08005975 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5976 error = -EFSCORRUPTED;
5977 goto del_cursor;
5978 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005979
Brian Fosterd0c22042019-12-11 13:18:38 -08005980 if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) {
Darrick J. Wongc2414ad2019-10-28 16:12:34 -07005981 error = -EFSCORRUPTED;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005982 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005983 }
5984
Christoph Hellwigbf806282017-10-19 11:07:34 -07005985 new_startoff = got.br_startoff + offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005986 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005987 if (new_startoff + got.br_blockcount > next.br_startoff) {
5988 error = -EINVAL;
5989 goto del_cursor;
5990 }
5991
5992 /*
5993 * Unlike a left shift (which involves a hole punch), a right
5994 * shift does not modify extent neighbors in any way. We should
5995 * never find mergeable extents in this scenario. Check anyways
5996 * and warn if we encounter two extents that could be one.
5997 */
5998 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5999 WARN_ON_ONCE(1);
6000 }
6001
Brian Foster0f37d172018-08-01 07:20:34 -07006002 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
6003 cur, &logflags, new_startoff);
Christoph Hellwig6b18af02017-10-19 11:07:10 -07006004 if (error)
6005 goto del_cursor;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07006006
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006007 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07006008 stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07006009 *done = true;
Christoph Hellwig6b18af02017-10-19 11:07:10 -07006010 goto del_cursor;
6011 }
Christoph Hellwig6b18af02017-10-19 11:07:10 -07006012
6013 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006014del_cursor:
6015 if (cur)
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07006016 xfs_btree_del_cursor(cur, error);
Brian Fosterca446d82014-09-02 12:12:53 +10006017 if (logflags)
6018 xfs_trans_log_inode(tp, ip, logflags);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006019 return error;
6020}
Namjae Jeona904b1c2015-03-25 15:08:56 +11006021
6022/*
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006023 * Splits an extent into two extents at split_fsb block such that it is the
6024 * first block of the current_ext. @ext is a target extent to be split.
6025 * @split_fsb is a block where the extents is split. If split_fsb lies in a
6026 * hole or the first block of extents, just return 0.
Namjae Jeona904b1c2015-03-25 15:08:56 +11006027 */
Brian Fosterb73df172020-02-26 09:43:15 -08006028int
6029xfs_bmap_split_extent(
Namjae Jeona904b1c2015-03-25 15:08:56 +11006030 struct xfs_trans *tp,
6031 struct xfs_inode *ip,
Brian Foster4b77a082018-07-11 22:26:27 -07006032 xfs_fileoff_t split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006033{
6034 int whichfork = XFS_DATA_FORK;
6035 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006036 struct xfs_bmbt_irec got;
6037 struct xfs_bmbt_irec new; /* split extent */
6038 struct xfs_mount *mp = ip->i_mount;
6039 struct xfs_ifork *ifp;
6040 xfs_fsblock_t gotblkcnt; /* new block count for got */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006041 struct xfs_iext_cursor icur;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006042 int error = 0;
6043 int logflags = 0;
6044 int i = 0;
6045
Darrick J. Wonga71895c2019-11-11 12:53:22 -08006046 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ip, whichfork)) ||
6047 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006048 return -EFSCORRUPTED;
6049 }
6050
6051 if (XFS_FORCED_SHUTDOWN(mp))
6052 return -EIO;
6053
6054 ifp = XFS_IFORK_PTR(ip, whichfork);
6055 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6056 /* Read in all the extents */
6057 error = xfs_iread_extents(tp, ip, whichfork);
6058 if (error)
6059 return error;
6060 }
6061
6062 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006063 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11006064 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006065 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006066 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006067 return 0;
6068
6069 gotblkcnt = split_fsb - got.br_startoff;
6070 new.br_startoff = split_fsb;
6071 new.br_startblock = got.br_startblock + gotblkcnt;
6072 new.br_blockcount = got.br_blockcount - gotblkcnt;
6073 new.br_state = got.br_state;
6074
6075 if (ifp->if_flags & XFS_IFBROOT) {
6076 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner92219c22020-03-10 17:52:53 -07006077 cur->bc_ino.flags = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07006078 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006079 if (error)
6080 goto del_cursor;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08006081 if (XFS_IS_CORRUPT(mp, i != 1)) {
6082 error = -EFSCORRUPTED;
6083 goto del_cursor;
6084 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006085 }
6086
Namjae Jeona904b1c2015-03-25 15:08:56 +11006087 got.br_blockcount = gotblkcnt;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006088 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
6089 &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006090
6091 logflags = XFS_ILOG_CORE;
6092 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07006093 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006094 if (error)
6095 goto del_cursor;
6096 } else
6097 logflags |= XFS_ILOG_DEXT;
6098
6099 /* Add new extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006100 xfs_iext_next(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07006101 xfs_iext_insert(ip, &icur, &new, 0);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006102 XFS_IFORK_NEXT_SET(ip, whichfork,
6103 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6104
6105 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07006106 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006107 if (error)
6108 goto del_cursor;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08006109 if (XFS_IS_CORRUPT(mp, i != 0)) {
6110 error = -EFSCORRUPTED;
6111 goto del_cursor;
6112 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006113 error = xfs_btree_insert(cur, &i);
6114 if (error)
6115 goto del_cursor;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08006116 if (XFS_IS_CORRUPT(mp, i != 1)) {
6117 error = -EFSCORRUPTED;
6118 goto del_cursor;
6119 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006120 }
6121
6122 /*
6123 * Convert to a btree if necessary.
6124 */
6125 if (xfs_bmap_needs_btree(ip, whichfork)) {
6126 int tmp_logflags; /* partial log flag return val */
6127
6128 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07006129 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
6130 &tmp_logflags, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006131 logflags |= tmp_logflags;
6132 }
6133
6134del_cursor:
6135 if (cur) {
Dave Chinner92219c22020-03-10 17:52:53 -07006136 cur->bc_ino.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07006137 xfs_btree_del_cursor(cur, error);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006138 }
6139
6140 if (logflags)
6141 xfs_trans_log_inode(tp, ip, logflags);
6142 return error;
6143}
6144
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006145/* Deferred mapping is only for real extents in the data fork. */
6146static bool
6147xfs_bmap_is_update_needed(
6148 struct xfs_bmbt_irec *bmap)
6149{
6150 return bmap->br_startblock != HOLESTARTBLOCK &&
6151 bmap->br_startblock != DELAYSTARTBLOCK;
6152}
6153
6154/* Record a bmap intent. */
6155static int
6156__xfs_bmap_add(
Brian Foster0f37d172018-08-01 07:20:34 -07006157 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006158 enum xfs_bmap_intent_type type,
6159 struct xfs_inode *ip,
6160 int whichfork,
6161 struct xfs_bmbt_irec *bmap)
6162{
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006163 struct xfs_bmap_intent *bi;
6164
Brian Foster0f37d172018-08-01 07:20:34 -07006165 trace_xfs_bmap_defer(tp->t_mountp,
6166 XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock),
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006167 type,
Brian Foster0f37d172018-08-01 07:20:34 -07006168 XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock),
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006169 ip->i_ino, whichfork,
6170 bmap->br_startoff,
6171 bmap->br_blockcount,
6172 bmap->br_state);
6173
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07006174 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_NOFS);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006175 INIT_LIST_HEAD(&bi->bi_list);
6176 bi->bi_type = type;
6177 bi->bi_owner = ip;
6178 bi->bi_whichfork = whichfork;
6179 bi->bi_bmap = *bmap;
6180
Brian Foster0f37d172018-08-01 07:20:34 -07006181 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006182 return 0;
6183}
6184
6185/* Map an extent into a file. */
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006186void
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006187xfs_bmap_map_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07006188 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006189 struct xfs_inode *ip,
6190 struct xfs_bmbt_irec *PREV)
6191{
6192 if (!xfs_bmap_is_update_needed(PREV))
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006193 return;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006194
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006195 __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, XFS_DATA_FORK, PREV);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006196}
6197
6198/* Unmap an extent out of a file. */
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006199void
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006200xfs_bmap_unmap_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07006201 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006202 struct xfs_inode *ip,
6203 struct xfs_bmbt_irec *PREV)
6204{
6205 if (!xfs_bmap_is_update_needed(PREV))
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006206 return;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006207
Darrick J. Wong3e08f422019-08-26 17:06:04 -07006208 __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, XFS_DATA_FORK, PREV);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006209}
6210
6211/*
6212 * Process one of the deferred bmap operations. We pass back the
6213 * btree cursor to maintain our lock on the bmapbt between calls.
6214 */
6215int
6216xfs_bmap_finish_one(
6217 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006218 struct xfs_inode *ip,
6219 enum xfs_bmap_intent_type type,
6220 int whichfork,
6221 xfs_fileoff_t startoff,
6222 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006223 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006224 xfs_exntst_t state)
6225{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006226 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006227
Brian Foster37283792018-07-11 22:26:23 -07006228 ASSERT(tp->t_firstblock == NULLFSBLOCK);
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006229
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006230 trace_xfs_bmap_deferred(tp->t_mountp,
6231 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6232 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006233 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006234
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006235 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006236 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006237
6238 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006239 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006240 return -EIO;
6241
6242 switch (type) {
6243 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006244 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Brian Fosterff3edf22018-07-11 22:26:14 -07006245 startblock, 0);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006246 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006247 break;
6248 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006249 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
Brian Foster2af52842018-07-11 22:26:25 -07006250 XFS_BMAPI_REMAP, 1);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006251 break;
6252 default:
6253 ASSERT(0);
6254 error = -EFSCORRUPTED;
6255 }
6256
6257 return error;
6258}
Darrick J. Wong30b09842018-03-23 10:06:52 -07006259
6260/* Check that an inode's extent does not have invalid flags or bad ranges. */
6261xfs_failaddr_t
6262xfs_bmap_validate_extent(
6263 struct xfs_inode *ip,
6264 int whichfork,
6265 struct xfs_bmbt_irec *irec)
6266{
6267 struct xfs_mount *mp = ip->i_mount;
6268 xfs_fsblock_t endfsb;
6269 bool isrt;
6270
6271 isrt = XFS_IS_REALTIME_INODE(ip);
6272 endfsb = irec->br_startblock + irec->br_blockcount - 1;
6273 if (isrt) {
6274 if (!xfs_verify_rtbno(mp, irec->br_startblock))
6275 return __this_address;
6276 if (!xfs_verify_rtbno(mp, endfsb))
6277 return __this_address;
6278 } else {
6279 if (!xfs_verify_fsbno(mp, irec->br_startblock))
6280 return __this_address;
6281 if (!xfs_verify_fsbno(mp, endfsb))
6282 return __this_address;
6283 if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) !=
6284 XFS_FSB_TO_AGNO(mp, endfsb))
6285 return __this_address;
6286 }
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11006287 if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6288 return __this_address;
Darrick J. Wong30b09842018-03-23 10:06:52 -07006289 return NULL;
6290}