blob: 4133bc461e3ec834f3252cac28d5cca819d33322 [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 Chinner57062782013-10-15 09:17:51 +110016#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110017#include "xfs_da_btree.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100018#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_btree.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_extfree_item.h"
24#include "xfs_alloc.h"
25#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100026#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110027#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_rtalloc.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070029#include "xfs_errortag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_quota.h"
32#include "xfs_trans_space.h"
33#include "xfs_buf_item.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000034#include "xfs_trace.h"
Dave Chinner19de7352013-04-03 16:11:18 +110035#include "xfs_symlink.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110036#include "xfs_attr_leaf.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110037#include "xfs_filestream.h"
Darrick J. Wong340785c2016-08-03 11:33:42 +100038#include "xfs_rmap.h"
Darrick J. Wong3fd129b2016-09-19 10:30:52 +100039#include "xfs_ag_resv.h"
Darrick J. Wong62aab202016-10-03 09:11:23 -070040#include "xfs_refcount.h"
Brian Foster974ae922016-11-28 14:57:42 +110041#include "xfs_icache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044kmem_zone_t *xfs_bmap_free_item_zone;
45
46/*
Dave Chinner9e5987a72013-02-25 12:31:26 +110047 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
Dave Chinner9e5987a72013-02-25 12:31:26 +110051 * Compute and fill in the value of the maximum depth of a bmap btree
52 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 */
Dave Chinner9e5987a72013-02-25 12:31:26 +110054void
55xfs_bmap_compute_maxlevels(
56 xfs_mount_t *mp, /* file system mount structure */
57 int whichfork) /* data or attr fork */
58{
59 int level; /* btree level */
60 uint maxblocks; /* max blocks at this level */
61 uint maxleafents; /* max leaf entries possible */
62 int maxrootrecs; /* max records in root block */
63 int minleafrecs; /* min records in leaf block */
64 int minnoderecs; /* min records in node block */
65 int sz; /* root block size */
66
67 /*
68 * The maximum number of extents in a file, hence the maximum
69 * number of leaf entries, is controlled by the type of di_nextents
70 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
71 * (a signed 16-bit number, xfs_aextnum_t).
72 *
73 * Note that we can no longer assume that if we are in ATTR1 that
74 * the fork offset of all the inodes will be
75 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
76 * with ATTR2 and then mounted back with ATTR1, keeping the
77 * di_forkoff's fixed but probably at various positions. Therefore,
78 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
79 * of a minimum size available.
80 */
81 if (whichfork == XFS_DATA_FORK) {
82 maxleafents = MAXEXTNUM;
83 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
84 } else {
85 maxleafents = MAXAEXTNUM;
86 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
87 }
Eric Sandeen152d93b2014-04-14 18:58:51 +100088 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +110089 minleafrecs = mp->m_bmap_dmnr[0];
90 minnoderecs = mp->m_bmap_dmnr[1];
91 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
92 for (level = 1; maxblocks > 1; level++) {
93 if (maxblocks <= maxrootrecs)
94 maxblocks = 1;
95 else
96 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
97 }
98 mp->m_bm_maxlevels[whichfork] = level;
99}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100101STATIC int /* error */
102xfs_bmbt_lookup_eq(
103 struct xfs_btree_cur *cur,
Christoph Hellwige16cf9b2017-10-17 14:16:26 -0700104 struct xfs_bmbt_irec *irec,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100105 int *stat) /* success/failure */
106{
Christoph Hellwige16cf9b2017-10-17 14:16:26 -0700107 cur->bc_rec.b = *irec;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100108 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
109}
110
111STATIC int /* error */
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700112xfs_bmbt_lookup_first(
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100113 struct xfs_btree_cur *cur,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100114 int *stat) /* success/failure */
115{
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700116 cur->bc_rec.b.br_startoff = 0;
117 cur->bc_rec.b.br_startblock = 0;
118 cur->bc_rec.b.br_blockcount = 0;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100119 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
120}
121
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100122/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000123 * Check if the inode needs to be converted to btree format.
124 */
125static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
126{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700127 return whichfork != XFS_COW_FORK &&
128 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000129 XFS_IFORK_NEXTENTS(ip, whichfork) >
130 XFS_IFORK_MAXEXT(ip, whichfork);
131}
132
133/*
134 * Check if the inode should be converted to extent format.
135 */
136static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
137{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700138 return whichfork != XFS_COW_FORK &&
139 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000140 XFS_IFORK_NEXTENTS(ip, whichfork) <=
141 XFS_IFORK_MAXEXT(ip, whichfork);
142}
143
144/*
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700145 * Update the record referred to by cur to the value given by irec
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100146 * This either works (return 0) or gets an EFSCORRUPTED error.
147 */
148STATIC int
149xfs_bmbt_update(
150 struct xfs_btree_cur *cur,
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700151 struct xfs_bmbt_irec *irec)
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100152{
153 union xfs_btree_rec rec;
154
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700155 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100156 return xfs_btree_update(cur, &rec);
157}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*
Dave Chinner9e5987a72013-02-25 12:31:26 +1100160 * Compute the worst-case number of indirect blocks that will be used
161 * for ip's delayed extent of length "len".
162 */
163STATIC xfs_filblks_t
164xfs_bmap_worst_indlen(
165 xfs_inode_t *ip, /* incore inode pointer */
166 xfs_filblks_t len) /* delayed extent length */
167{
168 int level; /* btree level number */
169 int maxrecs; /* maximum record count at this level */
170 xfs_mount_t *mp; /* mount structure */
171 xfs_filblks_t rval; /* return value */
172
173 mp = ip->i_mount;
174 maxrecs = mp->m_bmap_dmxr[0];
175 for (level = 0, rval = 0;
176 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
177 level++) {
178 len += maxrecs - 1;
179 do_div(len, maxrecs);
180 rval += len;
Darrick J. Wong5e5c9432017-09-18 09:41:17 -0700181 if (len == 1)
182 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
Dave Chinner9e5987a72013-02-25 12:31:26 +1100183 level - 1;
184 if (level == 0)
185 maxrecs = mp->m_bmap_dmxr[1];
186 }
187 return rval;
188}
189
190/*
191 * Calculate the default attribute fork offset for newly created inodes.
192 */
193uint
194xfs_default_attroffset(
195 struct xfs_inode *ip)
196{
197 struct xfs_mount *mp = ip->i_mount;
198 uint offset;
199
200 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100201 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a72013-02-25 12:31:26 +1100202 XFS_BMDR_SPACE_CALC(MINABTPTRS);
203 } else {
204 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
205 }
206
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100207 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100208 return offset;
209}
210
211/*
212 * Helper routine to reset inode di_forkoff field when switching
213 * attribute fork from local to extent format - we reset it where
214 * possible to make space available for inline data fork extents.
215 */
216STATIC void
217xfs_bmap_forkoff_reset(
Dave Chinner9e5987a72013-02-25 12:31:26 +1100218 xfs_inode_t *ip,
219 int whichfork)
220{
221 if (whichfork == XFS_ATTR_FORK &&
222 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
Dave Chinner9e5987a72013-02-25 12:31:26 +1100223 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
224 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
225
226 if (dfl_forkoff > ip->i_d.di_forkoff)
227 ip->i_d.di_forkoff = dfl_forkoff;
228 }
229}
230
Dave Chinner9e5987a72013-02-25 12:31:26 +1100231#ifdef DEBUG
232STATIC struct xfs_buf *
233xfs_bmap_get_bp(
234 struct xfs_btree_cur *cur,
235 xfs_fsblock_t bno)
236{
Dave Chinnere6631f82018-05-09 07:49:37 -0700237 struct xfs_log_item *lip;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100238 int i;
239
240 if (!cur)
241 return NULL;
242
243 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
244 if (!cur->bc_bufs[i])
245 break;
246 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
247 return cur->bc_bufs[i];
248 }
249
250 /* Chase down all the log items to see if the bp is there */
Dave Chinnere6631f82018-05-09 07:49:37 -0700251 list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
252 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
253
Dave Chinner9e5987a72013-02-25 12:31:26 +1100254 if (bip->bli_item.li_type == XFS_LI_BUF &&
255 XFS_BUF_ADDR(bip->bli_buf) == bno)
256 return bip->bli_buf;
257 }
258
259 return NULL;
260}
261
262STATIC void
263xfs_check_block(
264 struct xfs_btree_block *block,
265 xfs_mount_t *mp,
266 int root,
267 short sz)
268{
269 int i, j, dmxr;
270 __be64 *pp, *thispa; /* pointer to block address */
271 xfs_bmbt_key_t *prevp, *keyp;
272
273 ASSERT(be16_to_cpu(block->bb_level) > 0);
274
275 prevp = NULL;
276 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
277 dmxr = mp->m_bmap_dmxr[0];
278 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
279
280 if (prevp) {
281 ASSERT(be64_to_cpu(prevp->br_startoff) <
282 be64_to_cpu(keyp->br_startoff));
283 }
284 prevp = keyp;
285
286 /*
287 * Compare the block numbers to see if there are dups.
288 */
289 if (root)
290 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
291 else
292 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
293
294 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
295 if (root)
296 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
297 else
298 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
299 if (*thispa == *pp) {
300 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
301 __func__, j, i,
302 (unsigned long long)be64_to_cpu(*thispa));
Darrick J. Wongcec57252018-05-04 15:31:21 -0700303 xfs_err(mp, "%s: ptrs are equal in node\n",
Dave Chinner9e5987a72013-02-25 12:31:26 +1100304 __func__);
Darrick J. Wongcec57252018-05-04 15:31:21 -0700305 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100306 }
307 }
308 }
309}
310
311/*
312 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100313 * btree leaves. THis becomes prohibitively expensive for large extent count
314 * files, so don't bother with inodes that have more than 10,000 extents in
315 * them. The btree record ordering checks will still be done, so for such large
316 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100317 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100318STATIC void
319xfs_bmap_check_leaf_extents(
320 xfs_btree_cur_t *cur, /* btree cursor or null */
321 xfs_inode_t *ip, /* incore inode pointer */
322 int whichfork) /* data or attr fork */
323{
324 struct xfs_btree_block *block; /* current btree block */
325 xfs_fsblock_t bno; /* block # of "block" */
326 xfs_buf_t *bp; /* buffer for "block" */
327 int error; /* error return value */
328 xfs_extnum_t i=0, j; /* index into the extents list */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -0700329 struct xfs_ifork *ifp; /* fork structure */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100330 int level; /* btree level, for checking */
331 xfs_mount_t *mp; /* file system mount structure */
332 __be64 *pp; /* pointer to block address */
333 xfs_bmbt_rec_t *ep; /* pointer to current extent */
334 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
335 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
336 int bp_release = 0;
337
338 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
339 return;
340 }
341
Dave Chinnere3543812016-01-08 11:28:49 +1100342 /* skip large extent count inodes */
343 if (ip->i_d.di_nextents > 10000)
344 return;
345
Dave Chinner9e5987a72013-02-25 12:31:26 +1100346 bno = NULLFSBLOCK;
347 mp = ip->i_mount;
348 ifp = XFS_IFORK_PTR(ip, whichfork);
349 block = ifp->if_broot;
350 /*
351 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
352 */
353 level = be16_to_cpu(block->bb_level);
354 ASSERT(level > 0);
355 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
356 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
357 bno = be64_to_cpu(*pp);
358
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000359 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100360 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
361 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
362
363 /*
364 * Go down the tree until leaf level is reached, following the first
365 * pointer (leftmost) at each level.
366 */
367 while (level-- > 0) {
368 /* See if buf is in cur first */
369 bp_release = 0;
370 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
371 if (!bp) {
372 bp_release = 1;
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700373 error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100374 XFS_BMAP_BTREE_REF,
375 &xfs_bmbt_buf_ops);
376 if (error)
377 goto error_norelse;
378 }
379 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100380 if (level == 0)
381 break;
382
383 /*
384 * Check this block for basic sanity (increasing keys and
385 * no duplicate blocks).
386 */
387
388 xfs_check_block(block, mp, 0, 0);
389 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
390 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100391 XFS_WANT_CORRUPTED_GOTO(mp,
Darrick J. Wong59f6fec2018-01-08 10:51:00 -0800392 xfs_verify_fsbno(mp, bno), error0);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100393 if (bp_release) {
394 bp_release = 0;
395 xfs_trans_brelse(NULL, bp);
396 }
397 }
398
399 /*
400 * Here with bp and block set to the leftmost leaf node in the tree.
401 */
402 i = 0;
403
404 /*
405 * Loop over all leaf nodes checking that all extents are in the right order.
406 */
407 for (;;) {
408 xfs_fsblock_t nextbno;
409 xfs_extnum_t num_recs;
410
411
412 num_recs = xfs_btree_get_numrecs(block);
413
414 /*
415 * Read-ahead the next leaf block, if any.
416 */
417
418 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
419
420 /*
421 * Check all the extents to make sure they are OK.
422 * If we had a previous block, the last entry should
423 * conform with the first entry in this one.
424 */
425
426 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
427 if (i) {
428 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
429 xfs_bmbt_disk_get_blockcount(&last) <=
430 xfs_bmbt_disk_get_startoff(ep));
431 }
432 for (j = 1; j < num_recs; j++) {
433 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
434 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
435 xfs_bmbt_disk_get_blockcount(ep) <=
436 xfs_bmbt_disk_get_startoff(nextp));
437 ep = nextp;
438 }
439
440 last = *ep;
441 i += num_recs;
442 if (bp_release) {
443 bp_release = 0;
444 xfs_trans_brelse(NULL, bp);
445 }
446 bno = nextbno;
447 /*
448 * If we've reached the end, stop.
449 */
450 if (bno == NULLFSBLOCK)
451 break;
452
453 bp_release = 0;
454 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
455 if (!bp) {
456 bp_release = 1;
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700457 error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100458 XFS_BMAP_BTREE_REF,
459 &xfs_bmbt_buf_ops);
460 if (error)
461 goto error_norelse;
462 }
463 block = XFS_BUF_TO_BLOCK(bp);
464 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100465
Dave Chinner9e5987a72013-02-25 12:31:26 +1100466 return;
467
468error0:
469 xfs_warn(mp, "%s: at error0", __func__);
470 if (bp_release)
471 xfs_trans_brelse(NULL, bp);
472error_norelse:
473 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
474 __func__, i);
Darrick J. Wongcec57252018-05-04 15:31:21 -0700475 xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
476 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100477 return;
478}
479
480/*
Dave Chinner9e5987a72013-02-25 12:31:26 +1100481 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000482 * given the caller's original parameters. Specifically check the
483 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a72013-02-25 12:31:26 +1100484 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
485 */
486STATIC void
487xfs_bmap_validate_ret(
488 xfs_fileoff_t bno,
489 xfs_filblks_t len,
490 int flags,
491 xfs_bmbt_irec_t *mval,
492 int nmap,
493 int ret_nmap)
494{
495 int i; /* index to map values */
496
497 ASSERT(ret_nmap <= nmap);
498
499 for (i = 0; i < ret_nmap; i++) {
500 ASSERT(mval[i].br_blockcount > 0);
501 if (!(flags & XFS_BMAPI_ENTIRE)) {
502 ASSERT(mval[i].br_startoff >= bno);
503 ASSERT(mval[i].br_blockcount <= len);
504 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
505 bno + len);
506 } else {
507 ASSERT(mval[i].br_startoff < bno + len);
508 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
509 bno);
510 }
511 ASSERT(i == 0 ||
512 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
513 mval[i].br_startoff);
514 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
515 mval[i].br_startblock != HOLESTARTBLOCK);
516 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
517 mval[i].br_state == XFS_EXT_UNWRITTEN);
518 }
519}
520
521#else
522#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
Darrick J. Wong7bf7a192017-08-31 15:11:06 -0700523#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100524#endif /* DEBUG */
525
526/*
527 * bmap free list manipulation functions
528 */
529
530/*
531 * Add the extent to the list of extents to be free at transaction end.
532 * The list is maintained sorted (by block number).
533 */
534void
Brian Fosterfcb762f2018-05-09 08:45:04 -0700535__xfs_bmap_add_free(
Brian Foster0f37d172018-08-01 07:20:34 -0700536 struct xfs_trans *tp,
Darrick J. Wong340785c2016-08-03 11:33:42 +1000537 xfs_fsblock_t bno,
538 xfs_filblks_t len,
Darrick J. Wong66e32372018-12-12 08:46:23 -0800539 const struct xfs_owner_info *oinfo,
Brian Fosterfcb762f2018-05-09 08:45:04 -0700540 bool skip_discard)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100541{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000542 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100543#ifdef DEBUG
Brian Foster0f37d172018-08-01 07:20:34 -0700544 struct xfs_mount *mp = tp->t_mountp;
545 xfs_agnumber_t agno;
546 xfs_agblock_t agbno;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100547
548 ASSERT(bno != NULLFSBLOCK);
549 ASSERT(len > 0);
550 ASSERT(len <= MAXEXTLEN);
551 ASSERT(!isnullstartblock(bno));
552 agno = XFS_FSB_TO_AGNO(mp, bno);
553 agbno = XFS_FSB_TO_AGBNO(mp, bno);
554 ASSERT(agno < mp->m_sb.sb_agcount);
555 ASSERT(agbno < mp->m_sb.sb_agblocks);
556 ASSERT(len < mp->m_sb.sb_agblocks);
557 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
558#endif
559 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000560
Dave Chinner9e5987a72013-02-25 12:31:26 +1100561 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000562 new->xefi_startblock = bno;
563 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000564 if (oinfo)
565 new->xefi_oinfo = *oinfo;
566 else
Darrick J. Wong7280fed2018-12-12 08:46:23 -0800567 new->xefi_oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
Brian Fosterfcb762f2018-05-09 08:45:04 -0700568 new->xefi_skip_discard = skip_discard;
Brian Foster0f37d172018-08-01 07:20:34 -0700569 trace_xfs_bmap_free_defer(tp->t_mountp,
570 XFS_FSB_TO_AGNO(tp->t_mountp, bno), 0,
571 XFS_FSB_TO_AGBNO(tp->t_mountp, bno), len);
572 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100573}
574
575/*
576 * Inode fork format manipulation functions
577 */
578
579/*
Christoph Hellwigb101e332019-02-15 08:02:47 -0800580 * Convert the inode format to extent format if it currently is in btree format,
581 * but the extent list is small enough that it fits into the extent format.
582 *
583 * Since the extents are already in-core, all we have to do is give up the space
584 * for the btree root and pitch the leaf block.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100585 */
586STATIC int /* error */
587xfs_bmap_btree_to_extents(
Christoph Hellwigb101e332019-02-15 08:02:47 -0800588 struct xfs_trans *tp, /* transaction pointer */
589 struct xfs_inode *ip, /* incore inode pointer */
590 struct xfs_btree_cur *cur, /* btree cursor */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100591 int *logflagsp, /* inode logging flags */
592 int whichfork) /* data or attr fork */
593{
Christoph Hellwigb101e332019-02-15 08:02:47 -0800594 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
595 struct xfs_mount *mp = ip->i_mount;
596 struct xfs_btree_block *rblock = ifp->if_broot;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100597 struct xfs_btree_block *cblock;/* child btree block */
598 xfs_fsblock_t cbno; /* child block number */
599 xfs_buf_t *cbp; /* child block's buffer */
600 int error; /* error return value */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100601 __be64 *pp; /* ptr to block address */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000602 struct xfs_owner_info oinfo;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100603
Christoph Hellwigb101e332019-02-15 08:02:47 -0800604 /* check if we actually need the extent format first: */
605 if (!xfs_bmap_wants_extents(ip, whichfork))
606 return 0;
607
608 ASSERT(cur);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700609 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100610 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
611 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100612 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
613 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
614 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
Christoph Hellwigb101e332019-02-15 08:02:47 -0800615
Dave Chinner9e5987a72013-02-25 12:31:26 +1100616 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
617 cbno = be64_to_cpu(*pp);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100618#ifdef DEBUG
Darrick J. Wongf1357612017-10-17 21:37:33 -0700619 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
620 xfs_btree_check_lptr(cur, cbno, 1));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100621#endif
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700622 error = xfs_btree_read_bufl(mp, tp, cbno, &cbp, XFS_BMAP_BTREE_REF,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100623 &xfs_bmbt_buf_ops);
624 if (error)
625 return error;
626 cblock = XFS_BUF_TO_BLOCK(cbp);
627 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
628 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000629 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
Brian Foster0f37d172018-08-01 07:20:34 -0700630 xfs_bmap_add_free(cur->bc_tp, cbno, 1, &oinfo);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100631 ip->i_d.di_nblocks--;
632 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
633 xfs_trans_binval(tp, cbp);
634 if (cur->bc_bufs[0] == cbp)
635 cur->bc_bufs[0] = NULL;
636 xfs_iroot_realloc(ip, -1, whichfork);
637 ASSERT(ifp->if_broot == NULL);
638 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
639 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Christoph Hellwigb101e332019-02-15 08:02:47 -0800640 *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100641 return 0;
642}
643
644/*
645 * Convert an extents-format file into a btree-format file.
646 * The new file will have a root block (in the inode) and a single child block.
647 */
648STATIC int /* error */
649xfs_bmap_extents_to_btree(
Brian Foster81ba8f32018-07-11 22:26:16 -0700650 struct xfs_trans *tp, /* transaction pointer */
651 struct xfs_inode *ip, /* incore inode pointer */
Brian Foster81ba8f32018-07-11 22:26:16 -0700652 struct xfs_btree_cur **curp, /* cursor returned to caller */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100653 int wasdel, /* converting a delayed alloc */
654 int *logflagsp, /* inode logging flags */
655 int whichfork) /* data or attr fork */
656{
657 struct xfs_btree_block *ablock; /* allocated (child) bt block */
Brian Foster81ba8f32018-07-11 22:26:16 -0700658 struct xfs_buf *abp; /* buffer for ablock */
659 struct xfs_alloc_arg args; /* allocation arguments */
660 struct xfs_bmbt_rec *arp; /* child record pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100661 struct xfs_btree_block *block; /* btree root block */
Brian Foster81ba8f32018-07-11 22:26:16 -0700662 struct xfs_btree_cur *cur; /* bmap btree cursor */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100663 int error; /* error return value */
Brian Foster81ba8f32018-07-11 22:26:16 -0700664 struct xfs_ifork *ifp; /* inode fork pointer */
665 struct xfs_bmbt_key *kp; /* root block key pointer */
666 struct xfs_mount *mp; /* mount structure */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100667 xfs_bmbt_ptr_t *pp; /* root block address pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700668 struct xfs_iext_cursor icur;
Christoph Hellwig906abed2017-11-03 10:34:43 -0700669 struct xfs_bmbt_irec rec;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700670 xfs_extnum_t cnt = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100671
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500672 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700673 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100674 ifp = XFS_IFORK_PTR(ip, whichfork);
675 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
676
677 /*
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000678 * Make space in the inode incore. This needs to be undone if we fail
679 * to expand the root.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100680 */
681 xfs_iroot_realloc(ip, 1, whichfork);
682 ifp->if_flags |= XFS_IFBROOT;
683
684 /*
685 * Fill in the root.
686 */
687 block = ifp->if_broot;
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800688 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
689 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
Eric Sandeenf88ae462017-01-27 23:16:37 -0800690 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100691 /*
692 * Need a cursor. Can't allocate until bb_level is filled in.
693 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100694 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100695 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
696 /*
697 * Convert to a btree with two levels, one record in root.
698 */
699 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
700 memset(&args, 0, sizeof(args));
701 args.tp = tp;
702 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000703 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Brian Foster280253d2018-07-11 22:26:29 -0700704 if (tp->t_firstblock == NULLFSBLOCK) {
Dave Chinner9e5987a72013-02-25 12:31:26 +1100705 args.type = XFS_ALLOCTYPE_START_BNO;
706 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Brian Foster1214f1c2018-08-01 07:20:31 -0700707 } else if (tp->t_flags & XFS_TRANS_LOWMODE) {
Dave Chinner9e5987a72013-02-25 12:31:26 +1100708 args.type = XFS_ALLOCTYPE_START_BNO;
Brian Foster280253d2018-07-11 22:26:29 -0700709 args.fsbno = tp->t_firstblock;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100710 } else {
711 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Brian Foster280253d2018-07-11 22:26:29 -0700712 args.fsbno = tp->t_firstblock;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100713 }
714 args.minlen = args.maxlen = args.prod = 1;
715 args.wasdel = wasdel;
716 *logflagsp = 0;
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000717 error = xfs_alloc_vextent(&args);
718 if (error)
719 goto out_root_realloc;
Darrick J. Wong90e20562016-10-03 09:11:45 -0700720
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800721 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
Shan Hai01239d772018-08-10 17:55:55 -0700722 error = -ENOSPC;
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000723 goto out_root_realloc;
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800724 }
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000725
Dave Chinner9e5987a72013-02-25 12:31:26 +1100726 /*
727 * Allocation can't fail, the space was reserved.
728 */
Brian Foster280253d2018-07-11 22:26:29 -0700729 ASSERT(tp->t_firstblock == NULLFSBLOCK ||
730 args.agno >= XFS_FSB_TO_AGNO(mp, tp->t_firstblock));
Brian Fostercf612de2018-07-11 22:26:29 -0700731 tp->t_firstblock = args.fsbno;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100732 cur->bc_private.b.allocated++;
733 ip->i_d.di_nblocks++;
734 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700735 abp = xfs_btree_get_bufl(mp, tp, args.fsbno);
Shan Hai01239d772018-08-10 17:55:55 -0700736 if (!abp) {
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000737 error = -EFSCORRUPTED;
738 goto out_unreserve_dquot;
Shan Hai01239d772018-08-10 17:55:55 -0700739 }
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000740
Dave Chinner9e5987a72013-02-25 12:31:26 +1100741 /*
742 * Fill in the child block.
743 */
744 abp->b_ops = &xfs_bmbt_buf_ops;
745 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800746 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
747 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500748 XFS_BTREE_LONG_PTRS);
749
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700750 for_each_xfs_iext(ifp, &icur, &rec) {
Christoph Hellwig906abed2017-11-03 10:34:43 -0700751 if (isnullstartblock(rec.br_startblock))
752 continue;
753 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
754 xfs_bmbt_disk_set_all(arp, &rec);
755 cnt++;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100756 }
757 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
758 xfs_btree_set_numrecs(ablock, cnt);
759
760 /*
761 * Fill in the root key and pointer.
762 */
763 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
764 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
765 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
766 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
767 be16_to_cpu(block->bb_level)));
768 *pp = cpu_to_be64(args.fsbno);
769
770 /*
771 * Do all this logging at the end so that
772 * the root is at the right level.
773 */
774 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
775 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
776 ASSERT(*curp == NULL);
777 *curp = cur;
778 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
779 return 0;
Shan Hai01239d772018-08-10 17:55:55 -0700780
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000781out_unreserve_dquot:
Shan Hai01239d772018-08-10 17:55:55 -0700782 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000783out_root_realloc:
Shan Hai01239d772018-08-10 17:55:55 -0700784 xfs_iroot_realloc(ip, -1, whichfork);
785 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000786 ASSERT(ifp->if_broot == NULL);
Shan Hai01239d772018-08-10 17:55:55 -0700787 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
788
789 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100790}
791
792/*
793 * Convert a local file to an extents file.
794 * This code is out of bounds for data forks of regular files,
795 * since the file data needs to get logged so things will stay consistent.
796 * (The bmap-level manipulations are ok, though).
797 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000798void
799xfs_bmap_local_to_extents_empty(
800 struct xfs_inode *ip,
801 int whichfork)
802{
803 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
804
Darrick J. Wong60b49842016-10-03 09:11:34 -0700805 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000806 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
807 ASSERT(ifp->if_bytes == 0);
808 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
809
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000810 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000811 ifp->if_flags &= ~XFS_IFINLINE;
812 ifp->if_flags |= XFS_IFEXTENTS;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700813 ifp->if_u1.if_root = NULL;
814 ifp->if_height = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000815 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
816}
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) {
848 xfs_bmap_local_to_extents_empty(ip, whichfork);
849 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;
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700881 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000882
Dave Chinnerfe22d552015-01-22 09:30:06 +1100883 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100884 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100885 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100886 * The callout is responsible for logging because the remote format
887 * might differ from the local format and thus we don't know how much to
888 * log here. Note that init_fn must also set the buffer log item type
889 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100890 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000891 init_fn(tp, bp, ip, ifp);
892
Brian Fosterb7cdc662015-10-12 15:40:24 +1100893 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000894 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
895 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100896 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000897
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700898 ifp->if_u1.if_root = NULL;
899 ifp->if_height = 0;
900
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700901 rec.br_startoff = 0;
902 rec.br_startblock = args.fsbno;
903 rec.br_blockcount = 1;
904 rec.br_state = XFS_EXT_NORM;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700905 xfs_iext_first(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -0700906 xfs_iext_insert(ip, &icur, &rec, 0);
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700907
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000908 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
909 ip->i_d.di_nblocks = 1;
910 xfs_trans_mod_dquot_byino(tp, ip,
911 XFS_TRANS_DQ_BCOUNT, 1L);
912 flags |= xfs_ilog_fext(whichfork);
913
Dave Chinner9e5987a72013-02-25 12:31:26 +1100914done:
915 *logflagsp = flags;
916 return error;
917}
918
919/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 * Called from xfs_bmap_add_attrfork to handle btree format files.
921 */
922STATIC int /* error */
923xfs_bmap_add_attrfork_btree(
924 xfs_trans_t *tp, /* transaction pointer */
925 xfs_inode_t *ip, /* incore inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 int *flags) /* inode logging flags */
927{
928 xfs_btree_cur_t *cur; /* btree cursor */
929 int error; /* error return value */
930 xfs_mount_t *mp; /* file system mount struct */
931 int stat; /* newroot status */
932
933 mp = ip->i_mount;
934 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
935 *flags |= XFS_ILOG_DBROOT;
936 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100937 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700938 error = xfs_bmbt_lookup_first(cur, &stat);
939 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000941 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100942 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100943 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 goto error0;
945 if (stat == 0) {
946 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000947 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 cur->bc_private.b.allocated = 0;
950 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
951 }
952 return 0;
953error0:
954 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
955 return error;
956}
957
958/*
959 * Called from xfs_bmap_add_attrfork to handle extents format files.
960 */
961STATIC int /* error */
962xfs_bmap_add_attrfork_extents(
Brian Foster81ba8f32018-07-11 22:26:16 -0700963 struct xfs_trans *tp, /* transaction pointer */
964 struct xfs_inode *ip, /* incore inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 int *flags) /* inode logging flags */
966{
967 xfs_btree_cur_t *cur; /* bmap btree cursor */
968 int error; /* error return value */
969
970 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
971 return 0;
972 cur = NULL;
Brian Foster280253d2018-07-11 22:26:29 -0700973 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
974 XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (cur) {
976 cur->bc_private.b.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -0700977 xfs_btree_del_cursor(cur, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979 return error;
980}
981
982/*
Dave Chinner1e823792013-02-11 15:58:13 +1100983 * Called from xfs_bmap_add_attrfork to handle local format files. Each
984 * different data fork content type needs a different callout to do the
985 * conversion. Some are basic and only require special block initialisation
986 * callouts for the data formating, others (directories) are so specialised they
987 * handle everything themselves.
988 *
989 * XXX (dgc): investigate whether directory conversion can use the generic
990 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500991 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 */
993STATIC int /* error */
994xfs_bmap_add_attrfork_local(
Brian Foster825d75cd2018-07-11 22:26:21 -0700995 struct xfs_trans *tp, /* transaction pointer */
996 struct xfs_inode *ip, /* incore inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 int *flags) /* inode logging flags */
998{
Brian Foster825d75cd2018-07-11 22:26:21 -0700999 struct xfs_da_args dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1002 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001003
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001004 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001006 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 dargs.dp = ip;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001008 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 dargs.whichfork = XFS_DATA_FORK;
1010 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001011 return xfs_dir2_sf_to_block(&dargs);
1012 }
1013
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001014 if (S_ISLNK(VFS_I(ip)->i_mode))
Brian Foster280253d2018-07-11 22:26:29 -07001015 return xfs_bmap_local_to_extents(tp, ip, 1, flags,
1016 XFS_DATA_FORK,
Dave Chinner1e823792013-02-11 15:58:13 +11001017 xfs_symlink_local_to_remote);
1018
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001019 /* should only be called for types that support local format data */
1020 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001021 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
Allison Henderson2f3cd802018-10-18 17:21:16 +11001024/* Set an inode attr fork off based on the format */
1025int
1026xfs_bmap_set_attrforkoff(
1027 struct xfs_inode *ip,
1028 int size,
1029 int *version)
1030{
1031 switch (ip->i_d.di_format) {
1032 case XFS_DINODE_FMT_DEV:
1033 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1034 break;
1035 case XFS_DINODE_FMT_LOCAL:
1036 case XFS_DINODE_FMT_EXTENTS:
1037 case XFS_DINODE_FMT_BTREE:
1038 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1039 if (!ip->i_d.di_forkoff)
1040 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1041 else if ((ip->i_mount->m_flags & XFS_MOUNT_ATTR2) && version)
1042 *version = 2;
1043 break;
1044 default:
1045 ASSERT(0);
1046 return -EINVAL;
1047 }
1048
1049 return 0;
1050}
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001053 * Convert inode from non-attributed to attributed.
1054 * Must not be in a transaction, ip must not be locked.
1055 */
1056int /* error code */
1057xfs_bmap_add_attrfork(
1058 xfs_inode_t *ip, /* incore inode pointer */
1059 int size, /* space new attribute needs */
1060 int rsvd) /* xact may use reserved blks */
1061{
Dave Chinner9e5987a72013-02-25 12:31:26 +11001062 xfs_mount_t *mp; /* mount structure */
1063 xfs_trans_t *tp; /* transaction pointer */
1064 int blks; /* space reservation */
1065 int version = 1; /* superblock attr version */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001066 int logflags; /* logging flags */
1067 int error; /* error return value */
1068
1069 ASSERT(XFS_IFORK_Q(ip) == 0);
1070
1071 mp = ip->i_mount;
1072 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001073
Dave Chinner9e5987a72013-02-25 12:31:26 +11001074 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001075
1076 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1077 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1078 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001079 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001080
Dave Chinner9e5987a72013-02-25 12:31:26 +11001081 xfs_ilock(ip, XFS_ILOCK_EXCL);
1082 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1083 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1084 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001085 if (error)
1086 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001087 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001088 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001089 if (ip->i_d.di_anextents != 0) {
1090 error = -EFSCORRUPTED;
1091 goto trans_cancel;
1092 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001093 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1094 /*
1095 * For inodes coming from pre-6.2 filesystems.
1096 */
1097 ASSERT(ip->i_d.di_aformat == 0);
1098 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1099 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001100
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001101 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001102 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Allison Henderson2f3cd802018-10-18 17:21:16 +11001103 error = xfs_bmap_set_attrforkoff(ip, size, &version);
1104 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001105 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001106 ASSERT(ip->i_afp == NULL);
1107 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1108 ip->i_afp->if_flags = XFS_IFEXTENTS;
1109 logflags = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001110 switch (ip->i_d.di_format) {
1111 case XFS_DINODE_FMT_LOCAL:
Brian Foster825d75cd2018-07-11 22:26:21 -07001112 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001113 break;
1114 case XFS_DINODE_FMT_EXTENTS:
Brian Foster825d75cd2018-07-11 22:26:21 -07001115 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001116 break;
1117 case XFS_DINODE_FMT_BTREE:
Brian Foster825d75cd2018-07-11 22:26:21 -07001118 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001119 break;
1120 default:
1121 error = 0;
1122 break;
1123 }
1124 if (logflags)
1125 xfs_trans_log_inode(tp, ip, logflags);
1126 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07001127 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001128 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1129 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001130 bool log_sb = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001131
1132 spin_lock(&mp->m_sb_lock);
1133 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1134 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001135 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001136 }
1137 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1138 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001139 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001140 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001141 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001142 if (log_sb)
1143 xfs_log_sb(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001144 }
1145
Christoph Hellwig70393312015-06-04 13:48:08 +10001146 error = xfs_trans_commit(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001147 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001148 return error;
1149
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001150trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001151 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001152 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001153 return error;
1154}
1155
1156/*
1157 * Internal and external extent tree search functions.
1158 */
1159
1160/*
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001161 * Read in extents from a btree-format inode.
Dave Chinner9e5987a72013-02-25 12:31:26 +11001162 */
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001163int
1164xfs_iread_extents(
1165 struct xfs_trans *tp,
1166 struct xfs_inode *ip,
1167 int whichfork)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001168{
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001169 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwige8e0e172017-10-19 11:06:29 -07001170 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001171 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1172 xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
1173 struct xfs_btree_block *block = ifp->if_broot;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001174 struct xfs_iext_cursor icur;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -07001175 struct xfs_bmbt_irec new;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001176 xfs_fsblock_t bno;
1177 struct xfs_buf *bp;
1178 xfs_extnum_t i, j;
1179 int level;
1180 __be64 *pp;
1181 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001182
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001183 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1184
1185 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1186 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1187 return -EFSCORRUPTED;
1188 }
1189
Dave Chinner9e5987a72013-02-25 12:31:26 +11001190 /*
1191 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1192 */
1193 level = be16_to_cpu(block->bb_level);
Brian Foster6958d112019-03-17 15:21:49 -07001194 if (unlikely(level == 0)) {
1195 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1196 return -EFSCORRUPTED;
1197 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001198 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1199 bno = be64_to_cpu(*pp);
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -08001200
Dave Chinner9e5987a72013-02-25 12:31:26 +11001201 /*
1202 * Go down the tree until leaf level is reached, following the first
1203 * pointer (leftmost) at each level.
1204 */
1205 while (level-- > 0) {
Eric Sandeenf5b999c2019-06-12 09:00:00 -07001206 error = xfs_btree_read_bufl(mp, tp, bno, &bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +11001207 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1208 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001209 goto out;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001210 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001211 if (level == 0)
1212 break;
1213 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1214 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001215 XFS_WANT_CORRUPTED_GOTO(mp,
Darrick J. Wong59f6fec2018-01-08 10:51:00 -08001216 xfs_verify_fsbno(mp, bno), out_brelse);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001217 xfs_trans_brelse(tp, bp);
1218 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001219
Dave Chinner9e5987a72013-02-25 12:31:26 +11001220 /*
1221 * Here with bp and block set to the leftmost leaf node in the tree.
1222 */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001223 i = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001224 xfs_iext_first(ifp, &icur);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001225
Dave Chinner9e5987a72013-02-25 12:31:26 +11001226 /*
1227 * Loop over all leaf nodes. Copy information to the extent records.
1228 */
1229 for (;;) {
1230 xfs_bmbt_rec_t *frp;
1231 xfs_fsblock_t nextbno;
1232 xfs_extnum_t num_recs;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001233
1234 num_recs = xfs_btree_get_numrecs(block);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001235 if (unlikely(i + num_recs > nextents)) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001236 xfs_warn(ip->i_mount,
1237 "corrupt dinode %Lu, (btree extents).",
1238 (unsigned long long) ip->i_ino);
Darrick J. Wong90a58f92018-03-23 10:06:52 -07001239 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1240 __func__, block, sizeof(*block),
1241 __this_address);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001242 error = -EFSCORRUPTED;
1243 goto out_brelse;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001244 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001245 /*
1246 * Read-ahead the next leaf block, if any.
1247 */
1248 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1249 if (nextbno != NULLFSBLOCK)
1250 xfs_btree_reada_bufl(mp, nextbno, 1,
1251 &xfs_bmbt_buf_ops);
1252 /*
1253 * Copy records into the extent records.
1254 */
1255 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Christoph Hellwig6bdcf262017-11-03 10:34:46 -07001256 for (j = 0; j < num_recs; j++, frp++, i++) {
Darrick J. Wong30b09842018-03-23 10:06:52 -07001257 xfs_failaddr_t fa;
1258
Christoph Hellwigdac9c9b2017-11-03 10:34:47 -07001259 xfs_bmbt_disk_get_all(frp, &new);
Darrick J. Wong30b09842018-03-23 10:06:52 -07001260 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1261 if (fa) {
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001262 error = -EFSCORRUPTED;
Darrick J. Wong30b09842018-03-23 10:06:52 -07001263 xfs_inode_verifier_error(ip, error,
1264 "xfs_iread_extents(2)",
1265 frp, sizeof(*frp), fa);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001266 goto out_brelse;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001267 }
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001268 xfs_iext_insert(ip, &icur, &new, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001269 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
1270 xfs_iext_next(ifp, &icur);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001271 }
1272 xfs_trans_brelse(tp, bp);
1273 bno = nextbno;
1274 /*
1275 * If we've reached the end, stop.
1276 */
1277 if (bno == NULLFSBLOCK)
1278 break;
Eric Sandeenf5b999c2019-06-12 09:00:00 -07001279 error = xfs_btree_read_bufl(mp, tp, bno, &bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +11001280 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1281 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001282 goto out;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001283 block = XFS_BUF_TO_BLOCK(bp);
1284 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001285
1286 if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) {
1287 error = -EFSCORRUPTED;
1288 goto out;
1289 }
Eric Sandeen5d829302016-11-08 12:59:42 +11001290 ASSERT(i == xfs_iext_count(ifp));
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001291
1292 ifp->if_flags |= XFS_IFEXTENTS;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001293 return 0;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001294
1295out_brelse:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001296 xfs_trans_brelse(tp, bp);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001297out:
1298 xfs_iext_destroy(ifp);
1299 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001300}
1301
Dave Chinner9e5987a72013-02-25 12:31:26 +11001302/*
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001303 * Returns the relative block number of the first unused block(s) in the given
1304 * fork with at least "len" logically contiguous blocks free. This is the
1305 * lowest-address hole if the fork has holes, else the first block past the end
1306 * of fork. Return 0 if the fork is currently local (in-inode).
Dave Chinner9e5987a72013-02-25 12:31:26 +11001307 */
1308int /* error */
1309xfs_bmap_first_unused(
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001310 struct xfs_trans *tp, /* transaction pointer */
1311 struct xfs_inode *ip, /* incore inode */
1312 xfs_extlen_t len, /* size of hole to find */
1313 xfs_fileoff_t *first_unused, /* unused block */
1314 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001315{
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001316 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1317 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001318 struct xfs_iext_cursor icur;
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001319 xfs_fileoff_t lastaddr = 0;
1320 xfs_fileoff_t lowest, max;
1321 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001322
1323 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1324 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1325 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001326
Dave Chinner9e5987a72013-02-25 12:31:26 +11001327 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1328 *first_unused = 0;
1329 return 0;
1330 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001331
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001332 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1333 error = xfs_iread_extents(tp, ip, whichfork);
1334 if (error)
1335 return error;
1336 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001337
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001338 lowest = max = *first_unused;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001339 for_each_xfs_iext(ifp, &icur, &got) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001340 /*
1341 * See if the hole before this extent will work.
1342 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001343 if (got.br_startoff >= lowest + len &&
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001344 got.br_startoff - max >= len)
1345 break;
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001346 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001347 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1348 }
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001349
Dave Chinner9e5987a72013-02-25 12:31:26 +11001350 *first_unused = max;
1351 return 0;
1352}
1353
1354/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001355 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a72013-02-25 12:31:26 +11001356 * last_block (input value) in the file.
1357 * This is not based on i_size, it is based on the extent records.
1358 * Returns 0 for local files, as they do not have extent records.
1359 */
1360int /* error */
1361xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001362 struct xfs_trans *tp, /* transaction pointer */
1363 struct xfs_inode *ip, /* incore inode */
1364 xfs_fileoff_t *last_block, /* last block */
1365 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001366{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001367 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1368 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001369 struct xfs_iext_cursor icur;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001370 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001371
Christoph Hellwig86685f72016-11-24 11:39:38 +11001372 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1373 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001374 *last_block = 0;
1375 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001376 case XFS_DINODE_FMT_BTREE:
1377 case XFS_DINODE_FMT_EXTENTS:
1378 break;
1379 default:
1380 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001381 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001382
1383 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1384 error = xfs_iread_extents(tp, ip, whichfork);
1385 if (error)
1386 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001387 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001388
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001389 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
Christoph Hellwigdc560152017-10-23 16:32:39 -07001390 *last_block = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001391 return 0;
1392}
1393
Dave Chinner68988112013-08-12 20:49:42 +10001394int
Dave Chinner9e5987a72013-02-25 12:31:26 +11001395xfs_bmap_last_extent(
1396 struct xfs_trans *tp,
1397 struct xfs_inode *ip,
1398 int whichfork,
1399 struct xfs_bmbt_irec *rec,
1400 int *is_empty)
1401{
1402 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001403 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001404 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001405
1406 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1407 error = xfs_iread_extents(tp, ip, whichfork);
1408 if (error)
1409 return error;
1410 }
1411
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001412 xfs_iext_last(ifp, &icur);
1413 if (!xfs_iext_get_extent(ifp, &icur, rec))
Dave Chinner9e5987a72013-02-25 12:31:26 +11001414 *is_empty = 1;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001415 else
1416 *is_empty = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001417 return 0;
1418}
1419
1420/*
1421 * Check the last inode extent to determine whether this allocation will result
1422 * in blocks being allocated at the end of the file. When we allocate new data
1423 * blocks at the end of the file which do not start at the previous data block,
1424 * we will try to align the new blocks at stripe unit boundaries.
1425 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001426 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a72013-02-25 12:31:26 +11001427 * at, or past the EOF.
1428 */
1429STATIC int
1430xfs_bmap_isaeof(
1431 struct xfs_bmalloca *bma,
1432 int whichfork)
1433{
1434 struct xfs_bmbt_irec rec;
1435 int is_empty;
1436 int error;
1437
Thomas Meyer749f24f2017-10-09 11:38:54 -07001438 bma->aeof = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001439 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1440 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001441 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001442 return error;
1443
Dave Chinner6e708bc2013-11-22 10:41:16 +11001444 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001445 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001446 return 0;
1447 }
1448
Dave Chinner9e5987a72013-02-25 12:31:26 +11001449 /*
1450 * Check if we are allocation or past the last extent, or at least into
1451 * the last delayed allocated extent.
1452 */
1453 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1454 (bma->offset >= rec.br_startoff &&
1455 isnullstartblock(rec.br_startblock));
1456 return 0;
1457}
1458
1459/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001460 * Returns the file-relative block number of the first block past eof in
1461 * the file. This is not based on i_size, it is based on the extent records.
1462 * Returns 0 for local files, as they do not have extent records.
1463 */
1464int
1465xfs_bmap_last_offset(
Dave Chinner9e5987a72013-02-25 12:31:26 +11001466 struct xfs_inode *ip,
1467 xfs_fileoff_t *last_block,
1468 int whichfork)
1469{
1470 struct xfs_bmbt_irec rec;
1471 int is_empty;
1472 int error;
1473
1474 *last_block = 0;
1475
1476 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1477 return 0;
1478
1479 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1480 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001481 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001482
1483 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1484 if (error || is_empty)
1485 return error;
1486
1487 *last_block = rec.br_startoff + rec.br_blockcount;
1488 return 0;
1489}
1490
1491/*
1492 * Returns whether the selected fork of the inode has exactly one
1493 * block or not. For the data fork we check this matches di_size,
1494 * implying the file's range is 0..bsize-1.
1495 */
1496int /* 1=>1 block, 0=>otherwise */
1497xfs_bmap_one_block(
1498 xfs_inode_t *ip, /* incore inode */
1499 int whichfork) /* data or attr fork */
1500{
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07001501 struct xfs_ifork *ifp; /* inode fork pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001502 int rval; /* return value */
1503 xfs_bmbt_irec_t s; /* internal version of extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001504 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001505
1506#ifndef DEBUG
1507 if (whichfork == XFS_DATA_FORK)
1508 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1509#endif /* !DEBUG */
1510 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1511 return 0;
1512 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1513 return 0;
1514 ifp = XFS_IFORK_PTR(ip, whichfork);
1515 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001516 xfs_iext_first(ifp, &icur);
1517 xfs_iext_get_extent(ifp, &icur, &s);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001518 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1519 if (rval && whichfork == XFS_DATA_FORK)
1520 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1521 return rval;
1522}
1523
1524/*
1525 * Extent tree manipulation functions used during allocation.
1526 */
1527
1528/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001529 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 */
1531STATIC int /* error */
1532xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001533 struct xfs_bmalloca *bma,
1534 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001536 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 int i; /* temp state */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07001539 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 xfs_fileoff_t new_endoff; /* end offset of new entry */
1541 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1542 /* left is 0, right is 1, prev is 2 */
1543 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07001544 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001545 xfs_filblks_t da_new; /* new count del alloc blocks used */
1546 xfs_filblks_t da_old; /* old count del alloc blocks used */
1547 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001549 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001550 xfs_extnum_t *nextents;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001551 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001553 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001554 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001555 ASSERT(whichfork != XFS_ATTR_FORK);
1556 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1557 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001558
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001559 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001560 ASSERT(!bma->cur ||
1561 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001562
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001563 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565#define LEFT r[0]
1566#define RIGHT r[1]
1567#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 /*
1570 * Set up a bunch of variables to make the tests simpler.
1571 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001572 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001574 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 ASSERT(PREV.br_startoff <= new->br_startoff);
1576 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001577
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001578 da_old = startblockval(PREV.br_startblock);
1579 da_new = 0;
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 /*
1582 * Set flags determining what part of the previous delayed allocation
1583 * extent is being replaced by a real allocation.
1584 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001585 if (PREV.br_startoff == new->br_startoff)
1586 state |= BMAP_LEFT_FILLING;
1587 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1588 state |= BMAP_RIGHT_FILLING;
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 /*
1591 * Check and set flags if this segment has a left neighbor.
1592 * Don't set contiguous if the combined extent would be too large.
1593 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001594 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001595 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001596 if (isnullstartblock(LEFT.br_startblock))
1597 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001599
1600 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1601 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1602 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1603 LEFT.br_state == new->br_state &&
1604 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1605 state |= BMAP_LEFT_CONTIG;
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 /*
1608 * Check and set flags if this segment has a right neighbor.
1609 * Don't set contiguous if the combined extent would be too large.
1610 * Also check for all-three-contiguous being too large.
1611 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001612 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001613 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001614 if (isnullstartblock(RIGHT.br_startblock))
1615 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001617
1618 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1619 new_endoff == RIGHT.br_startoff &&
1620 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1621 new->br_state == RIGHT.br_state &&
1622 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1623 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1624 BMAP_RIGHT_FILLING)) !=
1625 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1626 BMAP_RIGHT_FILLING) ||
1627 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1628 <= MAXEXTLEN))
1629 state |= BMAP_RIGHT_CONTIG;
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 error = 0;
1632 /*
1633 * Switch out based on the FILLING and CONTIG state bits.
1634 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001635 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1636 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1637 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1638 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 /*
1640 * Filling in all of a previously delayed allocation extent.
1641 * The left and right neighbors are both contiguous with new.
1642 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001643 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001644
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001645 xfs_iext_remove(bma->ip, &bma->icur, state);
1646 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001647 xfs_iext_prev(ifp, &bma->icur);
1648 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001649 (*nextents)--;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001650
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001651 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1653 else {
1654 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001655 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001656 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001658 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001659 error = xfs_btree_delete(bma->cur, &i);
1660 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001662 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001663 error = xfs_btree_decrement(bma->cur, 0, &i);
1664 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001666 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001667 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001668 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 goto done;
1670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 break;
1672
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001673 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 /*
1675 * Filling in all of a previously delayed allocation extent.
1676 * The left neighbor is contiguous, the right is not.
1677 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001678 old = LEFT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001679 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001680
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001681 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001682 xfs_iext_prev(ifp, &bma->icur);
1683 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001684
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001685 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 rval = XFS_ILOG_DEXT;
1687 else {
1688 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001689 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001690 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001692 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001693 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001694 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 goto done;
1696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 break;
1698
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001699 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 /*
1701 * Filling in all of a previously delayed allocation extent.
Dave Chinner9230a0b2018-11-19 22:50:08 -08001702 * The right neighbor is contiguous, the left is not. Take care
1703 * with delay -> unwritten extent allocation here because the
1704 * delalloc record we are overwriting is always written.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001706 PREV.br_startblock = new->br_startblock;
1707 PREV.br_blockcount += RIGHT.br_blockcount;
Dave Chinner9230a0b2018-11-19 22:50:08 -08001708 PREV.br_state = new->br_state;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001709
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001710 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001711 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001712 xfs_iext_prev(ifp, &bma->icur);
1713 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001714
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001715 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 rval = XFS_ILOG_DEXT;
1717 else {
1718 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001719 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001720 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001722 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001723 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001724 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 goto done;
1726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 break;
1728
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001729 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 /*
1731 * Filling in all of a previously delayed allocation extent.
1732 * Neither the left nor right neighbors are contiguous with
1733 * the new one.
1734 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001735 PREV.br_startblock = new->br_startblock;
1736 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001737 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001738
Darrick J. Wong60b49842016-10-03 09:11:34 -07001739 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001740 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1742 else {
1743 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001744 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001745 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001747 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001748 error = xfs_btree_insert(bma->cur, &i);
1749 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001751 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 break;
1754
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001755 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 /*
1757 * Filling in the first part of a previous delayed allocation.
1758 * The left neighbor is contiguous.
1759 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001760 old = LEFT;
1761 temp = PREV.br_blockcount - new->br_blockcount;
1762 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1763 startblockval(PREV.br_startblock));
1764
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001765 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001766
Christoph Hellwigbf999712017-11-03 10:34:38 -07001767 PREV.br_blockcount = temp;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001768 PREV.br_startoff += new->br_blockcount;
1769 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwig0d045542017-11-03 10:34:39 -07001770
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001771 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1772 xfs_iext_prev(ifp, &bma->icur);
1773 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001774
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001775 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 rval = XFS_ILOG_DEXT;
1777 else {
1778 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001779 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001780 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001782 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001783 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001784 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 goto done;
1786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 break;
1788
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001789 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 /*
1791 * Filling in the first part of a previous delayed allocation.
1792 * The left neighbor is not contiguous.
1793 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001794 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001795 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001796 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1798 else {
1799 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001800 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001801 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001803 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001804 error = xfs_btree_insert(bma->cur, &i);
1805 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001807 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
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) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001821 (bma->cur ? bma->cur->bc_private.b.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;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001848 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001849 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001850 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 goto done;
1852 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001853
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001854 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001855 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001856 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001857
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001858 PREV.br_blockcount = temp;
1859 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001860
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001861 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1862 xfs_iext_next(ifp, &bma->icur);
1863 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 break;
1865
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001866 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 /*
1868 * Filling in the last part of a previous delayed allocation.
1869 * The right neighbor is not contiguous.
1870 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001871 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001872 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001873 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1875 else {
1876 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001877 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001878 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001880 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001881 error = xfs_btree_insert(bma->cur, &i);
1882 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001884 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001886
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001887 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001888 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001889 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 rval |= tmp_rval;
1891 if (error)
1892 goto done;
1893 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001894
1895 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001896 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001897 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001898 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001899
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001900 PREV.br_startblock = nullstartblock(da_new);
1901 PREV.br_blockcount = temp;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001902 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001903 xfs_iext_next(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 break;
1905
1906 case 0:
1907 /*
1908 * Filling in the middle part of a previous delayed allocation.
1909 * Contiguity is impossible here.
1910 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001911 *
1912 * We start with a delayed allocation:
1913 *
1914 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1915 * PREV @ idx
1916 *
1917 * and we are allocating:
1918 * +rrrrrrrrrrrrrrrrr+
1919 * new
1920 *
1921 * and we set it up for insertion as:
1922 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1923 * new
1924 * PREV @ idx LEFT RIGHT
1925 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001927 old = PREV;
1928
1929 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001930 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001931
1932 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001933 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001934 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001935 RIGHT.br_blockcount =
1936 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1937 RIGHT.br_startblock =
1938 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1939 RIGHT.br_blockcount));
1940
1941 /* truncate PREV */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001942 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1943 PREV.br_startblock =
1944 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1945 PREV.br_blockcount));
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001946 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001947
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001948 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001949 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1950 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001951 (*nextents)++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001952
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001953 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1955 else {
1956 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001957 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001958 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001960 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001961 error = xfs_btree_insert(bma->cur, &i);
1962 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001964 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001966
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001967 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001968 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001969 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 rval |= tmp_rval;
1971 if (error)
1972 goto done;
1973 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001974
1975 da_new = startblockval(PREV.br_startblock) +
1976 startblockval(RIGHT.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 break;
1978
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001979 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1980 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1981 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1982 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1983 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1984 case BMAP_LEFT_CONTIG:
1985 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 /*
1987 * These cases are all impossible.
1988 */
1989 ASSERT(0);
1990 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001991
Darrick J. Wong95eb3082018-05-09 10:02:32 -07001992 /* add reverse mapping unless caller opted out */
1993 if (!(bma->flags & XFS_BMAPI_NORMAP)) {
Brian Foster0f37d172018-08-01 07:20:34 -07001994 error = xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
Darrick J. Wong95eb3082018-05-09 10:02:32 -07001995 if (error)
1996 goto done;
1997 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10001998
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001999 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002000 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002001 int tmp_logflags; /* partial log flag return val */
2002
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002003 ASSERT(bma->cur == NULL);
2004 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07002005 &bma->cur, da_old > 0, &tmp_logflags,
2006 whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002007 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002008 if (error)
2009 goto done;
2010 }
2011
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07002012 if (da_new != da_old)
2013 xfs_mod_delalloc(mp, (int64_t)da_new - da_old);
2014
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002015 if (bma->cur) {
2016 da_new += bma->cur->bc_private.b.allocated;
2017 bma->cur->bc_private.b.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002018 }
2019
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002020 /* adjust for changes in reserved delayed indirect blocks */
2021 if (da_new != da_old) {
2022 ASSERT(state == 0 || da_new < da_old);
2023 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2024 false);
2025 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002026
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002027 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002029 if (whichfork != XFS_COW_FORK)
2030 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 return error;
2032#undef LEFT
2033#undef RIGHT
2034#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036
2037/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002038 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 */
Christoph Hellwig26b91c72019-02-18 09:38:48 -08002040int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002042 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002044 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002045 struct xfs_iext_cursor *icur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002047 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002048 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 int i; /* temp state */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07002053 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2056 /* left is 0, right is 1, prev is 2 */
2057 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002058 int state = xfs_bmap_fork_to_state(whichfork);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002059 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002060 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002062 *logflagsp = 0;
2063
2064 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002065 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002066
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002067 ASSERT(!isnullstartblock(new->br_startblock));
2068
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002069 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071#define LEFT r[0]
2072#define RIGHT r[1]
2073#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 /*
2076 * Set up a bunch of variables to make the tests simpler.
2077 */
2078 error = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002079 xfs_iext_get_extent(ifp, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002080 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 new_endoff = new->br_startoff + new->br_blockcount;
2082 ASSERT(PREV.br_startoff <= new->br_startoff);
2083 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 /*
2086 * Set flags determining what part of the previous oldext allocation
2087 * extent is being replaced by a newext allocation.
2088 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002089 if (PREV.br_startoff == new->br_startoff)
2090 state |= BMAP_LEFT_FILLING;
2091 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2092 state |= BMAP_RIGHT_FILLING;
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 /*
2095 * Check and set flags if this segment has a left neighbor.
2096 * Don't set contiguous if the combined extent would be too large.
2097 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002098 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002099 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002100 if (isnullstartblock(LEFT.br_startblock))
2101 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002103
2104 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2105 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2106 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002107 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002108 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2109 state |= BMAP_LEFT_CONTIG;
2110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 /*
2112 * Check and set flags if this segment has a right neighbor.
2113 * Don't set contiguous if the combined extent would be too large.
2114 * Also check for all-three-contiguous being too large.
2115 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002116 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002117 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002118 if (isnullstartblock(RIGHT.br_startblock))
2119 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002121
2122 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2123 new_endoff == RIGHT.br_startoff &&
2124 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002125 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002126 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2127 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2128 BMAP_RIGHT_FILLING)) !=
2129 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2130 BMAP_RIGHT_FILLING) ||
2131 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2132 <= MAXEXTLEN))
2133 state |= BMAP_RIGHT_CONTIG;
2134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 /*
2136 * Switch out based on the FILLING and CONTIG state bits.
2137 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002138 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2139 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2140 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2141 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 /*
2143 * Setting all of a previous oldext extent to newext.
2144 * The left and right neighbors are both contiguous with new.
2145 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002146 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002147
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002148 xfs_iext_remove(ip, icur, state);
2149 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002150 xfs_iext_prev(ifp, icur);
2151 xfs_iext_update_extent(ip, state, icur, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002152 XFS_IFORK_NEXT_SET(ip, whichfork,
2153 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 if (cur == NULL)
2155 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2156 else {
2157 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002158 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2159 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002161 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002162 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002164 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002165 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002167 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002168 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002170 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002171 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002173 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002174 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002175 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 goto done;
2177 }
2178 break;
2179
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002180 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 /*
2182 * Setting all of a previous oldext extent to newext.
2183 * The left neighbor is contiguous, the right is not.
2184 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002185 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002186
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002187 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002188 xfs_iext_prev(ifp, icur);
2189 xfs_iext_update_extent(ip, state, icur, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002190 XFS_IFORK_NEXT_SET(ip, whichfork,
2191 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 if (cur == NULL)
2193 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2194 else {
2195 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002196 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2197 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002199 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002200 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002202 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002203 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002205 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002206 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002207 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 goto done;
2209 }
2210 break;
2211
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002212 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 /*
2214 * Setting all of a previous oldext extent to newext.
2215 * The right neighbor is contiguous, the left is not.
2216 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002217 PREV.br_blockcount += RIGHT.br_blockcount;
2218 PREV.br_state = new->br_state;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002219
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002220 xfs_iext_next(ifp, icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002221 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002222 xfs_iext_prev(ifp, icur);
2223 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002224
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002225 XFS_IFORK_NEXT_SET(ip, whichfork,
2226 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (cur == NULL)
2228 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2229 else {
2230 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002231 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2232 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002234 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002235 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002237 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002238 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002240 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002241 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002242 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 goto done;
2244 }
2245 break;
2246
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002247 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 /*
2249 * Setting all of a previous oldext extent to newext.
2250 * Neither the left nor right neighbors are contiguous with
2251 * the new one.
2252 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002253 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002254 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (cur == NULL)
2257 rval = XFS_ILOG_DEXT;
2258 else {
2259 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002260 error = xfs_bmbt_lookup_eq(cur, new, &i);
2261 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002263 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002264 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002265 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 goto done;
2267 }
2268 break;
2269
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002270 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 /*
2272 * Setting the first part of a previous oldext extent to newext.
2273 * The left neighbor is contiguous.
2274 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002275 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002276
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002277 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002278 PREV.br_startoff += new->br_blockcount;
2279 PREV.br_startblock += new->br_blockcount;
2280 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002281
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002282 xfs_iext_update_extent(ip, state, icur, &PREV);
2283 xfs_iext_prev(ifp, icur);
2284 xfs_iext_update_extent(ip, state, icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 if (cur == NULL)
2287 rval = XFS_ILOG_DEXT;
2288 else {
2289 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002290 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002291 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002293 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002294 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002295 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002297 error = xfs_btree_decrement(cur, 0, &i);
2298 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002300 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002301 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 goto done;
2303 }
2304 break;
2305
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002306 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 /*
2308 * Setting the first part of a previous oldext extent to newext.
2309 * The left neighbor is not contiguous.
2310 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002311 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002312 PREV.br_startoff += new->br_blockcount;
2313 PREV.br_startblock += new->br_blockcount;
2314 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002315
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002316 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002317 xfs_iext_insert(ip, icur, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002318 XFS_IFORK_NEXT_SET(ip, whichfork,
2319 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 if (cur == NULL)
2321 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2322 else {
2323 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002324 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002325 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002327 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002328 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002329 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 goto done;
2331 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002332 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002334 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
2336 break;
2337
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002338 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 /*
2340 * Setting the last part of a previous oldext extent to newext.
2341 * The right neighbor is contiguous with the new allocation.
2342 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002343 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002344 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002345
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002346 RIGHT.br_startoff = new->br_startoff;
2347 RIGHT.br_startblock = new->br_startblock;
2348 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002349
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002350 xfs_iext_update_extent(ip, state, icur, &PREV);
2351 xfs_iext_next(ifp, icur);
2352 xfs_iext_update_extent(ip, state, icur, &RIGHT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (cur == NULL)
2355 rval = XFS_ILOG_DEXT;
2356 else {
2357 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002358 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002359 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002361 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002362 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002363 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002365 error = xfs_btree_increment(cur, 0, &i);
2366 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002368 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002369 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 goto done;
2371 }
2372 break;
2373
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002374 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 /*
2376 * Setting the last part of a previous oldext extent to newext.
2377 * The right neighbor is not contiguous.
2378 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002379 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002380 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002381
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002382 xfs_iext_update_extent(ip, state, icur, &PREV);
2383 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002384 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002385
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002386 XFS_IFORK_NEXT_SET(ip, whichfork,
2387 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if (cur == NULL)
2389 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2390 else {
2391 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002392 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002393 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002395 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002396 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002397 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002399 error = xfs_bmbt_lookup_eq(cur, new, &i);
2400 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002402 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002403 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002405 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
2407 break;
2408
2409 case 0:
2410 /*
2411 * Setting the middle part of a previous oldext extent to
2412 * newext. Contiguity is impossible here.
2413 * One extent becomes three extents.
2414 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002415 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002416 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 r[0] = *new;
2419 r[1].br_startoff = new_endoff;
2420 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002421 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002423 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002424
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002425 xfs_iext_update_extent(ip, state, icur, &PREV);
2426 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002427 xfs_iext_insert(ip, icur, &r[1], state);
2428 xfs_iext_insert(ip, icur, &r[0], state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002429
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002430 XFS_IFORK_NEXT_SET(ip, whichfork,
2431 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 if (cur == NULL)
2433 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2434 else {
2435 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002436 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002437 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002439 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002441 error = xfs_bmbt_update(cur, &r[1]);
2442 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 goto done;
2444 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002446 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002448 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002449 /*
2450 * Reset the cursor to the position of the new extent
2451 * we are about to insert as we can't trust it after
2452 * the previous insert.
2453 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002454 error = xfs_bmbt_lookup_eq(cur, new, &i);
2455 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002457 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002459 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002461 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 }
2463 break;
2464
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002465 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2466 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2467 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2468 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2469 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2470 case BMAP_LEFT_CONTIG:
2471 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 /*
2473 * These cases are all impossible.
2474 */
2475 ASSERT(0);
2476 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002477
Darrick J. Wong9c194642016-08-03 12:16:05 +10002478 /* update reverse mappings */
Brian Foster0f37d172018-08-01 07:20:34 -07002479 error = xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002480 if (error)
2481 goto done;
2482
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002483 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002484 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002485 int tmp_logflags; /* partial log flag return val */
2486
2487 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07002488 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2489 &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002490 *logflagsp |= tmp_logflags;
2491 if (error)
2492 goto done;
2493 }
2494
2495 /* clear out the allocated field, done with it now in any case. */
2496 if (cur) {
2497 cur->bc_private.b.allocated = 0;
2498 *curp = cur;
2499 }
2500
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002501 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002503 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 return error;
2505#undef LEFT
2506#undef RIGHT
2507#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
2510/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002511 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002513STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514xfs_bmap_add_extent_hole_delay(
2515 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002516 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002517 struct xfs_iext_cursor *icur,
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002518 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07002520 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2522 xfs_filblks_t newlen=0; /* new indirect size */
2523 xfs_filblks_t oldlen=0; /* old indirect size */
2524 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002525 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002526 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002528 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002529 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 /*
2532 * Check and set flags if this segment has a left neighbor
2533 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002534 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002535 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002536 if (isnullstartblock(left.br_startblock))
2537 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 /*
2541 * Check and set flags if the current (right) segment exists.
2542 * If it doesn't exist, we're converting the hole at end-of-file.
2543 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002544 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002545 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002546 if (isnullstartblock(right.br_startblock))
2547 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 /*
2551 * Set contiguity flags on the left and right neighbors.
2552 * Don't let extents get too large, even if the pieces are contiguous.
2553 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002554 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2555 left.br_startoff + left.br_blockcount == new->br_startoff &&
2556 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2557 state |= BMAP_LEFT_CONTIG;
2558
2559 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2560 new->br_startoff + new->br_blockcount == right.br_startoff &&
2561 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2562 (!(state & BMAP_LEFT_CONTIG) ||
2563 (left.br_blockcount + new->br_blockcount +
2564 right.br_blockcount <= MAXEXTLEN)))
2565 state |= BMAP_RIGHT_CONTIG;
2566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 /*
2568 * Switch out based on the contiguity flags.
2569 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002570 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2571 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 /*
2573 * New allocation is contiguous with delayed allocations
2574 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002575 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 */
2577 temp = left.br_blockcount + new->br_blockcount +
2578 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002579
Eric Sandeen9d87c312009-01-14 23:22:07 -06002580 oldlen = startblockval(left.br_startblock) +
2581 startblockval(new->br_startblock) +
2582 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002583 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2584 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002585 left.br_startblock = nullstartblock(newlen);
2586 left.br_blockcount = temp;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002587
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002588 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002589 xfs_iext_prev(ifp, icur);
2590 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 break;
2592
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002593 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 /*
2595 * New allocation is contiguous with a delayed allocation
2596 * on the left.
2597 * Merge the new allocation with the left neighbor.
2598 */
2599 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002600
Eric Sandeen9d87c312009-01-14 23:22:07 -06002601 oldlen = startblockval(left.br_startblock) +
2602 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002603 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2604 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002605 left.br_blockcount = temp;
2606 left.br_startblock = nullstartblock(newlen);
Christoph Hellwig41d196f2017-11-03 10:34:39 -07002607
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002608 xfs_iext_prev(ifp, icur);
2609 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 break;
2611
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002612 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 /*
2614 * New allocation is contiguous with a delayed allocation
2615 * on the right.
2616 * Merge the new allocation with the right neighbor.
2617 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002619 oldlen = startblockval(new->br_startblock) +
2620 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002621 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2622 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002623 right.br_startoff = new->br_startoff;
2624 right.br_startblock = nullstartblock(newlen);
2625 right.br_blockcount = temp;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002626 xfs_iext_update_extent(ip, state, icur, &right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 break;
2628
2629 case 0:
2630 /*
2631 * New allocation is not contiguous with another
2632 * delayed allocation.
2633 * Insert a new entry.
2634 */
2635 oldlen = newlen = 0;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002636 xfs_iext_insert(ip, icur, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 break;
2638 }
2639 if (oldlen != newlen) {
2640 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002641 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2642 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 /*
2644 * Nothing to do for disk quota accounting here.
2645 */
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07002646 xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648}
2649
2650/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002651 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 */
2653STATIC int /* error */
2654xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002655 struct xfs_trans *tp,
2656 struct xfs_inode *ip,
2657 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002658 struct xfs_iext_cursor *icur,
Christoph Hellwig6d045582017-04-11 16:45:54 -07002659 struct xfs_btree_cur **curp,
2660 struct xfs_bmbt_irec *new,
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002661 int *logflagsp,
2662 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002664 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2665 struct xfs_mount *mp = ip->i_mount;
2666 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2670 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002671 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002672 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002673 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002675 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002676 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002677
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002678 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 /*
2681 * Check and set flags if this segment has a left neighbor.
2682 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002683 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002684 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002685 if (isnullstartblock(left.br_startblock))
2686 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 /*
2690 * Check and set flags if this segment has a current value.
2691 * Not true if we're inserting into the "hole" at eof.
2692 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002693 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002694 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002695 if (isnullstartblock(right.br_startblock))
2696 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002698
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 /*
2700 * We're inserting a real allocation between "left" and "right".
2701 * Set the contiguity flags. Don't let extents get too large.
2702 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002703 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2704 left.br_startoff + left.br_blockcount == new->br_startoff &&
2705 left.br_startblock + left.br_blockcount == new->br_startblock &&
2706 left.br_state == new->br_state &&
2707 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2708 state |= BMAP_LEFT_CONTIG;
2709
2710 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2711 new->br_startoff + new->br_blockcount == right.br_startoff &&
2712 new->br_startblock + new->br_blockcount == right.br_startblock &&
2713 new->br_state == right.br_state &&
2714 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2715 (!(state & BMAP_LEFT_CONTIG) ||
2716 left.br_blockcount + new->br_blockcount +
2717 right.br_blockcount <= MAXEXTLEN))
2718 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002720 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 /*
2722 * Select which case we're in here, and implement it.
2723 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002724 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2725 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 /*
2727 * New allocation is contiguous with real allocations on the
2728 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002729 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002731 left.br_blockcount += new->br_blockcount + right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002732
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002733 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002734 xfs_iext_prev(ifp, icur);
2735 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002736
Christoph Hellwig6d045582017-04-11 16:45:54 -07002737 XFS_IFORK_NEXT_SET(ip, whichfork,
2738 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2739 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002740 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002741 } else {
2742 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002743 error = xfs_bmbt_lookup_eq(cur, &right, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002744 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002745 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002746 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002747 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002748 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002749 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002750 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002751 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002752 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002753 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002754 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002755 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002756 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002757 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002759 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002761 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 /*
2763 * New allocation is contiguous with a real allocation
2764 * on the left.
2765 * Merge the new allocation with the left neighbor.
2766 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002767 old = left;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002768 left.br_blockcount += new->br_blockcount;
Christoph Hellwig1d2e0082017-11-03 10:34:40 -07002769
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002770 xfs_iext_prev(ifp, icur);
2771 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002772
Christoph Hellwig6d045582017-04-11 16:45:54 -07002773 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002774 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002775 } else {
2776 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002777 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002778 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002779 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002780 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002781 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002782 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002783 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002785 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002787 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 /*
2789 * New allocation is contiguous with a real allocation
2790 * on the right.
2791 * Merge the new allocation with the right neighbor.
2792 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002793 old = right;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07002794
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002795 right.br_startoff = new->br_startoff;
2796 right.br_startblock = new->br_startblock;
2797 right.br_blockcount += new->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002798 xfs_iext_update_extent(ip, state, icur, &right);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002799
Christoph Hellwig6d045582017-04-11 16:45:54 -07002800 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002801 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002802 } else {
2803 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002804 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002805 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002806 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002807 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002808 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002809 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002810 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002812 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
2814 case 0:
2815 /*
2816 * New allocation is not contiguous with another
2817 * real allocation.
2818 * Insert a new entry.
2819 */
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002820 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002821 XFS_IFORK_NEXT_SET(ip, whichfork,
2822 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2823 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002824 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002825 } else {
2826 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002827 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002828 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002829 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002830 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002831 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002832 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002833 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002834 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002836 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002838
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002839 /* add reverse mapping unless caller opted out */
2840 if (!(flags & XFS_BMAPI_NORMAP)) {
Brian Foster0f37d172018-08-01 07:20:34 -07002841 error = xfs_rmap_map_extent(tp, ip, whichfork, new);
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002842 if (error)
2843 goto done;
2844 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10002845
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002846 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002847 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002848 int tmp_logflags; /* partial log flag return val */
2849
Christoph Hellwig6d045582017-04-11 16:45:54 -07002850 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07002851 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2852 &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002853 *logflagsp |= tmp_logflags;
2854 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002855 if (error)
2856 goto done;
2857 }
2858
2859 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002860 if (cur)
2861 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002862
Christoph Hellwig6d045582017-04-11 16:45:54 -07002863 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002864done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002865 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002866 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867}
2868
Nathan Scottdd9f4382006-01-11 15:28:28 +11002869/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11002870 * Functions used in the extent read, allocate and remove paths
2871 */
2872
2873/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002874 * Adjust the size of the new extent based on di_extsize and rt extsize.
2875 */
Dave Chinner68988112013-08-12 20:49:42 +10002876int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002877xfs_bmap_extsize_align(
2878 xfs_mount_t *mp,
2879 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2880 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2881 xfs_extlen_t extsz, /* align to this extent size */
2882 int rt, /* is this a realtime inode? */
2883 int eof, /* is extent at end-of-file? */
2884 int delay, /* creating delalloc extent? */
2885 int convert, /* overwriting unwritten extent? */
2886 xfs_fileoff_t *offp, /* in/out: aligned offset */
2887 xfs_extlen_t *lenp) /* in/out: aligned length */
2888{
2889 xfs_fileoff_t orig_off; /* original offset */
2890 xfs_extlen_t orig_alen; /* original length */
2891 xfs_fileoff_t orig_end; /* original off+len */
2892 xfs_fileoff_t nexto; /* next file offset */
2893 xfs_fileoff_t prevo; /* previous file offset */
2894 xfs_fileoff_t align_off; /* temp for offset */
2895 xfs_extlen_t align_alen; /* temp for length */
2896 xfs_extlen_t temp; /* temp for calculations */
2897
2898 if (convert)
2899 return 0;
2900
2901 orig_off = align_off = *offp;
2902 orig_alen = align_alen = *lenp;
2903 orig_end = orig_off + orig_alen;
2904
2905 /*
2906 * If this request overlaps an existing extent, then don't
2907 * attempt to perform any additional alignment.
2908 */
2909 if (!delay && !eof &&
2910 (orig_off >= gotp->br_startoff) &&
2911 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2912 return 0;
2913 }
2914
2915 /*
2916 * If the file offset is unaligned vs. the extent size
2917 * we need to align it. This will be possible unless
2918 * the file was previously written with a kernel that didn't
2919 * perform this alignment, or if a truncate shot us in the
2920 * foot.
2921 */
Dave Chinner0703a8e2018-06-08 09:54:22 -07002922 div_u64_rem(orig_off, extsz, &temp);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002923 if (temp) {
2924 align_alen += temp;
2925 align_off -= temp;
2926 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10002927
2928 /* Same adjustment for the end of the requested area. */
2929 temp = (align_alen % extsz);
2930 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11002931 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10002932
2933 /*
2934 * For large extent hint sizes, the aligned extent might be larger than
2935 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2936 * the length back under MAXEXTLEN. The outer allocation loops handle
2937 * short allocation just fine, so it is safe to do this. We only want to
2938 * do it when we are forced to, though, because it means more allocation
2939 * operations are required.
2940 */
2941 while (align_alen > MAXEXTLEN)
2942 align_alen -= extsz;
2943 ASSERT(align_alen <= MAXEXTLEN);
2944
Nathan Scottdd9f4382006-01-11 15:28:28 +11002945 /*
2946 * If the previous block overlaps with this proposed allocation
2947 * then move the start forward without adjusting the length.
2948 */
2949 if (prevp->br_startoff != NULLFILEOFF) {
2950 if (prevp->br_startblock == HOLESTARTBLOCK)
2951 prevo = prevp->br_startoff;
2952 else
2953 prevo = prevp->br_startoff + prevp->br_blockcount;
2954 } else
2955 prevo = 0;
2956 if (align_off != orig_off && align_off < prevo)
2957 align_off = prevo;
2958 /*
2959 * If the next block overlaps with this proposed allocation
2960 * then move the start back without adjusting the length,
2961 * but not before offset 0.
2962 * This may of course make the start overlap previous block,
2963 * and if we hit the offset 0 limit then the next block
2964 * can still overlap too.
2965 */
2966 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2967 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2968 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2969 nexto = gotp->br_startoff + gotp->br_blockcount;
2970 else
2971 nexto = gotp->br_startoff;
2972 } else
2973 nexto = NULLFILEOFF;
2974 if (!eof &&
2975 align_off + align_alen != orig_end &&
2976 align_off + align_alen > nexto)
2977 align_off = nexto > align_alen ? nexto - align_alen : 0;
2978 /*
2979 * If we're now overlapping the next or previous extent that
2980 * means we can't fit an extsz piece in this hole. Just move
2981 * the start forward to the first valid spot and set
2982 * the length so we hit the end.
2983 */
2984 if (align_off != orig_off && align_off < prevo)
2985 align_off = prevo;
2986 if (align_off + align_alen != orig_end &&
2987 align_off + align_alen > nexto &&
2988 nexto != NULLFILEOFF) {
2989 ASSERT(nexto > prevo);
2990 align_alen = nexto - align_off;
2991 }
2992
2993 /*
2994 * If realtime, and the result isn't a multiple of the realtime
2995 * extent size we need to remove blocks until it is.
2996 */
2997 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2998 /*
2999 * We're not covering the original request, or
3000 * we won't be able to once we fix the length.
3001 */
3002 if (orig_off < align_off ||
3003 orig_end > align_off + align_alen ||
3004 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003005 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003006 /*
3007 * Try to fix it by moving the start up.
3008 */
3009 if (align_off + temp <= orig_off) {
3010 align_alen -= temp;
3011 align_off += temp;
3012 }
3013 /*
3014 * Try to fix it by moving the end in.
3015 */
3016 else if (align_off + align_alen - temp >= orig_end)
3017 align_alen -= temp;
3018 /*
3019 * Set the start to the minimum then trim the length.
3020 */
3021 else {
3022 align_alen -= orig_off - align_off;
3023 align_off = orig_off;
3024 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3025 }
3026 /*
3027 * Result doesn't cover the request, fail it.
3028 */
3029 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003030 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003031 } else {
3032 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003033 /* see MAXEXTLEN handling above */
3034 ASSERT(orig_end <= align_off + align_alen ||
3035 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003036 }
3037
3038#ifdef DEBUG
3039 if (!eof && gotp->br_startoff != NULLFILEOFF)
3040 ASSERT(align_off + align_alen <= gotp->br_startoff);
3041 if (prevp->br_startoff != NULLFILEOFF)
3042 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3043#endif
3044
3045 *lenp = align_alen;
3046 *offp = align_off;
3047 return 0;
3048}
3049
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050#define XFS_ALLOC_GAP_UNITS 4
3051
Dave Chinner68988112013-08-12 20:49:42 +10003052void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003053xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003054 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
3056 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3058 xfs_mount_t *mp; /* mount point structure */
3059 int nullfb; /* true if ap->firstblock isn't set */
3060 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
3062#define ISVALID(x,y) \
3063 (rt ? \
3064 (x) < mp->m_sb.sb_rblocks : \
3065 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3066 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3067 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3068
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 mp = ap->ip->i_mount;
Brian Foster94c07b42018-07-11 22:26:28 -07003070 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003071 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3072 xfs_alloc_is_userdata(ap->datatype);
Brian Foster94c07b42018-07-11 22:26:28 -07003073 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3074 ap->tp->t_firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 /*
3076 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003077 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003079 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3080 !isnullstartblock(ap->prev.br_startblock) &&
3081 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3082 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003083 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 /*
3085 * Adjust for the gap between prevp and us.
3086 */
Dave Chinner3a756672011-09-18 20:40:58 +00003087 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003088 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003090 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3091 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 }
3093 /*
3094 * If not at eof, then compare the two neighbor blocks.
3095 * Figure out whether either one gives us a good starting point,
3096 * and pick the better one.
3097 */
3098 else if (!ap->eof) {
3099 xfs_fsblock_t gotbno; /* right side block number */
3100 xfs_fsblock_t gotdiff=0; /* right side difference */
3101 xfs_fsblock_t prevbno; /* left side block number */
3102 xfs_fsblock_t prevdiff=0; /* left side difference */
3103
3104 /*
3105 * If there's a previous (left) block, select a requested
3106 * start block based on it.
3107 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003108 if (ap->prev.br_startoff != NULLFILEOFF &&
3109 !isnullstartblock(ap->prev.br_startblock) &&
3110 (prevbno = ap->prev.br_startblock +
3111 ap->prev.br_blockcount) &&
3112 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 /*
3114 * Calculate gap to end of previous block.
3115 */
Dave Chinner3a756672011-09-18 20:40:58 +00003116 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003117 (ap->prev.br_startoff +
3118 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 /*
3120 * Figure the startblock based on the previous block's
3121 * end and the gap size.
3122 * Heuristic!
3123 * If the gap is large relative to the piece we're
3124 * allocating, or using it gives us an invalid block
3125 * number, then just use the end of the previous block.
3126 */
Dave Chinner3a756672011-09-18 20:40:58 +00003127 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003129 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 prevbno += adjust;
3131 else
3132 prevdiff += adjust;
3133 /*
3134 * If the firstblock forbids it, can't use it,
3135 * must use default.
3136 */
3137 if (!rt && !nullfb &&
3138 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3139 prevbno = NULLFSBLOCK;
3140 }
3141 /*
3142 * No previous block or can't follow it, just default.
3143 */
3144 else
3145 prevbno = NULLFSBLOCK;
3146 /*
3147 * If there's a following (right) block, select a requested
3148 * start block based on it.
3149 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003150 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 /*
3152 * Calculate gap to start of next block.
3153 */
Dave Chinner3a756672011-09-18 20:40:58 +00003154 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 /*
3156 * Figure the startblock based on the next block's
3157 * start and the gap size.
3158 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003159 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 /*
3161 * Heuristic!
3162 * If the gap is large relative to the piece we're
3163 * allocating, or using it gives us an invalid block
3164 * number, then just use the start of the next block
3165 * offset by our length.
3166 */
Dave Chinner3a756672011-09-18 20:40:58 +00003167 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 ISVALID(gotbno - gotdiff, gotbno))
3169 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003170 else if (ISVALID(gotbno - ap->length, gotbno)) {
3171 gotbno -= ap->length;
3172 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 } else
3174 gotdiff += adjust;
3175 /*
3176 * If the firstblock forbids it, can't use it,
3177 * must use default.
3178 */
3179 if (!rt && !nullfb &&
3180 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3181 gotbno = NULLFSBLOCK;
3182 }
3183 /*
3184 * No next block, just default.
3185 */
3186 else
3187 gotbno = NULLFSBLOCK;
3188 /*
3189 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003190 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 */
3192 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003193 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003195 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003197 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003199#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003200}
3201
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003202static int
3203xfs_bmap_longest_free_extent(
3204 struct xfs_trans *tp,
3205 xfs_agnumber_t ag,
3206 xfs_extlen_t *blen,
3207 int *notinit)
3208{
3209 struct xfs_mount *mp = tp->t_mountp;
3210 struct xfs_perag *pag;
3211 xfs_extlen_t longest;
3212 int error = 0;
3213
3214 pag = xfs_perag_get(mp, ag);
3215 if (!pag->pagf_init) {
3216 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3217 if (error)
3218 goto out;
3219
3220 if (!pag->pagf_init) {
3221 *notinit = 1;
3222 goto out;
3223 }
3224 }
3225
Eric Sandeena1f69412018-04-06 10:09:42 -07003226 longest = xfs_alloc_longest_free_extent(pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003227 xfs_alloc_min_freelist(mp, pag),
3228 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003229 if (*blen < longest)
3230 *blen = longest;
3231
3232out:
3233 xfs_perag_put(pag);
3234 return error;
3235}
3236
3237static void
3238xfs_bmap_select_minlen(
3239 struct xfs_bmalloca *ap,
3240 struct xfs_alloc_arg *args,
3241 xfs_extlen_t *blen,
3242 int notinit)
3243{
3244 if (notinit || *blen < ap->minlen) {
3245 /*
3246 * Since we did a BUF_TRYLOCK above, it is possible that
3247 * there is space for this request.
3248 */
3249 args->minlen = ap->minlen;
3250 } else if (*blen < args->maxlen) {
3251 /*
3252 * If the best seen length is less than the request length,
3253 * use the best as the minimum.
3254 */
3255 args->minlen = *blen;
3256 } else {
3257 /*
3258 * Otherwise we've seen an extent as big as maxlen, use that
3259 * as the minimum.
3260 */
3261 args->minlen = args->maxlen;
3262 }
3263}
3264
Nathan Scotta365bdd2006-03-14 13:34:16 +11003265STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003266xfs_bmap_btalloc_nullfb(
3267 struct xfs_bmalloca *ap,
3268 struct xfs_alloc_arg *args,
3269 xfs_extlen_t *blen)
3270{
3271 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003272 xfs_agnumber_t ag, startag;
3273 int notinit = 0;
3274 int error;
3275
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003276 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003277 args->total = ap->total;
3278
Christoph Hellwigc467c042010-02-15 23:34:42 +00003279 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3280 if (startag == NULLAGNUMBER)
3281 startag = ag = 0;
3282
Dave Chinner14b064c2011-01-27 12:16:28 +11003283 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003284 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3285 &notinit);
3286 if (error)
3287 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003288
Christoph Hellwigc467c042010-02-15 23:34:42 +00003289 if (++ag == mp->m_sb.sb_agcount)
3290 ag = 0;
3291 if (ag == startag)
3292 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003293 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003294
3295 xfs_bmap_select_minlen(ap, args, blen, notinit);
3296 return 0;
3297}
3298
3299STATIC int
3300xfs_bmap_btalloc_filestreams(
3301 struct xfs_bmalloca *ap,
3302 struct xfs_alloc_arg *args,
3303 xfs_extlen_t *blen)
3304{
3305 struct xfs_mount *mp = ap->ip->i_mount;
3306 xfs_agnumber_t ag;
3307 int notinit = 0;
3308 int error;
3309
3310 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3311 args->total = ap->total;
3312
3313 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3314 if (ag == NULLAGNUMBER)
3315 ag = 0;
3316
3317 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3318 if (error)
3319 return error;
3320
3321 if (*blen < args->maxlen) {
3322 error = xfs_filestream_new_ag(ap, &ag);
3323 if (error)
3324 return error;
3325
3326 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3327 &notinit);
3328 if (error)
3329 return error;
3330
3331 }
3332
3333 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003334
3335 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003336 * Set the failure fallback case to look in the selected AG as stream
3337 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003338 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003339 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003340 return 0;
3341}
3342
Darrick J. Wong751f3762018-01-25 13:58:13 -08003343/* Update all inode and quota accounting for the allocation we just did. */
3344static void
3345xfs_bmap_btalloc_accounting(
3346 struct xfs_bmalloca *ap,
3347 struct xfs_alloc_arg *args)
3348{
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003349 if (ap->flags & XFS_BMAPI_COWFORK) {
3350 /*
3351 * COW fork blocks are in-core only and thus are treated as
3352 * in-core quota reservation (like delalloc blocks) even when
3353 * converted to real blocks. The quota reservation is not
3354 * accounted to disk until blocks are remapped to the data
3355 * fork. So if these blocks were previously delalloc, we
3356 * already have quota reservation and there's nothing to do
3357 * yet.
3358 */
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003359 if (ap->wasdel) {
3360 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003361 return;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003362 }
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003363
3364 /*
3365 * Otherwise, we've allocated blocks in a hole. The transaction
3366 * has acquired in-core quota reservation for this extent.
3367 * Rather than account these as real blocks, however, we reduce
3368 * the transaction quota reservation based on the allocation.
3369 * This essentially transfers the transaction quota reservation
3370 * to that of a delalloc extent.
3371 */
3372 ap->ip->i_delayed_blks += args->len;
3373 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3374 -(long)args->len);
3375 return;
3376 }
3377
3378 /* data/attr fork only */
3379 ap->ip->i_d.di_nblocks += args->len;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003380 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003381 if (ap->wasdel) {
Darrick J. Wong751f3762018-01-25 13:58:13 -08003382 ap->ip->i_delayed_blks -= args->len;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003383 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3384 }
Darrick J. Wong751f3762018-01-25 13:58:13 -08003385 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3386 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3387 args->len);
3388}
3389
Christoph Hellwigc467c042010-02-15 23:34:42 +00003390STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003391xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003392 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003393{
3394 xfs_mount_t *mp; /* mount point structure */
3395 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003396 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003397 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003398 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003399 xfs_alloc_arg_t args;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003400 xfs_fileoff_t orig_offset;
3401 xfs_extlen_t orig_length;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003402 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003403 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003404 int nullfb; /* true if ap->firstblock isn't set */
3405 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003406 int tryagain;
3407 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003408 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003409
Dave Chinnera99ebf42011-12-01 11:24:20 +00003410 ASSERT(ap->length);
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003411 orig_offset = ap->offset;
3412 orig_length = ap->length;
Dave Chinnera99ebf42011-12-01 11:24:20 +00003413
Nathan Scotta365bdd2006-03-14 13:34:16 +11003414 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003415
3416 /* stripe alignment for allocation is determined by mount parameters */
3417 stripe_align = 0;
3418 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3419 stripe_align = mp->m_swidth;
3420 else if (mp->m_dalign)
3421 stripe_align = mp->m_dalign;
3422
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003423 if (ap->flags & XFS_BMAPI_COWFORK)
3424 align = xfs_get_cowextsz_hint(ap->ip);
3425 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003426 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003427 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003428 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003429 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003430 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003431 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003432 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003433 }
Dave Chinner33177f052013-12-12 16:34:36 +11003434
3435
Brian Foster94c07b42018-07-11 22:26:28 -07003436 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3437 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3438 ap->tp->t_firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003439 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003440 if (xfs_alloc_is_userdata(ap->datatype) &&
3441 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003442 ag = xfs_filestream_lookup_ag(ap->ip);
3443 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003444 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003445 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003446 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003447 }
3448 } else
Brian Foster94c07b42018-07-11 22:26:28 -07003449 ap->blkno = ap->tp->t_firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003450
3451 xfs_bmap_adjacent(ap);
3452
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003454 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 * it's in the right allocation group.
3456 */
Dave Chinner3a756672011-09-18 20:40:58 +00003457 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 ;
3459 else
Brian Foster94c07b42018-07-11 22:26:28 -07003460 ap->blkno = ap->tp->t_firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 * Normal allocation, done through xfs_alloc_vextent.
3463 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003464 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003465 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003466 args.tp = ap->tp;
3467 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003468 args.fsbno = ap->blkno;
Darrick J. Wong7280fed2018-12-12 08:46:23 -08003469 args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
Dave Chinner14b064c2011-01-27 12:16:28 +11003470
3471 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner9bb54cb2018-06-07 07:54:02 -07003472 args.maxlen = min(ap->length, mp->m_ag_max_usable);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003473 blen = 0;
3474 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003475 /*
3476 * Search for an allocation group with a single extent large
3477 * enough for the request. If one isn't found, then adjust
3478 * the minimum allocation size to the largest space found.
3479 */
Dave Chinner292378e2016-09-26 08:21:28 +10003480 if (xfs_alloc_is_userdata(ap->datatype) &&
3481 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003482 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3483 else
3484 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003485 if (error)
3486 return error;
Brian Foster1214f1c2018-08-01 07:20:31 -07003487 } else if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003488 if (xfs_inode_is_filestream(ap->ip))
3489 args.type = XFS_ALLOCTYPE_FIRST_AG;
3490 else
3491 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003492 args.total = args.minlen = ap->minlen;
3493 } else {
3494 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3495 args.total = ap->total;
3496 args.minlen = ap->minlen;
3497 }
David Chinner957d0eb2007-06-18 16:50:37 +10003498 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003499 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003500 args.prod = align;
Dave Chinner0703a8e2018-06-08 09:54:22 -07003501 div_u64_rem(ap->offset, args.prod, &args.mod);
3502 if (args.mod)
3503 args.mod = args.prod - args.mod;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003504 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003505 args.prod = 1;
3506 args.mod = 0;
3507 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003508 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner0703a8e2018-06-08 09:54:22 -07003509 div_u64_rem(ap->offset, args.prod, &args.mod);
3510 if (args.mod)
3511 args.mod = args.prod - args.mod;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003512 }
3513 /*
3514 * If we are not low on available data blocks, and the
3515 * underlying logical volume manager is a stripe, and
3516 * the file offset is zero then try to allocate data
3517 * blocks on stripe unit boundary.
3518 * NOTE: ap->aeof is only set if the allocation length
3519 * is >= the stripe unit and the allocation offset is
3520 * at the end of file.
3521 */
Brian Foster1214f1c2018-08-01 07:20:31 -07003522 if (!(ap->tp->t_flags & XFS_TRANS_LOWMODE) && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003523 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003524 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003525 atype = args.type;
3526 isaligned = 1;
3527 /*
3528 * Adjust for alignment
3529 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003530 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003531 args.minlen = blen - args.alignment;
3532 args.minalignslop = 0;
3533 } else {
3534 /*
3535 * First try an exact bno allocation.
3536 * If it fails then do a near or start bno
3537 * allocation with alignment turned on.
3538 */
3539 atype = args.type;
3540 tryagain = 1;
3541 args.type = XFS_ALLOCTYPE_THIS_BNO;
3542 args.alignment = 1;
3543 /*
3544 * Compute the minlen+alignment for the
3545 * next case. Set slop so that the value
3546 * of minlen+alignment+slop doesn't go up
3547 * between the calls.
3548 */
Dave Chinner33177f052013-12-12 16:34:36 +11003549 if (blen > stripe_align && blen <= args.maxlen)
3550 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003551 else
3552 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003553 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003554 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003555 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003556 args.minlen - 1;
3557 else
3558 args.minalignslop = 0;
3559 }
3560 } else {
3561 args.alignment = 1;
3562 args.minalignslop = 0;
3563 }
3564 args.minleft = ap->minleft;
3565 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003566 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003567 args.datatype = ap->datatype;
3568 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003569 args.ip = ap->ip;
3570
3571 error = xfs_alloc_vextent(&args);
3572 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003573 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003574
Nathan Scotta365bdd2006-03-14 13:34:16 +11003575 if (tryagain && args.fsbno == NULLFSBLOCK) {
3576 /*
3577 * Exact allocation failed. Now try with alignment
3578 * turned on.
3579 */
3580 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003581 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003582 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003583 args.minlen = nextminlen;
3584 args.minalignslop = 0;
3585 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 if ((error = xfs_alloc_vextent(&args)))
3587 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003588 }
3589 if (isaligned && args.fsbno == NULLFSBLOCK) {
3590 /*
3591 * allocation failed, so turn off alignment and
3592 * try again.
3593 */
3594 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003595 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003596 args.alignment = 0;
3597 if ((error = xfs_alloc_vextent(&args)))
3598 return error;
3599 }
3600 if (args.fsbno == NULLFSBLOCK && nullfb &&
3601 args.minlen > ap->minlen) {
3602 args.minlen = ap->minlen;
3603 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003604 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003605 if ((error = xfs_alloc_vextent(&args)))
3606 return error;
3607 }
3608 if (args.fsbno == NULLFSBLOCK && nullfb) {
3609 args.fsbno = 0;
3610 args.type = XFS_ALLOCTYPE_FIRST_AG;
3611 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003612 if ((error = xfs_alloc_vextent(&args)))
3613 return error;
Brian Foster1214f1c2018-08-01 07:20:31 -07003614 ap->tp->t_flags |= XFS_TRANS_LOWMODE;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003615 }
3616 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003617 /*
3618 * check the allocation happened at the same or higher AG than
3619 * the first block that was allocated.
3620 */
Brian Foster94c07b42018-07-11 22:26:28 -07003621 ASSERT(ap->tp->t_firstblock == NULLFSBLOCK ||
3622 XFS_FSB_TO_AGNO(mp, ap->tp->t_firstblock) <=
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003623 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003624
Dave Chinner3a756672011-09-18 20:40:58 +00003625 ap->blkno = args.fsbno;
Brian Foster94c07b42018-07-11 22:26:28 -07003626 if (ap->tp->t_firstblock == NULLFSBLOCK)
3627 ap->tp->t_firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003628 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003629 ap->length = args.len;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003630 /*
3631 * If the extent size hint is active, we tried to round the
3632 * caller's allocation request offset down to extsz and the
3633 * length up to another extsz boundary. If we found a free
3634 * extent we mapped it in starting at this new offset. If the
3635 * newly mapped space isn't long enough to cover any of the
3636 * range of offsets that was originally requested, move the
3637 * mapping up so that we can fill as much of the caller's
3638 * original request as possible. Free space is apparently
3639 * very fragmented so we're unlikely to be able to satisfy the
3640 * hints anyway.
3641 */
3642 if (ap->length <= orig_length)
3643 ap->offset = orig_offset;
3644 else if (ap->offset + ap->length < orig_offset + orig_length)
3645 ap->offset = orig_offset + orig_length - ap->length;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003646 xfs_bmap_btalloc_accounting(ap, &args);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003647 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003648 ap->blkno = NULLFSBLOCK;
3649 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 }
3651 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003652}
3653
3654/*
3655 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3656 * It figures out where to ask the underlying allocator to put the new extent.
3657 */
3658STATIC int
3659xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003660 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003661{
Dave Chinner292378e2016-09-26 08:21:28 +10003662 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3663 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003664 return xfs_bmap_rtalloc(ap);
3665 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666}
3667
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003668/* Trim extent to fit a logical block range. */
3669void
3670xfs_trim_extent(
3671 struct xfs_bmbt_irec *irec,
3672 xfs_fileoff_t bno,
3673 xfs_filblks_t len)
3674{
3675 xfs_fileoff_t distance;
3676 xfs_fileoff_t end = bno + len;
3677
3678 if (irec->br_startoff + irec->br_blockcount <= bno ||
3679 irec->br_startoff >= end) {
3680 irec->br_blockcount = 0;
3681 return;
3682 }
3683
3684 if (irec->br_startoff < bno) {
3685 distance = bno - irec->br_startoff;
3686 if (isnullstartblock(irec->br_startblock))
3687 irec->br_startblock = DELAYSTARTBLOCK;
3688 if (irec->br_startblock != DELAYSTARTBLOCK &&
3689 irec->br_startblock != HOLESTARTBLOCK)
3690 irec->br_startblock += distance;
3691 irec->br_startoff += distance;
3692 irec->br_blockcount -= distance;
3693 }
3694
3695 if (end < irec->br_startoff + irec->br_blockcount) {
3696 distance = irec->br_startoff + irec->br_blockcount - end;
3697 irec->br_blockcount -= distance;
3698 }
3699}
3700
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003702 * Trim the returned map to the required bounds
3703 */
3704STATIC void
3705xfs_bmapi_trim_map(
3706 struct xfs_bmbt_irec *mval,
3707 struct xfs_bmbt_irec *got,
3708 xfs_fileoff_t *bno,
3709 xfs_filblks_t len,
3710 xfs_fileoff_t obno,
3711 xfs_fileoff_t end,
3712 int n,
3713 int flags)
3714{
3715 if ((flags & XFS_BMAPI_ENTIRE) ||
3716 got->br_startoff + got->br_blockcount <= obno) {
3717 *mval = *got;
3718 if (isnullstartblock(got->br_startblock))
3719 mval->br_startblock = DELAYSTARTBLOCK;
3720 return;
3721 }
3722
3723 if (obno > *bno)
3724 *bno = obno;
3725 ASSERT((*bno >= obno) || (n == 0));
3726 ASSERT(*bno < end);
3727 mval->br_startoff = *bno;
3728 if (isnullstartblock(got->br_startblock))
3729 mval->br_startblock = DELAYSTARTBLOCK;
3730 else
3731 mval->br_startblock = got->br_startblock +
3732 (*bno - got->br_startoff);
3733 /*
3734 * Return the minimum of what we got and what we asked for for
3735 * the length. We can use the len variable here because it is
3736 * modified below and we could have been there before coming
3737 * here if the first part of the allocation didn't overlap what
3738 * was asked for.
3739 */
3740 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3741 got->br_blockcount - (*bno - got->br_startoff));
3742 mval->br_state = got->br_state;
3743 ASSERT(mval->br_blockcount <= len);
3744 return;
3745}
3746
3747/*
3748 * Update and validate the extent map to return
3749 */
3750STATIC void
3751xfs_bmapi_update_map(
3752 struct xfs_bmbt_irec **map,
3753 xfs_fileoff_t *bno,
3754 xfs_filblks_t *len,
3755 xfs_fileoff_t obno,
3756 xfs_fileoff_t end,
3757 int *n,
3758 int flags)
3759{
3760 xfs_bmbt_irec_t *mval = *map;
3761
3762 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3763 ((mval->br_startoff + mval->br_blockcount) <= end));
3764 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3765 (mval->br_startoff < obno));
3766
3767 *bno = mval->br_startoff + mval->br_blockcount;
3768 *len = end - *bno;
3769 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3770 /* update previous map with new information */
3771 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3772 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3773 ASSERT(mval->br_state == mval[-1].br_state);
3774 mval[-1].br_blockcount = mval->br_blockcount;
3775 mval[-1].br_state = mval->br_state;
3776 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3777 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3778 mval[-1].br_startblock != HOLESTARTBLOCK &&
3779 mval->br_startblock == mval[-1].br_startblock +
3780 mval[-1].br_blockcount &&
Christoph Hellwigc3a2f9f2018-07-11 22:26:01 -07003781 mval[-1].br_state == mval->br_state) {
Dave Chinneraef9a892011-09-18 20:40:44 +00003782 ASSERT(mval->br_startoff ==
3783 mval[-1].br_startoff + mval[-1].br_blockcount);
3784 mval[-1].br_blockcount += mval->br_blockcount;
3785 } else if (*n > 0 &&
3786 mval->br_startblock == DELAYSTARTBLOCK &&
3787 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3788 mval->br_startoff ==
3789 mval[-1].br_startoff + mval[-1].br_blockcount) {
3790 mval[-1].br_blockcount += mval->br_blockcount;
3791 mval[-1].br_state = mval->br_state;
3792 } else if (!((*n == 0) &&
3793 ((mval->br_startoff + mval->br_blockcount) <=
3794 obno))) {
3795 mval++;
3796 (*n)++;
3797 }
3798 *map = mval;
3799}
3800
3801/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003802 * Map file blocks to filesystem blocks without allocation.
3803 */
3804int
3805xfs_bmapi_read(
3806 struct xfs_inode *ip,
3807 xfs_fileoff_t bno,
3808 xfs_filblks_t len,
3809 struct xfs_bmbt_irec *mval,
3810 int *nmap,
3811 int flags)
3812{
3813 struct xfs_mount *mp = ip->i_mount;
3814 struct xfs_ifork *ifp;
3815 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003816 xfs_fileoff_t obno;
3817 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003818 struct xfs_iext_cursor icur;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003819 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003820 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003821 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003822 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003823
3824 ASSERT(*nmap >= 1);
3825 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Christoph Hellwigc3a2f9f2018-07-11 22:26:01 -07003826 XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003827 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003828
3829 if (unlikely(XFS_TEST_ERROR(
3830 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3831 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003832 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003833 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003834 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003835 }
3836
3837 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003838 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003839
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003840 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003841
3842 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003843
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003844 /* No CoW fork? Return a hole. */
3845 if (whichfork == XFS_COW_FORK && !ifp) {
3846 mval->br_startoff = bno;
3847 mval->br_startblock = HOLESTARTBLOCK;
3848 mval->br_blockcount = len;
3849 mval->br_state = XFS_EXT_NORM;
3850 *nmap = 1;
3851 return 0;
3852 }
3853
Dave Chinner5c8ed202011-09-18 20:40:45 +00003854 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3855 error = xfs_iread_extents(NULL, ip, whichfork);
3856 if (error)
3857 return error;
3858 }
3859
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003860 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003861 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003862 end = bno + len;
3863 obno = bno;
3864
3865 while (bno < end && n < *nmap) {
3866 /* Reading past eof, act as though there's a hole up to end. */
3867 if (eof)
3868 got.br_startoff = end;
3869 if (got.br_startoff > bno) {
3870 /* Reading in a hole. */
3871 mval->br_startoff = bno;
3872 mval->br_startblock = HOLESTARTBLOCK;
3873 mval->br_blockcount =
3874 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3875 mval->br_state = XFS_EXT_NORM;
3876 bno += mval->br_blockcount;
3877 len -= mval->br_blockcount;
3878 mval++;
3879 n++;
3880 continue;
3881 }
3882
3883 /* set up the extent map to return. */
3884 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3885 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3886
3887 /* If we're done, stop now. */
3888 if (bno >= end || n >= *nmap)
3889 break;
3890
3891 /* Else go on to the next record. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003892 if (!xfs_iext_next_extent(ifp, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003893 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003894 }
3895 *nmap = n;
3896 return 0;
3897}
3898
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003899/*
3900 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3901 * global pool and the extent inserted into the inode in-core extent tree.
3902 *
3903 * On entry, got refers to the first extent beyond the offset of the extent to
3904 * allocate or eof is specified if no such extent exists. On return, got refers
3905 * to the extent record that was inserted to the inode fork.
3906 *
3907 * Note that the allocated extent may have been merged with contiguous extents
3908 * during insertion into the inode fork. Thus, got does not reflect the current
3909 * state of the inode fork on return. If necessary, the caller can use lastx to
3910 * look up the updated record in the inode fork.
3911 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10003912int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003913xfs_bmapi_reserve_delalloc(
3914 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003915 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11003916 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003917 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11003918 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003919 struct xfs_bmbt_irec *got,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003920 struct xfs_iext_cursor *icur,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003921 int eof)
3922{
3923 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003924 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003925 xfs_extlen_t alen;
3926 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003927 int error;
Brian Foster974ae922016-11-28 14:57:42 +11003928 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003929
Brian Foster974ae922016-11-28 14:57:42 +11003930 /*
3931 * Cap the alloc length. Keep track of prealloc so we know whether to
3932 * tag the inode before we return.
3933 */
3934 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003935 if (!eof)
3936 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11003937 if (prealloc && alen >= len)
3938 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003939
3940 /* Figure out the extent size, adjust alen */
Shan Hai6ca30722018-01-23 13:56:11 -08003941 if (whichfork == XFS_COW_FORK) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003942 struct xfs_bmbt_irec prev;
Shan Hai6ca30722018-01-23 13:56:11 -08003943 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003944
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003945 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003946 prev.br_startoff = NULLFILEOFF;
3947
Shan Hai6ca30722018-01-23 13:56:11 -08003948 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003949 1, 0, &aoff, &alen);
3950 ASSERT(!error);
3951 }
3952
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003953 /*
3954 * Make a transaction-less quota reservation for delayed allocation
3955 * blocks. This number gets adjusted later. We return if we haven't
3956 * allocated blocks already inside this loop.
3957 */
3958 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
Shan Hai6ca30722018-01-23 13:56:11 -08003959 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003960 if (error)
3961 return error;
3962
3963 /*
3964 * Split changing sb for alen and indlen since they could be coming
3965 * from different places.
3966 */
3967 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
3968 ASSERT(indlen > 0);
3969
Shan Hai6ca30722018-01-23 13:56:11 -08003970 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003971 if (error)
3972 goto out_unreserve_quota;
3973
Dave Chinner0d485ad2015-02-23 21:22:03 +11003974 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003975 if (error)
3976 goto out_unreserve_blocks;
3977
3978
3979 ip->i_delayed_blks += alen;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003980 xfs_mod_delalloc(ip->i_mount, alen + indlen);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003981
3982 got->br_startoff = aoff;
3983 got->br_startblock = nullstartblock(indlen);
3984 got->br_blockcount = alen;
3985 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003986
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003987 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003988
Brian Foster974ae922016-11-28 14:57:42 +11003989 /*
3990 * Tag the inode if blocks were preallocated. Note that COW fork
3991 * preallocation can occur at the start or end of the extent, even when
3992 * prealloc == 0, so we must also check the aligned offset and length.
3993 */
3994 if (whichfork == XFS_DATA_FORK && prealloc)
3995 xfs_inode_set_eofblocks_tag(ip);
3996 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
3997 xfs_inode_set_cowblocks_tag(ip);
3998
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003999 return 0;
4000
4001out_unreserve_blocks:
Shan Hai6ca30722018-01-23 13:56:11 -08004002 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004003out_unreserve_quota:
4004 if (XFS_IS_QUOTA_ON(mp))
Shan Hai6ca30722018-01-23 13:56:11 -08004005 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0,
4006 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004007 return error;
4008}
4009
Dave Chinnercf11da92014-07-15 07:08:24 +10004010static int
4011xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004012 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004013{
4014 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004015 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004016 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004017 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004018 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004019
Dave Chinnera99ebf42011-12-01 11:24:20 +00004020 ASSERT(bma->length > 0);
4021
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004022 /*
4023 * For the wasdelay case, we could also just allocate the stuff asked
4024 * for in this bmap call but that wouldn't be as good.
4025 */
4026 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004027 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4028 bma->offset = bma->got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004029 xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004030 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004031 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004032 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004033 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004034 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004035 }
4036
4037 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004038 * Set the data type being allocated. For the data fork, the first data
4039 * in the file is treated differently to all other allocations. For the
4040 * attribute fork, we only need to ensure the allocated range is not on
4041 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004042 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004043 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004044 bma->datatype = XFS_ALLOC_NOBUSY;
4045 if (whichfork == XFS_DATA_FORK) {
4046 if (bma->offset == 0)
4047 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4048 else
4049 bma->datatype |= XFS_ALLOC_USERDATA;
4050 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004051 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004052 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004053 }
4054
Dave Chinnere04426b2012-10-05 11:06:59 +10004055 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004056
4057 /*
4058 * Only want to do the alignment at the eof if it is userdata and
4059 * allocation length is larger than a stripe unit.
4060 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004061 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004062 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004063 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004064 if (error)
4065 return error;
4066 }
4067
4068 error = xfs_bmap_alloc(bma);
4069 if (error)
4070 return error;
4071
Dave Chinner963c30c2011-09-18 20:40:59 +00004072 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004073 return 0;
Brian Fostercf612de2018-07-11 22:26:29 -07004074 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur)
Dave Chinner29c8d172011-09-18 20:41:00 +00004075 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004076 /*
4077 * Bump the number of extents we've allocated
4078 * in this call.
4079 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004080 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004081
Dave Chinner29c8d172011-09-18 20:41:00 +00004082 if (bma->cur)
4083 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004084 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4085
Dave Chinner963c30c2011-09-18 20:40:59 +00004086 bma->got.br_startoff = bma->offset;
4087 bma->got.br_startblock = bma->blkno;
4088 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004089 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004090
4091 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004092 * In the data fork, a wasdelay extent has been initialized, so
4093 * shouldn't be flagged as unwritten.
4094 *
4095 * For the cow fork, however, we convert delalloc reservations
4096 * (extents allocated for speculative preallocation) to
4097 * allocated unwritten extents, and only convert the unwritten
4098 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004099 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004100 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11004101 (bma->flags & XFS_BMAPI_PREALLOC))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004102 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004103
Christoph Hellwigc6534242011-09-18 20:41:05 +00004104 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004105 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004106 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004107 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004108 whichfork, &bma->icur, &bma->cur, &bma->got,
Brian Foster92f9da32018-07-11 22:26:28 -07004109 &bma->logflags, bma->flags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004110
Christoph Hellwigc315c902011-09-18 20:41:02 +00004111 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004112 if (error)
4113 return error;
4114
4115 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004116 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4117 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4118 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004119 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004120 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004121
Dave Chinner963c30c2011-09-18 20:40:59 +00004122 ASSERT(bma->got.br_startoff <= bma->offset);
4123 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4124 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004125 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4126 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004127 return 0;
4128}
4129
Dave Chinnerb447fe52011-09-18 20:40:51 +00004130STATIC int
4131xfs_bmapi_convert_unwritten(
4132 struct xfs_bmalloca *bma,
4133 struct xfs_bmbt_irec *mval,
4134 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004135 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004136{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004137 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004138 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004139 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004140 int error;
4141
Dave Chinnerb447fe52011-09-18 20:40:51 +00004142 /* check if we need to do unwritten->real conversion */
4143 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4144 (flags & XFS_BMAPI_PREALLOC))
4145 return 0;
4146
4147 /* check if we need to do real->unwritten conversion */
4148 if (mval->br_state == XFS_EXT_NORM &&
4149 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4150 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4151 return 0;
4152
4153 /*
4154 * Modify (by adding) the state flag, if writing.
4155 */
4156 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004157 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4158 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004159 bma->ip, whichfork);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004160 }
4161 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4162 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4163
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004164 /*
4165 * Before insertion into the bmbt, zero the range being converted
4166 * if required.
4167 */
4168 if (flags & XFS_BMAPI_ZERO) {
4169 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4170 mval->br_blockcount);
4171 if (error)
4172 return error;
4173 }
4174
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004175 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
Brian Foster92f9da32018-07-11 22:26:28 -07004176 &bma->icur, &bma->cur, mval, &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004177 /*
4178 * Log the inode core unconditionally in the unwritten extent conversion
4179 * path because the conversion might not have done so (e.g., if the
4180 * extent count hasn't changed). We need to make sure the inode is dirty
4181 * in the transaction for the sake of fsync(), even if nothing has
4182 * changed, because fsync() will not force the log for this transaction
4183 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004184 *
4185 * Note: If we're only converting cow fork extents, there aren't
4186 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004187 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004188 if (whichfork != XFS_COW_FORK)
4189 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004190 if (error)
4191 return error;
4192
4193 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004194 * Update our extent pointer, given that
4195 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4196 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004197 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004198 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004199
4200 /*
4201 * We may have combined previously unwritten space with written space,
4202 * so generate another request.
4203 */
4204 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004205 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004206 return 0;
4207}
4208
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004209static inline xfs_extlen_t
4210xfs_bmapi_minleft(
4211 struct xfs_trans *tp,
4212 struct xfs_inode *ip,
4213 int fork)
4214{
4215 if (tp && tp->t_firstblock != NULLFSBLOCK)
4216 return 0;
4217 if (XFS_IFORK_FORMAT(ip, fork) != XFS_DINODE_FMT_BTREE)
4218 return 1;
4219 return be16_to_cpu(XFS_IFORK_PTR(ip, fork)->if_broot->bb_level) + 1;
4220}
4221
4222/*
4223 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4224 * a case where the data is changed, there's an error, and it's not logged so we
4225 * don't shutdown when we should. Don't bother logging extents/btree changes if
4226 * we converted to the other format.
4227 */
4228static void
4229xfs_bmapi_finish(
4230 struct xfs_bmalloca *bma,
4231 int whichfork,
4232 int error)
4233{
4234 if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
4235 XFS_IFORK_FORMAT(bma->ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4236 bma->logflags &= ~xfs_ilog_fext(whichfork);
4237 else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
4238 XFS_IFORK_FORMAT(bma->ip, whichfork) != XFS_DINODE_FMT_BTREE)
4239 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4240
4241 if (bma->logflags)
4242 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4243 if (bma->cur)
4244 xfs_btree_del_cursor(bma->cur, error);
4245}
4246
Christoph Hellwig44032802011-09-18 20:40:48 +00004247/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004248 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4249 * extent state if necessary. Details behaviour is controlled by the flags
4250 * parameter. Only allocates blocks from a single allocation group, to avoid
4251 * locking problems.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004253int
4254xfs_bmapi_write(
4255 struct xfs_trans *tp, /* transaction pointer */
4256 struct xfs_inode *ip, /* incore inode */
4257 xfs_fileoff_t bno, /* starting file offs. mapped */
4258 xfs_filblks_t len, /* length to map in file */
4259 int flags, /* XFS_BMAPI_... */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004260 xfs_extlen_t total, /* total blocks needed */
4261 struct xfs_bmbt_irec *mval, /* output: map values */
Brian Foster6e702a52018-07-11 22:26:12 -07004262 int *nmap) /* i/o: mval size/count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263{
Darrick J. Wong4b0bce32019-03-19 08:16:22 -07004264 struct xfs_bmalloca bma = {
4265 .tp = tp,
4266 .ip = ip,
4267 .total = total,
4268 };
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004269 struct xfs_mount *mp = ip->i_mount;
4270 struct xfs_ifork *ifp;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004271 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004272 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004273 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004274 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004275 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004276 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004277
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004279 xfs_fileoff_t orig_bno; /* original block number value */
4280 int orig_flags; /* original flags arg value */
4281 xfs_filblks_t orig_len; /* original value of len arg */
4282 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4283 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
4285 orig_bno = bno;
4286 orig_len = len;
4287 orig_flags = flags;
4288 orig_mval = mval;
4289 orig_nmap = *nmap;
4290#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004291 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004292
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004294 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
Christoph Hellwig26b91c72019-02-18 09:38:48 -08004295 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004296 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004297 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004298 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004299 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004300
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004301 /* zeroing is for currently only for data extents, not metadata */
4302 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4303 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4304 /*
4305 * we can allocate unwritten extents or pre-zero allocated blocks,
4306 * but it makes no sense to do both at once. This would result in
4307 * zeroing the unwritten extent twice, but it still being an
4308 * unwritten extent....
4309 */
4310 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4311 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4312
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 if (unlikely(XFS_TEST_ERROR(
4314 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004315 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004316 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004317 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004318 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004320
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004322 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004323
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004325
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004326 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004327
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004328 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4329 error = xfs_iread_extents(tp, ip, whichfork);
4330 if (error)
4331 goto error0;
4332 }
4333
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004334 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004335 eof = true;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004336 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004337 bma.prev.br_startoff = NULLFILEOFF;
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004338 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004339
Brian Foster627209f2019-02-01 09:14:23 -08004340 n = 0;
4341 end = bno + len;
4342 obno = bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004344 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004345
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004346 /* in hole or beyond EOF? */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004347 if (eof || bma.got.br_startoff > bno) {
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004348 /*
4349 * CoW fork conversions should /never/ hit EOF or
4350 * holes. There should always be something for us
4351 * to work on.
4352 */
4353 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4354 (flags & XFS_BMAPI_COWFORK)));
4355
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004356 need_alloc = true;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004357 } else if (isnullstartblock(bma.got.br_startblock)) {
4358 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004359 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004360
4361 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 * First, deal with the hole before the allocated space
4363 * that we found, if any.
4364 */
Christoph Hellwig26b91c72019-02-18 09:38:48 -08004365 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004366 bma.eof = eof;
4367 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4368 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004369 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004370 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004371
Dave Chinnera99ebf42011-12-01 11:24:20 +00004372 /*
4373 * There's a 32/64 bit type mismatch between the
4374 * allocation length request (which can be 64 bits in
4375 * length) and the bma length request, which is
4376 * xfs_extlen_t and therefore 32 bits. Hence we have to
4377 * check for 32-bit overflows and handle them here.
4378 */
4379 if (len > (xfs_filblks_t)MAXEXTLEN)
4380 bma.length = MAXEXTLEN;
4381 else
4382 bma.length = len;
4383
4384 ASSERT(len > 0);
4385 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004386 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 if (error)
4388 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004389 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004390 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004391
4392 /*
4393 * If this is a CoW allocation, record the data in
4394 * the refcount btree for orphan recovery.
4395 */
4396 if (whichfork == XFS_COW_FORK) {
Brian Foster0f37d172018-08-01 07:20:34 -07004397 error = xfs_refcount_alloc_cow_extent(tp,
4398 bma.blkno, bma.length);
Darrick J. Wong174edb02016-10-03 09:11:39 -07004399 if (error)
4400 goto error0;
4401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004403
Dave Chinneraef9a892011-09-18 20:40:44 +00004404 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004405 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4406 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407
Dave Chinnerb447fe52011-09-18 20:40:51 +00004408 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004409 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004410 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004411 continue;
4412 if (error)
4413 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
Dave Chinneraef9a892011-09-18 20:40:44 +00004415 /* update the extent map to return */
4416 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4417
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 /*
4419 * If we're done, stop now. Stop when we've allocated
4420 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4421 * the transaction may get too big.
4422 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004423 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004425
4426 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004427 bma.prev = bma.got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004428 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004429 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004432
Christoph Hellwigb101e332019-02-15 08:02:47 -08004433 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4434 whichfork);
4435 if (error)
4436 goto error0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004437
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004439 XFS_IFORK_NEXTENTS(ip, whichfork) >
4440 XFS_IFORK_MAXEXT(ip, whichfork));
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004441 xfs_bmapi_finish(&bma, whichfork, 0);
4442 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4443 orig_nmap, *nmap);
4444 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445error0:
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004446 xfs_bmapi_finish(&bma, whichfork, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 return error;
4448}
4449
Brian Foster627209f2019-02-01 09:14:23 -08004450/*
4451 * Convert an existing delalloc extent to real blocks based on file offset. This
4452 * attempts to allocate the entire delalloc extent and may require multiple
4453 * invocations to allocate the target offset if a large enough physical extent
4454 * is not available.
4455 */
4456int
4457xfs_bmapi_convert_delalloc(
Brian Foster627209f2019-02-01 09:14:23 -08004458 struct xfs_inode *ip,
Brian Foster627209f2019-02-01 09:14:23 -08004459 int whichfork,
Christoph Hellwig491ce612019-02-15 08:02:49 -08004460 xfs_fileoff_t offset_fsb,
4461 struct xfs_bmbt_irec *imap,
4462 unsigned int *seq)
Brian Foster627209f2019-02-01 09:14:23 -08004463{
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004464 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004465 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004466 struct xfs_bmalloca bma = { NULL };
Christoph Hellwig491ce612019-02-15 08:02:49 -08004467 struct xfs_trans *tp;
Brian Foster627209f2019-02-01 09:14:23 -08004468 int error;
Brian Foster627209f2019-02-01 09:14:23 -08004469
Christoph Hellwig491ce612019-02-15 08:02:49 -08004470 /*
4471 * Space for the extent and indirect blocks was reserved when the
4472 * delalloc extent was created so there's no need to do so here.
4473 */
4474 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4475 XFS_TRANS_RESERVE, &tp);
4476 if (error)
4477 return error;
4478
4479 xfs_ilock(ip, XFS_ILOCK_EXCL);
4480 xfs_trans_ijoin(tp, ip, 0);
4481
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004482 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4483 bma.got.br_startoff > offset_fsb) {
4484 /*
4485 * No extent found in the range we are trying to convert. This
4486 * should only happen for the COW fork, where another thread
4487 * might have moved the extent to the data fork in the meantime.
4488 */
4489 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004490 error = -EAGAIN;
4491 goto out_trans_cancel;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004492 }
Brian Foster627209f2019-02-01 09:14:23 -08004493
4494 /*
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004495 * If we find a real extent here we raced with another thread converting
4496 * the extent. Just return the real extent at this offset.
Brian Foster627209f2019-02-01 09:14:23 -08004497 */
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004498 if (!isnullstartblock(bma.got.br_startblock)) {
4499 *imap = bma.got;
Christoph Hellwig491ce612019-02-15 08:02:49 -08004500 *seq = READ_ONCE(ifp->if_seq);
4501 goto out_trans_cancel;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004502 }
4503
4504 bma.tp = tp;
4505 bma.ip = ip;
4506 bma.wasdel = true;
4507 bma.offset = bma.got.br_startoff;
4508 bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount, MAXEXTLEN);
4509 bma.total = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
4510 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4511 if (whichfork == XFS_COW_FORK)
4512 bma.flags = XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC;
4513
4514 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4515 bma.prev.br_startoff = NULLFILEOFF;
4516
4517 error = xfs_bmapi_allocate(&bma);
4518 if (error)
4519 goto out_finish;
4520
4521 error = -ENOSPC;
4522 if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK))
4523 goto out_finish;
4524 error = -EFSCORRUPTED;
4525 if (WARN_ON_ONCE(!bma.got.br_startblock && !XFS_IS_REALTIME_INODE(ip)))
4526 goto out_finish;
4527
Christoph Hellwig125851a2019-02-15 08:02:49 -08004528 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4529 XFS_STATS_INC(mp, xs_xstrat_quick);
4530
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004531 ASSERT(!isnullstartblock(bma.got.br_startblock));
4532 *imap = bma.got;
Christoph Hellwig491ce612019-02-15 08:02:49 -08004533 *seq = READ_ONCE(ifp->if_seq);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004534
4535 if (whichfork == XFS_COW_FORK) {
4536 error = xfs_refcount_alloc_cow_extent(tp, bma.blkno,
4537 bma.length);
4538 if (error)
4539 goto out_finish;
4540 }
4541
4542 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4543 whichfork);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004544 if (error)
4545 goto out_finish;
4546
4547 xfs_bmapi_finish(&bma, whichfork, 0);
4548 error = xfs_trans_commit(tp);
4549 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4550 return error;
4551
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004552out_finish:
4553 xfs_bmapi_finish(&bma, whichfork, error);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004554out_trans_cancel:
4555 xfs_trans_cancel(tp);
4556 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Brian Foster627209f2019-02-01 09:14:23 -08004557 return error;
4558}
4559
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004560int
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004561xfs_bmapi_remap(
4562 struct xfs_trans *tp,
4563 struct xfs_inode *ip,
4564 xfs_fileoff_t bno,
4565 xfs_filblks_t len,
4566 xfs_fsblock_t startblock,
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004567 int flags)
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004568{
4569 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004570 struct xfs_ifork *ifp;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004571 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004572 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004573 struct xfs_iext_cursor icur;
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004574 int whichfork = xfs_bmapi_whichfork(flags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004575 int logflags = 0, error;
4576
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004577 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004578 ASSERT(len > 0);
4579 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4580 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Darrick J. Wong7644bd92018-05-14 06:34:35 -07004581 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4582 XFS_BMAPI_NORMAP)));
4583 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4584 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004585
4586 if (unlikely(XFS_TEST_ERROR(
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004587 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4588 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004589 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004590 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4591 return -EFSCORRUPTED;
4592 }
4593
4594 if (XFS_FORCED_SHUTDOWN(mp))
4595 return -EIO;
4596
4597 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004598 error = xfs_iread_extents(tp, ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004599 if (error)
4600 return error;
4601 }
4602
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004603 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004604 /* make sure we only reflink into a hole. */
4605 ASSERT(got.br_startoff > bno);
4606 ASSERT(got.br_startoff - bno >= len);
4607 }
4608
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004609 ip->i_d.di_nblocks += len;
4610 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004611
4612 if (ifp->if_flags & XFS_IFBROOT) {
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004613 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004614 cur->bc_private.b.flags = 0;
4615 }
4616
4617 got.br_startoff = bno;
4618 got.br_startblock = startblock;
4619 got.br_blockcount = len;
Darrick J. Wong7644bd92018-05-14 06:34:35 -07004620 if (flags & XFS_BMAPI_PREALLOC)
4621 got.br_state = XFS_EXT_UNWRITTEN;
4622 else
4623 got.br_state = XFS_EXT_NORM;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004624
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004625 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
Brian Foster92f9da32018-07-11 22:26:28 -07004626 &cur, &got, &logflags, flags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004627 if (error)
4628 goto error0;
4629
Christoph Hellwigb101e332019-02-15 08:02:47 -08004630 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004631
4632error0:
4633 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4634 logflags &= ~XFS_ILOG_DEXT;
4635 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4636 logflags &= ~XFS_ILOG_DBROOT;
4637
4638 if (logflags)
4639 xfs_trans_log_inode(tp, ip, logflags);
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07004640 if (cur)
4641 xfs_btree_del_cursor(cur, error);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004642 return error;
4643}
4644
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004646 * When a delalloc extent is split (e.g., due to a hole punch), the original
4647 * indlen reservation must be shared across the two new extents that are left
4648 * behind.
4649 *
4650 * Given the original reservation and the worst case indlen for the two new
4651 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004652 * reservation fairly across the two new extents. If necessary, steal available
4653 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4654 * ores == 1). The number of stolen blocks is returned. The availability and
4655 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004656 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004657static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004658xfs_bmap_split_indlen(
4659 xfs_filblks_t ores, /* original res. */
4660 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004661 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4662 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004663{
4664 xfs_filblks_t len1 = *indlen1;
4665 xfs_filblks_t len2 = *indlen2;
4666 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004667 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004668 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004669
4670 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004671 * Steal as many blocks as we can to try and satisfy the worst case
4672 * indlen for both new extents.
4673 */
Brian Foster75d65362017-02-13 22:48:30 -08004674 if (ores < nres && avail)
4675 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4676 ores += stolen;
4677
4678 /* nothing else to do if we've satisfied the new reservation */
4679 if (ores >= nres)
4680 return stolen;
Brian Fosterd34999c2016-03-15 11:42:47 +11004681
4682 /*
Brian Foster75d65362017-02-13 22:48:30 -08004683 * We can't meet the total required reservation for the two extents.
4684 * Calculate the percent of the overall shortage between both extents
4685 * and apply this percentage to each of the requested indlen values.
4686 * This distributes the shortage fairly and reduces the chances that one
4687 * of the two extents is left with nothing when extents are repeatedly
4688 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004689 */
Brian Foster75d65362017-02-13 22:48:30 -08004690 resfactor = (ores * 100);
4691 do_div(resfactor, nres);
4692 len1 *= resfactor;
4693 do_div(len1, 100);
4694 len2 *= resfactor;
4695 do_div(len2, 100);
4696 ASSERT(len1 + len2 <= ores);
4697 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4698
4699 /*
4700 * Hand out the remainder to each extent. If one of the two reservations
4701 * is zero, we want to make sure that one gets a block first. The loop
4702 * below starts with len1, so hand len2 a block right off the bat if it
4703 * is zero.
4704 */
4705 ores -= (len1 + len2);
4706 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4707 if (ores && !len2 && *indlen2) {
4708 len2++;
4709 ores--;
4710 }
4711 while (ores) {
4712 if (len1 < *indlen1) {
4713 len1++;
4714 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004715 }
Brian Foster75d65362017-02-13 22:48:30 -08004716 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004717 break;
Brian Foster75d65362017-02-13 22:48:30 -08004718 if (len2 < *indlen2) {
4719 len2++;
4720 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004721 }
4722 }
4723
4724 *indlen1 = len1;
4725 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004726
4727 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004728}
4729
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004730int
4731xfs_bmap_del_extent_delay(
4732 struct xfs_inode *ip,
4733 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004734 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004735 struct xfs_bmbt_irec *got,
4736 struct xfs_bmbt_irec *del)
4737{
4738 struct xfs_mount *mp = ip->i_mount;
4739 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4740 struct xfs_bmbt_irec new;
4741 int64_t da_old, da_new, da_diff = 0;
4742 xfs_fileoff_t del_endoff, got_endoff;
4743 xfs_filblks_t got_indlen, new_indlen, stolen;
Christoph Hellwig060ea652017-10-19 11:02:29 -07004744 int state = xfs_bmap_fork_to_state(whichfork);
4745 int error = 0;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004746 bool isrt;
4747
4748 XFS_STATS_INC(mp, xs_del_exlist);
4749
4750 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4751 del_endoff = del->br_startoff + del->br_blockcount;
4752 got_endoff = got->br_startoff + got->br_blockcount;
4753 da_old = startblockval(got->br_startblock);
4754 da_new = 0;
4755
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004756 ASSERT(del->br_blockcount > 0);
4757 ASSERT(got->br_startoff <= del->br_startoff);
4758 ASSERT(got_endoff >= del_endoff);
4759
4760 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004761 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004762
4763 do_div(rtexts, mp->m_sb.sb_rextsize);
4764 xfs_mod_frextents(mp, rtexts);
4765 }
4766
4767 /*
4768 * Update the inode delalloc counter now and wait to update the
4769 * sb counters as we might have to borrow some blocks for the
4770 * indirect block accounting.
4771 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004772 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4773 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004774 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004775 if (error)
4776 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004777 ip->i_delayed_blks -= del->br_blockcount;
4778
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004779 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004780 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004781 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004782 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004783
Christoph Hellwig0173c682017-10-17 14:16:22 -07004784 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4785 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004786 /*
4787 * Matches the whole extent. Delete the entry.
4788 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004789 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004790 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004791 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004792 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004793 /*
4794 * Deleting the first part of the extent.
4795 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004796 got->br_startoff = del_endoff;
4797 got->br_blockcount -= del->br_blockcount;
4798 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4799 got->br_blockcount), da_old);
4800 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004801 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004802 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004803 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004804 /*
4805 * Deleting the last part of the extent.
4806 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004807 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4808 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4809 got->br_blockcount), da_old);
4810 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004811 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004812 break;
4813 case 0:
4814 /*
4815 * Deleting the middle of the extent.
4816 *
4817 * Distribute the original indlen reservation across the two new
4818 * extents. Steal blocks from the deleted extent if necessary.
4819 * Stealing blocks simply fudges the fdblocks accounting below.
4820 * Warn if either of the new indlen reservations is zero as this
4821 * can lead to delalloc problems.
4822 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004823 got->br_blockcount = del->br_startoff - got->br_startoff;
4824 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4825
4826 new.br_blockcount = got_endoff - del_endoff;
4827 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4828
4829 WARN_ON_ONCE(!got_indlen || !new_indlen);
4830 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4831 del->br_blockcount);
4832
4833 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004834
4835 new.br_startoff = del_endoff;
4836 new.br_state = got->br_state;
4837 new.br_startblock = nullstartblock((int)new_indlen);
4838
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004839 xfs_iext_update_extent(ip, state, icur, got);
4840 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004841 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004842
4843 da_new = got_indlen + new_indlen - stolen;
4844 del->br_blockcount -= stolen;
4845 break;
4846 }
4847
4848 ASSERT(da_old >= da_new);
4849 da_diff = da_old - da_new;
4850 if (!isrt)
4851 da_diff += del->br_blockcount;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07004852 if (da_diff) {
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004853 xfs_mod_fdblocks(mp, da_diff, false);
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07004854 xfs_mod_delalloc(mp, -da_diff);
4855 }
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004856 return error;
4857}
4858
4859void
4860xfs_bmap_del_extent_cow(
4861 struct xfs_inode *ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004862 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004863 struct xfs_bmbt_irec *got,
4864 struct xfs_bmbt_irec *del)
4865{
4866 struct xfs_mount *mp = ip->i_mount;
4867 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4868 struct xfs_bmbt_irec new;
4869 xfs_fileoff_t del_endoff, got_endoff;
4870 int state = BMAP_COWFORK;
4871
4872 XFS_STATS_INC(mp, xs_del_exlist);
4873
4874 del_endoff = del->br_startoff + del->br_blockcount;
4875 got_endoff = got->br_startoff + got->br_blockcount;
4876
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004877 ASSERT(del->br_blockcount > 0);
4878 ASSERT(got->br_startoff <= del->br_startoff);
4879 ASSERT(got_endoff >= del_endoff);
4880 ASSERT(!isnullstartblock(got->br_startblock));
4881
4882 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004883 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004884 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004885 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004886
Christoph Hellwig0173c682017-10-17 14:16:22 -07004887 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4888 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004889 /*
4890 * Matches the whole extent. Delete the entry.
4891 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004892 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004893 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004894 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004895 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004896 /*
4897 * Deleting the first part of the extent.
4898 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004899 got->br_startoff = del_endoff;
4900 got->br_blockcount -= del->br_blockcount;
4901 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004902 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004903 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004904 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004905 /*
4906 * Deleting the last part of the extent.
4907 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004908 got->br_blockcount -= del->br_blockcount;
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 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004915 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004916
4917 new.br_startoff = del_endoff;
4918 new.br_blockcount = got_endoff - del_endoff;
4919 new.br_state = got->br_state;
4920 new.br_startblock = del->br_startblock + del->br_blockcount;
4921
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004922 xfs_iext_update_extent(ip, state, icur, got);
4923 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004924 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004925 break;
4926 }
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08004927 ip->i_delayed_blks -= del->br_blockcount;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004928}
4929
Brian Fostera9bd24a2016-03-15 11:42:46 +11004930/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11004931 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07004932 * after removing space.
Dave Chinner9e5987a72013-02-25 12:31:26 +11004933 */
4934STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07004935xfs_bmap_del_extent_real(
Dave Chinner9e5987a72013-02-25 12:31:26 +11004936 xfs_inode_t *ip, /* incore inode pointer */
4937 xfs_trans_t *tp, /* current transaction pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004938 struct xfs_iext_cursor *icur,
Dave Chinner9e5987a72013-02-25 12:31:26 +11004939 xfs_btree_cur_t *cur, /* if null, not a btree */
4940 xfs_bmbt_irec_t *del, /* data to remove from extents */
4941 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07004942 int whichfork, /* data or attr fork */
4943 int bflags) /* bmapi flags */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004944{
Dave Chinner9e5987a72013-02-25 12:31:26 +11004945 xfs_fsblock_t del_endblock=0; /* first block past del */
4946 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004947 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004948 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004949 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004950 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004951 xfs_fileoff_t got_endoff; /* first offset past got */
4952 int i; /* temp state */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07004953 struct xfs_ifork *ifp; /* inode fork pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004954 xfs_mount_t *mp; /* mount structure */
4955 xfs_filblks_t nblks; /* quota/sb block count */
4956 xfs_bmbt_irec_t new; /* new record to be inserted */
4957 /* REFERENCED */
4958 uint qfield; /* quota field to update */
Christoph Hellwig060ea652017-10-19 11:02:29 -07004959 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004960 struct xfs_bmbt_irec old;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004961
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004962 mp = ip->i_mount;
4963 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004964
Dave Chinner9e5987a72013-02-25 12:31:26 +11004965 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004966 ASSERT(del->br_blockcount > 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004967 xfs_iext_get_extent(ifp, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004968 ASSERT(got.br_startoff <= del->br_startoff);
4969 del_endoff = del->br_startoff + del->br_blockcount;
4970 got_endoff = got.br_startoff + got.br_blockcount;
4971 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004972 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a72013-02-25 12:31:26 +11004973 qfield = 0;
4974 error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004975
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004976 /*
4977 * If it's the case where the directory code is running with no block
4978 * reservation, and the deleted block is in the middle of its extent,
4979 * and the resulting insert of an extent would cause transformation to
4980 * btree format, then reject it. The calling code will then swap blocks
4981 * around instead. We have to do this now, rather than waiting for the
4982 * conversion to btree format, since the transaction will be dirty then.
4983 */
4984 if (tp->t_blk_res == 0 &&
4985 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4986 XFS_IFORK_NEXTENTS(ip, whichfork) >=
4987 XFS_IFORK_MAXEXT(ip, whichfork) &&
4988 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
4989 return -ENOSPC;
4990
4991 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004992 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4993 xfs_fsblock_t bno;
4994 xfs_filblks_t len;
Dave Chinner0703a8e2018-06-08 09:54:22 -07004995 xfs_extlen_t mod;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004996
Dave Chinner0703a8e2018-06-08 09:54:22 -07004997 bno = div_u64_rem(del->br_startblock, mp->m_sb.sb_rextsize,
4998 &mod);
4999 ASSERT(mod == 0);
5000 len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize,
5001 &mod);
5002 ASSERT(mod == 0);
5003
Christoph Hellwige1d75532017-10-17 14:16:21 -07005004 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5005 if (error)
5006 goto done;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005007 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005008 nblks = len * mp->m_sb.sb_rextsize;
5009 qfield = XFS_TRANS_DQ_RTBCOUNT;
5010 } else {
5011 do_fx = 1;
5012 nblks = del->br_blockcount;
5013 qfield = XFS_TRANS_DQ_BCOUNT;
5014 }
5015
5016 del_endblock = del->br_startblock + del->br_blockcount;
5017 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005018 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005019 if (error)
5020 goto done;
5021 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005022 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005023
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005024 if (got.br_startoff == del->br_startoff)
5025 state |= BMAP_LEFT_FILLING;
5026 if (got_endoff == del_endoff)
5027 state |= BMAP_RIGHT_FILLING;
5028
5029 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5030 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005031 /*
5032 * Matches the whole extent. Delete the entry.
5033 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005034 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005035 xfs_iext_prev(ifp, icur);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005036 XFS_IFORK_NEXT_SET(ip, whichfork,
5037 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5038 flags |= XFS_ILOG_CORE;
5039 if (!cur) {
5040 flags |= xfs_ilog_fext(whichfork);
5041 break;
5042 }
5043 if ((error = xfs_btree_delete(cur, &i)))
5044 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005045 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005046 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005047 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005048 /*
5049 * Deleting the first part of the extent.
5050 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005051 got.br_startoff = del_endoff;
5052 got.br_startblock = del_endblock;
5053 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005054 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005055 if (!cur) {
5056 flags |= xfs_ilog_fext(whichfork);
5057 break;
5058 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005059 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005060 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005061 goto done;
5062 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005063 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005064 /*
5065 * Deleting the last part of the extent.
5066 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005067 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005068 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005069 if (!cur) {
5070 flags |= xfs_ilog_fext(whichfork);
5071 break;
5072 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005073 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005074 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005075 goto done;
5076 break;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005077 case 0:
5078 /*
5079 * Deleting the middle of the extent.
5080 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005081 old = got;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07005082
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005083 got.br_blockcount = del->br_startoff - got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005084 xfs_iext_update_extent(ip, state, icur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005085
Dave Chinner9e5987a72013-02-25 12:31:26 +11005086 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005087 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005088 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005089 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005090
Christoph Hellwige1d75532017-10-17 14:16:21 -07005091 flags |= XFS_ILOG_CORE;
5092 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005093 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005094 if (error)
5095 goto done;
5096 error = xfs_btree_increment(cur, 0, &i);
5097 if (error)
5098 goto done;
5099 cur->bc_rec.b = new;
5100 error = xfs_btree_insert(cur, &i);
5101 if (error && error != -ENOSPC)
5102 goto done;
5103 /*
5104 * If get no-space back from btree insert, it tried a
5105 * split, and we have a zero block reservation. Fix up
5106 * our state and return the error.
5107 */
5108 if (error == -ENOSPC) {
5109 /*
5110 * Reset the cursor, don't trust it after any
5111 * insert operation.
5112 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005113 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005114 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005115 goto done;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005116 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5117 /*
5118 * Update the btree record back
5119 * to the original value.
5120 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005121 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005122 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005123 goto done;
5124 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005125 * Reset the extent record back
5126 * to the original value.
Dave Chinner9e5987a72013-02-25 12:31:26 +11005127 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005128 xfs_iext_update_extent(ip, state, icur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005129 flags = 0;
5130 error = -ENOSPC;
5131 goto done;
5132 }
5133 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5134 } else
5135 flags |= xfs_ilog_fext(whichfork);
5136 XFS_IFORK_NEXT_SET(ip, whichfork,
5137 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005138 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005139 xfs_iext_insert(ip, icur, &new, state);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005140 break;
5141 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005142
5143 /* remove reverse mapping */
Brian Foster0f37d172018-08-01 07:20:34 -07005144 error = xfs_rmap_unmap_extent(tp, ip, whichfork, del);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005145 if (error)
5146 goto done;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005147
Dave Chinner9e5987a72013-02-25 12:31:26 +11005148 /*
5149 * If we need to, add to list of extents to delete.
5150 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005151 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005152 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
Brian Foster0f37d172018-08-01 07:20:34 -07005153 error = xfs_refcount_decrease_extent(tp, del);
Darrick J. Wong62aab202016-10-03 09:11:23 -07005154 if (error)
5155 goto done;
Brian Fosterfcb762f2018-05-09 08:45:04 -07005156 } else {
Brian Foster0f37d172018-08-01 07:20:34 -07005157 __xfs_bmap_add_free(tp, del->br_startblock,
Brian Foster4e529332018-05-10 09:35:42 -07005158 del->br_blockcount, NULL,
5159 (bflags & XFS_BMAPI_NODISCARD) ||
5160 del->br_state == XFS_EXT_UNWRITTEN);
Brian Fosterfcb762f2018-05-09 08:45:04 -07005161 }
Darrick J. Wong62aab202016-10-03 09:11:23 -07005162 }
5163
Dave Chinner9e5987a72013-02-25 12:31:26 +11005164 /*
5165 * Adjust inode # blocks in the file.
5166 */
5167 if (nblks)
5168 ip->i_d.di_nblocks -= nblks;
5169 /*
5170 * Adjust quota data.
5171 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005172 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a72013-02-25 12:31:26 +11005173 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5174
Dave Chinner9e5987a72013-02-25 12:31:26 +11005175done:
5176 *logflagsp = flags;
5177 return error;
5178}
5179
5180/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 * Unmap (remove) blocks from a file.
5182 * If nexts is nonzero then the number of extents to remove is limited to
5183 * that value. If not all extents in the block range can be removed then
5184 * *done is set.
5185 */
5186int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005187__xfs_bunmapi(
Brian Fosterccd9d912018-07-11 22:26:13 -07005188 struct xfs_trans *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005190 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005191 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 int flags, /* misc flags */
Brian Foster2af52842018-07-11 22:26:25 -07005193 xfs_extnum_t nexts) /* number of extents max */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194{
Brian Fosterccd9d912018-07-11 22:26:13 -07005195 struct xfs_btree_cur *cur; /* bmap btree cursor */
5196 struct xfs_bmbt_irec del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 int error; /* error return value */
5198 xfs_extnum_t extno; /* extent number in list */
Brian Fosterccd9d912018-07-11 22:26:13 -07005199 struct xfs_bmbt_irec got; /* current extent record */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07005200 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 int isrt; /* freeing in rt area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 int logflags; /* transaction logging flags */
5203 xfs_extlen_t mod; /* rt extent offset */
Brian Fosterccd9d912018-07-11 22:26:13 -07005204 struct xfs_mount *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 int tmp_logflags; /* partial logging flags */
5206 int wasdel; /* was a delayed alloc extent */
5207 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005209 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005210 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005211 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005212 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005213 struct xfs_iext_cursor icur;
5214 bool done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005216 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005217
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005218 whichfork = xfs_bmapi_whichfork(flags);
5219 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 ifp = XFS_IFORK_PTR(ip, whichfork);
5221 if (unlikely(
5222 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5223 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5224 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5225 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005226 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 }
5228 mp = ip->i_mount;
5229 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005230 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005231
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005232 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 ASSERT(len > 0);
5234 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005235
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005236 /*
5237 * Guesstimate how many blocks we can unmap without running the risk of
5238 * blowing out the transaction with a mix of EFIs and reflink
5239 * adjustments.
5240 */
Darrick J. Wong8c57b882017-12-10 18:03:53 -08005241 if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005242 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5243 else
5244 max_len = len;
5245
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5247 (error = xfs_iread_extents(tp, ip, whichfork)))
5248 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005249 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005250 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 return 0;
5252 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005253 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005254 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwigdc560152017-10-23 16:32:39 -07005255 end = start + len;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005256
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005257 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
Christoph Hellwigdc560152017-10-23 16:32:39 -07005258 *rlen = 0;
5259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 }
Christoph Hellwigdc560152017-10-23 16:32:39 -07005261 end--;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005262
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 logflags = 0;
5264 if (ifp->if_flags & XFS_IFBROOT) {
5265 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005266 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 cur->bc_private.b.flags = 0;
5268 } else
5269 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005270
5271 if (isrt) {
5272 /*
5273 * Synchronize by locking the bitmap inode.
5274 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005275 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005276 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005277 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5278 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005279 }
5280
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 extno = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005282 while (end != (xfs_fileoff_t)-1 && end >= start &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005283 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005285 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 * Just back up to the previous extent, if so.
5287 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005288 if (got.br_startoff > end &&
5289 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5290 done = true;
5291 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 }
5293 /*
5294 * Is the last block of this extent before the range
5295 * we're supposed to delete? If so, we're done.
5296 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005297 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005299 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 break;
5301 /*
5302 * Then deal with the (possibly delayed) allocated space
5303 * we found.
5304 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005306 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005307
5308 /*
5309 * Make sure we don't touch multiple AGF headers out of order
5310 * in a single transaction, as that could cause AB-BA deadlocks.
5311 */
5312 if (!wasdel) {
5313 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5314 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5315 break;
5316 prev_agno = agno;
5317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 if (got.br_startoff < start) {
5319 del.br_startoff = start;
5320 del.br_blockcount -= start - got.br_startoff;
5321 if (!wasdel)
5322 del.br_startblock += start - got.br_startoff;
5323 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005324 if (del.br_startoff + del.br_blockcount > end + 1)
5325 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005326
5327 /* How much can we safely unmap? */
5328 if (max_len < del.br_blockcount) {
5329 del.br_startoff += del.br_blockcount - max_len;
5330 if (!wasdel)
5331 del.br_startblock += del.br_blockcount - max_len;
5332 del.br_blockcount = max_len;
5333 }
5334
Dave Chinner0703a8e2018-06-08 09:54:22 -07005335 if (!isrt)
5336 goto delete;
5337
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 sum = del.br_startblock + del.br_blockcount;
Dave Chinner0703a8e2018-06-08 09:54:22 -07005339 div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod);
5340 if (mod) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 /*
5342 * Realtime extent not lined up at the end.
5343 * The extent could have been split into written
5344 * and unwritten pieces, or we could just be
5345 * unmapping part of it. But we can't really
5346 * get rid of part of a realtime extent.
5347 */
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11005348 if (del.br_state == XFS_EXT_UNWRITTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 /*
5350 * This piece is unwritten, or we're not
5351 * using unwritten extents. Skip over it.
5352 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005353 ASSERT(end >= mod);
5354 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 del.br_blockcount : mod;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005356 if (end < got.br_startoff &&
5357 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5358 done = true;
5359 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 }
5361 continue;
5362 }
5363 /*
5364 * It's written, turn it unwritten.
5365 * This is better than zeroing it.
5366 */
5367 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005368 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 /*
5370 * If this spans a realtime extent boundary,
5371 * chop it back to the start of the one we end at.
5372 */
5373 if (del.br_blockcount > mod) {
5374 del.br_startoff += del.br_blockcount - mod;
5375 del.br_startblock += del.br_blockcount - mod;
5376 del.br_blockcount = mod;
5377 }
5378 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005379 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005380 whichfork, &icur, &cur, &del,
Brian Foster92f9da32018-07-11 22:26:28 -07005381 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 if (error)
5383 goto error0;
5384 goto nodelete;
5385 }
Dave Chinner0703a8e2018-06-08 09:54:22 -07005386 div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod);
5387 if (mod) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 /*
5389 * Realtime extent is lined up at the end but not
5390 * at the front. We'll get rid of full extents if
5391 * we can.
5392 */
5393 mod = mp->m_sb.sb_rextsize - mod;
5394 if (del.br_blockcount > mod) {
5395 del.br_blockcount -= mod;
5396 del.br_startoff += mod;
5397 del.br_startblock += mod;
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11005398 } else if (del.br_startoff == start &&
5399 (del.br_state == XFS_EXT_UNWRITTEN ||
5400 tp->t_blk_res == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 /*
5402 * Can't make it unwritten. There isn't
5403 * a full extent here so just skip it.
5404 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005405 ASSERT(end >= del.br_blockcount);
5406 end -= del.br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005407 if (got.br_startoff > end &&
5408 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5409 done = true;
5410 break;
5411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 continue;
5413 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005414 struct xfs_bmbt_irec prev;
5415
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 /*
5417 * This one is already unwritten.
5418 * It must have a written left neighbor.
5419 * Unwrite the killed part of that one and
5420 * try again.
5421 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005422 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5423 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005425 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 ASSERT(del.br_startblock ==
5427 prev.br_startblock + prev.br_blockcount);
5428 if (prev.br_startoff < start) {
5429 mod = start - prev.br_startoff;
5430 prev.br_blockcount -= mod;
5431 prev.br_startblock += mod;
5432 prev.br_startoff = start;
5433 }
5434 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005435 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005436 ip, whichfork, &icur, &cur,
Brian Foster92f9da32018-07-11 22:26:28 -07005437 &prev, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 if (error)
5439 goto error0;
5440 goto nodelete;
5441 } else {
5442 ASSERT(del.br_state == XFS_EXT_NORM);
5443 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005444 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005445 ip, whichfork, &icur, &cur,
Brian Foster92f9da32018-07-11 22:26:28 -07005446 &del, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 if (error)
5448 goto error0;
5449 goto nodelete;
5450 }
5451 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005452
Dave Chinner0703a8e2018-06-08 09:54:22 -07005453delete:
Brian Fosterb2706a02016-03-15 11:42:46 +11005454 if (wasdel) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005455 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005456 &got, &del);
5457 } else {
Brian Foster81ba8f32018-07-11 22:26:16 -07005458 error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5459 &del, &tmp_logflags, whichfork,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005460 flags);
5461 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005462 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005463
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 if (error)
5465 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005466
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005467 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005468 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 /*
5471 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005473 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005474 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5475 (got.br_startoff > end &&
5476 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5477 done = true;
5478 break;
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 extno++;
5481 }
5482 }
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005483 if (done || end == (xfs_fileoff_t)-1 || end < start)
Darrick J. Wong44535932016-10-03 09:11:29 -07005484 *rlen = 0;
5485 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005486 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005487
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 /*
5489 * Convert to a btree if necessary.
5490 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005491 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07005493 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5494 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 logflags |= tmp_logflags;
Christoph Hellwigb101e332019-02-15 08:02:47 -08005496 } else {
5497 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 }
Christoph Hellwigb101e332019-02-15 08:02:47 -08005500
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501error0:
5502 /*
5503 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005504 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005506 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005508 logflags &= ~xfs_ilog_fext(whichfork);
5509 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005511 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 /*
5513 * Log inode even in the error case, if the transaction
5514 * is dirty we'll need to shut down the filesystem.
5515 */
5516 if (logflags)
5517 xfs_trans_log_inode(tp, ip, logflags);
5518 if (cur) {
Brian Fostercf612de2018-07-11 22:26:29 -07005519 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 cur->bc_private.b.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07005521 xfs_btree_del_cursor(cur, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 }
5523 return error;
5524}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005525
Darrick J. Wong44535932016-10-03 09:11:29 -07005526/* Unmap a range of a file. */
5527int
5528xfs_bunmapi(
5529 xfs_trans_t *tp,
5530 struct xfs_inode *ip,
5531 xfs_fileoff_t bno,
5532 xfs_filblks_t len,
5533 int flags,
5534 xfs_extnum_t nexts,
Darrick J. Wong44535932016-10-03 09:11:29 -07005535 int *done)
5536{
5537 int error;
5538
Brian Foster2af52842018-07-11 22:26:25 -07005539 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
Darrick J. Wong44535932016-10-03 09:11:29 -07005540 *done = (len == 0);
5541 return error;
5542}
5543
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005544/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005545 * Determine whether an extent shift can be accomplished by a merge with the
5546 * extent that precedes the target hole of the shift.
5547 */
5548STATIC bool
5549xfs_bmse_can_merge(
5550 struct xfs_bmbt_irec *left, /* preceding extent */
5551 struct xfs_bmbt_irec *got, /* current extent to shift */
5552 xfs_fileoff_t shift) /* shift fsb */
5553{
5554 xfs_fileoff_t startoff;
5555
5556 startoff = got->br_startoff - shift;
5557
5558 /*
5559 * The extent, once shifted, must be adjacent in-file and on-disk with
5560 * the preceding extent.
5561 */
5562 if ((left->br_startoff + left->br_blockcount != startoff) ||
5563 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5564 (left->br_state != got->br_state) ||
5565 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5566 return false;
5567
5568 return true;
5569}
5570
5571/*
5572 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5573 * hole in the file. If an extent shift would result in the extent being fully
5574 * adjacent to the extent that currently precedes the hole, we can merge with
5575 * the preceding extent rather than do the shift.
5576 *
5577 * This function assumes the caller has verified a shift-by-merge is possible
5578 * with the provided extents via xfs_bmse_can_merge().
5579 */
5580STATIC int
5581xfs_bmse_merge(
Brian Foster0f37d172018-08-01 07:20:34 -07005582 struct xfs_trans *tp,
Brian Fosterddb19e32014-09-23 15:38:09 +10005583 struct xfs_inode *ip,
5584 int whichfork,
5585 xfs_fileoff_t shift, /* shift fsb */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005586 struct xfs_iext_cursor *icur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005587 struct xfs_bmbt_irec *got, /* extent to shift */
5588 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005589 struct xfs_btree_cur *cur,
Brian Foster0f37d172018-08-01 07:20:34 -07005590 int *logflags) /* output */
Brian Fosterddb19e32014-09-23 15:38:09 +10005591{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005592 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005593 xfs_filblks_t blockcount;
5594 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005595 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005596
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005597 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005598
5599 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5600 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005601 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005602
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005603 new = *left;
5604 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005605
5606 /*
5607 * Update the on-disk extent count, the btree if necessary and log the
5608 * inode.
5609 */
5610 XFS_IFORK_NEXT_SET(ip, whichfork,
5611 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5612 *logflags |= XFS_ILOG_CORE;
5613 if (!cur) {
5614 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005615 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005616 }
5617
5618 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005619 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005620 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005621 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005622 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005623
5624 error = xfs_btree_delete(cur, &i);
5625 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005626 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005627 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005628
5629 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005630 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005631 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005632 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005633 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005634
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005635 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005636 if (error)
5637 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005638
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005639done:
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005640 xfs_iext_remove(ip, icur, 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005641 xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur);
5642 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5643 &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005644
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005645 /* update reverse mapping. rmap functions merge the rmaps for us */
Brian Foster0f37d172018-08-01 07:20:34 -07005646 error = xfs_rmap_unmap_extent(tp, ip, whichfork, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005647 if (error)
5648 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005649 memcpy(&new, got, sizeof(new));
5650 new.br_startoff = left->br_startoff + left->br_blockcount;
Brian Foster0f37d172018-08-01 07:20:34 -07005651 return xfs_rmap_map_extent(tp, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005652}
5653
Christoph Hellwigbf806282017-10-19 11:07:34 -07005654static int
5655xfs_bmap_shift_update_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07005656 struct xfs_trans *tp,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005657 struct xfs_inode *ip,
5658 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005659 struct xfs_iext_cursor *icur,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005660 struct xfs_bmbt_irec *got,
5661 struct xfs_btree_cur *cur,
5662 int *logflags,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005663 xfs_fileoff_t startoff)
Brian Fostera979bdf2014-09-23 15:39:04 +10005664{
Christoph Hellwigbf806282017-10-19 11:07:34 -07005665 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005666 struct xfs_bmbt_irec prev = *got;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005667 int error, i;
Brian Fostera979bdf2014-09-23 15:39:04 +10005668
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005669 *logflags |= XFS_ILOG_CORE;
5670
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005671 got->br_startoff = startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005672
5673 if (cur) {
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005674 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005675 if (error)
5676 return error;
5677 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5678
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005679 error = xfs_bmbt_update(cur, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005680 if (error)
5681 return error;
5682 } else {
5683 *logflags |= XFS_ILOG_DEXT;
5684 }
5685
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005686 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5687 got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005688
Darrick J. Wong9c194642016-08-03 12:16:05 +10005689 /* update reverse mapping */
Brian Foster0f37d172018-08-01 07:20:34 -07005690 error = xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005691 if (error)
5692 return error;
Brian Foster0f37d172018-08-01 07:20:34 -07005693 return xfs_rmap_map_extent(tp, ip, whichfork, got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005694}
5695
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005696int
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005697xfs_bmap_collapse_extents(
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005698 struct xfs_trans *tp,
5699 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005700 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005701 xfs_fileoff_t offset_shift_fsb,
Brian Foster333f9502018-07-11 22:26:27 -07005702 bool *done)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005703{
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005704 int whichfork = XFS_DATA_FORK;
5705 struct xfs_mount *mp = ip->i_mount;
5706 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5707 struct xfs_btree_cur *cur = NULL;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005708 struct xfs_bmbt_irec got, prev;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005709 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005710 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005711 int error = 0;
5712 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005713
5714 if (unlikely(XFS_TEST_ERROR(
5715 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5716 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005717 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005718 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005719 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005720 }
5721
5722 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005723 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005724
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005725 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005726
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005727 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005728 error = xfs_iread_extents(tp, ip, whichfork);
5729 if (error)
5730 return error;
5731 }
5732
Brian Fosterddb19e32014-09-23 15:38:09 +10005733 if (ifp->if_flags & XFS_IFBROOT) {
5734 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Brian Fosterddb19e32014-09-23 15:38:09 +10005735 cur->bc_private.b.flags = 0;
5736 }
5737
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005738 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005739 *done = true;
5740 goto del_cursor;
5741 }
Eric Sandeend41c6172017-11-27 18:23:32 -08005742 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5743 del_cursor);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005744
Christoph Hellwigbf806282017-10-19 11:07:34 -07005745 new_startoff = got.br_startoff - offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005746 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005747 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5748 error = -EINVAL;
5749 goto del_cursor;
5750 }
5751
Christoph Hellwigbf806282017-10-19 11:07:34 -07005752 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
Brian Foster0f37d172018-08-01 07:20:34 -07005753 error = xfs_bmse_merge(tp, ip, whichfork,
5754 offset_shift_fsb, &icur, &got, &prev,
5755 cur, &logflags);
Christoph Hellwigbf806282017-10-19 11:07:34 -07005756 if (error)
5757 goto del_cursor;
5758 goto done;
5759 }
5760 } else {
5761 if (got.br_startoff < offset_shift_fsb) {
5762 error = -EINVAL;
5763 goto del_cursor;
5764 }
5765 }
5766
Brian Foster0f37d172018-08-01 07:20:34 -07005767 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5768 cur, &logflags, new_startoff);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005769 if (error)
5770 goto del_cursor;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005771
Christoph Hellwig42630362017-11-03 10:34:41 -07005772done:
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005773 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5774 *done = true;
5775 goto del_cursor;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005776 }
5777
Christoph Hellwigbf806282017-10-19 11:07:34 -07005778 *next_fsb = got.br_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005779del_cursor:
5780 if (cur)
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07005781 xfs_btree_del_cursor(cur, error);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005782 if (logflags)
5783 xfs_trans_log_inode(tp, ip, logflags);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005784 return error;
5785}
5786
Darrick J. Wongf62cb482018-06-21 23:26:57 -07005787/* Make sure we won't be right-shifting an extent past the maximum bound. */
5788int
5789xfs_bmap_can_insert_extents(
5790 struct xfs_inode *ip,
5791 xfs_fileoff_t off,
5792 xfs_fileoff_t shift)
5793{
5794 struct xfs_bmbt_irec got;
5795 int is_empty;
5796 int error = 0;
5797
5798 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5799
5800 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5801 return -EIO;
5802
5803 xfs_ilock(ip, XFS_ILOCK_EXCL);
5804 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
5805 if (!error && !is_empty && got.br_startoff >= off &&
5806 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
5807 error = -EINVAL;
5808 xfs_iunlock(ip, XFS_ILOCK_EXCL);
5809
5810 return error;
5811}
5812
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005813int
5814xfs_bmap_insert_extents(
5815 struct xfs_trans *tp,
5816 struct xfs_inode *ip,
5817 xfs_fileoff_t *next_fsb,
5818 xfs_fileoff_t offset_shift_fsb,
5819 bool *done,
Brian Foster333f9502018-07-11 22:26:27 -07005820 xfs_fileoff_t stop_fsb)
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005821{
5822 int whichfork = XFS_DATA_FORK;
5823 struct xfs_mount *mp = ip->i_mount;
5824 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5825 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005826 struct xfs_bmbt_irec got, next;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005827 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005828 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005829 int error = 0;
5830 int logflags = 0;
5831
5832 if (unlikely(XFS_TEST_ERROR(
5833 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5834 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5835 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5836 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
5837 return -EFSCORRUPTED;
5838 }
5839
5840 if (XFS_FORCED_SHUTDOWN(mp))
5841 return -EIO;
5842
5843 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5844
5845 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5846 error = xfs_iread_extents(tp, ip, whichfork);
5847 if (error)
5848 return error;
5849 }
5850
5851 if (ifp->if_flags & XFS_IFBROOT) {
5852 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005853 cur->bc_private.b.flags = 0;
5854 }
5855
Namjae Jeona904b1c2015-03-25 15:08:56 +11005856 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005857 xfs_iext_last(ifp, &icur);
5858 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005859 stop_fsb > got.br_startoff) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005860 *done = true;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005861 goto del_cursor;
5862 }
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005863 } else {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005864 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005865 *done = true;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005866 goto del_cursor;
5867 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005868 }
Eric Sandeend41c6172017-11-27 18:23:32 -08005869 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5870 del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005871
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005872 if (stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005873 error = -EIO;
5874 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005875 }
5876
Christoph Hellwigbf806282017-10-19 11:07:34 -07005877 new_startoff = got.br_startoff + offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005878 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005879 if (new_startoff + got.br_blockcount > next.br_startoff) {
5880 error = -EINVAL;
5881 goto del_cursor;
5882 }
5883
5884 /*
5885 * Unlike a left shift (which involves a hole punch), a right
5886 * shift does not modify extent neighbors in any way. We should
5887 * never find mergeable extents in this scenario. Check anyways
5888 * and warn if we encounter two extents that could be one.
5889 */
5890 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5891 WARN_ON_ONCE(1);
5892 }
5893
Brian Foster0f37d172018-08-01 07:20:34 -07005894 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5895 cur, &logflags, new_startoff);
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005896 if (error)
5897 goto del_cursor;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005898
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005899 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005900 stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005901 *done = true;
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005902 goto del_cursor;
5903 }
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005904
5905 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005906del_cursor:
5907 if (cur)
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07005908 xfs_btree_del_cursor(cur, error);
Brian Fosterca446d82014-09-02 12:12:53 +10005909 if (logflags)
5910 xfs_trans_log_inode(tp, ip, logflags);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005911 return error;
5912}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005913
5914/*
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005915 * Splits an extent into two extents at split_fsb block such that it is the
5916 * first block of the current_ext. @ext is a target extent to be split.
5917 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5918 * hole or the first block of extents, just return 0.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005919 */
5920STATIC int
5921xfs_bmap_split_extent_at(
5922 struct xfs_trans *tp,
5923 struct xfs_inode *ip,
Brian Foster4b77a082018-07-11 22:26:27 -07005924 xfs_fileoff_t split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005925{
5926 int whichfork = XFS_DATA_FORK;
5927 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005928 struct xfs_bmbt_irec got;
5929 struct xfs_bmbt_irec new; /* split extent */
5930 struct xfs_mount *mp = ip->i_mount;
5931 struct xfs_ifork *ifp;
5932 xfs_fsblock_t gotblkcnt; /* new block count for got */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005933 struct xfs_iext_cursor icur;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005934 int error = 0;
5935 int logflags = 0;
5936 int i = 0;
5937
5938 if (unlikely(XFS_TEST_ERROR(
5939 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5940 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005941 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11005942 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5943 XFS_ERRLEVEL_LOW, mp);
5944 return -EFSCORRUPTED;
5945 }
5946
5947 if (XFS_FORCED_SHUTDOWN(mp))
5948 return -EIO;
5949
5950 ifp = XFS_IFORK_PTR(ip, whichfork);
5951 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5952 /* Read in all the extents */
5953 error = xfs_iread_extents(tp, ip, whichfork);
5954 if (error)
5955 return error;
5956 }
5957
5958 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005959 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005960 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005961 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005962 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005963 return 0;
5964
5965 gotblkcnt = split_fsb - got.br_startoff;
5966 new.br_startoff = split_fsb;
5967 new.br_startblock = got.br_startblock + gotblkcnt;
5968 new.br_blockcount = got.br_blockcount - gotblkcnt;
5969 new.br_state = got.br_state;
5970
5971 if (ifp->if_flags & XFS_IFBROOT) {
5972 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005973 cur->bc_private.b.flags = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005974 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005975 if (error)
5976 goto del_cursor;
5977 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5978 }
5979
Namjae Jeona904b1c2015-03-25 15:08:56 +11005980 got.br_blockcount = gotblkcnt;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005981 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
5982 &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005983
5984 logflags = XFS_ILOG_CORE;
5985 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005986 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005987 if (error)
5988 goto del_cursor;
5989 } else
5990 logflags |= XFS_ILOG_DEXT;
5991
5992 /* Add new extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005993 xfs_iext_next(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005994 xfs_iext_insert(ip, &icur, &new, 0);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005995 XFS_IFORK_NEXT_SET(ip, whichfork,
5996 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5997
5998 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005999 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006000 if (error)
6001 goto del_cursor;
6002 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006003 error = xfs_btree_insert(cur, &i);
6004 if (error)
6005 goto del_cursor;
6006 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6007 }
6008
6009 /*
6010 * Convert to a btree if necessary.
6011 */
6012 if (xfs_bmap_needs_btree(ip, whichfork)) {
6013 int tmp_logflags; /* partial log flag return val */
6014
6015 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07006016 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
6017 &tmp_logflags, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006018 logflags |= tmp_logflags;
6019 }
6020
6021del_cursor:
6022 if (cur) {
6023 cur->bc_private.b.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07006024 xfs_btree_del_cursor(cur, error);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006025 }
6026
6027 if (logflags)
6028 xfs_trans_log_inode(tp, ip, logflags);
6029 return error;
6030}
6031
6032int
6033xfs_bmap_split_extent(
6034 struct xfs_inode *ip,
6035 xfs_fileoff_t split_fsb)
6036{
6037 struct xfs_mount *mp = ip->i_mount;
6038 struct xfs_trans *tp;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006039 int error;
6040
Christoph Hellwig253f4912016-04-06 09:19:55 +10006041 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6042 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6043 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006044 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006045
6046 xfs_ilock(ip, XFS_ILOCK_EXCL);
6047 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6048
Brian Foster4b77a082018-07-11 22:26:27 -07006049 error = xfs_bmap_split_extent_at(tp, ip, split_fsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006050 if (error)
6051 goto out;
6052
Christoph Hellwig70393312015-06-04 13:48:08 +10006053 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006054
6055out:
Christoph Hellwig4906e212015-06-04 13:47:56 +10006056 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006057 return error;
6058}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006059
6060/* Deferred mapping is only for real extents in the data fork. */
6061static bool
6062xfs_bmap_is_update_needed(
6063 struct xfs_bmbt_irec *bmap)
6064{
6065 return bmap->br_startblock != HOLESTARTBLOCK &&
6066 bmap->br_startblock != DELAYSTARTBLOCK;
6067}
6068
6069/* Record a bmap intent. */
6070static int
6071__xfs_bmap_add(
Brian Foster0f37d172018-08-01 07:20:34 -07006072 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006073 enum xfs_bmap_intent_type type,
6074 struct xfs_inode *ip,
6075 int whichfork,
6076 struct xfs_bmbt_irec *bmap)
6077{
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006078 struct xfs_bmap_intent *bi;
6079
Brian Foster0f37d172018-08-01 07:20:34 -07006080 trace_xfs_bmap_defer(tp->t_mountp,
6081 XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock),
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006082 type,
Brian Foster0f37d172018-08-01 07:20:34 -07006083 XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock),
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006084 ip->i_ino, whichfork,
6085 bmap->br_startoff,
6086 bmap->br_blockcount,
6087 bmap->br_state);
6088
6089 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6090 INIT_LIST_HEAD(&bi->bi_list);
6091 bi->bi_type = type;
6092 bi->bi_owner = ip;
6093 bi->bi_whichfork = whichfork;
6094 bi->bi_bmap = *bmap;
6095
Brian Foster0f37d172018-08-01 07:20:34 -07006096 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006097 return 0;
6098}
6099
6100/* Map an extent into a file. */
6101int
6102xfs_bmap_map_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07006103 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006104 struct xfs_inode *ip,
6105 struct xfs_bmbt_irec *PREV)
6106{
6107 if (!xfs_bmap_is_update_needed(PREV))
6108 return 0;
6109
Brian Foster0f37d172018-08-01 07:20:34 -07006110 return __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, XFS_DATA_FORK, PREV);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006111}
6112
6113/* Unmap an extent out of a file. */
6114int
6115xfs_bmap_unmap_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07006116 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006117 struct xfs_inode *ip,
6118 struct xfs_bmbt_irec *PREV)
6119{
6120 if (!xfs_bmap_is_update_needed(PREV))
6121 return 0;
6122
Brian Foster0f37d172018-08-01 07:20:34 -07006123 return __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, XFS_DATA_FORK, PREV);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006124}
6125
6126/*
6127 * Process one of the deferred bmap operations. We pass back the
6128 * btree cursor to maintain our lock on the bmapbt between calls.
6129 */
6130int
6131xfs_bmap_finish_one(
6132 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006133 struct xfs_inode *ip,
6134 enum xfs_bmap_intent_type type,
6135 int whichfork,
6136 xfs_fileoff_t startoff,
6137 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006138 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006139 xfs_exntst_t state)
6140{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006141 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006142
Brian Foster37283792018-07-11 22:26:23 -07006143 ASSERT(tp->t_firstblock == NULLFSBLOCK);
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006144
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006145 trace_xfs_bmap_deferred(tp->t_mountp,
6146 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6147 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006148 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006149
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006150 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006151 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006152
6153 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006154 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006155 return -EIO;
6156
6157 switch (type) {
6158 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006159 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Brian Fosterff3edf22018-07-11 22:26:14 -07006160 startblock, 0);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006161 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006162 break;
6163 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006164 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
Brian Foster2af52842018-07-11 22:26:25 -07006165 XFS_BMAPI_REMAP, 1);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006166 break;
6167 default:
6168 ASSERT(0);
6169 error = -EFSCORRUPTED;
6170 }
6171
6172 return error;
6173}
Darrick J. Wong30b09842018-03-23 10:06:52 -07006174
6175/* Check that an inode's extent does not have invalid flags or bad ranges. */
6176xfs_failaddr_t
6177xfs_bmap_validate_extent(
6178 struct xfs_inode *ip,
6179 int whichfork,
6180 struct xfs_bmbt_irec *irec)
6181{
6182 struct xfs_mount *mp = ip->i_mount;
6183 xfs_fsblock_t endfsb;
6184 bool isrt;
6185
6186 isrt = XFS_IS_REALTIME_INODE(ip);
6187 endfsb = irec->br_startblock + irec->br_blockcount - 1;
6188 if (isrt) {
6189 if (!xfs_verify_rtbno(mp, irec->br_startblock))
6190 return __this_address;
6191 if (!xfs_verify_rtbno(mp, endfsb))
6192 return __this_address;
6193 } else {
6194 if (!xfs_verify_fsbno(mp, irec->br_startblock))
6195 return __this_address;
6196 if (!xfs_verify_fsbno(mp, endfsb))
6197 return __this_address;
6198 if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) !=
6199 XFS_FSB_TO_AGNO(mp, endfsb))
6200 return __this_address;
6201 }
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11006202 if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6203 return __this_address;
Darrick J. Wong30b09842018-03-23 10:06:52 -07006204 return NULL;
6205}