blob: da73c1a011d36992aefbe21ef4eead3569f55c95 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11007#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11008#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +11009#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110012#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_sb.h"
Dave Chinnerf5ea1102013-04-24 18:58:02 +100014#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100015#include "xfs_defer.h"
Dave Chinner57062782013-10-15 09:17:51 +110016#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110017#include "xfs_da_btree.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100018#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_btree.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_extfree_item.h"
24#include "xfs_alloc.h"
25#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100026#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110027#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_rtalloc.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070029#include "xfs_errortag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_quota.h"
32#include "xfs_trans_space.h"
33#include "xfs_buf_item.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000034#include "xfs_trace.h"
Dave Chinner19de7352013-04-03 16:11:18 +110035#include "xfs_symlink.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110036#include "xfs_attr_leaf.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110037#include "xfs_filestream.h"
Darrick J. Wong340785c2016-08-03 11:33:42 +100038#include "xfs_rmap.h"
Darrick J. Wong3fd129b2016-09-19 10:30:52 +100039#include "xfs_ag_resv.h"
Darrick J. Wong62aab202016-10-03 09:11:23 -070040#include "xfs_refcount.h"
Brian Foster974ae922016-11-28 14:57:42 +110041#include "xfs_icache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044kmem_zone_t *xfs_bmap_free_item_zone;
45
46/*
Dave Chinner9e5987a72013-02-25 12:31:26 +110047 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
Dave Chinner9e5987a72013-02-25 12:31:26 +110051 * Compute and fill in the value of the maximum depth of a bmap btree
52 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 */
Dave Chinner9e5987a72013-02-25 12:31:26 +110054void
55xfs_bmap_compute_maxlevels(
56 xfs_mount_t *mp, /* file system mount structure */
57 int whichfork) /* data or attr fork */
58{
59 int level; /* btree level */
60 uint maxblocks; /* max blocks at this level */
61 uint maxleafents; /* max leaf entries possible */
62 int maxrootrecs; /* max records in root block */
63 int minleafrecs; /* min records in leaf block */
64 int minnoderecs; /* min records in node block */
65 int sz; /* root block size */
66
67 /*
68 * The maximum number of extents in a file, hence the maximum
69 * number of leaf entries, is controlled by the type of di_nextents
70 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
71 * (a signed 16-bit number, xfs_aextnum_t).
72 *
73 * Note that we can no longer assume that if we are in ATTR1 that
74 * the fork offset of all the inodes will be
75 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
76 * with ATTR2 and then mounted back with ATTR1, keeping the
77 * di_forkoff's fixed but probably at various positions. Therefore,
78 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
79 * of a minimum size available.
80 */
81 if (whichfork == XFS_DATA_FORK) {
82 maxleafents = MAXEXTNUM;
83 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
84 } else {
85 maxleafents = MAXAEXTNUM;
86 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
87 }
Eric Sandeen152d93b2014-04-14 18:58:51 +100088 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +110089 minleafrecs = mp->m_bmap_dmnr[0];
90 minnoderecs = mp->m_bmap_dmnr[1];
91 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
92 for (level = 1; maxblocks > 1; level++) {
93 if (maxblocks <= maxrootrecs)
94 maxblocks = 1;
95 else
96 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
97 }
98 mp->m_bm_maxlevels[whichfork] = level;
99}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100101STATIC int /* error */
102xfs_bmbt_lookup_eq(
103 struct xfs_btree_cur *cur,
Christoph Hellwige16cf9b2017-10-17 14:16:26 -0700104 struct xfs_bmbt_irec *irec,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100105 int *stat) /* success/failure */
106{
Christoph Hellwige16cf9b2017-10-17 14:16:26 -0700107 cur->bc_rec.b = *irec;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100108 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
109}
110
111STATIC int /* error */
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700112xfs_bmbt_lookup_first(
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100113 struct xfs_btree_cur *cur,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100114 int *stat) /* success/failure */
115{
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700116 cur->bc_rec.b.br_startoff = 0;
117 cur->bc_rec.b.br_startblock = 0;
118 cur->bc_rec.b.br_blockcount = 0;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100119 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
120}
121
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100122/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000123 * Check if the inode needs to be converted to btree format.
124 */
125static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
126{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700127 return whichfork != XFS_COW_FORK &&
128 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000129 XFS_IFORK_NEXTENTS(ip, whichfork) >
130 XFS_IFORK_MAXEXT(ip, whichfork);
131}
132
133/*
134 * Check if the inode should be converted to extent format.
135 */
136static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
137{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700138 return whichfork != XFS_COW_FORK &&
139 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000140 XFS_IFORK_NEXTENTS(ip, whichfork) <=
141 XFS_IFORK_MAXEXT(ip, whichfork);
142}
143
144/*
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700145 * Update the record referred to by cur to the value given by irec
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100146 * This either works (return 0) or gets an EFSCORRUPTED error.
147 */
148STATIC int
149xfs_bmbt_update(
150 struct xfs_btree_cur *cur,
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700151 struct xfs_bmbt_irec *irec)
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100152{
153 union xfs_btree_rec rec;
154
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700155 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100156 return xfs_btree_update(cur, &rec);
157}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*
Dave Chinner9e5987a72013-02-25 12:31:26 +1100160 * Compute the worst-case number of indirect blocks that will be used
161 * for ip's delayed extent of length "len".
162 */
163STATIC xfs_filblks_t
164xfs_bmap_worst_indlen(
165 xfs_inode_t *ip, /* incore inode pointer */
166 xfs_filblks_t len) /* delayed extent length */
167{
168 int level; /* btree level number */
169 int maxrecs; /* maximum record count at this level */
170 xfs_mount_t *mp; /* mount structure */
171 xfs_filblks_t rval; /* return value */
172
173 mp = ip->i_mount;
174 maxrecs = mp->m_bmap_dmxr[0];
175 for (level = 0, rval = 0;
176 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
177 level++) {
178 len += maxrecs - 1;
179 do_div(len, maxrecs);
180 rval += len;
Darrick J. Wong5e5c9432017-09-18 09:41:17 -0700181 if (len == 1)
182 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
Dave Chinner9e5987a72013-02-25 12:31:26 +1100183 level - 1;
184 if (level == 0)
185 maxrecs = mp->m_bmap_dmxr[1];
186 }
187 return rval;
188}
189
190/*
191 * Calculate the default attribute fork offset for newly created inodes.
192 */
193uint
194xfs_default_attroffset(
195 struct xfs_inode *ip)
196{
197 struct xfs_mount *mp = ip->i_mount;
198 uint offset;
199
200 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100201 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a72013-02-25 12:31:26 +1100202 XFS_BMDR_SPACE_CALC(MINABTPTRS);
203 } else {
204 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
205 }
206
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100207 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100208 return offset;
209}
210
211/*
212 * Helper routine to reset inode di_forkoff field when switching
213 * attribute fork from local to extent format - we reset it where
214 * possible to make space available for inline data fork extents.
215 */
216STATIC void
217xfs_bmap_forkoff_reset(
Dave Chinner9e5987a72013-02-25 12:31:26 +1100218 xfs_inode_t *ip,
219 int whichfork)
220{
221 if (whichfork == XFS_ATTR_FORK &&
222 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
Dave Chinner9e5987a72013-02-25 12:31:26 +1100223 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
224 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
225
226 if (dfl_forkoff > ip->i_d.di_forkoff)
227 ip->i_d.di_forkoff = dfl_forkoff;
228 }
229}
230
Dave Chinner9e5987a72013-02-25 12:31:26 +1100231#ifdef DEBUG
232STATIC struct xfs_buf *
233xfs_bmap_get_bp(
234 struct xfs_btree_cur *cur,
235 xfs_fsblock_t bno)
236{
Dave Chinnere6631f82018-05-09 07:49:37 -0700237 struct xfs_log_item *lip;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100238 int i;
239
240 if (!cur)
241 return NULL;
242
243 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
244 if (!cur->bc_bufs[i])
245 break;
246 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
247 return cur->bc_bufs[i];
248 }
249
250 /* Chase down all the log items to see if the bp is there */
Dave Chinnere6631f82018-05-09 07:49:37 -0700251 list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
252 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
253
Dave Chinner9e5987a72013-02-25 12:31:26 +1100254 if (bip->bli_item.li_type == XFS_LI_BUF &&
255 XFS_BUF_ADDR(bip->bli_buf) == bno)
256 return bip->bli_buf;
257 }
258
259 return NULL;
260}
261
262STATIC void
263xfs_check_block(
264 struct xfs_btree_block *block,
265 xfs_mount_t *mp,
266 int root,
267 short sz)
268{
269 int i, j, dmxr;
270 __be64 *pp, *thispa; /* pointer to block address */
271 xfs_bmbt_key_t *prevp, *keyp;
272
273 ASSERT(be16_to_cpu(block->bb_level) > 0);
274
275 prevp = NULL;
276 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
277 dmxr = mp->m_bmap_dmxr[0];
278 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
279
280 if (prevp) {
281 ASSERT(be64_to_cpu(prevp->br_startoff) <
282 be64_to_cpu(keyp->br_startoff));
283 }
284 prevp = keyp;
285
286 /*
287 * Compare the block numbers to see if there are dups.
288 */
289 if (root)
290 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
291 else
292 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
293
294 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
295 if (root)
296 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
297 else
298 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
299 if (*thispa == *pp) {
300 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
301 __func__, j, i,
302 (unsigned long long)be64_to_cpu(*thispa));
Darrick J. Wongcec57252018-05-04 15:31:21 -0700303 xfs_err(mp, "%s: ptrs are equal in node\n",
Dave Chinner9e5987a72013-02-25 12:31:26 +1100304 __func__);
Darrick J. Wongcec57252018-05-04 15:31:21 -0700305 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100306 }
307 }
308 }
309}
310
311/*
312 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100313 * btree leaves. THis becomes prohibitively expensive for large extent count
314 * files, so don't bother with inodes that have more than 10,000 extents in
315 * them. The btree record ordering checks will still be done, so for such large
316 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100317 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100318STATIC void
319xfs_bmap_check_leaf_extents(
320 xfs_btree_cur_t *cur, /* btree cursor or null */
321 xfs_inode_t *ip, /* incore inode pointer */
322 int whichfork) /* data or attr fork */
323{
324 struct xfs_btree_block *block; /* current btree block */
325 xfs_fsblock_t bno; /* block # of "block" */
326 xfs_buf_t *bp; /* buffer for "block" */
327 int error; /* error return value */
328 xfs_extnum_t i=0, j; /* index into the extents list */
329 xfs_ifork_t *ifp; /* fork structure */
330 int level; /* btree level, for checking */
331 xfs_mount_t *mp; /* file system mount structure */
332 __be64 *pp; /* pointer to block address */
333 xfs_bmbt_rec_t *ep; /* pointer to current extent */
334 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
335 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
336 int bp_release = 0;
337
338 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
339 return;
340 }
341
Dave Chinnere3543812016-01-08 11:28:49 +1100342 /* skip large extent count inodes */
343 if (ip->i_d.di_nextents > 10000)
344 return;
345
Dave Chinner9e5987a72013-02-25 12:31:26 +1100346 bno = NULLFSBLOCK;
347 mp = ip->i_mount;
348 ifp = XFS_IFORK_PTR(ip, whichfork);
349 block = ifp->if_broot;
350 /*
351 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
352 */
353 level = be16_to_cpu(block->bb_level);
354 ASSERT(level > 0);
355 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
356 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
357 bno = be64_to_cpu(*pp);
358
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000359 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100360 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
361 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
362
363 /*
364 * Go down the tree until leaf level is reached, following the first
365 * pointer (leftmost) at each level.
366 */
367 while (level-- > 0) {
368 /* See if buf is in cur first */
369 bp_release = 0;
370 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
371 if (!bp) {
372 bp_release = 1;
373 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
374 XFS_BMAP_BTREE_REF,
375 &xfs_bmbt_buf_ops);
376 if (error)
377 goto error_norelse;
378 }
379 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100380 if (level == 0)
381 break;
382
383 /*
384 * Check this block for basic sanity (increasing keys and
385 * no duplicate blocks).
386 */
387
388 xfs_check_block(block, mp, 0, 0);
389 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
390 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100391 XFS_WANT_CORRUPTED_GOTO(mp,
Darrick J. Wong59f6fec2018-01-08 10:51:00 -0800392 xfs_verify_fsbno(mp, bno), error0);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100393 if (bp_release) {
394 bp_release = 0;
395 xfs_trans_brelse(NULL, bp);
396 }
397 }
398
399 /*
400 * Here with bp and block set to the leftmost leaf node in the tree.
401 */
402 i = 0;
403
404 /*
405 * Loop over all leaf nodes checking that all extents are in the right order.
406 */
407 for (;;) {
408 xfs_fsblock_t nextbno;
409 xfs_extnum_t num_recs;
410
411
412 num_recs = xfs_btree_get_numrecs(block);
413
414 /*
415 * Read-ahead the next leaf block, if any.
416 */
417
418 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
419
420 /*
421 * Check all the extents to make sure they are OK.
422 * If we had a previous block, the last entry should
423 * conform with the first entry in this one.
424 */
425
426 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
427 if (i) {
428 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
429 xfs_bmbt_disk_get_blockcount(&last) <=
430 xfs_bmbt_disk_get_startoff(ep));
431 }
432 for (j = 1; j < num_recs; j++) {
433 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
434 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
435 xfs_bmbt_disk_get_blockcount(ep) <=
436 xfs_bmbt_disk_get_startoff(nextp));
437 ep = nextp;
438 }
439
440 last = *ep;
441 i += num_recs;
442 if (bp_release) {
443 bp_release = 0;
444 xfs_trans_brelse(NULL, bp);
445 }
446 bno = nextbno;
447 /*
448 * If we've reached the end, stop.
449 */
450 if (bno == NULLFSBLOCK)
451 break;
452
453 bp_release = 0;
454 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
455 if (!bp) {
456 bp_release = 1;
457 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
458 XFS_BMAP_BTREE_REF,
459 &xfs_bmbt_buf_ops);
460 if (error)
461 goto error_norelse;
462 }
463 block = XFS_BUF_TO_BLOCK(bp);
464 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100465
Dave Chinner9e5987a72013-02-25 12:31:26 +1100466 return;
467
468error0:
469 xfs_warn(mp, "%s: at error0", __func__);
470 if (bp_release)
471 xfs_trans_brelse(NULL, bp);
472error_norelse:
473 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
474 __func__, i);
Darrick J. Wongcec57252018-05-04 15:31:21 -0700475 xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
476 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100477 return;
478}
479
480/*
Dave Chinner9e5987a72013-02-25 12:31:26 +1100481 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000482 * given the caller's original parameters. Specifically check the
483 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a72013-02-25 12:31:26 +1100484 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
485 */
486STATIC void
487xfs_bmap_validate_ret(
488 xfs_fileoff_t bno,
489 xfs_filblks_t len,
490 int flags,
491 xfs_bmbt_irec_t *mval,
492 int nmap,
493 int ret_nmap)
494{
495 int i; /* index to map values */
496
497 ASSERT(ret_nmap <= nmap);
498
499 for (i = 0; i < ret_nmap; i++) {
500 ASSERT(mval[i].br_blockcount > 0);
501 if (!(flags & XFS_BMAPI_ENTIRE)) {
502 ASSERT(mval[i].br_startoff >= bno);
503 ASSERT(mval[i].br_blockcount <= len);
504 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
505 bno + len);
506 } else {
507 ASSERT(mval[i].br_startoff < bno + len);
508 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
509 bno);
510 }
511 ASSERT(i == 0 ||
512 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
513 mval[i].br_startoff);
514 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
515 mval[i].br_startblock != HOLESTARTBLOCK);
516 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
517 mval[i].br_state == XFS_EXT_UNWRITTEN);
518 }
519}
520
521#else
522#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
Darrick J. Wong7bf7a192017-08-31 15:11:06 -0700523#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100524#endif /* DEBUG */
525
526/*
527 * bmap free list manipulation functions
528 */
529
530/*
531 * Add the extent to the list of extents to be free at transaction end.
532 * The list is maintained sorted (by block number).
533 */
534void
Brian Fosterfcb762f2018-05-09 08:45:04 -0700535__xfs_bmap_add_free(
Darrick J. Wong340785c2016-08-03 11:33:42 +1000536 struct xfs_mount *mp,
537 struct xfs_defer_ops *dfops,
538 xfs_fsblock_t bno,
539 xfs_filblks_t len,
Brian Fosterfcb762f2018-05-09 08:45:04 -0700540 struct xfs_owner_info *oinfo,
541 bool skip_discard)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100542{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000543 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100544#ifdef DEBUG
545 xfs_agnumber_t agno;
546 xfs_agblock_t agbno;
547
548 ASSERT(bno != NULLFSBLOCK);
549 ASSERT(len > 0);
550 ASSERT(len <= MAXEXTLEN);
551 ASSERT(!isnullstartblock(bno));
552 agno = XFS_FSB_TO_AGNO(mp, bno);
553 agbno = XFS_FSB_TO_AGBNO(mp, bno);
554 ASSERT(agno < mp->m_sb.sb_agcount);
555 ASSERT(agbno < mp->m_sb.sb_agblocks);
556 ASSERT(len < mp->m_sb.sb_agblocks);
557 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
558#endif
559 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000560
Dave Chinner9e5987a72013-02-25 12:31:26 +1100561 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000562 new->xefi_startblock = bno;
563 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000564 if (oinfo)
565 new->xefi_oinfo = *oinfo;
566 else
567 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
Brian Fosterfcb762f2018-05-09 08:45:04 -0700568 new->xefi_skip_discard = skip_discard;
Darrick J. Wongba9e7802016-08-03 11:26:33 +1000569 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
570 XFS_FSB_TO_AGBNO(mp, bno), len);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000571 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100572}
573
574/*
575 * Inode fork format manipulation functions
576 */
577
578/*
579 * Transform a btree format file with only one leaf node, where the
580 * extents list will fit in the inode, into an extents format file.
581 * Since the file extents are already in-core, all we have to do is
582 * give up the space for the btree root and pitch the leaf block.
583 */
584STATIC int /* error */
585xfs_bmap_btree_to_extents(
586 xfs_trans_t *tp, /* transaction pointer */
587 xfs_inode_t *ip, /* incore inode pointer */
588 xfs_btree_cur_t *cur, /* btree cursor */
589 int *logflagsp, /* inode logging flags */
590 int whichfork) /* data or attr fork */
591{
592 /* REFERENCED */
593 struct xfs_btree_block *cblock;/* child btree block */
594 xfs_fsblock_t cbno; /* child block number */
595 xfs_buf_t *cbp; /* child block's buffer */
596 int error; /* error return value */
597 xfs_ifork_t *ifp; /* inode fork data */
598 xfs_mount_t *mp; /* mount point structure */
599 __be64 *pp; /* ptr to block address */
600 struct xfs_btree_block *rblock;/* root btree block */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000601 struct xfs_owner_info oinfo;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100602
603 mp = ip->i_mount;
604 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700605 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100606 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
607 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
608 rblock = ifp->if_broot;
609 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
610 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
611 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
612 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
613 cbno = be64_to_cpu(*pp);
614 *logflagsp = 0;
615#ifdef DEBUG
Darrick J. Wongf1357612017-10-17 21:37:33 -0700616 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
617 xfs_btree_check_lptr(cur, cbno, 1));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100618#endif
619 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
620 &xfs_bmbt_buf_ops);
621 if (error)
622 return error;
623 cblock = XFS_BUF_TO_BLOCK(cbp);
624 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
625 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000626 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
Brian Foster42b394a2018-07-11 22:26:16 -0700627 xfs_bmap_add_free(mp, cur->bc_tp->t_dfops, cbno, 1, &oinfo);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100628 ip->i_d.di_nblocks--;
629 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
630 xfs_trans_binval(tp, cbp);
631 if (cur->bc_bufs[0] == cbp)
632 cur->bc_bufs[0] = NULL;
633 xfs_iroot_realloc(ip, -1, whichfork);
634 ASSERT(ifp->if_broot == NULL);
635 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
636 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
637 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
638 return 0;
639}
640
641/*
642 * Convert an extents-format file into a btree-format file.
643 * The new file will have a root block (in the inode) and a single child block.
644 */
645STATIC int /* error */
646xfs_bmap_extents_to_btree(
Brian Foster81ba8f32018-07-11 22:26:16 -0700647 struct xfs_trans *tp, /* transaction pointer */
648 struct xfs_inode *ip, /* incore inode pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100649 xfs_fsblock_t *firstblock, /* first-block-allocated */
Brian Foster81ba8f32018-07-11 22:26:16 -0700650 struct xfs_btree_cur **curp, /* cursor returned to caller */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100651 int wasdel, /* converting a delayed alloc */
652 int *logflagsp, /* inode logging flags */
653 int whichfork) /* data or attr fork */
654{
655 struct xfs_btree_block *ablock; /* allocated (child) bt block */
Brian Foster81ba8f32018-07-11 22:26:16 -0700656 struct xfs_buf *abp; /* buffer for ablock */
657 struct xfs_alloc_arg args; /* allocation arguments */
658 struct xfs_bmbt_rec *arp; /* child record pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100659 struct xfs_btree_block *block; /* btree root block */
Brian Foster81ba8f32018-07-11 22:26:16 -0700660 struct xfs_btree_cur *cur; /* bmap btree cursor */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100661 int error; /* error return value */
Brian Foster81ba8f32018-07-11 22:26:16 -0700662 struct xfs_ifork *ifp; /* inode fork pointer */
663 struct xfs_bmbt_key *kp; /* root block key pointer */
664 struct xfs_mount *mp; /* mount structure */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100665 xfs_bmbt_ptr_t *pp; /* root block address pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700666 struct xfs_iext_cursor icur;
Christoph Hellwig906abed2017-11-03 10:34:43 -0700667 struct xfs_bmbt_irec rec;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700668 xfs_extnum_t cnt = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100669
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500670 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700671 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100672 ifp = XFS_IFORK_PTR(ip, whichfork);
673 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
674
675 /*
676 * Make space in the inode incore.
677 */
678 xfs_iroot_realloc(ip, 1, whichfork);
679 ifp->if_flags |= XFS_IFBROOT;
680
681 /*
682 * Fill in the root.
683 */
684 block = ifp->if_broot;
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800685 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
686 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
Eric Sandeenf88ae462017-01-27 23:16:37 -0800687 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100688 /*
689 * Need a cursor. Can't allocate until bb_level is filled in.
690 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100691 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
692 cur->bc_private.b.firstblock = *firstblock;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100693 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
694 /*
695 * Convert to a btree with two levels, one record in root.
696 */
697 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
698 memset(&args, 0, sizeof(args));
699 args.tp = tp;
700 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000701 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100702 args.firstblock = *firstblock;
703 if (*firstblock == NULLFSBLOCK) {
704 args.type = XFS_ALLOCTYPE_START_BNO;
705 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Brian Foster81ba8f32018-07-11 22:26:16 -0700706 } else if (tp->t_dfops->dop_low) {
Dave Chinner9e5987a72013-02-25 12:31:26 +1100707 args.type = XFS_ALLOCTYPE_START_BNO;
708 args.fsbno = *firstblock;
709 } else {
710 args.type = XFS_ALLOCTYPE_NEAR_BNO;
711 args.fsbno = *firstblock;
712 }
713 args.minlen = args.maxlen = args.prod = 1;
714 args.wasdel = wasdel;
715 *logflagsp = 0;
716 if ((error = xfs_alloc_vextent(&args))) {
717 xfs_iroot_realloc(ip, -1, whichfork);
Eric Sandeen2c4306f2018-04-16 23:07:27 -0700718 ASSERT(ifp->if_broot == NULL);
719 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100720 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
721 return error;
722 }
Darrick J. Wong90e20562016-10-03 09:11:45 -0700723
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800724 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
725 xfs_iroot_realloc(ip, -1, whichfork);
Eric Sandeen2c4306f2018-04-16 23:07:27 -0700726 ASSERT(ifp->if_broot == NULL);
727 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800728 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
729 return -ENOSPC;
730 }
Dave Chinner9e5987a72013-02-25 12:31:26 +1100731 /*
732 * Allocation can't fail, the space was reserved.
733 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100734 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -0800735 args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100736 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
737 cur->bc_private.b.allocated++;
738 ip->i_d.di_nblocks++;
739 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
740 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
741 /*
742 * Fill in the child block.
743 */
744 abp->b_ops = &xfs_bmbt_buf_ops;
745 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800746 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
747 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500748 XFS_BTREE_LONG_PTRS);
749
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700750 for_each_xfs_iext(ifp, &icur, &rec) {
Christoph Hellwig906abed2017-11-03 10:34:43 -0700751 if (isnullstartblock(rec.br_startblock))
752 continue;
753 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
754 xfs_bmbt_disk_set_all(arp, &rec);
755 cnt++;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100756 }
757 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
758 xfs_btree_set_numrecs(ablock, cnt);
759
760 /*
761 * Fill in the root key and pointer.
762 */
763 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
764 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
765 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
766 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
767 be16_to_cpu(block->bb_level)));
768 *pp = cpu_to_be64(args.fsbno);
769
770 /*
771 * Do all this logging at the end so that
772 * the root is at the right level.
773 */
774 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
775 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
776 ASSERT(*curp == NULL);
777 *curp = cur;
778 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
779 return 0;
780}
781
782/*
783 * Convert a local file to an extents file.
784 * This code is out of bounds for data forks of regular files,
785 * since the file data needs to get logged so things will stay consistent.
786 * (The bmap-level manipulations are ok, though).
787 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000788void
789xfs_bmap_local_to_extents_empty(
790 struct xfs_inode *ip,
791 int whichfork)
792{
793 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
794
Darrick J. Wong60b49842016-10-03 09:11:34 -0700795 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000796 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
797 ASSERT(ifp->if_bytes == 0);
798 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
799
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000800 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000801 ifp->if_flags &= ~XFS_IFINLINE;
802 ifp->if_flags |= XFS_IFEXTENTS;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700803 ifp->if_u1.if_root = NULL;
804 ifp->if_height = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000805 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
806}
807
808
Dave Chinner9e5987a72013-02-25 12:31:26 +1100809STATIC int /* error */
810xfs_bmap_local_to_extents(
811 xfs_trans_t *tp, /* transaction pointer */
812 xfs_inode_t *ip, /* incore inode pointer */
813 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
814 xfs_extlen_t total, /* total blocks needed by transaction */
815 int *logflagsp, /* inode logging flags */
816 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500817 void (*init_fn)(struct xfs_trans *tp,
818 struct xfs_buf *bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100819 struct xfs_inode *ip,
820 struct xfs_ifork *ifp))
821{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000822 int error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100823 int flags; /* logging flags returned */
824 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000825 xfs_alloc_arg_t args; /* allocation arguments */
826 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700827 struct xfs_bmbt_irec rec;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700828 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100829
830 /*
831 * We don't want to deal with the case of keeping inode data inline yet.
832 * So sending the data fork of a regular inode is invalid.
833 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100834 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100835 ifp = XFS_IFORK_PTR(ip, whichfork);
836 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000837
838 if (!ifp->if_bytes) {
839 xfs_bmap_local_to_extents_empty(ip, whichfork);
840 flags = XFS_ILOG_CORE;
841 goto done;
842 }
843
Dave Chinner9e5987a72013-02-25 12:31:26 +1100844 flags = 0;
845 error = 0;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700846 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000847 memset(&args, 0, sizeof(args));
848 args.tp = tp;
849 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000850 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000851 args.firstblock = *firstblock;
852 /*
853 * Allocate a block. We know we need only one, since the
854 * file currently fits in an inode.
855 */
856 if (*firstblock == NULLFSBLOCK) {
857 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
858 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100859 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000860 args.fsbno = *firstblock;
861 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100862 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000863 args.total = total;
864 args.minlen = args.maxlen = args.prod = 1;
865 error = xfs_alloc_vextent(&args);
866 if (error)
867 goto done;
868
869 /* Can't fail, the space was reserved. */
870 ASSERT(args.fsbno != NULLFSBLOCK);
871 ASSERT(args.len == 1);
872 *firstblock = args.fsbno;
873 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
874
Dave Chinnerfe22d552015-01-22 09:30:06 +1100875 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100876 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100877 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100878 * The callout is responsible for logging because the remote format
879 * might differ from the local format and thus we don't know how much to
880 * log here. Note that init_fn must also set the buffer log item type
881 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100882 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000883 init_fn(tp, bp, ip, ifp);
884
Brian Fosterb7cdc662015-10-12 15:40:24 +1100885 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000886 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
887 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100888 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000889
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700890 ifp->if_u1.if_root = NULL;
891 ifp->if_height = 0;
892
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700893 rec.br_startoff = 0;
894 rec.br_startblock = args.fsbno;
895 rec.br_blockcount = 1;
896 rec.br_state = XFS_EXT_NORM;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700897 xfs_iext_first(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -0700898 xfs_iext_insert(ip, &icur, &rec, 0);
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700899
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000900 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
901 ip->i_d.di_nblocks = 1;
902 xfs_trans_mod_dquot_byino(tp, ip,
903 XFS_TRANS_DQ_BCOUNT, 1L);
904 flags |= xfs_ilog_fext(whichfork);
905
Dave Chinner9e5987a72013-02-25 12:31:26 +1100906done:
907 *logflagsp = flags;
908 return error;
909}
910
911/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 * Called from xfs_bmap_add_attrfork to handle btree format files.
913 */
914STATIC int /* error */
915xfs_bmap_add_attrfork_btree(
916 xfs_trans_t *tp, /* transaction pointer */
917 xfs_inode_t *ip, /* incore inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 int *flags) /* inode logging flags */
919{
920 xfs_btree_cur_t *cur; /* btree cursor */
921 int error; /* error return value */
922 xfs_mount_t *mp; /* file system mount struct */
923 int stat; /* newroot status */
924
925 mp = ip->i_mount;
926 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
927 *flags |= XFS_ILOG_DBROOT;
928 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100929 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Brian Foster825d75cd2018-07-11 22:26:21 -0700930 cur->bc_private.b.firstblock = tp->t_firstblock;
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700931 error = xfs_bmbt_lookup_first(cur, &stat);
932 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000934 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100935 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100936 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 goto error0;
938 if (stat == 0) {
939 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000940 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
Brian Foster825d75cd2018-07-11 22:26:21 -0700942 tp->t_firstblock = cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 cur->bc_private.b.allocated = 0;
944 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
945 }
946 return 0;
947error0:
948 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
949 return error;
950}
951
952/*
953 * Called from xfs_bmap_add_attrfork to handle extents format files.
954 */
955STATIC int /* error */
956xfs_bmap_add_attrfork_extents(
Brian Foster81ba8f32018-07-11 22:26:16 -0700957 struct xfs_trans *tp, /* transaction pointer */
958 struct xfs_inode *ip, /* incore inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 int *flags) /* inode logging flags */
960{
961 xfs_btree_cur_t *cur; /* bmap btree cursor */
962 int error; /* error return value */
963
964 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
965 return 0;
966 cur = NULL;
Brian Foster825d75cd2018-07-11 22:26:21 -0700967 error = xfs_bmap_extents_to_btree(tp, ip, &tp->t_firstblock, &cur, 0,
968 flags, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (cur) {
970 cur->bc_private.b.allocated = 0;
971 xfs_btree_del_cursor(cur,
972 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
973 }
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;
Brian Foster825d75cd2018-07-11 22:26:21 -07001003 dargs.firstblock = &tp->t_firstblock;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001004 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 dargs.whichfork = XFS_DATA_FORK;
1006 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001007 return xfs_dir2_sf_to_block(&dargs);
1008 }
1009
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001010 if (S_ISLNK(VFS_I(ip)->i_mode))
Brian Foster825d75cd2018-07-11 22:26:21 -07001011 return xfs_bmap_local_to_extents(tp, ip, &tp->t_firstblock, 1,
Dave Chinner1e823792013-02-11 15:58:13 +11001012 flags, XFS_DATA_FORK,
1013 xfs_symlink_local_to_remote);
1014
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001015 /* should only be called for types that support local format data */
1016 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001017 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
1020/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001021 * Convert inode from non-attributed to attributed.
1022 * Must not be in a transaction, ip must not be locked.
1023 */
1024int /* error code */
1025xfs_bmap_add_attrfork(
1026 xfs_inode_t *ip, /* incore inode pointer */
1027 int size, /* space new attribute needs */
1028 int rsvd) /* xact may use reserved blks */
1029{
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001030 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001031 xfs_mount_t *mp; /* mount structure */
1032 xfs_trans_t *tp; /* transaction pointer */
1033 int blks; /* space reservation */
1034 int version = 1; /* superblock attr version */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001035 int logflags; /* logging flags */
1036 int error; /* error return value */
1037
1038 ASSERT(XFS_IFORK_Q(ip) == 0);
1039
1040 mp = ip->i_mount;
1041 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001042
Dave Chinner9e5987a72013-02-25 12:31:26 +11001043 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001044
1045 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1046 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1047 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001048 return error;
Brian Foster825d75cd2018-07-11 22:26:21 -07001049 xfs_defer_init(tp, &dfops, &tp->t_firstblock);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001050
Dave Chinner9e5987a72013-02-25 12:31:26 +11001051 xfs_ilock(ip, XFS_ILOCK_EXCL);
1052 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1053 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1054 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001055 if (error)
1056 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001057 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001058 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001059 if (ip->i_d.di_anextents != 0) {
1060 error = -EFSCORRUPTED;
1061 goto trans_cancel;
1062 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001063 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1064 /*
1065 * For inodes coming from pre-6.2 filesystems.
1066 */
1067 ASSERT(ip->i_d.di_aformat == 0);
1068 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1069 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001070
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001071 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001072 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1073
1074 switch (ip->i_d.di_format) {
1075 case XFS_DINODE_FMT_DEV:
1076 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1077 break;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001078 case XFS_DINODE_FMT_LOCAL:
1079 case XFS_DINODE_FMT_EXTENTS:
1080 case XFS_DINODE_FMT_BTREE:
1081 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1082 if (!ip->i_d.di_forkoff)
1083 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1084 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1085 version = 2;
1086 break;
1087 default:
1088 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001089 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001090 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001091 }
1092
1093 ASSERT(ip->i_afp == NULL);
1094 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1095 ip->i_afp->if_flags = XFS_IFEXTENTS;
1096 logflags = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001097 switch (ip->i_d.di_format) {
1098 case XFS_DINODE_FMT_LOCAL:
Brian Foster825d75cd2018-07-11 22:26:21 -07001099 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001100 break;
1101 case XFS_DINODE_FMT_EXTENTS:
Brian Foster825d75cd2018-07-11 22:26:21 -07001102 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001103 break;
1104 case XFS_DINODE_FMT_BTREE:
Brian Foster825d75cd2018-07-11 22:26:21 -07001105 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001106 break;
1107 default:
1108 error = 0;
1109 break;
1110 }
1111 if (logflags)
1112 xfs_trans_log_inode(tp, ip, logflags);
1113 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001114 goto bmap_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001115 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1116 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001117 bool log_sb = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001118
1119 spin_lock(&mp->m_sb_lock);
1120 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1121 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001122 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001123 }
1124 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1125 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001126 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001127 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001128 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001129 if (log_sb)
1130 xfs_log_sb(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001131 }
1132
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001133 error = xfs_defer_finish(&tp, &dfops);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001134 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001135 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001136 error = xfs_trans_commit(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001137 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001138 return error;
1139
1140bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001141 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001142trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001143 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001144 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001145 return error;
1146}
1147
1148/*
1149 * Internal and external extent tree search functions.
1150 */
1151
1152/*
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001153 * Read in extents from a btree-format inode.
Dave Chinner9e5987a72013-02-25 12:31:26 +11001154 */
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001155int
1156xfs_iread_extents(
1157 struct xfs_trans *tp,
1158 struct xfs_inode *ip,
1159 int whichfork)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001160{
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001161 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwige8e0e172017-10-19 11:06:29 -07001162 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001163 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1164 xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
1165 struct xfs_btree_block *block = ifp->if_broot;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001166 struct xfs_iext_cursor icur;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -07001167 struct xfs_bmbt_irec new;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001168 xfs_fsblock_t bno;
1169 struct xfs_buf *bp;
1170 xfs_extnum_t i, j;
1171 int level;
1172 __be64 *pp;
1173 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001174
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001175 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1176
1177 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1178 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1179 return -EFSCORRUPTED;
1180 }
1181
Dave Chinner9e5987a72013-02-25 12:31:26 +11001182 /*
1183 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1184 */
1185 level = be16_to_cpu(block->bb_level);
1186 ASSERT(level > 0);
1187 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1188 bno = be64_to_cpu(*pp);
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -08001189
Dave Chinner9e5987a72013-02-25 12:31:26 +11001190 /*
1191 * Go down the tree until leaf level is reached, following the first
1192 * pointer (leftmost) at each level.
1193 */
1194 while (level-- > 0) {
1195 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1196 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1197 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001198 goto out;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001199 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001200 if (level == 0)
1201 break;
1202 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1203 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001204 XFS_WANT_CORRUPTED_GOTO(mp,
Darrick J. Wong59f6fec2018-01-08 10:51:00 -08001205 xfs_verify_fsbno(mp, bno), out_brelse);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001206 xfs_trans_brelse(tp, bp);
1207 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001208
Dave Chinner9e5987a72013-02-25 12:31:26 +11001209 /*
1210 * Here with bp and block set to the leftmost leaf node in the tree.
1211 */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001212 i = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001213 xfs_iext_first(ifp, &icur);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001214
Dave Chinner9e5987a72013-02-25 12:31:26 +11001215 /*
1216 * Loop over all leaf nodes. Copy information to the extent records.
1217 */
1218 for (;;) {
1219 xfs_bmbt_rec_t *frp;
1220 xfs_fsblock_t nextbno;
1221 xfs_extnum_t num_recs;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001222
1223 num_recs = xfs_btree_get_numrecs(block);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001224 if (unlikely(i + num_recs > nextents)) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001225 xfs_warn(ip->i_mount,
1226 "corrupt dinode %Lu, (btree extents).",
1227 (unsigned long long) ip->i_ino);
Darrick J. Wong90a58f92018-03-23 10:06:52 -07001228 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1229 __func__, block, sizeof(*block),
1230 __this_address);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001231 error = -EFSCORRUPTED;
1232 goto out_brelse;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001233 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001234 /*
1235 * Read-ahead the next leaf block, if any.
1236 */
1237 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1238 if (nextbno != NULLFSBLOCK)
1239 xfs_btree_reada_bufl(mp, nextbno, 1,
1240 &xfs_bmbt_buf_ops);
1241 /*
1242 * Copy records into the extent records.
1243 */
1244 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Christoph Hellwig6bdcf262017-11-03 10:34:46 -07001245 for (j = 0; j < num_recs; j++, frp++, i++) {
Darrick J. Wong30b09842018-03-23 10:06:52 -07001246 xfs_failaddr_t fa;
1247
Christoph Hellwigdac9c9b2017-11-03 10:34:47 -07001248 xfs_bmbt_disk_get_all(frp, &new);
Darrick J. Wong30b09842018-03-23 10:06:52 -07001249 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1250 if (fa) {
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001251 error = -EFSCORRUPTED;
Darrick J. Wong30b09842018-03-23 10:06:52 -07001252 xfs_inode_verifier_error(ip, error,
1253 "xfs_iread_extents(2)",
1254 frp, sizeof(*frp), fa);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001255 goto out_brelse;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001256 }
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001257 xfs_iext_insert(ip, &icur, &new, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001258 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
1259 xfs_iext_next(ifp, &icur);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001260 }
1261 xfs_trans_brelse(tp, bp);
1262 bno = nextbno;
1263 /*
1264 * If we've reached the end, stop.
1265 */
1266 if (bno == NULLFSBLOCK)
1267 break;
1268 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1269 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1270 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001271 goto out;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001272 block = XFS_BUF_TO_BLOCK(bp);
1273 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001274
1275 if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) {
1276 error = -EFSCORRUPTED;
1277 goto out;
1278 }
Eric Sandeen5d829302016-11-08 12:59:42 +11001279 ASSERT(i == xfs_iext_count(ifp));
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001280
1281 ifp->if_flags |= XFS_IFEXTENTS;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001282 return 0;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001283
1284out_brelse:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001285 xfs_trans_brelse(tp, bp);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001286out:
1287 xfs_iext_destroy(ifp);
1288 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001289}
1290
Dave Chinner9e5987a72013-02-25 12:31:26 +11001291/*
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001292 * Returns the relative block number of the first unused block(s) in the given
1293 * fork with at least "len" logically contiguous blocks free. This is the
1294 * lowest-address hole if the fork has holes, else the first block past the end
1295 * of fork. Return 0 if the fork is currently local (in-inode).
Dave Chinner9e5987a72013-02-25 12:31:26 +11001296 */
1297int /* error */
1298xfs_bmap_first_unused(
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001299 struct xfs_trans *tp, /* transaction pointer */
1300 struct xfs_inode *ip, /* incore inode */
1301 xfs_extlen_t len, /* size of hole to find */
1302 xfs_fileoff_t *first_unused, /* unused block */
1303 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001304{
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001305 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1306 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001307 struct xfs_iext_cursor icur;
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001308 xfs_fileoff_t lastaddr = 0;
1309 xfs_fileoff_t lowest, max;
1310 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001311
1312 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1313 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1314 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001315
Dave Chinner9e5987a72013-02-25 12:31:26 +11001316 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1317 *first_unused = 0;
1318 return 0;
1319 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001320
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001321 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1322 error = xfs_iread_extents(tp, ip, whichfork);
1323 if (error)
1324 return error;
1325 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001326
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001327 lowest = max = *first_unused;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001328 for_each_xfs_iext(ifp, &icur, &got) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001329 /*
1330 * See if the hole before this extent will work.
1331 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001332 if (got.br_startoff >= lowest + len &&
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001333 got.br_startoff - max >= len)
1334 break;
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001335 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001336 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1337 }
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001338
Dave Chinner9e5987a72013-02-25 12:31:26 +11001339 *first_unused = max;
1340 return 0;
1341}
1342
1343/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001344 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a72013-02-25 12:31:26 +11001345 * last_block (input value) in the file.
1346 * This is not based on i_size, it is based on the extent records.
1347 * Returns 0 for local files, as they do not have extent records.
1348 */
1349int /* error */
1350xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001351 struct xfs_trans *tp, /* transaction pointer */
1352 struct xfs_inode *ip, /* incore inode */
1353 xfs_fileoff_t *last_block, /* last block */
1354 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001355{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001356 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1357 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001358 struct xfs_iext_cursor icur;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001359 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001360
Christoph Hellwig86685f72016-11-24 11:39:38 +11001361 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1362 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001363 *last_block = 0;
1364 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001365 case XFS_DINODE_FMT_BTREE:
1366 case XFS_DINODE_FMT_EXTENTS:
1367 break;
1368 default:
1369 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001370 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001371
1372 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1373 error = xfs_iread_extents(tp, ip, whichfork);
1374 if (error)
1375 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001376 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001377
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001378 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
Christoph Hellwigdc560152017-10-23 16:32:39 -07001379 *last_block = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001380 return 0;
1381}
1382
Dave Chinner68988112013-08-12 20:49:42 +10001383int
Dave Chinner9e5987a72013-02-25 12:31:26 +11001384xfs_bmap_last_extent(
1385 struct xfs_trans *tp,
1386 struct xfs_inode *ip,
1387 int whichfork,
1388 struct xfs_bmbt_irec *rec,
1389 int *is_empty)
1390{
1391 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001392 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001393 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001394
1395 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1396 error = xfs_iread_extents(tp, ip, whichfork);
1397 if (error)
1398 return error;
1399 }
1400
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001401 xfs_iext_last(ifp, &icur);
1402 if (!xfs_iext_get_extent(ifp, &icur, rec))
Dave Chinner9e5987a72013-02-25 12:31:26 +11001403 *is_empty = 1;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001404 else
1405 *is_empty = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001406 return 0;
1407}
1408
1409/*
1410 * Check the last inode extent to determine whether this allocation will result
1411 * in blocks being allocated at the end of the file. When we allocate new data
1412 * blocks at the end of the file which do not start at the previous data block,
1413 * we will try to align the new blocks at stripe unit boundaries.
1414 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001415 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a72013-02-25 12:31:26 +11001416 * at, or past the EOF.
1417 */
1418STATIC int
1419xfs_bmap_isaeof(
1420 struct xfs_bmalloca *bma,
1421 int whichfork)
1422{
1423 struct xfs_bmbt_irec rec;
1424 int is_empty;
1425 int error;
1426
Thomas Meyer749f24f2017-10-09 11:38:54 -07001427 bma->aeof = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001428 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1429 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001430 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001431 return error;
1432
Dave Chinner6e708bc2013-11-22 10:41:16 +11001433 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001434 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001435 return 0;
1436 }
1437
Dave Chinner9e5987a72013-02-25 12:31:26 +11001438 /*
1439 * Check if we are allocation or past the last extent, or at least into
1440 * the last delayed allocated extent.
1441 */
1442 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1443 (bma->offset >= rec.br_startoff &&
1444 isnullstartblock(rec.br_startblock));
1445 return 0;
1446}
1447
1448/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001449 * Returns the file-relative block number of the first block past eof in
1450 * the file. This is not based on i_size, it is based on the extent records.
1451 * Returns 0 for local files, as they do not have extent records.
1452 */
1453int
1454xfs_bmap_last_offset(
Dave Chinner9e5987a72013-02-25 12:31:26 +11001455 struct xfs_inode *ip,
1456 xfs_fileoff_t *last_block,
1457 int whichfork)
1458{
1459 struct xfs_bmbt_irec rec;
1460 int is_empty;
1461 int error;
1462
1463 *last_block = 0;
1464
1465 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1466 return 0;
1467
1468 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1469 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001470 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001471
1472 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1473 if (error || is_empty)
1474 return error;
1475
1476 *last_block = rec.br_startoff + rec.br_blockcount;
1477 return 0;
1478}
1479
1480/*
1481 * Returns whether the selected fork of the inode has exactly one
1482 * block or not. For the data fork we check this matches di_size,
1483 * implying the file's range is 0..bsize-1.
1484 */
1485int /* 1=>1 block, 0=>otherwise */
1486xfs_bmap_one_block(
1487 xfs_inode_t *ip, /* incore inode */
1488 int whichfork) /* data or attr fork */
1489{
Dave Chinner9e5987a72013-02-25 12:31:26 +11001490 xfs_ifork_t *ifp; /* inode fork pointer */
1491 int rval; /* return value */
1492 xfs_bmbt_irec_t s; /* internal version of extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001493 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001494
1495#ifndef DEBUG
1496 if (whichfork == XFS_DATA_FORK)
1497 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1498#endif /* !DEBUG */
1499 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1500 return 0;
1501 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1502 return 0;
1503 ifp = XFS_IFORK_PTR(ip, whichfork);
1504 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001505 xfs_iext_first(ifp, &icur);
1506 xfs_iext_get_extent(ifp, &icur, &s);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001507 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1508 if (rval && whichfork == XFS_DATA_FORK)
1509 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1510 return rval;
1511}
1512
1513/*
1514 * Extent tree manipulation functions used during allocation.
1515 */
1516
1517/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001518 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 */
1520STATIC int /* error */
1521xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001522 struct xfs_bmalloca *bma,
1523 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001525 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001528 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 xfs_fileoff_t new_endoff; /* end offset of new entry */
1530 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1531 /* left is 0, right is 1, prev is 2 */
1532 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07001533 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001534 xfs_filblks_t da_new; /* new count del alloc blocks used */
1535 xfs_filblks_t da_old; /* old count del alloc blocks used */
1536 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001538 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001539 xfs_extnum_t *nextents;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001540 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001542 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001543 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001544 ASSERT(whichfork != XFS_ATTR_FORK);
1545 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1546 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001547
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001548 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001549 ASSERT(!bma->cur ||
1550 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001551
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001552 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554#define LEFT r[0]
1555#define RIGHT r[1]
1556#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 /*
1559 * Set up a bunch of variables to make the tests simpler.
1560 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001561 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001563 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 ASSERT(PREV.br_startoff <= new->br_startoff);
1565 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001566
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001567 da_old = startblockval(PREV.br_startblock);
1568 da_new = 0;
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 /*
1571 * Set flags determining what part of the previous delayed allocation
1572 * extent is being replaced by a real allocation.
1573 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001574 if (PREV.br_startoff == new->br_startoff)
1575 state |= BMAP_LEFT_FILLING;
1576 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1577 state |= BMAP_RIGHT_FILLING;
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 /*
1580 * Check and set flags if this segment has a left neighbor.
1581 * Don't set contiguous if the combined extent would be too large.
1582 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001583 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001584 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001585 if (isnullstartblock(LEFT.br_startblock))
1586 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001588
1589 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1590 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1591 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1592 LEFT.br_state == new->br_state &&
1593 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1594 state |= BMAP_LEFT_CONTIG;
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 /*
1597 * Check and set flags if this segment has a right neighbor.
1598 * Don't set contiguous if the combined extent would be too large.
1599 * Also check for all-three-contiguous being too large.
1600 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001601 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001602 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001603 if (isnullstartblock(RIGHT.br_startblock))
1604 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001606
1607 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1608 new_endoff == RIGHT.br_startoff &&
1609 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1610 new->br_state == RIGHT.br_state &&
1611 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1612 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1613 BMAP_RIGHT_FILLING)) !=
1614 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1615 BMAP_RIGHT_FILLING) ||
1616 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1617 <= MAXEXTLEN))
1618 state |= BMAP_RIGHT_CONTIG;
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 error = 0;
1621 /*
1622 * Switch out based on the FILLING and CONTIG state bits.
1623 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001624 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1625 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1626 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1627 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 /*
1629 * Filling in all of a previously delayed allocation extent.
1630 * The left and right neighbors are both contiguous with new.
1631 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001632 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001633
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001634 xfs_iext_remove(bma->ip, &bma->icur, state);
1635 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001636 xfs_iext_prev(ifp, &bma->icur);
1637 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001638 (*nextents)--;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001639
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001640 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1642 else {
1643 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001644 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001645 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001647 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001648 error = xfs_btree_delete(bma->cur, &i);
1649 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001651 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001652 error = xfs_btree_decrement(bma->cur, 0, &i);
1653 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001655 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001656 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001657 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 goto done;
1659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 break;
1661
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001662 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 /*
1664 * Filling in all of a previously delayed allocation extent.
1665 * The left neighbor is contiguous, the right is not.
1666 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001667 old = LEFT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001668 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001669
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001670 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001671 xfs_iext_prev(ifp, &bma->icur);
1672 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001673
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001674 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 rval = XFS_ILOG_DEXT;
1676 else {
1677 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001678 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001679 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001681 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001682 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001683 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 goto done;
1685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 break;
1687
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001688 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 /*
1690 * Filling in all of a previously delayed allocation extent.
1691 * The right neighbor is contiguous, the left is not.
1692 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001693 PREV.br_startblock = new->br_startblock;
1694 PREV.br_blockcount += RIGHT.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001695
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001696 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001697 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001698 xfs_iext_prev(ifp, &bma->icur);
1699 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001700
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001701 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 rval = XFS_ILOG_DEXT;
1703 else {
1704 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001705 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001706 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001708 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001709 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001710 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 goto done;
1712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 break;
1714
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001715 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 /*
1717 * Filling in all of a previously delayed allocation extent.
1718 * Neither the left nor right neighbors are contiguous with
1719 * the new one.
1720 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001721 PREV.br_startblock = new->br_startblock;
1722 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001723 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001724
Darrick J. Wong60b49842016-10-03 09:11:34 -07001725 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001726 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1728 else {
1729 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001730 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001731 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001733 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001734 error = xfs_btree_insert(bma->cur, &i);
1735 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001737 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 break;
1740
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001741 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 /*
1743 * Filling in the first part of a previous delayed allocation.
1744 * The left neighbor is contiguous.
1745 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001746 old = LEFT;
1747 temp = PREV.br_blockcount - new->br_blockcount;
1748 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1749 startblockval(PREV.br_startblock));
1750
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001751 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001752
Christoph Hellwigbf999712017-11-03 10:34:38 -07001753 PREV.br_blockcount = temp;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001754 PREV.br_startoff += new->br_blockcount;
1755 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwig0d045542017-11-03 10:34:39 -07001756
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001757 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1758 xfs_iext_prev(ifp, &bma->icur);
1759 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001760
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001761 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 rval = XFS_ILOG_DEXT;
1763 else {
1764 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001765 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001766 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001768 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001769 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001770 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 goto done;
1772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 break;
1774
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001775 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 /*
1777 * Filling in the first part of a previous delayed allocation.
1778 * The left neighbor is not contiguous.
1779 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001780 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001781 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001782 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1784 else {
1785 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001786 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001787 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001789 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001790 error = xfs_btree_insert(bma->cur, &i);
1791 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001793 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001795
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001796 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001797 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster81ba8f32018-07-11 22:26:16 -07001798 bma->firstblock, &bma->cur, 1,
1799 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 rval |= tmp_rval;
1801 if (error)
1802 goto done;
1803 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001804
1805 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001806 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001807 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001808 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001809
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001810 PREV.br_startoff = new_endoff;
1811 PREV.br_blockcount = temp;
1812 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001813 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001814 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001815 xfs_iext_prev(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 break;
1817
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001818 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 /*
1820 * Filling in the last part of a previous delayed allocation.
1821 * The right neighbor is contiguous with the new allocation.
1822 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001823 old = RIGHT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001824 RIGHT.br_startoff = new->br_startoff;
1825 RIGHT.br_startblock = new->br_startblock;
1826 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001827
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001828 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 rval = XFS_ILOG_DEXT;
1830 else {
1831 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001832 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001833 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001835 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001836 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001837 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 goto done;
1839 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001840
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001841 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001842 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001843 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001844
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001845 PREV.br_blockcount = temp;
1846 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001847
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001848 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1849 xfs_iext_next(ifp, &bma->icur);
1850 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 break;
1852
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001853 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 /*
1855 * Filling in the last part of a previous delayed allocation.
1856 * The right neighbor is not contiguous.
1857 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001858 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001859 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001860 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1862 else {
1863 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001864 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001865 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001867 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001868 error = xfs_btree_insert(bma->cur, &i);
1869 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001871 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001873
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001874 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001875 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster81ba8f32018-07-11 22:26:16 -07001876 bma->firstblock, &bma->cur, 1, &tmp_rval,
1877 whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 rval |= tmp_rval;
1879 if (error)
1880 goto done;
1881 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001882
1883 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001884 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001885 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001886 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001887
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001888 PREV.br_startblock = nullstartblock(da_new);
1889 PREV.br_blockcount = temp;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001890 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001891 xfs_iext_next(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 break;
1893
1894 case 0:
1895 /*
1896 * Filling in the middle part of a previous delayed allocation.
1897 * Contiguity is impossible here.
1898 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001899 *
1900 * We start with a delayed allocation:
1901 *
1902 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1903 * PREV @ idx
1904 *
1905 * and we are allocating:
1906 * +rrrrrrrrrrrrrrrrr+
1907 * new
1908 *
1909 * and we set it up for insertion as:
1910 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1911 * new
1912 * PREV @ idx LEFT RIGHT
1913 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001915 old = PREV;
1916
1917 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001918 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001919
1920 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001921 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001922 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001923 RIGHT.br_blockcount =
1924 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1925 RIGHT.br_startblock =
1926 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1927 RIGHT.br_blockcount));
1928
1929 /* truncate PREV */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001930 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1931 PREV.br_startblock =
1932 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1933 PREV.br_blockcount));
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001934 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001935
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001936 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001937 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1938 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001939 (*nextents)++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001940
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001941 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1943 else {
1944 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001945 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001946 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001948 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001949 error = xfs_btree_insert(bma->cur, &i);
1950 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001952 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001954
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001955 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001956 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster81ba8f32018-07-11 22:26:16 -07001957 bma->firstblock, &bma->cur, 1,
1958 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 rval |= tmp_rval;
1960 if (error)
1961 goto done;
1962 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001963
1964 da_new = startblockval(PREV.br_startblock) +
1965 startblockval(RIGHT.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 break;
1967
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001968 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1969 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1970 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1971 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1972 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1973 case BMAP_LEFT_CONTIG:
1974 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 /*
1976 * These cases are all impossible.
1977 */
1978 ASSERT(0);
1979 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001980
Darrick J. Wong95eb3082018-05-09 10:02:32 -07001981 /* add reverse mapping unless caller opted out */
1982 if (!(bma->flags & XFS_BMAPI_NORMAP)) {
Brian Foster3e3673e2018-07-11 22:26:14 -07001983 error = xfs_rmap_map_extent(mp, bma->tp->t_dfops, bma->ip,
Darrick J. Wong95eb3082018-05-09 10:02:32 -07001984 whichfork, new);
1985 if (error)
1986 goto done;
1987 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10001988
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001989 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001990 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001991 int tmp_logflags; /* partial log flag return val */
1992
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001993 ASSERT(bma->cur == NULL);
1994 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster81ba8f32018-07-11 22:26:16 -07001995 bma->firstblock, &bma->cur, da_old > 0,
1996 &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001997 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001998 if (error)
1999 goto done;
2000 }
2001
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002002 if (bma->cur) {
2003 da_new += bma->cur->bc_private.b.allocated;
2004 bma->cur->bc_private.b.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002005 }
2006
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002007 /* adjust for changes in reserved delayed indirect blocks */
2008 if (da_new != da_old) {
2009 ASSERT(state == 0 || da_new < da_old);
2010 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2011 false);
2012 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002013
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002014 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002016 if (whichfork != XFS_COW_FORK)
2017 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 return error;
2019#undef LEFT
2020#undef RIGHT
2021#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
2024/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002025 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 */
2027STATIC int /* error */
2028xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002029 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002031 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002032 struct xfs_iext_cursor *icur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002034 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002035 xfs_fsblock_t *first, /* pointer to firstblock variable */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002036 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002041 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2044 /* left is 0, right is 1, prev is 2 */
2045 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002046 int state = xfs_bmap_fork_to_state(whichfork);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002047 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002048 struct xfs_bmbt_irec old;
Brian Foster81ba8f32018-07-11 22:26:16 -07002049 struct xfs_defer_ops *dfops = tp ? tp->t_dfops : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002051 *logflagsp = 0;
2052
2053 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002054 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002055
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002056 ASSERT(!isnullstartblock(new->br_startblock));
2057
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002058 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060#define LEFT r[0]
2061#define RIGHT r[1]
2062#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 /*
2065 * Set up a bunch of variables to make the tests simpler.
2066 */
2067 error = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002068 xfs_iext_get_extent(ifp, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002069 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 new_endoff = new->br_startoff + new->br_blockcount;
2071 ASSERT(PREV.br_startoff <= new->br_startoff);
2072 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 /*
2075 * Set flags determining what part of the previous oldext allocation
2076 * extent is being replaced by a newext allocation.
2077 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002078 if (PREV.br_startoff == new->br_startoff)
2079 state |= BMAP_LEFT_FILLING;
2080 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2081 state |= BMAP_RIGHT_FILLING;
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 /*
2084 * Check and set flags if this segment has a left neighbor.
2085 * Don't set contiguous if the combined extent would be too large.
2086 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002087 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002088 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002089 if (isnullstartblock(LEFT.br_startblock))
2090 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002092
2093 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2094 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2095 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002096 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002097 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2098 state |= BMAP_LEFT_CONTIG;
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 /*
2101 * Check and set flags if this segment has a right neighbor.
2102 * Don't set contiguous if the combined extent would be too large.
2103 * Also check for all-three-contiguous being too large.
2104 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002105 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002106 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002107 if (isnullstartblock(RIGHT.br_startblock))
2108 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002110
2111 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2112 new_endoff == RIGHT.br_startoff &&
2113 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002114 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002115 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2116 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2117 BMAP_RIGHT_FILLING)) !=
2118 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2119 BMAP_RIGHT_FILLING) ||
2120 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2121 <= MAXEXTLEN))
2122 state |= BMAP_RIGHT_CONTIG;
2123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 /*
2125 * Switch out based on the FILLING and CONTIG state bits.
2126 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002127 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2128 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2129 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2130 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 /*
2132 * Setting all of a previous oldext extent to newext.
2133 * The left and right neighbors are both contiguous with new.
2134 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002135 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002136
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002137 xfs_iext_remove(ip, icur, state);
2138 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002139 xfs_iext_prev(ifp, icur);
2140 xfs_iext_update_extent(ip, state, icur, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002141 XFS_IFORK_NEXT_SET(ip, whichfork,
2142 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (cur == NULL)
2144 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2145 else {
2146 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002147 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2148 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002150 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002151 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002153 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002154 if ((error = xfs_btree_decrement(cur, 0, &i)))
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 Hellwiga67d00a2017-10-17 14:16:26 -07002163 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002164 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 goto done;
2166 }
2167 break;
2168
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002169 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 /*
2171 * Setting all of a previous oldext extent to newext.
2172 * The left neighbor is contiguous, the right is not.
2173 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002174 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002175
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002176 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002177 xfs_iext_prev(ifp, icur);
2178 xfs_iext_update_extent(ip, state, icur, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002179 XFS_IFORK_NEXT_SET(ip, whichfork,
2180 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (cur == NULL)
2182 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2183 else {
2184 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002185 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2186 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002188 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002189 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002191 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002192 if ((error = xfs_btree_decrement(cur, 0, &i)))
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 Hellwiga67d00a2017-10-17 14:16:26 -07002195 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002196 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 goto done;
2198 }
2199 break;
2200
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002201 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 /*
2203 * Setting all of a previous oldext extent to newext.
2204 * The right neighbor is contiguous, the left is not.
2205 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002206 PREV.br_blockcount += RIGHT.br_blockcount;
2207 PREV.br_state = new->br_state;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002208
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002209 xfs_iext_next(ifp, icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002210 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002211 xfs_iext_prev(ifp, icur);
2212 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002213
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002214 XFS_IFORK_NEXT_SET(ip, whichfork,
2215 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 if (cur == NULL)
2217 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2218 else {
2219 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002220 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2221 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002223 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002224 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002226 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002227 if ((error = xfs_btree_decrement(cur, 0, &i)))
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 Hellwiga67d00a2017-10-17 14:16:26 -07002230 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002231 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 goto done;
2233 }
2234 break;
2235
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002236 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 /*
2238 * Setting all of a previous oldext extent to newext.
2239 * Neither the left nor right neighbors are contiguous with
2240 * the new one.
2241 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002242 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002243 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 if (cur == NULL)
2246 rval = XFS_ILOG_DEXT;
2247 else {
2248 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002249 error = xfs_bmbt_lookup_eq(cur, new, &i);
2250 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002252 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002253 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002254 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 goto done;
2256 }
2257 break;
2258
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002259 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 /*
2261 * Setting the first part of a previous oldext extent to newext.
2262 * The left neighbor is contiguous.
2263 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002264 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002265
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002266 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002267 PREV.br_startoff += new->br_blockcount;
2268 PREV.br_startblock += new->br_blockcount;
2269 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002270
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002271 xfs_iext_update_extent(ip, state, icur, &PREV);
2272 xfs_iext_prev(ifp, icur);
2273 xfs_iext_update_extent(ip, state, icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 if (cur == NULL)
2276 rval = XFS_ILOG_DEXT;
2277 else {
2278 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002279 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002280 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002282 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002283 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002284 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002286 error = xfs_btree_decrement(cur, 0, &i);
2287 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002289 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002290 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 goto done;
2292 }
2293 break;
2294
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002295 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 /*
2297 * Setting the first part of a previous oldext extent to newext.
2298 * The left neighbor is not contiguous.
2299 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002300 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002301 PREV.br_startoff += new->br_blockcount;
2302 PREV.br_startblock += new->br_blockcount;
2303 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002304
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002305 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002306 xfs_iext_insert(ip, icur, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002307 XFS_IFORK_NEXT_SET(ip, whichfork,
2308 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 if (cur == NULL)
2310 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2311 else {
2312 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002313 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002314 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002316 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002317 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002318 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 goto done;
2320 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002321 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002323 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 }
2325 break;
2326
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002327 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 /*
2329 * Setting the last part of a previous oldext extent to newext.
2330 * The right neighbor is contiguous with the new allocation.
2331 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002332 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002333 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002334
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002335 RIGHT.br_startoff = new->br_startoff;
2336 RIGHT.br_startblock = new->br_startblock;
2337 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002338
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002339 xfs_iext_update_extent(ip, state, icur, &PREV);
2340 xfs_iext_next(ifp, icur);
2341 xfs_iext_update_extent(ip, state, icur, &RIGHT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (cur == NULL)
2344 rval = XFS_ILOG_DEXT;
2345 else {
2346 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002347 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002348 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002350 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002351 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002352 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002354 error = xfs_btree_increment(cur, 0, &i);
2355 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002357 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002358 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 goto done;
2360 }
2361 break;
2362
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002363 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 /*
2365 * Setting the last part of a previous oldext extent to newext.
2366 * The right neighbor is not contiguous.
2367 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002368 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002369 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002370
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002371 xfs_iext_update_extent(ip, state, icur, &PREV);
2372 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002373 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002374
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002375 XFS_IFORK_NEXT_SET(ip, whichfork,
2376 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 if (cur == NULL)
2378 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2379 else {
2380 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002381 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002382 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002384 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002385 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002386 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002388 error = xfs_bmbt_lookup_eq(cur, new, &i);
2389 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002391 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002392 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002394 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 }
2396 break;
2397
2398 case 0:
2399 /*
2400 * Setting the middle part of a previous oldext extent to
2401 * newext. Contiguity is impossible here.
2402 * One extent becomes three extents.
2403 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002404 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002405 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 r[0] = *new;
2408 r[1].br_startoff = new_endoff;
2409 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002410 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002412 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002413
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002414 xfs_iext_update_extent(ip, state, icur, &PREV);
2415 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002416 xfs_iext_insert(ip, icur, &r[1], state);
2417 xfs_iext_insert(ip, icur, &r[0], state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002418
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002419 XFS_IFORK_NEXT_SET(ip, whichfork,
2420 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 if (cur == NULL)
2422 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2423 else {
2424 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002425 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002426 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002428 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002430 error = xfs_bmbt_update(cur, &r[1]);
2431 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 goto done;
2433 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002435 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002437 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002438 /*
2439 * Reset the cursor to the position of the new extent
2440 * we are about to insert as we can't trust it after
2441 * the previous insert.
2442 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002443 error = xfs_bmbt_lookup_eq(cur, new, &i);
2444 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002446 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002448 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002450 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 }
2452 break;
2453
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002454 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2455 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2456 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2457 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2458 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2459 case BMAP_LEFT_CONTIG:
2460 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 /*
2462 * These cases are all impossible.
2463 */
2464 ASSERT(0);
2465 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002466
Darrick J. Wong9c194642016-08-03 12:16:05 +10002467 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002468 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002469 if (error)
2470 goto done;
2471
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002472 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002473 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002474 int tmp_logflags; /* partial log flag return val */
2475
2476 ASSERT(cur == NULL);
Brian Foster81ba8f32018-07-11 22:26:16 -07002477 error = xfs_bmap_extents_to_btree(tp, ip, first, &cur, 0,
2478 &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002479 *logflagsp |= tmp_logflags;
2480 if (error)
2481 goto done;
2482 }
2483
2484 /* clear out the allocated field, done with it now in any case. */
2485 if (cur) {
2486 cur->bc_private.b.allocated = 0;
2487 *curp = cur;
2488 }
2489
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002490 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002492 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 return error;
2494#undef LEFT
2495#undef RIGHT
2496#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497}
2498
2499/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002500 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002502STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503xfs_bmap_add_extent_hole_delay(
2504 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002505 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002506 struct xfs_iext_cursor *icur,
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002507 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002509 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2511 xfs_filblks_t newlen=0; /* new indirect size */
2512 xfs_filblks_t oldlen=0; /* old indirect size */
2513 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002514 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002515 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002517 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002518 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 /*
2521 * Check and set flags if this segment has a left neighbor
2522 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002523 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002524 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002525 if (isnullstartblock(left.br_startblock))
2526 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 /*
2530 * Check and set flags if the current (right) segment exists.
2531 * If it doesn't exist, we're converting the hole at end-of-file.
2532 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002533 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002534 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002535 if (isnullstartblock(right.br_startblock))
2536 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002538
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 /*
2540 * Set contiguity flags on the left and right neighbors.
2541 * Don't let extents get too large, even if the pieces are contiguous.
2542 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002543 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2544 left.br_startoff + left.br_blockcount == new->br_startoff &&
2545 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2546 state |= BMAP_LEFT_CONTIG;
2547
2548 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2549 new->br_startoff + new->br_blockcount == right.br_startoff &&
2550 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2551 (!(state & BMAP_LEFT_CONTIG) ||
2552 (left.br_blockcount + new->br_blockcount +
2553 right.br_blockcount <= MAXEXTLEN)))
2554 state |= BMAP_RIGHT_CONTIG;
2555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 /*
2557 * Switch out based on the contiguity flags.
2558 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002559 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2560 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 /*
2562 * New allocation is contiguous with delayed allocations
2563 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002564 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 */
2566 temp = left.br_blockcount + new->br_blockcount +
2567 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002568
Eric Sandeen9d87c312009-01-14 23:22:07 -06002569 oldlen = startblockval(left.br_startblock) +
2570 startblockval(new->br_startblock) +
2571 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002572 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2573 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002574 left.br_startblock = nullstartblock(newlen);
2575 left.br_blockcount = temp;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002576
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002577 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002578 xfs_iext_prev(ifp, icur);
2579 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 break;
2581
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002582 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 /*
2584 * New allocation is contiguous with a delayed allocation
2585 * on the left.
2586 * Merge the new allocation with the left neighbor.
2587 */
2588 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002589
Eric Sandeen9d87c312009-01-14 23:22:07 -06002590 oldlen = startblockval(left.br_startblock) +
2591 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002592 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2593 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002594 left.br_blockcount = temp;
2595 left.br_startblock = nullstartblock(newlen);
Christoph Hellwig41d196f2017-11-03 10:34:39 -07002596
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002597 xfs_iext_prev(ifp, icur);
2598 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 break;
2600
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002601 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 /*
2603 * New allocation is contiguous with a delayed allocation
2604 * on the right.
2605 * Merge the new allocation with the right neighbor.
2606 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002608 oldlen = startblockval(new->br_startblock) +
2609 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002610 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2611 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002612 right.br_startoff = new->br_startoff;
2613 right.br_startblock = nullstartblock(newlen);
2614 right.br_blockcount = temp;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002615 xfs_iext_update_extent(ip, state, icur, &right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 break;
2617
2618 case 0:
2619 /*
2620 * New allocation is not contiguous with another
2621 * delayed allocation.
2622 * Insert a new entry.
2623 */
2624 oldlen = newlen = 0;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002625 xfs_iext_insert(ip, icur, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 break;
2627 }
2628 if (oldlen != newlen) {
2629 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002630 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2631 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 /*
2633 * Nothing to do for disk quota accounting here.
2634 */
2635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636}
2637
2638/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002639 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 */
2641STATIC int /* error */
2642xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002643 struct xfs_trans *tp,
2644 struct xfs_inode *ip,
2645 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002646 struct xfs_iext_cursor *icur,
Christoph Hellwig6d045582017-04-11 16:45:54 -07002647 struct xfs_btree_cur **curp,
2648 struct xfs_bmbt_irec *new,
2649 xfs_fsblock_t *first,
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002650 int *logflagsp,
2651 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002653 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2654 struct xfs_mount *mp = ip->i_mount;
2655 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2659 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002660 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002661 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002662 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002664 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002665 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002666
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002667 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002668
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 /*
2670 * Check and set flags if this segment has a left neighbor.
2671 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002672 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002673 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002674 if (isnullstartblock(left.br_startblock))
2675 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 /*
2679 * Check and set flags if this segment has a current value.
2680 * Not true if we're inserting into the "hole" at eof.
2681 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002682 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002683 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002684 if (isnullstartblock(right.br_startblock))
2685 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 /*
2689 * We're inserting a real allocation between "left" and "right".
2690 * Set the contiguity flags. Don't let extents get too large.
2691 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002692 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2693 left.br_startoff + left.br_blockcount == new->br_startoff &&
2694 left.br_startblock + left.br_blockcount == new->br_startblock &&
2695 left.br_state == new->br_state &&
2696 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2697 state |= BMAP_LEFT_CONTIG;
2698
2699 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2700 new->br_startoff + new->br_blockcount == right.br_startoff &&
2701 new->br_startblock + new->br_blockcount == right.br_startblock &&
2702 new->br_state == right.br_state &&
2703 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2704 (!(state & BMAP_LEFT_CONTIG) ||
2705 left.br_blockcount + new->br_blockcount +
2706 right.br_blockcount <= MAXEXTLEN))
2707 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002709 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 /*
2711 * Select which case we're in here, and implement it.
2712 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002713 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2714 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 /*
2716 * New allocation is contiguous with real allocations on the
2717 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002718 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002720 left.br_blockcount += new->br_blockcount + right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002721
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002722 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002723 xfs_iext_prev(ifp, icur);
2724 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002725
Christoph Hellwig6d045582017-04-11 16:45:54 -07002726 XFS_IFORK_NEXT_SET(ip, whichfork,
2727 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2728 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002729 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002730 } else {
2731 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002732 error = xfs_bmbt_lookup_eq(cur, &right, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002733 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002734 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002735 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002736 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002737 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002738 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002739 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002740 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002741 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002742 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002743 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002744 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002745 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002746 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002748 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002750 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 /*
2752 * New allocation is contiguous with a real allocation
2753 * on the left.
2754 * Merge the new allocation with the left neighbor.
2755 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002756 old = left;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002757 left.br_blockcount += new->br_blockcount;
Christoph Hellwig1d2e0082017-11-03 10:34:40 -07002758
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002759 xfs_iext_prev(ifp, icur);
2760 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002761
Christoph Hellwig6d045582017-04-11 16:45:54 -07002762 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002763 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002764 } else {
2765 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002766 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002767 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002768 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002769 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002770 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002771 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002772 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002774 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002776 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 /*
2778 * New allocation is contiguous with a real allocation
2779 * on the right.
2780 * Merge the new allocation with the right neighbor.
2781 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002782 old = right;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07002783
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002784 right.br_startoff = new->br_startoff;
2785 right.br_startblock = new->br_startblock;
2786 right.br_blockcount += new->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002787 xfs_iext_update_extent(ip, state, icur, &right);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002788
Christoph Hellwig6d045582017-04-11 16:45:54 -07002789 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002790 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002791 } else {
2792 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002793 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002794 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002795 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002796 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002797 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002798 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002799 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002801 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
2803 case 0:
2804 /*
2805 * New allocation is not contiguous with another
2806 * real allocation.
2807 * Insert a new entry.
2808 */
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002809 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002810 XFS_IFORK_NEXT_SET(ip, whichfork,
2811 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2812 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002813 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002814 } else {
2815 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002816 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002817 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002818 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002819 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002820 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002821 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002822 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002823 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002825 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002827
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002828 /* add reverse mapping unless caller opted out */
2829 if (!(flags & XFS_BMAPI_NORMAP)) {
Brian Foster81ba8f32018-07-11 22:26:16 -07002830 error = xfs_rmap_map_extent(mp, tp->t_dfops, ip, whichfork,
2831 new);
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002832 if (error)
2833 goto done;
2834 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10002835
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002836 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002837 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002838 int tmp_logflags; /* partial log flag return val */
2839
Christoph Hellwig6d045582017-04-11 16:45:54 -07002840 ASSERT(cur == NULL);
Brian Foster81ba8f32018-07-11 22:26:16 -07002841 error = xfs_bmap_extents_to_btree(tp, ip, first, curp, 0,
2842 &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002843 *logflagsp |= tmp_logflags;
2844 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002845 if (error)
2846 goto done;
2847 }
2848
2849 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002850 if (cur)
2851 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002852
Christoph Hellwig6d045582017-04-11 16:45:54 -07002853 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002854done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002855 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002856 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857}
2858
Nathan Scottdd9f4382006-01-11 15:28:28 +11002859/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11002860 * Functions used in the extent read, allocate and remove paths
2861 */
2862
2863/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002864 * Adjust the size of the new extent based on di_extsize and rt extsize.
2865 */
Dave Chinner68988112013-08-12 20:49:42 +10002866int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002867xfs_bmap_extsize_align(
2868 xfs_mount_t *mp,
2869 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2870 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2871 xfs_extlen_t extsz, /* align to this extent size */
2872 int rt, /* is this a realtime inode? */
2873 int eof, /* is extent at end-of-file? */
2874 int delay, /* creating delalloc extent? */
2875 int convert, /* overwriting unwritten extent? */
2876 xfs_fileoff_t *offp, /* in/out: aligned offset */
2877 xfs_extlen_t *lenp) /* in/out: aligned length */
2878{
2879 xfs_fileoff_t orig_off; /* original offset */
2880 xfs_extlen_t orig_alen; /* original length */
2881 xfs_fileoff_t orig_end; /* original off+len */
2882 xfs_fileoff_t nexto; /* next file offset */
2883 xfs_fileoff_t prevo; /* previous file offset */
2884 xfs_fileoff_t align_off; /* temp for offset */
2885 xfs_extlen_t align_alen; /* temp for length */
2886 xfs_extlen_t temp; /* temp for calculations */
2887
2888 if (convert)
2889 return 0;
2890
2891 orig_off = align_off = *offp;
2892 orig_alen = align_alen = *lenp;
2893 orig_end = orig_off + orig_alen;
2894
2895 /*
2896 * If this request overlaps an existing extent, then don't
2897 * attempt to perform any additional alignment.
2898 */
2899 if (!delay && !eof &&
2900 (orig_off >= gotp->br_startoff) &&
2901 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2902 return 0;
2903 }
2904
2905 /*
2906 * If the file offset is unaligned vs. the extent size
2907 * we need to align it. This will be possible unless
2908 * the file was previously written with a kernel that didn't
2909 * perform this alignment, or if a truncate shot us in the
2910 * foot.
2911 */
Dave Chinner0703a8e2018-06-08 09:54:22 -07002912 div_u64_rem(orig_off, extsz, &temp);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002913 if (temp) {
2914 align_alen += temp;
2915 align_off -= temp;
2916 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10002917
2918 /* Same adjustment for the end of the requested area. */
2919 temp = (align_alen % extsz);
2920 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11002921 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10002922
2923 /*
2924 * For large extent hint sizes, the aligned extent might be larger than
2925 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2926 * the length back under MAXEXTLEN. The outer allocation loops handle
2927 * short allocation just fine, so it is safe to do this. We only want to
2928 * do it when we are forced to, though, because it means more allocation
2929 * operations are required.
2930 */
2931 while (align_alen > MAXEXTLEN)
2932 align_alen -= extsz;
2933 ASSERT(align_alen <= MAXEXTLEN);
2934
Nathan Scottdd9f4382006-01-11 15:28:28 +11002935 /*
2936 * If the previous block overlaps with this proposed allocation
2937 * then move the start forward without adjusting the length.
2938 */
2939 if (prevp->br_startoff != NULLFILEOFF) {
2940 if (prevp->br_startblock == HOLESTARTBLOCK)
2941 prevo = prevp->br_startoff;
2942 else
2943 prevo = prevp->br_startoff + prevp->br_blockcount;
2944 } else
2945 prevo = 0;
2946 if (align_off != orig_off && align_off < prevo)
2947 align_off = prevo;
2948 /*
2949 * If the next block overlaps with this proposed allocation
2950 * then move the start back without adjusting the length,
2951 * but not before offset 0.
2952 * This may of course make the start overlap previous block,
2953 * and if we hit the offset 0 limit then the next block
2954 * can still overlap too.
2955 */
2956 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2957 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2958 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2959 nexto = gotp->br_startoff + gotp->br_blockcount;
2960 else
2961 nexto = gotp->br_startoff;
2962 } else
2963 nexto = NULLFILEOFF;
2964 if (!eof &&
2965 align_off + align_alen != orig_end &&
2966 align_off + align_alen > nexto)
2967 align_off = nexto > align_alen ? nexto - align_alen : 0;
2968 /*
2969 * If we're now overlapping the next or previous extent that
2970 * means we can't fit an extsz piece in this hole. Just move
2971 * the start forward to the first valid spot and set
2972 * the length so we hit the end.
2973 */
2974 if (align_off != orig_off && align_off < prevo)
2975 align_off = prevo;
2976 if (align_off + align_alen != orig_end &&
2977 align_off + align_alen > nexto &&
2978 nexto != NULLFILEOFF) {
2979 ASSERT(nexto > prevo);
2980 align_alen = nexto - align_off;
2981 }
2982
2983 /*
2984 * If realtime, and the result isn't a multiple of the realtime
2985 * extent size we need to remove blocks until it is.
2986 */
2987 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2988 /*
2989 * We're not covering the original request, or
2990 * we won't be able to once we fix the length.
2991 */
2992 if (orig_off < align_off ||
2993 orig_end > align_off + align_alen ||
2994 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10002995 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11002996 /*
2997 * Try to fix it by moving the start up.
2998 */
2999 if (align_off + temp <= orig_off) {
3000 align_alen -= temp;
3001 align_off += temp;
3002 }
3003 /*
3004 * Try to fix it by moving the end in.
3005 */
3006 else if (align_off + align_alen - temp >= orig_end)
3007 align_alen -= temp;
3008 /*
3009 * Set the start to the minimum then trim the length.
3010 */
3011 else {
3012 align_alen -= orig_off - align_off;
3013 align_off = orig_off;
3014 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3015 }
3016 /*
3017 * Result doesn't cover the request, fail it.
3018 */
3019 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003020 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003021 } else {
3022 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003023 /* see MAXEXTLEN handling above */
3024 ASSERT(orig_end <= align_off + align_alen ||
3025 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003026 }
3027
3028#ifdef DEBUG
3029 if (!eof && gotp->br_startoff != NULLFILEOFF)
3030 ASSERT(align_off + align_alen <= gotp->br_startoff);
3031 if (prevp->br_startoff != NULLFILEOFF)
3032 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3033#endif
3034
3035 *lenp = align_alen;
3036 *offp = align_off;
3037 return 0;
3038}
3039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040#define XFS_ALLOC_GAP_UNITS 4
3041
Dave Chinner68988112013-08-12 20:49:42 +10003042void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003043xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003044 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045{
3046 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3048 xfs_mount_t *mp; /* mount point structure */
3049 int nullfb; /* true if ap->firstblock isn't set */
3050 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
3052#define ISVALID(x,y) \
3053 (rt ? \
3054 (x) < mp->m_sb.sb_rblocks : \
3055 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3056 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3057 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3058
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003060 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003061 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3062 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003063 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 /*
3065 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003066 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003068 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3069 !isnullstartblock(ap->prev.br_startblock) &&
3070 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3071 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003072 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 /*
3074 * Adjust for the gap between prevp and us.
3075 */
Dave Chinner3a756672011-09-18 20:40:58 +00003076 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003077 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003079 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3080 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 }
3082 /*
3083 * If not at eof, then compare the two neighbor blocks.
3084 * Figure out whether either one gives us a good starting point,
3085 * and pick the better one.
3086 */
3087 else if (!ap->eof) {
3088 xfs_fsblock_t gotbno; /* right side block number */
3089 xfs_fsblock_t gotdiff=0; /* right side difference */
3090 xfs_fsblock_t prevbno; /* left side block number */
3091 xfs_fsblock_t prevdiff=0; /* left side difference */
3092
3093 /*
3094 * If there's a previous (left) block, select a requested
3095 * start block based on it.
3096 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003097 if (ap->prev.br_startoff != NULLFILEOFF &&
3098 !isnullstartblock(ap->prev.br_startblock) &&
3099 (prevbno = ap->prev.br_startblock +
3100 ap->prev.br_blockcount) &&
3101 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 /*
3103 * Calculate gap to end of previous block.
3104 */
Dave Chinner3a756672011-09-18 20:40:58 +00003105 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003106 (ap->prev.br_startoff +
3107 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 /*
3109 * Figure the startblock based on the previous block's
3110 * end and the gap size.
3111 * Heuristic!
3112 * If the gap is large relative to the piece we're
3113 * allocating, or using it gives us an invalid block
3114 * number, then just use the end of the previous block.
3115 */
Dave Chinner3a756672011-09-18 20:40:58 +00003116 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003118 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 prevbno += adjust;
3120 else
3121 prevdiff += adjust;
3122 /*
3123 * If the firstblock forbids it, can't use it,
3124 * must use default.
3125 */
3126 if (!rt && !nullfb &&
3127 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3128 prevbno = NULLFSBLOCK;
3129 }
3130 /*
3131 * No previous block or can't follow it, just default.
3132 */
3133 else
3134 prevbno = NULLFSBLOCK;
3135 /*
3136 * If there's a following (right) block, select a requested
3137 * start block based on it.
3138 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003139 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 /*
3141 * Calculate gap to start of next block.
3142 */
Dave Chinner3a756672011-09-18 20:40:58 +00003143 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 /*
3145 * Figure the startblock based on the next block's
3146 * start and the gap size.
3147 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003148 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 /*
3150 * Heuristic!
3151 * If the gap is large relative to the piece we're
3152 * allocating, or using it gives us an invalid block
3153 * number, then just use the start of the next block
3154 * offset by our length.
3155 */
Dave Chinner3a756672011-09-18 20:40:58 +00003156 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 ISVALID(gotbno - gotdiff, gotbno))
3158 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003159 else if (ISVALID(gotbno - ap->length, gotbno)) {
3160 gotbno -= ap->length;
3161 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 } else
3163 gotdiff += adjust;
3164 /*
3165 * If the firstblock forbids it, can't use it,
3166 * must use default.
3167 */
3168 if (!rt && !nullfb &&
3169 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3170 gotbno = NULLFSBLOCK;
3171 }
3172 /*
3173 * No next block, just default.
3174 */
3175 else
3176 gotbno = NULLFSBLOCK;
3177 /*
3178 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003179 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 */
3181 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003182 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003184 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003186 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003188#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003189}
3190
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003191static int
3192xfs_bmap_longest_free_extent(
3193 struct xfs_trans *tp,
3194 xfs_agnumber_t ag,
3195 xfs_extlen_t *blen,
3196 int *notinit)
3197{
3198 struct xfs_mount *mp = tp->t_mountp;
3199 struct xfs_perag *pag;
3200 xfs_extlen_t longest;
3201 int error = 0;
3202
3203 pag = xfs_perag_get(mp, ag);
3204 if (!pag->pagf_init) {
3205 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3206 if (error)
3207 goto out;
3208
3209 if (!pag->pagf_init) {
3210 *notinit = 1;
3211 goto out;
3212 }
3213 }
3214
Eric Sandeena1f69412018-04-06 10:09:42 -07003215 longest = xfs_alloc_longest_free_extent(pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003216 xfs_alloc_min_freelist(mp, pag),
3217 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003218 if (*blen < longest)
3219 *blen = longest;
3220
3221out:
3222 xfs_perag_put(pag);
3223 return error;
3224}
3225
3226static void
3227xfs_bmap_select_minlen(
3228 struct xfs_bmalloca *ap,
3229 struct xfs_alloc_arg *args,
3230 xfs_extlen_t *blen,
3231 int notinit)
3232{
3233 if (notinit || *blen < ap->minlen) {
3234 /*
3235 * Since we did a BUF_TRYLOCK above, it is possible that
3236 * there is space for this request.
3237 */
3238 args->minlen = ap->minlen;
3239 } else if (*blen < args->maxlen) {
3240 /*
3241 * If the best seen length is less than the request length,
3242 * use the best as the minimum.
3243 */
3244 args->minlen = *blen;
3245 } else {
3246 /*
3247 * Otherwise we've seen an extent as big as maxlen, use that
3248 * as the minimum.
3249 */
3250 args->minlen = args->maxlen;
3251 }
3252}
3253
Nathan Scotta365bdd2006-03-14 13:34:16 +11003254STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003255xfs_bmap_btalloc_nullfb(
3256 struct xfs_bmalloca *ap,
3257 struct xfs_alloc_arg *args,
3258 xfs_extlen_t *blen)
3259{
3260 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003261 xfs_agnumber_t ag, startag;
3262 int notinit = 0;
3263 int error;
3264
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003265 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003266 args->total = ap->total;
3267
Christoph Hellwigc467c042010-02-15 23:34:42 +00003268 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3269 if (startag == NULLAGNUMBER)
3270 startag = ag = 0;
3271
Dave Chinner14b064c2011-01-27 12:16:28 +11003272 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003273 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3274 &notinit);
3275 if (error)
3276 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003277
Christoph Hellwigc467c042010-02-15 23:34:42 +00003278 if (++ag == mp->m_sb.sb_agcount)
3279 ag = 0;
3280 if (ag == startag)
3281 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003282 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003283
3284 xfs_bmap_select_minlen(ap, args, blen, notinit);
3285 return 0;
3286}
3287
3288STATIC int
3289xfs_bmap_btalloc_filestreams(
3290 struct xfs_bmalloca *ap,
3291 struct xfs_alloc_arg *args,
3292 xfs_extlen_t *blen)
3293{
3294 struct xfs_mount *mp = ap->ip->i_mount;
3295 xfs_agnumber_t ag;
3296 int notinit = 0;
3297 int error;
3298
3299 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3300 args->total = ap->total;
3301
3302 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3303 if (ag == NULLAGNUMBER)
3304 ag = 0;
3305
3306 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3307 if (error)
3308 return error;
3309
3310 if (*blen < args->maxlen) {
3311 error = xfs_filestream_new_ag(ap, &ag);
3312 if (error)
3313 return error;
3314
3315 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3316 &notinit);
3317 if (error)
3318 return error;
3319
3320 }
3321
3322 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003323
3324 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003325 * Set the failure fallback case to look in the selected AG as stream
3326 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003327 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003328 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003329 return 0;
3330}
3331
Darrick J. Wong751f3762018-01-25 13:58:13 -08003332/* Update all inode and quota accounting for the allocation we just did. */
3333static void
3334xfs_bmap_btalloc_accounting(
3335 struct xfs_bmalloca *ap,
3336 struct xfs_alloc_arg *args)
3337{
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003338 if (ap->flags & XFS_BMAPI_COWFORK) {
3339 /*
3340 * COW fork blocks are in-core only and thus are treated as
3341 * in-core quota reservation (like delalloc blocks) even when
3342 * converted to real blocks. The quota reservation is not
3343 * accounted to disk until blocks are remapped to the data
3344 * fork. So if these blocks were previously delalloc, we
3345 * already have quota reservation and there's nothing to do
3346 * yet.
3347 */
3348 if (ap->wasdel)
3349 return;
3350
3351 /*
3352 * Otherwise, we've allocated blocks in a hole. The transaction
3353 * has acquired in-core quota reservation for this extent.
3354 * Rather than account these as real blocks, however, we reduce
3355 * the transaction quota reservation based on the allocation.
3356 * This essentially transfers the transaction quota reservation
3357 * to that of a delalloc extent.
3358 */
3359 ap->ip->i_delayed_blks += args->len;
3360 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3361 -(long)args->len);
3362 return;
3363 }
3364
3365 /* data/attr fork only */
3366 ap->ip->i_d.di_nblocks += args->len;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003367 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3368 if (ap->wasdel)
3369 ap->ip->i_delayed_blks -= args->len;
3370 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3371 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3372 args->len);
3373}
3374
Christoph Hellwigc467c042010-02-15 23:34:42 +00003375STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003376xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003377 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003378{
3379 xfs_mount_t *mp; /* mount point structure */
3380 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003381 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003382 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003383 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003384 xfs_alloc_arg_t args;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003385 xfs_fileoff_t orig_offset;
3386 xfs_extlen_t orig_length;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003387 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003388 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003389 int nullfb; /* true if ap->firstblock isn't set */
3390 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003391 int tryagain;
3392 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003393 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003394
Dave Chinnera99ebf42011-12-01 11:24:20 +00003395 ASSERT(ap->length);
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003396 orig_offset = ap->offset;
3397 orig_length = ap->length;
Dave Chinnera99ebf42011-12-01 11:24:20 +00003398
Nathan Scotta365bdd2006-03-14 13:34:16 +11003399 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003400
3401 /* stripe alignment for allocation is determined by mount parameters */
3402 stripe_align = 0;
3403 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3404 stripe_align = mp->m_swidth;
3405 else if (mp->m_dalign)
3406 stripe_align = mp->m_dalign;
3407
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003408 if (ap->flags & XFS_BMAPI_COWFORK)
3409 align = xfs_get_cowextsz_hint(ap->ip);
3410 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003411 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003412 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003413 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003414 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003415 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003416 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003417 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003418 }
Dave Chinner33177f052013-12-12 16:34:36 +11003419
3420
Dave Chinner0937e0f2011-09-18 20:40:57 +00003421 nullfb = *ap->firstblock == NULLFSBLOCK;
3422 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003423 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003424 if (xfs_alloc_is_userdata(ap->datatype) &&
3425 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003426 ag = xfs_filestream_lookup_ag(ap->ip);
3427 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003428 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003429 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003430 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003431 }
3432 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003433 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003434
3435 xfs_bmap_adjacent(ap);
3436
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003438 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 * it's in the right allocation group.
3440 */
Dave Chinner3a756672011-09-18 20:40:58 +00003441 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 ;
3443 else
Dave Chinner3a756672011-09-18 20:40:58 +00003444 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 * Normal allocation, done through xfs_alloc_vextent.
3447 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003448 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003449 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003450 args.tp = ap->tp;
3451 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003452 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003453 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003454
3455 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner9bb54cb2018-06-07 07:54:02 -07003456 args.maxlen = min(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003457 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003458 blen = 0;
3459 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003460 /*
3461 * Search for an allocation group with a single extent large
3462 * enough for the request. If one isn't found, then adjust
3463 * the minimum allocation size to the largest space found.
3464 */
Dave Chinner292378e2016-09-26 08:21:28 +10003465 if (xfs_alloc_is_userdata(ap->datatype) &&
3466 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003467 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3468 else
3469 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003470 if (error)
3471 return error;
Brian Foster3e3673e2018-07-11 22:26:14 -07003472 } else if (ap->tp->t_dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003473 if (xfs_inode_is_filestream(ap->ip))
3474 args.type = XFS_ALLOCTYPE_FIRST_AG;
3475 else
3476 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003477 args.total = args.minlen = ap->minlen;
3478 } else {
3479 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3480 args.total = ap->total;
3481 args.minlen = ap->minlen;
3482 }
David Chinner957d0eb2007-06-18 16:50:37 +10003483 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003484 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003485 args.prod = align;
Dave Chinner0703a8e2018-06-08 09:54:22 -07003486 div_u64_rem(ap->offset, args.prod, &args.mod);
3487 if (args.mod)
3488 args.mod = args.prod - args.mod;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003489 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003490 args.prod = 1;
3491 args.mod = 0;
3492 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003493 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner0703a8e2018-06-08 09:54:22 -07003494 div_u64_rem(ap->offset, args.prod, &args.mod);
3495 if (args.mod)
3496 args.mod = args.prod - args.mod;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003497 }
3498 /*
3499 * If we are not low on available data blocks, and the
3500 * underlying logical volume manager is a stripe, and
3501 * the file offset is zero then try to allocate data
3502 * blocks on stripe unit boundary.
3503 * NOTE: ap->aeof is only set if the allocation length
3504 * is >= the stripe unit and the allocation offset is
3505 * at the end of file.
3506 */
Brian Foster3e3673e2018-07-11 22:26:14 -07003507 if (!ap->tp->t_dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003508 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003509 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003510 atype = args.type;
3511 isaligned = 1;
3512 /*
3513 * Adjust for alignment
3514 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003515 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003516 args.minlen = blen - args.alignment;
3517 args.minalignslop = 0;
3518 } else {
3519 /*
3520 * First try an exact bno allocation.
3521 * If it fails then do a near or start bno
3522 * allocation with alignment turned on.
3523 */
3524 atype = args.type;
3525 tryagain = 1;
3526 args.type = XFS_ALLOCTYPE_THIS_BNO;
3527 args.alignment = 1;
3528 /*
3529 * Compute the minlen+alignment for the
3530 * next case. Set slop so that the value
3531 * of minlen+alignment+slop doesn't go up
3532 * between the calls.
3533 */
Dave Chinner33177f052013-12-12 16:34:36 +11003534 if (blen > stripe_align && blen <= args.maxlen)
3535 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003536 else
3537 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003538 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003539 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003540 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003541 args.minlen - 1;
3542 else
3543 args.minalignslop = 0;
3544 }
3545 } else {
3546 args.alignment = 1;
3547 args.minalignslop = 0;
3548 }
3549 args.minleft = ap->minleft;
3550 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003551 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003552 args.datatype = ap->datatype;
3553 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003554 args.ip = ap->ip;
3555
3556 error = xfs_alloc_vextent(&args);
3557 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003558 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003559
Nathan Scotta365bdd2006-03-14 13:34:16 +11003560 if (tryagain && args.fsbno == NULLFSBLOCK) {
3561 /*
3562 * Exact allocation failed. Now try with alignment
3563 * turned on.
3564 */
3565 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003566 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003567 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003568 args.minlen = nextminlen;
3569 args.minalignslop = 0;
3570 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 if ((error = xfs_alloc_vextent(&args)))
3572 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003573 }
3574 if (isaligned && args.fsbno == NULLFSBLOCK) {
3575 /*
3576 * allocation failed, so turn off alignment and
3577 * try again.
3578 */
3579 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003580 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003581 args.alignment = 0;
3582 if ((error = xfs_alloc_vextent(&args)))
3583 return error;
3584 }
3585 if (args.fsbno == NULLFSBLOCK && nullfb &&
3586 args.minlen > ap->minlen) {
3587 args.minlen = ap->minlen;
3588 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003589 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003590 if ((error = xfs_alloc_vextent(&args)))
3591 return error;
3592 }
3593 if (args.fsbno == NULLFSBLOCK && nullfb) {
3594 args.fsbno = 0;
3595 args.type = XFS_ALLOCTYPE_FIRST_AG;
3596 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003597 if ((error = xfs_alloc_vextent(&args)))
3598 return error;
Brian Foster3e3673e2018-07-11 22:26:14 -07003599 ap->tp->t_dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003600 }
3601 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003602 /*
3603 * check the allocation happened at the same or higher AG than
3604 * the first block that was allocated.
3605 */
3606 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003607 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3608 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003609
Dave Chinner3a756672011-09-18 20:40:58 +00003610 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003611 if (*ap->firstblock == NULLFSBLOCK)
3612 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003613 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003614 ap->length = args.len;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003615 /*
3616 * If the extent size hint is active, we tried to round the
3617 * caller's allocation request offset down to extsz and the
3618 * length up to another extsz boundary. If we found a free
3619 * extent we mapped it in starting at this new offset. If the
3620 * newly mapped space isn't long enough to cover any of the
3621 * range of offsets that was originally requested, move the
3622 * mapping up so that we can fill as much of the caller's
3623 * original request as possible. Free space is apparently
3624 * very fragmented so we're unlikely to be able to satisfy the
3625 * hints anyway.
3626 */
3627 if (ap->length <= orig_length)
3628 ap->offset = orig_offset;
3629 else if (ap->offset + ap->length < orig_offset + orig_length)
3630 ap->offset = orig_offset + orig_length - ap->length;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003631 xfs_bmap_btalloc_accounting(ap, &args);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003632 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003633 ap->blkno = NULLFSBLOCK;
3634 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 }
3636 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003637}
3638
3639/*
3640 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3641 * It figures out where to ask the underlying allocator to put the new extent.
3642 */
3643STATIC int
3644xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003645 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003646{
Dave Chinner292378e2016-09-26 08:21:28 +10003647 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3648 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003649 return xfs_bmap_rtalloc(ap);
3650 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651}
3652
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003653/* Trim extent to fit a logical block range. */
3654void
3655xfs_trim_extent(
3656 struct xfs_bmbt_irec *irec,
3657 xfs_fileoff_t bno,
3658 xfs_filblks_t len)
3659{
3660 xfs_fileoff_t distance;
3661 xfs_fileoff_t end = bno + len;
3662
3663 if (irec->br_startoff + irec->br_blockcount <= bno ||
3664 irec->br_startoff >= end) {
3665 irec->br_blockcount = 0;
3666 return;
3667 }
3668
3669 if (irec->br_startoff < bno) {
3670 distance = bno - irec->br_startoff;
3671 if (isnullstartblock(irec->br_startblock))
3672 irec->br_startblock = DELAYSTARTBLOCK;
3673 if (irec->br_startblock != DELAYSTARTBLOCK &&
3674 irec->br_startblock != HOLESTARTBLOCK)
3675 irec->br_startblock += distance;
3676 irec->br_startoff += distance;
3677 irec->br_blockcount -= distance;
3678 }
3679
3680 if (end < irec->br_startoff + irec->br_blockcount) {
3681 distance = irec->br_startoff + irec->br_blockcount - end;
3682 irec->br_blockcount -= distance;
3683 }
3684}
3685
Brian Foster40214d12017-10-13 09:47:46 -07003686/* trim extent to within eof */
3687void
3688xfs_trim_extent_eof(
3689 struct xfs_bmbt_irec *irec,
3690 struct xfs_inode *ip)
3691
3692{
3693 xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
3694 i_size_read(VFS_I(ip))));
3695}
3696
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003698 * Trim the returned map to the required bounds
3699 */
3700STATIC void
3701xfs_bmapi_trim_map(
3702 struct xfs_bmbt_irec *mval,
3703 struct xfs_bmbt_irec *got,
3704 xfs_fileoff_t *bno,
3705 xfs_filblks_t len,
3706 xfs_fileoff_t obno,
3707 xfs_fileoff_t end,
3708 int n,
3709 int flags)
3710{
3711 if ((flags & XFS_BMAPI_ENTIRE) ||
3712 got->br_startoff + got->br_blockcount <= obno) {
3713 *mval = *got;
3714 if (isnullstartblock(got->br_startblock))
3715 mval->br_startblock = DELAYSTARTBLOCK;
3716 return;
3717 }
3718
3719 if (obno > *bno)
3720 *bno = obno;
3721 ASSERT((*bno >= obno) || (n == 0));
3722 ASSERT(*bno < end);
3723 mval->br_startoff = *bno;
3724 if (isnullstartblock(got->br_startblock))
3725 mval->br_startblock = DELAYSTARTBLOCK;
3726 else
3727 mval->br_startblock = got->br_startblock +
3728 (*bno - got->br_startoff);
3729 /*
3730 * Return the minimum of what we got and what we asked for for
3731 * the length. We can use the len variable here because it is
3732 * modified below and we could have been there before coming
3733 * here if the first part of the allocation didn't overlap what
3734 * was asked for.
3735 */
3736 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3737 got->br_blockcount - (*bno - got->br_startoff));
3738 mval->br_state = got->br_state;
3739 ASSERT(mval->br_blockcount <= len);
3740 return;
3741}
3742
3743/*
3744 * Update and validate the extent map to return
3745 */
3746STATIC void
3747xfs_bmapi_update_map(
3748 struct xfs_bmbt_irec **map,
3749 xfs_fileoff_t *bno,
3750 xfs_filblks_t *len,
3751 xfs_fileoff_t obno,
3752 xfs_fileoff_t end,
3753 int *n,
3754 int flags)
3755{
3756 xfs_bmbt_irec_t *mval = *map;
3757
3758 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3759 ((mval->br_startoff + mval->br_blockcount) <= end));
3760 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3761 (mval->br_startoff < obno));
3762
3763 *bno = mval->br_startoff + mval->br_blockcount;
3764 *len = end - *bno;
3765 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3766 /* update previous map with new information */
3767 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3768 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3769 ASSERT(mval->br_state == mval[-1].br_state);
3770 mval[-1].br_blockcount = mval->br_blockcount;
3771 mval[-1].br_state = mval->br_state;
3772 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3773 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3774 mval[-1].br_startblock != HOLESTARTBLOCK &&
3775 mval->br_startblock == mval[-1].br_startblock +
3776 mval[-1].br_blockcount &&
Christoph Hellwigc3a2f9f2018-07-11 22:26:01 -07003777 mval[-1].br_state == mval->br_state) {
Dave Chinneraef9a892011-09-18 20:40:44 +00003778 ASSERT(mval->br_startoff ==
3779 mval[-1].br_startoff + mval[-1].br_blockcount);
3780 mval[-1].br_blockcount += mval->br_blockcount;
3781 } else if (*n > 0 &&
3782 mval->br_startblock == DELAYSTARTBLOCK &&
3783 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3784 mval->br_startoff ==
3785 mval[-1].br_startoff + mval[-1].br_blockcount) {
3786 mval[-1].br_blockcount += mval->br_blockcount;
3787 mval[-1].br_state = mval->br_state;
3788 } else if (!((*n == 0) &&
3789 ((mval->br_startoff + mval->br_blockcount) <=
3790 obno))) {
3791 mval++;
3792 (*n)++;
3793 }
3794 *map = mval;
3795}
3796
3797/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003798 * Map file blocks to filesystem blocks without allocation.
3799 */
3800int
3801xfs_bmapi_read(
3802 struct xfs_inode *ip,
3803 xfs_fileoff_t bno,
3804 xfs_filblks_t len,
3805 struct xfs_bmbt_irec *mval,
3806 int *nmap,
3807 int flags)
3808{
3809 struct xfs_mount *mp = ip->i_mount;
3810 struct xfs_ifork *ifp;
3811 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003812 xfs_fileoff_t obno;
3813 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003814 struct xfs_iext_cursor icur;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003815 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003816 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003817 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003818 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003819
3820 ASSERT(*nmap >= 1);
3821 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Christoph Hellwigc3a2f9f2018-07-11 22:26:01 -07003822 XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003823 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003824
3825 if (unlikely(XFS_TEST_ERROR(
3826 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3827 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003828 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003829 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003830 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003831 }
3832
3833 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003834 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003835
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003836 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003837
3838 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003839
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003840 /* No CoW fork? Return a hole. */
3841 if (whichfork == XFS_COW_FORK && !ifp) {
3842 mval->br_startoff = bno;
3843 mval->br_startblock = HOLESTARTBLOCK;
3844 mval->br_blockcount = len;
3845 mval->br_state = XFS_EXT_NORM;
3846 *nmap = 1;
3847 return 0;
3848 }
3849
Dave Chinner5c8ed202011-09-18 20:40:45 +00003850 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3851 error = xfs_iread_extents(NULL, ip, whichfork);
3852 if (error)
3853 return error;
3854 }
3855
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003856 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003857 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003858 end = bno + len;
3859 obno = bno;
3860
3861 while (bno < end && n < *nmap) {
3862 /* Reading past eof, act as though there's a hole up to end. */
3863 if (eof)
3864 got.br_startoff = end;
3865 if (got.br_startoff > bno) {
3866 /* Reading in a hole. */
3867 mval->br_startoff = bno;
3868 mval->br_startblock = HOLESTARTBLOCK;
3869 mval->br_blockcount =
3870 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3871 mval->br_state = XFS_EXT_NORM;
3872 bno += mval->br_blockcount;
3873 len -= mval->br_blockcount;
3874 mval++;
3875 n++;
3876 continue;
3877 }
3878
3879 /* set up the extent map to return. */
3880 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3881 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3882
3883 /* If we're done, stop now. */
3884 if (bno >= end || n >= *nmap)
3885 break;
3886
3887 /* Else go on to the next record. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003888 if (!xfs_iext_next_extent(ifp, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003889 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003890 }
3891 *nmap = n;
3892 return 0;
3893}
3894
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003895/*
3896 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3897 * global pool and the extent inserted into the inode in-core extent tree.
3898 *
3899 * On entry, got refers to the first extent beyond the offset of the extent to
3900 * allocate or eof is specified if no such extent exists. On return, got refers
3901 * to the extent record that was inserted to the inode fork.
3902 *
3903 * Note that the allocated extent may have been merged with contiguous extents
3904 * during insertion into the inode fork. Thus, got does not reflect the current
3905 * state of the inode fork on return. If necessary, the caller can use lastx to
3906 * look up the updated record in the inode fork.
3907 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10003908int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003909xfs_bmapi_reserve_delalloc(
3910 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003911 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11003912 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003913 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11003914 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003915 struct xfs_bmbt_irec *got,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003916 struct xfs_iext_cursor *icur,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003917 int eof)
3918{
3919 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003920 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003921 xfs_extlen_t alen;
3922 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003923 int error;
Brian Foster974ae922016-11-28 14:57:42 +11003924 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003925
Brian Foster974ae922016-11-28 14:57:42 +11003926 /*
3927 * Cap the alloc length. Keep track of prealloc so we know whether to
3928 * tag the inode before we return.
3929 */
3930 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003931 if (!eof)
3932 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11003933 if (prealloc && alen >= len)
3934 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003935
3936 /* Figure out the extent size, adjust alen */
Shan Hai6ca30722018-01-23 13:56:11 -08003937 if (whichfork == XFS_COW_FORK) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003938 struct xfs_bmbt_irec prev;
Shan Hai6ca30722018-01-23 13:56:11 -08003939 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003940
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003941 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003942 prev.br_startoff = NULLFILEOFF;
3943
Shan Hai6ca30722018-01-23 13:56:11 -08003944 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003945 1, 0, &aoff, &alen);
3946 ASSERT(!error);
3947 }
3948
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003949 /*
3950 * Make a transaction-less quota reservation for delayed allocation
3951 * blocks. This number gets adjusted later. We return if we haven't
3952 * allocated blocks already inside this loop.
3953 */
3954 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
Shan Hai6ca30722018-01-23 13:56:11 -08003955 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003956 if (error)
3957 return error;
3958
3959 /*
3960 * Split changing sb for alen and indlen since they could be coming
3961 * from different places.
3962 */
3963 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
3964 ASSERT(indlen > 0);
3965
Shan Hai6ca30722018-01-23 13:56:11 -08003966 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003967 if (error)
3968 goto out_unreserve_quota;
3969
Dave Chinner0d485ad2015-02-23 21:22:03 +11003970 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003971 if (error)
3972 goto out_unreserve_blocks;
3973
3974
3975 ip->i_delayed_blks += alen;
3976
3977 got->br_startoff = aoff;
3978 got->br_startblock = nullstartblock(indlen);
3979 got->br_blockcount = alen;
3980 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003981
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003982 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003983
Brian Foster974ae922016-11-28 14:57:42 +11003984 /*
3985 * Tag the inode if blocks were preallocated. Note that COW fork
3986 * preallocation can occur at the start or end of the extent, even when
3987 * prealloc == 0, so we must also check the aligned offset and length.
3988 */
3989 if (whichfork == XFS_DATA_FORK && prealloc)
3990 xfs_inode_set_eofblocks_tag(ip);
3991 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
3992 xfs_inode_set_cowblocks_tag(ip);
3993
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003994 return 0;
3995
3996out_unreserve_blocks:
Shan Hai6ca30722018-01-23 13:56:11 -08003997 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003998out_unreserve_quota:
3999 if (XFS_IS_QUOTA_ON(mp))
Shan Hai6ca30722018-01-23 13:56:11 -08004000 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0,
4001 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004002 return error;
4003}
4004
Dave Chinnercf11da92014-07-15 07:08:24 +10004005static int
4006xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004007 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004008{
4009 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004010 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004011 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004012 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004013 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004014
Dave Chinnera99ebf42011-12-01 11:24:20 +00004015 ASSERT(bma->length > 0);
4016
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004017 /*
4018 * For the wasdelay case, we could also just allocate the stuff asked
4019 * for in this bmap call but that wouldn't be as good.
4020 */
4021 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004022 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4023 bma->offset = bma->got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004024 xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004025 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004026 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004027 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004028 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004029 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004030 }
4031
4032 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004033 * Set the data type being allocated. For the data fork, the first data
4034 * in the file is treated differently to all other allocations. For the
4035 * attribute fork, we only need to ensure the allocated range is not on
4036 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004037 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004038 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004039 bma->datatype = XFS_ALLOC_NOBUSY;
4040 if (whichfork == XFS_DATA_FORK) {
4041 if (bma->offset == 0)
4042 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4043 else
4044 bma->datatype |= XFS_ALLOC_USERDATA;
4045 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004046 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004047 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004048 }
4049
Dave Chinnere04426b2012-10-05 11:06:59 +10004050 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004051
4052 /*
4053 * Only want to do the alignment at the eof if it is userdata and
4054 * allocation length is larger than a stripe unit.
4055 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004056 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004057 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004058 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004059 if (error)
4060 return error;
4061 }
4062
4063 error = xfs_bmap_alloc(bma);
4064 if (error)
4065 return error;
4066
Dave Chinner29c8d172011-09-18 20:41:00 +00004067 if (bma->cur)
4068 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004069 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004070 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004071 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4072 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4073 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004074 }
4075 /*
4076 * Bump the number of extents we've allocated
4077 * in this call.
4078 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004079 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004080
Dave Chinner29c8d172011-09-18 20:41:00 +00004081 if (bma->cur)
4082 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004083 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4084
Dave Chinner963c30c2011-09-18 20:40:59 +00004085 bma->got.br_startoff = bma->offset;
4086 bma->got.br_startblock = bma->blkno;
4087 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004088 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004089
4090 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004091 * In the data fork, a wasdelay extent has been initialized, so
4092 * shouldn't be flagged as unwritten.
4093 *
4094 * For the cow fork, however, we convert delalloc reservations
4095 * (extents allocated for speculative preallocation) to
4096 * allocated unwritten extents, and only convert the unwritten
4097 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004098 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004099 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4100 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004101 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004102 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004103
Christoph Hellwigc6534242011-09-18 20:41:05 +00004104 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004105 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004106 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004107 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004108 whichfork, &bma->icur, &bma->cur, &bma->got,
Brian Foster81ba8f32018-07-11 22:26:16 -07004109 bma->firstblock, &bma->logflags, bma->flags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004110
Christoph Hellwigc315c902011-09-18 20:41:02 +00004111 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004112 if (error)
4113 return error;
4114
4115 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004116 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4117 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4118 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004119 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004120 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004121
Dave Chinner963c30c2011-09-18 20:40:59 +00004122 ASSERT(bma->got.br_startoff <= bma->offset);
4123 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4124 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004125 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4126 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004127 return 0;
4128}
4129
Dave Chinnerb447fe52011-09-18 20:40:51 +00004130STATIC int
4131xfs_bmapi_convert_unwritten(
4132 struct xfs_bmalloca *bma,
4133 struct xfs_bmbt_irec *mval,
4134 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004135 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004136{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004137 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004138 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004139 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004140 int error;
4141
Dave Chinnerb447fe52011-09-18 20:40:51 +00004142 /* check if we need to do unwritten->real conversion */
4143 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4144 (flags & XFS_BMAPI_PREALLOC))
4145 return 0;
4146
4147 /* check if we need to do real->unwritten conversion */
4148 if (mval->br_state == XFS_EXT_NORM &&
4149 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4150 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4151 return 0;
4152
4153 /*
4154 * Modify (by adding) the state flag, if writing.
4155 */
4156 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004157 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4158 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004159 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004160 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004161 }
4162 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4163 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4164
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004165 /*
4166 * Before insertion into the bmbt, zero the range being converted
4167 * if required.
4168 */
4169 if (flags & XFS_BMAPI_ZERO) {
4170 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4171 mval->br_blockcount);
4172 if (error)
4173 return error;
4174 }
4175
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004176 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004177 &bma->icur, &bma->cur, mval, bma->firstblock,
Brian Foster81ba8f32018-07-11 22:26:16 -07004178 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004179 /*
4180 * Log the inode core unconditionally in the unwritten extent conversion
4181 * path because the conversion might not have done so (e.g., if the
4182 * extent count hasn't changed). We need to make sure the inode is dirty
4183 * in the transaction for the sake of fsync(), even if nothing has
4184 * changed, because fsync() will not force the log for this transaction
4185 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004186 *
4187 * Note: If we're only converting cow fork extents, there aren't
4188 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004189 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004190 if (whichfork != XFS_COW_FORK)
4191 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004192 if (error)
4193 return error;
4194
4195 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004196 * Update our extent pointer, given that
4197 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4198 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004199 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004200 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004201
4202 /*
4203 * We may have combined previously unwritten space with written space,
4204 * so generate another request.
4205 */
4206 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004207 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004208 return 0;
4209}
4210
Christoph Hellwig44032802011-09-18 20:40:48 +00004211/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004212 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4213 * extent state if necessary. Details behaviour is controlled by the flags
4214 * parameter. Only allocates blocks from a single allocation group, to avoid
4215 * locking problems.
4216 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 * The returned value in "firstblock" from the first call in a transaction
4218 * must be remembered and presented to subsequent calls in "firstblock".
4219 * An upper bound for the number of blocks to be allocated is supplied to
4220 * the first call in "total"; if no allocation group has that many free
4221 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4222 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004223int
4224xfs_bmapi_write(
4225 struct xfs_trans *tp, /* transaction pointer */
4226 struct xfs_inode *ip, /* incore inode */
4227 xfs_fileoff_t bno, /* starting file offs. mapped */
4228 xfs_filblks_t len, /* length to map in file */
4229 int flags, /* XFS_BMAPI_... */
4230 xfs_fsblock_t *firstblock, /* first allocated block
4231 controls a.g. for allocs */
4232 xfs_extlen_t total, /* total blocks needed */
4233 struct xfs_bmbt_irec *mval, /* output: map values */
Brian Foster6e702a52018-07-11 22:26:12 -07004234 int *nmap) /* i/o: mval size/count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004236 struct xfs_mount *mp = ip->i_mount;
4237 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004238 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004239 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004240 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004241 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004242 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004243 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004244 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004245
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004247 xfs_fileoff_t orig_bno; /* original block number value */
4248 int orig_flags; /* original flags arg value */
4249 xfs_filblks_t orig_len; /* original value of len arg */
4250 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4251 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
4253 orig_bno = bno;
4254 orig_len = len;
4255 orig_flags = flags;
4256 orig_mval = mval;
4257 orig_nmap = *nmap;
4258#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004259 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004260
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004262 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004263 ASSERT(tp != NULL ||
4264 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4265 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004266 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004267 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004268 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004269 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004270
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004271 /* zeroing is for currently only for data extents, not metadata */
4272 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4273 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4274 /*
4275 * we can allocate unwritten extents or pre-zero allocated blocks,
4276 * but it makes no sense to do both at once. This would result in
4277 * zeroing the unwritten extent twice, but it still being an
4278 * unwritten extent....
4279 */
4280 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4281 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4282
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 if (unlikely(XFS_TEST_ERROR(
4284 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004285 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004286 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004287 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004288 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004290
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004292 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004293
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004295
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004296 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004297
Brian Foster3ae2d892018-07-11 22:26:19 -07004298 if (!tp || *firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004300 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004302 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004303 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004304 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004305 }
4306
4307 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4308 error = xfs_iread_extents(tp, ip, whichfork);
4309 if (error)
4310 goto error0;
4311 }
4312
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 n = 0;
4314 end = bno + len;
4315 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004316
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004317 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004318 eof = true;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004319 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004320 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004321 bma.tp = tp;
4322 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004323 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004324 bma.datatype = 0;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004325 bma.firstblock = firstblock;
Brian Foster3e3673e2018-07-11 22:26:14 -07004326 ASSERT(!tp || tp->t_dfops);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004327
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004329 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004330
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004331 /* in hole or beyond EOF? */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004332 if (eof || bma.got.br_startoff > bno) {
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004333 /*
4334 * CoW fork conversions should /never/ hit EOF or
4335 * holes. There should always be something for us
4336 * to work on.
4337 */
4338 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4339 (flags & XFS_BMAPI_COWFORK)));
4340
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004341 if (flags & XFS_BMAPI_DELALLOC) {
4342 /*
4343 * For the COW fork we can reasonably get a
4344 * request for converting an extent that races
4345 * with other threads already having converted
4346 * part of it, as there converting COW to
4347 * regular blocks is not protected using the
4348 * IOLOCK.
4349 */
4350 ASSERT(flags & XFS_BMAPI_COWFORK);
4351 if (!(flags & XFS_BMAPI_COWFORK)) {
4352 error = -EIO;
4353 goto error0;
4354 }
4355
4356 if (eof || bno >= end)
4357 break;
4358 } else {
4359 need_alloc = true;
4360 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004361 } else if (isnullstartblock(bma.got.br_startblock)) {
4362 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004363 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004364
4365 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 * First, deal with the hole before the allocated space
4367 * that we found, if any.
4368 */
Christoph Hellwigb1214592017-11-03 10:34:44 -07004369 if ((need_alloc || wasdelay) &&
4370 !(flags & XFS_BMAPI_CONVERT_ONLY)) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004371 bma.eof = eof;
4372 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4373 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004374 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004375 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004376
Dave Chinnera99ebf42011-12-01 11:24:20 +00004377 /*
4378 * There's a 32/64 bit type mismatch between the
4379 * allocation length request (which can be 64 bits in
4380 * length) and the bma length request, which is
4381 * xfs_extlen_t and therefore 32 bits. Hence we have to
4382 * check for 32-bit overflows and handle them here.
4383 */
4384 if (len > (xfs_filblks_t)MAXEXTLEN)
4385 bma.length = MAXEXTLEN;
4386 else
4387 bma.length = len;
4388
4389 ASSERT(len > 0);
4390 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004391 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 if (error)
4393 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004394 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004395 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004396
4397 /*
4398 * If this is a CoW allocation, record the data in
4399 * the refcount btree for orphan recovery.
4400 */
4401 if (whichfork == XFS_COW_FORK) {
Brian Foster6e702a52018-07-11 22:26:12 -07004402 error = xfs_refcount_alloc_cow_extent(mp,
4403 tp->t_dfops, bma.blkno,
4404 bma.length);
Darrick J. Wong174edb02016-10-03 09:11:39 -07004405 if (error)
4406 goto error0;
4407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004409
Dave Chinneraef9a892011-09-18 20:40:44 +00004410 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004411 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4412 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
Dave Chinnerb447fe52011-09-18 20:40:51 +00004414 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004415 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004416 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004417 continue;
4418 if (error)
4419 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
Dave Chinneraef9a892011-09-18 20:40:44 +00004421 /* update the extent map to return */
4422 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4423
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 /*
4425 * If we're done, stop now. Stop when we've allocated
4426 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4427 * the transaction may get too big.
4428 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004429 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004431
4432 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004433 bma.prev = bma.got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004434 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004435 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004438
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 /*
4440 * Transform from btree to extents, give it cur.
4441 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004442 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004443 int tmp_logflags = 0;
4444
Dave Chinner29c8d172011-09-18 20:41:00 +00004445 ASSERT(bma.cur);
4446 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004448 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 if (error)
4450 goto error0;
4451 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004452
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004454 XFS_IFORK_NEXTENTS(ip, whichfork) >
4455 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457error0:
4458 /*
4459 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004460 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004462 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004464 bma.logflags &= ~xfs_ilog_fext(whichfork);
4465 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004467 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 /*
4469 * Log whatever the flags say, even if error. Otherwise we might miss
4470 * detecting a case where the data is changed, there's an error,
4471 * and it's not logged so we don't shutdown when we should.
4472 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004473 if (bma.logflags)
4474 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004475
Dave Chinner29c8d172011-09-18 20:41:00 +00004476 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 if (!error) {
4478 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004479 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004481 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004482 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004484 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4486 }
4487 if (!error)
4488 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4489 orig_nmap, *nmap);
4490 return error;
4491}
4492
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004493int
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004494xfs_bmapi_remap(
4495 struct xfs_trans *tp,
4496 struct xfs_inode *ip,
4497 xfs_fileoff_t bno,
4498 xfs_filblks_t len,
4499 xfs_fsblock_t startblock,
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004500 int flags)
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004501{
4502 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004503 struct xfs_ifork *ifp;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004504 struct xfs_btree_cur *cur = NULL;
4505 xfs_fsblock_t firstblock = NULLFSBLOCK;
4506 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004507 struct xfs_iext_cursor icur;
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004508 int whichfork = xfs_bmapi_whichfork(flags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004509 int logflags = 0, error;
4510
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004511 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004512 ASSERT(len > 0);
4513 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4514 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Darrick J. Wong7644bd92018-05-14 06:34:35 -07004515 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4516 XFS_BMAPI_NORMAP)));
4517 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4518 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004519
4520 if (unlikely(XFS_TEST_ERROR(
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004521 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4522 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004523 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004524 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4525 return -EFSCORRUPTED;
4526 }
4527
4528 if (XFS_FORCED_SHUTDOWN(mp))
4529 return -EIO;
4530
4531 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004532 error = xfs_iread_extents(tp, ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004533 if (error)
4534 return error;
4535 }
4536
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004537 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004538 /* make sure we only reflink into a hole. */
4539 ASSERT(got.br_startoff > bno);
4540 ASSERT(got.br_startoff - bno >= len);
4541 }
4542
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004543 ip->i_d.di_nblocks += len;
4544 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004545
4546 if (ifp->if_flags & XFS_IFBROOT) {
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004547 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004548 cur->bc_private.b.firstblock = firstblock;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004549 cur->bc_private.b.flags = 0;
4550 }
4551
4552 got.br_startoff = bno;
4553 got.br_startblock = startblock;
4554 got.br_blockcount = len;
Darrick J. Wong7644bd92018-05-14 06:34:35 -07004555 if (flags & XFS_BMAPI_PREALLOC)
4556 got.br_state = XFS_EXT_UNWRITTEN;
4557 else
4558 got.br_state = XFS_EXT_NORM;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004559
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004560 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
Brian Foster81ba8f32018-07-11 22:26:16 -07004561 &cur, &got, &firstblock, &logflags, flags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004562 if (error)
4563 goto error0;
4564
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004565 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004566 int tmp_logflags = 0;
4567
4568 error = xfs_bmap_btree_to_extents(tp, ip, cur,
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004569 &tmp_logflags, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004570 logflags |= tmp_logflags;
4571 }
4572
4573error0:
4574 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4575 logflags &= ~XFS_ILOG_DEXT;
4576 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4577 logflags &= ~XFS_ILOG_DBROOT;
4578
4579 if (logflags)
4580 xfs_trans_log_inode(tp, ip, logflags);
4581 if (cur) {
4582 xfs_btree_del_cursor(cur,
4583 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4584 }
4585 return error;
4586}
4587
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004589 * When a delalloc extent is split (e.g., due to a hole punch), the original
4590 * indlen reservation must be shared across the two new extents that are left
4591 * behind.
4592 *
4593 * Given the original reservation and the worst case indlen for the two new
4594 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004595 * reservation fairly across the two new extents. If necessary, steal available
4596 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4597 * ores == 1). The number of stolen blocks is returned. The availability and
4598 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004599 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004600static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004601xfs_bmap_split_indlen(
4602 xfs_filblks_t ores, /* original res. */
4603 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004604 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4605 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004606{
4607 xfs_filblks_t len1 = *indlen1;
4608 xfs_filblks_t len2 = *indlen2;
4609 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004610 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004611 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004612
4613 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004614 * Steal as many blocks as we can to try and satisfy the worst case
4615 * indlen for both new extents.
4616 */
Brian Foster75d65362017-02-13 22:48:30 -08004617 if (ores < nres && avail)
4618 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4619 ores += stolen;
4620
4621 /* nothing else to do if we've satisfied the new reservation */
4622 if (ores >= nres)
4623 return stolen;
Brian Fosterd34999c2016-03-15 11:42:47 +11004624
4625 /*
Brian Foster75d65362017-02-13 22:48:30 -08004626 * We can't meet the total required reservation for the two extents.
4627 * Calculate the percent of the overall shortage between both extents
4628 * and apply this percentage to each of the requested indlen values.
4629 * This distributes the shortage fairly and reduces the chances that one
4630 * of the two extents is left with nothing when extents are repeatedly
4631 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004632 */
Brian Foster75d65362017-02-13 22:48:30 -08004633 resfactor = (ores * 100);
4634 do_div(resfactor, nres);
4635 len1 *= resfactor;
4636 do_div(len1, 100);
4637 len2 *= resfactor;
4638 do_div(len2, 100);
4639 ASSERT(len1 + len2 <= ores);
4640 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4641
4642 /*
4643 * Hand out the remainder to each extent. If one of the two reservations
4644 * is zero, we want to make sure that one gets a block first. The loop
4645 * below starts with len1, so hand len2 a block right off the bat if it
4646 * is zero.
4647 */
4648 ores -= (len1 + len2);
4649 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4650 if (ores && !len2 && *indlen2) {
4651 len2++;
4652 ores--;
4653 }
4654 while (ores) {
4655 if (len1 < *indlen1) {
4656 len1++;
4657 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004658 }
Brian Foster75d65362017-02-13 22:48:30 -08004659 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004660 break;
Brian Foster75d65362017-02-13 22:48:30 -08004661 if (len2 < *indlen2) {
4662 len2++;
4663 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004664 }
4665 }
4666
4667 *indlen1 = len1;
4668 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004669
4670 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004671}
4672
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004673int
4674xfs_bmap_del_extent_delay(
4675 struct xfs_inode *ip,
4676 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004677 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004678 struct xfs_bmbt_irec *got,
4679 struct xfs_bmbt_irec *del)
4680{
4681 struct xfs_mount *mp = ip->i_mount;
4682 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4683 struct xfs_bmbt_irec new;
4684 int64_t da_old, da_new, da_diff = 0;
4685 xfs_fileoff_t del_endoff, got_endoff;
4686 xfs_filblks_t got_indlen, new_indlen, stolen;
Christoph Hellwig060ea652017-10-19 11:02:29 -07004687 int state = xfs_bmap_fork_to_state(whichfork);
4688 int error = 0;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004689 bool isrt;
4690
4691 XFS_STATS_INC(mp, xs_del_exlist);
4692
4693 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4694 del_endoff = del->br_startoff + del->br_blockcount;
4695 got_endoff = got->br_startoff + got->br_blockcount;
4696 da_old = startblockval(got->br_startblock);
4697 da_new = 0;
4698
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004699 ASSERT(del->br_blockcount > 0);
4700 ASSERT(got->br_startoff <= del->br_startoff);
4701 ASSERT(got_endoff >= del_endoff);
4702
4703 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004704 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004705
4706 do_div(rtexts, mp->m_sb.sb_rextsize);
4707 xfs_mod_frextents(mp, rtexts);
4708 }
4709
4710 /*
4711 * Update the inode delalloc counter now and wait to update the
4712 * sb counters as we might have to borrow some blocks for the
4713 * indirect block accounting.
4714 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004715 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4716 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004717 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004718 if (error)
4719 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004720 ip->i_delayed_blks -= del->br_blockcount;
4721
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004722 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004723 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004724 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004725 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004726
Christoph Hellwig0173c682017-10-17 14:16:22 -07004727 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4728 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004729 /*
4730 * Matches the whole extent. Delete the entry.
4731 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004732 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004733 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004734 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004735 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004736 /*
4737 * Deleting the first part of the extent.
4738 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004739 got->br_startoff = del_endoff;
4740 got->br_blockcount -= del->br_blockcount;
4741 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4742 got->br_blockcount), da_old);
4743 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004744 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004745 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004746 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004747 /*
4748 * Deleting the last part of the extent.
4749 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004750 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4751 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4752 got->br_blockcount), da_old);
4753 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004754 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004755 break;
4756 case 0:
4757 /*
4758 * Deleting the middle of the extent.
4759 *
4760 * Distribute the original indlen reservation across the two new
4761 * extents. Steal blocks from the deleted extent if necessary.
4762 * Stealing blocks simply fudges the fdblocks accounting below.
4763 * Warn if either of the new indlen reservations is zero as this
4764 * can lead to delalloc problems.
4765 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004766 got->br_blockcount = del->br_startoff - got->br_startoff;
4767 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4768
4769 new.br_blockcount = got_endoff - del_endoff;
4770 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4771
4772 WARN_ON_ONCE(!got_indlen || !new_indlen);
4773 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4774 del->br_blockcount);
4775
4776 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004777
4778 new.br_startoff = del_endoff;
4779 new.br_state = got->br_state;
4780 new.br_startblock = nullstartblock((int)new_indlen);
4781
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004782 xfs_iext_update_extent(ip, state, icur, got);
4783 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004784 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004785
4786 da_new = got_indlen + new_indlen - stolen;
4787 del->br_blockcount -= stolen;
4788 break;
4789 }
4790
4791 ASSERT(da_old >= da_new);
4792 da_diff = da_old - da_new;
4793 if (!isrt)
4794 da_diff += del->br_blockcount;
4795 if (da_diff)
4796 xfs_mod_fdblocks(mp, da_diff, false);
4797 return error;
4798}
4799
4800void
4801xfs_bmap_del_extent_cow(
4802 struct xfs_inode *ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004803 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004804 struct xfs_bmbt_irec *got,
4805 struct xfs_bmbt_irec *del)
4806{
4807 struct xfs_mount *mp = ip->i_mount;
4808 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4809 struct xfs_bmbt_irec new;
4810 xfs_fileoff_t del_endoff, got_endoff;
4811 int state = BMAP_COWFORK;
4812
4813 XFS_STATS_INC(mp, xs_del_exlist);
4814
4815 del_endoff = del->br_startoff + del->br_blockcount;
4816 got_endoff = got->br_startoff + got->br_blockcount;
4817
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004818 ASSERT(del->br_blockcount > 0);
4819 ASSERT(got->br_startoff <= del->br_startoff);
4820 ASSERT(got_endoff >= del_endoff);
4821 ASSERT(!isnullstartblock(got->br_startblock));
4822
4823 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004824 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004825 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004826 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004827
Christoph Hellwig0173c682017-10-17 14:16:22 -07004828 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4829 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004830 /*
4831 * Matches the whole extent. Delete the entry.
4832 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004833 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004834 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004835 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004836 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004837 /*
4838 * Deleting the first part of the extent.
4839 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004840 got->br_startoff = del_endoff;
4841 got->br_blockcount -= del->br_blockcount;
4842 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004843 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004844 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004845 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004846 /*
4847 * Deleting the last part of the extent.
4848 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004849 got->br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004850 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004851 break;
4852 case 0:
4853 /*
4854 * Deleting the middle of the extent.
4855 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004856 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004857
4858 new.br_startoff = del_endoff;
4859 new.br_blockcount = got_endoff - del_endoff;
4860 new.br_state = got->br_state;
4861 new.br_startblock = del->br_startblock + del->br_blockcount;
4862
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004863 xfs_iext_update_extent(ip, state, icur, got);
4864 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004865 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004866 break;
4867 }
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08004868 ip->i_delayed_blks -= del->br_blockcount;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004869}
4870
Brian Fostera9bd24a2016-03-15 11:42:46 +11004871/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11004872 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07004873 * after removing space.
Dave Chinner9e5987a72013-02-25 12:31:26 +11004874 */
4875STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07004876xfs_bmap_del_extent_real(
Dave Chinner9e5987a72013-02-25 12:31:26 +11004877 xfs_inode_t *ip, /* incore inode pointer */
4878 xfs_trans_t *tp, /* current transaction pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004879 struct xfs_iext_cursor *icur,
Dave Chinner9e5987a72013-02-25 12:31:26 +11004880 xfs_btree_cur_t *cur, /* if null, not a btree */
4881 xfs_bmbt_irec_t *del, /* data to remove from extents */
4882 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07004883 int whichfork, /* data or attr fork */
4884 int bflags) /* bmapi flags */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004885{
Dave Chinner9e5987a72013-02-25 12:31:26 +11004886 xfs_fsblock_t del_endblock=0; /* first block past del */
4887 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004888 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004889 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004890 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004891 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004892 xfs_fileoff_t got_endoff; /* first offset past got */
4893 int i; /* temp state */
4894 xfs_ifork_t *ifp; /* inode fork pointer */
4895 xfs_mount_t *mp; /* mount structure */
4896 xfs_filblks_t nblks; /* quota/sb block count */
4897 xfs_bmbt_irec_t new; /* new record to be inserted */
4898 /* REFERENCED */
4899 uint qfield; /* quota field to update */
Christoph Hellwig060ea652017-10-19 11:02:29 -07004900 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004901 struct xfs_bmbt_irec old;
Brian Foster81ba8f32018-07-11 22:26:16 -07004902 struct xfs_defer_ops *dfops = tp ? tp->t_dfops : NULL;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004903
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004904 mp = ip->i_mount;
4905 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004906
Dave Chinner9e5987a72013-02-25 12:31:26 +11004907 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004908 ASSERT(del->br_blockcount > 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004909 xfs_iext_get_extent(ifp, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004910 ASSERT(got.br_startoff <= del->br_startoff);
4911 del_endoff = del->br_startoff + del->br_blockcount;
4912 got_endoff = got.br_startoff + got.br_blockcount;
4913 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004914 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a72013-02-25 12:31:26 +11004915 qfield = 0;
4916 error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004917
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004918 /*
4919 * If it's the case where the directory code is running with no block
4920 * reservation, and the deleted block is in the middle of its extent,
4921 * and the resulting insert of an extent would cause transformation to
4922 * btree format, then reject it. The calling code will then swap blocks
4923 * around instead. We have to do this now, rather than waiting for the
4924 * conversion to btree format, since the transaction will be dirty then.
4925 */
4926 if (tp->t_blk_res == 0 &&
4927 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4928 XFS_IFORK_NEXTENTS(ip, whichfork) >=
4929 XFS_IFORK_MAXEXT(ip, whichfork) &&
4930 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
4931 return -ENOSPC;
4932
4933 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004934 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4935 xfs_fsblock_t bno;
4936 xfs_filblks_t len;
Dave Chinner0703a8e2018-06-08 09:54:22 -07004937 xfs_extlen_t mod;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004938
Dave Chinner0703a8e2018-06-08 09:54:22 -07004939 bno = div_u64_rem(del->br_startblock, mp->m_sb.sb_rextsize,
4940 &mod);
4941 ASSERT(mod == 0);
4942 len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize,
4943 &mod);
4944 ASSERT(mod == 0);
4945
Christoph Hellwige1d75532017-10-17 14:16:21 -07004946 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4947 if (error)
4948 goto done;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004949 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004950 nblks = len * mp->m_sb.sb_rextsize;
4951 qfield = XFS_TRANS_DQ_RTBCOUNT;
4952 } else {
4953 do_fx = 1;
4954 nblks = del->br_blockcount;
4955 qfield = XFS_TRANS_DQ_BCOUNT;
4956 }
4957
4958 del_endblock = del->br_startblock + del->br_blockcount;
4959 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07004960 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004961 if (error)
4962 goto done;
4963 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004964 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10004965
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004966 if (got.br_startoff == del->br_startoff)
4967 state |= BMAP_LEFT_FILLING;
4968 if (got_endoff == del_endoff)
4969 state |= BMAP_RIGHT_FILLING;
4970
4971 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4972 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11004973 /*
4974 * Matches the whole extent. Delete the entry.
4975 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004976 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004977 xfs_iext_prev(ifp, icur);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004978 XFS_IFORK_NEXT_SET(ip, whichfork,
4979 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4980 flags |= XFS_ILOG_CORE;
4981 if (!cur) {
4982 flags |= xfs_ilog_fext(whichfork);
4983 break;
4984 }
4985 if ((error = xfs_btree_delete(cur, &i)))
4986 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004987 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004988 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004989 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11004990 /*
4991 * Deleting the first part of the extent.
4992 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004993 got.br_startoff = del_endoff;
4994 got.br_startblock = del_endblock;
4995 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004996 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004997 if (!cur) {
4998 flags |= xfs_ilog_fext(whichfork);
4999 break;
5000 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005001 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005002 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005003 goto done;
5004 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005005 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005006 /*
5007 * Deleting the last part of the extent.
5008 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005009 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005010 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005011 if (!cur) {
5012 flags |= xfs_ilog_fext(whichfork);
5013 break;
5014 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005015 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005016 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005017 goto done;
5018 break;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005019 case 0:
5020 /*
5021 * Deleting the middle of the extent.
5022 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005023 old = got;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07005024
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005025 got.br_blockcount = del->br_startoff - got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005026 xfs_iext_update_extent(ip, state, icur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005027
Dave Chinner9e5987a72013-02-25 12:31:26 +11005028 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005029 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005030 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005031 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005032
Christoph Hellwige1d75532017-10-17 14:16:21 -07005033 flags |= XFS_ILOG_CORE;
5034 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005035 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005036 if (error)
5037 goto done;
5038 error = xfs_btree_increment(cur, 0, &i);
5039 if (error)
5040 goto done;
5041 cur->bc_rec.b = new;
5042 error = xfs_btree_insert(cur, &i);
5043 if (error && error != -ENOSPC)
5044 goto done;
5045 /*
5046 * If get no-space back from btree insert, it tried a
5047 * split, and we have a zero block reservation. Fix up
5048 * our state and return the error.
5049 */
5050 if (error == -ENOSPC) {
5051 /*
5052 * Reset the cursor, don't trust it after any
5053 * insert operation.
5054 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005055 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005056 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005057 goto done;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005058 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5059 /*
5060 * Update the btree record back
5061 * to the original value.
5062 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005063 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005064 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005065 goto done;
5066 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005067 * Reset the extent record back
5068 * to the original value.
Dave Chinner9e5987a72013-02-25 12:31:26 +11005069 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005070 xfs_iext_update_extent(ip, state, icur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005071 flags = 0;
5072 error = -ENOSPC;
5073 goto done;
5074 }
5075 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5076 } else
5077 flags |= xfs_ilog_fext(whichfork);
5078 XFS_IFORK_NEXT_SET(ip, whichfork,
5079 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005080 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005081 xfs_iext_insert(ip, icur, &new, state);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005082 break;
5083 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005084
5085 /* remove reverse mapping */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005086 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5087 if (error)
5088 goto done;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005089
Dave Chinner9e5987a72013-02-25 12:31:26 +11005090 /*
5091 * If we need to, add to list of extents to delete.
5092 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005093 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005094 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5095 error = xfs_refcount_decrease_extent(mp, dfops, del);
5096 if (error)
5097 goto done;
Brian Fosterfcb762f2018-05-09 08:45:04 -07005098 } else {
Brian Foster4e529332018-05-10 09:35:42 -07005099 __xfs_bmap_add_free(mp, dfops, del->br_startblock,
5100 del->br_blockcount, NULL,
5101 (bflags & XFS_BMAPI_NODISCARD) ||
5102 del->br_state == XFS_EXT_UNWRITTEN);
Brian Fosterfcb762f2018-05-09 08:45:04 -07005103 }
Darrick J. Wong62aab202016-10-03 09:11:23 -07005104 }
5105
Dave Chinner9e5987a72013-02-25 12:31:26 +11005106 /*
5107 * Adjust inode # blocks in the file.
5108 */
5109 if (nblks)
5110 ip->i_d.di_nblocks -= nblks;
5111 /*
5112 * Adjust quota data.
5113 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005114 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a72013-02-25 12:31:26 +11005115 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5116
Dave Chinner9e5987a72013-02-25 12:31:26 +11005117done:
5118 *logflagsp = flags;
5119 return error;
5120}
5121
5122/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 * Unmap (remove) blocks from a file.
5124 * If nexts is nonzero then the number of extents to remove is limited to
5125 * that value. If not all extents in the block range can be removed then
5126 * *done is set.
5127 */
5128int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005129__xfs_bunmapi(
Brian Fosterccd9d912018-07-11 22:26:13 -07005130 struct xfs_trans *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005132 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005133 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 int flags, /* misc flags */
5135 xfs_extnum_t nexts, /* number of extents max */
Brian Fosterccd9d912018-07-11 22:26:13 -07005136 xfs_fsblock_t *firstblock) /* first allocated block
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 controls a.g. for allocs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138{
Brian Fosterccd9d912018-07-11 22:26:13 -07005139 struct xfs_btree_cur *cur; /* bmap btree cursor */
5140 struct xfs_bmbt_irec del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 int error; /* error return value */
5142 xfs_extnum_t extno; /* extent number in list */
Brian Fosterccd9d912018-07-11 22:26:13 -07005143 struct xfs_bmbt_irec got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 xfs_ifork_t *ifp; /* inode fork pointer */
5145 int isrt; /* freeing in rt area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 int logflags; /* transaction logging flags */
5147 xfs_extlen_t mod; /* rt extent offset */
Brian Fosterccd9d912018-07-11 22:26:13 -07005148 struct xfs_mount *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 int tmp_logflags; /* partial logging flags */
5150 int wasdel; /* was a delayed alloc extent */
5151 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005153 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005154 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005155 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005156 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005157 struct xfs_iext_cursor icur;
5158 bool done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005160 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005161
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005162 whichfork = xfs_bmapi_whichfork(flags);
5163 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 ifp = XFS_IFORK_PTR(ip, whichfork);
5165 if (unlikely(
5166 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5167 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5168 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5169 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005170 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 }
5172 mp = ip->i_mount;
5173 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005174 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005175
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005176 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 ASSERT(len > 0);
5178 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005179
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005180 /*
5181 * Guesstimate how many blocks we can unmap without running the risk of
5182 * blowing out the transaction with a mix of EFIs and reflink
5183 * adjustments.
5184 */
Darrick J. Wong8c57b882017-12-10 18:03:53 -08005185 if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005186 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5187 else
5188 max_len = len;
5189
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5191 (error = xfs_iread_extents(tp, ip, whichfork)))
5192 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005193 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005194 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 return 0;
5196 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005197 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005198 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwigdc560152017-10-23 16:32:39 -07005199 end = start + len;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005200
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005201 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
Christoph Hellwigdc560152017-10-23 16:32:39 -07005202 *rlen = 0;
5203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 }
Christoph Hellwigdc560152017-10-23 16:32:39 -07005205 end--;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005206
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 logflags = 0;
5208 if (ifp->if_flags & XFS_IFBROOT) {
5209 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005210 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 cur->bc_private.b.firstblock = *firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 cur->bc_private.b.flags = 0;
5213 } else
5214 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005215
5216 if (isrt) {
5217 /*
5218 * Synchronize by locking the bitmap inode.
5219 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005220 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005221 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005222 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5223 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005224 }
5225
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 extno = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005227 while (end != (xfs_fileoff_t)-1 && end >= start &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005228 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005230 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 * Just back up to the previous extent, if so.
5232 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005233 if (got.br_startoff > end &&
5234 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5235 done = true;
5236 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 }
5238 /*
5239 * Is the last block of this extent before the range
5240 * we're supposed to delete? If so, we're done.
5241 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005242 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005244 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 break;
5246 /*
5247 * Then deal with the (possibly delayed) allocated space
5248 * we found.
5249 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005251 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005252
5253 /*
5254 * Make sure we don't touch multiple AGF headers out of order
5255 * in a single transaction, as that could cause AB-BA deadlocks.
5256 */
5257 if (!wasdel) {
5258 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5259 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5260 break;
5261 prev_agno = agno;
5262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 if (got.br_startoff < start) {
5264 del.br_startoff = start;
5265 del.br_blockcount -= start - got.br_startoff;
5266 if (!wasdel)
5267 del.br_startblock += start - got.br_startoff;
5268 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005269 if (del.br_startoff + del.br_blockcount > end + 1)
5270 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005271
5272 /* How much can we safely unmap? */
5273 if (max_len < del.br_blockcount) {
5274 del.br_startoff += del.br_blockcount - max_len;
5275 if (!wasdel)
5276 del.br_startblock += del.br_blockcount - max_len;
5277 del.br_blockcount = max_len;
5278 }
5279
Dave Chinner0703a8e2018-06-08 09:54:22 -07005280 if (!isrt)
5281 goto delete;
5282
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 sum = del.br_startblock + del.br_blockcount;
Dave Chinner0703a8e2018-06-08 09:54:22 -07005284 div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod);
5285 if (mod) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 /*
5287 * Realtime extent not lined up at the end.
5288 * The extent could have been split into written
5289 * and unwritten pieces, or we could just be
5290 * unmapping part of it. But we can't really
5291 * get rid of part of a realtime extent.
5292 */
5293 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005294 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 /*
5296 * This piece is unwritten, or we're not
5297 * using unwritten extents. Skip over it.
5298 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005299 ASSERT(end >= mod);
5300 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 del.br_blockcount : mod;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005302 if (end < got.br_startoff &&
5303 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5304 done = true;
5305 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 }
5307 continue;
5308 }
5309 /*
5310 * It's written, turn it unwritten.
5311 * This is better than zeroing it.
5312 */
5313 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005314 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 /*
5316 * If this spans a realtime extent boundary,
5317 * chop it back to the start of the one we end at.
5318 */
5319 if (del.br_blockcount > mod) {
5320 del.br_startoff += del.br_blockcount - mod;
5321 del.br_startblock += del.br_blockcount - mod;
5322 del.br_blockcount = mod;
5323 }
5324 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005325 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005326 whichfork, &icur, &cur, &del,
Brian Foster81ba8f32018-07-11 22:26:16 -07005327 firstblock, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 if (error)
5329 goto error0;
5330 goto nodelete;
5331 }
Dave Chinner0703a8e2018-06-08 09:54:22 -07005332 div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod);
5333 if (mod) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 /*
5335 * Realtime extent is lined up at the end but not
5336 * at the front. We'll get rid of full extents if
5337 * we can.
5338 */
5339 mod = mp->m_sb.sb_rextsize - mod;
5340 if (del.br_blockcount > mod) {
5341 del.br_blockcount -= mod;
5342 del.br_startoff += mod;
5343 del.br_startblock += mod;
5344 } else if ((del.br_startoff == start &&
5345 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005346 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005347 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 /*
5349 * Can't make it unwritten. There isn't
5350 * a full extent here so just skip it.
5351 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005352 ASSERT(end >= del.br_blockcount);
5353 end -= del.br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005354 if (got.br_startoff > end &&
5355 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5356 done = true;
5357 break;
5358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 continue;
5360 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005361 struct xfs_bmbt_irec prev;
5362
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 /*
5364 * This one is already unwritten.
5365 * It must have a written left neighbor.
5366 * Unwrite the killed part of that one and
5367 * try again.
5368 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005369 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5370 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005372 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 ASSERT(del.br_startblock ==
5374 prev.br_startblock + prev.br_blockcount);
5375 if (prev.br_startoff < start) {
5376 mod = start - prev.br_startoff;
5377 prev.br_blockcount -= mod;
5378 prev.br_startblock += mod;
5379 prev.br_startoff = start;
5380 }
5381 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005382 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005383 ip, whichfork, &icur, &cur,
Brian Foster81ba8f32018-07-11 22:26:16 -07005384 &prev, firstblock, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 if (error)
5386 goto error0;
5387 goto nodelete;
5388 } else {
5389 ASSERT(del.br_state == XFS_EXT_NORM);
5390 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005391 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005392 ip, whichfork, &icur, &cur,
Brian Foster81ba8f32018-07-11 22:26:16 -07005393 &del, firstblock, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 if (error)
5395 goto error0;
5396 goto nodelete;
5397 }
5398 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005399
Dave Chinner0703a8e2018-06-08 09:54:22 -07005400delete:
Brian Fosterb2706a02016-03-15 11:42:46 +11005401 if (wasdel) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005402 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005403 &got, &del);
5404 } else {
Brian Foster81ba8f32018-07-11 22:26:16 -07005405 error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5406 &del, &tmp_logflags, whichfork,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005407 flags);
5408 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005409 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005410
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 if (error)
5412 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005413
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005414 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005415 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 /*
5418 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005420 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005421 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5422 (got.br_startoff > end &&
5423 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5424 done = true;
5425 break;
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 extno++;
5428 }
5429 }
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005430 if (done || end == (xfs_fileoff_t)-1 || end < start)
Darrick J. Wong44535932016-10-03 09:11:29 -07005431 *rlen = 0;
5432 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005433 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005434
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 /*
5436 * Convert to a btree if necessary.
5437 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005438 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 ASSERT(cur == NULL);
Brian Foster81ba8f32018-07-11 22:26:16 -07005440 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, &cur, 0,
5441 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 logflags |= tmp_logflags;
5443 if (error)
5444 goto error0;
5445 }
5446 /*
5447 * transform from btree to extents, give it cur
5448 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005449 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 ASSERT(cur != NULL);
5451 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5452 whichfork);
5453 logflags |= tmp_logflags;
5454 if (error)
5455 goto error0;
5456 }
5457 /*
5458 * transform from extents to local?
5459 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 error = 0;
5461error0:
5462 /*
5463 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005464 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005466 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005468 logflags &= ~xfs_ilog_fext(whichfork);
5469 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005471 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 /*
5473 * Log inode even in the error case, if the transaction
5474 * is dirty we'll need to shut down the filesystem.
5475 */
5476 if (logflags)
5477 xfs_trans_log_inode(tp, ip, logflags);
5478 if (cur) {
5479 if (!error) {
5480 *firstblock = cur->bc_private.b.firstblock;
5481 cur->bc_private.b.allocated = 0;
5482 }
5483 xfs_btree_del_cursor(cur,
5484 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5485 }
5486 return error;
5487}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005488
Darrick J. Wong44535932016-10-03 09:11:29 -07005489/* Unmap a range of a file. */
5490int
5491xfs_bunmapi(
5492 xfs_trans_t *tp,
5493 struct xfs_inode *ip,
5494 xfs_fileoff_t bno,
5495 xfs_filblks_t len,
5496 int flags,
5497 xfs_extnum_t nexts,
5498 xfs_fsblock_t *firstblock,
Darrick J. Wong44535932016-10-03 09:11:29 -07005499 int *done)
5500{
5501 int error;
5502
Brian Fosterccd9d912018-07-11 22:26:13 -07005503 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock);
Darrick J. Wong44535932016-10-03 09:11:29 -07005504 *done = (len == 0);
5505 return error;
5506}
5507
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005508/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005509 * Determine whether an extent shift can be accomplished by a merge with the
5510 * extent that precedes the target hole of the shift.
5511 */
5512STATIC bool
5513xfs_bmse_can_merge(
5514 struct xfs_bmbt_irec *left, /* preceding extent */
5515 struct xfs_bmbt_irec *got, /* current extent to shift */
5516 xfs_fileoff_t shift) /* shift fsb */
5517{
5518 xfs_fileoff_t startoff;
5519
5520 startoff = got->br_startoff - shift;
5521
5522 /*
5523 * The extent, once shifted, must be adjacent in-file and on-disk with
5524 * the preceding extent.
5525 */
5526 if ((left->br_startoff + left->br_blockcount != startoff) ||
5527 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5528 (left->br_state != got->br_state) ||
5529 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5530 return false;
5531
5532 return true;
5533}
5534
5535/*
5536 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5537 * hole in the file. If an extent shift would result in the extent being fully
5538 * adjacent to the extent that currently precedes the hole, we can merge with
5539 * the preceding extent rather than do the shift.
5540 *
5541 * This function assumes the caller has verified a shift-by-merge is possible
5542 * with the provided extents via xfs_bmse_can_merge().
5543 */
5544STATIC int
5545xfs_bmse_merge(
5546 struct xfs_inode *ip,
5547 int whichfork,
5548 xfs_fileoff_t shift, /* shift fsb */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005549 struct xfs_iext_cursor *icur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005550 struct xfs_bmbt_irec *got, /* extent to shift */
5551 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005552 struct xfs_btree_cur *cur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005553 int *logflags, /* output */
5554 struct xfs_defer_ops *dfops)
Brian Fosterddb19e32014-09-23 15:38:09 +10005555{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005556 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005557 xfs_filblks_t blockcount;
5558 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005559 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005560
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005561 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005562
5563 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5564 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005565 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005566
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005567 new = *left;
5568 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005569
5570 /*
5571 * Update the on-disk extent count, the btree if necessary and log the
5572 * inode.
5573 */
5574 XFS_IFORK_NEXT_SET(ip, whichfork,
5575 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5576 *logflags |= XFS_ILOG_CORE;
5577 if (!cur) {
5578 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005579 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005580 }
5581
5582 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005583 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005584 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005585 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005586 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005587
5588 error = xfs_btree_delete(cur, &i);
5589 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005590 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005591 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005592
5593 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005594 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005595 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005596 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005597 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005598
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005599 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005600 if (error)
5601 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005602
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005603done:
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005604 xfs_iext_remove(ip, icur, 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005605 xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur);
5606 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5607 &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005608
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005609 /* update reverse mapping. rmap functions merge the rmaps for us */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005610 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
5611 if (error)
5612 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005613 memcpy(&new, got, sizeof(new));
5614 new.br_startoff = left->br_startoff + left->br_blockcount;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005615 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005616}
5617
Christoph Hellwigbf806282017-10-19 11:07:34 -07005618static int
5619xfs_bmap_shift_update_extent(
5620 struct xfs_inode *ip,
5621 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005622 struct xfs_iext_cursor *icur,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005623 struct xfs_bmbt_irec *got,
5624 struct xfs_btree_cur *cur,
5625 int *logflags,
5626 struct xfs_defer_ops *dfops,
5627 xfs_fileoff_t startoff)
Brian Fostera979bdf2014-09-23 15:39:04 +10005628{
Christoph Hellwigbf806282017-10-19 11:07:34 -07005629 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005630 struct xfs_bmbt_irec prev = *got;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005631 int error, i;
Brian Fostera979bdf2014-09-23 15:39:04 +10005632
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005633 *logflags |= XFS_ILOG_CORE;
5634
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005635 got->br_startoff = startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005636
5637 if (cur) {
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005638 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005639 if (error)
5640 return error;
5641 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5642
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005643 error = xfs_bmbt_update(cur, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005644 if (error)
5645 return error;
5646 } else {
5647 *logflags |= XFS_ILOG_DEXT;
5648 }
5649
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005650 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5651 got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005652
Darrick J. Wong9c194642016-08-03 12:16:05 +10005653 /* update reverse mapping */
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005654 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &prev);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005655 if (error)
5656 return error;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005657 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005658}
5659
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005660int
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005661xfs_bmap_collapse_extents(
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005662 struct xfs_trans *tp,
5663 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005664 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005665 xfs_fileoff_t offset_shift_fsb,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005666 bool *done,
Brian Fosterf4a9cf972018-07-11 22:26:15 -07005667 xfs_fsblock_t *firstblock)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005668{
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005669 int whichfork = XFS_DATA_FORK;
5670 struct xfs_mount *mp = ip->i_mount;
5671 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5672 struct xfs_btree_cur *cur = NULL;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005673 struct xfs_bmbt_irec got, prev;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005674 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005675 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005676 int error = 0;
5677 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005678
5679 if (unlikely(XFS_TEST_ERROR(
5680 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5681 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005682 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005683 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005684 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005685 }
5686
5687 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005688 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005689
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005690 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005691
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005692 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005693 error = xfs_iread_extents(tp, ip, whichfork);
5694 if (error)
5695 return error;
5696 }
5697
Brian Fosterddb19e32014-09-23 15:38:09 +10005698 if (ifp->if_flags & XFS_IFBROOT) {
5699 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5700 cur->bc_private.b.firstblock = *firstblock;
Brian Fosterddb19e32014-09-23 15:38:09 +10005701 cur->bc_private.b.flags = 0;
5702 }
5703
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005704 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005705 *done = true;
5706 goto del_cursor;
5707 }
Eric Sandeend41c6172017-11-27 18:23:32 -08005708 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5709 del_cursor);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005710
Christoph Hellwigbf806282017-10-19 11:07:34 -07005711 new_startoff = got.br_startoff - offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005712 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005713 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5714 error = -EINVAL;
5715 goto del_cursor;
5716 }
5717
Christoph Hellwigbf806282017-10-19 11:07:34 -07005718 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5719 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005720 &icur, &got, &prev, cur, &logflags,
Brian Fosterf4a9cf972018-07-11 22:26:15 -07005721 tp->t_dfops);
Christoph Hellwigbf806282017-10-19 11:07:34 -07005722 if (error)
5723 goto del_cursor;
5724 goto done;
5725 }
5726 } else {
5727 if (got.br_startoff < offset_shift_fsb) {
5728 error = -EINVAL;
5729 goto del_cursor;
5730 }
5731 }
5732
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005733 error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur,
Brian Fosterf4a9cf972018-07-11 22:26:15 -07005734 &logflags, tp->t_dfops, new_startoff);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005735 if (error)
5736 goto del_cursor;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005737
Christoph Hellwig42630362017-11-03 10:34:41 -07005738done:
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005739 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5740 *done = true;
5741 goto del_cursor;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005742 }
5743
Christoph Hellwigbf806282017-10-19 11:07:34 -07005744 *next_fsb = got.br_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005745del_cursor:
5746 if (cur)
5747 xfs_btree_del_cursor(cur,
5748 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005749 if (logflags)
5750 xfs_trans_log_inode(tp, ip, logflags);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005751 return error;
5752}
5753
Darrick J. Wongf62cb482018-06-21 23:26:57 -07005754/* Make sure we won't be right-shifting an extent past the maximum bound. */
5755int
5756xfs_bmap_can_insert_extents(
5757 struct xfs_inode *ip,
5758 xfs_fileoff_t off,
5759 xfs_fileoff_t shift)
5760{
5761 struct xfs_bmbt_irec got;
5762 int is_empty;
5763 int error = 0;
5764
5765 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5766
5767 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5768 return -EIO;
5769
5770 xfs_ilock(ip, XFS_ILOCK_EXCL);
5771 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
5772 if (!error && !is_empty && got.br_startoff >= off &&
5773 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
5774 error = -EINVAL;
5775 xfs_iunlock(ip, XFS_ILOCK_EXCL);
5776
5777 return error;
5778}
5779
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005780int
5781xfs_bmap_insert_extents(
5782 struct xfs_trans *tp,
5783 struct xfs_inode *ip,
5784 xfs_fileoff_t *next_fsb,
5785 xfs_fileoff_t offset_shift_fsb,
5786 bool *done,
5787 xfs_fileoff_t stop_fsb,
Brian Fosterf4a9cf972018-07-11 22:26:15 -07005788 xfs_fsblock_t *firstblock)
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005789{
5790 int whichfork = XFS_DATA_FORK;
5791 struct xfs_mount *mp = ip->i_mount;
5792 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5793 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005794 struct xfs_bmbt_irec got, next;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005795 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005796 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005797 int error = 0;
5798 int logflags = 0;
5799
5800 if (unlikely(XFS_TEST_ERROR(
5801 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5802 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5803 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5804 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
5805 return -EFSCORRUPTED;
5806 }
5807
5808 if (XFS_FORCED_SHUTDOWN(mp))
5809 return -EIO;
5810
5811 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5812
5813 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5814 error = xfs_iread_extents(tp, ip, whichfork);
5815 if (error)
5816 return error;
5817 }
5818
5819 if (ifp->if_flags & XFS_IFBROOT) {
5820 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5821 cur->bc_private.b.firstblock = *firstblock;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005822 cur->bc_private.b.flags = 0;
5823 }
5824
Namjae Jeona904b1c2015-03-25 15:08:56 +11005825 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005826 xfs_iext_last(ifp, &icur);
5827 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005828 stop_fsb > got.br_startoff) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005829 *done = true;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005830 goto del_cursor;
5831 }
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005832 } else {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005833 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005834 *done = true;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005835 goto del_cursor;
5836 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005837 }
Eric Sandeend41c6172017-11-27 18:23:32 -08005838 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5839 del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005840
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005841 if (stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005842 error = -EIO;
5843 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005844 }
5845
Christoph Hellwigbf806282017-10-19 11:07:34 -07005846 new_startoff = got.br_startoff + offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005847 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005848 if (new_startoff + got.br_blockcount > next.br_startoff) {
5849 error = -EINVAL;
5850 goto del_cursor;
5851 }
5852
5853 /*
5854 * Unlike a left shift (which involves a hole punch), a right
5855 * shift does not modify extent neighbors in any way. We should
5856 * never find mergeable extents in this scenario. Check anyways
5857 * and warn if we encounter two extents that could be one.
5858 */
5859 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5860 WARN_ON_ONCE(1);
5861 }
5862
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005863 error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur,
Brian Fosterf4a9cf972018-07-11 22:26:15 -07005864 &logflags, tp->t_dfops, new_startoff);
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005865 if (error)
5866 goto del_cursor;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005867
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005868 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005869 stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005870 *done = true;
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005871 goto del_cursor;
5872 }
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005873
5874 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005875del_cursor:
5876 if (cur)
5877 xfs_btree_del_cursor(cur,
5878 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Brian Fosterca446d82014-09-02 12:12:53 +10005879 if (logflags)
5880 xfs_trans_log_inode(tp, ip, logflags);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005881 return error;
5882}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005883
5884/*
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005885 * Splits an extent into two extents at split_fsb block such that it is the
5886 * first block of the current_ext. @ext is a target extent to be split.
5887 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5888 * hole or the first block of extents, just return 0.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005889 */
5890STATIC int
5891xfs_bmap_split_extent_at(
5892 struct xfs_trans *tp,
5893 struct xfs_inode *ip,
5894 xfs_fileoff_t split_fsb,
Brian Fosterd76e6ce2018-07-11 22:26:10 -07005895 xfs_fsblock_t *firstfsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005896{
5897 int whichfork = XFS_DATA_FORK;
5898 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005899 struct xfs_bmbt_irec got;
5900 struct xfs_bmbt_irec new; /* split extent */
5901 struct xfs_mount *mp = ip->i_mount;
5902 struct xfs_ifork *ifp;
5903 xfs_fsblock_t gotblkcnt; /* new block count for got */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005904 struct xfs_iext_cursor icur;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005905 int error = 0;
5906 int logflags = 0;
5907 int i = 0;
5908
5909 if (unlikely(XFS_TEST_ERROR(
5910 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5911 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005912 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11005913 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5914 XFS_ERRLEVEL_LOW, mp);
5915 return -EFSCORRUPTED;
5916 }
5917
5918 if (XFS_FORCED_SHUTDOWN(mp))
5919 return -EIO;
5920
5921 ifp = XFS_IFORK_PTR(ip, whichfork);
5922 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5923 /* Read in all the extents */
5924 error = xfs_iread_extents(tp, ip, whichfork);
5925 if (error)
5926 return error;
5927 }
5928
5929 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005930 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005931 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005932 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005933 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005934 return 0;
5935
5936 gotblkcnt = split_fsb - got.br_startoff;
5937 new.br_startoff = split_fsb;
5938 new.br_startblock = got.br_startblock + gotblkcnt;
5939 new.br_blockcount = got.br_blockcount - gotblkcnt;
5940 new.br_state = got.br_state;
5941
5942 if (ifp->if_flags & XFS_IFBROOT) {
5943 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5944 cur->bc_private.b.firstblock = *firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005945 cur->bc_private.b.flags = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005946 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005947 if (error)
5948 goto del_cursor;
5949 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5950 }
5951
Namjae Jeona904b1c2015-03-25 15:08:56 +11005952 got.br_blockcount = gotblkcnt;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005953 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
5954 &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005955
5956 logflags = XFS_ILOG_CORE;
5957 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005958 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005959 if (error)
5960 goto del_cursor;
5961 } else
5962 logflags |= XFS_ILOG_DEXT;
5963
5964 /* Add new extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005965 xfs_iext_next(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005966 xfs_iext_insert(ip, &icur, &new, 0);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005967 XFS_IFORK_NEXT_SET(ip, whichfork,
5968 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5969
5970 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005971 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005972 if (error)
5973 goto del_cursor;
5974 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005975 error = xfs_btree_insert(cur, &i);
5976 if (error)
5977 goto del_cursor;
5978 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5979 }
5980
5981 /*
5982 * Convert to a btree if necessary.
5983 */
5984 if (xfs_bmap_needs_btree(ip, whichfork)) {
5985 int tmp_logflags; /* partial log flag return val */
5986
5987 ASSERT(cur == NULL);
Brian Foster81ba8f32018-07-11 22:26:16 -07005988 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, &cur, 0,
5989 &tmp_logflags, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005990 logflags |= tmp_logflags;
5991 }
5992
5993del_cursor:
5994 if (cur) {
5995 cur->bc_private.b.allocated = 0;
5996 xfs_btree_del_cursor(cur,
5997 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5998 }
5999
6000 if (logflags)
6001 xfs_trans_log_inode(tp, ip, logflags);
6002 return error;
6003}
6004
6005int
6006xfs_bmap_split_extent(
6007 struct xfs_inode *ip,
6008 xfs_fileoff_t split_fsb)
6009{
6010 struct xfs_mount *mp = ip->i_mount;
6011 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006012 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006013 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006014 int error;
6015
Christoph Hellwig253f4912016-04-06 09:19:55 +10006016 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6017 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6018 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006019 return error;
Brian Fosterbcd2c9f2018-07-11 22:26:19 -07006020 xfs_defer_init(tp, &dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006021
6022 xfs_ilock(ip, XFS_ILOCK_EXCL);
6023 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6024
Namjae Jeona904b1c2015-03-25 15:08:56 +11006025 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Brian Fosterd76e6ce2018-07-11 22:26:10 -07006026 &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006027 if (error)
6028 goto out;
6029
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07006030 error = xfs_defer_finish(&tp, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006031 if (error)
6032 goto out;
6033
Christoph Hellwig70393312015-06-04 13:48:08 +10006034 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006035
6036out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006037 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006038 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006039 return error;
6040}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006041
6042/* Deferred mapping is only for real extents in the data fork. */
6043static bool
6044xfs_bmap_is_update_needed(
6045 struct xfs_bmbt_irec *bmap)
6046{
6047 return bmap->br_startblock != HOLESTARTBLOCK &&
6048 bmap->br_startblock != DELAYSTARTBLOCK;
6049}
6050
6051/* Record a bmap intent. */
6052static int
6053__xfs_bmap_add(
6054 struct xfs_mount *mp,
6055 struct xfs_defer_ops *dfops,
6056 enum xfs_bmap_intent_type type,
6057 struct xfs_inode *ip,
6058 int whichfork,
6059 struct xfs_bmbt_irec *bmap)
6060{
6061 int error;
6062 struct xfs_bmap_intent *bi;
6063
6064 trace_xfs_bmap_defer(mp,
6065 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6066 type,
6067 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6068 ip->i_ino, whichfork,
6069 bmap->br_startoff,
6070 bmap->br_blockcount,
6071 bmap->br_state);
6072
6073 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6074 INIT_LIST_HEAD(&bi->bi_list);
6075 bi->bi_type = type;
6076 bi->bi_owner = ip;
6077 bi->bi_whichfork = whichfork;
6078 bi->bi_bmap = *bmap;
6079
Christoph Hellwig882d8782017-08-28 10:21:03 -07006080 error = xfs_defer_ijoin(dfops, bi->bi_owner);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006081 if (error) {
6082 kmem_free(bi);
6083 return error;
6084 }
6085
6086 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6087 return 0;
6088}
6089
6090/* Map an extent into a file. */
6091int
6092xfs_bmap_map_extent(
6093 struct xfs_mount *mp,
6094 struct xfs_defer_ops *dfops,
6095 struct xfs_inode *ip,
6096 struct xfs_bmbt_irec *PREV)
6097{
6098 if (!xfs_bmap_is_update_needed(PREV))
6099 return 0;
6100
6101 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6102 XFS_DATA_FORK, PREV);
6103}
6104
6105/* Unmap an extent out of a file. */
6106int
6107xfs_bmap_unmap_extent(
6108 struct xfs_mount *mp,
6109 struct xfs_defer_ops *dfops,
6110 struct xfs_inode *ip,
6111 struct xfs_bmbt_irec *PREV)
6112{
6113 if (!xfs_bmap_is_update_needed(PREV))
6114 return 0;
6115
6116 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6117 XFS_DATA_FORK, PREV);
6118}
6119
6120/*
6121 * Process one of the deferred bmap operations. We pass back the
6122 * btree cursor to maintain our lock on the bmapbt between calls.
6123 */
6124int
6125xfs_bmap_finish_one(
6126 struct xfs_trans *tp,
6127 struct xfs_defer_ops *dfops,
6128 struct xfs_inode *ip,
6129 enum xfs_bmap_intent_type type,
6130 int whichfork,
6131 xfs_fileoff_t startoff,
6132 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006133 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006134 xfs_exntst_t state)
6135{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006136 xfs_fsblock_t firstfsb;
6137 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006138
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006139 /*
6140 * firstfsb is tied to the transaction lifetime and is used to
6141 * ensure correct AG locking order and schedule work item
6142 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6143 * to only making one bmap call per transaction, so it should
6144 * be safe to have it as a local variable here.
6145 */
6146 firstfsb = NULLFSBLOCK;
6147
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006148 trace_xfs_bmap_deferred(tp->t_mountp,
6149 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6150 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006151 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006152
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006153 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006154 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006155
6156 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006157 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006158 return -EIO;
6159
6160 switch (type) {
6161 case XFS_BMAP_MAP:
Brian Fosterff3edf22018-07-11 22:26:14 -07006162 ASSERT(dfops == tp->t_dfops);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006163 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Brian Fosterff3edf22018-07-11 22:26:14 -07006164 startblock, 0);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006165 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006166 break;
6167 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006168 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
Brian Fosterccd9d912018-07-11 22:26:13 -07006169 XFS_BMAPI_REMAP, 1, &firstfsb);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006170 break;
6171 default:
6172 ASSERT(0);
6173 error = -EFSCORRUPTED;
6174 }
6175
6176 return error;
6177}
Darrick J. Wong30b09842018-03-23 10:06:52 -07006178
6179/* Check that an inode's extent does not have invalid flags or bad ranges. */
6180xfs_failaddr_t
6181xfs_bmap_validate_extent(
6182 struct xfs_inode *ip,
6183 int whichfork,
6184 struct xfs_bmbt_irec *irec)
6185{
6186 struct xfs_mount *mp = ip->i_mount;
6187 xfs_fsblock_t endfsb;
6188 bool isrt;
6189
6190 isrt = XFS_IS_REALTIME_INODE(ip);
6191 endfsb = irec->br_startblock + irec->br_blockcount - 1;
6192 if (isrt) {
6193 if (!xfs_verify_rtbno(mp, irec->br_startblock))
6194 return __this_address;
6195 if (!xfs_verify_rtbno(mp, endfsb))
6196 return __this_address;
6197 } else {
6198 if (!xfs_verify_fsbno(mp, irec->br_startblock))
6199 return __this_address;
6200 if (!xfs_verify_fsbno(mp, endfsb))
6201 return __this_address;
6202 if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) !=
6203 XFS_FSB_TO_AGNO(mp, endfsb))
6204 return __this_address;
6205 }
6206 if (irec->br_state != XFS_EXT_NORM) {
6207 if (whichfork != XFS_DATA_FORK)
6208 return __this_address;
6209 if (!xfs_sb_version_hasextflgbit(&mp->m_sb))
6210 return __this_address;
6211 }
6212 return NULL;
6213}