blob: 48502cb9990f184a55b780372adaef3bda406509 [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;
373 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
374 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;
457 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
458 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
622 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
623 &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);
735 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
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;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000881 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
882
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);
1194 ASSERT(level > 0);
1195 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1196 bno = be64_to_cpu(*pp);
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -08001197
Dave Chinner9e5987a72013-02-25 12:31:26 +11001198 /*
1199 * Go down the tree until leaf level is reached, following the first
1200 * pointer (leftmost) at each level.
1201 */
1202 while (level-- > 0) {
1203 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1204 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1205 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001206 goto out;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001207 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001208 if (level == 0)
1209 break;
1210 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1211 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001212 XFS_WANT_CORRUPTED_GOTO(mp,
Darrick J. Wong59f6fec2018-01-08 10:51:00 -08001213 xfs_verify_fsbno(mp, bno), out_brelse);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001214 xfs_trans_brelse(tp, bp);
1215 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001216
Dave Chinner9e5987a72013-02-25 12:31:26 +11001217 /*
1218 * Here with bp and block set to the leftmost leaf node in the tree.
1219 */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001220 i = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001221 xfs_iext_first(ifp, &icur);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001222
Dave Chinner9e5987a72013-02-25 12:31:26 +11001223 /*
1224 * Loop over all leaf nodes. Copy information to the extent records.
1225 */
1226 for (;;) {
1227 xfs_bmbt_rec_t *frp;
1228 xfs_fsblock_t nextbno;
1229 xfs_extnum_t num_recs;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001230
1231 num_recs = xfs_btree_get_numrecs(block);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001232 if (unlikely(i + num_recs > nextents)) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001233 xfs_warn(ip->i_mount,
1234 "corrupt dinode %Lu, (btree extents).",
1235 (unsigned long long) ip->i_ino);
Darrick J. Wong90a58f92018-03-23 10:06:52 -07001236 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1237 __func__, block, sizeof(*block),
1238 __this_address);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001239 error = -EFSCORRUPTED;
1240 goto out_brelse;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001241 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001242 /*
1243 * Read-ahead the next leaf block, if any.
1244 */
1245 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1246 if (nextbno != NULLFSBLOCK)
1247 xfs_btree_reada_bufl(mp, nextbno, 1,
1248 &xfs_bmbt_buf_ops);
1249 /*
1250 * Copy records into the extent records.
1251 */
1252 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Christoph Hellwig6bdcf262017-11-03 10:34:46 -07001253 for (j = 0; j < num_recs; j++, frp++, i++) {
Darrick J. Wong30b09842018-03-23 10:06:52 -07001254 xfs_failaddr_t fa;
1255
Christoph Hellwigdac9c9b2017-11-03 10:34:47 -07001256 xfs_bmbt_disk_get_all(frp, &new);
Darrick J. Wong30b09842018-03-23 10:06:52 -07001257 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1258 if (fa) {
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001259 error = -EFSCORRUPTED;
Darrick J. Wong30b09842018-03-23 10:06:52 -07001260 xfs_inode_verifier_error(ip, error,
1261 "xfs_iread_extents(2)",
1262 frp, sizeof(*frp), fa);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001263 goto out_brelse;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001264 }
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001265 xfs_iext_insert(ip, &icur, &new, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001266 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
1267 xfs_iext_next(ifp, &icur);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001268 }
1269 xfs_trans_brelse(tp, bp);
1270 bno = nextbno;
1271 /*
1272 * If we've reached the end, stop.
1273 */
1274 if (bno == NULLFSBLOCK)
1275 break;
1276 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1277 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1278 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001279 goto out;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001280 block = XFS_BUF_TO_BLOCK(bp);
1281 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001282
1283 if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) {
1284 error = -EFSCORRUPTED;
1285 goto out;
1286 }
Eric Sandeen5d829302016-11-08 12:59:42 +11001287 ASSERT(i == xfs_iext_count(ifp));
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001288
1289 ifp->if_flags |= XFS_IFEXTENTS;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001290 return 0;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001291
1292out_brelse:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001293 xfs_trans_brelse(tp, bp);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001294out:
1295 xfs_iext_destroy(ifp);
1296 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001297}
1298
Dave Chinner9e5987a72013-02-25 12:31:26 +11001299/*
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001300 * Returns the relative block number of the first unused block(s) in the given
1301 * fork with at least "len" logically contiguous blocks free. This is the
1302 * lowest-address hole if the fork has holes, else the first block past the end
1303 * of fork. Return 0 if the fork is currently local (in-inode).
Dave Chinner9e5987a72013-02-25 12:31:26 +11001304 */
1305int /* error */
1306xfs_bmap_first_unused(
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001307 struct xfs_trans *tp, /* transaction pointer */
1308 struct xfs_inode *ip, /* incore inode */
1309 xfs_extlen_t len, /* size of hole to find */
1310 xfs_fileoff_t *first_unused, /* unused block */
1311 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001312{
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001313 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1314 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001315 struct xfs_iext_cursor icur;
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001316 xfs_fileoff_t lastaddr = 0;
1317 xfs_fileoff_t lowest, max;
1318 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001319
1320 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1321 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1322 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001323
Dave Chinner9e5987a72013-02-25 12:31:26 +11001324 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1325 *first_unused = 0;
1326 return 0;
1327 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001328
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001329 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1330 error = xfs_iread_extents(tp, ip, whichfork);
1331 if (error)
1332 return error;
1333 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001334
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001335 lowest = max = *first_unused;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001336 for_each_xfs_iext(ifp, &icur, &got) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001337 /*
1338 * See if the hole before this extent will work.
1339 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001340 if (got.br_startoff >= lowest + len &&
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001341 got.br_startoff - max >= len)
1342 break;
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001343 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001344 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1345 }
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001346
Dave Chinner9e5987a72013-02-25 12:31:26 +11001347 *first_unused = max;
1348 return 0;
1349}
1350
1351/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001352 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a72013-02-25 12:31:26 +11001353 * last_block (input value) in the file.
1354 * This is not based on i_size, it is based on the extent records.
1355 * Returns 0 for local files, as they do not have extent records.
1356 */
1357int /* error */
1358xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001359 struct xfs_trans *tp, /* transaction pointer */
1360 struct xfs_inode *ip, /* incore inode */
1361 xfs_fileoff_t *last_block, /* last block */
1362 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001363{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001364 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1365 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001366 struct xfs_iext_cursor icur;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001367 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001368
Christoph Hellwig86685f72016-11-24 11:39:38 +11001369 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1370 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001371 *last_block = 0;
1372 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001373 case XFS_DINODE_FMT_BTREE:
1374 case XFS_DINODE_FMT_EXTENTS:
1375 break;
1376 default:
1377 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001378 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001379
1380 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1381 error = xfs_iread_extents(tp, ip, whichfork);
1382 if (error)
1383 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001384 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001385
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001386 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
Christoph Hellwigdc560152017-10-23 16:32:39 -07001387 *last_block = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001388 return 0;
1389}
1390
Dave Chinner68988112013-08-12 20:49:42 +10001391int
Dave Chinner9e5987a72013-02-25 12:31:26 +11001392xfs_bmap_last_extent(
1393 struct xfs_trans *tp,
1394 struct xfs_inode *ip,
1395 int whichfork,
1396 struct xfs_bmbt_irec *rec,
1397 int *is_empty)
1398{
1399 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001400 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001401 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001402
1403 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1404 error = xfs_iread_extents(tp, ip, whichfork);
1405 if (error)
1406 return error;
1407 }
1408
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001409 xfs_iext_last(ifp, &icur);
1410 if (!xfs_iext_get_extent(ifp, &icur, rec))
Dave Chinner9e5987a72013-02-25 12:31:26 +11001411 *is_empty = 1;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001412 else
1413 *is_empty = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001414 return 0;
1415}
1416
1417/*
1418 * Check the last inode extent to determine whether this allocation will result
1419 * in blocks being allocated at the end of the file. When we allocate new data
1420 * blocks at the end of the file which do not start at the previous data block,
1421 * we will try to align the new blocks at stripe unit boundaries.
1422 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001423 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a72013-02-25 12:31:26 +11001424 * at, or past the EOF.
1425 */
1426STATIC int
1427xfs_bmap_isaeof(
1428 struct xfs_bmalloca *bma,
1429 int whichfork)
1430{
1431 struct xfs_bmbt_irec rec;
1432 int is_empty;
1433 int error;
1434
Thomas Meyer749f24f2017-10-09 11:38:54 -07001435 bma->aeof = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001436 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1437 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001438 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001439 return error;
1440
Dave Chinner6e708bc2013-11-22 10:41:16 +11001441 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001442 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001443 return 0;
1444 }
1445
Dave Chinner9e5987a72013-02-25 12:31:26 +11001446 /*
1447 * Check if we are allocation or past the last extent, or at least into
1448 * the last delayed allocated extent.
1449 */
1450 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1451 (bma->offset >= rec.br_startoff &&
1452 isnullstartblock(rec.br_startblock));
1453 return 0;
1454}
1455
1456/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001457 * Returns the file-relative block number of the first block past eof in
1458 * the file. This is not based on i_size, it is based on the extent records.
1459 * Returns 0 for local files, as they do not have extent records.
1460 */
1461int
1462xfs_bmap_last_offset(
Dave Chinner9e5987a72013-02-25 12:31:26 +11001463 struct xfs_inode *ip,
1464 xfs_fileoff_t *last_block,
1465 int whichfork)
1466{
1467 struct xfs_bmbt_irec rec;
1468 int is_empty;
1469 int error;
1470
1471 *last_block = 0;
1472
1473 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1474 return 0;
1475
1476 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1477 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001478 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001479
1480 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1481 if (error || is_empty)
1482 return error;
1483
1484 *last_block = rec.br_startoff + rec.br_blockcount;
1485 return 0;
1486}
1487
1488/*
1489 * Returns whether the selected fork of the inode has exactly one
1490 * block or not. For the data fork we check this matches di_size,
1491 * implying the file's range is 0..bsize-1.
1492 */
1493int /* 1=>1 block, 0=>otherwise */
1494xfs_bmap_one_block(
1495 xfs_inode_t *ip, /* incore inode */
1496 int whichfork) /* data or attr fork */
1497{
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07001498 struct xfs_ifork *ifp; /* inode fork pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001499 int rval; /* return value */
1500 xfs_bmbt_irec_t s; /* internal version of extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001501 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001502
1503#ifndef DEBUG
1504 if (whichfork == XFS_DATA_FORK)
1505 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1506#endif /* !DEBUG */
1507 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1508 return 0;
1509 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1510 return 0;
1511 ifp = XFS_IFORK_PTR(ip, whichfork);
1512 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001513 xfs_iext_first(ifp, &icur);
1514 xfs_iext_get_extent(ifp, &icur, &s);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001515 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1516 if (rval && whichfork == XFS_DATA_FORK)
1517 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1518 return rval;
1519}
1520
1521/*
1522 * Extent tree manipulation functions used during allocation.
1523 */
1524
1525/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001526 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 */
1528STATIC int /* error */
1529xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001530 struct xfs_bmalloca *bma,
1531 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001533 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 int i; /* temp state */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07001536 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 xfs_fileoff_t new_endoff; /* end offset of new entry */
1538 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1539 /* left is 0, right is 1, prev is 2 */
1540 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07001541 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001542 xfs_filblks_t da_new; /* new count del alloc blocks used */
1543 xfs_filblks_t da_old; /* old count del alloc blocks used */
1544 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001546 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001547 xfs_extnum_t *nextents;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001548 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001550 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001551 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001552 ASSERT(whichfork != XFS_ATTR_FORK);
1553 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1554 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001555
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001556 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001557 ASSERT(!bma->cur ||
1558 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001559
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001560 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562#define LEFT r[0]
1563#define RIGHT r[1]
1564#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 /*
1567 * Set up a bunch of variables to make the tests simpler.
1568 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001569 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001571 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 ASSERT(PREV.br_startoff <= new->br_startoff);
1573 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001574
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001575 da_old = startblockval(PREV.br_startblock);
1576 da_new = 0;
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 /*
1579 * Set flags determining what part of the previous delayed allocation
1580 * extent is being replaced by a real allocation.
1581 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001582 if (PREV.br_startoff == new->br_startoff)
1583 state |= BMAP_LEFT_FILLING;
1584 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1585 state |= BMAP_RIGHT_FILLING;
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 /*
1588 * Check and set flags if this segment has a left neighbor.
1589 * Don't set contiguous if the combined extent would be too large.
1590 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001591 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001592 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001593 if (isnullstartblock(LEFT.br_startblock))
1594 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001596
1597 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1598 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1599 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1600 LEFT.br_state == new->br_state &&
1601 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1602 state |= BMAP_LEFT_CONTIG;
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 /*
1605 * Check and set flags if this segment has a right neighbor.
1606 * Don't set contiguous if the combined extent would be too large.
1607 * Also check for all-three-contiguous being too large.
1608 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001609 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001610 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001611 if (isnullstartblock(RIGHT.br_startblock))
1612 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001614
1615 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1616 new_endoff == RIGHT.br_startoff &&
1617 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1618 new->br_state == RIGHT.br_state &&
1619 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1620 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1621 BMAP_RIGHT_FILLING)) !=
1622 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1623 BMAP_RIGHT_FILLING) ||
1624 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1625 <= MAXEXTLEN))
1626 state |= BMAP_RIGHT_CONTIG;
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 error = 0;
1629 /*
1630 * Switch out based on the FILLING and CONTIG state bits.
1631 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001632 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1633 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1634 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1635 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 /*
1637 * Filling in all of a previously delayed allocation extent.
1638 * The left and right neighbors are both contiguous with new.
1639 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001640 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001641
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001642 xfs_iext_remove(bma->ip, &bma->icur, state);
1643 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001644 xfs_iext_prev(ifp, &bma->icur);
1645 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001646 (*nextents)--;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001647
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001648 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1650 else {
1651 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001652 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001653 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001655 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001656 error = xfs_btree_delete(bma->cur, &i);
1657 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001659 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001660 error = xfs_btree_decrement(bma->cur, 0, &i);
1661 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001663 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001664 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001665 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 goto done;
1667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 break;
1669
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001670 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 /*
1672 * Filling in all of a previously delayed allocation extent.
1673 * The left neighbor is contiguous, the right is not.
1674 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001675 old = LEFT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001676 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001677
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001678 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001679 xfs_iext_prev(ifp, &bma->icur);
1680 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001681
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001682 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 rval = XFS_ILOG_DEXT;
1684 else {
1685 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001686 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001687 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001689 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001690 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001691 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 goto done;
1693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 break;
1695
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001696 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 /*
1698 * Filling in all of a previously delayed allocation extent.
Dave Chinner9230a0b2018-11-19 22:50:08 -08001699 * The right neighbor is contiguous, the left is not. Take care
1700 * with delay -> unwritten extent allocation here because the
1701 * delalloc record we are overwriting is always written.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001703 PREV.br_startblock = new->br_startblock;
1704 PREV.br_blockcount += RIGHT.br_blockcount;
Dave Chinner9230a0b2018-11-19 22:50:08 -08001705 PREV.br_state = new->br_state;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001706
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001707 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001708 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001709 xfs_iext_prev(ifp, &bma->icur);
1710 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001711
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001712 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 rval = XFS_ILOG_DEXT;
1714 else {
1715 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001716 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001717 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001719 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001720 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001721 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 goto done;
1723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 break;
1725
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001726 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 /*
1728 * Filling in all of a previously delayed allocation extent.
1729 * Neither the left nor right neighbors are contiguous with
1730 * the new one.
1731 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001732 PREV.br_startblock = new->br_startblock;
1733 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001734 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001735
Darrick J. Wong60b49842016-10-03 09:11:34 -07001736 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001737 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1739 else {
1740 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001741 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001742 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001744 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001745 error = xfs_btree_insert(bma->cur, &i);
1746 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001748 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 break;
1751
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001752 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 /*
1754 * Filling in the first part of a previous delayed allocation.
1755 * The left neighbor is contiguous.
1756 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001757 old = LEFT;
1758 temp = PREV.br_blockcount - new->br_blockcount;
1759 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1760 startblockval(PREV.br_startblock));
1761
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001762 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001763
Christoph Hellwigbf999712017-11-03 10:34:38 -07001764 PREV.br_blockcount = temp;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001765 PREV.br_startoff += new->br_blockcount;
1766 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwig0d045542017-11-03 10:34:39 -07001767
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001768 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1769 xfs_iext_prev(ifp, &bma->icur);
1770 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001771
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001772 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 rval = XFS_ILOG_DEXT;
1774 else {
1775 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001776 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001777 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001779 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001780 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001781 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 goto done;
1783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 break;
1785
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001786 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 /*
1788 * Filling in the first part of a previous delayed allocation.
1789 * The left neighbor is not contiguous.
1790 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001791 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001792 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001793 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1795 else {
1796 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001797 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001798 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001800 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001801 error = xfs_btree_insert(bma->cur, &i);
1802 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001804 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001806
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001807 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001808 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001809 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 rval |= tmp_rval;
1811 if (error)
1812 goto done;
1813 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001814
1815 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001816 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001817 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001818 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001819
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001820 PREV.br_startoff = new_endoff;
1821 PREV.br_blockcount = temp;
1822 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001823 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001824 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001825 xfs_iext_prev(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 break;
1827
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001828 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 /*
1830 * Filling in the last part of a previous delayed allocation.
1831 * The right neighbor is contiguous with the new allocation.
1832 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001833 old = RIGHT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001834 RIGHT.br_startoff = new->br_startoff;
1835 RIGHT.br_startblock = new->br_startblock;
1836 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001837
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001838 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 rval = XFS_ILOG_DEXT;
1840 else {
1841 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001842 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001843 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001845 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001846 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001847 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 goto done;
1849 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001850
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001851 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001852 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001853 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001854
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001855 PREV.br_blockcount = temp;
1856 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001857
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001858 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1859 xfs_iext_next(ifp, &bma->icur);
1860 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 break;
1862
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001863 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 /*
1865 * Filling in the last part of a previous delayed allocation.
1866 * The right neighbor is not contiguous.
1867 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001868 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001869 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001870 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1872 else {
1873 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001874 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001875 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001877 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001878 error = xfs_btree_insert(bma->cur, &i);
1879 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001881 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001883
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001884 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001885 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001886 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 rval |= tmp_rval;
1888 if (error)
1889 goto done;
1890 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001891
1892 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001893 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001894 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001895 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001896
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001897 PREV.br_startblock = nullstartblock(da_new);
1898 PREV.br_blockcount = temp;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001899 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001900 xfs_iext_next(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 break;
1902
1903 case 0:
1904 /*
1905 * Filling in the middle part of a previous delayed allocation.
1906 * Contiguity is impossible here.
1907 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001908 *
1909 * We start with a delayed allocation:
1910 *
1911 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1912 * PREV @ idx
1913 *
1914 * and we are allocating:
1915 * +rrrrrrrrrrrrrrrrr+
1916 * new
1917 *
1918 * and we set it up for insertion as:
1919 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1920 * new
1921 * PREV @ idx LEFT RIGHT
1922 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001924 old = PREV;
1925
1926 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001927 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001928
1929 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001930 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001931 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001932 RIGHT.br_blockcount =
1933 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1934 RIGHT.br_startblock =
1935 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1936 RIGHT.br_blockcount));
1937
1938 /* truncate PREV */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001939 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1940 PREV.br_startblock =
1941 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1942 PREV.br_blockcount));
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001943 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001944
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001945 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001946 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1947 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001948 (*nextents)++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001949
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001950 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1952 else {
1953 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001954 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001955 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001957 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001958 error = xfs_btree_insert(bma->cur, &i);
1959 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001961 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001963
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001964 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001965 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001966 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 rval |= tmp_rval;
1968 if (error)
1969 goto done;
1970 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001971
1972 da_new = startblockval(PREV.br_startblock) +
1973 startblockval(RIGHT.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 break;
1975
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001976 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1977 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1978 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1979 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1980 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1981 case BMAP_LEFT_CONTIG:
1982 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 /*
1984 * These cases are all impossible.
1985 */
1986 ASSERT(0);
1987 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001988
Darrick J. Wong95eb3082018-05-09 10:02:32 -07001989 /* add reverse mapping unless caller opted out */
1990 if (!(bma->flags & XFS_BMAPI_NORMAP)) {
Brian Foster0f37d172018-08-01 07:20:34 -07001991 error = xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
Darrick J. Wong95eb3082018-05-09 10:02:32 -07001992 if (error)
1993 goto done;
1994 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10001995
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001996 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001997 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001998 int tmp_logflags; /* partial log flag return val */
1999
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002000 ASSERT(bma->cur == NULL);
2001 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07002002 &bma->cur, da_old > 0, &tmp_logflags,
2003 whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002004 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002005 if (error)
2006 goto done;
2007 }
2008
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002009 if (bma->cur) {
2010 da_new += bma->cur->bc_private.b.allocated;
2011 bma->cur->bc_private.b.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002012 }
2013
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002014 /* adjust for changes in reserved delayed indirect blocks */
2015 if (da_new != da_old) {
2016 ASSERT(state == 0 || da_new < da_old);
2017 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2018 false);
2019 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002020
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002021 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002023 if (whichfork != XFS_COW_FORK)
2024 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return error;
2026#undef LEFT
2027#undef RIGHT
2028#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029}
2030
2031/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002032 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 */
Christoph Hellwig26b91c72019-02-18 09:38:48 -08002034int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002036 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002038 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002039 struct xfs_iext_cursor *icur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002041 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002042 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 int i; /* temp state */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07002047 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2050 /* left is 0, right is 1, prev is 2 */
2051 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002052 int state = xfs_bmap_fork_to_state(whichfork);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002053 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002054 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002056 *logflagsp = 0;
2057
2058 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002059 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002060
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002061 ASSERT(!isnullstartblock(new->br_startblock));
2062
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002063 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065#define LEFT r[0]
2066#define RIGHT r[1]
2067#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 /*
2070 * Set up a bunch of variables to make the tests simpler.
2071 */
2072 error = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002073 xfs_iext_get_extent(ifp, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002074 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 new_endoff = new->br_startoff + new->br_blockcount;
2076 ASSERT(PREV.br_startoff <= new->br_startoff);
2077 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 /*
2080 * Set flags determining what part of the previous oldext allocation
2081 * extent is being replaced by a newext allocation.
2082 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002083 if (PREV.br_startoff == new->br_startoff)
2084 state |= BMAP_LEFT_FILLING;
2085 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2086 state |= BMAP_RIGHT_FILLING;
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 /*
2089 * Check and set flags if this segment has a left neighbor.
2090 * Don't set contiguous if the combined extent would be too large.
2091 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002092 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002093 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002094 if (isnullstartblock(LEFT.br_startblock))
2095 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002097
2098 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2099 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2100 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002101 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002102 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2103 state |= BMAP_LEFT_CONTIG;
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 /*
2106 * Check and set flags if this segment has a right neighbor.
2107 * Don't set contiguous if the combined extent would be too large.
2108 * Also check for all-three-contiguous being too large.
2109 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002110 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002111 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002112 if (isnullstartblock(RIGHT.br_startblock))
2113 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002115
2116 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2117 new_endoff == RIGHT.br_startoff &&
2118 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002119 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002120 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2121 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2122 BMAP_RIGHT_FILLING)) !=
2123 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2124 BMAP_RIGHT_FILLING) ||
2125 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2126 <= MAXEXTLEN))
2127 state |= BMAP_RIGHT_CONTIG;
2128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 /*
2130 * Switch out based on the FILLING and CONTIG state bits.
2131 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002132 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2133 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2134 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2135 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 /*
2137 * Setting all of a previous oldext extent to newext.
2138 * The left and right neighbors are both contiguous with new.
2139 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002140 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002141
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002142 xfs_iext_remove(ip, icur, state);
2143 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002144 xfs_iext_prev(ifp, icur);
2145 xfs_iext_update_extent(ip, state, icur, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002146 XFS_IFORK_NEXT_SET(ip, whichfork,
2147 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 if (cur == NULL)
2149 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2150 else {
2151 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002152 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2153 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002155 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002156 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002158 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002159 if ((error = xfs_btree_decrement(cur, 0, &i)))
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 Hellwiga67d00a2017-10-17 14:16:26 -07002168 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002169 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 goto done;
2171 }
2172 break;
2173
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002174 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 /*
2176 * Setting all of a previous oldext extent to newext.
2177 * The left neighbor is contiguous, the right is not.
2178 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002179 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002180
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002181 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002182 xfs_iext_prev(ifp, icur);
2183 xfs_iext_update_extent(ip, state, icur, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002184 XFS_IFORK_NEXT_SET(ip, whichfork,
2185 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 if (cur == NULL)
2187 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2188 else {
2189 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002190 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2191 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002193 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002194 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002196 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002197 if ((error = xfs_btree_decrement(cur, 0, &i)))
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 Hellwiga67d00a2017-10-17 14:16:26 -07002200 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002201 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 goto done;
2203 }
2204 break;
2205
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002206 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 /*
2208 * Setting all of a previous oldext extent to newext.
2209 * The right neighbor is contiguous, the left is not.
2210 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002211 PREV.br_blockcount += RIGHT.br_blockcount;
2212 PREV.br_state = new->br_state;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002213
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002214 xfs_iext_next(ifp, icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002215 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002216 xfs_iext_prev(ifp, icur);
2217 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002218
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002219 XFS_IFORK_NEXT_SET(ip, whichfork,
2220 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 if (cur == NULL)
2222 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2223 else {
2224 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002225 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2226 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002228 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002229 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002231 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002232 if ((error = xfs_btree_decrement(cur, 0, &i)))
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 Hellwiga67d00a2017-10-17 14:16:26 -07002235 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002236 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 goto done;
2238 }
2239 break;
2240
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002241 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 /*
2243 * Setting all of a previous oldext extent to newext.
2244 * Neither the left nor right neighbors are contiguous with
2245 * the new one.
2246 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002247 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002248 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 if (cur == NULL)
2251 rval = XFS_ILOG_DEXT;
2252 else {
2253 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002254 error = xfs_bmbt_lookup_eq(cur, new, &i);
2255 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002257 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002258 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002259 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 goto done;
2261 }
2262 break;
2263
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002264 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 /*
2266 * Setting the first part of a previous oldext extent to newext.
2267 * The left neighbor is contiguous.
2268 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002269 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002270
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002271 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002272 PREV.br_startoff += new->br_blockcount;
2273 PREV.br_startblock += new->br_blockcount;
2274 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002275
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002276 xfs_iext_update_extent(ip, state, icur, &PREV);
2277 xfs_iext_prev(ifp, icur);
2278 xfs_iext_update_extent(ip, state, icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 if (cur == NULL)
2281 rval = XFS_ILOG_DEXT;
2282 else {
2283 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002284 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002285 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002287 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002288 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002289 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002291 error = xfs_btree_decrement(cur, 0, &i);
2292 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002294 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002295 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 goto done;
2297 }
2298 break;
2299
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002300 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 /*
2302 * Setting the first part of a previous oldext extent to newext.
2303 * The left neighbor is not contiguous.
2304 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002305 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002306 PREV.br_startoff += new->br_blockcount;
2307 PREV.br_startblock += new->br_blockcount;
2308 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002309
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002310 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002311 xfs_iext_insert(ip, icur, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002312 XFS_IFORK_NEXT_SET(ip, whichfork,
2313 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (cur == NULL)
2315 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2316 else {
2317 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002318 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002319 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002321 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002322 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002323 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 goto done;
2325 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002326 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002328 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
2330 break;
2331
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002332 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 /*
2334 * Setting the last part of a previous oldext extent to newext.
2335 * The right neighbor is contiguous with the new allocation.
2336 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002337 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002338 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002339
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002340 RIGHT.br_startoff = new->br_startoff;
2341 RIGHT.br_startblock = new->br_startblock;
2342 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002343
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002344 xfs_iext_update_extent(ip, state, icur, &PREV);
2345 xfs_iext_next(ifp, icur);
2346 xfs_iext_update_extent(ip, state, icur, &RIGHT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 if (cur == NULL)
2349 rval = XFS_ILOG_DEXT;
2350 else {
2351 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002352 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002353 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002355 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002356 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002357 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002359 error = xfs_btree_increment(cur, 0, &i);
2360 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002362 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002363 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 goto done;
2365 }
2366 break;
2367
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002368 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 /*
2370 * Setting the last part of a previous oldext extent to newext.
2371 * The right neighbor is not contiguous.
2372 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002373 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002374 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002375
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002376 xfs_iext_update_extent(ip, state, icur, &PREV);
2377 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002378 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002379
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002380 XFS_IFORK_NEXT_SET(ip, whichfork,
2381 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 if (cur == NULL)
2383 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2384 else {
2385 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002386 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002387 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002389 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002390 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002391 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002393 error = xfs_bmbt_lookup_eq(cur, new, &i);
2394 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002396 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002397 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002399 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
2401 break;
2402
2403 case 0:
2404 /*
2405 * Setting the middle part of a previous oldext extent to
2406 * newext. Contiguity is impossible here.
2407 * One extent becomes three extents.
2408 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002409 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002410 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 r[0] = *new;
2413 r[1].br_startoff = new_endoff;
2414 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002415 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002417 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002418
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002419 xfs_iext_update_extent(ip, state, icur, &PREV);
2420 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002421 xfs_iext_insert(ip, icur, &r[1], state);
2422 xfs_iext_insert(ip, icur, &r[0], state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002423
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002424 XFS_IFORK_NEXT_SET(ip, whichfork,
2425 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 if (cur == NULL)
2427 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2428 else {
2429 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002430 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002431 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002433 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002435 error = xfs_bmbt_update(cur, &r[1]);
2436 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 goto done;
2438 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002440 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002442 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002443 /*
2444 * Reset the cursor to the position of the new extent
2445 * we are about to insert as we can't trust it after
2446 * the previous insert.
2447 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002448 error = xfs_bmbt_lookup_eq(cur, new, &i);
2449 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002451 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002453 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002455 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 }
2457 break;
2458
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002459 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2460 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2461 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2462 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2463 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2464 case BMAP_LEFT_CONTIG:
2465 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 /*
2467 * These cases are all impossible.
2468 */
2469 ASSERT(0);
2470 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002471
Darrick J. Wong9c194642016-08-03 12:16:05 +10002472 /* update reverse mappings */
Brian Foster0f37d172018-08-01 07:20:34 -07002473 error = xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002474 if (error)
2475 goto done;
2476
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002477 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002478 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002479 int tmp_logflags; /* partial log flag return val */
2480
2481 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07002482 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2483 &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002484 *logflagsp |= tmp_logflags;
2485 if (error)
2486 goto done;
2487 }
2488
2489 /* clear out the allocated field, done with it now in any case. */
2490 if (cur) {
2491 cur->bc_private.b.allocated = 0;
2492 *curp = cur;
2493 }
2494
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002495 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002497 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 return error;
2499#undef LEFT
2500#undef RIGHT
2501#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502}
2503
2504/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002505 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002507STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508xfs_bmap_add_extent_hole_delay(
2509 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002510 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002511 struct xfs_iext_cursor *icur,
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002512 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07002514 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2516 xfs_filblks_t newlen=0; /* new indirect size */
2517 xfs_filblks_t oldlen=0; /* old indirect size */
2518 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002519 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002520 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002522 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002523 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 /*
2526 * Check and set flags if this segment has a left neighbor
2527 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002528 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002529 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002530 if (isnullstartblock(left.br_startblock))
2531 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 /*
2535 * Check and set flags if the current (right) segment exists.
2536 * If it doesn't exist, we're converting the hole at end-of-file.
2537 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002538 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002539 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002540 if (isnullstartblock(right.br_startblock))
2541 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 /*
2545 * Set contiguity flags on the left and right neighbors.
2546 * Don't let extents get too large, even if the pieces are contiguous.
2547 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002548 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2549 left.br_startoff + left.br_blockcount == new->br_startoff &&
2550 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2551 state |= BMAP_LEFT_CONTIG;
2552
2553 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2554 new->br_startoff + new->br_blockcount == right.br_startoff &&
2555 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2556 (!(state & BMAP_LEFT_CONTIG) ||
2557 (left.br_blockcount + new->br_blockcount +
2558 right.br_blockcount <= MAXEXTLEN)))
2559 state |= BMAP_RIGHT_CONTIG;
2560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 /*
2562 * Switch out based on the contiguity flags.
2563 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002564 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2565 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 /*
2567 * New allocation is contiguous with delayed allocations
2568 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002569 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 */
2571 temp = left.br_blockcount + new->br_blockcount +
2572 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002573
Eric Sandeen9d87c312009-01-14 23:22:07 -06002574 oldlen = startblockval(left.br_startblock) +
2575 startblockval(new->br_startblock) +
2576 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002577 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2578 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002579 left.br_startblock = nullstartblock(newlen);
2580 left.br_blockcount = temp;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002581
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002582 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002583 xfs_iext_prev(ifp, icur);
2584 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 break;
2586
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002587 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 /*
2589 * New allocation is contiguous with a delayed allocation
2590 * on the left.
2591 * Merge the new allocation with the left neighbor.
2592 */
2593 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002594
Eric Sandeen9d87c312009-01-14 23:22:07 -06002595 oldlen = startblockval(left.br_startblock) +
2596 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002597 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2598 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002599 left.br_blockcount = temp;
2600 left.br_startblock = nullstartblock(newlen);
Christoph Hellwig41d196f2017-11-03 10:34:39 -07002601
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002602 xfs_iext_prev(ifp, icur);
2603 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 break;
2605
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002606 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 /*
2608 * New allocation is contiguous with a delayed allocation
2609 * on the right.
2610 * Merge the new allocation with the right neighbor.
2611 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002613 oldlen = startblockval(new->br_startblock) +
2614 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002615 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2616 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002617 right.br_startoff = new->br_startoff;
2618 right.br_startblock = nullstartblock(newlen);
2619 right.br_blockcount = temp;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002620 xfs_iext_update_extent(ip, state, icur, &right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 break;
2622
2623 case 0:
2624 /*
2625 * New allocation is not contiguous with another
2626 * delayed allocation.
2627 * Insert a new entry.
2628 */
2629 oldlen = newlen = 0;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002630 xfs_iext_insert(ip, icur, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 break;
2632 }
2633 if (oldlen != newlen) {
2634 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002635 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2636 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 /*
2638 * Nothing to do for disk quota accounting here.
2639 */
2640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
2642
2643/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002644 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 */
2646STATIC int /* error */
2647xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002648 struct xfs_trans *tp,
2649 struct xfs_inode *ip,
2650 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002651 struct xfs_iext_cursor *icur,
Christoph Hellwig6d045582017-04-11 16:45:54 -07002652 struct xfs_btree_cur **curp,
2653 struct xfs_bmbt_irec *new,
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002654 int *logflagsp,
2655 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002657 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2658 struct xfs_mount *mp = ip->i_mount;
2659 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2663 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002664 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002665 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002666 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002668 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002669 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002670
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002671 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 /*
2674 * Check and set flags if this segment has a left neighbor.
2675 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002676 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002677 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002678 if (isnullstartblock(left.br_startblock))
2679 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 /*
2683 * Check and set flags if this segment has a current value.
2684 * Not true if we're inserting into the "hole" at eof.
2685 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002686 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002687 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002688 if (isnullstartblock(right.br_startblock))
2689 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 /*
2693 * We're inserting a real allocation between "left" and "right".
2694 * Set the contiguity flags. Don't let extents get too large.
2695 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002696 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2697 left.br_startoff + left.br_blockcount == new->br_startoff &&
2698 left.br_startblock + left.br_blockcount == new->br_startblock &&
2699 left.br_state == new->br_state &&
2700 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2701 state |= BMAP_LEFT_CONTIG;
2702
2703 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2704 new->br_startoff + new->br_blockcount == right.br_startoff &&
2705 new->br_startblock + new->br_blockcount == right.br_startblock &&
2706 new->br_state == right.br_state &&
2707 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2708 (!(state & BMAP_LEFT_CONTIG) ||
2709 left.br_blockcount + new->br_blockcount +
2710 right.br_blockcount <= MAXEXTLEN))
2711 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002713 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 /*
2715 * Select which case we're in here, and implement it.
2716 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002717 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2718 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 /*
2720 * New allocation is contiguous with real allocations on the
2721 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002722 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002724 left.br_blockcount += new->br_blockcount + right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002725
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002726 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002727 xfs_iext_prev(ifp, icur);
2728 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002729
Christoph Hellwig6d045582017-04-11 16:45:54 -07002730 XFS_IFORK_NEXT_SET(ip, whichfork,
2731 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2732 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002733 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002734 } else {
2735 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002736 error = xfs_bmbt_lookup_eq(cur, &right, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002737 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002738 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002739 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002740 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002741 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002742 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002743 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002744 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002745 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002746 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002747 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002748 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002749 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002750 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002752 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002754 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 /*
2756 * New allocation is contiguous with a real allocation
2757 * on the left.
2758 * Merge the new allocation with the left neighbor.
2759 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002760 old = left;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002761 left.br_blockcount += new->br_blockcount;
Christoph Hellwig1d2e0082017-11-03 10:34:40 -07002762
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002763 xfs_iext_prev(ifp, icur);
2764 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002765
Christoph Hellwig6d045582017-04-11 16:45:54 -07002766 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002767 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002768 } else {
2769 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002770 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002771 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002772 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002773 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002774 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002775 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002776 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002778 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002780 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 /*
2782 * New allocation is contiguous with a real allocation
2783 * on the right.
2784 * Merge the new allocation with the right neighbor.
2785 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002786 old = right;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07002787
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002788 right.br_startoff = new->br_startoff;
2789 right.br_startblock = new->br_startblock;
2790 right.br_blockcount += new->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002791 xfs_iext_update_extent(ip, state, icur, &right);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002792
Christoph Hellwig6d045582017-04-11 16:45:54 -07002793 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002794 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002795 } else {
2796 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002797 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002798 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002799 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002800 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002801 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002802 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002803 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002805 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
2807 case 0:
2808 /*
2809 * New allocation is not contiguous with another
2810 * real allocation.
2811 * Insert a new entry.
2812 */
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002813 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002814 XFS_IFORK_NEXT_SET(ip, whichfork,
2815 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2816 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002817 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002818 } else {
2819 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002820 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002821 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002822 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002823 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002824 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002825 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002826 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002827 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002829 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002831
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002832 /* add reverse mapping unless caller opted out */
2833 if (!(flags & XFS_BMAPI_NORMAP)) {
Brian Foster0f37d172018-08-01 07:20:34 -07002834 error = xfs_rmap_map_extent(tp, ip, whichfork, new);
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002835 if (error)
2836 goto done;
2837 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10002838
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002839 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002840 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002841 int tmp_logflags; /* partial log flag return val */
2842
Christoph Hellwig6d045582017-04-11 16:45:54 -07002843 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07002844 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2845 &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002846 *logflagsp |= tmp_logflags;
2847 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002848 if (error)
2849 goto done;
2850 }
2851
2852 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002853 if (cur)
2854 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002855
Christoph Hellwig6d045582017-04-11 16:45:54 -07002856 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002857done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002858 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002859 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860}
2861
Nathan Scottdd9f4382006-01-11 15:28:28 +11002862/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11002863 * Functions used in the extent read, allocate and remove paths
2864 */
2865
2866/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002867 * Adjust the size of the new extent based on di_extsize and rt extsize.
2868 */
Dave Chinner68988112013-08-12 20:49:42 +10002869int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002870xfs_bmap_extsize_align(
2871 xfs_mount_t *mp,
2872 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2873 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2874 xfs_extlen_t extsz, /* align to this extent size */
2875 int rt, /* is this a realtime inode? */
2876 int eof, /* is extent at end-of-file? */
2877 int delay, /* creating delalloc extent? */
2878 int convert, /* overwriting unwritten extent? */
2879 xfs_fileoff_t *offp, /* in/out: aligned offset */
2880 xfs_extlen_t *lenp) /* in/out: aligned length */
2881{
2882 xfs_fileoff_t orig_off; /* original offset */
2883 xfs_extlen_t orig_alen; /* original length */
2884 xfs_fileoff_t orig_end; /* original off+len */
2885 xfs_fileoff_t nexto; /* next file offset */
2886 xfs_fileoff_t prevo; /* previous file offset */
2887 xfs_fileoff_t align_off; /* temp for offset */
2888 xfs_extlen_t align_alen; /* temp for length */
2889 xfs_extlen_t temp; /* temp for calculations */
2890
2891 if (convert)
2892 return 0;
2893
2894 orig_off = align_off = *offp;
2895 orig_alen = align_alen = *lenp;
2896 orig_end = orig_off + orig_alen;
2897
2898 /*
2899 * If this request overlaps an existing extent, then don't
2900 * attempt to perform any additional alignment.
2901 */
2902 if (!delay && !eof &&
2903 (orig_off >= gotp->br_startoff) &&
2904 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2905 return 0;
2906 }
2907
2908 /*
2909 * If the file offset is unaligned vs. the extent size
2910 * we need to align it. This will be possible unless
2911 * the file was previously written with a kernel that didn't
2912 * perform this alignment, or if a truncate shot us in the
2913 * foot.
2914 */
Dave Chinner0703a8e2018-06-08 09:54:22 -07002915 div_u64_rem(orig_off, extsz, &temp);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002916 if (temp) {
2917 align_alen += temp;
2918 align_off -= temp;
2919 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10002920
2921 /* Same adjustment for the end of the requested area. */
2922 temp = (align_alen % extsz);
2923 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11002924 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10002925
2926 /*
2927 * For large extent hint sizes, the aligned extent might be larger than
2928 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2929 * the length back under MAXEXTLEN. The outer allocation loops handle
2930 * short allocation just fine, so it is safe to do this. We only want to
2931 * do it when we are forced to, though, because it means more allocation
2932 * operations are required.
2933 */
2934 while (align_alen > MAXEXTLEN)
2935 align_alen -= extsz;
2936 ASSERT(align_alen <= MAXEXTLEN);
2937
Nathan Scottdd9f4382006-01-11 15:28:28 +11002938 /*
2939 * If the previous block overlaps with this proposed allocation
2940 * then move the start forward without adjusting the length.
2941 */
2942 if (prevp->br_startoff != NULLFILEOFF) {
2943 if (prevp->br_startblock == HOLESTARTBLOCK)
2944 prevo = prevp->br_startoff;
2945 else
2946 prevo = prevp->br_startoff + prevp->br_blockcount;
2947 } else
2948 prevo = 0;
2949 if (align_off != orig_off && align_off < prevo)
2950 align_off = prevo;
2951 /*
2952 * If the next block overlaps with this proposed allocation
2953 * then move the start back without adjusting the length,
2954 * but not before offset 0.
2955 * This may of course make the start overlap previous block,
2956 * and if we hit the offset 0 limit then the next block
2957 * can still overlap too.
2958 */
2959 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2960 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2961 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2962 nexto = gotp->br_startoff + gotp->br_blockcount;
2963 else
2964 nexto = gotp->br_startoff;
2965 } else
2966 nexto = NULLFILEOFF;
2967 if (!eof &&
2968 align_off + align_alen != orig_end &&
2969 align_off + align_alen > nexto)
2970 align_off = nexto > align_alen ? nexto - align_alen : 0;
2971 /*
2972 * If we're now overlapping the next or previous extent that
2973 * means we can't fit an extsz piece in this hole. Just move
2974 * the start forward to the first valid spot and set
2975 * the length so we hit the end.
2976 */
2977 if (align_off != orig_off && align_off < prevo)
2978 align_off = prevo;
2979 if (align_off + align_alen != orig_end &&
2980 align_off + align_alen > nexto &&
2981 nexto != NULLFILEOFF) {
2982 ASSERT(nexto > prevo);
2983 align_alen = nexto - align_off;
2984 }
2985
2986 /*
2987 * If realtime, and the result isn't a multiple of the realtime
2988 * extent size we need to remove blocks until it is.
2989 */
2990 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2991 /*
2992 * We're not covering the original request, or
2993 * we won't be able to once we fix the length.
2994 */
2995 if (orig_off < align_off ||
2996 orig_end > align_off + align_alen ||
2997 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10002998 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002999 /*
3000 * Try to fix it by moving the start up.
3001 */
3002 if (align_off + temp <= orig_off) {
3003 align_alen -= temp;
3004 align_off += temp;
3005 }
3006 /*
3007 * Try to fix it by moving the end in.
3008 */
3009 else if (align_off + align_alen - temp >= orig_end)
3010 align_alen -= temp;
3011 /*
3012 * Set the start to the minimum then trim the length.
3013 */
3014 else {
3015 align_alen -= orig_off - align_off;
3016 align_off = orig_off;
3017 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3018 }
3019 /*
3020 * Result doesn't cover the request, fail it.
3021 */
3022 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003023 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003024 } else {
3025 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003026 /* see MAXEXTLEN handling above */
3027 ASSERT(orig_end <= align_off + align_alen ||
3028 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003029 }
3030
3031#ifdef DEBUG
3032 if (!eof && gotp->br_startoff != NULLFILEOFF)
3033 ASSERT(align_off + align_alen <= gotp->br_startoff);
3034 if (prevp->br_startoff != NULLFILEOFF)
3035 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3036#endif
3037
3038 *lenp = align_alen;
3039 *offp = align_off;
3040 return 0;
3041}
3042
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043#define XFS_ALLOC_GAP_UNITS 4
3044
Dave Chinner68988112013-08-12 20:49:42 +10003045void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003046xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003047 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
3049 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3051 xfs_mount_t *mp; /* mount point structure */
3052 int nullfb; /* true if ap->firstblock isn't set */
3053 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
3055#define ISVALID(x,y) \
3056 (rt ? \
3057 (x) < mp->m_sb.sb_rblocks : \
3058 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3059 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3060 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 mp = ap->ip->i_mount;
Brian Foster94c07b42018-07-11 22:26:28 -07003063 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003064 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3065 xfs_alloc_is_userdata(ap->datatype);
Brian Foster94c07b42018-07-11 22:26:28 -07003066 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3067 ap->tp->t_firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 /*
3069 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003070 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003072 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3073 !isnullstartblock(ap->prev.br_startblock) &&
3074 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3075 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003076 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 /*
3078 * Adjust for the gap between prevp and us.
3079 */
Dave Chinner3a756672011-09-18 20:40:58 +00003080 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003081 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003083 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3084 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 }
3086 /*
3087 * If not at eof, then compare the two neighbor blocks.
3088 * Figure out whether either one gives us a good starting point,
3089 * and pick the better one.
3090 */
3091 else if (!ap->eof) {
3092 xfs_fsblock_t gotbno; /* right side block number */
3093 xfs_fsblock_t gotdiff=0; /* right side difference */
3094 xfs_fsblock_t prevbno; /* left side block number */
3095 xfs_fsblock_t prevdiff=0; /* left side difference */
3096
3097 /*
3098 * If there's a previous (left) block, select a requested
3099 * start block based on it.
3100 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003101 if (ap->prev.br_startoff != NULLFILEOFF &&
3102 !isnullstartblock(ap->prev.br_startblock) &&
3103 (prevbno = ap->prev.br_startblock +
3104 ap->prev.br_blockcount) &&
3105 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 /*
3107 * Calculate gap to end of previous block.
3108 */
Dave Chinner3a756672011-09-18 20:40:58 +00003109 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003110 (ap->prev.br_startoff +
3111 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 /*
3113 * Figure the startblock based on the previous block's
3114 * end and the gap size.
3115 * Heuristic!
3116 * If the gap is large relative to the piece we're
3117 * allocating, or using it gives us an invalid block
3118 * number, then just use the end of the previous block.
3119 */
Dave Chinner3a756672011-09-18 20:40:58 +00003120 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003122 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 prevbno += adjust;
3124 else
3125 prevdiff += adjust;
3126 /*
3127 * If the firstblock forbids it, can't use it,
3128 * must use default.
3129 */
3130 if (!rt && !nullfb &&
3131 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3132 prevbno = NULLFSBLOCK;
3133 }
3134 /*
3135 * No previous block or can't follow it, just default.
3136 */
3137 else
3138 prevbno = NULLFSBLOCK;
3139 /*
3140 * If there's a following (right) block, select a requested
3141 * start block based on it.
3142 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003143 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 /*
3145 * Calculate gap to start of next block.
3146 */
Dave Chinner3a756672011-09-18 20:40:58 +00003147 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 /*
3149 * Figure the startblock based on the next block's
3150 * start and the gap size.
3151 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003152 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 /*
3154 * Heuristic!
3155 * If the gap is large relative to the piece we're
3156 * allocating, or using it gives us an invalid block
3157 * number, then just use the start of the next block
3158 * offset by our length.
3159 */
Dave Chinner3a756672011-09-18 20:40:58 +00003160 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 ISVALID(gotbno - gotdiff, gotbno))
3162 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003163 else if (ISVALID(gotbno - ap->length, gotbno)) {
3164 gotbno -= ap->length;
3165 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 } else
3167 gotdiff += adjust;
3168 /*
3169 * If the firstblock forbids it, can't use it,
3170 * must use default.
3171 */
3172 if (!rt && !nullfb &&
3173 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3174 gotbno = NULLFSBLOCK;
3175 }
3176 /*
3177 * No next block, just default.
3178 */
3179 else
3180 gotbno = NULLFSBLOCK;
3181 /*
3182 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003183 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 */
3185 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003186 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003188 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003190 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003192#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003193}
3194
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003195static int
3196xfs_bmap_longest_free_extent(
3197 struct xfs_trans *tp,
3198 xfs_agnumber_t ag,
3199 xfs_extlen_t *blen,
3200 int *notinit)
3201{
3202 struct xfs_mount *mp = tp->t_mountp;
3203 struct xfs_perag *pag;
3204 xfs_extlen_t longest;
3205 int error = 0;
3206
3207 pag = xfs_perag_get(mp, ag);
3208 if (!pag->pagf_init) {
3209 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3210 if (error)
3211 goto out;
3212
3213 if (!pag->pagf_init) {
3214 *notinit = 1;
3215 goto out;
3216 }
3217 }
3218
Eric Sandeena1f69412018-04-06 10:09:42 -07003219 longest = xfs_alloc_longest_free_extent(pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003220 xfs_alloc_min_freelist(mp, pag),
3221 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003222 if (*blen < longest)
3223 *blen = longest;
3224
3225out:
3226 xfs_perag_put(pag);
3227 return error;
3228}
3229
3230static void
3231xfs_bmap_select_minlen(
3232 struct xfs_bmalloca *ap,
3233 struct xfs_alloc_arg *args,
3234 xfs_extlen_t *blen,
3235 int notinit)
3236{
3237 if (notinit || *blen < ap->minlen) {
3238 /*
3239 * Since we did a BUF_TRYLOCK above, it is possible that
3240 * there is space for this request.
3241 */
3242 args->minlen = ap->minlen;
3243 } else if (*blen < args->maxlen) {
3244 /*
3245 * If the best seen length is less than the request length,
3246 * use the best as the minimum.
3247 */
3248 args->minlen = *blen;
3249 } else {
3250 /*
3251 * Otherwise we've seen an extent as big as maxlen, use that
3252 * as the minimum.
3253 */
3254 args->minlen = args->maxlen;
3255 }
3256}
3257
Nathan Scotta365bdd2006-03-14 13:34:16 +11003258STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003259xfs_bmap_btalloc_nullfb(
3260 struct xfs_bmalloca *ap,
3261 struct xfs_alloc_arg *args,
3262 xfs_extlen_t *blen)
3263{
3264 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003265 xfs_agnumber_t ag, startag;
3266 int notinit = 0;
3267 int error;
3268
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003269 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003270 args->total = ap->total;
3271
Christoph Hellwigc467c042010-02-15 23:34:42 +00003272 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3273 if (startag == NULLAGNUMBER)
3274 startag = ag = 0;
3275
Dave Chinner14b064c2011-01-27 12:16:28 +11003276 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003277 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3278 &notinit);
3279 if (error)
3280 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003281
Christoph Hellwigc467c042010-02-15 23:34:42 +00003282 if (++ag == mp->m_sb.sb_agcount)
3283 ag = 0;
3284 if (ag == startag)
3285 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003286 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003287
3288 xfs_bmap_select_minlen(ap, args, blen, notinit);
3289 return 0;
3290}
3291
3292STATIC int
3293xfs_bmap_btalloc_filestreams(
3294 struct xfs_bmalloca *ap,
3295 struct xfs_alloc_arg *args,
3296 xfs_extlen_t *blen)
3297{
3298 struct xfs_mount *mp = ap->ip->i_mount;
3299 xfs_agnumber_t ag;
3300 int notinit = 0;
3301 int error;
3302
3303 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3304 args->total = ap->total;
3305
3306 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3307 if (ag == NULLAGNUMBER)
3308 ag = 0;
3309
3310 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3311 if (error)
3312 return error;
3313
3314 if (*blen < args->maxlen) {
3315 error = xfs_filestream_new_ag(ap, &ag);
3316 if (error)
3317 return error;
3318
3319 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3320 &notinit);
3321 if (error)
3322 return error;
3323
3324 }
3325
3326 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003327
3328 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003329 * Set the failure fallback case to look in the selected AG as stream
3330 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003331 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003332 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003333 return 0;
3334}
3335
Darrick J. Wong751f3762018-01-25 13:58:13 -08003336/* Update all inode and quota accounting for the allocation we just did. */
3337static void
3338xfs_bmap_btalloc_accounting(
3339 struct xfs_bmalloca *ap,
3340 struct xfs_alloc_arg *args)
3341{
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003342 if (ap->flags & XFS_BMAPI_COWFORK) {
3343 /*
3344 * COW fork blocks are in-core only and thus are treated as
3345 * in-core quota reservation (like delalloc blocks) even when
3346 * converted to real blocks. The quota reservation is not
3347 * accounted to disk until blocks are remapped to the data
3348 * fork. So if these blocks were previously delalloc, we
3349 * already have quota reservation and there's nothing to do
3350 * yet.
3351 */
3352 if (ap->wasdel)
3353 return;
3354
3355 /*
3356 * Otherwise, we've allocated blocks in a hole. The transaction
3357 * has acquired in-core quota reservation for this extent.
3358 * Rather than account these as real blocks, however, we reduce
3359 * the transaction quota reservation based on the allocation.
3360 * This essentially transfers the transaction quota reservation
3361 * to that of a delalloc extent.
3362 */
3363 ap->ip->i_delayed_blks += args->len;
3364 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3365 -(long)args->len);
3366 return;
3367 }
3368
3369 /* data/attr fork only */
3370 ap->ip->i_d.di_nblocks += args->len;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003371 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3372 if (ap->wasdel)
3373 ap->ip->i_delayed_blks -= args->len;
3374 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3375 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3376 args->len);
3377}
3378
Christoph Hellwigc467c042010-02-15 23:34:42 +00003379STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003380xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003381 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003382{
3383 xfs_mount_t *mp; /* mount point structure */
3384 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003385 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003386 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003387 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003388 xfs_alloc_arg_t args;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003389 xfs_fileoff_t orig_offset;
3390 xfs_extlen_t orig_length;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003391 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003392 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003393 int nullfb; /* true if ap->firstblock isn't set */
3394 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003395 int tryagain;
3396 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003397 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003398
Dave Chinnera99ebf42011-12-01 11:24:20 +00003399 ASSERT(ap->length);
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003400 orig_offset = ap->offset;
3401 orig_length = ap->length;
Dave Chinnera99ebf42011-12-01 11:24:20 +00003402
Nathan Scotta365bdd2006-03-14 13:34:16 +11003403 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003404
3405 /* stripe alignment for allocation is determined by mount parameters */
3406 stripe_align = 0;
3407 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3408 stripe_align = mp->m_swidth;
3409 else if (mp->m_dalign)
3410 stripe_align = mp->m_dalign;
3411
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003412 if (ap->flags & XFS_BMAPI_COWFORK)
3413 align = xfs_get_cowextsz_hint(ap->ip);
3414 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003415 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003416 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003417 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003418 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003419 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003420 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003421 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003422 }
Dave Chinner33177f052013-12-12 16:34:36 +11003423
3424
Brian Foster94c07b42018-07-11 22:26:28 -07003425 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3426 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3427 ap->tp->t_firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003428 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003429 if (xfs_alloc_is_userdata(ap->datatype) &&
3430 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003431 ag = xfs_filestream_lookup_ag(ap->ip);
3432 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003433 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003434 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003435 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003436 }
3437 } else
Brian Foster94c07b42018-07-11 22:26:28 -07003438 ap->blkno = ap->tp->t_firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003439
3440 xfs_bmap_adjacent(ap);
3441
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003443 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 * it's in the right allocation group.
3445 */
Dave Chinner3a756672011-09-18 20:40:58 +00003446 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 ;
3448 else
Brian Foster94c07b42018-07-11 22:26:28 -07003449 ap->blkno = ap->tp->t_firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 * Normal allocation, done through xfs_alloc_vextent.
3452 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003453 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003454 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003455 args.tp = ap->tp;
3456 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003457 args.fsbno = ap->blkno;
Darrick J. Wong7280fed2018-12-12 08:46:23 -08003458 args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
Dave Chinner14b064c2011-01-27 12:16:28 +11003459
3460 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner9bb54cb2018-06-07 07:54:02 -07003461 args.maxlen = min(ap->length, mp->m_ag_max_usable);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003462 blen = 0;
3463 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003464 /*
3465 * Search for an allocation group with a single extent large
3466 * enough for the request. If one isn't found, then adjust
3467 * the minimum allocation size to the largest space found.
3468 */
Dave Chinner292378e2016-09-26 08:21:28 +10003469 if (xfs_alloc_is_userdata(ap->datatype) &&
3470 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003471 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3472 else
3473 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003474 if (error)
3475 return error;
Brian Foster1214f1c2018-08-01 07:20:31 -07003476 } else if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003477 if (xfs_inode_is_filestream(ap->ip))
3478 args.type = XFS_ALLOCTYPE_FIRST_AG;
3479 else
3480 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003481 args.total = args.minlen = ap->minlen;
3482 } else {
3483 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3484 args.total = ap->total;
3485 args.minlen = ap->minlen;
3486 }
David Chinner957d0eb2007-06-18 16:50:37 +10003487 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003488 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003489 args.prod = align;
Dave Chinner0703a8e2018-06-08 09:54:22 -07003490 div_u64_rem(ap->offset, args.prod, &args.mod);
3491 if (args.mod)
3492 args.mod = args.prod - args.mod;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003493 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003494 args.prod = 1;
3495 args.mod = 0;
3496 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003497 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner0703a8e2018-06-08 09:54:22 -07003498 div_u64_rem(ap->offset, args.prod, &args.mod);
3499 if (args.mod)
3500 args.mod = args.prod - args.mod;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003501 }
3502 /*
3503 * If we are not low on available data blocks, and the
3504 * underlying logical volume manager is a stripe, and
3505 * the file offset is zero then try to allocate data
3506 * blocks on stripe unit boundary.
3507 * NOTE: ap->aeof is only set if the allocation length
3508 * is >= the stripe unit and the allocation offset is
3509 * at the end of file.
3510 */
Brian Foster1214f1c2018-08-01 07:20:31 -07003511 if (!(ap->tp->t_flags & XFS_TRANS_LOWMODE) && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003512 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003513 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003514 atype = args.type;
3515 isaligned = 1;
3516 /*
3517 * Adjust for alignment
3518 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003519 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003520 args.minlen = blen - args.alignment;
3521 args.minalignslop = 0;
3522 } else {
3523 /*
3524 * First try an exact bno allocation.
3525 * If it fails then do a near or start bno
3526 * allocation with alignment turned on.
3527 */
3528 atype = args.type;
3529 tryagain = 1;
3530 args.type = XFS_ALLOCTYPE_THIS_BNO;
3531 args.alignment = 1;
3532 /*
3533 * Compute the minlen+alignment for the
3534 * next case. Set slop so that the value
3535 * of minlen+alignment+slop doesn't go up
3536 * between the calls.
3537 */
Dave Chinner33177f052013-12-12 16:34:36 +11003538 if (blen > stripe_align && blen <= args.maxlen)
3539 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003540 else
3541 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003542 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003543 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003544 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003545 args.minlen - 1;
3546 else
3547 args.minalignslop = 0;
3548 }
3549 } else {
3550 args.alignment = 1;
3551 args.minalignslop = 0;
3552 }
3553 args.minleft = ap->minleft;
3554 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003555 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003556 args.datatype = ap->datatype;
3557 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003558 args.ip = ap->ip;
3559
3560 error = xfs_alloc_vextent(&args);
3561 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003562 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003563
Nathan Scotta365bdd2006-03-14 13:34:16 +11003564 if (tryagain && args.fsbno == NULLFSBLOCK) {
3565 /*
3566 * Exact allocation failed. Now try with alignment
3567 * turned on.
3568 */
3569 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003570 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003571 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003572 args.minlen = nextminlen;
3573 args.minalignslop = 0;
3574 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 if ((error = xfs_alloc_vextent(&args)))
3576 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003577 }
3578 if (isaligned && args.fsbno == NULLFSBLOCK) {
3579 /*
3580 * allocation failed, so turn off alignment and
3581 * try again.
3582 */
3583 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003584 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003585 args.alignment = 0;
3586 if ((error = xfs_alloc_vextent(&args)))
3587 return error;
3588 }
3589 if (args.fsbno == NULLFSBLOCK && nullfb &&
3590 args.minlen > ap->minlen) {
3591 args.minlen = ap->minlen;
3592 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003593 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003594 if ((error = xfs_alloc_vextent(&args)))
3595 return error;
3596 }
3597 if (args.fsbno == NULLFSBLOCK && nullfb) {
3598 args.fsbno = 0;
3599 args.type = XFS_ALLOCTYPE_FIRST_AG;
3600 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003601 if ((error = xfs_alloc_vextent(&args)))
3602 return error;
Brian Foster1214f1c2018-08-01 07:20:31 -07003603 ap->tp->t_flags |= XFS_TRANS_LOWMODE;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003604 }
3605 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003606 /*
3607 * check the allocation happened at the same or higher AG than
3608 * the first block that was allocated.
3609 */
Brian Foster94c07b42018-07-11 22:26:28 -07003610 ASSERT(ap->tp->t_firstblock == NULLFSBLOCK ||
3611 XFS_FSB_TO_AGNO(mp, ap->tp->t_firstblock) <=
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003612 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003613
Dave Chinner3a756672011-09-18 20:40:58 +00003614 ap->blkno = args.fsbno;
Brian Foster94c07b42018-07-11 22:26:28 -07003615 if (ap->tp->t_firstblock == NULLFSBLOCK)
3616 ap->tp->t_firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003617 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003618 ap->length = args.len;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003619 /*
3620 * If the extent size hint is active, we tried to round the
3621 * caller's allocation request offset down to extsz and the
3622 * length up to another extsz boundary. If we found a free
3623 * extent we mapped it in starting at this new offset. If the
3624 * newly mapped space isn't long enough to cover any of the
3625 * range of offsets that was originally requested, move the
3626 * mapping up so that we can fill as much of the caller's
3627 * original request as possible. Free space is apparently
3628 * very fragmented so we're unlikely to be able to satisfy the
3629 * hints anyway.
3630 */
3631 if (ap->length <= orig_length)
3632 ap->offset = orig_offset;
3633 else if (ap->offset + ap->length < orig_offset + orig_length)
3634 ap->offset = orig_offset + orig_length - ap->length;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003635 xfs_bmap_btalloc_accounting(ap, &args);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003636 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003637 ap->blkno = NULLFSBLOCK;
3638 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 }
3640 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003641}
3642
3643/*
3644 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3645 * It figures out where to ask the underlying allocator to put the new extent.
3646 */
3647STATIC int
3648xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003649 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003650{
Dave Chinner292378e2016-09-26 08:21:28 +10003651 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3652 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003653 return xfs_bmap_rtalloc(ap);
3654 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655}
3656
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003657/* Trim extent to fit a logical block range. */
3658void
3659xfs_trim_extent(
3660 struct xfs_bmbt_irec *irec,
3661 xfs_fileoff_t bno,
3662 xfs_filblks_t len)
3663{
3664 xfs_fileoff_t distance;
3665 xfs_fileoff_t end = bno + len;
3666
3667 if (irec->br_startoff + irec->br_blockcount <= bno ||
3668 irec->br_startoff >= end) {
3669 irec->br_blockcount = 0;
3670 return;
3671 }
3672
3673 if (irec->br_startoff < bno) {
3674 distance = bno - irec->br_startoff;
3675 if (isnullstartblock(irec->br_startblock))
3676 irec->br_startblock = DELAYSTARTBLOCK;
3677 if (irec->br_startblock != DELAYSTARTBLOCK &&
3678 irec->br_startblock != HOLESTARTBLOCK)
3679 irec->br_startblock += distance;
3680 irec->br_startoff += distance;
3681 irec->br_blockcount -= distance;
3682 }
3683
3684 if (end < irec->br_startoff + irec->br_blockcount) {
3685 distance = irec->br_startoff + irec->br_blockcount - end;
3686 irec->br_blockcount -= distance;
3687 }
3688}
3689
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003691 * Trim the returned map to the required bounds
3692 */
3693STATIC void
3694xfs_bmapi_trim_map(
3695 struct xfs_bmbt_irec *mval,
3696 struct xfs_bmbt_irec *got,
3697 xfs_fileoff_t *bno,
3698 xfs_filblks_t len,
3699 xfs_fileoff_t obno,
3700 xfs_fileoff_t end,
3701 int n,
3702 int flags)
3703{
3704 if ((flags & XFS_BMAPI_ENTIRE) ||
3705 got->br_startoff + got->br_blockcount <= obno) {
3706 *mval = *got;
3707 if (isnullstartblock(got->br_startblock))
3708 mval->br_startblock = DELAYSTARTBLOCK;
3709 return;
3710 }
3711
3712 if (obno > *bno)
3713 *bno = obno;
3714 ASSERT((*bno >= obno) || (n == 0));
3715 ASSERT(*bno < end);
3716 mval->br_startoff = *bno;
3717 if (isnullstartblock(got->br_startblock))
3718 mval->br_startblock = DELAYSTARTBLOCK;
3719 else
3720 mval->br_startblock = got->br_startblock +
3721 (*bno - got->br_startoff);
3722 /*
3723 * Return the minimum of what we got and what we asked for for
3724 * the length. We can use the len variable here because it is
3725 * modified below and we could have been there before coming
3726 * here if the first part of the allocation didn't overlap what
3727 * was asked for.
3728 */
3729 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3730 got->br_blockcount - (*bno - got->br_startoff));
3731 mval->br_state = got->br_state;
3732 ASSERT(mval->br_blockcount <= len);
3733 return;
3734}
3735
3736/*
3737 * Update and validate the extent map to return
3738 */
3739STATIC void
3740xfs_bmapi_update_map(
3741 struct xfs_bmbt_irec **map,
3742 xfs_fileoff_t *bno,
3743 xfs_filblks_t *len,
3744 xfs_fileoff_t obno,
3745 xfs_fileoff_t end,
3746 int *n,
3747 int flags)
3748{
3749 xfs_bmbt_irec_t *mval = *map;
3750
3751 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3752 ((mval->br_startoff + mval->br_blockcount) <= end));
3753 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3754 (mval->br_startoff < obno));
3755
3756 *bno = mval->br_startoff + mval->br_blockcount;
3757 *len = end - *bno;
3758 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3759 /* update previous map with new information */
3760 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3761 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3762 ASSERT(mval->br_state == mval[-1].br_state);
3763 mval[-1].br_blockcount = mval->br_blockcount;
3764 mval[-1].br_state = mval->br_state;
3765 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3766 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3767 mval[-1].br_startblock != HOLESTARTBLOCK &&
3768 mval->br_startblock == mval[-1].br_startblock +
3769 mval[-1].br_blockcount &&
Christoph Hellwigc3a2f9f2018-07-11 22:26:01 -07003770 mval[-1].br_state == mval->br_state) {
Dave Chinneraef9a892011-09-18 20:40:44 +00003771 ASSERT(mval->br_startoff ==
3772 mval[-1].br_startoff + mval[-1].br_blockcount);
3773 mval[-1].br_blockcount += mval->br_blockcount;
3774 } else if (*n > 0 &&
3775 mval->br_startblock == DELAYSTARTBLOCK &&
3776 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3777 mval->br_startoff ==
3778 mval[-1].br_startoff + mval[-1].br_blockcount) {
3779 mval[-1].br_blockcount += mval->br_blockcount;
3780 mval[-1].br_state = mval->br_state;
3781 } else if (!((*n == 0) &&
3782 ((mval->br_startoff + mval->br_blockcount) <=
3783 obno))) {
3784 mval++;
3785 (*n)++;
3786 }
3787 *map = mval;
3788}
3789
3790/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003791 * Map file blocks to filesystem blocks without allocation.
3792 */
3793int
3794xfs_bmapi_read(
3795 struct xfs_inode *ip,
3796 xfs_fileoff_t bno,
3797 xfs_filblks_t len,
3798 struct xfs_bmbt_irec *mval,
3799 int *nmap,
3800 int flags)
3801{
3802 struct xfs_mount *mp = ip->i_mount;
3803 struct xfs_ifork *ifp;
3804 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003805 xfs_fileoff_t obno;
3806 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003807 struct xfs_iext_cursor icur;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003808 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003809 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003810 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003811 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003812
3813 ASSERT(*nmap >= 1);
3814 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Christoph Hellwigc3a2f9f2018-07-11 22:26:01 -07003815 XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003816 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003817
3818 if (unlikely(XFS_TEST_ERROR(
3819 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3820 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003821 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003822 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003823 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003824 }
3825
3826 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003827 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003828
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003829 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003830
3831 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003832
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003833 /* No CoW fork? Return a hole. */
3834 if (whichfork == XFS_COW_FORK && !ifp) {
3835 mval->br_startoff = bno;
3836 mval->br_startblock = HOLESTARTBLOCK;
3837 mval->br_blockcount = len;
3838 mval->br_state = XFS_EXT_NORM;
3839 *nmap = 1;
3840 return 0;
3841 }
3842
Dave Chinner5c8ed202011-09-18 20:40:45 +00003843 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3844 error = xfs_iread_extents(NULL, ip, whichfork);
3845 if (error)
3846 return error;
3847 }
3848
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003849 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003850 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003851 end = bno + len;
3852 obno = bno;
3853
3854 while (bno < end && n < *nmap) {
3855 /* Reading past eof, act as though there's a hole up to end. */
3856 if (eof)
3857 got.br_startoff = end;
3858 if (got.br_startoff > bno) {
3859 /* Reading in a hole. */
3860 mval->br_startoff = bno;
3861 mval->br_startblock = HOLESTARTBLOCK;
3862 mval->br_blockcount =
3863 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3864 mval->br_state = XFS_EXT_NORM;
3865 bno += mval->br_blockcount;
3866 len -= mval->br_blockcount;
3867 mval++;
3868 n++;
3869 continue;
3870 }
3871
3872 /* set up the extent map to return. */
3873 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3874 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3875
3876 /* If we're done, stop now. */
3877 if (bno >= end || n >= *nmap)
3878 break;
3879
3880 /* Else go on to the next record. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003881 if (!xfs_iext_next_extent(ifp, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003882 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003883 }
3884 *nmap = n;
3885 return 0;
3886}
3887
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003888/*
3889 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3890 * global pool and the extent inserted into the inode in-core extent tree.
3891 *
3892 * On entry, got refers to the first extent beyond the offset of the extent to
3893 * allocate or eof is specified if no such extent exists. On return, got refers
3894 * to the extent record that was inserted to the inode fork.
3895 *
3896 * Note that the allocated extent may have been merged with contiguous extents
3897 * during insertion into the inode fork. Thus, got does not reflect the current
3898 * state of the inode fork on return. If necessary, the caller can use lastx to
3899 * look up the updated record in the inode fork.
3900 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10003901int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003902xfs_bmapi_reserve_delalloc(
3903 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003904 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11003905 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003906 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11003907 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003908 struct xfs_bmbt_irec *got,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003909 struct xfs_iext_cursor *icur,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003910 int eof)
3911{
3912 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003913 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003914 xfs_extlen_t alen;
3915 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003916 int error;
Brian Foster974ae922016-11-28 14:57:42 +11003917 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003918
Brian Foster974ae922016-11-28 14:57:42 +11003919 /*
3920 * Cap the alloc length. Keep track of prealloc so we know whether to
3921 * tag the inode before we return.
3922 */
3923 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003924 if (!eof)
3925 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11003926 if (prealloc && alen >= len)
3927 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003928
3929 /* Figure out the extent size, adjust alen */
Shan Hai6ca30722018-01-23 13:56:11 -08003930 if (whichfork == XFS_COW_FORK) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003931 struct xfs_bmbt_irec prev;
Shan Hai6ca30722018-01-23 13:56:11 -08003932 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003933
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003934 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003935 prev.br_startoff = NULLFILEOFF;
3936
Shan Hai6ca30722018-01-23 13:56:11 -08003937 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003938 1, 0, &aoff, &alen);
3939 ASSERT(!error);
3940 }
3941
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003942 /*
3943 * Make a transaction-less quota reservation for delayed allocation
3944 * blocks. This number gets adjusted later. We return if we haven't
3945 * allocated blocks already inside this loop.
3946 */
3947 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
Shan Hai6ca30722018-01-23 13:56:11 -08003948 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003949 if (error)
3950 return error;
3951
3952 /*
3953 * Split changing sb for alen and indlen since they could be coming
3954 * from different places.
3955 */
3956 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
3957 ASSERT(indlen > 0);
3958
Shan Hai6ca30722018-01-23 13:56:11 -08003959 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003960 if (error)
3961 goto out_unreserve_quota;
3962
Dave Chinner0d485ad2015-02-23 21:22:03 +11003963 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003964 if (error)
3965 goto out_unreserve_blocks;
3966
3967
3968 ip->i_delayed_blks += alen;
3969
3970 got->br_startoff = aoff;
3971 got->br_startblock = nullstartblock(indlen);
3972 got->br_blockcount = alen;
3973 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003974
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003975 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003976
Brian Foster974ae922016-11-28 14:57:42 +11003977 /*
3978 * Tag the inode if blocks were preallocated. Note that COW fork
3979 * preallocation can occur at the start or end of the extent, even when
3980 * prealloc == 0, so we must also check the aligned offset and length.
3981 */
3982 if (whichfork == XFS_DATA_FORK && prealloc)
3983 xfs_inode_set_eofblocks_tag(ip);
3984 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
3985 xfs_inode_set_cowblocks_tag(ip);
3986
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003987 return 0;
3988
3989out_unreserve_blocks:
Shan Hai6ca30722018-01-23 13:56:11 -08003990 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003991out_unreserve_quota:
3992 if (XFS_IS_QUOTA_ON(mp))
Shan Hai6ca30722018-01-23 13:56:11 -08003993 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0,
3994 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003995 return error;
3996}
3997
Dave Chinnercf11da92014-07-15 07:08:24 +10003998static int
3999xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004000 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004001{
4002 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004003 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004004 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004005 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004006 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004007
Dave Chinnera99ebf42011-12-01 11:24:20 +00004008 ASSERT(bma->length > 0);
4009
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004010 /*
4011 * For the wasdelay case, we could also just allocate the stuff asked
4012 * for in this bmap call but that wouldn't be as good.
4013 */
4014 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004015 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4016 bma->offset = bma->got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004017 xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004018 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004019 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004020 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004021 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004022 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004023 }
4024
4025 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004026 * Set the data type being allocated. For the data fork, the first data
4027 * in the file is treated differently to all other allocations. For the
4028 * attribute fork, we only need to ensure the allocated range is not on
4029 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004030 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004031 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004032 bma->datatype = XFS_ALLOC_NOBUSY;
4033 if (whichfork == XFS_DATA_FORK) {
4034 if (bma->offset == 0)
4035 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4036 else
4037 bma->datatype |= XFS_ALLOC_USERDATA;
4038 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004039 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004040 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004041 }
4042
Dave Chinnere04426b2012-10-05 11:06:59 +10004043 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004044
4045 /*
4046 * Only want to do the alignment at the eof if it is userdata and
4047 * allocation length is larger than a stripe unit.
4048 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004049 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004050 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004051 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004052 if (error)
4053 return error;
4054 }
4055
4056 error = xfs_bmap_alloc(bma);
4057 if (error)
4058 return error;
4059
Dave Chinner963c30c2011-09-18 20:40:59 +00004060 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004061 return 0;
Brian Fostercf612de2018-07-11 22:26:29 -07004062 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur)
Dave Chinner29c8d172011-09-18 20:41:00 +00004063 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004064 /*
4065 * Bump the number of extents we've allocated
4066 * in this call.
4067 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004068 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004069
Dave Chinner29c8d172011-09-18 20:41:00 +00004070 if (bma->cur)
4071 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004072 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4073
Dave Chinner963c30c2011-09-18 20:40:59 +00004074 bma->got.br_startoff = bma->offset;
4075 bma->got.br_startblock = bma->blkno;
4076 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004077 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004078
4079 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004080 * In the data fork, a wasdelay extent has been initialized, so
4081 * shouldn't be flagged as unwritten.
4082 *
4083 * For the cow fork, however, we convert delalloc reservations
4084 * (extents allocated for speculative preallocation) to
4085 * allocated unwritten extents, and only convert the unwritten
4086 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004087 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004088 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11004089 (bma->flags & XFS_BMAPI_PREALLOC))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004090 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004091
Christoph Hellwigc6534242011-09-18 20:41:05 +00004092 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004093 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004094 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004095 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004096 whichfork, &bma->icur, &bma->cur, &bma->got,
Brian Foster92f9da32018-07-11 22:26:28 -07004097 &bma->logflags, bma->flags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004098
Christoph Hellwigc315c902011-09-18 20:41:02 +00004099 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004100 if (error)
4101 return error;
4102
4103 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004104 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4105 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4106 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004107 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004108 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004109
Dave Chinner963c30c2011-09-18 20:40:59 +00004110 ASSERT(bma->got.br_startoff <= bma->offset);
4111 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4112 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004113 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4114 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004115 return 0;
4116}
4117
Dave Chinnerb447fe52011-09-18 20:40:51 +00004118STATIC int
4119xfs_bmapi_convert_unwritten(
4120 struct xfs_bmalloca *bma,
4121 struct xfs_bmbt_irec *mval,
4122 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004123 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004124{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004125 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004126 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004127 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004128 int error;
4129
Dave Chinnerb447fe52011-09-18 20:40:51 +00004130 /* check if we need to do unwritten->real conversion */
4131 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4132 (flags & XFS_BMAPI_PREALLOC))
4133 return 0;
4134
4135 /* check if we need to do real->unwritten conversion */
4136 if (mval->br_state == XFS_EXT_NORM &&
4137 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4138 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4139 return 0;
4140
4141 /*
4142 * Modify (by adding) the state flag, if writing.
4143 */
4144 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004145 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4146 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004147 bma->ip, whichfork);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004148 }
4149 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4150 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4151
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004152 /*
4153 * Before insertion into the bmbt, zero the range being converted
4154 * if required.
4155 */
4156 if (flags & XFS_BMAPI_ZERO) {
4157 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4158 mval->br_blockcount);
4159 if (error)
4160 return error;
4161 }
4162
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004163 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
Brian Foster92f9da32018-07-11 22:26:28 -07004164 &bma->icur, &bma->cur, mval, &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004165 /*
4166 * Log the inode core unconditionally in the unwritten extent conversion
4167 * path because the conversion might not have done so (e.g., if the
4168 * extent count hasn't changed). We need to make sure the inode is dirty
4169 * in the transaction for the sake of fsync(), even if nothing has
4170 * changed, because fsync() will not force the log for this transaction
4171 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004172 *
4173 * Note: If we're only converting cow fork extents, there aren't
4174 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004175 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004176 if (whichfork != XFS_COW_FORK)
4177 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004178 if (error)
4179 return error;
4180
4181 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004182 * Update our extent pointer, given that
4183 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4184 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004185 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004186 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004187
4188 /*
4189 * We may have combined previously unwritten space with written space,
4190 * so generate another request.
4191 */
4192 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004193 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004194 return 0;
4195}
4196
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004197static inline xfs_extlen_t
4198xfs_bmapi_minleft(
4199 struct xfs_trans *tp,
4200 struct xfs_inode *ip,
4201 int fork)
4202{
4203 if (tp && tp->t_firstblock != NULLFSBLOCK)
4204 return 0;
4205 if (XFS_IFORK_FORMAT(ip, fork) != XFS_DINODE_FMT_BTREE)
4206 return 1;
4207 return be16_to_cpu(XFS_IFORK_PTR(ip, fork)->if_broot->bb_level) + 1;
4208}
4209
4210/*
4211 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4212 * a case where the data is changed, there's an error, and it's not logged so we
4213 * don't shutdown when we should. Don't bother logging extents/btree changes if
4214 * we converted to the other format.
4215 */
4216static void
4217xfs_bmapi_finish(
4218 struct xfs_bmalloca *bma,
4219 int whichfork,
4220 int error)
4221{
4222 if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
4223 XFS_IFORK_FORMAT(bma->ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4224 bma->logflags &= ~xfs_ilog_fext(whichfork);
4225 else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
4226 XFS_IFORK_FORMAT(bma->ip, whichfork) != XFS_DINODE_FMT_BTREE)
4227 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4228
4229 if (bma->logflags)
4230 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4231 if (bma->cur)
4232 xfs_btree_del_cursor(bma->cur, error);
4233}
4234
Christoph Hellwig44032802011-09-18 20:40:48 +00004235/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004236 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4237 * extent state if necessary. Details behaviour is controlled by the flags
4238 * parameter. Only allocates blocks from a single allocation group, to avoid
4239 * locking problems.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004241int
4242xfs_bmapi_write(
4243 struct xfs_trans *tp, /* transaction pointer */
4244 struct xfs_inode *ip, /* incore inode */
4245 xfs_fileoff_t bno, /* starting file offs. mapped */
4246 xfs_filblks_t len, /* length to map in file */
4247 int flags, /* XFS_BMAPI_... */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004248 xfs_extlen_t total, /* total blocks needed */
4249 struct xfs_bmbt_irec *mval, /* output: map values */
Brian Foster6e702a52018-07-11 22:26:12 -07004250 int *nmap) /* i/o: mval size/count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004252 struct xfs_mount *mp = ip->i_mount;
4253 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004254 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004255 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004256 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004257 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004258 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004259 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004260 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004261
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004263 xfs_fileoff_t orig_bno; /* original block number value */
4264 int orig_flags; /* original flags arg value */
4265 xfs_filblks_t orig_len; /* original value of len arg */
4266 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4267 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
4269 orig_bno = bno;
4270 orig_len = len;
4271 orig_flags = flags;
4272 orig_mval = mval;
4273 orig_nmap = *nmap;
4274#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004275 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004276
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004278 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
Christoph Hellwig26b91c72019-02-18 09:38:48 -08004279 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004280 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004281 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004282 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004283 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004284
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004285 /* zeroing is for currently only for data extents, not metadata */
4286 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4287 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4288 /*
4289 * we can allocate unwritten extents or pre-zero allocated blocks,
4290 * but it makes no sense to do both at once. This would result in
4291 * zeroing the unwritten extent twice, but it still being an
4292 * unwritten extent....
4293 */
4294 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4295 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4296
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 if (unlikely(XFS_TEST_ERROR(
4298 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004299 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004300 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004301 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004302 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004304
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004306 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004307
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004309
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004310 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004311
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004312 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4313 error = xfs_iread_extents(tp, ip, whichfork);
4314 if (error)
4315 goto error0;
4316 }
4317
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004318 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004319 eof = true;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004320 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004321 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004322 bma.tp = tp;
4323 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004324 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004325 bma.datatype = 0;
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004326 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004327
Brian Foster627209f2019-02-01 09:14:23 -08004328 n = 0;
4329 end = bno + len;
4330 obno = bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004332 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004333
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004334 /* in hole or beyond EOF? */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004335 if (eof || bma.got.br_startoff > bno) {
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004336 /*
4337 * CoW fork conversions should /never/ hit EOF or
4338 * holes. There should always be something for us
4339 * to work on.
4340 */
4341 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4342 (flags & XFS_BMAPI_COWFORK)));
4343
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004344 need_alloc = true;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004345 } else if (isnullstartblock(bma.got.br_startblock)) {
4346 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004347 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004348
4349 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 * First, deal with the hole before the allocated space
4351 * that we found, if any.
4352 */
Christoph Hellwig26b91c72019-02-18 09:38:48 -08004353 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004354 bma.eof = eof;
4355 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4356 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004357 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004358 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004359
Dave Chinnera99ebf42011-12-01 11:24:20 +00004360 /*
4361 * There's a 32/64 bit type mismatch between the
4362 * allocation length request (which can be 64 bits in
4363 * length) and the bma length request, which is
4364 * xfs_extlen_t and therefore 32 bits. Hence we have to
4365 * check for 32-bit overflows and handle them here.
4366 */
4367 if (len > (xfs_filblks_t)MAXEXTLEN)
4368 bma.length = MAXEXTLEN;
4369 else
4370 bma.length = len;
4371
4372 ASSERT(len > 0);
4373 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004374 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 if (error)
4376 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004377 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004378 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004379
4380 /*
4381 * If this is a CoW allocation, record the data in
4382 * the refcount btree for orphan recovery.
4383 */
4384 if (whichfork == XFS_COW_FORK) {
Brian Foster0f37d172018-08-01 07:20:34 -07004385 error = xfs_refcount_alloc_cow_extent(tp,
4386 bma.blkno, bma.length);
Darrick J. Wong174edb02016-10-03 09:11:39 -07004387 if (error)
4388 goto error0;
4389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004391
Dave Chinneraef9a892011-09-18 20:40:44 +00004392 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004393 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4394 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
Dave Chinnerb447fe52011-09-18 20:40:51 +00004396 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004397 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004398 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004399 continue;
4400 if (error)
4401 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402
Dave Chinneraef9a892011-09-18 20:40:44 +00004403 /* update the extent map to return */
4404 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4405
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 /*
4407 * If we're done, stop now. Stop when we've allocated
4408 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4409 * the transaction may get too big.
4410 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004411 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004413
4414 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004415 bma.prev = bma.got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004416 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004417 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004420
Christoph Hellwigb101e332019-02-15 08:02:47 -08004421 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4422 whichfork);
4423 if (error)
4424 goto error0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004425
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004427 XFS_IFORK_NEXTENTS(ip, whichfork) >
4428 XFS_IFORK_MAXEXT(ip, whichfork));
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004429 xfs_bmapi_finish(&bma, whichfork, 0);
4430 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4431 orig_nmap, *nmap);
4432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433error0:
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004434 xfs_bmapi_finish(&bma, whichfork, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 return error;
4436}
4437
Brian Foster627209f2019-02-01 09:14:23 -08004438/*
4439 * Convert an existing delalloc extent to real blocks based on file offset. This
4440 * attempts to allocate the entire delalloc extent and may require multiple
4441 * invocations to allocate the target offset if a large enough physical extent
4442 * is not available.
4443 */
4444int
4445xfs_bmapi_convert_delalloc(
Brian Foster627209f2019-02-01 09:14:23 -08004446 struct xfs_inode *ip,
Brian Foster627209f2019-02-01 09:14:23 -08004447 int whichfork,
Christoph Hellwig491ce612019-02-15 08:02:49 -08004448 xfs_fileoff_t offset_fsb,
4449 struct xfs_bmbt_irec *imap,
4450 unsigned int *seq)
Brian Foster627209f2019-02-01 09:14:23 -08004451{
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004452 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004453 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004454 struct xfs_bmalloca bma = { NULL };
Christoph Hellwig491ce612019-02-15 08:02:49 -08004455 struct xfs_trans *tp;
Brian Foster627209f2019-02-01 09:14:23 -08004456 int error;
Brian Foster627209f2019-02-01 09:14:23 -08004457
Christoph Hellwig491ce612019-02-15 08:02:49 -08004458 /*
4459 * Space for the extent and indirect blocks was reserved when the
4460 * delalloc extent was created so there's no need to do so here.
4461 */
4462 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4463 XFS_TRANS_RESERVE, &tp);
4464 if (error)
4465 return error;
4466
4467 xfs_ilock(ip, XFS_ILOCK_EXCL);
4468 xfs_trans_ijoin(tp, ip, 0);
4469
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004470 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4471 bma.got.br_startoff > offset_fsb) {
4472 /*
4473 * No extent found in the range we are trying to convert. This
4474 * should only happen for the COW fork, where another thread
4475 * might have moved the extent to the data fork in the meantime.
4476 */
4477 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004478 error = -EAGAIN;
4479 goto out_trans_cancel;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004480 }
Brian Foster627209f2019-02-01 09:14:23 -08004481
4482 /*
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004483 * If we find a real extent here we raced with another thread converting
4484 * the extent. Just return the real extent at this offset.
Brian Foster627209f2019-02-01 09:14:23 -08004485 */
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004486 if (!isnullstartblock(bma.got.br_startblock)) {
4487 *imap = bma.got;
Christoph Hellwig491ce612019-02-15 08:02:49 -08004488 *seq = READ_ONCE(ifp->if_seq);
4489 goto out_trans_cancel;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004490 }
4491
4492 bma.tp = tp;
4493 bma.ip = ip;
4494 bma.wasdel = true;
4495 bma.offset = bma.got.br_startoff;
4496 bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount, MAXEXTLEN);
4497 bma.total = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
4498 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4499 if (whichfork == XFS_COW_FORK)
4500 bma.flags = XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC;
4501
4502 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4503 bma.prev.br_startoff = NULLFILEOFF;
4504
4505 error = xfs_bmapi_allocate(&bma);
4506 if (error)
4507 goto out_finish;
4508
4509 error = -ENOSPC;
4510 if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK))
4511 goto out_finish;
4512 error = -EFSCORRUPTED;
4513 if (WARN_ON_ONCE(!bma.got.br_startblock && !XFS_IS_REALTIME_INODE(ip)))
4514 goto out_finish;
4515
Christoph Hellwig125851a2019-02-15 08:02:49 -08004516 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4517 XFS_STATS_INC(mp, xs_xstrat_quick);
4518
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004519 ASSERT(!isnullstartblock(bma.got.br_startblock));
4520 *imap = bma.got;
Christoph Hellwig491ce612019-02-15 08:02:49 -08004521 *seq = READ_ONCE(ifp->if_seq);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004522
4523 if (whichfork == XFS_COW_FORK) {
4524 error = xfs_refcount_alloc_cow_extent(tp, bma.blkno,
4525 bma.length);
4526 if (error)
4527 goto out_finish;
4528 }
4529
4530 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4531 whichfork);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004532 if (error)
4533 goto out_finish;
4534
4535 xfs_bmapi_finish(&bma, whichfork, 0);
4536 error = xfs_trans_commit(tp);
4537 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4538 return error;
4539
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004540out_finish:
4541 xfs_bmapi_finish(&bma, whichfork, error);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004542out_trans_cancel:
4543 xfs_trans_cancel(tp);
4544 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Brian Foster627209f2019-02-01 09:14:23 -08004545 return error;
4546}
4547
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004548int
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004549xfs_bmapi_remap(
4550 struct xfs_trans *tp,
4551 struct xfs_inode *ip,
4552 xfs_fileoff_t bno,
4553 xfs_filblks_t len,
4554 xfs_fsblock_t startblock,
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004555 int flags)
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004556{
4557 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004558 struct xfs_ifork *ifp;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004559 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004560 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004561 struct xfs_iext_cursor icur;
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004562 int whichfork = xfs_bmapi_whichfork(flags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004563 int logflags = 0, error;
4564
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004565 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004566 ASSERT(len > 0);
4567 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4568 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Darrick J. Wong7644bd92018-05-14 06:34:35 -07004569 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4570 XFS_BMAPI_NORMAP)));
4571 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4572 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004573
4574 if (unlikely(XFS_TEST_ERROR(
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004575 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4576 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004577 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004578 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4579 return -EFSCORRUPTED;
4580 }
4581
4582 if (XFS_FORCED_SHUTDOWN(mp))
4583 return -EIO;
4584
4585 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004586 error = xfs_iread_extents(tp, ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004587 if (error)
4588 return error;
4589 }
4590
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004591 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004592 /* make sure we only reflink into a hole. */
4593 ASSERT(got.br_startoff > bno);
4594 ASSERT(got.br_startoff - bno >= len);
4595 }
4596
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004597 ip->i_d.di_nblocks += len;
4598 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004599
4600 if (ifp->if_flags & XFS_IFBROOT) {
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004601 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004602 cur->bc_private.b.flags = 0;
4603 }
4604
4605 got.br_startoff = bno;
4606 got.br_startblock = startblock;
4607 got.br_blockcount = len;
Darrick J. Wong7644bd92018-05-14 06:34:35 -07004608 if (flags & XFS_BMAPI_PREALLOC)
4609 got.br_state = XFS_EXT_UNWRITTEN;
4610 else
4611 got.br_state = XFS_EXT_NORM;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004612
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004613 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
Brian Foster92f9da32018-07-11 22:26:28 -07004614 &cur, &got, &logflags, flags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004615 if (error)
4616 goto error0;
4617
Christoph Hellwigb101e332019-02-15 08:02:47 -08004618 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004619
4620error0:
4621 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4622 logflags &= ~XFS_ILOG_DEXT;
4623 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4624 logflags &= ~XFS_ILOG_DBROOT;
4625
4626 if (logflags)
4627 xfs_trans_log_inode(tp, ip, logflags);
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07004628 if (cur)
4629 xfs_btree_del_cursor(cur, error);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004630 return error;
4631}
4632
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004634 * When a delalloc extent is split (e.g., due to a hole punch), the original
4635 * indlen reservation must be shared across the two new extents that are left
4636 * behind.
4637 *
4638 * Given the original reservation and the worst case indlen for the two new
4639 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004640 * reservation fairly across the two new extents. If necessary, steal available
4641 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4642 * ores == 1). The number of stolen blocks is returned. The availability and
4643 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004644 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004645static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004646xfs_bmap_split_indlen(
4647 xfs_filblks_t ores, /* original res. */
4648 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004649 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4650 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004651{
4652 xfs_filblks_t len1 = *indlen1;
4653 xfs_filblks_t len2 = *indlen2;
4654 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004655 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004656 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004657
4658 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004659 * Steal as many blocks as we can to try and satisfy the worst case
4660 * indlen for both new extents.
4661 */
Brian Foster75d65362017-02-13 22:48:30 -08004662 if (ores < nres && avail)
4663 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4664 ores += stolen;
4665
4666 /* nothing else to do if we've satisfied the new reservation */
4667 if (ores >= nres)
4668 return stolen;
Brian Fosterd34999c2016-03-15 11:42:47 +11004669
4670 /*
Brian Foster75d65362017-02-13 22:48:30 -08004671 * We can't meet the total required reservation for the two extents.
4672 * Calculate the percent of the overall shortage between both extents
4673 * and apply this percentage to each of the requested indlen values.
4674 * This distributes the shortage fairly and reduces the chances that one
4675 * of the two extents is left with nothing when extents are repeatedly
4676 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004677 */
Brian Foster75d65362017-02-13 22:48:30 -08004678 resfactor = (ores * 100);
4679 do_div(resfactor, nres);
4680 len1 *= resfactor;
4681 do_div(len1, 100);
4682 len2 *= resfactor;
4683 do_div(len2, 100);
4684 ASSERT(len1 + len2 <= ores);
4685 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4686
4687 /*
4688 * Hand out the remainder to each extent. If one of the two reservations
4689 * is zero, we want to make sure that one gets a block first. The loop
4690 * below starts with len1, so hand len2 a block right off the bat if it
4691 * is zero.
4692 */
4693 ores -= (len1 + len2);
4694 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4695 if (ores && !len2 && *indlen2) {
4696 len2++;
4697 ores--;
4698 }
4699 while (ores) {
4700 if (len1 < *indlen1) {
4701 len1++;
4702 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004703 }
Brian Foster75d65362017-02-13 22:48:30 -08004704 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004705 break;
Brian Foster75d65362017-02-13 22:48:30 -08004706 if (len2 < *indlen2) {
4707 len2++;
4708 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004709 }
4710 }
4711
4712 *indlen1 = len1;
4713 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004714
4715 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004716}
4717
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004718int
4719xfs_bmap_del_extent_delay(
4720 struct xfs_inode *ip,
4721 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004722 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004723 struct xfs_bmbt_irec *got,
4724 struct xfs_bmbt_irec *del)
4725{
4726 struct xfs_mount *mp = ip->i_mount;
4727 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4728 struct xfs_bmbt_irec new;
4729 int64_t da_old, da_new, da_diff = 0;
4730 xfs_fileoff_t del_endoff, got_endoff;
4731 xfs_filblks_t got_indlen, new_indlen, stolen;
Christoph Hellwig060ea652017-10-19 11:02:29 -07004732 int state = xfs_bmap_fork_to_state(whichfork);
4733 int error = 0;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004734 bool isrt;
4735
4736 XFS_STATS_INC(mp, xs_del_exlist);
4737
4738 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4739 del_endoff = del->br_startoff + del->br_blockcount;
4740 got_endoff = got->br_startoff + got->br_blockcount;
4741 da_old = startblockval(got->br_startblock);
4742 da_new = 0;
4743
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004744 ASSERT(del->br_blockcount > 0);
4745 ASSERT(got->br_startoff <= del->br_startoff);
4746 ASSERT(got_endoff >= del_endoff);
4747
4748 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004749 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004750
4751 do_div(rtexts, mp->m_sb.sb_rextsize);
4752 xfs_mod_frextents(mp, rtexts);
4753 }
4754
4755 /*
4756 * Update the inode delalloc counter now and wait to update the
4757 * sb counters as we might have to borrow some blocks for the
4758 * indirect block accounting.
4759 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004760 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4761 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004762 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004763 if (error)
4764 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004765 ip->i_delayed_blks -= del->br_blockcount;
4766
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004767 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004768 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004769 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004770 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004771
Christoph Hellwig0173c682017-10-17 14:16:22 -07004772 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4773 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004774 /*
4775 * Matches the whole extent. Delete the entry.
4776 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004777 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004778 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004779 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004780 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004781 /*
4782 * Deleting the first part of the extent.
4783 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004784 got->br_startoff = del_endoff;
4785 got->br_blockcount -= del->br_blockcount;
4786 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4787 got->br_blockcount), da_old);
4788 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004789 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004790 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004791 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004792 /*
4793 * Deleting the last part of the extent.
4794 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004795 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4796 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4797 got->br_blockcount), da_old);
4798 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004799 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004800 break;
4801 case 0:
4802 /*
4803 * Deleting the middle of the extent.
4804 *
4805 * Distribute the original indlen reservation across the two new
4806 * extents. Steal blocks from the deleted extent if necessary.
4807 * Stealing blocks simply fudges the fdblocks accounting below.
4808 * Warn if either of the new indlen reservations is zero as this
4809 * can lead to delalloc problems.
4810 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004811 got->br_blockcount = del->br_startoff - got->br_startoff;
4812 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4813
4814 new.br_blockcount = got_endoff - del_endoff;
4815 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4816
4817 WARN_ON_ONCE(!got_indlen || !new_indlen);
4818 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4819 del->br_blockcount);
4820
4821 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004822
4823 new.br_startoff = del_endoff;
4824 new.br_state = got->br_state;
4825 new.br_startblock = nullstartblock((int)new_indlen);
4826
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004827 xfs_iext_update_extent(ip, state, icur, got);
4828 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004829 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004830
4831 da_new = got_indlen + new_indlen - stolen;
4832 del->br_blockcount -= stolen;
4833 break;
4834 }
4835
4836 ASSERT(da_old >= da_new);
4837 da_diff = da_old - da_new;
4838 if (!isrt)
4839 da_diff += del->br_blockcount;
4840 if (da_diff)
4841 xfs_mod_fdblocks(mp, da_diff, false);
4842 return error;
4843}
4844
4845void
4846xfs_bmap_del_extent_cow(
4847 struct xfs_inode *ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004848 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004849 struct xfs_bmbt_irec *got,
4850 struct xfs_bmbt_irec *del)
4851{
4852 struct xfs_mount *mp = ip->i_mount;
4853 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4854 struct xfs_bmbt_irec new;
4855 xfs_fileoff_t del_endoff, got_endoff;
4856 int state = BMAP_COWFORK;
4857
4858 XFS_STATS_INC(mp, xs_del_exlist);
4859
4860 del_endoff = del->br_startoff + del->br_blockcount;
4861 got_endoff = got->br_startoff + got->br_blockcount;
4862
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004863 ASSERT(del->br_blockcount > 0);
4864 ASSERT(got->br_startoff <= del->br_startoff);
4865 ASSERT(got_endoff >= del_endoff);
4866 ASSERT(!isnullstartblock(got->br_startblock));
4867
4868 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004869 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004870 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004871 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004872
Christoph Hellwig0173c682017-10-17 14:16:22 -07004873 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4874 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004875 /*
4876 * Matches the whole extent. Delete the entry.
4877 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004878 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004879 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004880 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004881 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004882 /*
4883 * Deleting the first part of the extent.
4884 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004885 got->br_startoff = del_endoff;
4886 got->br_blockcount -= del->br_blockcount;
4887 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004888 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004889 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004890 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004891 /*
4892 * Deleting the last part of the extent.
4893 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004894 got->br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004895 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004896 break;
4897 case 0:
4898 /*
4899 * Deleting the middle of the extent.
4900 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004901 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004902
4903 new.br_startoff = del_endoff;
4904 new.br_blockcount = got_endoff - del_endoff;
4905 new.br_state = got->br_state;
4906 new.br_startblock = del->br_startblock + del->br_blockcount;
4907
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004908 xfs_iext_update_extent(ip, state, icur, got);
4909 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004910 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004911 break;
4912 }
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08004913 ip->i_delayed_blks -= del->br_blockcount;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004914}
4915
Brian Fostera9bd24a2016-03-15 11:42:46 +11004916/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11004917 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07004918 * after removing space.
Dave Chinner9e5987a72013-02-25 12:31:26 +11004919 */
4920STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07004921xfs_bmap_del_extent_real(
Dave Chinner9e5987a72013-02-25 12:31:26 +11004922 xfs_inode_t *ip, /* incore inode pointer */
4923 xfs_trans_t *tp, /* current transaction pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004924 struct xfs_iext_cursor *icur,
Dave Chinner9e5987a72013-02-25 12:31:26 +11004925 xfs_btree_cur_t *cur, /* if null, not a btree */
4926 xfs_bmbt_irec_t *del, /* data to remove from extents */
4927 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07004928 int whichfork, /* data or attr fork */
4929 int bflags) /* bmapi flags */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004930{
Dave Chinner9e5987a72013-02-25 12:31:26 +11004931 xfs_fsblock_t del_endblock=0; /* first block past del */
4932 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004933 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004934 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004935 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004936 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004937 xfs_fileoff_t got_endoff; /* first offset past got */
4938 int i; /* temp state */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07004939 struct xfs_ifork *ifp; /* inode fork pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004940 xfs_mount_t *mp; /* mount structure */
4941 xfs_filblks_t nblks; /* quota/sb block count */
4942 xfs_bmbt_irec_t new; /* new record to be inserted */
4943 /* REFERENCED */
4944 uint qfield; /* quota field to update */
Christoph Hellwig060ea652017-10-19 11:02:29 -07004945 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004946 struct xfs_bmbt_irec old;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004947
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004948 mp = ip->i_mount;
4949 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004950
Dave Chinner9e5987a72013-02-25 12:31:26 +11004951 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004952 ASSERT(del->br_blockcount > 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004953 xfs_iext_get_extent(ifp, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004954 ASSERT(got.br_startoff <= del->br_startoff);
4955 del_endoff = del->br_startoff + del->br_blockcount;
4956 got_endoff = got.br_startoff + got.br_blockcount;
4957 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004958 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a72013-02-25 12:31:26 +11004959 qfield = 0;
4960 error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004961
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004962 /*
4963 * If it's the case where the directory code is running with no block
4964 * reservation, and the deleted block is in the middle of its extent,
4965 * and the resulting insert of an extent would cause transformation to
4966 * btree format, then reject it. The calling code will then swap blocks
4967 * around instead. We have to do this now, rather than waiting for the
4968 * conversion to btree format, since the transaction will be dirty then.
4969 */
4970 if (tp->t_blk_res == 0 &&
4971 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4972 XFS_IFORK_NEXTENTS(ip, whichfork) >=
4973 XFS_IFORK_MAXEXT(ip, whichfork) &&
4974 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
4975 return -ENOSPC;
4976
4977 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004978 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4979 xfs_fsblock_t bno;
4980 xfs_filblks_t len;
Dave Chinner0703a8e2018-06-08 09:54:22 -07004981 xfs_extlen_t mod;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004982
Dave Chinner0703a8e2018-06-08 09:54:22 -07004983 bno = div_u64_rem(del->br_startblock, mp->m_sb.sb_rextsize,
4984 &mod);
4985 ASSERT(mod == 0);
4986 len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize,
4987 &mod);
4988 ASSERT(mod == 0);
4989
Christoph Hellwige1d75532017-10-17 14:16:21 -07004990 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4991 if (error)
4992 goto done;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004993 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004994 nblks = len * mp->m_sb.sb_rextsize;
4995 qfield = XFS_TRANS_DQ_RTBCOUNT;
4996 } else {
4997 do_fx = 1;
4998 nblks = del->br_blockcount;
4999 qfield = XFS_TRANS_DQ_BCOUNT;
5000 }
5001
5002 del_endblock = del->br_startblock + del->br_blockcount;
5003 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005004 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005005 if (error)
5006 goto done;
5007 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005008 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005009
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005010 if (got.br_startoff == del->br_startoff)
5011 state |= BMAP_LEFT_FILLING;
5012 if (got_endoff == del_endoff)
5013 state |= BMAP_RIGHT_FILLING;
5014
5015 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5016 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005017 /*
5018 * Matches the whole extent. Delete the entry.
5019 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005020 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005021 xfs_iext_prev(ifp, icur);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005022 XFS_IFORK_NEXT_SET(ip, whichfork,
5023 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5024 flags |= XFS_ILOG_CORE;
5025 if (!cur) {
5026 flags |= xfs_ilog_fext(whichfork);
5027 break;
5028 }
5029 if ((error = xfs_btree_delete(cur, &i)))
5030 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005031 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005032 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005033 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005034 /*
5035 * Deleting the first part of the extent.
5036 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005037 got.br_startoff = del_endoff;
5038 got.br_startblock = del_endblock;
5039 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005040 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005041 if (!cur) {
5042 flags |= xfs_ilog_fext(whichfork);
5043 break;
5044 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005045 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005046 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005047 goto done;
5048 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005049 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005050 /*
5051 * Deleting the last part of the extent.
5052 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005053 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;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005063 case 0:
5064 /*
5065 * Deleting the middle of the extent.
5066 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005067 old = got;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07005068
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005069 got.br_blockcount = del->br_startoff - got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005070 xfs_iext_update_extent(ip, state, icur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005071
Dave Chinner9e5987a72013-02-25 12:31:26 +11005072 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005073 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005074 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005075 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005076
Christoph Hellwige1d75532017-10-17 14:16:21 -07005077 flags |= XFS_ILOG_CORE;
5078 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005079 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005080 if (error)
5081 goto done;
5082 error = xfs_btree_increment(cur, 0, &i);
5083 if (error)
5084 goto done;
5085 cur->bc_rec.b = new;
5086 error = xfs_btree_insert(cur, &i);
5087 if (error && error != -ENOSPC)
5088 goto done;
5089 /*
5090 * If get no-space back from btree insert, it tried a
5091 * split, and we have a zero block reservation. Fix up
5092 * our state and return the error.
5093 */
5094 if (error == -ENOSPC) {
5095 /*
5096 * Reset the cursor, don't trust it after any
5097 * insert operation.
5098 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005099 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005100 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005101 goto done;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005102 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5103 /*
5104 * Update the btree record back
5105 * to the original value.
5106 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005107 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005108 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005109 goto done;
5110 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005111 * Reset the extent record back
5112 * to the original value.
Dave Chinner9e5987a72013-02-25 12:31:26 +11005113 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005114 xfs_iext_update_extent(ip, state, icur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005115 flags = 0;
5116 error = -ENOSPC;
5117 goto done;
5118 }
5119 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5120 } else
5121 flags |= xfs_ilog_fext(whichfork);
5122 XFS_IFORK_NEXT_SET(ip, whichfork,
5123 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005124 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005125 xfs_iext_insert(ip, icur, &new, state);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005126 break;
5127 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005128
5129 /* remove reverse mapping */
Brian Foster0f37d172018-08-01 07:20:34 -07005130 error = xfs_rmap_unmap_extent(tp, ip, whichfork, del);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005131 if (error)
5132 goto done;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005133
Dave Chinner9e5987a72013-02-25 12:31:26 +11005134 /*
5135 * If we need to, add to list of extents to delete.
5136 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005137 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005138 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
Brian Foster0f37d172018-08-01 07:20:34 -07005139 error = xfs_refcount_decrease_extent(tp, del);
Darrick J. Wong62aab202016-10-03 09:11:23 -07005140 if (error)
5141 goto done;
Brian Fosterfcb762f2018-05-09 08:45:04 -07005142 } else {
Brian Foster0f37d172018-08-01 07:20:34 -07005143 __xfs_bmap_add_free(tp, del->br_startblock,
Brian Foster4e529332018-05-10 09:35:42 -07005144 del->br_blockcount, NULL,
5145 (bflags & XFS_BMAPI_NODISCARD) ||
5146 del->br_state == XFS_EXT_UNWRITTEN);
Brian Fosterfcb762f2018-05-09 08:45:04 -07005147 }
Darrick J. Wong62aab202016-10-03 09:11:23 -07005148 }
5149
Dave Chinner9e5987a72013-02-25 12:31:26 +11005150 /*
5151 * Adjust inode # blocks in the file.
5152 */
5153 if (nblks)
5154 ip->i_d.di_nblocks -= nblks;
5155 /*
5156 * Adjust quota data.
5157 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005158 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a72013-02-25 12:31:26 +11005159 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5160
Dave Chinner9e5987a72013-02-25 12:31:26 +11005161done:
5162 *logflagsp = flags;
5163 return error;
5164}
5165
5166/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 * Unmap (remove) blocks from a file.
5168 * If nexts is nonzero then the number of extents to remove is limited to
5169 * that value. If not all extents in the block range can be removed then
5170 * *done is set.
5171 */
5172int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005173__xfs_bunmapi(
Brian Fosterccd9d912018-07-11 22:26:13 -07005174 struct xfs_trans *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005176 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005177 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 int flags, /* misc flags */
Brian Foster2af52842018-07-11 22:26:25 -07005179 xfs_extnum_t nexts) /* number of extents max */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180{
Brian Fosterccd9d912018-07-11 22:26:13 -07005181 struct xfs_btree_cur *cur; /* bmap btree cursor */
5182 struct xfs_bmbt_irec del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 int error; /* error return value */
5184 xfs_extnum_t extno; /* extent number in list */
Brian Fosterccd9d912018-07-11 22:26:13 -07005185 struct xfs_bmbt_irec got; /* current extent record */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07005186 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 int isrt; /* freeing in rt area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 int logflags; /* transaction logging flags */
5189 xfs_extlen_t mod; /* rt extent offset */
Brian Fosterccd9d912018-07-11 22:26:13 -07005190 struct xfs_mount *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 int tmp_logflags; /* partial logging flags */
5192 int wasdel; /* was a delayed alloc extent */
5193 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005195 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005196 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005197 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005198 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005199 struct xfs_iext_cursor icur;
5200 bool done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005202 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005203
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005204 whichfork = xfs_bmapi_whichfork(flags);
5205 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 ifp = XFS_IFORK_PTR(ip, whichfork);
5207 if (unlikely(
5208 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5209 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5210 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5211 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005212 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 }
5214 mp = ip->i_mount;
5215 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005216 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005217
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005218 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 ASSERT(len > 0);
5220 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005221
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005222 /*
5223 * Guesstimate how many blocks we can unmap without running the risk of
5224 * blowing out the transaction with a mix of EFIs and reflink
5225 * adjustments.
5226 */
Darrick J. Wong8c57b882017-12-10 18:03:53 -08005227 if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005228 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5229 else
5230 max_len = len;
5231
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5233 (error = xfs_iread_extents(tp, ip, whichfork)))
5234 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005235 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005236 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 return 0;
5238 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005239 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005240 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwigdc560152017-10-23 16:32:39 -07005241 end = start + len;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005242
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005243 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
Christoph Hellwigdc560152017-10-23 16:32:39 -07005244 *rlen = 0;
5245 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 }
Christoph Hellwigdc560152017-10-23 16:32:39 -07005247 end--;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005248
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 logflags = 0;
5250 if (ifp->if_flags & XFS_IFBROOT) {
5251 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005252 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 cur->bc_private.b.flags = 0;
5254 } else
5255 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005256
5257 if (isrt) {
5258 /*
5259 * Synchronize by locking the bitmap inode.
5260 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005261 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005262 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005263 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5264 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005265 }
5266
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 extno = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005268 while (end != (xfs_fileoff_t)-1 && end >= start &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005269 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005271 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 * Just back up to the previous extent, if so.
5273 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005274 if (got.br_startoff > end &&
5275 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5276 done = true;
5277 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 }
5279 /*
5280 * Is the last block of this extent before the range
5281 * we're supposed to delete? If so, we're done.
5282 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005283 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005285 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 break;
5287 /*
5288 * Then deal with the (possibly delayed) allocated space
5289 * we found.
5290 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005292 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005293
5294 /*
5295 * Make sure we don't touch multiple AGF headers out of order
5296 * in a single transaction, as that could cause AB-BA deadlocks.
5297 */
5298 if (!wasdel) {
5299 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5300 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5301 break;
5302 prev_agno = agno;
5303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 if (got.br_startoff < start) {
5305 del.br_startoff = start;
5306 del.br_blockcount -= start - got.br_startoff;
5307 if (!wasdel)
5308 del.br_startblock += start - got.br_startoff;
5309 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005310 if (del.br_startoff + del.br_blockcount > end + 1)
5311 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005312
5313 /* How much can we safely unmap? */
5314 if (max_len < del.br_blockcount) {
5315 del.br_startoff += del.br_blockcount - max_len;
5316 if (!wasdel)
5317 del.br_startblock += del.br_blockcount - max_len;
5318 del.br_blockcount = max_len;
5319 }
5320
Dave Chinner0703a8e2018-06-08 09:54:22 -07005321 if (!isrt)
5322 goto delete;
5323
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 sum = del.br_startblock + del.br_blockcount;
Dave Chinner0703a8e2018-06-08 09:54:22 -07005325 div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod);
5326 if (mod) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 /*
5328 * Realtime extent not lined up at the end.
5329 * The extent could have been split into written
5330 * and unwritten pieces, or we could just be
5331 * unmapping part of it. But we can't really
5332 * get rid of part of a realtime extent.
5333 */
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11005334 if (del.br_state == XFS_EXT_UNWRITTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 /*
5336 * This piece is unwritten, or we're not
5337 * using unwritten extents. Skip over it.
5338 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005339 ASSERT(end >= mod);
5340 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 del.br_blockcount : mod;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005342 if (end < got.br_startoff &&
5343 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5344 done = true;
5345 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 }
5347 continue;
5348 }
5349 /*
5350 * It's written, turn it unwritten.
5351 * This is better than zeroing it.
5352 */
5353 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005354 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 /*
5356 * If this spans a realtime extent boundary,
5357 * chop it back to the start of the one we end at.
5358 */
5359 if (del.br_blockcount > mod) {
5360 del.br_startoff += del.br_blockcount - mod;
5361 del.br_startblock += del.br_blockcount - mod;
5362 del.br_blockcount = mod;
5363 }
5364 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005365 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005366 whichfork, &icur, &cur, &del,
Brian Foster92f9da32018-07-11 22:26:28 -07005367 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 if (error)
5369 goto error0;
5370 goto nodelete;
5371 }
Dave Chinner0703a8e2018-06-08 09:54:22 -07005372 div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod);
5373 if (mod) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 /*
5375 * Realtime extent is lined up at the end but not
5376 * at the front. We'll get rid of full extents if
5377 * we can.
5378 */
5379 mod = mp->m_sb.sb_rextsize - mod;
5380 if (del.br_blockcount > mod) {
5381 del.br_blockcount -= mod;
5382 del.br_startoff += mod;
5383 del.br_startblock += mod;
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11005384 } else if (del.br_startoff == start &&
5385 (del.br_state == XFS_EXT_UNWRITTEN ||
5386 tp->t_blk_res == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 /*
5388 * Can't make it unwritten. There isn't
5389 * a full extent here so just skip it.
5390 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005391 ASSERT(end >= del.br_blockcount);
5392 end -= del.br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005393 if (got.br_startoff > end &&
5394 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5395 done = true;
5396 break;
5397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 continue;
5399 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005400 struct xfs_bmbt_irec prev;
5401
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 /*
5403 * This one is already unwritten.
5404 * It must have a written left neighbor.
5405 * Unwrite the killed part of that one and
5406 * try again.
5407 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005408 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5409 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005411 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 ASSERT(del.br_startblock ==
5413 prev.br_startblock + prev.br_blockcount);
5414 if (prev.br_startoff < start) {
5415 mod = start - prev.br_startoff;
5416 prev.br_blockcount -= mod;
5417 prev.br_startblock += mod;
5418 prev.br_startoff = start;
5419 }
5420 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005421 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005422 ip, whichfork, &icur, &cur,
Brian Foster92f9da32018-07-11 22:26:28 -07005423 &prev, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 if (error)
5425 goto error0;
5426 goto nodelete;
5427 } else {
5428 ASSERT(del.br_state == XFS_EXT_NORM);
5429 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005430 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005431 ip, whichfork, &icur, &cur,
Brian Foster92f9da32018-07-11 22:26:28 -07005432 &del, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 if (error)
5434 goto error0;
5435 goto nodelete;
5436 }
5437 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005438
Dave Chinner0703a8e2018-06-08 09:54:22 -07005439delete:
Brian Fosterb2706a02016-03-15 11:42:46 +11005440 if (wasdel) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005441 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005442 &got, &del);
5443 } else {
Brian Foster81ba8f32018-07-11 22:26:16 -07005444 error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5445 &del, &tmp_logflags, whichfork,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005446 flags);
5447 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005448 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005449
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 if (error)
5451 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005452
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005453 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005454 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 /*
5457 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005459 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005460 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5461 (got.br_startoff > end &&
5462 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5463 done = true;
5464 break;
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 extno++;
5467 }
5468 }
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005469 if (done || end == (xfs_fileoff_t)-1 || end < start)
Darrick J. Wong44535932016-10-03 09:11:29 -07005470 *rlen = 0;
5471 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005472 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005473
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 /*
5475 * Convert to a btree if necessary.
5476 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005477 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07005479 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5480 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 logflags |= tmp_logflags;
Christoph Hellwigb101e332019-02-15 08:02:47 -08005482 } else {
5483 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 }
Christoph Hellwigb101e332019-02-15 08:02:47 -08005486
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487error0:
5488 /*
5489 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005490 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005492 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005494 logflags &= ~xfs_ilog_fext(whichfork);
5495 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005497 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 /*
5499 * Log inode even in the error case, if the transaction
5500 * is dirty we'll need to shut down the filesystem.
5501 */
5502 if (logflags)
5503 xfs_trans_log_inode(tp, ip, logflags);
5504 if (cur) {
Brian Fostercf612de2018-07-11 22:26:29 -07005505 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 cur->bc_private.b.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07005507 xfs_btree_del_cursor(cur, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 }
5509 return error;
5510}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005511
Darrick J. Wong44535932016-10-03 09:11:29 -07005512/* Unmap a range of a file. */
5513int
5514xfs_bunmapi(
5515 xfs_trans_t *tp,
5516 struct xfs_inode *ip,
5517 xfs_fileoff_t bno,
5518 xfs_filblks_t len,
5519 int flags,
5520 xfs_extnum_t nexts,
Darrick J. Wong44535932016-10-03 09:11:29 -07005521 int *done)
5522{
5523 int error;
5524
Brian Foster2af52842018-07-11 22:26:25 -07005525 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
Darrick J. Wong44535932016-10-03 09:11:29 -07005526 *done = (len == 0);
5527 return error;
5528}
5529
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005530/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005531 * Determine whether an extent shift can be accomplished by a merge with the
5532 * extent that precedes the target hole of the shift.
5533 */
5534STATIC bool
5535xfs_bmse_can_merge(
5536 struct xfs_bmbt_irec *left, /* preceding extent */
5537 struct xfs_bmbt_irec *got, /* current extent to shift */
5538 xfs_fileoff_t shift) /* shift fsb */
5539{
5540 xfs_fileoff_t startoff;
5541
5542 startoff = got->br_startoff - shift;
5543
5544 /*
5545 * The extent, once shifted, must be adjacent in-file and on-disk with
5546 * the preceding extent.
5547 */
5548 if ((left->br_startoff + left->br_blockcount != startoff) ||
5549 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5550 (left->br_state != got->br_state) ||
5551 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5552 return false;
5553
5554 return true;
5555}
5556
5557/*
5558 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5559 * hole in the file. If an extent shift would result in the extent being fully
5560 * adjacent to the extent that currently precedes the hole, we can merge with
5561 * the preceding extent rather than do the shift.
5562 *
5563 * This function assumes the caller has verified a shift-by-merge is possible
5564 * with the provided extents via xfs_bmse_can_merge().
5565 */
5566STATIC int
5567xfs_bmse_merge(
Brian Foster0f37d172018-08-01 07:20:34 -07005568 struct xfs_trans *tp,
Brian Fosterddb19e32014-09-23 15:38:09 +10005569 struct xfs_inode *ip,
5570 int whichfork,
5571 xfs_fileoff_t shift, /* shift fsb */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005572 struct xfs_iext_cursor *icur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005573 struct xfs_bmbt_irec *got, /* extent to shift */
5574 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005575 struct xfs_btree_cur *cur,
Brian Foster0f37d172018-08-01 07:20:34 -07005576 int *logflags) /* output */
Brian Fosterddb19e32014-09-23 15:38:09 +10005577{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005578 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005579 xfs_filblks_t blockcount;
5580 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005581 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005582
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005583 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005584
5585 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5586 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005587 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005588
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005589 new = *left;
5590 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005591
5592 /*
5593 * Update the on-disk extent count, the btree if necessary and log the
5594 * inode.
5595 */
5596 XFS_IFORK_NEXT_SET(ip, whichfork,
5597 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5598 *logflags |= XFS_ILOG_CORE;
5599 if (!cur) {
5600 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005601 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005602 }
5603
5604 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005605 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005606 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005607 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005608 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005609
5610 error = xfs_btree_delete(cur, &i);
5611 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005612 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005613 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005614
5615 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005616 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005617 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005618 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005619 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005620
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005621 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005622 if (error)
5623 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005624
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005625done:
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005626 xfs_iext_remove(ip, icur, 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005627 xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur);
5628 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5629 &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005630
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005631 /* update reverse mapping. rmap functions merge the rmaps for us */
Brian Foster0f37d172018-08-01 07:20:34 -07005632 error = xfs_rmap_unmap_extent(tp, ip, whichfork, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005633 if (error)
5634 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005635 memcpy(&new, got, sizeof(new));
5636 new.br_startoff = left->br_startoff + left->br_blockcount;
Brian Foster0f37d172018-08-01 07:20:34 -07005637 return xfs_rmap_map_extent(tp, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005638}
5639
Christoph Hellwigbf806282017-10-19 11:07:34 -07005640static int
5641xfs_bmap_shift_update_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07005642 struct xfs_trans *tp,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005643 struct xfs_inode *ip,
5644 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005645 struct xfs_iext_cursor *icur,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005646 struct xfs_bmbt_irec *got,
5647 struct xfs_btree_cur *cur,
5648 int *logflags,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005649 xfs_fileoff_t startoff)
Brian Fostera979bdf2014-09-23 15:39:04 +10005650{
Christoph Hellwigbf806282017-10-19 11:07:34 -07005651 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005652 struct xfs_bmbt_irec prev = *got;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005653 int error, i;
Brian Fostera979bdf2014-09-23 15:39:04 +10005654
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005655 *logflags |= XFS_ILOG_CORE;
5656
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005657 got->br_startoff = startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005658
5659 if (cur) {
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005660 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005661 if (error)
5662 return error;
5663 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5664
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005665 error = xfs_bmbt_update(cur, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005666 if (error)
5667 return error;
5668 } else {
5669 *logflags |= XFS_ILOG_DEXT;
5670 }
5671
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005672 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5673 got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005674
Darrick J. Wong9c194642016-08-03 12:16:05 +10005675 /* update reverse mapping */
Brian Foster0f37d172018-08-01 07:20:34 -07005676 error = xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005677 if (error)
5678 return error;
Brian Foster0f37d172018-08-01 07:20:34 -07005679 return xfs_rmap_map_extent(tp, ip, whichfork, got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005680}
5681
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005682int
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005683xfs_bmap_collapse_extents(
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005684 struct xfs_trans *tp,
5685 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005686 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005687 xfs_fileoff_t offset_shift_fsb,
Brian Foster333f9502018-07-11 22:26:27 -07005688 bool *done)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005689{
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005690 int whichfork = XFS_DATA_FORK;
5691 struct xfs_mount *mp = ip->i_mount;
5692 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5693 struct xfs_btree_cur *cur = NULL;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005694 struct xfs_bmbt_irec got, prev;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005695 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005696 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005697 int error = 0;
5698 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005699
5700 if (unlikely(XFS_TEST_ERROR(
5701 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5702 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005703 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005704 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005705 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005706 }
5707
5708 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005709 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005710
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005711 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005712
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005713 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005714 error = xfs_iread_extents(tp, ip, whichfork);
5715 if (error)
5716 return error;
5717 }
5718
Brian Fosterddb19e32014-09-23 15:38:09 +10005719 if (ifp->if_flags & XFS_IFBROOT) {
5720 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Brian Fosterddb19e32014-09-23 15:38:09 +10005721 cur->bc_private.b.flags = 0;
5722 }
5723
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005724 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005725 *done = true;
5726 goto del_cursor;
5727 }
Eric Sandeend41c6172017-11-27 18:23:32 -08005728 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5729 del_cursor);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005730
Christoph Hellwigbf806282017-10-19 11:07:34 -07005731 new_startoff = got.br_startoff - offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005732 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005733 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5734 error = -EINVAL;
5735 goto del_cursor;
5736 }
5737
Christoph Hellwigbf806282017-10-19 11:07:34 -07005738 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
Brian Foster0f37d172018-08-01 07:20:34 -07005739 error = xfs_bmse_merge(tp, ip, whichfork,
5740 offset_shift_fsb, &icur, &got, &prev,
5741 cur, &logflags);
Christoph Hellwigbf806282017-10-19 11:07:34 -07005742 if (error)
5743 goto del_cursor;
5744 goto done;
5745 }
5746 } else {
5747 if (got.br_startoff < offset_shift_fsb) {
5748 error = -EINVAL;
5749 goto del_cursor;
5750 }
5751 }
5752
Brian Foster0f37d172018-08-01 07:20:34 -07005753 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5754 cur, &logflags, new_startoff);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005755 if (error)
5756 goto del_cursor;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005757
Christoph Hellwig42630362017-11-03 10:34:41 -07005758done:
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005759 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5760 *done = true;
5761 goto del_cursor;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005762 }
5763
Christoph Hellwigbf806282017-10-19 11:07:34 -07005764 *next_fsb = got.br_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005765del_cursor:
5766 if (cur)
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07005767 xfs_btree_del_cursor(cur, error);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005768 if (logflags)
5769 xfs_trans_log_inode(tp, ip, logflags);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005770 return error;
5771}
5772
Darrick J. Wongf62cb482018-06-21 23:26:57 -07005773/* Make sure we won't be right-shifting an extent past the maximum bound. */
5774int
5775xfs_bmap_can_insert_extents(
5776 struct xfs_inode *ip,
5777 xfs_fileoff_t off,
5778 xfs_fileoff_t shift)
5779{
5780 struct xfs_bmbt_irec got;
5781 int is_empty;
5782 int error = 0;
5783
5784 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5785
5786 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5787 return -EIO;
5788
5789 xfs_ilock(ip, XFS_ILOCK_EXCL);
5790 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
5791 if (!error && !is_empty && got.br_startoff >= off &&
5792 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
5793 error = -EINVAL;
5794 xfs_iunlock(ip, XFS_ILOCK_EXCL);
5795
5796 return error;
5797}
5798
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005799int
5800xfs_bmap_insert_extents(
5801 struct xfs_trans *tp,
5802 struct xfs_inode *ip,
5803 xfs_fileoff_t *next_fsb,
5804 xfs_fileoff_t offset_shift_fsb,
5805 bool *done,
Brian Foster333f9502018-07-11 22:26:27 -07005806 xfs_fileoff_t stop_fsb)
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005807{
5808 int whichfork = XFS_DATA_FORK;
5809 struct xfs_mount *mp = ip->i_mount;
5810 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5811 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005812 struct xfs_bmbt_irec got, next;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005813 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005814 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005815 int error = 0;
5816 int logflags = 0;
5817
5818 if (unlikely(XFS_TEST_ERROR(
5819 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5820 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5821 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5822 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
5823 return -EFSCORRUPTED;
5824 }
5825
5826 if (XFS_FORCED_SHUTDOWN(mp))
5827 return -EIO;
5828
5829 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5830
5831 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5832 error = xfs_iread_extents(tp, ip, whichfork);
5833 if (error)
5834 return error;
5835 }
5836
5837 if (ifp->if_flags & XFS_IFBROOT) {
5838 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005839 cur->bc_private.b.flags = 0;
5840 }
5841
Namjae Jeona904b1c2015-03-25 15:08:56 +11005842 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005843 xfs_iext_last(ifp, &icur);
5844 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005845 stop_fsb > got.br_startoff) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005846 *done = true;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005847 goto del_cursor;
5848 }
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005849 } else {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005850 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005851 *done = true;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005852 goto del_cursor;
5853 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005854 }
Eric Sandeend41c6172017-11-27 18:23:32 -08005855 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5856 del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005857
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005858 if (stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005859 error = -EIO;
5860 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005861 }
5862
Christoph Hellwigbf806282017-10-19 11:07:34 -07005863 new_startoff = got.br_startoff + offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005864 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005865 if (new_startoff + got.br_blockcount > next.br_startoff) {
5866 error = -EINVAL;
5867 goto del_cursor;
5868 }
5869
5870 /*
5871 * Unlike a left shift (which involves a hole punch), a right
5872 * shift does not modify extent neighbors in any way. We should
5873 * never find mergeable extents in this scenario. Check anyways
5874 * and warn if we encounter two extents that could be one.
5875 */
5876 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5877 WARN_ON_ONCE(1);
5878 }
5879
Brian Foster0f37d172018-08-01 07:20:34 -07005880 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5881 cur, &logflags, new_startoff);
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005882 if (error)
5883 goto del_cursor;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005884
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005885 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005886 stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005887 *done = true;
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005888 goto del_cursor;
5889 }
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005890
5891 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005892del_cursor:
5893 if (cur)
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07005894 xfs_btree_del_cursor(cur, error);
Brian Fosterca446d82014-09-02 12:12:53 +10005895 if (logflags)
5896 xfs_trans_log_inode(tp, ip, logflags);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005897 return error;
5898}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005899
5900/*
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005901 * Splits an extent into two extents at split_fsb block such that it is the
5902 * first block of the current_ext. @ext is a target extent to be split.
5903 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5904 * hole or the first block of extents, just return 0.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005905 */
5906STATIC int
5907xfs_bmap_split_extent_at(
5908 struct xfs_trans *tp,
5909 struct xfs_inode *ip,
Brian Foster4b77a082018-07-11 22:26:27 -07005910 xfs_fileoff_t split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005911{
5912 int whichfork = XFS_DATA_FORK;
5913 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005914 struct xfs_bmbt_irec got;
5915 struct xfs_bmbt_irec new; /* split extent */
5916 struct xfs_mount *mp = ip->i_mount;
5917 struct xfs_ifork *ifp;
5918 xfs_fsblock_t gotblkcnt; /* new block count for got */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005919 struct xfs_iext_cursor icur;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005920 int error = 0;
5921 int logflags = 0;
5922 int i = 0;
5923
5924 if (unlikely(XFS_TEST_ERROR(
5925 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5926 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005927 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11005928 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5929 XFS_ERRLEVEL_LOW, mp);
5930 return -EFSCORRUPTED;
5931 }
5932
5933 if (XFS_FORCED_SHUTDOWN(mp))
5934 return -EIO;
5935
5936 ifp = XFS_IFORK_PTR(ip, whichfork);
5937 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5938 /* Read in all the extents */
5939 error = xfs_iread_extents(tp, ip, whichfork);
5940 if (error)
5941 return error;
5942 }
5943
5944 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005945 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005946 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005947 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005948 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005949 return 0;
5950
5951 gotblkcnt = split_fsb - got.br_startoff;
5952 new.br_startoff = split_fsb;
5953 new.br_startblock = got.br_startblock + gotblkcnt;
5954 new.br_blockcount = got.br_blockcount - gotblkcnt;
5955 new.br_state = got.br_state;
5956
5957 if (ifp->if_flags & XFS_IFBROOT) {
5958 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005959 cur->bc_private.b.flags = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005960 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005961 if (error)
5962 goto del_cursor;
5963 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5964 }
5965
Namjae Jeona904b1c2015-03-25 15:08:56 +11005966 got.br_blockcount = gotblkcnt;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005967 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
5968 &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005969
5970 logflags = XFS_ILOG_CORE;
5971 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005972 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005973 if (error)
5974 goto del_cursor;
5975 } else
5976 logflags |= XFS_ILOG_DEXT;
5977
5978 /* Add new extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005979 xfs_iext_next(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005980 xfs_iext_insert(ip, &icur, &new, 0);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005981 XFS_IFORK_NEXT_SET(ip, whichfork,
5982 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5983
5984 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005985 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005986 if (error)
5987 goto del_cursor;
5988 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005989 error = xfs_btree_insert(cur, &i);
5990 if (error)
5991 goto del_cursor;
5992 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5993 }
5994
5995 /*
5996 * Convert to a btree if necessary.
5997 */
5998 if (xfs_bmap_needs_btree(ip, whichfork)) {
5999 int tmp_logflags; /* partial log flag return val */
6000
6001 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07006002 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
6003 &tmp_logflags, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006004 logflags |= tmp_logflags;
6005 }
6006
6007del_cursor:
6008 if (cur) {
6009 cur->bc_private.b.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07006010 xfs_btree_del_cursor(cur, error);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006011 }
6012
6013 if (logflags)
6014 xfs_trans_log_inode(tp, ip, logflags);
6015 return error;
6016}
6017
6018int
6019xfs_bmap_split_extent(
6020 struct xfs_inode *ip,
6021 xfs_fileoff_t split_fsb)
6022{
6023 struct xfs_mount *mp = ip->i_mount;
6024 struct xfs_trans *tp;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006025 int error;
6026
Christoph Hellwig253f4912016-04-06 09:19:55 +10006027 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6028 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6029 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006030 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006031
6032 xfs_ilock(ip, XFS_ILOCK_EXCL);
6033 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6034
Brian Foster4b77a082018-07-11 22:26:27 -07006035 error = xfs_bmap_split_extent_at(tp, ip, split_fsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006036 if (error)
6037 goto out;
6038
Christoph Hellwig70393312015-06-04 13:48:08 +10006039 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006040
6041out:
Christoph Hellwig4906e212015-06-04 13:47:56 +10006042 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006043 return error;
6044}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006045
6046/* Deferred mapping is only for real extents in the data fork. */
6047static bool
6048xfs_bmap_is_update_needed(
6049 struct xfs_bmbt_irec *bmap)
6050{
6051 return bmap->br_startblock != HOLESTARTBLOCK &&
6052 bmap->br_startblock != DELAYSTARTBLOCK;
6053}
6054
6055/* Record a bmap intent. */
6056static int
6057__xfs_bmap_add(
Brian Foster0f37d172018-08-01 07:20:34 -07006058 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006059 enum xfs_bmap_intent_type type,
6060 struct xfs_inode *ip,
6061 int whichfork,
6062 struct xfs_bmbt_irec *bmap)
6063{
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006064 struct xfs_bmap_intent *bi;
6065
Brian Foster0f37d172018-08-01 07:20:34 -07006066 trace_xfs_bmap_defer(tp->t_mountp,
6067 XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock),
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006068 type,
Brian Foster0f37d172018-08-01 07:20:34 -07006069 XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock),
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006070 ip->i_ino, whichfork,
6071 bmap->br_startoff,
6072 bmap->br_blockcount,
6073 bmap->br_state);
6074
6075 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6076 INIT_LIST_HEAD(&bi->bi_list);
6077 bi->bi_type = type;
6078 bi->bi_owner = ip;
6079 bi->bi_whichfork = whichfork;
6080 bi->bi_bmap = *bmap;
6081
Brian Foster0f37d172018-08-01 07:20:34 -07006082 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006083 return 0;
6084}
6085
6086/* Map an extent into a file. */
6087int
6088xfs_bmap_map_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07006089 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006090 struct xfs_inode *ip,
6091 struct xfs_bmbt_irec *PREV)
6092{
6093 if (!xfs_bmap_is_update_needed(PREV))
6094 return 0;
6095
Brian Foster0f37d172018-08-01 07:20:34 -07006096 return __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, XFS_DATA_FORK, PREV);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006097}
6098
6099/* Unmap an extent out of a file. */
6100int
6101xfs_bmap_unmap_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07006102 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006103 struct xfs_inode *ip,
6104 struct xfs_bmbt_irec *PREV)
6105{
6106 if (!xfs_bmap_is_update_needed(PREV))
6107 return 0;
6108
Brian Foster0f37d172018-08-01 07:20:34 -07006109 return __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, XFS_DATA_FORK, PREV);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006110}
6111
6112/*
6113 * Process one of the deferred bmap operations. We pass back the
6114 * btree cursor to maintain our lock on the bmapbt between calls.
6115 */
6116int
6117xfs_bmap_finish_one(
6118 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006119 struct xfs_inode *ip,
6120 enum xfs_bmap_intent_type type,
6121 int whichfork,
6122 xfs_fileoff_t startoff,
6123 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006124 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006125 xfs_exntst_t state)
6126{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006127 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006128
Brian Foster37283792018-07-11 22:26:23 -07006129 ASSERT(tp->t_firstblock == NULLFSBLOCK);
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006130
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006131 trace_xfs_bmap_deferred(tp->t_mountp,
6132 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6133 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006134 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006135
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006136 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006137 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006138
6139 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006140 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006141 return -EIO;
6142
6143 switch (type) {
6144 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006145 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Brian Fosterff3edf22018-07-11 22:26:14 -07006146 startblock, 0);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006147 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006148 break;
6149 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006150 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
Brian Foster2af52842018-07-11 22:26:25 -07006151 XFS_BMAPI_REMAP, 1);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006152 break;
6153 default:
6154 ASSERT(0);
6155 error = -EFSCORRUPTED;
6156 }
6157
6158 return error;
6159}
Darrick J. Wong30b09842018-03-23 10:06:52 -07006160
6161/* Check that an inode's extent does not have invalid flags or bad ranges. */
6162xfs_failaddr_t
6163xfs_bmap_validate_extent(
6164 struct xfs_inode *ip,
6165 int whichfork,
6166 struct xfs_bmbt_irec *irec)
6167{
6168 struct xfs_mount *mp = ip->i_mount;
6169 xfs_fsblock_t endfsb;
6170 bool isrt;
6171
6172 isrt = XFS_IS_REALTIME_INODE(ip);
6173 endfsb = irec->br_startblock + irec->br_blockcount - 1;
6174 if (isrt) {
6175 if (!xfs_verify_rtbno(mp, irec->br_startblock))
6176 return __this_address;
6177 if (!xfs_verify_rtbno(mp, endfsb))
6178 return __this_address;
6179 } else {
6180 if (!xfs_verify_fsbno(mp, irec->br_startblock))
6181 return __this_address;
6182 if (!xfs_verify_fsbno(mp, endfsb))
6183 return __this_address;
6184 if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) !=
6185 XFS_FSB_TO_AGNO(mp, endfsb))
6186 return __this_address;
6187 }
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11006188 if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6189 return __this_address;
Darrick J. Wong30b09842018-03-23 10:06:52 -07006190 return NULL;
6191}