blob: 65f4348af9ae323400d61ac58ccde35322291675 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11007#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11008#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +11009#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110012#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_sb.h"
Dave Chinnerf5ea1102013-04-24 18:58:02 +100014#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100015#include "xfs_defer.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100016#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110018#include "xfs_btree.h"
Dave Chinner239880e2013-10-23 10:50:10 +110019#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_alloc.h"
21#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100022#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110023#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_rtalloc.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070025#include "xfs_errortag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_quota.h"
28#include "xfs_trans_space.h"
29#include "xfs_buf_item.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000030#include "xfs_trace.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110031#include "xfs_attr_leaf.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110032#include "xfs_filestream.h"
Darrick J. Wong340785c2016-08-03 11:33:42 +100033#include "xfs_rmap.h"
Darrick J. Wong3fd129b2016-09-19 10:30:52 +100034#include "xfs_ag_resv.h"
Darrick J. Wong62aab202016-10-03 09:11:23 -070035#include "xfs_refcount.h"
Brian Foster974ae922016-11-28 14:57:42 +110036#include "xfs_icache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039kmem_zone_t *xfs_bmap_free_item_zone;
40
41/*
Dave Chinner9e5987a72013-02-25 12:31:26 +110042 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 */
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
Dave Chinner9e5987a72013-02-25 12:31:26 +110046 * Compute and fill in the value of the maximum depth of a bmap btree
47 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
Dave Chinner9e5987a72013-02-25 12:31:26 +110049void
50xfs_bmap_compute_maxlevels(
51 xfs_mount_t *mp, /* file system mount structure */
52 int whichfork) /* data or attr fork */
53{
54 int level; /* btree level */
55 uint maxblocks; /* max blocks at this level */
56 uint maxleafents; /* max leaf entries possible */
57 int maxrootrecs; /* max records in root block */
58 int minleafrecs; /* min records in leaf block */
59 int minnoderecs; /* min records in node block */
60 int sz; /* root block size */
61
62 /*
63 * The maximum number of extents in a file, hence the maximum
64 * number of leaf entries, is controlled by the type of di_nextents
65 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
66 * (a signed 16-bit number, xfs_aextnum_t).
67 *
68 * Note that we can no longer assume that if we are in ATTR1 that
69 * the fork offset of all the inodes will be
70 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
71 * with ATTR2 and then mounted back with ATTR1, keeping the
72 * di_forkoff's fixed but probably at various positions. Therefore,
73 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
74 * of a minimum size available.
75 */
76 if (whichfork == XFS_DATA_FORK) {
77 maxleafents = MAXEXTNUM;
78 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
79 } else {
80 maxleafents = MAXAEXTNUM;
81 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
82 }
Eric Sandeen152d93b2014-04-14 18:58:51 +100083 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +110084 minleafrecs = mp->m_bmap_dmnr[0];
85 minnoderecs = mp->m_bmap_dmnr[1];
86 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
87 for (level = 1; maxblocks > 1; level++) {
88 if (maxblocks <= maxrootrecs)
89 maxblocks = 1;
90 else
91 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
92 }
93 mp->m_bm_maxlevels[whichfork] = level;
94}
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110096STATIC int /* error */
97xfs_bmbt_lookup_eq(
98 struct xfs_btree_cur *cur,
Christoph Hellwige16cf9b2017-10-17 14:16:26 -070099 struct xfs_bmbt_irec *irec,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100100 int *stat) /* success/failure */
101{
Christoph Hellwige16cf9b2017-10-17 14:16:26 -0700102 cur->bc_rec.b = *irec;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100103 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
104}
105
106STATIC int /* error */
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700107xfs_bmbt_lookup_first(
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100108 struct xfs_btree_cur *cur,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100109 int *stat) /* success/failure */
110{
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700111 cur->bc_rec.b.br_startoff = 0;
112 cur->bc_rec.b.br_startblock = 0;
113 cur->bc_rec.b.br_blockcount = 0;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100114 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
115}
116
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100117/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000118 * Check if the inode needs to be converted to btree format.
119 */
120static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
121{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700122 return whichfork != XFS_COW_FORK &&
123 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000124 XFS_IFORK_NEXTENTS(ip, whichfork) >
125 XFS_IFORK_MAXEXT(ip, whichfork);
126}
127
128/*
129 * Check if the inode should be converted to extent format.
130 */
131static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
132{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700133 return whichfork != XFS_COW_FORK &&
134 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000135 XFS_IFORK_NEXTENTS(ip, whichfork) <=
136 XFS_IFORK_MAXEXT(ip, whichfork);
137}
138
139/*
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700140 * Update the record referred to by cur to the value given by irec
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100141 * This either works (return 0) or gets an EFSCORRUPTED error.
142 */
143STATIC int
144xfs_bmbt_update(
145 struct xfs_btree_cur *cur,
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700146 struct xfs_bmbt_irec *irec)
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100147{
148 union xfs_btree_rec rec;
149
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700150 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100151 return xfs_btree_update(cur, &rec);
152}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
Dave Chinner9e5987a72013-02-25 12:31:26 +1100155 * Compute the worst-case number of indirect blocks that will be used
156 * for ip's delayed extent of length "len".
157 */
158STATIC xfs_filblks_t
159xfs_bmap_worst_indlen(
160 xfs_inode_t *ip, /* incore inode pointer */
161 xfs_filblks_t len) /* delayed extent length */
162{
163 int level; /* btree level number */
164 int maxrecs; /* maximum record count at this level */
165 xfs_mount_t *mp; /* mount structure */
166 xfs_filblks_t rval; /* return value */
167
168 mp = ip->i_mount;
169 maxrecs = mp->m_bmap_dmxr[0];
170 for (level = 0, rval = 0;
171 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
172 level++) {
173 len += maxrecs - 1;
174 do_div(len, maxrecs);
175 rval += len;
Darrick J. Wong5e5c9432017-09-18 09:41:17 -0700176 if (len == 1)
177 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
Dave Chinner9e5987a72013-02-25 12:31:26 +1100178 level - 1;
179 if (level == 0)
180 maxrecs = mp->m_bmap_dmxr[1];
181 }
182 return rval;
183}
184
185/*
186 * Calculate the default attribute fork offset for newly created inodes.
187 */
188uint
189xfs_default_attroffset(
190 struct xfs_inode *ip)
191{
192 struct xfs_mount *mp = ip->i_mount;
193 uint offset;
194
195 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100196 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a72013-02-25 12:31:26 +1100197 XFS_BMDR_SPACE_CALC(MINABTPTRS);
198 } else {
199 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
200 }
201
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100202 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100203 return offset;
204}
205
206/*
207 * Helper routine to reset inode di_forkoff field when switching
208 * attribute fork from local to extent format - we reset it where
209 * possible to make space available for inline data fork extents.
210 */
211STATIC void
212xfs_bmap_forkoff_reset(
Dave Chinner9e5987a72013-02-25 12:31:26 +1100213 xfs_inode_t *ip,
214 int whichfork)
215{
216 if (whichfork == XFS_ATTR_FORK &&
217 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
Dave Chinner9e5987a72013-02-25 12:31:26 +1100218 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
219 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
220
221 if (dfl_forkoff > ip->i_d.di_forkoff)
222 ip->i_d.di_forkoff = dfl_forkoff;
223 }
224}
225
Dave Chinner9e5987a72013-02-25 12:31:26 +1100226#ifdef DEBUG
227STATIC struct xfs_buf *
228xfs_bmap_get_bp(
229 struct xfs_btree_cur *cur,
230 xfs_fsblock_t bno)
231{
Dave Chinnere6631f82018-05-09 07:49:37 -0700232 struct xfs_log_item *lip;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100233 int i;
234
235 if (!cur)
236 return NULL;
237
238 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
239 if (!cur->bc_bufs[i])
240 break;
241 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
242 return cur->bc_bufs[i];
243 }
244
245 /* Chase down all the log items to see if the bp is there */
Dave Chinnere6631f82018-05-09 07:49:37 -0700246 list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
247 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
248
Dave Chinner9e5987a72013-02-25 12:31:26 +1100249 if (bip->bli_item.li_type == XFS_LI_BUF &&
250 XFS_BUF_ADDR(bip->bli_buf) == bno)
251 return bip->bli_buf;
252 }
253
254 return NULL;
255}
256
257STATIC void
258xfs_check_block(
259 struct xfs_btree_block *block,
260 xfs_mount_t *mp,
261 int root,
262 short sz)
263{
264 int i, j, dmxr;
265 __be64 *pp, *thispa; /* pointer to block address */
266 xfs_bmbt_key_t *prevp, *keyp;
267
268 ASSERT(be16_to_cpu(block->bb_level) > 0);
269
270 prevp = NULL;
271 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
272 dmxr = mp->m_bmap_dmxr[0];
273 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
274
275 if (prevp) {
276 ASSERT(be64_to_cpu(prevp->br_startoff) <
277 be64_to_cpu(keyp->br_startoff));
278 }
279 prevp = keyp;
280
281 /*
282 * Compare the block numbers to see if there are dups.
283 */
284 if (root)
285 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
286 else
287 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
288
289 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
290 if (root)
291 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
292 else
293 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
294 if (*thispa == *pp) {
295 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
296 __func__, j, i,
297 (unsigned long long)be64_to_cpu(*thispa));
Darrick J. Wongcec57252018-05-04 15:31:21 -0700298 xfs_err(mp, "%s: ptrs are equal in node\n",
Dave Chinner9e5987a72013-02-25 12:31:26 +1100299 __func__);
Darrick J. Wongcec57252018-05-04 15:31:21 -0700300 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100301 }
302 }
303 }
304}
305
306/*
307 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100308 * btree leaves. THis becomes prohibitively expensive for large extent count
309 * files, so don't bother with inodes that have more than 10,000 extents in
310 * them. The btree record ordering checks will still be done, so for such large
311 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100312 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100313STATIC void
314xfs_bmap_check_leaf_extents(
315 xfs_btree_cur_t *cur, /* btree cursor or null */
316 xfs_inode_t *ip, /* incore inode pointer */
317 int whichfork) /* data or attr fork */
318{
319 struct xfs_btree_block *block; /* current btree block */
320 xfs_fsblock_t bno; /* block # of "block" */
321 xfs_buf_t *bp; /* buffer for "block" */
322 int error; /* error return value */
323 xfs_extnum_t i=0, j; /* index into the extents list */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -0700324 struct xfs_ifork *ifp; /* fork structure */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100325 int level; /* btree level, for checking */
326 xfs_mount_t *mp; /* file system mount structure */
327 __be64 *pp; /* pointer to block address */
328 xfs_bmbt_rec_t *ep; /* pointer to current extent */
329 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
330 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
331 int bp_release = 0;
332
333 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
334 return;
335 }
336
Dave Chinnere3543812016-01-08 11:28:49 +1100337 /* skip large extent count inodes */
338 if (ip->i_d.di_nextents > 10000)
339 return;
340
Dave Chinner9e5987a72013-02-25 12:31:26 +1100341 bno = NULLFSBLOCK;
342 mp = ip->i_mount;
343 ifp = XFS_IFORK_PTR(ip, whichfork);
344 block = ifp->if_broot;
345 /*
346 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
347 */
348 level = be16_to_cpu(block->bb_level);
349 ASSERT(level > 0);
350 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
351 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
352 bno = be64_to_cpu(*pp);
353
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000354 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100355 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
356 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
357
358 /*
359 * Go down the tree until leaf level is reached, following the first
360 * pointer (leftmost) at each level.
361 */
362 while (level-- > 0) {
363 /* See if buf is in cur first */
364 bp_release = 0;
365 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
366 if (!bp) {
367 bp_release = 1;
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700368 error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100369 XFS_BMAP_BTREE_REF,
370 &xfs_bmbt_buf_ops);
371 if (error)
372 goto error_norelse;
373 }
374 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100375 if (level == 0)
376 break;
377
378 /*
379 * Check this block for basic sanity (increasing keys and
380 * no duplicate blocks).
381 */
382
383 xfs_check_block(block, mp, 0, 0);
384 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
385 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100386 XFS_WANT_CORRUPTED_GOTO(mp,
Darrick J. Wong59f6fec2018-01-08 10:51:00 -0800387 xfs_verify_fsbno(mp, bno), error0);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100388 if (bp_release) {
389 bp_release = 0;
390 xfs_trans_brelse(NULL, bp);
391 }
392 }
393
394 /*
395 * Here with bp and block set to the leftmost leaf node in the tree.
396 */
397 i = 0;
398
399 /*
400 * Loop over all leaf nodes checking that all extents are in the right order.
401 */
402 for (;;) {
403 xfs_fsblock_t nextbno;
404 xfs_extnum_t num_recs;
405
406
407 num_recs = xfs_btree_get_numrecs(block);
408
409 /*
410 * Read-ahead the next leaf block, if any.
411 */
412
413 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
414
415 /*
416 * Check all the extents to make sure they are OK.
417 * If we had a previous block, the last entry should
418 * conform with the first entry in this one.
419 */
420
421 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
422 if (i) {
423 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
424 xfs_bmbt_disk_get_blockcount(&last) <=
425 xfs_bmbt_disk_get_startoff(ep));
426 }
427 for (j = 1; j < num_recs; j++) {
428 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
429 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
430 xfs_bmbt_disk_get_blockcount(ep) <=
431 xfs_bmbt_disk_get_startoff(nextp));
432 ep = nextp;
433 }
434
435 last = *ep;
436 i += num_recs;
437 if (bp_release) {
438 bp_release = 0;
439 xfs_trans_brelse(NULL, bp);
440 }
441 bno = nextbno;
442 /*
443 * If we've reached the end, stop.
444 */
445 if (bno == NULLFSBLOCK)
446 break;
447
448 bp_release = 0;
449 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
450 if (!bp) {
451 bp_release = 1;
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700452 error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100453 XFS_BMAP_BTREE_REF,
454 &xfs_bmbt_buf_ops);
455 if (error)
456 goto error_norelse;
457 }
458 block = XFS_BUF_TO_BLOCK(bp);
459 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100460
Dave Chinner9e5987a72013-02-25 12:31:26 +1100461 return;
462
463error0:
464 xfs_warn(mp, "%s: at error0", __func__);
465 if (bp_release)
466 xfs_trans_brelse(NULL, bp);
467error_norelse:
468 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
469 __func__, i);
Darrick J. Wongcec57252018-05-04 15:31:21 -0700470 xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
471 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100472 return;
473}
474
475/*
Dave Chinner9e5987a72013-02-25 12:31:26 +1100476 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000477 * given the caller's original parameters. Specifically check the
478 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a72013-02-25 12:31:26 +1100479 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
480 */
481STATIC void
482xfs_bmap_validate_ret(
483 xfs_fileoff_t bno,
484 xfs_filblks_t len,
485 int flags,
486 xfs_bmbt_irec_t *mval,
487 int nmap,
488 int ret_nmap)
489{
490 int i; /* index to map values */
491
492 ASSERT(ret_nmap <= nmap);
493
494 for (i = 0; i < ret_nmap; i++) {
495 ASSERT(mval[i].br_blockcount > 0);
496 if (!(flags & XFS_BMAPI_ENTIRE)) {
497 ASSERT(mval[i].br_startoff >= bno);
498 ASSERT(mval[i].br_blockcount <= len);
499 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
500 bno + len);
501 } else {
502 ASSERT(mval[i].br_startoff < bno + len);
503 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
504 bno);
505 }
506 ASSERT(i == 0 ||
507 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
508 mval[i].br_startoff);
509 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
510 mval[i].br_startblock != HOLESTARTBLOCK);
511 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
512 mval[i].br_state == XFS_EXT_UNWRITTEN);
513 }
514}
515
516#else
517#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
Darrick J. Wong7bf7a192017-08-31 15:11:06 -0700518#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100519#endif /* DEBUG */
520
521/*
522 * bmap free list manipulation functions
523 */
524
525/*
526 * Add the extent to the list of extents to be free at transaction end.
527 * The list is maintained sorted (by block number).
528 */
529void
Brian Fosterfcb762f2018-05-09 08:45:04 -0700530__xfs_bmap_add_free(
Brian Foster0f37d172018-08-01 07:20:34 -0700531 struct xfs_trans *tp,
Darrick J. Wong340785c2016-08-03 11:33:42 +1000532 xfs_fsblock_t bno,
533 xfs_filblks_t len,
Darrick J. Wong66e32372018-12-12 08:46:23 -0800534 const struct xfs_owner_info *oinfo,
Brian Fosterfcb762f2018-05-09 08:45:04 -0700535 bool skip_discard)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100536{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000537 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100538#ifdef DEBUG
Brian Foster0f37d172018-08-01 07:20:34 -0700539 struct xfs_mount *mp = tp->t_mountp;
540 xfs_agnumber_t agno;
541 xfs_agblock_t agbno;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100542
543 ASSERT(bno != NULLFSBLOCK);
544 ASSERT(len > 0);
545 ASSERT(len <= MAXEXTLEN);
546 ASSERT(!isnullstartblock(bno));
547 agno = XFS_FSB_TO_AGNO(mp, bno);
548 agbno = XFS_FSB_TO_AGBNO(mp, bno);
549 ASSERT(agno < mp->m_sb.sb_agcount);
550 ASSERT(agbno < mp->m_sb.sb_agblocks);
551 ASSERT(len < mp->m_sb.sb_agblocks);
552 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
553#endif
554 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000555
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700556 new = kmem_zone_alloc(xfs_bmap_free_item_zone, 0);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000557 new->xefi_startblock = bno;
558 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000559 if (oinfo)
560 new->xefi_oinfo = *oinfo;
561 else
Darrick J. Wong7280fed2018-12-12 08:46:23 -0800562 new->xefi_oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
Brian Fosterfcb762f2018-05-09 08:45:04 -0700563 new->xefi_skip_discard = skip_discard;
Brian Foster0f37d172018-08-01 07:20:34 -0700564 trace_xfs_bmap_free_defer(tp->t_mountp,
565 XFS_FSB_TO_AGNO(tp->t_mountp, bno), 0,
566 XFS_FSB_TO_AGBNO(tp->t_mountp, bno), len);
567 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100568}
569
570/*
571 * Inode fork format manipulation functions
572 */
573
574/*
Christoph Hellwigb101e332019-02-15 08:02:47 -0800575 * Convert the inode format to extent format if it currently is in btree format,
576 * but the extent list is small enough that it fits into the extent format.
577 *
578 * Since the extents are already in-core, all we have to do is give up the space
579 * for the btree root and pitch the leaf block.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100580 */
581STATIC int /* error */
582xfs_bmap_btree_to_extents(
Christoph Hellwigb101e332019-02-15 08:02:47 -0800583 struct xfs_trans *tp, /* transaction pointer */
584 struct xfs_inode *ip, /* incore inode pointer */
585 struct xfs_btree_cur *cur, /* btree cursor */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100586 int *logflagsp, /* inode logging flags */
587 int whichfork) /* data or attr fork */
588{
Christoph Hellwigb101e332019-02-15 08:02:47 -0800589 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
590 struct xfs_mount *mp = ip->i_mount;
591 struct xfs_btree_block *rblock = ifp->if_broot;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100592 struct xfs_btree_block *cblock;/* child btree block */
593 xfs_fsblock_t cbno; /* child block number */
594 xfs_buf_t *cbp; /* child block's buffer */
595 int error; /* error return value */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100596 __be64 *pp; /* ptr to block address */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000597 struct xfs_owner_info oinfo;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100598
Christoph Hellwigb101e332019-02-15 08:02:47 -0800599 /* check if we actually need the extent format first: */
600 if (!xfs_bmap_wants_extents(ip, whichfork))
601 return 0;
602
603 ASSERT(cur);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700604 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100605 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
606 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100607 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
608 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
609 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
Christoph Hellwigb101e332019-02-15 08:02:47 -0800610
Dave Chinner9e5987a72013-02-25 12:31:26 +1100611 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
612 cbno = be64_to_cpu(*pp);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100613#ifdef DEBUG
Darrick J. Wongf1357612017-10-17 21:37:33 -0700614 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
615 xfs_btree_check_lptr(cur, cbno, 1));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100616#endif
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700617 error = xfs_btree_read_bufl(mp, tp, cbno, &cbp, XFS_BMAP_BTREE_REF,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100618 &xfs_bmbt_buf_ops);
619 if (error)
620 return error;
621 cblock = XFS_BUF_TO_BLOCK(cbp);
622 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
623 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000624 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
Brian Foster0f37d172018-08-01 07:20:34 -0700625 xfs_bmap_add_free(cur->bc_tp, cbno, 1, &oinfo);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100626 ip->i_d.di_nblocks--;
627 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
628 xfs_trans_binval(tp, cbp);
629 if (cur->bc_bufs[0] == cbp)
630 cur->bc_bufs[0] = NULL;
631 xfs_iroot_realloc(ip, -1, whichfork);
632 ASSERT(ifp->if_broot == NULL);
633 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
634 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Christoph Hellwigb101e332019-02-15 08:02:47 -0800635 *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100636 return 0;
637}
638
639/*
640 * Convert an extents-format file into a btree-format file.
641 * The new file will have a root block (in the inode) and a single child block.
642 */
643STATIC int /* error */
644xfs_bmap_extents_to_btree(
Brian Foster81ba8f32018-07-11 22:26:16 -0700645 struct xfs_trans *tp, /* transaction pointer */
646 struct xfs_inode *ip, /* incore inode pointer */
Brian Foster81ba8f32018-07-11 22:26:16 -0700647 struct xfs_btree_cur **curp, /* cursor returned to caller */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100648 int wasdel, /* converting a delayed alloc */
649 int *logflagsp, /* inode logging flags */
650 int whichfork) /* data or attr fork */
651{
652 struct xfs_btree_block *ablock; /* allocated (child) bt block */
Brian Foster81ba8f32018-07-11 22:26:16 -0700653 struct xfs_buf *abp; /* buffer for ablock */
654 struct xfs_alloc_arg args; /* allocation arguments */
655 struct xfs_bmbt_rec *arp; /* child record pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100656 struct xfs_btree_block *block; /* btree root block */
Brian Foster81ba8f32018-07-11 22:26:16 -0700657 struct xfs_btree_cur *cur; /* bmap btree cursor */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100658 int error; /* error return value */
Brian Foster81ba8f32018-07-11 22:26:16 -0700659 struct xfs_ifork *ifp; /* inode fork pointer */
660 struct xfs_bmbt_key *kp; /* root block key pointer */
661 struct xfs_mount *mp; /* mount structure */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100662 xfs_bmbt_ptr_t *pp; /* root block address pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700663 struct xfs_iext_cursor icur;
Christoph Hellwig906abed2017-11-03 10:34:43 -0700664 struct xfs_bmbt_irec rec;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700665 xfs_extnum_t cnt = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100666
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500667 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700668 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100669 ifp = XFS_IFORK_PTR(ip, whichfork);
670 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
671
672 /*
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000673 * Make space in the inode incore. This needs to be undone if we fail
674 * to expand the root.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100675 */
676 xfs_iroot_realloc(ip, 1, whichfork);
677 ifp->if_flags |= XFS_IFBROOT;
678
679 /*
680 * Fill in the root.
681 */
682 block = ifp->if_broot;
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800683 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
684 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
Eric Sandeenf88ae462017-01-27 23:16:37 -0800685 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100686 /*
687 * Need a cursor. Can't allocate until bb_level is filled in.
688 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100689 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100690 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
691 /*
692 * Convert to a btree with two levels, one record in root.
693 */
694 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
695 memset(&args, 0, sizeof(args));
696 args.tp = tp;
697 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000698 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Brian Foster280253d2018-07-11 22:26:29 -0700699 if (tp->t_firstblock == NULLFSBLOCK) {
Dave Chinner9e5987a72013-02-25 12:31:26 +1100700 args.type = XFS_ALLOCTYPE_START_BNO;
701 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Brian Foster1214f1c2018-08-01 07:20:31 -0700702 } else if (tp->t_flags & XFS_TRANS_LOWMODE) {
Dave Chinner9e5987a72013-02-25 12:31:26 +1100703 args.type = XFS_ALLOCTYPE_START_BNO;
Brian Foster280253d2018-07-11 22:26:29 -0700704 args.fsbno = tp->t_firstblock;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100705 } else {
706 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Brian Foster280253d2018-07-11 22:26:29 -0700707 args.fsbno = tp->t_firstblock;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100708 }
709 args.minlen = args.maxlen = args.prod = 1;
710 args.wasdel = wasdel;
711 *logflagsp = 0;
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000712 error = xfs_alloc_vextent(&args);
713 if (error)
714 goto out_root_realloc;
Darrick J. Wong90e20562016-10-03 09:11:45 -0700715
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800716 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
Shan Hai01239d772018-08-10 17:55:55 -0700717 error = -ENOSPC;
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000718 goto out_root_realloc;
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800719 }
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000720
Dave Chinner9e5987a72013-02-25 12:31:26 +1100721 /*
722 * Allocation can't fail, the space was reserved.
723 */
Brian Foster280253d2018-07-11 22:26:29 -0700724 ASSERT(tp->t_firstblock == NULLFSBLOCK ||
725 args.agno >= XFS_FSB_TO_AGNO(mp, tp->t_firstblock));
Brian Fostercf612de2018-07-11 22:26:29 -0700726 tp->t_firstblock = args.fsbno;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100727 cur->bc_private.b.allocated++;
728 ip->i_d.di_nblocks++;
729 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700730 abp = xfs_btree_get_bufl(mp, tp, args.fsbno);
Shan Hai01239d772018-08-10 17:55:55 -0700731 if (!abp) {
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000732 error = -EFSCORRUPTED;
733 goto out_unreserve_dquot;
Shan Hai01239d772018-08-10 17:55:55 -0700734 }
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000735
Dave Chinner9e5987a72013-02-25 12:31:26 +1100736 /*
737 * Fill in the child block.
738 */
739 abp->b_ops = &xfs_bmbt_buf_ops;
740 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800741 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
742 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500743 XFS_BTREE_LONG_PTRS);
744
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700745 for_each_xfs_iext(ifp, &icur, &rec) {
Christoph Hellwig906abed2017-11-03 10:34:43 -0700746 if (isnullstartblock(rec.br_startblock))
747 continue;
748 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
749 xfs_bmbt_disk_set_all(arp, &rec);
750 cnt++;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100751 }
752 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
753 xfs_btree_set_numrecs(ablock, cnt);
754
755 /*
756 * Fill in the root key and pointer.
757 */
758 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
759 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
760 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
761 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
762 be16_to_cpu(block->bb_level)));
763 *pp = cpu_to_be64(args.fsbno);
764
765 /*
766 * Do all this logging at the end so that
767 * the root is at the right level.
768 */
769 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
770 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
771 ASSERT(*curp == NULL);
772 *curp = cur;
773 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
774 return 0;
Shan Hai01239d772018-08-10 17:55:55 -0700775
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000776out_unreserve_dquot:
Shan Hai01239d772018-08-10 17:55:55 -0700777 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000778out_root_realloc:
Shan Hai01239d772018-08-10 17:55:55 -0700779 xfs_iroot_realloc(ip, -1, whichfork);
780 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Dave Chinnere55ec4d2018-10-01 08:11:07 +1000781 ASSERT(ifp->if_broot == NULL);
Shan Hai01239d772018-08-10 17:55:55 -0700782 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
783
784 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100785}
786
787/*
788 * Convert a local file to an extents file.
789 * This code is out of bounds for data forks of regular files,
790 * since the file data needs to get logged so things will stay consistent.
791 * (The bmap-level manipulations are ok, though).
792 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000793void
794xfs_bmap_local_to_extents_empty(
795 struct xfs_inode *ip,
796 int whichfork)
797{
798 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
799
Darrick J. Wong60b49842016-10-03 09:11:34 -0700800 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000801 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
802 ASSERT(ifp->if_bytes == 0);
803 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
804
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000805 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000806 ifp->if_flags &= ~XFS_IFINLINE;
807 ifp->if_flags |= XFS_IFEXTENTS;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700808 ifp->if_u1.if_root = NULL;
809 ifp->if_height = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000810 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
811}
812
813
Dave Chinner9e5987a72013-02-25 12:31:26 +1100814STATIC int /* error */
815xfs_bmap_local_to_extents(
816 xfs_trans_t *tp, /* transaction pointer */
817 xfs_inode_t *ip, /* incore inode pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100818 xfs_extlen_t total, /* total blocks needed by transaction */
819 int *logflagsp, /* inode logging flags */
820 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500821 void (*init_fn)(struct xfs_trans *tp,
822 struct xfs_buf *bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100823 struct xfs_inode *ip,
824 struct xfs_ifork *ifp))
825{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000826 int error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100827 int flags; /* logging flags returned */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -0700828 struct xfs_ifork *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000829 xfs_alloc_arg_t args; /* allocation arguments */
830 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700831 struct xfs_bmbt_irec rec;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700832 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100833
834 /*
835 * We don't want to deal with the case of keeping inode data inline yet.
836 * So sending the data fork of a regular inode is invalid.
837 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100838 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100839 ifp = XFS_IFORK_PTR(ip, whichfork);
840 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000841
842 if (!ifp->if_bytes) {
843 xfs_bmap_local_to_extents_empty(ip, whichfork);
844 flags = XFS_ILOG_CORE;
845 goto done;
846 }
847
Dave Chinner9e5987a72013-02-25 12:31:26 +1100848 flags = 0;
849 error = 0;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700850 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000851 memset(&args, 0, sizeof(args));
852 args.tp = tp;
853 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000854 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000855 /*
856 * Allocate a block. We know we need only one, since the
857 * file currently fits in an inode.
858 */
Brian Foster280253d2018-07-11 22:26:29 -0700859 if (tp->t_firstblock == NULLFSBLOCK) {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000860 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
861 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100862 } else {
Brian Foster280253d2018-07-11 22:26:29 -0700863 args.fsbno = tp->t_firstblock;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000864 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100865 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000866 args.total = total;
867 args.minlen = args.maxlen = args.prod = 1;
868 error = xfs_alloc_vextent(&args);
869 if (error)
870 goto done;
871
872 /* Can't fail, the space was reserved. */
873 ASSERT(args.fsbno != NULLFSBLOCK);
874 ASSERT(args.len == 1);
Brian Foster280253d2018-07-11 22:26:29 -0700875 tp->t_firstblock = args.fsbno;
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700876 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000877
Dave Chinnerfe22d552015-01-22 09:30:06 +1100878 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100879 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100880 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100881 * The callout is responsible for logging because the remote format
882 * might differ from the local format and thus we don't know how much to
883 * log here. Note that init_fn must also set the buffer log item type
884 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100885 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000886 init_fn(tp, bp, ip, ifp);
887
Brian Fosterb7cdc662015-10-12 15:40:24 +1100888 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000889 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
890 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100891 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000892
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700893 ifp->if_u1.if_root = NULL;
894 ifp->if_height = 0;
895
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700896 rec.br_startoff = 0;
897 rec.br_startblock = args.fsbno;
898 rec.br_blockcount = 1;
899 rec.br_state = XFS_EXT_NORM;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700900 xfs_iext_first(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -0700901 xfs_iext_insert(ip, &icur, &rec, 0);
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700902
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000903 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
904 ip->i_d.di_nblocks = 1;
905 xfs_trans_mod_dquot_byino(tp, ip,
906 XFS_TRANS_DQ_BCOUNT, 1L);
907 flags |= xfs_ilog_fext(whichfork);
908
Dave Chinner9e5987a72013-02-25 12:31:26 +1100909done:
910 *logflagsp = flags;
911 return error;
912}
913
914/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 * Called from xfs_bmap_add_attrfork to handle btree format files.
916 */
917STATIC int /* error */
918xfs_bmap_add_attrfork_btree(
919 xfs_trans_t *tp, /* transaction pointer */
920 xfs_inode_t *ip, /* incore inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 int *flags) /* inode logging flags */
922{
923 xfs_btree_cur_t *cur; /* btree cursor */
924 int error; /* error return value */
925 xfs_mount_t *mp; /* file system mount struct */
926 int stat; /* newroot status */
927
928 mp = ip->i_mount;
929 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
930 *flags |= XFS_ILOG_DBROOT;
931 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100932 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700933 error = xfs_bmbt_lookup_first(cur, &stat);
934 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000936 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100937 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100938 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 goto error0;
940 if (stat == 0) {
941 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000942 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 cur->bc_private.b.allocated = 0;
945 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
946 }
947 return 0;
948error0:
949 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
950 return error;
951}
952
953/*
954 * Called from xfs_bmap_add_attrfork to handle extents format files.
955 */
956STATIC int /* error */
957xfs_bmap_add_attrfork_extents(
Brian Foster81ba8f32018-07-11 22:26:16 -0700958 struct xfs_trans *tp, /* transaction pointer */
959 struct xfs_inode *ip, /* incore inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 int *flags) /* inode logging flags */
961{
962 xfs_btree_cur_t *cur; /* bmap btree cursor */
963 int error; /* error return value */
964
965 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
966 return 0;
967 cur = NULL;
Brian Foster280253d2018-07-11 22:26:29 -0700968 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
969 XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (cur) {
971 cur->bc_private.b.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -0700972 xfs_btree_del_cursor(cur, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974 return error;
975}
976
977/*
Dave Chinner1e823792013-02-11 15:58:13 +1100978 * Called from xfs_bmap_add_attrfork to handle local format files. Each
979 * different data fork content type needs a different callout to do the
980 * conversion. Some are basic and only require special block initialisation
981 * callouts for the data formating, others (directories) are so specialised they
982 * handle everything themselves.
983 *
984 * XXX (dgc): investigate whether directory conversion can use the generic
985 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500986 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 */
988STATIC int /* error */
989xfs_bmap_add_attrfork_local(
Brian Foster825d75cd2018-07-11 22:26:21 -0700990 struct xfs_trans *tp, /* transaction pointer */
991 struct xfs_inode *ip, /* incore inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 int *flags) /* inode logging flags */
993{
Brian Foster825d75cd2018-07-11 22:26:21 -0700994 struct xfs_da_args dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
997 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +1100998
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100999 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001001 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 dargs.dp = ip;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001003 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 dargs.whichfork = XFS_DATA_FORK;
1005 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001006 return xfs_dir2_sf_to_block(&dargs);
1007 }
1008
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001009 if (S_ISLNK(VFS_I(ip)->i_mode))
Brian Foster280253d2018-07-11 22:26:29 -07001010 return xfs_bmap_local_to_extents(tp, ip, 1, flags,
1011 XFS_DATA_FORK,
Dave Chinner1e823792013-02-11 15:58:13 +11001012 xfs_symlink_local_to_remote);
1013
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001014 /* should only be called for types that support local format data */
1015 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001016 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
Allison Henderson2f3cd802018-10-18 17:21:16 +11001019/* Set an inode attr fork off based on the format */
1020int
1021xfs_bmap_set_attrforkoff(
1022 struct xfs_inode *ip,
1023 int size,
1024 int *version)
1025{
1026 switch (ip->i_d.di_format) {
1027 case XFS_DINODE_FMT_DEV:
1028 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1029 break;
1030 case XFS_DINODE_FMT_LOCAL:
1031 case XFS_DINODE_FMT_EXTENTS:
1032 case XFS_DINODE_FMT_BTREE:
1033 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1034 if (!ip->i_d.di_forkoff)
1035 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1036 else if ((ip->i_mount->m_flags & XFS_MOUNT_ATTR2) && version)
1037 *version = 2;
1038 break;
1039 default:
1040 ASSERT(0);
1041 return -EINVAL;
1042 }
1043
1044 return 0;
1045}
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001048 * Convert inode from non-attributed to attributed.
1049 * Must not be in a transaction, ip must not be locked.
1050 */
1051int /* error code */
1052xfs_bmap_add_attrfork(
1053 xfs_inode_t *ip, /* incore inode pointer */
1054 int size, /* space new attribute needs */
1055 int rsvd) /* xact may use reserved blks */
1056{
Dave Chinner9e5987a72013-02-25 12:31:26 +11001057 xfs_mount_t *mp; /* mount structure */
1058 xfs_trans_t *tp; /* transaction pointer */
1059 int blks; /* space reservation */
1060 int version = 1; /* superblock attr version */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001061 int logflags; /* logging flags */
1062 int error; /* error return value */
1063
1064 ASSERT(XFS_IFORK_Q(ip) == 0);
1065
1066 mp = ip->i_mount;
1067 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001068
Dave Chinner9e5987a72013-02-25 12:31:26 +11001069 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001070
1071 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1072 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1073 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001074 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001075
Dave Chinner9e5987a72013-02-25 12:31:26 +11001076 xfs_ilock(ip, XFS_ILOCK_EXCL);
1077 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1078 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1079 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001080 if (error)
1081 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001082 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001083 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001084 if (ip->i_d.di_anextents != 0) {
1085 error = -EFSCORRUPTED;
1086 goto trans_cancel;
1087 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001088 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1089 /*
1090 * For inodes coming from pre-6.2 filesystems.
1091 */
1092 ASSERT(ip->i_d.di_aformat == 0);
1093 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1094 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001095
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001096 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001097 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Allison Henderson2f3cd802018-10-18 17:21:16 +11001098 error = xfs_bmap_set_attrforkoff(ip, size, &version);
1099 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001100 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001101 ASSERT(ip->i_afp == NULL);
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07001102 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001103 ip->i_afp->if_flags = XFS_IFEXTENTS;
1104 logflags = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001105 switch (ip->i_d.di_format) {
1106 case XFS_DINODE_FMT_LOCAL:
Brian Foster825d75cd2018-07-11 22:26:21 -07001107 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001108 break;
1109 case XFS_DINODE_FMT_EXTENTS:
Brian Foster825d75cd2018-07-11 22:26:21 -07001110 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001111 break;
1112 case XFS_DINODE_FMT_BTREE:
Brian Foster825d75cd2018-07-11 22:26:21 -07001113 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001114 break;
1115 default:
1116 error = 0;
1117 break;
1118 }
1119 if (logflags)
1120 xfs_trans_log_inode(tp, ip, logflags);
1121 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07001122 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001123 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1124 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001125 bool log_sb = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001126
1127 spin_lock(&mp->m_sb_lock);
1128 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1129 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001130 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001131 }
1132 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1133 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001134 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001135 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001136 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001137 if (log_sb)
1138 xfs_log_sb(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001139 }
1140
Christoph Hellwig70393312015-06-04 13:48:08 +10001141 error = xfs_trans_commit(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001142 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001143 return error;
1144
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001145trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001146 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001147 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001148 return error;
1149}
1150
1151/*
1152 * Internal and external extent tree search functions.
1153 */
1154
1155/*
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001156 * Read in extents from a btree-format inode.
Dave Chinner9e5987a72013-02-25 12:31:26 +11001157 */
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001158int
1159xfs_iread_extents(
1160 struct xfs_trans *tp,
1161 struct xfs_inode *ip,
1162 int whichfork)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001163{
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001164 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwige8e0e172017-10-19 11:06:29 -07001165 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001166 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1167 xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
1168 struct xfs_btree_block *block = ifp->if_broot;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001169 struct xfs_iext_cursor icur;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -07001170 struct xfs_bmbt_irec new;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001171 xfs_fsblock_t bno;
1172 struct xfs_buf *bp;
1173 xfs_extnum_t i, j;
1174 int level;
1175 __be64 *pp;
1176 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001177
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001178 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1179
1180 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1181 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1182 return -EFSCORRUPTED;
1183 }
1184
Dave Chinner9e5987a72013-02-25 12:31:26 +11001185 /*
1186 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1187 */
1188 level = be16_to_cpu(block->bb_level);
Brian Foster6958d112019-03-17 15:21:49 -07001189 if (unlikely(level == 0)) {
1190 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1191 return -EFSCORRUPTED;
1192 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001193 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1194 bno = be64_to_cpu(*pp);
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -08001195
Dave Chinner9e5987a72013-02-25 12:31:26 +11001196 /*
1197 * Go down the tree until leaf level is reached, following the first
1198 * pointer (leftmost) at each level.
1199 */
1200 while (level-- > 0) {
Eric Sandeenf5b999c2019-06-12 09:00:00 -07001201 error = xfs_btree_read_bufl(mp, tp, bno, &bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +11001202 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1203 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001204 goto out;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001205 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001206 if (level == 0)
1207 break;
1208 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1209 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001210 XFS_WANT_CORRUPTED_GOTO(mp,
Darrick J. Wong59f6fec2018-01-08 10:51:00 -08001211 xfs_verify_fsbno(mp, bno), out_brelse);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001212 xfs_trans_brelse(tp, bp);
1213 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001214
Dave Chinner9e5987a72013-02-25 12:31:26 +11001215 /*
1216 * Here with bp and block set to the leftmost leaf node in the tree.
1217 */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001218 i = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001219 xfs_iext_first(ifp, &icur);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001220
Dave Chinner9e5987a72013-02-25 12:31:26 +11001221 /*
1222 * Loop over all leaf nodes. Copy information to the extent records.
1223 */
1224 for (;;) {
1225 xfs_bmbt_rec_t *frp;
1226 xfs_fsblock_t nextbno;
1227 xfs_extnum_t num_recs;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001228
1229 num_recs = xfs_btree_get_numrecs(block);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001230 if (unlikely(i + num_recs > nextents)) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001231 xfs_warn(ip->i_mount,
1232 "corrupt dinode %Lu, (btree extents).",
1233 (unsigned long long) ip->i_ino);
Darrick J. Wong90a58f92018-03-23 10:06:52 -07001234 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1235 __func__, block, sizeof(*block),
1236 __this_address);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001237 error = -EFSCORRUPTED;
1238 goto out_brelse;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001239 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001240 /*
1241 * Read-ahead the next leaf block, if any.
1242 */
1243 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1244 if (nextbno != NULLFSBLOCK)
1245 xfs_btree_reada_bufl(mp, nextbno, 1,
1246 &xfs_bmbt_buf_ops);
1247 /*
1248 * Copy records into the extent records.
1249 */
1250 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Christoph Hellwig6bdcf262017-11-03 10:34:46 -07001251 for (j = 0; j < num_recs; j++, frp++, i++) {
Darrick J. Wong30b09842018-03-23 10:06:52 -07001252 xfs_failaddr_t fa;
1253
Christoph Hellwigdac9c9b2017-11-03 10:34:47 -07001254 xfs_bmbt_disk_get_all(frp, &new);
Darrick J. Wong30b09842018-03-23 10:06:52 -07001255 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1256 if (fa) {
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001257 error = -EFSCORRUPTED;
Darrick J. Wong30b09842018-03-23 10:06:52 -07001258 xfs_inode_verifier_error(ip, error,
1259 "xfs_iread_extents(2)",
1260 frp, sizeof(*frp), fa);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001261 goto out_brelse;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001262 }
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001263 xfs_iext_insert(ip, &icur, &new, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001264 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
1265 xfs_iext_next(ifp, &icur);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001266 }
1267 xfs_trans_brelse(tp, bp);
1268 bno = nextbno;
1269 /*
1270 * If we've reached the end, stop.
1271 */
1272 if (bno == NULLFSBLOCK)
1273 break;
Eric Sandeenf5b999c2019-06-12 09:00:00 -07001274 error = xfs_btree_read_bufl(mp, tp, bno, &bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +11001275 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1276 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001277 goto out;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001278 block = XFS_BUF_TO_BLOCK(bp);
1279 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001280
1281 if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) {
1282 error = -EFSCORRUPTED;
1283 goto out;
1284 }
Eric Sandeen5d829302016-11-08 12:59:42 +11001285 ASSERT(i == xfs_iext_count(ifp));
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001286
1287 ifp->if_flags |= XFS_IFEXTENTS;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001288 return 0;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001289
1290out_brelse:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001291 xfs_trans_brelse(tp, bp);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001292out:
1293 xfs_iext_destroy(ifp);
1294 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001295}
1296
Dave Chinner9e5987a72013-02-25 12:31:26 +11001297/*
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001298 * Returns the relative block number of the first unused block(s) in the given
1299 * fork with at least "len" logically contiguous blocks free. This is the
1300 * lowest-address hole if the fork has holes, else the first block past the end
1301 * of fork. Return 0 if the fork is currently local (in-inode).
Dave Chinner9e5987a72013-02-25 12:31:26 +11001302 */
1303int /* error */
1304xfs_bmap_first_unused(
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001305 struct xfs_trans *tp, /* transaction pointer */
1306 struct xfs_inode *ip, /* incore inode */
1307 xfs_extlen_t len, /* size of hole to find */
1308 xfs_fileoff_t *first_unused, /* unused block */
1309 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001310{
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001311 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1312 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001313 struct xfs_iext_cursor icur;
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001314 xfs_fileoff_t lastaddr = 0;
1315 xfs_fileoff_t lowest, max;
1316 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001317
1318 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1319 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1320 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001321
Dave Chinner9e5987a72013-02-25 12:31:26 +11001322 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1323 *first_unused = 0;
1324 return 0;
1325 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001326
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001327 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1328 error = xfs_iread_extents(tp, ip, whichfork);
1329 if (error)
1330 return error;
1331 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001332
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001333 lowest = max = *first_unused;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001334 for_each_xfs_iext(ifp, &icur, &got) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001335 /*
1336 * See if the hole before this extent will work.
1337 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001338 if (got.br_startoff >= lowest + len &&
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001339 got.br_startoff - max >= len)
1340 break;
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001341 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001342 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1343 }
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001344
Dave Chinner9e5987a72013-02-25 12:31:26 +11001345 *first_unused = max;
1346 return 0;
1347}
1348
1349/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001350 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a72013-02-25 12:31:26 +11001351 * last_block (input value) in the file.
1352 * This is not based on i_size, it is based on the extent records.
1353 * Returns 0 for local files, as they do not have extent records.
1354 */
1355int /* error */
1356xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001357 struct xfs_trans *tp, /* transaction pointer */
1358 struct xfs_inode *ip, /* incore inode */
1359 xfs_fileoff_t *last_block, /* last block */
1360 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001361{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001362 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1363 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001364 struct xfs_iext_cursor icur;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001365 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001366
Christoph Hellwig86685f72016-11-24 11:39:38 +11001367 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1368 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001369 *last_block = 0;
1370 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001371 case XFS_DINODE_FMT_BTREE:
1372 case XFS_DINODE_FMT_EXTENTS:
1373 break;
1374 default:
1375 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001376 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001377
1378 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1379 error = xfs_iread_extents(tp, ip, whichfork);
1380 if (error)
1381 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001382 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001383
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001384 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
Christoph Hellwigdc560152017-10-23 16:32:39 -07001385 *last_block = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001386 return 0;
1387}
1388
Dave Chinner68988112013-08-12 20:49:42 +10001389int
Dave Chinner9e5987a72013-02-25 12:31:26 +11001390xfs_bmap_last_extent(
1391 struct xfs_trans *tp,
1392 struct xfs_inode *ip,
1393 int whichfork,
1394 struct xfs_bmbt_irec *rec,
1395 int *is_empty)
1396{
1397 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001398 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001399 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001400
1401 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1402 error = xfs_iread_extents(tp, ip, whichfork);
1403 if (error)
1404 return error;
1405 }
1406
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001407 xfs_iext_last(ifp, &icur);
1408 if (!xfs_iext_get_extent(ifp, &icur, rec))
Dave Chinner9e5987a72013-02-25 12:31:26 +11001409 *is_empty = 1;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001410 else
1411 *is_empty = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001412 return 0;
1413}
1414
1415/*
1416 * Check the last inode extent to determine whether this allocation will result
1417 * in blocks being allocated at the end of the file. When we allocate new data
1418 * blocks at the end of the file which do not start at the previous data block,
1419 * we will try to align the new blocks at stripe unit boundaries.
1420 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001421 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a72013-02-25 12:31:26 +11001422 * at, or past the EOF.
1423 */
1424STATIC int
1425xfs_bmap_isaeof(
1426 struct xfs_bmalloca *bma,
1427 int whichfork)
1428{
1429 struct xfs_bmbt_irec rec;
1430 int is_empty;
1431 int error;
1432
Thomas Meyer749f24f2017-10-09 11:38:54 -07001433 bma->aeof = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001434 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1435 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001436 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001437 return error;
1438
Dave Chinner6e708bc2013-11-22 10:41:16 +11001439 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001440 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001441 return 0;
1442 }
1443
Dave Chinner9e5987a72013-02-25 12:31:26 +11001444 /*
1445 * Check if we are allocation or past the last extent, or at least into
1446 * the last delayed allocated extent.
1447 */
1448 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1449 (bma->offset >= rec.br_startoff &&
1450 isnullstartblock(rec.br_startblock));
1451 return 0;
1452}
1453
1454/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001455 * Returns the file-relative block number of the first block past eof in
1456 * the file. This is not based on i_size, it is based on the extent records.
1457 * Returns 0 for local files, as they do not have extent records.
1458 */
1459int
1460xfs_bmap_last_offset(
Dave Chinner9e5987a72013-02-25 12:31:26 +11001461 struct xfs_inode *ip,
1462 xfs_fileoff_t *last_block,
1463 int whichfork)
1464{
1465 struct xfs_bmbt_irec rec;
1466 int is_empty;
1467 int error;
1468
1469 *last_block = 0;
1470
1471 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1472 return 0;
1473
1474 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1475 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001476 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001477
1478 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1479 if (error || is_empty)
1480 return error;
1481
1482 *last_block = rec.br_startoff + rec.br_blockcount;
1483 return 0;
1484}
1485
1486/*
1487 * Returns whether the selected fork of the inode has exactly one
1488 * block or not. For the data fork we check this matches di_size,
1489 * implying the file's range is 0..bsize-1.
1490 */
1491int /* 1=>1 block, 0=>otherwise */
1492xfs_bmap_one_block(
1493 xfs_inode_t *ip, /* incore inode */
1494 int whichfork) /* data or attr fork */
1495{
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07001496 struct xfs_ifork *ifp; /* inode fork pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001497 int rval; /* return value */
1498 xfs_bmbt_irec_t s; /* internal version of extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001499 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001500
1501#ifndef DEBUG
1502 if (whichfork == XFS_DATA_FORK)
1503 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1504#endif /* !DEBUG */
1505 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1506 return 0;
1507 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1508 return 0;
1509 ifp = XFS_IFORK_PTR(ip, whichfork);
1510 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001511 xfs_iext_first(ifp, &icur);
1512 xfs_iext_get_extent(ifp, &icur, &s);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001513 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1514 if (rval && whichfork == XFS_DATA_FORK)
1515 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1516 return rval;
1517}
1518
1519/*
1520 * Extent tree manipulation functions used during allocation.
1521 */
1522
1523/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001524 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 */
1526STATIC int /* error */
1527xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001528 struct xfs_bmalloca *bma,
1529 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001531 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 int i; /* temp state */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07001534 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 xfs_fileoff_t new_endoff; /* end offset of new entry */
1536 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1537 /* left is 0, right is 1, prev is 2 */
1538 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07001539 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001540 xfs_filblks_t da_new; /* new count del alloc blocks used */
1541 xfs_filblks_t da_old; /* old count del alloc blocks used */
1542 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001544 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001545 xfs_extnum_t *nextents;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001546 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001548 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001549 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001550 ASSERT(whichfork != XFS_ATTR_FORK);
1551 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1552 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001553
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001554 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001555 ASSERT(!bma->cur ||
1556 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001557
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001558 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560#define LEFT r[0]
1561#define RIGHT r[1]
1562#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 /*
1565 * Set up a bunch of variables to make the tests simpler.
1566 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001567 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001569 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 ASSERT(PREV.br_startoff <= new->br_startoff);
1571 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001572
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001573 da_old = startblockval(PREV.br_startblock);
1574 da_new = 0;
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 /*
1577 * Set flags determining what part of the previous delayed allocation
1578 * extent is being replaced by a real allocation.
1579 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001580 if (PREV.br_startoff == new->br_startoff)
1581 state |= BMAP_LEFT_FILLING;
1582 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1583 state |= BMAP_RIGHT_FILLING;
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 /*
1586 * Check and set flags if this segment has a left neighbor.
1587 * Don't set contiguous if the combined extent would be too large.
1588 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001589 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001590 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001591 if (isnullstartblock(LEFT.br_startblock))
1592 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001594
1595 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1596 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1597 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1598 LEFT.br_state == new->br_state &&
1599 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1600 state |= BMAP_LEFT_CONTIG;
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 /*
1603 * Check and set flags if this segment has a right neighbor.
1604 * Don't set contiguous if the combined extent would be too large.
1605 * Also check for all-three-contiguous being too large.
1606 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001607 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001608 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001609 if (isnullstartblock(RIGHT.br_startblock))
1610 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001612
1613 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1614 new_endoff == RIGHT.br_startoff &&
1615 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1616 new->br_state == RIGHT.br_state &&
1617 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1618 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1619 BMAP_RIGHT_FILLING)) !=
1620 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1621 BMAP_RIGHT_FILLING) ||
1622 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1623 <= MAXEXTLEN))
1624 state |= BMAP_RIGHT_CONTIG;
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 error = 0;
1627 /*
1628 * Switch out based on the FILLING and CONTIG state bits.
1629 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001630 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1631 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1632 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1633 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 /*
1635 * Filling in all of a previously delayed allocation extent.
1636 * The left and right neighbors are both contiguous with new.
1637 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001638 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001639
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001640 xfs_iext_remove(bma->ip, &bma->icur, state);
1641 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001642 xfs_iext_prev(ifp, &bma->icur);
1643 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001644 (*nextents)--;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001645
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001646 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1648 else {
1649 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001650 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001651 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001653 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001654 error = xfs_btree_delete(bma->cur, &i);
1655 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001657 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001658 error = xfs_btree_decrement(bma->cur, 0, &i);
1659 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001661 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001662 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001663 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 goto done;
1665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 break;
1667
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001668 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 /*
1670 * Filling in all of a previously delayed allocation extent.
1671 * The left neighbor is contiguous, the right is not.
1672 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001673 old = LEFT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001674 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001675
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001676 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001677 xfs_iext_prev(ifp, &bma->icur);
1678 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001679
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001680 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 rval = XFS_ILOG_DEXT;
1682 else {
1683 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001684 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001685 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001687 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001688 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001689 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 goto done;
1691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 break;
1693
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001694 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /*
1696 * Filling in all of a previously delayed allocation extent.
Dave Chinner9230a0b2018-11-19 22:50:08 -08001697 * The right neighbor is contiguous, the left is not. Take care
1698 * with delay -> unwritten extent allocation here because the
1699 * delalloc record we are overwriting is always written.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001701 PREV.br_startblock = new->br_startblock;
1702 PREV.br_blockcount += RIGHT.br_blockcount;
Dave Chinner9230a0b2018-11-19 22:50:08 -08001703 PREV.br_state = new->br_state;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001704
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001705 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001706 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001707 xfs_iext_prev(ifp, &bma->icur);
1708 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001709
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001710 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 rval = XFS_ILOG_DEXT;
1712 else {
1713 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001714 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001715 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001717 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001718 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001719 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 goto done;
1721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 break;
1723
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001724 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 /*
1726 * Filling in all of a previously delayed allocation extent.
1727 * Neither the left nor right neighbors are contiguous with
1728 * the new one.
1729 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001730 PREV.br_startblock = new->br_startblock;
1731 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001732 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001733
Darrick J. Wong60b49842016-10-03 09:11:34 -07001734 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001735 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1737 else {
1738 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001739 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001740 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001742 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001743 error = xfs_btree_insert(bma->cur, &i);
1744 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001746 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 break;
1749
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001750 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 /*
1752 * Filling in the first part of a previous delayed allocation.
1753 * The left neighbor is contiguous.
1754 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001755 old = LEFT;
1756 temp = PREV.br_blockcount - new->br_blockcount;
1757 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1758 startblockval(PREV.br_startblock));
1759
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001760 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001761
Christoph Hellwigbf999712017-11-03 10:34:38 -07001762 PREV.br_blockcount = temp;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001763 PREV.br_startoff += new->br_blockcount;
1764 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwig0d045542017-11-03 10:34:39 -07001765
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001766 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1767 xfs_iext_prev(ifp, &bma->icur);
1768 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001769
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001770 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 rval = XFS_ILOG_DEXT;
1772 else {
1773 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001774 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001775 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001777 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001778 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001779 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 goto done;
1781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 break;
1783
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001784 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 /*
1786 * Filling in the first part of a previous delayed allocation.
1787 * The left neighbor is not contiguous.
1788 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001789 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001790 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001791 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1793 else {
1794 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001795 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001796 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001798 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001799 error = xfs_btree_insert(bma->cur, &i);
1800 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001802 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001804
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001805 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001806 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001807 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 rval |= tmp_rval;
1809 if (error)
1810 goto done;
1811 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001812
1813 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001814 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001815 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001816 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001817
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001818 PREV.br_startoff = new_endoff;
1819 PREV.br_blockcount = temp;
1820 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001821 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001822 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001823 xfs_iext_prev(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 break;
1825
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001826 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 /*
1828 * Filling in the last part of a previous delayed allocation.
1829 * The right neighbor is contiguous with the new allocation.
1830 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001831 old = RIGHT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001832 RIGHT.br_startoff = new->br_startoff;
1833 RIGHT.br_startblock = new->br_startblock;
1834 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001835
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001836 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 rval = XFS_ILOG_DEXT;
1838 else {
1839 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001840 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001841 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001843 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001844 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001845 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 goto done;
1847 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001848
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001849 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001850 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001851 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001852
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001853 PREV.br_blockcount = temp;
1854 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001855
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001856 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1857 xfs_iext_next(ifp, &bma->icur);
1858 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 break;
1860
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001861 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 /*
1863 * Filling in the last part of a previous delayed allocation.
1864 * The right neighbor is not contiguous.
1865 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001866 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001867 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001868 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1870 else {
1871 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001872 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001873 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001875 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001876 error = xfs_btree_insert(bma->cur, &i);
1877 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001879 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001881
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001882 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001883 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001884 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 rval |= tmp_rval;
1886 if (error)
1887 goto done;
1888 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001889
1890 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001891 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001892 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001893 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001894
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001895 PREV.br_startblock = nullstartblock(da_new);
1896 PREV.br_blockcount = temp;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001897 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001898 xfs_iext_next(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 break;
1900
1901 case 0:
1902 /*
1903 * Filling in the middle part of a previous delayed allocation.
1904 * Contiguity is impossible here.
1905 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001906 *
1907 * We start with a delayed allocation:
1908 *
1909 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1910 * PREV @ idx
1911 *
1912 * and we are allocating:
1913 * +rrrrrrrrrrrrrrrrr+
1914 * new
1915 *
1916 * and we set it up for insertion as:
1917 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1918 * new
1919 * PREV @ idx LEFT RIGHT
1920 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001922 old = PREV;
1923
1924 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001925 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001926
1927 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001928 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001929 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001930 RIGHT.br_blockcount =
1931 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1932 RIGHT.br_startblock =
1933 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1934 RIGHT.br_blockcount));
1935
1936 /* truncate PREV */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001937 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1938 PREV.br_startblock =
1939 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1940 PREV.br_blockcount));
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001941 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001942
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001943 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001944 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1945 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001946 (*nextents)++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001947
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001948 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1950 else {
1951 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001952 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001953 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001955 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001956 error = xfs_btree_insert(bma->cur, &i);
1957 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001959 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001961
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001962 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001963 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07001964 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 rval |= tmp_rval;
1966 if (error)
1967 goto done;
1968 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001969
1970 da_new = startblockval(PREV.br_startblock) +
1971 startblockval(RIGHT.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 break;
1973
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001974 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1975 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1976 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1977 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1978 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1979 case BMAP_LEFT_CONTIG:
1980 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 /*
1982 * These cases are all impossible.
1983 */
1984 ASSERT(0);
1985 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001986
Darrick J. Wong95eb3082018-05-09 10:02:32 -07001987 /* add reverse mapping unless caller opted out */
1988 if (!(bma->flags & XFS_BMAPI_NORMAP)) {
Brian Foster0f37d172018-08-01 07:20:34 -07001989 error = xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
Darrick J. Wong95eb3082018-05-09 10:02:32 -07001990 if (error)
1991 goto done;
1992 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10001993
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001994 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001995 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001996 int tmp_logflags; /* partial log flag return val */
1997
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001998 ASSERT(bma->cur == NULL);
1999 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster280253d2018-07-11 22:26:29 -07002000 &bma->cur, da_old > 0, &tmp_logflags,
2001 whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002002 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002003 if (error)
2004 goto done;
2005 }
2006
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07002007 if (da_new != da_old)
2008 xfs_mod_delalloc(mp, (int64_t)da_new - da_old);
2009
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002010 if (bma->cur) {
2011 da_new += bma->cur->bc_private.b.allocated;
2012 bma->cur->bc_private.b.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002013 }
2014
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002015 /* adjust for changes in reserved delayed indirect blocks */
2016 if (da_new != da_old) {
2017 ASSERT(state == 0 || da_new < da_old);
2018 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2019 false);
2020 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002021
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002022 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002024 if (whichfork != XFS_COW_FORK)
2025 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 return error;
2027#undef LEFT
2028#undef RIGHT
2029#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
2032/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002033 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 */
Christoph Hellwig26b91c72019-02-18 09:38:48 -08002035int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002037 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002039 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002040 struct xfs_iext_cursor *icur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002042 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002043 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 int i; /* temp state */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07002048 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2051 /* left is 0, right is 1, prev is 2 */
2052 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002053 int state = xfs_bmap_fork_to_state(whichfork);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002054 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002055 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002057 *logflagsp = 0;
2058
2059 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002060 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002061
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002062 ASSERT(!isnullstartblock(new->br_startblock));
2063
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002064 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066#define LEFT r[0]
2067#define RIGHT r[1]
2068#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 /*
2071 * Set up a bunch of variables to make the tests simpler.
2072 */
2073 error = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002074 xfs_iext_get_extent(ifp, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002075 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 new_endoff = new->br_startoff + new->br_blockcount;
2077 ASSERT(PREV.br_startoff <= new->br_startoff);
2078 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 /*
2081 * Set flags determining what part of the previous oldext allocation
2082 * extent is being replaced by a newext allocation.
2083 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002084 if (PREV.br_startoff == new->br_startoff)
2085 state |= BMAP_LEFT_FILLING;
2086 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2087 state |= BMAP_RIGHT_FILLING;
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 /*
2090 * Check and set flags if this segment has a left neighbor.
2091 * Don't set contiguous if the combined extent would be too large.
2092 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002093 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002094 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002095 if (isnullstartblock(LEFT.br_startblock))
2096 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002098
2099 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2100 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2101 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002102 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002103 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2104 state |= BMAP_LEFT_CONTIG;
2105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 /*
2107 * Check and set flags if this segment has a right neighbor.
2108 * Don't set contiguous if the combined extent would be too large.
2109 * Also check for all-three-contiguous being too large.
2110 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002111 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002112 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002113 if (isnullstartblock(RIGHT.br_startblock))
2114 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002116
2117 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2118 new_endoff == RIGHT.br_startoff &&
2119 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002120 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002121 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2122 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2123 BMAP_RIGHT_FILLING)) !=
2124 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2125 BMAP_RIGHT_FILLING) ||
2126 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2127 <= MAXEXTLEN))
2128 state |= BMAP_RIGHT_CONTIG;
2129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 /*
2131 * Switch out based on the FILLING and CONTIG state bits.
2132 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002133 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2134 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2135 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2136 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 /*
2138 * Setting all of a previous oldext extent to newext.
2139 * The left and right neighbors are both contiguous with new.
2140 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002141 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002142
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002143 xfs_iext_remove(ip, icur, state);
2144 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002145 xfs_iext_prev(ifp, icur);
2146 xfs_iext_update_extent(ip, state, icur, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002147 XFS_IFORK_NEXT_SET(ip, whichfork,
2148 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (cur == NULL)
2150 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2151 else {
2152 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002153 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2154 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002156 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002157 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002159 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002160 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002162 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002163 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002165 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002166 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002168 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002169 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002170 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 goto done;
2172 }
2173 break;
2174
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002175 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 /*
2177 * Setting all of a previous oldext extent to newext.
2178 * The left neighbor is contiguous, the right is not.
2179 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002180 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002181
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002182 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002183 xfs_iext_prev(ifp, icur);
2184 xfs_iext_update_extent(ip, state, icur, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002185 XFS_IFORK_NEXT_SET(ip, whichfork,
2186 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 if (cur == NULL)
2188 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2189 else {
2190 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002191 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2192 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002194 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002195 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002197 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002198 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002200 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002201 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002202 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 goto done;
2204 }
2205 break;
2206
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002207 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 /*
2209 * Setting all of a previous oldext extent to newext.
2210 * The right neighbor is contiguous, the left is not.
2211 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002212 PREV.br_blockcount += RIGHT.br_blockcount;
2213 PREV.br_state = new->br_state;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002214
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002215 xfs_iext_next(ifp, icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002216 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002217 xfs_iext_prev(ifp, icur);
2218 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002219
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002220 XFS_IFORK_NEXT_SET(ip, whichfork,
2221 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 if (cur == NULL)
2223 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2224 else {
2225 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002226 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2227 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002229 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002230 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002232 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002233 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002235 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002236 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002237 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 goto done;
2239 }
2240 break;
2241
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002242 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 /*
2244 * Setting all of a previous oldext extent to newext.
2245 * Neither the left nor right neighbors are contiguous with
2246 * the new one.
2247 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002248 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002249 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if (cur == NULL)
2252 rval = XFS_ILOG_DEXT;
2253 else {
2254 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002255 error = xfs_bmbt_lookup_eq(cur, new, &i);
2256 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002258 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002259 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002260 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 goto done;
2262 }
2263 break;
2264
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002265 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 /*
2267 * Setting the first part of a previous oldext extent to newext.
2268 * The left neighbor is contiguous.
2269 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002270 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002271
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002272 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002273 PREV.br_startoff += new->br_blockcount;
2274 PREV.br_startblock += new->br_blockcount;
2275 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002276
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002277 xfs_iext_update_extent(ip, state, icur, &PREV);
2278 xfs_iext_prev(ifp, icur);
2279 xfs_iext_update_extent(ip, state, icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (cur == NULL)
2282 rval = XFS_ILOG_DEXT;
2283 else {
2284 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002285 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002286 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002288 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002289 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002290 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002292 error = xfs_btree_decrement(cur, 0, &i);
2293 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002295 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002296 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 goto done;
2298 }
2299 break;
2300
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002301 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 /*
2303 * Setting the first part of a previous oldext extent to newext.
2304 * The left neighbor is not contiguous.
2305 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002306 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002307 PREV.br_startoff += new->br_blockcount;
2308 PREV.br_startblock += new->br_blockcount;
2309 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002310
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002311 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002312 xfs_iext_insert(ip, icur, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002313 XFS_IFORK_NEXT_SET(ip, whichfork,
2314 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 if (cur == NULL)
2316 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2317 else {
2318 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002319 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002320 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002322 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002323 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002324 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 goto done;
2326 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002327 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002329 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 }
2331 break;
2332
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002333 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 /*
2335 * Setting the last part of a previous oldext extent to newext.
2336 * The right neighbor is contiguous with the new allocation.
2337 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002338 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002339 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002340
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002341 RIGHT.br_startoff = new->br_startoff;
2342 RIGHT.br_startblock = new->br_startblock;
2343 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002344
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002345 xfs_iext_update_extent(ip, state, icur, &PREV);
2346 xfs_iext_next(ifp, icur);
2347 xfs_iext_update_extent(ip, state, icur, &RIGHT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (cur == NULL)
2350 rval = XFS_ILOG_DEXT;
2351 else {
2352 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002353 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002354 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002356 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002357 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002358 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002360 error = xfs_btree_increment(cur, 0, &i);
2361 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002363 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002364 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 goto done;
2366 }
2367 break;
2368
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002369 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 /*
2371 * Setting the last part of a previous oldext extent to newext.
2372 * The right neighbor is not contiguous.
2373 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002374 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002375 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002376
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002377 xfs_iext_update_extent(ip, state, icur, &PREV);
2378 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002379 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002380
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002381 XFS_IFORK_NEXT_SET(ip, whichfork,
2382 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 if (cur == NULL)
2384 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2385 else {
2386 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002387 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002388 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002390 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002391 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002392 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002394 error = xfs_bmbt_lookup_eq(cur, new, &i);
2395 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002397 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002398 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002400 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
2402 break;
2403
2404 case 0:
2405 /*
2406 * Setting the middle part of a previous oldext extent to
2407 * newext. Contiguity is impossible here.
2408 * One extent becomes three extents.
2409 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002410 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002411 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002412
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 r[0] = *new;
2414 r[1].br_startoff = new_endoff;
2415 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002416 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002418 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002419
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002420 xfs_iext_update_extent(ip, state, icur, &PREV);
2421 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002422 xfs_iext_insert(ip, icur, &r[1], state);
2423 xfs_iext_insert(ip, icur, &r[0], state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002424
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002425 XFS_IFORK_NEXT_SET(ip, whichfork,
2426 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (cur == NULL)
2428 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2429 else {
2430 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002431 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002432 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002434 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002436 error = xfs_bmbt_update(cur, &r[1]);
2437 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 goto done;
2439 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002441 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002443 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002444 /*
2445 * Reset the cursor to the position of the new extent
2446 * we are about to insert as we can't trust it after
2447 * the previous insert.
2448 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002449 error = xfs_bmbt_lookup_eq(cur, new, &i);
2450 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002452 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002454 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002456 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 }
2458 break;
2459
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002460 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2461 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2462 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2463 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2464 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2465 case BMAP_LEFT_CONTIG:
2466 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 /*
2468 * These cases are all impossible.
2469 */
2470 ASSERT(0);
2471 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002472
Darrick J. Wong9c194642016-08-03 12:16:05 +10002473 /* update reverse mappings */
Brian Foster0f37d172018-08-01 07:20:34 -07002474 error = xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002475 if (error)
2476 goto done;
2477
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002478 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002479 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002480 int tmp_logflags; /* partial log flag return val */
2481
2482 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07002483 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2484 &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002485 *logflagsp |= tmp_logflags;
2486 if (error)
2487 goto done;
2488 }
2489
2490 /* clear out the allocated field, done with it now in any case. */
2491 if (cur) {
2492 cur->bc_private.b.allocated = 0;
2493 *curp = cur;
2494 }
2495
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002496 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002498 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 return error;
2500#undef LEFT
2501#undef RIGHT
2502#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
2505/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002506 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002508STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509xfs_bmap_add_extent_hole_delay(
2510 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002511 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002512 struct xfs_iext_cursor *icur,
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002513 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07002515 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2517 xfs_filblks_t newlen=0; /* new indirect size */
2518 xfs_filblks_t oldlen=0; /* old indirect size */
2519 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002520 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002521 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002523 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002524 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 /*
2527 * Check and set flags if this segment has a left neighbor
2528 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002529 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002530 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002531 if (isnullstartblock(left.br_startblock))
2532 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 /*
2536 * Check and set flags if the current (right) segment exists.
2537 * If it doesn't exist, we're converting the hole at end-of-file.
2538 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002539 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002540 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002541 if (isnullstartblock(right.br_startblock))
2542 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 /*
2546 * Set contiguity flags on the left and right neighbors.
2547 * Don't let extents get too large, even if the pieces are contiguous.
2548 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002549 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2550 left.br_startoff + left.br_blockcount == new->br_startoff &&
2551 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2552 state |= BMAP_LEFT_CONTIG;
2553
2554 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2555 new->br_startoff + new->br_blockcount == right.br_startoff &&
2556 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2557 (!(state & BMAP_LEFT_CONTIG) ||
2558 (left.br_blockcount + new->br_blockcount +
2559 right.br_blockcount <= MAXEXTLEN)))
2560 state |= BMAP_RIGHT_CONTIG;
2561
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 /*
2563 * Switch out based on the contiguity flags.
2564 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002565 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2566 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 /*
2568 * New allocation is contiguous with delayed allocations
2569 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002570 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 */
2572 temp = left.br_blockcount + new->br_blockcount +
2573 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002574
Eric Sandeen9d87c312009-01-14 23:22:07 -06002575 oldlen = startblockval(left.br_startblock) +
2576 startblockval(new->br_startblock) +
2577 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002578 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2579 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002580 left.br_startblock = nullstartblock(newlen);
2581 left.br_blockcount = temp;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002582
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002583 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002584 xfs_iext_prev(ifp, icur);
2585 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 break;
2587
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002588 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 /*
2590 * New allocation is contiguous with a delayed allocation
2591 * on the left.
2592 * Merge the new allocation with the left neighbor.
2593 */
2594 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002595
Eric Sandeen9d87c312009-01-14 23:22:07 -06002596 oldlen = startblockval(left.br_startblock) +
2597 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002598 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2599 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002600 left.br_blockcount = temp;
2601 left.br_startblock = nullstartblock(newlen);
Christoph Hellwig41d196f2017-11-03 10:34:39 -07002602
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002603 xfs_iext_prev(ifp, icur);
2604 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 break;
2606
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002607 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 /*
2609 * New allocation is contiguous with a delayed allocation
2610 * on the right.
2611 * Merge the new allocation with the right neighbor.
2612 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002614 oldlen = startblockval(new->br_startblock) +
2615 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002616 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2617 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002618 right.br_startoff = new->br_startoff;
2619 right.br_startblock = nullstartblock(newlen);
2620 right.br_blockcount = temp;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002621 xfs_iext_update_extent(ip, state, icur, &right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 break;
2623
2624 case 0:
2625 /*
2626 * New allocation is not contiguous with another
2627 * delayed allocation.
2628 * Insert a new entry.
2629 */
2630 oldlen = newlen = 0;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002631 xfs_iext_insert(ip, icur, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 break;
2633 }
2634 if (oldlen != newlen) {
2635 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002636 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2637 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 /*
2639 * Nothing to do for disk quota accounting here.
2640 */
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07002641 xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643}
2644
2645/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002646 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 */
2648STATIC int /* error */
2649xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002650 struct xfs_trans *tp,
2651 struct xfs_inode *ip,
2652 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002653 struct xfs_iext_cursor *icur,
Christoph Hellwig6d045582017-04-11 16:45:54 -07002654 struct xfs_btree_cur **curp,
2655 struct xfs_bmbt_irec *new,
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002656 int *logflagsp,
2657 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002659 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2660 struct xfs_mount *mp = ip->i_mount;
2661 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2665 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002666 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002667 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002668 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002670 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002671 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002672
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002673 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 /*
2676 * Check and set flags if this segment has a left neighbor.
2677 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002678 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002679 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002680 if (isnullstartblock(left.br_startblock))
2681 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002683
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 /*
2685 * Check and set flags if this segment has a current value.
2686 * Not true if we're inserting into the "hole" at eof.
2687 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002688 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002689 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002690 if (isnullstartblock(right.br_startblock))
2691 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002693
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 /*
2695 * We're inserting a real allocation between "left" and "right".
2696 * Set the contiguity flags. Don't let extents get too large.
2697 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002698 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2699 left.br_startoff + left.br_blockcount == new->br_startoff &&
2700 left.br_startblock + left.br_blockcount == new->br_startblock &&
2701 left.br_state == new->br_state &&
2702 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2703 state |= BMAP_LEFT_CONTIG;
2704
2705 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2706 new->br_startoff + new->br_blockcount == right.br_startoff &&
2707 new->br_startblock + new->br_blockcount == right.br_startblock &&
2708 new->br_state == right.br_state &&
2709 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2710 (!(state & BMAP_LEFT_CONTIG) ||
2711 left.br_blockcount + new->br_blockcount +
2712 right.br_blockcount <= MAXEXTLEN))
2713 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002715 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 /*
2717 * Select which case we're in here, and implement it.
2718 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002719 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2720 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 /*
2722 * New allocation is contiguous with real allocations on the
2723 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002724 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002726 left.br_blockcount += new->br_blockcount + right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002727
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002728 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002729 xfs_iext_prev(ifp, icur);
2730 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002731
Christoph Hellwig6d045582017-04-11 16:45:54 -07002732 XFS_IFORK_NEXT_SET(ip, whichfork,
2733 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2734 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002735 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002736 } else {
2737 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002738 error = xfs_bmbt_lookup_eq(cur, &right, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002739 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002740 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002741 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002742 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002743 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002744 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002745 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002746 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002747 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002748 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002749 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002750 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002751 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002752 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002754 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002756 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 /*
2758 * New allocation is contiguous with a real allocation
2759 * on the left.
2760 * Merge the new allocation with the left neighbor.
2761 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002762 old = left;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002763 left.br_blockcount += new->br_blockcount;
Christoph Hellwig1d2e0082017-11-03 10:34:40 -07002764
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002765 xfs_iext_prev(ifp, icur);
2766 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002767
Christoph Hellwig6d045582017-04-11 16:45:54 -07002768 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002769 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002770 } else {
2771 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002772 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002773 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002774 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002775 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002776 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002777 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002778 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002780 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002782 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 /*
2784 * New allocation is contiguous with a real allocation
2785 * on the right.
2786 * Merge the new allocation with the right neighbor.
2787 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002788 old = right;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07002789
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002790 right.br_startoff = new->br_startoff;
2791 right.br_startblock = new->br_startblock;
2792 right.br_blockcount += new->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002793 xfs_iext_update_extent(ip, state, icur, &right);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002794
Christoph Hellwig6d045582017-04-11 16:45:54 -07002795 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002796 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002797 } else {
2798 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002799 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002800 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002801 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002802 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002803 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002804 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002805 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002807 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
2809 case 0:
2810 /*
2811 * New allocation is not contiguous with another
2812 * real allocation.
2813 * Insert a new entry.
2814 */
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002815 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002816 XFS_IFORK_NEXT_SET(ip, whichfork,
2817 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2818 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002819 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002820 } else {
2821 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002822 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002823 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002824 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002825 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002826 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002827 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002828 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002829 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002831 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002833
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002834 /* add reverse mapping unless caller opted out */
2835 if (!(flags & XFS_BMAPI_NORMAP)) {
Brian Foster0f37d172018-08-01 07:20:34 -07002836 error = xfs_rmap_map_extent(tp, ip, whichfork, new);
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002837 if (error)
2838 goto done;
2839 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10002840
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002841 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002842 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002843 int tmp_logflags; /* partial log flag return val */
2844
Christoph Hellwig6d045582017-04-11 16:45:54 -07002845 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07002846 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2847 &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002848 *logflagsp |= tmp_logflags;
2849 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002850 if (error)
2851 goto done;
2852 }
2853
2854 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002855 if (cur)
2856 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002857
Christoph Hellwig6d045582017-04-11 16:45:54 -07002858 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002859done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002860 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002861 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862}
2863
Nathan Scottdd9f4382006-01-11 15:28:28 +11002864/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11002865 * Functions used in the extent read, allocate and remove paths
2866 */
2867
2868/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002869 * Adjust the size of the new extent based on di_extsize and rt extsize.
2870 */
Dave Chinner68988112013-08-12 20:49:42 +10002871int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002872xfs_bmap_extsize_align(
2873 xfs_mount_t *mp,
2874 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2875 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2876 xfs_extlen_t extsz, /* align to this extent size */
2877 int rt, /* is this a realtime inode? */
2878 int eof, /* is extent at end-of-file? */
2879 int delay, /* creating delalloc extent? */
2880 int convert, /* overwriting unwritten extent? */
2881 xfs_fileoff_t *offp, /* in/out: aligned offset */
2882 xfs_extlen_t *lenp) /* in/out: aligned length */
2883{
2884 xfs_fileoff_t orig_off; /* original offset */
2885 xfs_extlen_t orig_alen; /* original length */
2886 xfs_fileoff_t orig_end; /* original off+len */
2887 xfs_fileoff_t nexto; /* next file offset */
2888 xfs_fileoff_t prevo; /* previous file offset */
2889 xfs_fileoff_t align_off; /* temp for offset */
2890 xfs_extlen_t align_alen; /* temp for length */
2891 xfs_extlen_t temp; /* temp for calculations */
2892
2893 if (convert)
2894 return 0;
2895
2896 orig_off = align_off = *offp;
2897 orig_alen = align_alen = *lenp;
2898 orig_end = orig_off + orig_alen;
2899
2900 /*
2901 * If this request overlaps an existing extent, then don't
2902 * attempt to perform any additional alignment.
2903 */
2904 if (!delay && !eof &&
2905 (orig_off >= gotp->br_startoff) &&
2906 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2907 return 0;
2908 }
2909
2910 /*
2911 * If the file offset is unaligned vs. the extent size
2912 * we need to align it. This will be possible unless
2913 * the file was previously written with a kernel that didn't
2914 * perform this alignment, or if a truncate shot us in the
2915 * foot.
2916 */
Dave Chinner0703a8e2018-06-08 09:54:22 -07002917 div_u64_rem(orig_off, extsz, &temp);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002918 if (temp) {
2919 align_alen += temp;
2920 align_off -= temp;
2921 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10002922
2923 /* Same adjustment for the end of the requested area. */
2924 temp = (align_alen % extsz);
2925 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11002926 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10002927
2928 /*
2929 * For large extent hint sizes, the aligned extent might be larger than
2930 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2931 * the length back under MAXEXTLEN. The outer allocation loops handle
2932 * short allocation just fine, so it is safe to do this. We only want to
2933 * do it when we are forced to, though, because it means more allocation
2934 * operations are required.
2935 */
2936 while (align_alen > MAXEXTLEN)
2937 align_alen -= extsz;
2938 ASSERT(align_alen <= MAXEXTLEN);
2939
Nathan Scottdd9f4382006-01-11 15:28:28 +11002940 /*
2941 * If the previous block overlaps with this proposed allocation
2942 * then move the start forward without adjusting the length.
2943 */
2944 if (prevp->br_startoff != NULLFILEOFF) {
2945 if (prevp->br_startblock == HOLESTARTBLOCK)
2946 prevo = prevp->br_startoff;
2947 else
2948 prevo = prevp->br_startoff + prevp->br_blockcount;
2949 } else
2950 prevo = 0;
2951 if (align_off != orig_off && align_off < prevo)
2952 align_off = prevo;
2953 /*
2954 * If the next block overlaps with this proposed allocation
2955 * then move the start back without adjusting the length,
2956 * but not before offset 0.
2957 * This may of course make the start overlap previous block,
2958 * and if we hit the offset 0 limit then the next block
2959 * can still overlap too.
2960 */
2961 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2962 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2963 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2964 nexto = gotp->br_startoff + gotp->br_blockcount;
2965 else
2966 nexto = gotp->br_startoff;
2967 } else
2968 nexto = NULLFILEOFF;
2969 if (!eof &&
2970 align_off + align_alen != orig_end &&
2971 align_off + align_alen > nexto)
2972 align_off = nexto > align_alen ? nexto - align_alen : 0;
2973 /*
2974 * If we're now overlapping the next or previous extent that
2975 * means we can't fit an extsz piece in this hole. Just move
2976 * the start forward to the first valid spot and set
2977 * the length so we hit the end.
2978 */
2979 if (align_off != orig_off && align_off < prevo)
2980 align_off = prevo;
2981 if (align_off + align_alen != orig_end &&
2982 align_off + align_alen > nexto &&
2983 nexto != NULLFILEOFF) {
2984 ASSERT(nexto > prevo);
2985 align_alen = nexto - align_off;
2986 }
2987
2988 /*
2989 * If realtime, and the result isn't a multiple of the realtime
2990 * extent size we need to remove blocks until it is.
2991 */
2992 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2993 /*
2994 * We're not covering the original request, or
2995 * we won't be able to once we fix the length.
2996 */
2997 if (orig_off < align_off ||
2998 orig_end > align_off + align_alen ||
2999 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003000 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003001 /*
3002 * Try to fix it by moving the start up.
3003 */
3004 if (align_off + temp <= orig_off) {
3005 align_alen -= temp;
3006 align_off += temp;
3007 }
3008 /*
3009 * Try to fix it by moving the end in.
3010 */
3011 else if (align_off + align_alen - temp >= orig_end)
3012 align_alen -= temp;
3013 /*
3014 * Set the start to the minimum then trim the length.
3015 */
3016 else {
3017 align_alen -= orig_off - align_off;
3018 align_off = orig_off;
3019 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3020 }
3021 /*
3022 * Result doesn't cover the request, fail it.
3023 */
3024 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003025 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003026 } else {
3027 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003028 /* see MAXEXTLEN handling above */
3029 ASSERT(orig_end <= align_off + align_alen ||
3030 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003031 }
3032
3033#ifdef DEBUG
3034 if (!eof && gotp->br_startoff != NULLFILEOFF)
3035 ASSERT(align_off + align_alen <= gotp->br_startoff);
3036 if (prevp->br_startoff != NULLFILEOFF)
3037 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3038#endif
3039
3040 *lenp = align_alen;
3041 *offp = align_off;
3042 return 0;
3043}
3044
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045#define XFS_ALLOC_GAP_UNITS 4
3046
Dave Chinner68988112013-08-12 20:49:42 +10003047void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003048xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003049 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
3051 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3053 xfs_mount_t *mp; /* mount point structure */
3054 int nullfb; /* true if ap->firstblock isn't set */
3055 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
3057#define ISVALID(x,y) \
3058 (rt ? \
3059 (x) < mp->m_sb.sb_rblocks : \
3060 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3061 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3062 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 mp = ap->ip->i_mount;
Brian Foster94c07b42018-07-11 22:26:28 -07003065 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003066 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3067 xfs_alloc_is_userdata(ap->datatype);
Brian Foster94c07b42018-07-11 22:26:28 -07003068 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3069 ap->tp->t_firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 /*
3071 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003072 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003074 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3075 !isnullstartblock(ap->prev.br_startblock) &&
3076 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3077 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003078 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 /*
3080 * Adjust for the gap between prevp and us.
3081 */
Dave Chinner3a756672011-09-18 20:40:58 +00003082 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003083 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003085 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3086 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 }
3088 /*
3089 * If not at eof, then compare the two neighbor blocks.
3090 * Figure out whether either one gives us a good starting point,
3091 * and pick the better one.
3092 */
3093 else if (!ap->eof) {
3094 xfs_fsblock_t gotbno; /* right side block number */
3095 xfs_fsblock_t gotdiff=0; /* right side difference */
3096 xfs_fsblock_t prevbno; /* left side block number */
3097 xfs_fsblock_t prevdiff=0; /* left side difference */
3098
3099 /*
3100 * If there's a previous (left) block, select a requested
3101 * start block based on it.
3102 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003103 if (ap->prev.br_startoff != NULLFILEOFF &&
3104 !isnullstartblock(ap->prev.br_startblock) &&
3105 (prevbno = ap->prev.br_startblock +
3106 ap->prev.br_blockcount) &&
3107 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 /*
3109 * Calculate gap to end of previous block.
3110 */
Dave Chinner3a756672011-09-18 20:40:58 +00003111 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003112 (ap->prev.br_startoff +
3113 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 /*
3115 * Figure the startblock based on the previous block's
3116 * end and the gap size.
3117 * Heuristic!
3118 * If the gap is large relative to the piece we're
3119 * allocating, or using it gives us an invalid block
3120 * number, then just use the end of the previous block.
3121 */
Dave Chinner3a756672011-09-18 20:40:58 +00003122 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003124 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 prevbno += adjust;
3126 else
3127 prevdiff += adjust;
3128 /*
3129 * If the firstblock forbids it, can't use it,
3130 * must use default.
3131 */
3132 if (!rt && !nullfb &&
3133 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3134 prevbno = NULLFSBLOCK;
3135 }
3136 /*
3137 * No previous block or can't follow it, just default.
3138 */
3139 else
3140 prevbno = NULLFSBLOCK;
3141 /*
3142 * If there's a following (right) block, select a requested
3143 * start block based on it.
3144 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003145 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 /*
3147 * Calculate gap to start of next block.
3148 */
Dave Chinner3a756672011-09-18 20:40:58 +00003149 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 /*
3151 * Figure the startblock based on the next block's
3152 * start and the gap size.
3153 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003154 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 /*
3156 * Heuristic!
3157 * If the gap is large relative to the piece we're
3158 * allocating, or using it gives us an invalid block
3159 * number, then just use the start of the next block
3160 * offset by our length.
3161 */
Dave Chinner3a756672011-09-18 20:40:58 +00003162 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 ISVALID(gotbno - gotdiff, gotbno))
3164 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003165 else if (ISVALID(gotbno - ap->length, gotbno)) {
3166 gotbno -= ap->length;
3167 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 } else
3169 gotdiff += adjust;
3170 /*
3171 * If the firstblock forbids it, can't use it,
3172 * must use default.
3173 */
3174 if (!rt && !nullfb &&
3175 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3176 gotbno = NULLFSBLOCK;
3177 }
3178 /*
3179 * No next block, just default.
3180 */
3181 else
3182 gotbno = NULLFSBLOCK;
3183 /*
3184 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003185 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 */
3187 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003188 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003190 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003192 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003194#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003195}
3196
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003197static int
3198xfs_bmap_longest_free_extent(
3199 struct xfs_trans *tp,
3200 xfs_agnumber_t ag,
3201 xfs_extlen_t *blen,
3202 int *notinit)
3203{
3204 struct xfs_mount *mp = tp->t_mountp;
3205 struct xfs_perag *pag;
3206 xfs_extlen_t longest;
3207 int error = 0;
3208
3209 pag = xfs_perag_get(mp, ag);
3210 if (!pag->pagf_init) {
3211 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3212 if (error)
3213 goto out;
3214
3215 if (!pag->pagf_init) {
3216 *notinit = 1;
3217 goto out;
3218 }
3219 }
3220
Eric Sandeena1f69412018-04-06 10:09:42 -07003221 longest = xfs_alloc_longest_free_extent(pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003222 xfs_alloc_min_freelist(mp, pag),
3223 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003224 if (*blen < longest)
3225 *blen = longest;
3226
3227out:
3228 xfs_perag_put(pag);
3229 return error;
3230}
3231
3232static void
3233xfs_bmap_select_minlen(
3234 struct xfs_bmalloca *ap,
3235 struct xfs_alloc_arg *args,
3236 xfs_extlen_t *blen,
3237 int notinit)
3238{
3239 if (notinit || *blen < ap->minlen) {
3240 /*
3241 * Since we did a BUF_TRYLOCK above, it is possible that
3242 * there is space for this request.
3243 */
3244 args->minlen = ap->minlen;
3245 } else if (*blen < args->maxlen) {
3246 /*
3247 * If the best seen length is less than the request length,
3248 * use the best as the minimum.
3249 */
3250 args->minlen = *blen;
3251 } else {
3252 /*
3253 * Otherwise we've seen an extent as big as maxlen, use that
3254 * as the minimum.
3255 */
3256 args->minlen = args->maxlen;
3257 }
3258}
3259
Nathan Scotta365bdd2006-03-14 13:34:16 +11003260STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003261xfs_bmap_btalloc_nullfb(
3262 struct xfs_bmalloca *ap,
3263 struct xfs_alloc_arg *args,
3264 xfs_extlen_t *blen)
3265{
3266 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003267 xfs_agnumber_t ag, startag;
3268 int notinit = 0;
3269 int error;
3270
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003271 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003272 args->total = ap->total;
3273
Christoph Hellwigc467c042010-02-15 23:34:42 +00003274 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3275 if (startag == NULLAGNUMBER)
3276 startag = ag = 0;
3277
Dave Chinner14b064c2011-01-27 12:16:28 +11003278 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003279 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3280 &notinit);
3281 if (error)
3282 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003283
Christoph Hellwigc467c042010-02-15 23:34:42 +00003284 if (++ag == mp->m_sb.sb_agcount)
3285 ag = 0;
3286 if (ag == startag)
3287 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003288 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003289
3290 xfs_bmap_select_minlen(ap, args, blen, notinit);
3291 return 0;
3292}
3293
3294STATIC int
3295xfs_bmap_btalloc_filestreams(
3296 struct xfs_bmalloca *ap,
3297 struct xfs_alloc_arg *args,
3298 xfs_extlen_t *blen)
3299{
3300 struct xfs_mount *mp = ap->ip->i_mount;
3301 xfs_agnumber_t ag;
3302 int notinit = 0;
3303 int error;
3304
3305 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3306 args->total = ap->total;
3307
3308 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3309 if (ag == NULLAGNUMBER)
3310 ag = 0;
3311
3312 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3313 if (error)
3314 return error;
3315
3316 if (*blen < args->maxlen) {
3317 error = xfs_filestream_new_ag(ap, &ag);
3318 if (error)
3319 return error;
3320
3321 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3322 &notinit);
3323 if (error)
3324 return error;
3325
3326 }
3327
3328 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003329
3330 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003331 * Set the failure fallback case to look in the selected AG as stream
3332 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003333 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003334 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003335 return 0;
3336}
3337
Darrick J. Wong751f3762018-01-25 13:58:13 -08003338/* Update all inode and quota accounting for the allocation we just did. */
3339static void
3340xfs_bmap_btalloc_accounting(
3341 struct xfs_bmalloca *ap,
3342 struct xfs_alloc_arg *args)
3343{
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003344 if (ap->flags & XFS_BMAPI_COWFORK) {
3345 /*
3346 * COW fork blocks are in-core only and thus are treated as
3347 * in-core quota reservation (like delalloc blocks) even when
3348 * converted to real blocks. The quota reservation is not
3349 * accounted to disk until blocks are remapped to the data
3350 * fork. So if these blocks were previously delalloc, we
3351 * already have quota reservation and there's nothing to do
3352 * yet.
3353 */
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003354 if (ap->wasdel) {
3355 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003356 return;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003357 }
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003358
3359 /*
3360 * Otherwise, we've allocated blocks in a hole. The transaction
3361 * has acquired in-core quota reservation for this extent.
3362 * Rather than account these as real blocks, however, we reduce
3363 * the transaction quota reservation based on the allocation.
3364 * This essentially transfers the transaction quota reservation
3365 * to that of a delalloc extent.
3366 */
3367 ap->ip->i_delayed_blks += args->len;
3368 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3369 -(long)args->len);
3370 return;
3371 }
3372
3373 /* data/attr fork only */
3374 ap->ip->i_d.di_nblocks += args->len;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003375 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003376 if (ap->wasdel) {
Darrick J. Wong751f3762018-01-25 13:58:13 -08003377 ap->ip->i_delayed_blks -= args->len;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003378 xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3379 }
Darrick J. Wong751f3762018-01-25 13:58:13 -08003380 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3381 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3382 args->len);
3383}
3384
Christoph Hellwigc467c042010-02-15 23:34:42 +00003385STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003386xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003387 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003388{
3389 xfs_mount_t *mp; /* mount point structure */
3390 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003391 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003392 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003393 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003394 xfs_alloc_arg_t args;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003395 xfs_fileoff_t orig_offset;
3396 xfs_extlen_t orig_length;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003397 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003398 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003399 int nullfb; /* true if ap->firstblock isn't set */
3400 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003401 int tryagain;
3402 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003403 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003404
Dave Chinnera99ebf42011-12-01 11:24:20 +00003405 ASSERT(ap->length);
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003406 orig_offset = ap->offset;
3407 orig_length = ap->length;
Dave Chinnera99ebf42011-12-01 11:24:20 +00003408
Nathan Scotta365bdd2006-03-14 13:34:16 +11003409 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003410
3411 /* stripe alignment for allocation is determined by mount parameters */
3412 stripe_align = 0;
3413 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3414 stripe_align = mp->m_swidth;
3415 else if (mp->m_dalign)
3416 stripe_align = mp->m_dalign;
3417
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003418 if (ap->flags & XFS_BMAPI_COWFORK)
3419 align = xfs_get_cowextsz_hint(ap->ip);
3420 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003421 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003422 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003423 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003424 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003425 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003426 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003427 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003428 }
Dave Chinner33177f052013-12-12 16:34:36 +11003429
3430
Brian Foster94c07b42018-07-11 22:26:28 -07003431 nullfb = ap->tp->t_firstblock == NULLFSBLOCK;
3432 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp,
3433 ap->tp->t_firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003434 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003435 if (xfs_alloc_is_userdata(ap->datatype) &&
3436 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003437 ag = xfs_filestream_lookup_ag(ap->ip);
3438 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003439 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003440 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003441 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003442 }
3443 } else
Brian Foster94c07b42018-07-11 22:26:28 -07003444 ap->blkno = ap->tp->t_firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003445
3446 xfs_bmap_adjacent(ap);
3447
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003449 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 * it's in the right allocation group.
3451 */
Dave Chinner3a756672011-09-18 20:40:58 +00003452 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 ;
3454 else
Brian Foster94c07b42018-07-11 22:26:28 -07003455 ap->blkno = ap->tp->t_firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 * Normal allocation, done through xfs_alloc_vextent.
3458 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003459 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003460 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003461 args.tp = ap->tp;
3462 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003463 args.fsbno = ap->blkno;
Darrick J. Wong7280fed2018-12-12 08:46:23 -08003464 args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
Dave Chinner14b064c2011-01-27 12:16:28 +11003465
3466 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner9bb54cb2018-06-07 07:54:02 -07003467 args.maxlen = min(ap->length, mp->m_ag_max_usable);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003468 blen = 0;
3469 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003470 /*
3471 * Search for an allocation group with a single extent large
3472 * enough for the request. If one isn't found, then adjust
3473 * the minimum allocation size to the largest space found.
3474 */
Dave Chinner292378e2016-09-26 08:21:28 +10003475 if (xfs_alloc_is_userdata(ap->datatype) &&
3476 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003477 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3478 else
3479 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003480 if (error)
3481 return error;
Brian Foster1214f1c2018-08-01 07:20:31 -07003482 } else if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003483 if (xfs_inode_is_filestream(ap->ip))
3484 args.type = XFS_ALLOCTYPE_FIRST_AG;
3485 else
3486 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003487 args.total = args.minlen = ap->minlen;
3488 } else {
3489 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3490 args.total = ap->total;
3491 args.minlen = ap->minlen;
3492 }
David Chinner957d0eb2007-06-18 16:50:37 +10003493 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003494 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003495 args.prod = align;
Dave Chinner0703a8e2018-06-08 09:54:22 -07003496 div_u64_rem(ap->offset, args.prod, &args.mod);
3497 if (args.mod)
3498 args.mod = args.prod - args.mod;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003499 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003500 args.prod = 1;
3501 args.mod = 0;
3502 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003503 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner0703a8e2018-06-08 09:54:22 -07003504 div_u64_rem(ap->offset, args.prod, &args.mod);
3505 if (args.mod)
3506 args.mod = args.prod - args.mod;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003507 }
3508 /*
3509 * If we are not low on available data blocks, and the
3510 * underlying logical volume manager is a stripe, and
3511 * the file offset is zero then try to allocate data
3512 * blocks on stripe unit boundary.
3513 * NOTE: ap->aeof is only set if the allocation length
3514 * is >= the stripe unit and the allocation offset is
3515 * at the end of file.
3516 */
Brian Foster1214f1c2018-08-01 07:20:31 -07003517 if (!(ap->tp->t_flags & XFS_TRANS_LOWMODE) && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003518 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003519 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003520 atype = args.type;
3521 isaligned = 1;
3522 /*
3523 * Adjust for alignment
3524 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003525 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003526 args.minlen = blen - args.alignment;
3527 args.minalignslop = 0;
3528 } else {
3529 /*
3530 * First try an exact bno allocation.
3531 * If it fails then do a near or start bno
3532 * allocation with alignment turned on.
3533 */
3534 atype = args.type;
3535 tryagain = 1;
3536 args.type = XFS_ALLOCTYPE_THIS_BNO;
3537 args.alignment = 1;
3538 /*
3539 * Compute the minlen+alignment for the
3540 * next case. Set slop so that the value
3541 * of minlen+alignment+slop doesn't go up
3542 * between the calls.
3543 */
Dave Chinner33177f052013-12-12 16:34:36 +11003544 if (blen > stripe_align && blen <= args.maxlen)
3545 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003546 else
3547 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003548 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003549 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003550 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003551 args.minlen - 1;
3552 else
3553 args.minalignslop = 0;
3554 }
3555 } else {
3556 args.alignment = 1;
3557 args.minalignslop = 0;
3558 }
3559 args.minleft = ap->minleft;
3560 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003561 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003562 args.datatype = ap->datatype;
3563 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003564 args.ip = ap->ip;
3565
3566 error = xfs_alloc_vextent(&args);
3567 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003568 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003569
Nathan Scotta365bdd2006-03-14 13:34:16 +11003570 if (tryagain && args.fsbno == NULLFSBLOCK) {
3571 /*
3572 * Exact allocation failed. Now try with alignment
3573 * turned on.
3574 */
3575 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003576 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003577 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003578 args.minlen = nextminlen;
3579 args.minalignslop = 0;
3580 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 if ((error = xfs_alloc_vextent(&args)))
3582 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003583 }
3584 if (isaligned && args.fsbno == NULLFSBLOCK) {
3585 /*
3586 * allocation failed, so turn off alignment and
3587 * try again.
3588 */
3589 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003590 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003591 args.alignment = 0;
3592 if ((error = xfs_alloc_vextent(&args)))
3593 return error;
3594 }
3595 if (args.fsbno == NULLFSBLOCK && nullfb &&
3596 args.minlen > ap->minlen) {
3597 args.minlen = ap->minlen;
3598 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003599 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003600 if ((error = xfs_alloc_vextent(&args)))
3601 return error;
3602 }
3603 if (args.fsbno == NULLFSBLOCK && nullfb) {
3604 args.fsbno = 0;
3605 args.type = XFS_ALLOCTYPE_FIRST_AG;
3606 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003607 if ((error = xfs_alloc_vextent(&args)))
3608 return error;
Brian Foster1214f1c2018-08-01 07:20:31 -07003609 ap->tp->t_flags |= XFS_TRANS_LOWMODE;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003610 }
3611 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003612 /*
3613 * check the allocation happened at the same or higher AG than
3614 * the first block that was allocated.
3615 */
Brian Foster94c07b42018-07-11 22:26:28 -07003616 ASSERT(ap->tp->t_firstblock == NULLFSBLOCK ||
3617 XFS_FSB_TO_AGNO(mp, ap->tp->t_firstblock) <=
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003618 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003619
Dave Chinner3a756672011-09-18 20:40:58 +00003620 ap->blkno = args.fsbno;
Brian Foster94c07b42018-07-11 22:26:28 -07003621 if (ap->tp->t_firstblock == NULLFSBLOCK)
3622 ap->tp->t_firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003623 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003624 ap->length = args.len;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003625 /*
3626 * If the extent size hint is active, we tried to round the
3627 * caller's allocation request offset down to extsz and the
3628 * length up to another extsz boundary. If we found a free
3629 * extent we mapped it in starting at this new offset. If the
3630 * newly mapped space isn't long enough to cover any of the
3631 * range of offsets that was originally requested, move the
3632 * mapping up so that we can fill as much of the caller's
3633 * original request as possible. Free space is apparently
3634 * very fragmented so we're unlikely to be able to satisfy the
3635 * hints anyway.
3636 */
3637 if (ap->length <= orig_length)
3638 ap->offset = orig_offset;
3639 else if (ap->offset + ap->length < orig_offset + orig_length)
3640 ap->offset = orig_offset + orig_length - ap->length;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003641 xfs_bmap_btalloc_accounting(ap, &args);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003642 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003643 ap->blkno = NULLFSBLOCK;
3644 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 }
3646 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003647}
3648
3649/*
3650 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3651 * It figures out where to ask the underlying allocator to put the new extent.
3652 */
3653STATIC int
3654xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003655 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003656{
Dave Chinner292378e2016-09-26 08:21:28 +10003657 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3658 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003659 return xfs_bmap_rtalloc(ap);
3660 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661}
3662
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003663/* Trim extent to fit a logical block range. */
3664void
3665xfs_trim_extent(
3666 struct xfs_bmbt_irec *irec,
3667 xfs_fileoff_t bno,
3668 xfs_filblks_t len)
3669{
3670 xfs_fileoff_t distance;
3671 xfs_fileoff_t end = bno + len;
3672
3673 if (irec->br_startoff + irec->br_blockcount <= bno ||
3674 irec->br_startoff >= end) {
3675 irec->br_blockcount = 0;
3676 return;
3677 }
3678
3679 if (irec->br_startoff < bno) {
3680 distance = bno - irec->br_startoff;
3681 if (isnullstartblock(irec->br_startblock))
3682 irec->br_startblock = DELAYSTARTBLOCK;
3683 if (irec->br_startblock != DELAYSTARTBLOCK &&
3684 irec->br_startblock != HOLESTARTBLOCK)
3685 irec->br_startblock += distance;
3686 irec->br_startoff += distance;
3687 irec->br_blockcount -= distance;
3688 }
3689
3690 if (end < irec->br_startoff + irec->br_blockcount) {
3691 distance = irec->br_startoff + irec->br_blockcount - end;
3692 irec->br_blockcount -= distance;
3693 }
3694}
3695
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003697 * Trim the returned map to the required bounds
3698 */
3699STATIC void
3700xfs_bmapi_trim_map(
3701 struct xfs_bmbt_irec *mval,
3702 struct xfs_bmbt_irec *got,
3703 xfs_fileoff_t *bno,
3704 xfs_filblks_t len,
3705 xfs_fileoff_t obno,
3706 xfs_fileoff_t end,
3707 int n,
3708 int flags)
3709{
3710 if ((flags & XFS_BMAPI_ENTIRE) ||
3711 got->br_startoff + got->br_blockcount <= obno) {
3712 *mval = *got;
3713 if (isnullstartblock(got->br_startblock))
3714 mval->br_startblock = DELAYSTARTBLOCK;
3715 return;
3716 }
3717
3718 if (obno > *bno)
3719 *bno = obno;
3720 ASSERT((*bno >= obno) || (n == 0));
3721 ASSERT(*bno < end);
3722 mval->br_startoff = *bno;
3723 if (isnullstartblock(got->br_startblock))
3724 mval->br_startblock = DELAYSTARTBLOCK;
3725 else
3726 mval->br_startblock = got->br_startblock +
3727 (*bno - got->br_startoff);
3728 /*
3729 * Return the minimum of what we got and what we asked for for
3730 * the length. We can use the len variable here because it is
3731 * modified below and we could have been there before coming
3732 * here if the first part of the allocation didn't overlap what
3733 * was asked for.
3734 */
3735 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3736 got->br_blockcount - (*bno - got->br_startoff));
3737 mval->br_state = got->br_state;
3738 ASSERT(mval->br_blockcount <= len);
3739 return;
3740}
3741
3742/*
3743 * Update and validate the extent map to return
3744 */
3745STATIC void
3746xfs_bmapi_update_map(
3747 struct xfs_bmbt_irec **map,
3748 xfs_fileoff_t *bno,
3749 xfs_filblks_t *len,
3750 xfs_fileoff_t obno,
3751 xfs_fileoff_t end,
3752 int *n,
3753 int flags)
3754{
3755 xfs_bmbt_irec_t *mval = *map;
3756
3757 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3758 ((mval->br_startoff + mval->br_blockcount) <= end));
3759 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3760 (mval->br_startoff < obno));
3761
3762 *bno = mval->br_startoff + mval->br_blockcount;
3763 *len = end - *bno;
3764 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3765 /* update previous map with new information */
3766 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3767 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3768 ASSERT(mval->br_state == mval[-1].br_state);
3769 mval[-1].br_blockcount = mval->br_blockcount;
3770 mval[-1].br_state = mval->br_state;
3771 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3772 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3773 mval[-1].br_startblock != HOLESTARTBLOCK &&
3774 mval->br_startblock == mval[-1].br_startblock +
3775 mval[-1].br_blockcount &&
Christoph Hellwigc3a2f9f2018-07-11 22:26:01 -07003776 mval[-1].br_state == mval->br_state) {
Dave Chinneraef9a892011-09-18 20:40:44 +00003777 ASSERT(mval->br_startoff ==
3778 mval[-1].br_startoff + mval[-1].br_blockcount);
3779 mval[-1].br_blockcount += mval->br_blockcount;
3780 } else if (*n > 0 &&
3781 mval->br_startblock == DELAYSTARTBLOCK &&
3782 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3783 mval->br_startoff ==
3784 mval[-1].br_startoff + mval[-1].br_blockcount) {
3785 mval[-1].br_blockcount += mval->br_blockcount;
3786 mval[-1].br_state = mval->br_state;
3787 } else if (!((*n == 0) &&
3788 ((mval->br_startoff + mval->br_blockcount) <=
3789 obno))) {
3790 mval++;
3791 (*n)++;
3792 }
3793 *map = mval;
3794}
3795
3796/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003797 * Map file blocks to filesystem blocks without allocation.
3798 */
3799int
3800xfs_bmapi_read(
3801 struct xfs_inode *ip,
3802 xfs_fileoff_t bno,
3803 xfs_filblks_t len,
3804 struct xfs_bmbt_irec *mval,
3805 int *nmap,
3806 int flags)
3807{
3808 struct xfs_mount *mp = ip->i_mount;
3809 struct xfs_ifork *ifp;
3810 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003811 xfs_fileoff_t obno;
3812 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003813 struct xfs_iext_cursor icur;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003814 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003815 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003816 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003817 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003818
3819 ASSERT(*nmap >= 1);
3820 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Christoph Hellwigc3a2f9f2018-07-11 22:26:01 -07003821 XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003822 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003823
3824 if (unlikely(XFS_TEST_ERROR(
3825 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3826 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003827 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003828 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003829 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003830 }
3831
3832 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003833 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003834
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003835 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003836
3837 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong8612de32019-08-11 15:52:27 -07003838 if (!ifp) {
3839 /* No CoW fork? Return a hole. */
3840 if (whichfork == XFS_COW_FORK) {
3841 mval->br_startoff = bno;
3842 mval->br_startblock = HOLESTARTBLOCK;
3843 mval->br_blockcount = len;
3844 mval->br_state = XFS_EXT_NORM;
3845 *nmap = 1;
3846 return 0;
3847 }
Dave Chinner5c8ed202011-09-18 20:40:45 +00003848
Darrick J. Wong8612de32019-08-11 15:52:27 -07003849 /*
3850 * A missing attr ifork implies that the inode says we're in
3851 * extents or btree format but failed to pass the inode fork
3852 * verifier while trying to load it. Treat that as a file
3853 * corruption too.
3854 */
3855#ifdef DEBUG
3856 xfs_alert(mp, "%s: inode %llu missing fork %d",
3857 __func__, ip->i_ino, whichfork);
3858#endif /* DEBUG */
3859 return -EFSCORRUPTED;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003860 }
3861
Dave Chinner5c8ed202011-09-18 20:40:45 +00003862 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3863 error = xfs_iread_extents(NULL, ip, whichfork);
3864 if (error)
3865 return error;
3866 }
3867
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003868 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003869 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003870 end = bno + len;
3871 obno = bno;
3872
3873 while (bno < end && n < *nmap) {
3874 /* Reading past eof, act as though there's a hole up to end. */
3875 if (eof)
3876 got.br_startoff = end;
3877 if (got.br_startoff > bno) {
3878 /* Reading in a hole. */
3879 mval->br_startoff = bno;
3880 mval->br_startblock = HOLESTARTBLOCK;
3881 mval->br_blockcount =
3882 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3883 mval->br_state = XFS_EXT_NORM;
3884 bno += mval->br_blockcount;
3885 len -= mval->br_blockcount;
3886 mval++;
3887 n++;
3888 continue;
3889 }
3890
3891 /* set up the extent map to return. */
3892 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3893 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3894
3895 /* If we're done, stop now. */
3896 if (bno >= end || n >= *nmap)
3897 break;
3898
3899 /* Else go on to the next record. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003900 if (!xfs_iext_next_extent(ifp, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003901 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003902 }
3903 *nmap = n;
3904 return 0;
3905}
3906
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003907/*
3908 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3909 * global pool and the extent inserted into the inode in-core extent tree.
3910 *
3911 * On entry, got refers to the first extent beyond the offset of the extent to
3912 * allocate or eof is specified if no such extent exists. On return, got refers
3913 * to the extent record that was inserted to the inode fork.
3914 *
3915 * Note that the allocated extent may have been merged with contiguous extents
3916 * during insertion into the inode fork. Thus, got does not reflect the current
3917 * state of the inode fork on return. If necessary, the caller can use lastx to
3918 * look up the updated record in the inode fork.
3919 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10003920int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003921xfs_bmapi_reserve_delalloc(
3922 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003923 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11003924 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003925 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11003926 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003927 struct xfs_bmbt_irec *got,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003928 struct xfs_iext_cursor *icur,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003929 int eof)
3930{
3931 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003932 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003933 xfs_extlen_t alen;
3934 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003935 int error;
Brian Foster974ae922016-11-28 14:57:42 +11003936 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003937
Brian Foster974ae922016-11-28 14:57:42 +11003938 /*
3939 * Cap the alloc length. Keep track of prealloc so we know whether to
3940 * tag the inode before we return.
3941 */
3942 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003943 if (!eof)
3944 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11003945 if (prealloc && alen >= len)
3946 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003947
3948 /* Figure out the extent size, adjust alen */
Shan Hai6ca30722018-01-23 13:56:11 -08003949 if (whichfork == XFS_COW_FORK) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003950 struct xfs_bmbt_irec prev;
Shan Hai6ca30722018-01-23 13:56:11 -08003951 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003952
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003953 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003954 prev.br_startoff = NULLFILEOFF;
3955
Shan Hai6ca30722018-01-23 13:56:11 -08003956 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003957 1, 0, &aoff, &alen);
3958 ASSERT(!error);
3959 }
3960
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003961 /*
3962 * Make a transaction-less quota reservation for delayed allocation
3963 * blocks. This number gets adjusted later. We return if we haven't
3964 * allocated blocks already inside this loop.
3965 */
3966 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
Shan Hai6ca30722018-01-23 13:56:11 -08003967 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003968 if (error)
3969 return error;
3970
3971 /*
3972 * Split changing sb for alen and indlen since they could be coming
3973 * from different places.
3974 */
3975 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
3976 ASSERT(indlen > 0);
3977
Shan Hai6ca30722018-01-23 13:56:11 -08003978 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003979 if (error)
3980 goto out_unreserve_quota;
3981
Dave Chinner0d485ad2015-02-23 21:22:03 +11003982 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003983 if (error)
3984 goto out_unreserve_blocks;
3985
3986
3987 ip->i_delayed_blks += alen;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07003988 xfs_mod_delalloc(ip->i_mount, alen + indlen);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003989
3990 got->br_startoff = aoff;
3991 got->br_startblock = nullstartblock(indlen);
3992 got->br_blockcount = alen;
3993 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003994
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003995 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003996
Brian Foster974ae922016-11-28 14:57:42 +11003997 /*
3998 * Tag the inode if blocks were preallocated. Note that COW fork
3999 * preallocation can occur at the start or end of the extent, even when
4000 * prealloc == 0, so we must also check the aligned offset and length.
4001 */
4002 if (whichfork == XFS_DATA_FORK && prealloc)
4003 xfs_inode_set_eofblocks_tag(ip);
4004 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4005 xfs_inode_set_cowblocks_tag(ip);
4006
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004007 return 0;
4008
4009out_unreserve_blocks:
Shan Hai6ca30722018-01-23 13:56:11 -08004010 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004011out_unreserve_quota:
4012 if (XFS_IS_QUOTA_ON(mp))
Shan Hai6ca30722018-01-23 13:56:11 -08004013 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0,
4014 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004015 return error;
4016}
4017
Dave Chinnercf11da92014-07-15 07:08:24 +10004018static int
4019xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004020 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004021{
4022 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004023 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004024 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004025 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004026 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004027
Dave Chinnera99ebf42011-12-01 11:24:20 +00004028 ASSERT(bma->length > 0);
4029
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004030 /*
4031 * For the wasdelay case, we could also just allocate the stuff asked
4032 * for in this bmap call but that wouldn't be as good.
4033 */
4034 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004035 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4036 bma->offset = bma->got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004037 xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004038 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004039 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004040 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004041 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004042 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004043 }
4044
4045 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004046 * Set the data type being allocated. For the data fork, the first data
4047 * in the file is treated differently to all other allocations. For the
4048 * attribute fork, we only need to ensure the allocated range is not on
4049 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004050 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004051 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004052 bma->datatype = XFS_ALLOC_NOBUSY;
4053 if (whichfork == XFS_DATA_FORK) {
4054 if (bma->offset == 0)
4055 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4056 else
4057 bma->datatype |= XFS_ALLOC_USERDATA;
4058 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004059 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004060 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004061 }
4062
Dave Chinnere04426b2012-10-05 11:06:59 +10004063 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004064
4065 /*
4066 * Only want to do the alignment at the eof if it is userdata and
4067 * allocation length is larger than a stripe unit.
4068 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004069 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004070 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004071 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004072 if (error)
4073 return error;
4074 }
4075
4076 error = xfs_bmap_alloc(bma);
4077 if (error)
4078 return error;
4079
Dave Chinner963c30c2011-09-18 20:40:59 +00004080 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004081 return 0;
Brian Fostercf612de2018-07-11 22:26:29 -07004082 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur)
Dave Chinner29c8d172011-09-18 20:41:00 +00004083 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004084 /*
4085 * Bump the number of extents we've allocated
4086 * in this call.
4087 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004088 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004089
Dave Chinner29c8d172011-09-18 20:41:00 +00004090 if (bma->cur)
4091 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004092 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4093
Dave Chinner963c30c2011-09-18 20:40:59 +00004094 bma->got.br_startoff = bma->offset;
4095 bma->got.br_startblock = bma->blkno;
4096 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004097 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004098
4099 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004100 * In the data fork, a wasdelay extent has been initialized, so
4101 * shouldn't be flagged as unwritten.
4102 *
4103 * For the cow fork, however, we convert delalloc reservations
4104 * (extents allocated for speculative preallocation) to
4105 * allocated unwritten extents, and only convert the unwritten
4106 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004107 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004108 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11004109 (bma->flags & XFS_BMAPI_PREALLOC))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004110 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004111
Christoph Hellwigc6534242011-09-18 20:41:05 +00004112 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004113 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004114 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004115 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004116 whichfork, &bma->icur, &bma->cur, &bma->got,
Brian Foster92f9da32018-07-11 22:26:28 -07004117 &bma->logflags, bma->flags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004118
Christoph Hellwigc315c902011-09-18 20:41:02 +00004119 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004120 if (error)
4121 return error;
4122
4123 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004124 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4125 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4126 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004127 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004128 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004129
Dave Chinner963c30c2011-09-18 20:40:59 +00004130 ASSERT(bma->got.br_startoff <= bma->offset);
4131 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4132 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004133 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4134 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004135 return 0;
4136}
4137
Dave Chinnerb447fe52011-09-18 20:40:51 +00004138STATIC int
4139xfs_bmapi_convert_unwritten(
4140 struct xfs_bmalloca *bma,
4141 struct xfs_bmbt_irec *mval,
4142 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004143 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004144{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004145 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004146 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004147 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004148 int error;
4149
Dave Chinnerb447fe52011-09-18 20:40:51 +00004150 /* check if we need to do unwritten->real conversion */
4151 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4152 (flags & XFS_BMAPI_PREALLOC))
4153 return 0;
4154
4155 /* check if we need to do real->unwritten conversion */
4156 if (mval->br_state == XFS_EXT_NORM &&
4157 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4158 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4159 return 0;
4160
4161 /*
4162 * Modify (by adding) the state flag, if writing.
4163 */
4164 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004165 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4166 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004167 bma->ip, whichfork);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004168 }
4169 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4170 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4171
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004172 /*
4173 * Before insertion into the bmbt, zero the range being converted
4174 * if required.
4175 */
4176 if (flags & XFS_BMAPI_ZERO) {
4177 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4178 mval->br_blockcount);
4179 if (error)
4180 return error;
4181 }
4182
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004183 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
Brian Foster92f9da32018-07-11 22:26:28 -07004184 &bma->icur, &bma->cur, mval, &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004185 /*
4186 * Log the inode core unconditionally in the unwritten extent conversion
4187 * path because the conversion might not have done so (e.g., if the
4188 * extent count hasn't changed). We need to make sure the inode is dirty
4189 * in the transaction for the sake of fsync(), even if nothing has
4190 * changed, because fsync() will not force the log for this transaction
4191 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004192 *
4193 * Note: If we're only converting cow fork extents, there aren't
4194 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004195 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004196 if (whichfork != XFS_COW_FORK)
4197 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004198 if (error)
4199 return error;
4200
4201 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004202 * Update our extent pointer, given that
4203 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4204 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004205 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004206 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004207
4208 /*
4209 * We may have combined previously unwritten space with written space,
4210 * so generate another request.
4211 */
4212 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004213 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004214 return 0;
4215}
4216
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004217static inline xfs_extlen_t
4218xfs_bmapi_minleft(
4219 struct xfs_trans *tp,
4220 struct xfs_inode *ip,
4221 int fork)
4222{
4223 if (tp && tp->t_firstblock != NULLFSBLOCK)
4224 return 0;
4225 if (XFS_IFORK_FORMAT(ip, fork) != XFS_DINODE_FMT_BTREE)
4226 return 1;
4227 return be16_to_cpu(XFS_IFORK_PTR(ip, fork)->if_broot->bb_level) + 1;
4228}
4229
4230/*
4231 * Log whatever the flags say, even if error. Otherwise we might miss detecting
4232 * a case where the data is changed, there's an error, and it's not logged so we
4233 * don't shutdown when we should. Don't bother logging extents/btree changes if
4234 * we converted to the other format.
4235 */
4236static void
4237xfs_bmapi_finish(
4238 struct xfs_bmalloca *bma,
4239 int whichfork,
4240 int error)
4241{
4242 if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
4243 XFS_IFORK_FORMAT(bma->ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4244 bma->logflags &= ~xfs_ilog_fext(whichfork);
4245 else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
4246 XFS_IFORK_FORMAT(bma->ip, whichfork) != XFS_DINODE_FMT_BTREE)
4247 bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4248
4249 if (bma->logflags)
4250 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4251 if (bma->cur)
4252 xfs_btree_del_cursor(bma->cur, error);
4253}
4254
Christoph Hellwig44032802011-09-18 20:40:48 +00004255/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004256 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4257 * extent state if necessary. Details behaviour is controlled by the flags
4258 * parameter. Only allocates blocks from a single allocation group, to avoid
4259 * locking problems.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004261int
4262xfs_bmapi_write(
4263 struct xfs_trans *tp, /* transaction pointer */
4264 struct xfs_inode *ip, /* incore inode */
4265 xfs_fileoff_t bno, /* starting file offs. mapped */
4266 xfs_filblks_t len, /* length to map in file */
4267 int flags, /* XFS_BMAPI_... */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004268 xfs_extlen_t total, /* total blocks needed */
4269 struct xfs_bmbt_irec *mval, /* output: map values */
Brian Foster6e702a52018-07-11 22:26:12 -07004270 int *nmap) /* i/o: mval size/count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
Darrick J. Wong4b0bce32019-03-19 08:16:22 -07004272 struct xfs_bmalloca bma = {
4273 .tp = tp,
4274 .ip = ip,
4275 .total = total,
4276 };
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004277 struct xfs_mount *mp = ip->i_mount;
4278 struct xfs_ifork *ifp;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004279 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004280 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004281 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004282 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004283 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004284 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004285
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004287 xfs_fileoff_t orig_bno; /* original block number value */
4288 int orig_flags; /* original flags arg value */
4289 xfs_filblks_t orig_len; /* original value of len arg */
4290 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4291 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
4293 orig_bno = bno;
4294 orig_len = len;
4295 orig_flags = flags;
4296 orig_mval = mval;
4297 orig_nmap = *nmap;
4298#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004299 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004300
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004302 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
Christoph Hellwig26b91c72019-02-18 09:38:48 -08004303 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004304 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004305 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004306 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004307 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004308
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004309 /* zeroing is for currently only for data extents, not metadata */
4310 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4311 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4312 /*
4313 * we can allocate unwritten extents or pre-zero allocated blocks,
4314 * but it makes no sense to do both at once. This would result in
4315 * zeroing the unwritten extent twice, but it still being an
4316 * unwritten extent....
4317 */
4318 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4319 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4320
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 if (unlikely(XFS_TEST_ERROR(
4322 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004323 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004324 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004325 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004326 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004328
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004330 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004331
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004333
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004334 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004335
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004336 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4337 error = xfs_iread_extents(tp, ip, whichfork);
4338 if (error)
4339 goto error0;
4340 }
4341
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004342 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004343 eof = true;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004344 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004345 bma.prev.br_startoff = NULLFILEOFF;
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004346 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004347
Brian Foster627209f2019-02-01 09:14:23 -08004348 n = 0;
4349 end = bno + len;
4350 obno = bno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004352 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004353
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004354 /* in hole or beyond EOF? */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004355 if (eof || bma.got.br_startoff > bno) {
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004356 /*
4357 * CoW fork conversions should /never/ hit EOF or
4358 * holes. There should always be something for us
4359 * to work on.
4360 */
4361 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4362 (flags & XFS_BMAPI_COWFORK)));
4363
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004364 need_alloc = true;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004365 } else if (isnullstartblock(bma.got.br_startblock)) {
4366 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004367 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004368
4369 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 * First, deal with the hole before the allocated space
4371 * that we found, if any.
4372 */
Christoph Hellwig26b91c72019-02-18 09:38:48 -08004373 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004374 bma.eof = eof;
4375 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4376 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004377 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004378 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004379
Dave Chinnera99ebf42011-12-01 11:24:20 +00004380 /*
4381 * There's a 32/64 bit type mismatch between the
4382 * allocation length request (which can be 64 bits in
4383 * length) and the bma length request, which is
4384 * xfs_extlen_t and therefore 32 bits. Hence we have to
4385 * check for 32-bit overflows and handle them here.
4386 */
4387 if (len > (xfs_filblks_t)MAXEXTLEN)
4388 bma.length = MAXEXTLEN;
4389 else
4390 bma.length = len;
4391
4392 ASSERT(len > 0);
4393 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004394 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 if (error)
4396 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004397 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004398 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004399
4400 /*
4401 * If this is a CoW allocation, record the data in
4402 * the refcount btree for orphan recovery.
4403 */
4404 if (whichfork == XFS_COW_FORK) {
Brian Foster0f37d172018-08-01 07:20:34 -07004405 error = xfs_refcount_alloc_cow_extent(tp,
4406 bma.blkno, bma.length);
Darrick J. Wong174edb02016-10-03 09:11:39 -07004407 if (error)
4408 goto error0;
4409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004411
Dave Chinneraef9a892011-09-18 20:40:44 +00004412 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004413 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4414 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
Dave Chinnerb447fe52011-09-18 20:40:51 +00004416 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004417 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004418 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004419 continue;
4420 if (error)
4421 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
Dave Chinneraef9a892011-09-18 20:40:44 +00004423 /* update the extent map to return */
4424 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4425
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 /*
4427 * If we're done, stop now. Stop when we've allocated
4428 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4429 * the transaction may get too big.
4430 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004431 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004433
4434 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004435 bma.prev = bma.got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004436 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004437 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004440
Christoph Hellwigb101e332019-02-15 08:02:47 -08004441 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4442 whichfork);
4443 if (error)
4444 goto error0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004445
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004447 XFS_IFORK_NEXTENTS(ip, whichfork) >
4448 XFS_IFORK_MAXEXT(ip, whichfork));
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004449 xfs_bmapi_finish(&bma, whichfork, 0);
4450 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4451 orig_nmap, *nmap);
4452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453error0:
Christoph Hellwigc8b54672019-02-15 08:02:48 -08004454 xfs_bmapi_finish(&bma, whichfork, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 return error;
4456}
4457
Brian Foster627209f2019-02-01 09:14:23 -08004458/*
4459 * Convert an existing delalloc extent to real blocks based on file offset. This
4460 * attempts to allocate the entire delalloc extent and may require multiple
4461 * invocations to allocate the target offset if a large enough physical extent
4462 * is not available.
4463 */
4464int
4465xfs_bmapi_convert_delalloc(
Brian Foster627209f2019-02-01 09:14:23 -08004466 struct xfs_inode *ip,
Brian Foster627209f2019-02-01 09:14:23 -08004467 int whichfork,
Christoph Hellwig491ce612019-02-15 08:02:49 -08004468 xfs_fileoff_t offset_fsb,
4469 struct xfs_bmbt_irec *imap,
4470 unsigned int *seq)
Brian Foster627209f2019-02-01 09:14:23 -08004471{
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004472 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004473 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004474 struct xfs_bmalloca bma = { NULL };
Christoph Hellwig491ce612019-02-15 08:02:49 -08004475 struct xfs_trans *tp;
Brian Foster627209f2019-02-01 09:14:23 -08004476 int error;
Brian Foster627209f2019-02-01 09:14:23 -08004477
Christoph Hellwig491ce612019-02-15 08:02:49 -08004478 /*
4479 * Space for the extent and indirect blocks was reserved when the
4480 * delalloc extent was created so there's no need to do so here.
4481 */
4482 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4483 XFS_TRANS_RESERVE, &tp);
4484 if (error)
4485 return error;
4486
4487 xfs_ilock(ip, XFS_ILOCK_EXCL);
4488 xfs_trans_ijoin(tp, ip, 0);
4489
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004490 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4491 bma.got.br_startoff > offset_fsb) {
4492 /*
4493 * No extent found in the range we are trying to convert. This
4494 * should only happen for the COW fork, where another thread
4495 * might have moved the extent to the data fork in the meantime.
4496 */
4497 WARN_ON_ONCE(whichfork != XFS_COW_FORK);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004498 error = -EAGAIN;
4499 goto out_trans_cancel;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004500 }
Brian Foster627209f2019-02-01 09:14:23 -08004501
4502 /*
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004503 * If we find a real extent here we raced with another thread converting
4504 * the extent. Just return the real extent at this offset.
Brian Foster627209f2019-02-01 09:14:23 -08004505 */
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004506 if (!isnullstartblock(bma.got.br_startblock)) {
4507 *imap = bma.got;
Christoph Hellwig491ce612019-02-15 08:02:49 -08004508 *seq = READ_ONCE(ifp->if_seq);
4509 goto out_trans_cancel;
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004510 }
4511
4512 bma.tp = tp;
4513 bma.ip = ip;
4514 bma.wasdel = true;
4515 bma.offset = bma.got.br_startoff;
4516 bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount, MAXEXTLEN);
4517 bma.total = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
4518 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4519 if (whichfork == XFS_COW_FORK)
4520 bma.flags = XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC;
4521
4522 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4523 bma.prev.br_startoff = NULLFILEOFF;
4524
4525 error = xfs_bmapi_allocate(&bma);
4526 if (error)
4527 goto out_finish;
4528
4529 error = -ENOSPC;
4530 if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK))
4531 goto out_finish;
4532 error = -EFSCORRUPTED;
4533 if (WARN_ON_ONCE(!bma.got.br_startblock && !XFS_IS_REALTIME_INODE(ip)))
4534 goto out_finish;
4535
Christoph Hellwig125851a2019-02-15 08:02:49 -08004536 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4537 XFS_STATS_INC(mp, xs_xstrat_quick);
4538
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004539 ASSERT(!isnullstartblock(bma.got.br_startblock));
4540 *imap = bma.got;
Christoph Hellwig491ce612019-02-15 08:02:49 -08004541 *seq = READ_ONCE(ifp->if_seq);
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004542
4543 if (whichfork == XFS_COW_FORK) {
4544 error = xfs_refcount_alloc_cow_extent(tp, bma.blkno,
4545 bma.length);
4546 if (error)
4547 goto out_finish;
4548 }
4549
4550 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4551 whichfork);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004552 if (error)
4553 goto out_finish;
4554
4555 xfs_bmapi_finish(&bma, whichfork, 0);
4556 error = xfs_trans_commit(tp);
4557 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4558 return error;
4559
Christoph Hellwigd8ae82e2019-02-15 08:02:48 -08004560out_finish:
4561 xfs_bmapi_finish(&bma, whichfork, error);
Christoph Hellwig491ce612019-02-15 08:02:49 -08004562out_trans_cancel:
4563 xfs_trans_cancel(tp);
4564 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Brian Foster627209f2019-02-01 09:14:23 -08004565 return error;
4566}
4567
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004568int
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004569xfs_bmapi_remap(
4570 struct xfs_trans *tp,
4571 struct xfs_inode *ip,
4572 xfs_fileoff_t bno,
4573 xfs_filblks_t len,
4574 xfs_fsblock_t startblock,
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004575 int flags)
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004576{
4577 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004578 struct xfs_ifork *ifp;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004579 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004580 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004581 struct xfs_iext_cursor icur;
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004582 int whichfork = xfs_bmapi_whichfork(flags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004583 int logflags = 0, error;
4584
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004585 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004586 ASSERT(len > 0);
4587 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4588 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Darrick J. Wong7644bd92018-05-14 06:34:35 -07004589 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4590 XFS_BMAPI_NORMAP)));
4591 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4592 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004593
4594 if (unlikely(XFS_TEST_ERROR(
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004595 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4596 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004597 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004598 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4599 return -EFSCORRUPTED;
4600 }
4601
4602 if (XFS_FORCED_SHUTDOWN(mp))
4603 return -EIO;
4604
4605 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004606 error = xfs_iread_extents(tp, ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004607 if (error)
4608 return error;
4609 }
4610
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004611 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004612 /* make sure we only reflink into a hole. */
4613 ASSERT(got.br_startoff > bno);
4614 ASSERT(got.br_startoff - bno >= len);
4615 }
4616
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004617 ip->i_d.di_nblocks += len;
4618 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004619
4620 if (ifp->if_flags & XFS_IFBROOT) {
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004621 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004622 cur->bc_private.b.flags = 0;
4623 }
4624
4625 got.br_startoff = bno;
4626 got.br_startblock = startblock;
4627 got.br_blockcount = len;
Darrick J. Wong7644bd92018-05-14 06:34:35 -07004628 if (flags & XFS_BMAPI_PREALLOC)
4629 got.br_state = XFS_EXT_UNWRITTEN;
4630 else
4631 got.br_state = XFS_EXT_NORM;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004632
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004633 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
Brian Foster92f9da32018-07-11 22:26:28 -07004634 &cur, &got, &logflags, flags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004635 if (error)
4636 goto error0;
4637
Christoph Hellwigb101e332019-02-15 08:02:47 -08004638 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004639
4640error0:
4641 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4642 logflags &= ~XFS_ILOG_DEXT;
4643 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4644 logflags &= ~XFS_ILOG_DBROOT;
4645
4646 if (logflags)
4647 xfs_trans_log_inode(tp, ip, logflags);
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07004648 if (cur)
4649 xfs_btree_del_cursor(cur, error);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004650 return error;
4651}
4652
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004654 * When a delalloc extent is split (e.g., due to a hole punch), the original
4655 * indlen reservation must be shared across the two new extents that are left
4656 * behind.
4657 *
4658 * Given the original reservation and the worst case indlen for the two new
4659 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004660 * reservation fairly across the two new extents. If necessary, steal available
4661 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4662 * ores == 1). The number of stolen blocks is returned. The availability and
4663 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004664 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004665static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004666xfs_bmap_split_indlen(
4667 xfs_filblks_t ores, /* original res. */
4668 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004669 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4670 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004671{
4672 xfs_filblks_t len1 = *indlen1;
4673 xfs_filblks_t len2 = *indlen2;
4674 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004675 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004676 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004677
4678 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004679 * Steal as many blocks as we can to try and satisfy the worst case
4680 * indlen for both new extents.
4681 */
Brian Foster75d65362017-02-13 22:48:30 -08004682 if (ores < nres && avail)
4683 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4684 ores += stolen;
4685
4686 /* nothing else to do if we've satisfied the new reservation */
4687 if (ores >= nres)
4688 return stolen;
Brian Fosterd34999c2016-03-15 11:42:47 +11004689
4690 /*
Brian Foster75d65362017-02-13 22:48:30 -08004691 * We can't meet the total required reservation for the two extents.
4692 * Calculate the percent of the overall shortage between both extents
4693 * and apply this percentage to each of the requested indlen values.
4694 * This distributes the shortage fairly and reduces the chances that one
4695 * of the two extents is left with nothing when extents are repeatedly
4696 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004697 */
Brian Foster75d65362017-02-13 22:48:30 -08004698 resfactor = (ores * 100);
4699 do_div(resfactor, nres);
4700 len1 *= resfactor;
4701 do_div(len1, 100);
4702 len2 *= resfactor;
4703 do_div(len2, 100);
4704 ASSERT(len1 + len2 <= ores);
4705 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4706
4707 /*
4708 * Hand out the remainder to each extent. If one of the two reservations
4709 * is zero, we want to make sure that one gets a block first. The loop
4710 * below starts with len1, so hand len2 a block right off the bat if it
4711 * is zero.
4712 */
4713 ores -= (len1 + len2);
4714 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4715 if (ores && !len2 && *indlen2) {
4716 len2++;
4717 ores--;
4718 }
4719 while (ores) {
4720 if (len1 < *indlen1) {
4721 len1++;
4722 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004723 }
Brian Foster75d65362017-02-13 22:48:30 -08004724 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004725 break;
Brian Foster75d65362017-02-13 22:48:30 -08004726 if (len2 < *indlen2) {
4727 len2++;
4728 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004729 }
4730 }
4731
4732 *indlen1 = len1;
4733 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004734
4735 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004736}
4737
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004738int
4739xfs_bmap_del_extent_delay(
4740 struct xfs_inode *ip,
4741 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004742 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004743 struct xfs_bmbt_irec *got,
4744 struct xfs_bmbt_irec *del)
4745{
4746 struct xfs_mount *mp = ip->i_mount;
4747 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4748 struct xfs_bmbt_irec new;
4749 int64_t da_old, da_new, da_diff = 0;
4750 xfs_fileoff_t del_endoff, got_endoff;
4751 xfs_filblks_t got_indlen, new_indlen, stolen;
Christoph Hellwig060ea652017-10-19 11:02:29 -07004752 int state = xfs_bmap_fork_to_state(whichfork);
4753 int error = 0;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004754 bool isrt;
4755
4756 XFS_STATS_INC(mp, xs_del_exlist);
4757
4758 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4759 del_endoff = del->br_startoff + del->br_blockcount;
4760 got_endoff = got->br_startoff + got->br_blockcount;
4761 da_old = startblockval(got->br_startblock);
4762 da_new = 0;
4763
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004764 ASSERT(del->br_blockcount > 0);
4765 ASSERT(got->br_startoff <= del->br_startoff);
4766 ASSERT(got_endoff >= del_endoff);
4767
4768 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004769 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004770
4771 do_div(rtexts, mp->m_sb.sb_rextsize);
4772 xfs_mod_frextents(mp, rtexts);
4773 }
4774
4775 /*
4776 * Update the inode delalloc counter now and wait to update the
4777 * sb counters as we might have to borrow some blocks for the
4778 * indirect block accounting.
4779 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004780 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4781 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004782 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004783 if (error)
4784 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004785 ip->i_delayed_blks -= del->br_blockcount;
4786
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004787 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004788 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004789 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004790 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004791
Christoph Hellwig0173c682017-10-17 14:16:22 -07004792 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4793 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004794 /*
4795 * Matches the whole extent. Delete the entry.
4796 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004797 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004798 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004799 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004800 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004801 /*
4802 * Deleting the first part of the extent.
4803 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004804 got->br_startoff = del_endoff;
4805 got->br_blockcount -= del->br_blockcount;
4806 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4807 got->br_blockcount), da_old);
4808 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004809 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004810 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004811 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004812 /*
4813 * Deleting the last part of the extent.
4814 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004815 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4816 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4817 got->br_blockcount), da_old);
4818 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004819 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004820 break;
4821 case 0:
4822 /*
4823 * Deleting the middle of the extent.
4824 *
4825 * Distribute the original indlen reservation across the two new
4826 * extents. Steal blocks from the deleted extent if necessary.
4827 * Stealing blocks simply fudges the fdblocks accounting below.
4828 * Warn if either of the new indlen reservations is zero as this
4829 * can lead to delalloc problems.
4830 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004831 got->br_blockcount = del->br_startoff - got->br_startoff;
4832 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4833
4834 new.br_blockcount = got_endoff - del_endoff;
4835 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4836
4837 WARN_ON_ONCE(!got_indlen || !new_indlen);
4838 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4839 del->br_blockcount);
4840
4841 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004842
4843 new.br_startoff = del_endoff;
4844 new.br_state = got->br_state;
4845 new.br_startblock = nullstartblock((int)new_indlen);
4846
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004847 xfs_iext_update_extent(ip, state, icur, got);
4848 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004849 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004850
4851 da_new = got_indlen + new_indlen - stolen;
4852 del->br_blockcount -= stolen;
4853 break;
4854 }
4855
4856 ASSERT(da_old >= da_new);
4857 da_diff = da_old - da_new;
4858 if (!isrt)
4859 da_diff += del->br_blockcount;
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07004860 if (da_diff) {
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004861 xfs_mod_fdblocks(mp, da_diff, false);
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07004862 xfs_mod_delalloc(mp, -da_diff);
4863 }
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004864 return error;
4865}
4866
4867void
4868xfs_bmap_del_extent_cow(
4869 struct xfs_inode *ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004870 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004871 struct xfs_bmbt_irec *got,
4872 struct xfs_bmbt_irec *del)
4873{
4874 struct xfs_mount *mp = ip->i_mount;
4875 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4876 struct xfs_bmbt_irec new;
4877 xfs_fileoff_t del_endoff, got_endoff;
4878 int state = BMAP_COWFORK;
4879
4880 XFS_STATS_INC(mp, xs_del_exlist);
4881
4882 del_endoff = del->br_startoff + del->br_blockcount;
4883 got_endoff = got->br_startoff + got->br_blockcount;
4884
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004885 ASSERT(del->br_blockcount > 0);
4886 ASSERT(got->br_startoff <= del->br_startoff);
4887 ASSERT(got_endoff >= del_endoff);
4888 ASSERT(!isnullstartblock(got->br_startblock));
4889
4890 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004891 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004892 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004893 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004894
Christoph Hellwig0173c682017-10-17 14:16:22 -07004895 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4896 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004897 /*
4898 * Matches the whole extent. Delete the entry.
4899 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004900 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004901 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004902 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004903 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004904 /*
4905 * Deleting the first part of the extent.
4906 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004907 got->br_startoff = del_endoff;
4908 got->br_blockcount -= del->br_blockcount;
4909 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004910 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004911 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004912 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004913 /*
4914 * Deleting the last part of the extent.
4915 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004916 got->br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004917 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004918 break;
4919 case 0:
4920 /*
4921 * Deleting the middle of the extent.
4922 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004923 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004924
4925 new.br_startoff = del_endoff;
4926 new.br_blockcount = got_endoff - del_endoff;
4927 new.br_state = got->br_state;
4928 new.br_startblock = del->br_startblock + del->br_blockcount;
4929
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004930 xfs_iext_update_extent(ip, state, icur, got);
4931 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004932 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004933 break;
4934 }
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08004935 ip->i_delayed_blks -= del->br_blockcount;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004936}
4937
Brian Fostera9bd24a2016-03-15 11:42:46 +11004938/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11004939 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07004940 * after removing space.
Dave Chinner9e5987a72013-02-25 12:31:26 +11004941 */
4942STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07004943xfs_bmap_del_extent_real(
Dave Chinner9e5987a72013-02-25 12:31:26 +11004944 xfs_inode_t *ip, /* incore inode pointer */
4945 xfs_trans_t *tp, /* current transaction pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004946 struct xfs_iext_cursor *icur,
Dave Chinner9e5987a72013-02-25 12:31:26 +11004947 xfs_btree_cur_t *cur, /* if null, not a btree */
4948 xfs_bmbt_irec_t *del, /* data to remove from extents */
4949 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07004950 int whichfork, /* data or attr fork */
4951 int bflags) /* bmapi flags */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004952{
Dave Chinner9e5987a72013-02-25 12:31:26 +11004953 xfs_fsblock_t del_endblock=0; /* first block past del */
4954 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004955 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004956 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004957 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004958 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004959 xfs_fileoff_t got_endoff; /* first offset past got */
4960 int i; /* temp state */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07004961 struct xfs_ifork *ifp; /* inode fork pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004962 xfs_mount_t *mp; /* mount structure */
4963 xfs_filblks_t nblks; /* quota/sb block count */
4964 xfs_bmbt_irec_t new; /* new record to be inserted */
4965 /* REFERENCED */
4966 uint qfield; /* quota field to update */
Christoph Hellwig060ea652017-10-19 11:02:29 -07004967 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004968 struct xfs_bmbt_irec old;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004969
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004970 mp = ip->i_mount;
4971 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004972
Dave Chinner9e5987a72013-02-25 12:31:26 +11004973 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004974 ASSERT(del->br_blockcount > 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004975 xfs_iext_get_extent(ifp, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004976 ASSERT(got.br_startoff <= del->br_startoff);
4977 del_endoff = del->br_startoff + del->br_blockcount;
4978 got_endoff = got.br_startoff + got.br_blockcount;
4979 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004980 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a72013-02-25 12:31:26 +11004981 qfield = 0;
4982 error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004983
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004984 /*
4985 * If it's the case where the directory code is running with no block
4986 * reservation, and the deleted block is in the middle of its extent,
4987 * and the resulting insert of an extent would cause transformation to
4988 * btree format, then reject it. The calling code will then swap blocks
4989 * around instead. We have to do this now, rather than waiting for the
4990 * conversion to btree format, since the transaction will be dirty then.
4991 */
4992 if (tp->t_blk_res == 0 &&
4993 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4994 XFS_IFORK_NEXTENTS(ip, whichfork) >=
4995 XFS_IFORK_MAXEXT(ip, whichfork) &&
4996 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
4997 return -ENOSPC;
4998
4999 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005000 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5001 xfs_fsblock_t bno;
5002 xfs_filblks_t len;
Dave Chinner0703a8e2018-06-08 09:54:22 -07005003 xfs_extlen_t mod;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005004
Dave Chinner0703a8e2018-06-08 09:54:22 -07005005 bno = div_u64_rem(del->br_startblock, mp->m_sb.sb_rextsize,
5006 &mod);
5007 ASSERT(mod == 0);
5008 len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize,
5009 &mod);
5010 ASSERT(mod == 0);
5011
Christoph Hellwige1d75532017-10-17 14:16:21 -07005012 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5013 if (error)
5014 goto done;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005015 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005016 nblks = len * mp->m_sb.sb_rextsize;
5017 qfield = XFS_TRANS_DQ_RTBCOUNT;
5018 } else {
5019 do_fx = 1;
5020 nblks = del->br_blockcount;
5021 qfield = XFS_TRANS_DQ_BCOUNT;
5022 }
5023
5024 del_endblock = del->br_startblock + del->br_blockcount;
5025 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005026 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005027 if (error)
5028 goto done;
5029 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005030 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005031
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005032 if (got.br_startoff == del->br_startoff)
5033 state |= BMAP_LEFT_FILLING;
5034 if (got_endoff == del_endoff)
5035 state |= BMAP_RIGHT_FILLING;
5036
5037 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5038 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005039 /*
5040 * Matches the whole extent. Delete the entry.
5041 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005042 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005043 xfs_iext_prev(ifp, icur);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005044 XFS_IFORK_NEXT_SET(ip, whichfork,
5045 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5046 flags |= XFS_ILOG_CORE;
5047 if (!cur) {
5048 flags |= xfs_ilog_fext(whichfork);
5049 break;
5050 }
5051 if ((error = xfs_btree_delete(cur, &i)))
5052 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005053 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005054 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005055 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005056 /*
5057 * Deleting the first part of the extent.
5058 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005059 got.br_startoff = del_endoff;
5060 got.br_startblock = del_endblock;
5061 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005062 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005063 if (!cur) {
5064 flags |= xfs_ilog_fext(whichfork);
5065 break;
5066 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005067 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005068 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005069 goto done;
5070 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005071 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005072 /*
5073 * Deleting the last part of the extent.
5074 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005075 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005076 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005077 if (!cur) {
5078 flags |= xfs_ilog_fext(whichfork);
5079 break;
5080 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005081 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005082 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005083 goto done;
5084 break;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005085 case 0:
5086 /*
5087 * Deleting the middle of the extent.
5088 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005089 old = got;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07005090
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005091 got.br_blockcount = del->br_startoff - got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005092 xfs_iext_update_extent(ip, state, icur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005093
Dave Chinner9e5987a72013-02-25 12:31:26 +11005094 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005095 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005096 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005097 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005098
Christoph Hellwige1d75532017-10-17 14:16:21 -07005099 flags |= XFS_ILOG_CORE;
5100 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005101 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005102 if (error)
5103 goto done;
5104 error = xfs_btree_increment(cur, 0, &i);
5105 if (error)
5106 goto done;
5107 cur->bc_rec.b = new;
5108 error = xfs_btree_insert(cur, &i);
5109 if (error && error != -ENOSPC)
5110 goto done;
5111 /*
5112 * If get no-space back from btree insert, it tried a
5113 * split, and we have a zero block reservation. Fix up
5114 * our state and return the error.
5115 */
5116 if (error == -ENOSPC) {
5117 /*
5118 * Reset the cursor, don't trust it after any
5119 * insert operation.
5120 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005121 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005122 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005123 goto done;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005124 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5125 /*
5126 * Update the btree record back
5127 * to the original value.
5128 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005129 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005130 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005131 goto done;
5132 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005133 * Reset the extent record back
5134 * to the original value.
Dave Chinner9e5987a72013-02-25 12:31:26 +11005135 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005136 xfs_iext_update_extent(ip, state, icur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005137 flags = 0;
5138 error = -ENOSPC;
5139 goto done;
5140 }
5141 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5142 } else
5143 flags |= xfs_ilog_fext(whichfork);
5144 XFS_IFORK_NEXT_SET(ip, whichfork,
5145 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005146 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005147 xfs_iext_insert(ip, icur, &new, state);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005148 break;
5149 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005150
5151 /* remove reverse mapping */
Brian Foster0f37d172018-08-01 07:20:34 -07005152 error = xfs_rmap_unmap_extent(tp, ip, whichfork, del);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005153 if (error)
5154 goto done;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005155
Dave Chinner9e5987a72013-02-25 12:31:26 +11005156 /*
5157 * If we need to, add to list of extents to delete.
5158 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005159 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005160 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
Brian Foster0f37d172018-08-01 07:20:34 -07005161 error = xfs_refcount_decrease_extent(tp, del);
Darrick J. Wong62aab202016-10-03 09:11:23 -07005162 if (error)
5163 goto done;
Brian Fosterfcb762f2018-05-09 08:45:04 -07005164 } else {
Brian Foster0f37d172018-08-01 07:20:34 -07005165 __xfs_bmap_add_free(tp, del->br_startblock,
Brian Foster4e529332018-05-10 09:35:42 -07005166 del->br_blockcount, NULL,
5167 (bflags & XFS_BMAPI_NODISCARD) ||
5168 del->br_state == XFS_EXT_UNWRITTEN);
Brian Fosterfcb762f2018-05-09 08:45:04 -07005169 }
Darrick J. Wong62aab202016-10-03 09:11:23 -07005170 }
5171
Dave Chinner9e5987a72013-02-25 12:31:26 +11005172 /*
5173 * Adjust inode # blocks in the file.
5174 */
5175 if (nblks)
5176 ip->i_d.di_nblocks -= nblks;
5177 /*
5178 * Adjust quota data.
5179 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005180 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a72013-02-25 12:31:26 +11005181 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5182
Dave Chinner9e5987a72013-02-25 12:31:26 +11005183done:
5184 *logflagsp = flags;
5185 return error;
5186}
5187
5188/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 * Unmap (remove) blocks from a file.
5190 * If nexts is nonzero then the number of extents to remove is limited to
5191 * that value. If not all extents in the block range can be removed then
5192 * *done is set.
5193 */
5194int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005195__xfs_bunmapi(
Brian Fosterccd9d912018-07-11 22:26:13 -07005196 struct xfs_trans *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005198 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005199 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 int flags, /* misc flags */
Brian Foster2af52842018-07-11 22:26:25 -07005201 xfs_extnum_t nexts) /* number of extents max */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202{
Brian Fosterccd9d912018-07-11 22:26:13 -07005203 struct xfs_btree_cur *cur; /* bmap btree cursor */
5204 struct xfs_bmbt_irec del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 int error; /* error return value */
5206 xfs_extnum_t extno; /* extent number in list */
Brian Fosterccd9d912018-07-11 22:26:13 -07005207 struct xfs_bmbt_irec got; /* current extent record */
Christoph Hellwig3ba738d2018-07-17 16:51:50 -07005208 struct xfs_ifork *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 int isrt; /* freeing in rt area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 int logflags; /* transaction logging flags */
5211 xfs_extlen_t mod; /* rt extent offset */
Brian Fosterccd9d912018-07-11 22:26:13 -07005212 struct xfs_mount *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 int tmp_logflags; /* partial logging flags */
5214 int wasdel; /* was a delayed alloc extent */
5215 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005217 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005218 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005219 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005220 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005221 struct xfs_iext_cursor icur;
5222 bool done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005224 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005225
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005226 whichfork = xfs_bmapi_whichfork(flags);
5227 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 ifp = XFS_IFORK_PTR(ip, whichfork);
5229 if (unlikely(
5230 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5231 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5232 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5233 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005234 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 }
5236 mp = ip->i_mount;
5237 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005238 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005239
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005240 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 ASSERT(len > 0);
5242 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005243
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005244 /*
5245 * Guesstimate how many blocks we can unmap without running the risk of
5246 * blowing out the transaction with a mix of EFIs and reflink
5247 * adjustments.
5248 */
Darrick J. Wong8c57b882017-12-10 18:03:53 -08005249 if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005250 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5251 else
5252 max_len = len;
5253
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5255 (error = xfs_iread_extents(tp, ip, whichfork)))
5256 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005257 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005258 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 return 0;
5260 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005261 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005262 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwigdc560152017-10-23 16:32:39 -07005263 end = start + len;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005264
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005265 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
Christoph Hellwigdc560152017-10-23 16:32:39 -07005266 *rlen = 0;
5267 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 }
Christoph Hellwigdc560152017-10-23 16:32:39 -07005269 end--;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005270
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 logflags = 0;
5272 if (ifp->if_flags & XFS_IFBROOT) {
5273 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005274 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 cur->bc_private.b.flags = 0;
5276 } else
5277 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005278
5279 if (isrt) {
5280 /*
5281 * Synchronize by locking the bitmap inode.
5282 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005283 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005284 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005285 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5286 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005287 }
5288
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 extno = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005290 while (end != (xfs_fileoff_t)-1 && end >= start &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005291 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005293 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 * Just back up to the previous extent, if so.
5295 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005296 if (got.br_startoff > end &&
5297 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5298 done = true;
5299 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 }
5301 /*
5302 * Is the last block of this extent before the range
5303 * we're supposed to delete? If so, we're done.
5304 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005305 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005307 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 break;
5309 /*
5310 * Then deal with the (possibly delayed) allocated space
5311 * we found.
5312 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005314 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005315
5316 /*
5317 * Make sure we don't touch multiple AGF headers out of order
5318 * in a single transaction, as that could cause AB-BA deadlocks.
5319 */
5320 if (!wasdel) {
5321 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5322 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5323 break;
5324 prev_agno = agno;
5325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 if (got.br_startoff < start) {
5327 del.br_startoff = start;
5328 del.br_blockcount -= start - got.br_startoff;
5329 if (!wasdel)
5330 del.br_startblock += start - got.br_startoff;
5331 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005332 if (del.br_startoff + del.br_blockcount > end + 1)
5333 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005334
5335 /* How much can we safely unmap? */
5336 if (max_len < del.br_blockcount) {
5337 del.br_startoff += del.br_blockcount - max_len;
5338 if (!wasdel)
5339 del.br_startblock += del.br_blockcount - max_len;
5340 del.br_blockcount = max_len;
5341 }
5342
Dave Chinner0703a8e2018-06-08 09:54:22 -07005343 if (!isrt)
5344 goto delete;
5345
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 sum = del.br_startblock + del.br_blockcount;
Dave Chinner0703a8e2018-06-08 09:54:22 -07005347 div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod);
5348 if (mod) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 /*
5350 * Realtime extent not lined up at the end.
5351 * The extent could have been split into written
5352 * and unwritten pieces, or we could just be
5353 * unmapping part of it. But we can't really
5354 * get rid of part of a realtime extent.
5355 */
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11005356 if (del.br_state == XFS_EXT_UNWRITTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 /*
5358 * This piece is unwritten, or we're not
5359 * using unwritten extents. Skip over it.
5360 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005361 ASSERT(end >= mod);
5362 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 del.br_blockcount : mod;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005364 if (end < got.br_startoff &&
5365 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5366 done = true;
5367 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 }
5369 continue;
5370 }
5371 /*
5372 * It's written, turn it unwritten.
5373 * This is better than zeroing it.
5374 */
5375 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005376 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 /*
5378 * If this spans a realtime extent boundary,
5379 * chop it back to the start of the one we end at.
5380 */
5381 if (del.br_blockcount > mod) {
5382 del.br_startoff += del.br_blockcount - mod;
5383 del.br_startblock += del.br_blockcount - mod;
5384 del.br_blockcount = mod;
5385 }
5386 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005387 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005388 whichfork, &icur, &cur, &del,
Brian Foster92f9da32018-07-11 22:26:28 -07005389 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 if (error)
5391 goto error0;
5392 goto nodelete;
5393 }
Dave Chinner0703a8e2018-06-08 09:54:22 -07005394 div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod);
5395 if (mod) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 /*
5397 * Realtime extent is lined up at the end but not
5398 * at the front. We'll get rid of full extents if
5399 * we can.
5400 */
5401 mod = mp->m_sb.sb_rextsize - mod;
5402 if (del.br_blockcount > mod) {
5403 del.br_blockcount -= mod;
5404 del.br_startoff += mod;
5405 del.br_startblock += mod;
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11005406 } else if (del.br_startoff == start &&
5407 (del.br_state == XFS_EXT_UNWRITTEN ||
5408 tp->t_blk_res == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 /*
5410 * Can't make it unwritten. There isn't
5411 * a full extent here so just skip it.
5412 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005413 ASSERT(end >= del.br_blockcount);
5414 end -= del.br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005415 if (got.br_startoff > end &&
5416 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5417 done = true;
5418 break;
5419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 continue;
5421 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005422 struct xfs_bmbt_irec prev;
5423
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 /*
5425 * This one is already unwritten.
5426 * It must have a written left neighbor.
5427 * Unwrite the killed part of that one and
5428 * try again.
5429 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005430 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5431 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005433 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 ASSERT(del.br_startblock ==
5435 prev.br_startblock + prev.br_blockcount);
5436 if (prev.br_startoff < start) {
5437 mod = start - prev.br_startoff;
5438 prev.br_blockcount -= mod;
5439 prev.br_startblock += mod;
5440 prev.br_startoff = start;
5441 }
5442 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005443 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005444 ip, whichfork, &icur, &cur,
Brian Foster92f9da32018-07-11 22:26:28 -07005445 &prev, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 if (error)
5447 goto error0;
5448 goto nodelete;
5449 } else {
5450 ASSERT(del.br_state == XFS_EXT_NORM);
5451 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005452 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005453 ip, whichfork, &icur, &cur,
Brian Foster92f9da32018-07-11 22:26:28 -07005454 &del, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 if (error)
5456 goto error0;
5457 goto nodelete;
5458 }
5459 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005460
Dave Chinner0703a8e2018-06-08 09:54:22 -07005461delete:
Brian Fosterb2706a02016-03-15 11:42:46 +11005462 if (wasdel) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005463 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005464 &got, &del);
5465 } else {
Brian Foster81ba8f32018-07-11 22:26:16 -07005466 error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5467 &del, &tmp_logflags, whichfork,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005468 flags);
5469 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005470 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005471
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 if (error)
5473 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005474
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005475 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005476 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 /*
5479 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005481 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005482 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5483 (got.br_startoff > end &&
5484 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5485 done = true;
5486 break;
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 extno++;
5489 }
5490 }
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005491 if (done || end == (xfs_fileoff_t)-1 || end < start)
Darrick J. Wong44535932016-10-03 09:11:29 -07005492 *rlen = 0;
5493 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005494 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005495
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 /*
5497 * Convert to a btree if necessary.
5498 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005499 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07005501 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5502 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 logflags |= tmp_logflags;
Christoph Hellwigb101e332019-02-15 08:02:47 -08005504 } else {
5505 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 }
Christoph Hellwigb101e332019-02-15 08:02:47 -08005508
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509error0:
5510 /*
5511 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005512 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005514 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005516 logflags &= ~xfs_ilog_fext(whichfork);
5517 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005519 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 /*
5521 * Log inode even in the error case, if the transaction
5522 * is dirty we'll need to shut down the filesystem.
5523 */
5524 if (logflags)
5525 xfs_trans_log_inode(tp, ip, logflags);
5526 if (cur) {
Brian Fostercf612de2018-07-11 22:26:29 -07005527 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 cur->bc_private.b.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07005529 xfs_btree_del_cursor(cur, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 }
5531 return error;
5532}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005533
Darrick J. Wong44535932016-10-03 09:11:29 -07005534/* Unmap a range of a file. */
5535int
5536xfs_bunmapi(
5537 xfs_trans_t *tp,
5538 struct xfs_inode *ip,
5539 xfs_fileoff_t bno,
5540 xfs_filblks_t len,
5541 int flags,
5542 xfs_extnum_t nexts,
Darrick J. Wong44535932016-10-03 09:11:29 -07005543 int *done)
5544{
5545 int error;
5546
Brian Foster2af52842018-07-11 22:26:25 -07005547 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
Darrick J. Wong44535932016-10-03 09:11:29 -07005548 *done = (len == 0);
5549 return error;
5550}
5551
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005552/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005553 * Determine whether an extent shift can be accomplished by a merge with the
5554 * extent that precedes the target hole of the shift.
5555 */
5556STATIC bool
5557xfs_bmse_can_merge(
5558 struct xfs_bmbt_irec *left, /* preceding extent */
5559 struct xfs_bmbt_irec *got, /* current extent to shift */
5560 xfs_fileoff_t shift) /* shift fsb */
5561{
5562 xfs_fileoff_t startoff;
5563
5564 startoff = got->br_startoff - shift;
5565
5566 /*
5567 * The extent, once shifted, must be adjacent in-file and on-disk with
5568 * the preceding extent.
5569 */
5570 if ((left->br_startoff + left->br_blockcount != startoff) ||
5571 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5572 (left->br_state != got->br_state) ||
5573 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5574 return false;
5575
5576 return true;
5577}
5578
5579/*
5580 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5581 * hole in the file. If an extent shift would result in the extent being fully
5582 * adjacent to the extent that currently precedes the hole, we can merge with
5583 * the preceding extent rather than do the shift.
5584 *
5585 * This function assumes the caller has verified a shift-by-merge is possible
5586 * with the provided extents via xfs_bmse_can_merge().
5587 */
5588STATIC int
5589xfs_bmse_merge(
Brian Foster0f37d172018-08-01 07:20:34 -07005590 struct xfs_trans *tp,
Brian Fosterddb19e32014-09-23 15:38:09 +10005591 struct xfs_inode *ip,
5592 int whichfork,
5593 xfs_fileoff_t shift, /* shift fsb */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005594 struct xfs_iext_cursor *icur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005595 struct xfs_bmbt_irec *got, /* extent to shift */
5596 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005597 struct xfs_btree_cur *cur,
Brian Foster0f37d172018-08-01 07:20:34 -07005598 int *logflags) /* output */
Brian Fosterddb19e32014-09-23 15:38:09 +10005599{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005600 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005601 xfs_filblks_t blockcount;
5602 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005603 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005604
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005605 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005606
5607 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5608 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005609 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005610
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005611 new = *left;
5612 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005613
5614 /*
5615 * Update the on-disk extent count, the btree if necessary and log the
5616 * inode.
5617 */
5618 XFS_IFORK_NEXT_SET(ip, whichfork,
5619 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5620 *logflags |= XFS_ILOG_CORE;
5621 if (!cur) {
5622 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005623 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005624 }
5625
5626 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005627 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005628 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005629 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005630 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005631
5632 error = xfs_btree_delete(cur, &i);
5633 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005634 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005635 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005636
5637 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005638 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005639 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005640 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005641 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005642
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005643 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005644 if (error)
5645 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005646
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005647done:
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005648 xfs_iext_remove(ip, icur, 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005649 xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur);
5650 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5651 &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005652
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005653 /* update reverse mapping. rmap functions merge the rmaps for us */
Brian Foster0f37d172018-08-01 07:20:34 -07005654 error = xfs_rmap_unmap_extent(tp, ip, whichfork, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005655 if (error)
5656 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005657 memcpy(&new, got, sizeof(new));
5658 new.br_startoff = left->br_startoff + left->br_blockcount;
Brian Foster0f37d172018-08-01 07:20:34 -07005659 return xfs_rmap_map_extent(tp, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005660}
5661
Christoph Hellwigbf806282017-10-19 11:07:34 -07005662static int
5663xfs_bmap_shift_update_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07005664 struct xfs_trans *tp,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005665 struct xfs_inode *ip,
5666 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005667 struct xfs_iext_cursor *icur,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005668 struct xfs_bmbt_irec *got,
5669 struct xfs_btree_cur *cur,
5670 int *logflags,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005671 xfs_fileoff_t startoff)
Brian Fostera979bdf2014-09-23 15:39:04 +10005672{
Christoph Hellwigbf806282017-10-19 11:07:34 -07005673 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005674 struct xfs_bmbt_irec prev = *got;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005675 int error, i;
Brian Fostera979bdf2014-09-23 15:39:04 +10005676
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005677 *logflags |= XFS_ILOG_CORE;
5678
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005679 got->br_startoff = startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005680
5681 if (cur) {
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005682 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005683 if (error)
5684 return error;
5685 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5686
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005687 error = xfs_bmbt_update(cur, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005688 if (error)
5689 return error;
5690 } else {
5691 *logflags |= XFS_ILOG_DEXT;
5692 }
5693
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005694 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5695 got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005696
Darrick J. Wong9c194642016-08-03 12:16:05 +10005697 /* update reverse mapping */
Brian Foster0f37d172018-08-01 07:20:34 -07005698 error = xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005699 if (error)
5700 return error;
Brian Foster0f37d172018-08-01 07:20:34 -07005701 return xfs_rmap_map_extent(tp, ip, whichfork, got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005702}
5703
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005704int
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005705xfs_bmap_collapse_extents(
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005706 struct xfs_trans *tp,
5707 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005708 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005709 xfs_fileoff_t offset_shift_fsb,
Brian Foster333f9502018-07-11 22:26:27 -07005710 bool *done)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005711{
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005712 int whichfork = XFS_DATA_FORK;
5713 struct xfs_mount *mp = ip->i_mount;
5714 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5715 struct xfs_btree_cur *cur = NULL;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005716 struct xfs_bmbt_irec got, prev;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005717 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005718 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005719 int error = 0;
5720 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005721
5722 if (unlikely(XFS_TEST_ERROR(
5723 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5724 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005725 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005726 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005727 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005728 }
5729
5730 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005731 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005732
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005733 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005734
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005735 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005736 error = xfs_iread_extents(tp, ip, whichfork);
5737 if (error)
5738 return error;
5739 }
5740
Brian Fosterddb19e32014-09-23 15:38:09 +10005741 if (ifp->if_flags & XFS_IFBROOT) {
5742 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Brian Fosterddb19e32014-09-23 15:38:09 +10005743 cur->bc_private.b.flags = 0;
5744 }
5745
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005746 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005747 *done = true;
5748 goto del_cursor;
5749 }
Eric Sandeend41c6172017-11-27 18:23:32 -08005750 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5751 del_cursor);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005752
Christoph Hellwigbf806282017-10-19 11:07:34 -07005753 new_startoff = got.br_startoff - offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005754 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005755 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5756 error = -EINVAL;
5757 goto del_cursor;
5758 }
5759
Christoph Hellwigbf806282017-10-19 11:07:34 -07005760 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
Brian Foster0f37d172018-08-01 07:20:34 -07005761 error = xfs_bmse_merge(tp, ip, whichfork,
5762 offset_shift_fsb, &icur, &got, &prev,
5763 cur, &logflags);
Christoph Hellwigbf806282017-10-19 11:07:34 -07005764 if (error)
5765 goto del_cursor;
5766 goto done;
5767 }
5768 } else {
5769 if (got.br_startoff < offset_shift_fsb) {
5770 error = -EINVAL;
5771 goto del_cursor;
5772 }
5773 }
5774
Brian Foster0f37d172018-08-01 07:20:34 -07005775 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5776 cur, &logflags, new_startoff);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005777 if (error)
5778 goto del_cursor;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005779
Christoph Hellwig42630362017-11-03 10:34:41 -07005780done:
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005781 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5782 *done = true;
5783 goto del_cursor;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005784 }
5785
Christoph Hellwigbf806282017-10-19 11:07:34 -07005786 *next_fsb = got.br_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005787del_cursor:
5788 if (cur)
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07005789 xfs_btree_del_cursor(cur, error);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005790 if (logflags)
5791 xfs_trans_log_inode(tp, ip, logflags);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005792 return error;
5793}
5794
Darrick J. Wongf62cb482018-06-21 23:26:57 -07005795/* Make sure we won't be right-shifting an extent past the maximum bound. */
5796int
5797xfs_bmap_can_insert_extents(
5798 struct xfs_inode *ip,
5799 xfs_fileoff_t off,
5800 xfs_fileoff_t shift)
5801{
5802 struct xfs_bmbt_irec got;
5803 int is_empty;
5804 int error = 0;
5805
5806 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5807
5808 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5809 return -EIO;
5810
5811 xfs_ilock(ip, XFS_ILOCK_EXCL);
5812 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
5813 if (!error && !is_empty && got.br_startoff >= off &&
5814 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
5815 error = -EINVAL;
5816 xfs_iunlock(ip, XFS_ILOCK_EXCL);
5817
5818 return error;
5819}
5820
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005821int
5822xfs_bmap_insert_extents(
5823 struct xfs_trans *tp,
5824 struct xfs_inode *ip,
5825 xfs_fileoff_t *next_fsb,
5826 xfs_fileoff_t offset_shift_fsb,
5827 bool *done,
Brian Foster333f9502018-07-11 22:26:27 -07005828 xfs_fileoff_t stop_fsb)
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005829{
5830 int whichfork = XFS_DATA_FORK;
5831 struct xfs_mount *mp = ip->i_mount;
5832 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5833 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005834 struct xfs_bmbt_irec got, next;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005835 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005836 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005837 int error = 0;
5838 int logflags = 0;
5839
5840 if (unlikely(XFS_TEST_ERROR(
5841 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5842 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5843 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5844 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
5845 return -EFSCORRUPTED;
5846 }
5847
5848 if (XFS_FORCED_SHUTDOWN(mp))
5849 return -EIO;
5850
5851 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5852
5853 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5854 error = xfs_iread_extents(tp, ip, whichfork);
5855 if (error)
5856 return error;
5857 }
5858
5859 if (ifp->if_flags & XFS_IFBROOT) {
5860 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005861 cur->bc_private.b.flags = 0;
5862 }
5863
Namjae Jeona904b1c2015-03-25 15:08:56 +11005864 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005865 xfs_iext_last(ifp, &icur);
5866 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005867 stop_fsb > got.br_startoff) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005868 *done = true;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005869 goto del_cursor;
5870 }
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005871 } else {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005872 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005873 *done = true;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005874 goto del_cursor;
5875 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005876 }
Eric Sandeend41c6172017-11-27 18:23:32 -08005877 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5878 del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005879
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005880 if (stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005881 error = -EIO;
5882 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005883 }
5884
Christoph Hellwigbf806282017-10-19 11:07:34 -07005885 new_startoff = got.br_startoff + offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005886 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005887 if (new_startoff + got.br_blockcount > next.br_startoff) {
5888 error = -EINVAL;
5889 goto del_cursor;
5890 }
5891
5892 /*
5893 * Unlike a left shift (which involves a hole punch), a right
5894 * shift does not modify extent neighbors in any way. We should
5895 * never find mergeable extents in this scenario. Check anyways
5896 * and warn if we encounter two extents that could be one.
5897 */
5898 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5899 WARN_ON_ONCE(1);
5900 }
5901
Brian Foster0f37d172018-08-01 07:20:34 -07005902 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5903 cur, &logflags, new_startoff);
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005904 if (error)
5905 goto del_cursor;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005906
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005907 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005908 stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005909 *done = true;
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005910 goto del_cursor;
5911 }
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005912
5913 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005914del_cursor:
5915 if (cur)
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07005916 xfs_btree_del_cursor(cur, error);
Brian Fosterca446d82014-09-02 12:12:53 +10005917 if (logflags)
5918 xfs_trans_log_inode(tp, ip, logflags);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005919 return error;
5920}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005921
5922/*
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005923 * Splits an extent into two extents at split_fsb block such that it is the
5924 * first block of the current_ext. @ext is a target extent to be split.
5925 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5926 * hole or the first block of extents, just return 0.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005927 */
5928STATIC int
5929xfs_bmap_split_extent_at(
5930 struct xfs_trans *tp,
5931 struct xfs_inode *ip,
Brian Foster4b77a082018-07-11 22:26:27 -07005932 xfs_fileoff_t split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005933{
5934 int whichfork = XFS_DATA_FORK;
5935 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005936 struct xfs_bmbt_irec got;
5937 struct xfs_bmbt_irec new; /* split extent */
5938 struct xfs_mount *mp = ip->i_mount;
5939 struct xfs_ifork *ifp;
5940 xfs_fsblock_t gotblkcnt; /* new block count for got */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005941 struct xfs_iext_cursor icur;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005942 int error = 0;
5943 int logflags = 0;
5944 int i = 0;
5945
5946 if (unlikely(XFS_TEST_ERROR(
5947 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5948 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005949 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11005950 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5951 XFS_ERRLEVEL_LOW, mp);
5952 return -EFSCORRUPTED;
5953 }
5954
5955 if (XFS_FORCED_SHUTDOWN(mp))
5956 return -EIO;
5957
5958 ifp = XFS_IFORK_PTR(ip, whichfork);
5959 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5960 /* Read in all the extents */
5961 error = xfs_iread_extents(tp, ip, whichfork);
5962 if (error)
5963 return error;
5964 }
5965
5966 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005967 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005968 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005969 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005970 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005971 return 0;
5972
5973 gotblkcnt = split_fsb - got.br_startoff;
5974 new.br_startoff = split_fsb;
5975 new.br_startblock = got.br_startblock + gotblkcnt;
5976 new.br_blockcount = got.br_blockcount - gotblkcnt;
5977 new.br_state = got.br_state;
5978
5979 if (ifp->if_flags & XFS_IFBROOT) {
5980 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005981 cur->bc_private.b.flags = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005982 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005983 if (error)
5984 goto del_cursor;
5985 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5986 }
5987
Namjae Jeona904b1c2015-03-25 15:08:56 +11005988 got.br_blockcount = gotblkcnt;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005989 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
5990 &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005991
5992 logflags = XFS_ILOG_CORE;
5993 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005994 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005995 if (error)
5996 goto del_cursor;
5997 } else
5998 logflags |= XFS_ILOG_DEXT;
5999
6000 /* Add new extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07006001 xfs_iext_next(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07006002 xfs_iext_insert(ip, &icur, &new, 0);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006003 XFS_IFORK_NEXT_SET(ip, whichfork,
6004 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6005
6006 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07006007 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006008 if (error)
6009 goto del_cursor;
6010 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006011 error = xfs_btree_insert(cur, &i);
6012 if (error)
6013 goto del_cursor;
6014 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6015 }
6016
6017 /*
6018 * Convert to a btree if necessary.
6019 */
6020 if (xfs_bmap_needs_btree(ip, whichfork)) {
6021 int tmp_logflags; /* partial log flag return val */
6022
6023 ASSERT(cur == NULL);
Brian Foster280253d2018-07-11 22:26:29 -07006024 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
6025 &tmp_logflags, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006026 logflags |= tmp_logflags;
6027 }
6028
6029del_cursor:
6030 if (cur) {
6031 cur->bc_private.b.allocated = 0;
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -07006032 xfs_btree_del_cursor(cur, error);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006033 }
6034
6035 if (logflags)
6036 xfs_trans_log_inode(tp, ip, logflags);
6037 return error;
6038}
6039
6040int
6041xfs_bmap_split_extent(
6042 struct xfs_inode *ip,
6043 xfs_fileoff_t split_fsb)
6044{
6045 struct xfs_mount *mp = ip->i_mount;
6046 struct xfs_trans *tp;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006047 int error;
6048
Christoph Hellwig253f4912016-04-06 09:19:55 +10006049 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6050 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6051 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006052 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006053
6054 xfs_ilock(ip, XFS_ILOCK_EXCL);
6055 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6056
Brian Foster4b77a082018-07-11 22:26:27 -07006057 error = xfs_bmap_split_extent_at(tp, ip, split_fsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006058 if (error)
6059 goto out;
6060
Christoph Hellwig70393312015-06-04 13:48:08 +10006061 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006062
6063out:
Christoph Hellwig4906e212015-06-04 13:47:56 +10006064 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006065 return error;
6066}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006067
6068/* Deferred mapping is only for real extents in the data fork. */
6069static bool
6070xfs_bmap_is_update_needed(
6071 struct xfs_bmbt_irec *bmap)
6072{
6073 return bmap->br_startblock != HOLESTARTBLOCK &&
6074 bmap->br_startblock != DELAYSTARTBLOCK;
6075}
6076
6077/* Record a bmap intent. */
6078static int
6079__xfs_bmap_add(
Brian Foster0f37d172018-08-01 07:20:34 -07006080 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006081 enum xfs_bmap_intent_type type,
6082 struct xfs_inode *ip,
6083 int whichfork,
6084 struct xfs_bmbt_irec *bmap)
6085{
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006086 struct xfs_bmap_intent *bi;
6087
Brian Foster0f37d172018-08-01 07:20:34 -07006088 trace_xfs_bmap_defer(tp->t_mountp,
6089 XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock),
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006090 type,
Brian Foster0f37d172018-08-01 07:20:34 -07006091 XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock),
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006092 ip->i_ino, whichfork,
6093 bmap->br_startoff,
6094 bmap->br_blockcount,
6095 bmap->br_state);
6096
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07006097 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_NOFS);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006098 INIT_LIST_HEAD(&bi->bi_list);
6099 bi->bi_type = type;
6100 bi->bi_owner = ip;
6101 bi->bi_whichfork = whichfork;
6102 bi->bi_bmap = *bmap;
6103
Brian Foster0f37d172018-08-01 07:20:34 -07006104 xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006105 return 0;
6106}
6107
6108/* Map an extent into a file. */
6109int
6110xfs_bmap_map_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07006111 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006112 struct xfs_inode *ip,
6113 struct xfs_bmbt_irec *PREV)
6114{
6115 if (!xfs_bmap_is_update_needed(PREV))
6116 return 0;
6117
Brian Foster0f37d172018-08-01 07:20:34 -07006118 return __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, XFS_DATA_FORK, PREV);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006119}
6120
6121/* Unmap an extent out of a file. */
6122int
6123xfs_bmap_unmap_extent(
Brian Foster0f37d172018-08-01 07:20:34 -07006124 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006125 struct xfs_inode *ip,
6126 struct xfs_bmbt_irec *PREV)
6127{
6128 if (!xfs_bmap_is_update_needed(PREV))
6129 return 0;
6130
Brian Foster0f37d172018-08-01 07:20:34 -07006131 return __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, XFS_DATA_FORK, PREV);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006132}
6133
6134/*
6135 * Process one of the deferred bmap operations. We pass back the
6136 * btree cursor to maintain our lock on the bmapbt between calls.
6137 */
6138int
6139xfs_bmap_finish_one(
6140 struct xfs_trans *tp,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006141 struct xfs_inode *ip,
6142 enum xfs_bmap_intent_type type,
6143 int whichfork,
6144 xfs_fileoff_t startoff,
6145 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006146 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006147 xfs_exntst_t state)
6148{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006149 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006150
Brian Foster37283792018-07-11 22:26:23 -07006151 ASSERT(tp->t_firstblock == NULLFSBLOCK);
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006152
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006153 trace_xfs_bmap_deferred(tp->t_mountp,
6154 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6155 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006156 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006157
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006158 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006159 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006160
6161 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006162 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006163 return -EIO;
6164
6165 switch (type) {
6166 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006167 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Brian Fosterff3edf22018-07-11 22:26:14 -07006168 startblock, 0);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006169 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006170 break;
6171 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006172 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
Brian Foster2af52842018-07-11 22:26:25 -07006173 XFS_BMAPI_REMAP, 1);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006174 break;
6175 default:
6176 ASSERT(0);
6177 error = -EFSCORRUPTED;
6178 }
6179
6180 return error;
6181}
Darrick J. Wong30b09842018-03-23 10:06:52 -07006182
6183/* Check that an inode's extent does not have invalid flags or bad ranges. */
6184xfs_failaddr_t
6185xfs_bmap_validate_extent(
6186 struct xfs_inode *ip,
6187 int whichfork,
6188 struct xfs_bmbt_irec *irec)
6189{
6190 struct xfs_mount *mp = ip->i_mount;
6191 xfs_fsblock_t endfsb;
6192 bool isrt;
6193
6194 isrt = XFS_IS_REALTIME_INODE(ip);
6195 endfsb = irec->br_startblock + irec->br_blockcount - 1;
6196 if (isrt) {
6197 if (!xfs_verify_rtbno(mp, irec->br_startblock))
6198 return __this_address;
6199 if (!xfs_verify_rtbno(mp, endfsb))
6200 return __this_address;
6201 } else {
6202 if (!xfs_verify_fsbno(mp, irec->br_startblock))
6203 return __this_address;
6204 if (!xfs_verify_fsbno(mp, endfsb))
6205 return __this_address;
6206 if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) !=
6207 XFS_FSB_TO_AGNO(mp, endfsb))
6208 return __this_address;
6209 }
Christoph Hellwigdaa79ba2018-10-18 17:18:58 +11006210 if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6211 return __this_address;
Darrick J. Wong30b09842018-03-23 10:06:52 -07006212 return NULL;
6213}