blob: 89a21dc7a7cb84106d27c562a2d7aa6d4d1ae839 [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);
627 xfs_bmap_add_free(mp, cur->bc_private.b.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(
647 xfs_trans_t *tp, /* transaction pointer */
648 xfs_inode_t *ip, /* incore inode pointer */
649 xfs_fsblock_t *firstblock, /* first-block-allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000650 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100651 xfs_btree_cur_t **curp, /* cursor returned to caller */
652 int wasdel, /* converting a delayed alloc */
653 int *logflagsp, /* inode logging flags */
654 int whichfork) /* data or attr fork */
655{
656 struct xfs_btree_block *ablock; /* allocated (child) bt block */
657 xfs_buf_t *abp; /* buffer for ablock */
658 xfs_alloc_arg_t args; /* allocation arguments */
659 xfs_bmbt_rec_t *arp; /* child record pointer */
660 struct xfs_btree_block *block; /* btree root block */
661 xfs_btree_cur_t *cur; /* bmap btree cursor */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100662 int error; /* error return value */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100663 xfs_ifork_t *ifp; /* inode fork pointer */
664 xfs_bmbt_key_t *kp; /* root block key pointer */
665 xfs_mount_t *mp; /* mount structure */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100666 xfs_bmbt_ptr_t *pp; /* root block address pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700667 struct xfs_iext_cursor icur;
Christoph Hellwig906abed2017-11-03 10:34:43 -0700668 struct xfs_bmbt_irec rec;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700669 xfs_extnum_t cnt = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100670
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500671 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700672 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100673 ifp = XFS_IFORK_PTR(ip, whichfork);
674 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
675
676 /*
677 * Make space in the inode incore.
678 */
679 xfs_iroot_realloc(ip, 1, whichfork);
680 ifp->if_flags |= XFS_IFBROOT;
681
682 /*
683 * Fill in the root.
684 */
685 block = ifp->if_broot;
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800686 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
687 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
Eric Sandeenf88ae462017-01-27 23:16:37 -0800688 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100689 /*
690 * Need a cursor. Can't allocate until bb_level is filled in.
691 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100692 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
693 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000694 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100695 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
696 /*
697 * Convert to a btree with two levels, one record in root.
698 */
699 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
700 memset(&args, 0, sizeof(args));
701 args.tp = tp;
702 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000703 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100704 args.firstblock = *firstblock;
705 if (*firstblock == NULLFSBLOCK) {
706 args.type = XFS_ALLOCTYPE_START_BNO;
707 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000708 } else if (dfops->dop_low) {
Dave Chinner9e5987a72013-02-25 12:31:26 +1100709 args.type = XFS_ALLOCTYPE_START_BNO;
710 args.fsbno = *firstblock;
711 } else {
712 args.type = XFS_ALLOCTYPE_NEAR_BNO;
713 args.fsbno = *firstblock;
714 }
715 args.minlen = args.maxlen = args.prod = 1;
716 args.wasdel = wasdel;
717 *logflagsp = 0;
718 if ((error = xfs_alloc_vextent(&args))) {
719 xfs_iroot_realloc(ip, -1, whichfork);
Eric Sandeen2c4306f2018-04-16 23:07:27 -0700720 ASSERT(ifp->if_broot == NULL);
721 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100722 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
723 return error;
724 }
Darrick J. Wong90e20562016-10-03 09:11:45 -0700725
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800726 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
727 xfs_iroot_realloc(ip, -1, whichfork);
Eric Sandeen2c4306f2018-04-16 23:07:27 -0700728 ASSERT(ifp->if_broot == NULL);
729 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800730 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
731 return -ENOSPC;
732 }
Dave Chinner9e5987a72013-02-25 12:31:26 +1100733 /*
734 * Allocation can't fail, the space was reserved.
735 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100736 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -0800737 args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100738 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
739 cur->bc_private.b.allocated++;
740 ip->i_d.di_nblocks++;
741 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
742 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
743 /*
744 * Fill in the child block.
745 */
746 abp->b_ops = &xfs_bmbt_buf_ops;
747 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800748 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
749 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500750 XFS_BTREE_LONG_PTRS);
751
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700752 for_each_xfs_iext(ifp, &icur, &rec) {
Christoph Hellwig906abed2017-11-03 10:34:43 -0700753 if (isnullstartblock(rec.br_startblock))
754 continue;
755 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
756 xfs_bmbt_disk_set_all(arp, &rec);
757 cnt++;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100758 }
759 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
760 xfs_btree_set_numrecs(ablock, cnt);
761
762 /*
763 * Fill in the root key and pointer.
764 */
765 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
766 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
767 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
768 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
769 be16_to_cpu(block->bb_level)));
770 *pp = cpu_to_be64(args.fsbno);
771
772 /*
773 * Do all this logging at the end so that
774 * the root is at the right level.
775 */
776 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
777 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
778 ASSERT(*curp == NULL);
779 *curp = cur;
780 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
781 return 0;
782}
783
784/*
785 * Convert a local file to an extents file.
786 * This code is out of bounds for data forks of regular files,
787 * since the file data needs to get logged so things will stay consistent.
788 * (The bmap-level manipulations are ok, though).
789 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000790void
791xfs_bmap_local_to_extents_empty(
792 struct xfs_inode *ip,
793 int whichfork)
794{
795 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
796
Darrick J. Wong60b49842016-10-03 09:11:34 -0700797 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000798 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
799 ASSERT(ifp->if_bytes == 0);
800 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
801
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000802 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000803 ifp->if_flags &= ~XFS_IFINLINE;
804 ifp->if_flags |= XFS_IFEXTENTS;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700805 ifp->if_u1.if_root = NULL;
806 ifp->if_height = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000807 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
808}
809
810
Dave Chinner9e5987a72013-02-25 12:31:26 +1100811STATIC int /* error */
812xfs_bmap_local_to_extents(
813 xfs_trans_t *tp, /* transaction pointer */
814 xfs_inode_t *ip, /* incore inode pointer */
815 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
816 xfs_extlen_t total, /* total blocks needed by transaction */
817 int *logflagsp, /* inode logging flags */
818 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500819 void (*init_fn)(struct xfs_trans *tp,
820 struct xfs_buf *bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100821 struct xfs_inode *ip,
822 struct xfs_ifork *ifp))
823{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000824 int error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100825 int flags; /* logging flags returned */
826 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000827 xfs_alloc_arg_t args; /* allocation arguments */
828 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700829 struct xfs_bmbt_irec rec;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700830 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100831
832 /*
833 * We don't want to deal with the case of keeping inode data inline yet.
834 * So sending the data fork of a regular inode is invalid.
835 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100836 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100837 ifp = XFS_IFORK_PTR(ip, whichfork);
838 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000839
840 if (!ifp->if_bytes) {
841 xfs_bmap_local_to_extents_empty(ip, whichfork);
842 flags = XFS_ILOG_CORE;
843 goto done;
844 }
845
Dave Chinner9e5987a72013-02-25 12:31:26 +1100846 flags = 0;
847 error = 0;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700848 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000849 memset(&args, 0, sizeof(args));
850 args.tp = tp;
851 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000852 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000853 args.firstblock = *firstblock;
854 /*
855 * Allocate a block. We know we need only one, since the
856 * file currently fits in an inode.
857 */
858 if (*firstblock == NULLFSBLOCK) {
859 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
860 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100861 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000862 args.fsbno = *firstblock;
863 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100864 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000865 args.total = total;
866 args.minlen = args.maxlen = args.prod = 1;
867 error = xfs_alloc_vextent(&args);
868 if (error)
869 goto done;
870
871 /* Can't fail, the space was reserved. */
872 ASSERT(args.fsbno != NULLFSBLOCK);
873 ASSERT(args.len == 1);
874 *firstblock = args.fsbno;
875 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
876
Dave Chinnerfe22d552015-01-22 09:30:06 +1100877 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100878 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100879 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100880 * The callout is responsible for logging because the remote format
881 * might differ from the local format and thus we don't know how much to
882 * log here. Note that init_fn must also set the buffer log item type
883 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100884 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000885 init_fn(tp, bp, ip, ifp);
886
Brian Fosterb7cdc662015-10-12 15:40:24 +1100887 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000888 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
889 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100890 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000891
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700892 ifp->if_u1.if_root = NULL;
893 ifp->if_height = 0;
894
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700895 rec.br_startoff = 0;
896 rec.br_startblock = args.fsbno;
897 rec.br_blockcount = 1;
898 rec.br_state = XFS_EXT_NORM;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700899 xfs_iext_first(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -0700900 xfs_iext_insert(ip, &icur, &rec, 0);
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700901
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000902 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
903 ip->i_d.di_nblocks = 1;
904 xfs_trans_mod_dquot_byino(tp, ip,
905 XFS_TRANS_DQ_BCOUNT, 1L);
906 flags |= xfs_ilog_fext(whichfork);
907
Dave Chinner9e5987a72013-02-25 12:31:26 +1100908done:
909 *logflagsp = flags;
910 return error;
911}
912
913/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 * Called from xfs_bmap_add_attrfork to handle btree format files.
915 */
916STATIC int /* error */
917xfs_bmap_add_attrfork_btree(
918 xfs_trans_t *tp, /* transaction pointer */
919 xfs_inode_t *ip, /* incore inode pointer */
920 xfs_fsblock_t *firstblock, /* first block allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 int *flags) /* inode logging flags */
922{
923 xfs_btree_cur_t *cur; /* btree cursor */
924 int error; /* error return value */
925 xfs_mount_t *mp; /* file system mount struct */
926 int stat; /* newroot status */
927
928 mp = ip->i_mount;
929 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
930 *flags |= XFS_ILOG_DBROOT;
931 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100932 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Brian Foster0bd62072018-07-11 22:26:10 -0700933 cur->bc_private.b.dfops = tp->t_dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 cur->bc_private.b.firstblock = *firstblock;
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700935 error = xfs_bmbt_lookup_first(cur, &stat);
936 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000938 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100939 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100940 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 goto error0;
942 if (stat == 0) {
943 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000944 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946 *firstblock = cur->bc_private.b.firstblock;
947 cur->bc_private.b.allocated = 0;
948 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
949 }
950 return 0;
951error0:
952 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
953 return error;
954}
955
956/*
957 * Called from xfs_bmap_add_attrfork to handle extents format files.
958 */
959STATIC int /* error */
960xfs_bmap_add_attrfork_extents(
961 xfs_trans_t *tp, /* transaction pointer */
962 xfs_inode_t *ip, /* incore inode pointer */
963 xfs_fsblock_t *firstblock, /* first block allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 int *flags) /* inode logging flags */
965{
966 xfs_btree_cur_t *cur; /* bmap btree cursor */
967 int error; /* error return value */
968
969 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
970 return 0;
971 cur = NULL;
Brian Foster0bd62072018-07-11 22:26:10 -0700972 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, tp->t_dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 flags, XFS_DATA_FORK);
974 if (cur) {
975 cur->bc_private.b.allocated = 0;
976 xfs_btree_del_cursor(cur,
977 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
978 }
979 return error;
980}
981
982/*
Dave Chinner1e823792013-02-11 15:58:13 +1100983 * Called from xfs_bmap_add_attrfork to handle local format files. Each
984 * different data fork content type needs a different callout to do the
985 * conversion. Some are basic and only require special block initialisation
986 * callouts for the data formating, others (directories) are so specialised they
987 * handle everything themselves.
988 *
989 * XXX (dgc): investigate whether directory conversion can use the generic
990 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500991 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 */
993STATIC int /* error */
994xfs_bmap_add_attrfork_local(
995 xfs_trans_t *tp, /* transaction pointer */
996 xfs_inode_t *ip, /* incore inode pointer */
997 xfs_fsblock_t *firstblock, /* first block allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 int *flags) /* inode logging flags */
999{
1000 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1003 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001004
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001005 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001007 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 dargs.dp = ip;
1009 dargs.firstblock = firstblock;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001010 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 dargs.whichfork = XFS_DATA_FORK;
1012 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001013 return xfs_dir2_sf_to_block(&dargs);
1014 }
1015
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001016 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001017 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1018 flags, XFS_DATA_FORK,
1019 xfs_symlink_local_to_remote);
1020
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001021 /* should only be called for types that support local format data */
1022 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001023 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
1026/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001027 * Convert inode from non-attributed to attributed.
1028 * Must not be in a transaction, ip must not be locked.
1029 */
1030int /* error code */
1031xfs_bmap_add_attrfork(
1032 xfs_inode_t *ip, /* incore inode pointer */
1033 int size, /* space new attribute needs */
1034 int rsvd) /* xact may use reserved blks */
1035{
1036 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001037 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001038 xfs_mount_t *mp; /* mount structure */
1039 xfs_trans_t *tp; /* transaction pointer */
1040 int blks; /* space reservation */
1041 int version = 1; /* superblock attr version */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001042 int logflags; /* logging flags */
1043 int error; /* error return value */
1044
1045 ASSERT(XFS_IFORK_Q(ip) == 0);
1046
1047 mp = ip->i_mount;
1048 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001049
Dave Chinner9e5987a72013-02-25 12:31:26 +11001050 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001051
1052 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1053 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1054 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001055 return error;
Brian Foster40d03ac2018-07-11 22:26:09 -07001056 xfs_defer_init(&dfops, &firstblock);
1057 tp->t_dfops = &dfops;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001058
Dave Chinner9e5987a72013-02-25 12:31:26 +11001059 xfs_ilock(ip, XFS_ILOCK_EXCL);
1060 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1061 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1062 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001063 if (error)
1064 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001065 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001066 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001067 if (ip->i_d.di_anextents != 0) {
1068 error = -EFSCORRUPTED;
1069 goto trans_cancel;
1070 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001071 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1072 /*
1073 * For inodes coming from pre-6.2 filesystems.
1074 */
1075 ASSERT(ip->i_d.di_aformat == 0);
1076 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1077 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001078
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001079 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001080 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1081
1082 switch (ip->i_d.di_format) {
1083 case XFS_DINODE_FMT_DEV:
1084 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1085 break;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001086 case XFS_DINODE_FMT_LOCAL:
1087 case XFS_DINODE_FMT_EXTENTS:
1088 case XFS_DINODE_FMT_BTREE:
1089 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1090 if (!ip->i_d.di_forkoff)
1091 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1092 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1093 version = 2;
1094 break;
1095 default:
1096 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001097 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001098 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001099 }
1100
1101 ASSERT(ip->i_afp == NULL);
1102 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1103 ip->i_afp->if_flags = XFS_IFEXTENTS;
1104 logflags = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001105 switch (ip->i_d.di_format) {
1106 case XFS_DINODE_FMT_LOCAL:
Brian Foster0bd62072018-07-11 22:26:10 -07001107 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock,
1108 &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001109 break;
1110 case XFS_DINODE_FMT_EXTENTS:
1111 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Brian Foster0bd62072018-07-11 22:26:10 -07001112 &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001113 break;
1114 case XFS_DINODE_FMT_BTREE:
Brian Foster0bd62072018-07-11 22:26:10 -07001115 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock,
1116 &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001117 break;
1118 default:
1119 error = 0;
1120 break;
1121 }
1122 if (logflags)
1123 xfs_trans_log_inode(tp, ip, logflags);
1124 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001125 goto bmap_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001126 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1127 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001128 bool log_sb = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001129
1130 spin_lock(&mp->m_sb_lock);
1131 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1132 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001133 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001134 }
1135 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1136 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001137 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001138 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001139 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001140 if (log_sb)
1141 xfs_log_sb(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001142 }
1143
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001144 error = xfs_defer_finish(&tp, &dfops);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001145 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001146 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001147 error = xfs_trans_commit(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001148 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001149 return error;
1150
1151bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001152 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001153trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001154 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001155 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001156 return error;
1157}
1158
1159/*
1160 * Internal and external extent tree search functions.
1161 */
1162
1163/*
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001164 * Read in extents from a btree-format inode.
Dave Chinner9e5987a72013-02-25 12:31:26 +11001165 */
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001166int
1167xfs_iread_extents(
1168 struct xfs_trans *tp,
1169 struct xfs_inode *ip,
1170 int whichfork)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001171{
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001172 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwige8e0e172017-10-19 11:06:29 -07001173 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001174 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1175 xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
1176 struct xfs_btree_block *block = ifp->if_broot;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001177 struct xfs_iext_cursor icur;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -07001178 struct xfs_bmbt_irec new;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001179 xfs_fsblock_t bno;
1180 struct xfs_buf *bp;
1181 xfs_extnum_t i, j;
1182 int level;
1183 __be64 *pp;
1184 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001185
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001186 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1187
1188 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1189 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1190 return -EFSCORRUPTED;
1191 }
1192
Dave Chinner9e5987a72013-02-25 12:31:26 +11001193 /*
1194 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1195 */
1196 level = be16_to_cpu(block->bb_level);
1197 ASSERT(level > 0);
1198 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1199 bno = be64_to_cpu(*pp);
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -08001200
Dave Chinner9e5987a72013-02-25 12:31:26 +11001201 /*
1202 * Go down the tree until leaf level is reached, following the first
1203 * pointer (leftmost) at each level.
1204 */
1205 while (level-- > 0) {
1206 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1207 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1208 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001209 goto out;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001210 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001211 if (level == 0)
1212 break;
1213 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1214 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001215 XFS_WANT_CORRUPTED_GOTO(mp,
Darrick J. Wong59f6fec2018-01-08 10:51:00 -08001216 xfs_verify_fsbno(mp, bno), out_brelse);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001217 xfs_trans_brelse(tp, bp);
1218 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001219
Dave Chinner9e5987a72013-02-25 12:31:26 +11001220 /*
1221 * Here with bp and block set to the leftmost leaf node in the tree.
1222 */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001223 i = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001224 xfs_iext_first(ifp, &icur);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001225
Dave Chinner9e5987a72013-02-25 12:31:26 +11001226 /*
1227 * Loop over all leaf nodes. Copy information to the extent records.
1228 */
1229 for (;;) {
1230 xfs_bmbt_rec_t *frp;
1231 xfs_fsblock_t nextbno;
1232 xfs_extnum_t num_recs;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001233
1234 num_recs = xfs_btree_get_numrecs(block);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001235 if (unlikely(i + num_recs > nextents)) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001236 xfs_warn(ip->i_mount,
1237 "corrupt dinode %Lu, (btree extents).",
1238 (unsigned long long) ip->i_ino);
Darrick J. Wong90a58f92018-03-23 10:06:52 -07001239 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1240 __func__, block, sizeof(*block),
1241 __this_address);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001242 error = -EFSCORRUPTED;
1243 goto out_brelse;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001244 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001245 /*
1246 * Read-ahead the next leaf block, if any.
1247 */
1248 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1249 if (nextbno != NULLFSBLOCK)
1250 xfs_btree_reada_bufl(mp, nextbno, 1,
1251 &xfs_bmbt_buf_ops);
1252 /*
1253 * Copy records into the extent records.
1254 */
1255 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Christoph Hellwig6bdcf262017-11-03 10:34:46 -07001256 for (j = 0; j < num_recs; j++, frp++, i++) {
Darrick J. Wong30b09842018-03-23 10:06:52 -07001257 xfs_failaddr_t fa;
1258
Christoph Hellwigdac9c9b2017-11-03 10:34:47 -07001259 xfs_bmbt_disk_get_all(frp, &new);
Darrick J. Wong30b09842018-03-23 10:06:52 -07001260 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1261 if (fa) {
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001262 error = -EFSCORRUPTED;
Darrick J. Wong30b09842018-03-23 10:06:52 -07001263 xfs_inode_verifier_error(ip, error,
1264 "xfs_iread_extents(2)",
1265 frp, sizeof(*frp), fa);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001266 goto out_brelse;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001267 }
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001268 xfs_iext_insert(ip, &icur, &new, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001269 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
1270 xfs_iext_next(ifp, &icur);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001271 }
1272 xfs_trans_brelse(tp, bp);
1273 bno = nextbno;
1274 /*
1275 * If we've reached the end, stop.
1276 */
1277 if (bno == NULLFSBLOCK)
1278 break;
1279 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1280 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1281 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001282 goto out;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001283 block = XFS_BUF_TO_BLOCK(bp);
1284 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001285
1286 if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) {
1287 error = -EFSCORRUPTED;
1288 goto out;
1289 }
Eric Sandeen5d829302016-11-08 12:59:42 +11001290 ASSERT(i == xfs_iext_count(ifp));
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001291
1292 ifp->if_flags |= XFS_IFEXTENTS;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001293 return 0;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001294
1295out_brelse:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001296 xfs_trans_brelse(tp, bp);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001297out:
1298 xfs_iext_destroy(ifp);
1299 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001300}
1301
Dave Chinner9e5987a72013-02-25 12:31:26 +11001302/*
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001303 * Returns the relative block number of the first unused block(s) in the given
1304 * fork with at least "len" logically contiguous blocks free. This is the
1305 * lowest-address hole if the fork has holes, else the first block past the end
1306 * of fork. Return 0 if the fork is currently local (in-inode).
Dave Chinner9e5987a72013-02-25 12:31:26 +11001307 */
1308int /* error */
1309xfs_bmap_first_unused(
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001310 struct xfs_trans *tp, /* transaction pointer */
1311 struct xfs_inode *ip, /* incore inode */
1312 xfs_extlen_t len, /* size of hole to find */
1313 xfs_fileoff_t *first_unused, /* unused block */
1314 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001315{
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001316 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1317 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001318 struct xfs_iext_cursor icur;
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001319 xfs_fileoff_t lastaddr = 0;
1320 xfs_fileoff_t lowest, max;
1321 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001322
1323 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1324 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1325 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001326
Dave Chinner9e5987a72013-02-25 12:31:26 +11001327 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1328 *first_unused = 0;
1329 return 0;
1330 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001331
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001332 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1333 error = xfs_iread_extents(tp, ip, whichfork);
1334 if (error)
1335 return error;
1336 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001337
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001338 lowest = max = *first_unused;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001339 for_each_xfs_iext(ifp, &icur, &got) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001340 /*
1341 * See if the hole before this extent will work.
1342 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001343 if (got.br_startoff >= lowest + len &&
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001344 got.br_startoff - max >= len)
1345 break;
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001346 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001347 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1348 }
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001349
Dave Chinner9e5987a72013-02-25 12:31:26 +11001350 *first_unused = max;
1351 return 0;
1352}
1353
1354/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001355 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a72013-02-25 12:31:26 +11001356 * last_block (input value) in the file.
1357 * This is not based on i_size, it is based on the extent records.
1358 * Returns 0 for local files, as they do not have extent records.
1359 */
1360int /* error */
1361xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001362 struct xfs_trans *tp, /* transaction pointer */
1363 struct xfs_inode *ip, /* incore inode */
1364 xfs_fileoff_t *last_block, /* last block */
1365 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001366{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001367 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1368 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001369 struct xfs_iext_cursor icur;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001370 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001371
Christoph Hellwig86685f72016-11-24 11:39:38 +11001372 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1373 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001374 *last_block = 0;
1375 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001376 case XFS_DINODE_FMT_BTREE:
1377 case XFS_DINODE_FMT_EXTENTS:
1378 break;
1379 default:
1380 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001381 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001382
1383 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1384 error = xfs_iread_extents(tp, ip, whichfork);
1385 if (error)
1386 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001387 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001388
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001389 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
Christoph Hellwigdc560152017-10-23 16:32:39 -07001390 *last_block = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001391 return 0;
1392}
1393
Dave Chinner68988112013-08-12 20:49:42 +10001394int
Dave Chinner9e5987a72013-02-25 12:31:26 +11001395xfs_bmap_last_extent(
1396 struct xfs_trans *tp,
1397 struct xfs_inode *ip,
1398 int whichfork,
1399 struct xfs_bmbt_irec *rec,
1400 int *is_empty)
1401{
1402 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001403 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001404 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001405
1406 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1407 error = xfs_iread_extents(tp, ip, whichfork);
1408 if (error)
1409 return error;
1410 }
1411
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001412 xfs_iext_last(ifp, &icur);
1413 if (!xfs_iext_get_extent(ifp, &icur, rec))
Dave Chinner9e5987a72013-02-25 12:31:26 +11001414 *is_empty = 1;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001415 else
1416 *is_empty = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001417 return 0;
1418}
1419
1420/*
1421 * Check the last inode extent to determine whether this allocation will result
1422 * in blocks being allocated at the end of the file. When we allocate new data
1423 * blocks at the end of the file which do not start at the previous data block,
1424 * we will try to align the new blocks at stripe unit boundaries.
1425 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001426 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a72013-02-25 12:31:26 +11001427 * at, or past the EOF.
1428 */
1429STATIC int
1430xfs_bmap_isaeof(
1431 struct xfs_bmalloca *bma,
1432 int whichfork)
1433{
1434 struct xfs_bmbt_irec rec;
1435 int is_empty;
1436 int error;
1437
Thomas Meyer749f24f2017-10-09 11:38:54 -07001438 bma->aeof = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001439 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1440 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001441 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001442 return error;
1443
Dave Chinner6e708bc2013-11-22 10:41:16 +11001444 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001445 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001446 return 0;
1447 }
1448
Dave Chinner9e5987a72013-02-25 12:31:26 +11001449 /*
1450 * Check if we are allocation or past the last extent, or at least into
1451 * the last delayed allocated extent.
1452 */
1453 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1454 (bma->offset >= rec.br_startoff &&
1455 isnullstartblock(rec.br_startblock));
1456 return 0;
1457}
1458
1459/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001460 * Returns the file-relative block number of the first block past eof in
1461 * the file. This is not based on i_size, it is based on the extent records.
1462 * Returns 0 for local files, as they do not have extent records.
1463 */
1464int
1465xfs_bmap_last_offset(
Dave Chinner9e5987a72013-02-25 12:31:26 +11001466 struct xfs_inode *ip,
1467 xfs_fileoff_t *last_block,
1468 int whichfork)
1469{
1470 struct xfs_bmbt_irec rec;
1471 int is_empty;
1472 int error;
1473
1474 *last_block = 0;
1475
1476 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1477 return 0;
1478
1479 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1480 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001481 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001482
1483 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1484 if (error || is_empty)
1485 return error;
1486
1487 *last_block = rec.br_startoff + rec.br_blockcount;
1488 return 0;
1489}
1490
1491/*
1492 * Returns whether the selected fork of the inode has exactly one
1493 * block or not. For the data fork we check this matches di_size,
1494 * implying the file's range is 0..bsize-1.
1495 */
1496int /* 1=>1 block, 0=>otherwise */
1497xfs_bmap_one_block(
1498 xfs_inode_t *ip, /* incore inode */
1499 int whichfork) /* data or attr fork */
1500{
Dave Chinner9e5987a72013-02-25 12:31:26 +11001501 xfs_ifork_t *ifp; /* inode fork pointer */
1502 int rval; /* return value */
1503 xfs_bmbt_irec_t s; /* internal version of extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001504 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001505
1506#ifndef DEBUG
1507 if (whichfork == XFS_DATA_FORK)
1508 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1509#endif /* !DEBUG */
1510 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1511 return 0;
1512 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1513 return 0;
1514 ifp = XFS_IFORK_PTR(ip, whichfork);
1515 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001516 xfs_iext_first(ifp, &icur);
1517 xfs_iext_get_extent(ifp, &icur, &s);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001518 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1519 if (rval && whichfork == XFS_DATA_FORK)
1520 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1521 return rval;
1522}
1523
1524/*
1525 * Extent tree manipulation functions used during allocation.
1526 */
1527
1528/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001529 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 */
1531STATIC int /* error */
1532xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001533 struct xfs_bmalloca *bma,
1534 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001536 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001539 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 xfs_fileoff_t new_endoff; /* end offset of new entry */
1541 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1542 /* left is 0, right is 1, prev is 2 */
1543 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07001544 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001545 xfs_filblks_t da_new; /* new count del alloc blocks used */
1546 xfs_filblks_t da_old; /* old count del alloc blocks used */
1547 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001549 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001550 xfs_extnum_t *nextents;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001551 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001553 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001554 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001555 ASSERT(whichfork != XFS_ATTR_FORK);
1556 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1557 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001558
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001559 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001560 ASSERT(!bma->cur ||
1561 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001562
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001563 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565#define LEFT r[0]
1566#define RIGHT r[1]
1567#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 /*
1570 * Set up a bunch of variables to make the tests simpler.
1571 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001572 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001574 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 ASSERT(PREV.br_startoff <= new->br_startoff);
1576 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001577
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001578 da_old = startblockval(PREV.br_startblock);
1579 da_new = 0;
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 /*
1582 * Set flags determining what part of the previous delayed allocation
1583 * extent is being replaced by a real allocation.
1584 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001585 if (PREV.br_startoff == new->br_startoff)
1586 state |= BMAP_LEFT_FILLING;
1587 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1588 state |= BMAP_RIGHT_FILLING;
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 /*
1591 * Check and set flags if this segment has a left neighbor.
1592 * Don't set contiguous if the combined extent would be too large.
1593 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001594 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001595 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001596 if (isnullstartblock(LEFT.br_startblock))
1597 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001599
1600 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1601 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1602 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1603 LEFT.br_state == new->br_state &&
1604 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1605 state |= BMAP_LEFT_CONTIG;
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 /*
1608 * Check and set flags if this segment has a right neighbor.
1609 * Don't set contiguous if the combined extent would be too large.
1610 * Also check for all-three-contiguous being too large.
1611 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001612 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001613 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001614 if (isnullstartblock(RIGHT.br_startblock))
1615 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001617
1618 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1619 new_endoff == RIGHT.br_startoff &&
1620 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1621 new->br_state == RIGHT.br_state &&
1622 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1623 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1624 BMAP_RIGHT_FILLING)) !=
1625 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1626 BMAP_RIGHT_FILLING) ||
1627 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1628 <= MAXEXTLEN))
1629 state |= BMAP_RIGHT_CONTIG;
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 error = 0;
1632 /*
1633 * Switch out based on the FILLING and CONTIG state bits.
1634 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001635 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1636 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1637 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1638 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 /*
1640 * Filling in all of a previously delayed allocation extent.
1641 * The left and right neighbors are both contiguous with new.
1642 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001643 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001644
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001645 xfs_iext_remove(bma->ip, &bma->icur, state);
1646 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001647 xfs_iext_prev(ifp, &bma->icur);
1648 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001649 (*nextents)--;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001650
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001651 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1653 else {
1654 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001655 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001656 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001658 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001659 error = xfs_btree_delete(bma->cur, &i);
1660 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001662 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001663 error = xfs_btree_decrement(bma->cur, 0, &i);
1664 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001666 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001667 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001668 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 goto done;
1670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 break;
1672
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001673 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 /*
1675 * Filling in all of a previously delayed allocation extent.
1676 * The left neighbor is contiguous, the right is not.
1677 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001678 old = LEFT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001679 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001680
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001681 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001682 xfs_iext_prev(ifp, &bma->icur);
1683 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001684
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001685 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 rval = XFS_ILOG_DEXT;
1687 else {
1688 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001689 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001690 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001692 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001693 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001694 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 goto done;
1696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 break;
1698
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001699 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 /*
1701 * Filling in all of a previously delayed allocation extent.
1702 * The right neighbor is contiguous, the left is not.
1703 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001704 PREV.br_startblock = new->br_startblock;
1705 PREV.br_blockcount += RIGHT.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001706
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001707 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001708 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001709 xfs_iext_prev(ifp, &bma->icur);
1710 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001711
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001712 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 rval = XFS_ILOG_DEXT;
1714 else {
1715 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001716 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001717 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001719 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001720 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001721 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 goto done;
1723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 break;
1725
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001726 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 /*
1728 * Filling in all of a previously delayed allocation extent.
1729 * Neither the left nor right neighbors are contiguous with
1730 * the new one.
1731 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001732 PREV.br_startblock = new->br_startblock;
1733 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001734 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001735
Darrick J. Wong60b49842016-10-03 09:11:34 -07001736 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001737 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1739 else {
1740 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001741 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001742 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001744 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001745 error = xfs_btree_insert(bma->cur, &i);
1746 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001748 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 break;
1751
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001752 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 /*
1754 * Filling in the first part of a previous delayed allocation.
1755 * The left neighbor is contiguous.
1756 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001757 old = LEFT;
1758 temp = PREV.br_blockcount - new->br_blockcount;
1759 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1760 startblockval(PREV.br_startblock));
1761
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001762 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001763
Christoph Hellwigbf999712017-11-03 10:34:38 -07001764 PREV.br_blockcount = temp;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001765 PREV.br_startoff += new->br_blockcount;
1766 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwig0d045542017-11-03 10:34:39 -07001767
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001768 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1769 xfs_iext_prev(ifp, &bma->icur);
1770 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001771
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001772 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 rval = XFS_ILOG_DEXT;
1774 else {
1775 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001776 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001777 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001779 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001780 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001781 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 goto done;
1783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 break;
1785
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001786 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 /*
1788 * Filling in the first part of a previous delayed allocation.
1789 * The left neighbor is not contiguous.
1790 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001791 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001792 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001793 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1795 else {
1796 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001797 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001798 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001800 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001801 error = xfs_btree_insert(bma->cur, &i);
1802 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001804 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001806
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001807 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001808 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster3e3673e2018-07-11 22:26:14 -07001809 bma->firstblock, bma->tp->t_dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001810 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 rval |= tmp_rval;
1812 if (error)
1813 goto done;
1814 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001815
1816 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001817 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001818 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001819 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001820
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001821 PREV.br_startoff = new_endoff;
1822 PREV.br_blockcount = temp;
1823 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001824 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001825 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001826 xfs_iext_prev(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 break;
1828
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001829 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 /*
1831 * Filling in the last part of a previous delayed allocation.
1832 * The right neighbor is contiguous with the new allocation.
1833 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001834 old = RIGHT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001835 RIGHT.br_startoff = new->br_startoff;
1836 RIGHT.br_startblock = new->br_startblock;
1837 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001838
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001839 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 rval = XFS_ILOG_DEXT;
1841 else {
1842 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001843 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001844 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001846 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001847 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001848 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 goto done;
1850 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001851
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001852 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001853 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001854 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001855
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001856 PREV.br_blockcount = temp;
1857 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001858
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001859 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1860 xfs_iext_next(ifp, &bma->icur);
1861 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 break;
1863
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001864 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 /*
1866 * Filling in the last part of a previous delayed allocation.
1867 * The right neighbor is not contiguous.
1868 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001869 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001870 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001871 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1873 else {
1874 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001875 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001876 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001878 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001879 error = xfs_btree_insert(bma->cur, &i);
1880 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001882 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001884
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001885 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001886 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster3e3673e2018-07-11 22:26:14 -07001887 bma->firstblock, bma->tp->t_dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001888 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 rval |= tmp_rval;
1890 if (error)
1891 goto done;
1892 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001893
1894 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001895 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001896 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001897 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001898
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001899 PREV.br_startblock = nullstartblock(da_new);
1900 PREV.br_blockcount = temp;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001901 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001902 xfs_iext_next(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 break;
1904
1905 case 0:
1906 /*
1907 * Filling in the middle part of a previous delayed allocation.
1908 * Contiguity is impossible here.
1909 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001910 *
1911 * We start with a delayed allocation:
1912 *
1913 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1914 * PREV @ idx
1915 *
1916 * and we are allocating:
1917 * +rrrrrrrrrrrrrrrrr+
1918 * new
1919 *
1920 * and we set it up for insertion as:
1921 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1922 * new
1923 * PREV @ idx LEFT RIGHT
1924 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001926 old = PREV;
1927
1928 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001929 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001930
1931 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001932 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001933 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001934 RIGHT.br_blockcount =
1935 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1936 RIGHT.br_startblock =
1937 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1938 RIGHT.br_blockcount));
1939
1940 /* truncate PREV */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001941 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1942 PREV.br_startblock =
1943 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1944 PREV.br_blockcount));
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001945 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001946
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001947 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001948 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1949 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001950 (*nextents)++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001951
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001952 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1954 else {
1955 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001956 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001957 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001959 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001960 error = xfs_btree_insert(bma->cur, &i);
1961 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001963 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001965
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001966 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001967 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster3e3673e2018-07-11 22:26:14 -07001968 bma->firstblock, bma->tp->t_dfops,
1969 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 rval |= tmp_rval;
1971 if (error)
1972 goto done;
1973 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001974
1975 da_new = startblockval(PREV.br_startblock) +
1976 startblockval(RIGHT.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 break;
1978
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001979 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1980 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1981 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1982 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1983 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1984 case BMAP_LEFT_CONTIG:
1985 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 /*
1987 * These cases are all impossible.
1988 */
1989 ASSERT(0);
1990 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001991
Darrick J. Wong95eb3082018-05-09 10:02:32 -07001992 /* add reverse mapping unless caller opted out */
1993 if (!(bma->flags & XFS_BMAPI_NORMAP)) {
Brian Foster3e3673e2018-07-11 22:26:14 -07001994 error = xfs_rmap_map_extent(mp, bma->tp->t_dfops, bma->ip,
Darrick J. Wong95eb3082018-05-09 10:02:32 -07001995 whichfork, new);
1996 if (error)
1997 goto done;
1998 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10001999
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002000 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002001 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002002 int tmp_logflags; /* partial log flag return val */
2003
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002004 ASSERT(bma->cur == NULL);
2005 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Brian Foster3e3673e2018-07-11 22:26:14 -07002006 bma->firstblock, bma->tp->t_dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002007 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002008 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002009 if (error)
2010 goto done;
2011 }
2012
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002013 if (bma->cur) {
2014 da_new += bma->cur->bc_private.b.allocated;
2015 bma->cur->bc_private.b.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002016 }
2017
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002018 /* adjust for changes in reserved delayed indirect blocks */
2019 if (da_new != da_old) {
2020 ASSERT(state == 0 || da_new < da_old);
2021 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2022 false);
2023 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002024
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002025 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002027 if (whichfork != XFS_COW_FORK)
2028 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 return error;
2030#undef LEFT
2031#undef RIGHT
2032#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033}
2034
2035/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002036 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 */
2038STATIC int /* error */
2039xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002040 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002042 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002043 struct xfs_iext_cursor *icur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002045 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002046 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002047 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002048 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002053 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2056 /* left is 0, right is 1, prev is 2 */
2057 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002058 int state = xfs_bmap_fork_to_state(whichfork);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002059 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002060 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002062 *logflagsp = 0;
2063
2064 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002065 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002066
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002067 ASSERT(!isnullstartblock(new->br_startblock));
2068
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002069 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071#define LEFT r[0]
2072#define RIGHT r[1]
2073#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 /*
2076 * Set up a bunch of variables to make the tests simpler.
2077 */
2078 error = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002079 xfs_iext_get_extent(ifp, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002080 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 new_endoff = new->br_startoff + new->br_blockcount;
2082 ASSERT(PREV.br_startoff <= new->br_startoff);
2083 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 /*
2086 * Set flags determining what part of the previous oldext allocation
2087 * extent is being replaced by a newext allocation.
2088 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002089 if (PREV.br_startoff == new->br_startoff)
2090 state |= BMAP_LEFT_FILLING;
2091 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2092 state |= BMAP_RIGHT_FILLING;
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 /*
2095 * Check and set flags if this segment has a left neighbor.
2096 * Don't set contiguous if the combined extent would be too large.
2097 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002098 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002099 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002100 if (isnullstartblock(LEFT.br_startblock))
2101 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002103
2104 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2105 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2106 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002107 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002108 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2109 state |= BMAP_LEFT_CONTIG;
2110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 /*
2112 * Check and set flags if this segment has a right neighbor.
2113 * Don't set contiguous if the combined extent would be too large.
2114 * Also check for all-three-contiguous being too large.
2115 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002116 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002117 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002118 if (isnullstartblock(RIGHT.br_startblock))
2119 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002121
2122 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2123 new_endoff == RIGHT.br_startoff &&
2124 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002125 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002126 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2127 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2128 BMAP_RIGHT_FILLING)) !=
2129 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2130 BMAP_RIGHT_FILLING) ||
2131 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2132 <= MAXEXTLEN))
2133 state |= BMAP_RIGHT_CONTIG;
2134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 /*
2136 * Switch out based on the FILLING and CONTIG state bits.
2137 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002138 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2139 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2140 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2141 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 /*
2143 * Setting all of a previous oldext extent to newext.
2144 * The left and right neighbors are both contiguous with new.
2145 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002146 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002147
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002148 xfs_iext_remove(ip, icur, state);
2149 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002150 xfs_iext_prev(ifp, icur);
2151 xfs_iext_update_extent(ip, state, icur, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002152 XFS_IFORK_NEXT_SET(ip, whichfork,
2153 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 if (cur == NULL)
2155 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2156 else {
2157 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002158 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2159 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002161 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002162 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002164 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002165 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002167 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002168 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002170 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002171 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002173 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002174 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002175 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 goto done;
2177 }
2178 break;
2179
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002180 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 /*
2182 * Setting all of a previous oldext extent to newext.
2183 * The left neighbor is contiguous, the right is not.
2184 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002185 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002186
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002187 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002188 xfs_iext_prev(ifp, icur);
2189 xfs_iext_update_extent(ip, state, icur, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002190 XFS_IFORK_NEXT_SET(ip, whichfork,
2191 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 if (cur == NULL)
2193 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2194 else {
2195 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002196 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2197 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002199 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002200 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002202 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002203 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002205 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002206 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002207 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 goto done;
2209 }
2210 break;
2211
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002212 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 /*
2214 * Setting all of a previous oldext extent to newext.
2215 * The right neighbor is contiguous, the left is not.
2216 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002217 PREV.br_blockcount += RIGHT.br_blockcount;
2218 PREV.br_state = new->br_state;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002219
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002220 xfs_iext_next(ifp, icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002221 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002222 xfs_iext_prev(ifp, icur);
2223 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002224
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002225 XFS_IFORK_NEXT_SET(ip, whichfork,
2226 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (cur == NULL)
2228 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2229 else {
2230 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002231 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2232 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002234 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002235 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002237 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002238 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002240 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002241 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002242 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 goto done;
2244 }
2245 break;
2246
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002247 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 /*
2249 * Setting all of a previous oldext extent to newext.
2250 * Neither the left nor right neighbors are contiguous with
2251 * the new one.
2252 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002253 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002254 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (cur == NULL)
2257 rval = XFS_ILOG_DEXT;
2258 else {
2259 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002260 error = xfs_bmbt_lookup_eq(cur, new, &i);
2261 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002263 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002264 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002265 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 goto done;
2267 }
2268 break;
2269
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002270 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 /*
2272 * Setting the first part of a previous oldext extent to newext.
2273 * The left neighbor is contiguous.
2274 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002275 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002276
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002277 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002278 PREV.br_startoff += new->br_blockcount;
2279 PREV.br_startblock += new->br_blockcount;
2280 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002281
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002282 xfs_iext_update_extent(ip, state, icur, &PREV);
2283 xfs_iext_prev(ifp, icur);
2284 xfs_iext_update_extent(ip, state, icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 if (cur == NULL)
2287 rval = XFS_ILOG_DEXT;
2288 else {
2289 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002290 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002291 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002293 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002294 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002295 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002297 error = xfs_btree_decrement(cur, 0, &i);
2298 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002300 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002301 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 goto done;
2303 }
2304 break;
2305
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002306 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 /*
2308 * Setting the first part of a previous oldext extent to newext.
2309 * The left neighbor is not contiguous.
2310 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002311 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002312 PREV.br_startoff += new->br_blockcount;
2313 PREV.br_startblock += new->br_blockcount;
2314 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002315
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002316 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002317 xfs_iext_insert(ip, icur, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002318 XFS_IFORK_NEXT_SET(ip, whichfork,
2319 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 if (cur == NULL)
2321 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2322 else {
2323 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002324 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002325 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002327 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002328 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002329 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 goto done;
2331 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002332 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002334 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
2336 break;
2337
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002338 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 /*
2340 * Setting the last part of a previous oldext extent to newext.
2341 * The right neighbor is contiguous with the new allocation.
2342 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002343 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002344 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002345
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002346 RIGHT.br_startoff = new->br_startoff;
2347 RIGHT.br_startblock = new->br_startblock;
2348 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002349
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002350 xfs_iext_update_extent(ip, state, icur, &PREV);
2351 xfs_iext_next(ifp, icur);
2352 xfs_iext_update_extent(ip, state, icur, &RIGHT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (cur == NULL)
2355 rval = XFS_ILOG_DEXT;
2356 else {
2357 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002358 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002359 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002361 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002362 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002363 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002365 error = xfs_btree_increment(cur, 0, &i);
2366 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002368 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002369 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 goto done;
2371 }
2372 break;
2373
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002374 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 /*
2376 * Setting the last part of a previous oldext extent to newext.
2377 * The right neighbor is not contiguous.
2378 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002379 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002380 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002381
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002382 xfs_iext_update_extent(ip, state, icur, &PREV);
2383 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002384 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002385
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002386 XFS_IFORK_NEXT_SET(ip, whichfork,
2387 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if (cur == NULL)
2389 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2390 else {
2391 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002392 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002393 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002395 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002396 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002397 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002399 error = xfs_bmbt_lookup_eq(cur, new, &i);
2400 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002402 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002403 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002405 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
2407 break;
2408
2409 case 0:
2410 /*
2411 * Setting the middle part of a previous oldext extent to
2412 * newext. Contiguity is impossible here.
2413 * One extent becomes three extents.
2414 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002415 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002416 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 r[0] = *new;
2419 r[1].br_startoff = new_endoff;
2420 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002421 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002423 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002424
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002425 xfs_iext_update_extent(ip, state, icur, &PREV);
2426 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002427 xfs_iext_insert(ip, icur, &r[1], state);
2428 xfs_iext_insert(ip, icur, &r[0], state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002429
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002430 XFS_IFORK_NEXT_SET(ip, whichfork,
2431 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 if (cur == NULL)
2433 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2434 else {
2435 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002436 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002437 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002439 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002441 error = xfs_bmbt_update(cur, &r[1]);
2442 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 goto done;
2444 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002446 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002448 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002449 /*
2450 * Reset the cursor to the position of the new extent
2451 * we are about to insert as we can't trust it after
2452 * the previous insert.
2453 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002454 error = xfs_bmbt_lookup_eq(cur, new, &i);
2455 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002457 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002459 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002461 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 }
2463 break;
2464
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002465 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2466 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2467 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2468 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2469 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2470 case BMAP_LEFT_CONTIG:
2471 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 /*
2473 * These cases are all impossible.
2474 */
2475 ASSERT(0);
2476 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002477
Darrick J. Wong9c194642016-08-03 12:16:05 +10002478 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002479 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002480 if (error)
2481 goto done;
2482
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002483 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002484 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002485 int tmp_logflags; /* partial log flag return val */
2486
2487 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002488 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002489 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002490 *logflagsp |= tmp_logflags;
2491 if (error)
2492 goto done;
2493 }
2494
2495 /* clear out the allocated field, done with it now in any case. */
2496 if (cur) {
2497 cur->bc_private.b.allocated = 0;
2498 *curp = cur;
2499 }
2500
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002501 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002503 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 return error;
2505#undef LEFT
2506#undef RIGHT
2507#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
2510/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002511 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002513STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514xfs_bmap_add_extent_hole_delay(
2515 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002516 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002517 struct xfs_iext_cursor *icur,
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002518 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002520 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2522 xfs_filblks_t newlen=0; /* new indirect size */
2523 xfs_filblks_t oldlen=0; /* old indirect size */
2524 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002525 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002526 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002528 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002529 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 /*
2532 * Check and set flags if this segment has a left neighbor
2533 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002534 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002535 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002536 if (isnullstartblock(left.br_startblock))
2537 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 /*
2541 * Check and set flags if the current (right) segment exists.
2542 * If it doesn't exist, we're converting the hole at end-of-file.
2543 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002544 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002545 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002546 if (isnullstartblock(right.br_startblock))
2547 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 /*
2551 * Set contiguity flags on the left and right neighbors.
2552 * Don't let extents get too large, even if the pieces are contiguous.
2553 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002554 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2555 left.br_startoff + left.br_blockcount == new->br_startoff &&
2556 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2557 state |= BMAP_LEFT_CONTIG;
2558
2559 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2560 new->br_startoff + new->br_blockcount == right.br_startoff &&
2561 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2562 (!(state & BMAP_LEFT_CONTIG) ||
2563 (left.br_blockcount + new->br_blockcount +
2564 right.br_blockcount <= MAXEXTLEN)))
2565 state |= BMAP_RIGHT_CONTIG;
2566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 /*
2568 * Switch out based on the contiguity flags.
2569 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002570 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2571 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 /*
2573 * New allocation is contiguous with delayed allocations
2574 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002575 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 */
2577 temp = left.br_blockcount + new->br_blockcount +
2578 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002579
Eric Sandeen9d87c312009-01-14 23:22:07 -06002580 oldlen = startblockval(left.br_startblock) +
2581 startblockval(new->br_startblock) +
2582 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002583 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2584 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002585 left.br_startblock = nullstartblock(newlen);
2586 left.br_blockcount = temp;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002587
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002588 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002589 xfs_iext_prev(ifp, icur);
2590 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 break;
2592
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002593 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 /*
2595 * New allocation is contiguous with a delayed allocation
2596 * on the left.
2597 * Merge the new allocation with the left neighbor.
2598 */
2599 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002600
Eric Sandeen9d87c312009-01-14 23:22:07 -06002601 oldlen = startblockval(left.br_startblock) +
2602 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002603 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2604 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002605 left.br_blockcount = temp;
2606 left.br_startblock = nullstartblock(newlen);
Christoph Hellwig41d196f2017-11-03 10:34:39 -07002607
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002608 xfs_iext_prev(ifp, icur);
2609 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 break;
2611
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002612 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 /*
2614 * New allocation is contiguous with a delayed allocation
2615 * on the right.
2616 * Merge the new allocation with the right neighbor.
2617 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002619 oldlen = startblockval(new->br_startblock) +
2620 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002621 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2622 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002623 right.br_startoff = new->br_startoff;
2624 right.br_startblock = nullstartblock(newlen);
2625 right.br_blockcount = temp;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002626 xfs_iext_update_extent(ip, state, icur, &right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 break;
2628
2629 case 0:
2630 /*
2631 * New allocation is not contiguous with another
2632 * delayed allocation.
2633 * Insert a new entry.
2634 */
2635 oldlen = newlen = 0;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002636 xfs_iext_insert(ip, icur, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 break;
2638 }
2639 if (oldlen != newlen) {
2640 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002641 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2642 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 /*
2644 * Nothing to do for disk quota accounting here.
2645 */
2646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647}
2648
2649/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002650 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 */
2652STATIC int /* error */
2653xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002654 struct xfs_trans *tp,
2655 struct xfs_inode *ip,
2656 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002657 struct xfs_iext_cursor *icur,
Christoph Hellwig6d045582017-04-11 16:45:54 -07002658 struct xfs_btree_cur **curp,
2659 struct xfs_bmbt_irec *new,
2660 xfs_fsblock_t *first,
2661 struct xfs_defer_ops *dfops,
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002662 int *logflagsp,
2663 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002665 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2666 struct xfs_mount *mp = ip->i_mount;
2667 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2671 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002672 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002673 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002674 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002676 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002677 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002678
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002679 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 /*
2682 * Check and set flags if this segment has a left neighbor.
2683 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002684 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002685 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002686 if (isnullstartblock(left.br_startblock))
2687 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 /*
2691 * Check and set flags if this segment has a current value.
2692 * Not true if we're inserting into the "hole" at eof.
2693 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002694 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002695 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002696 if (isnullstartblock(right.br_startblock))
2697 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002699
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 /*
2701 * We're inserting a real allocation between "left" and "right".
2702 * Set the contiguity flags. Don't let extents get too large.
2703 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002704 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2705 left.br_startoff + left.br_blockcount == new->br_startoff &&
2706 left.br_startblock + left.br_blockcount == new->br_startblock &&
2707 left.br_state == new->br_state &&
2708 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2709 state |= BMAP_LEFT_CONTIG;
2710
2711 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2712 new->br_startoff + new->br_blockcount == right.br_startoff &&
2713 new->br_startblock + new->br_blockcount == right.br_startblock &&
2714 new->br_state == right.br_state &&
2715 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2716 (!(state & BMAP_LEFT_CONTIG) ||
2717 left.br_blockcount + new->br_blockcount +
2718 right.br_blockcount <= MAXEXTLEN))
2719 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002721 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 /*
2723 * Select which case we're in here, and implement it.
2724 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002725 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2726 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 /*
2728 * New allocation is contiguous with real allocations on the
2729 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002730 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002732 left.br_blockcount += new->br_blockcount + right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002733
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002734 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002735 xfs_iext_prev(ifp, icur);
2736 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002737
Christoph Hellwig6d045582017-04-11 16:45:54 -07002738 XFS_IFORK_NEXT_SET(ip, whichfork,
2739 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2740 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002741 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002742 } else {
2743 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002744 error = xfs_bmbt_lookup_eq(cur, &right, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002745 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002746 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002747 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002748 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002749 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002750 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002751 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002752 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002753 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002754 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002755 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002756 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002757 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002758 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002760 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002762 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 /*
2764 * New allocation is contiguous with a real allocation
2765 * on the left.
2766 * Merge the new allocation with the left neighbor.
2767 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002768 old = left;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002769 left.br_blockcount += new->br_blockcount;
Christoph Hellwig1d2e0082017-11-03 10:34:40 -07002770
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002771 xfs_iext_prev(ifp, icur);
2772 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002773
Christoph Hellwig6d045582017-04-11 16:45:54 -07002774 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002775 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002776 } else {
2777 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002778 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002779 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002780 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002781 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002782 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002783 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002784 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002786 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002788 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 /*
2790 * New allocation is contiguous with a real allocation
2791 * on the right.
2792 * Merge the new allocation with the right neighbor.
2793 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002794 old = right;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07002795
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002796 right.br_startoff = new->br_startoff;
2797 right.br_startblock = new->br_startblock;
2798 right.br_blockcount += new->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002799 xfs_iext_update_extent(ip, state, icur, &right);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002800
Christoph Hellwig6d045582017-04-11 16:45:54 -07002801 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002802 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002803 } else {
2804 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002805 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002806 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002807 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002808 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002809 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002810 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002811 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002813 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
2815 case 0:
2816 /*
2817 * New allocation is not contiguous with another
2818 * real allocation.
2819 * Insert a new entry.
2820 */
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002821 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002822 XFS_IFORK_NEXT_SET(ip, whichfork,
2823 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2824 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002825 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002826 } else {
2827 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002828 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002829 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002830 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002831 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002832 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002833 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002834 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002835 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002837 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002839
Darrick J. Wong95eb3082018-05-09 10:02:32 -07002840 /* add reverse mapping unless caller opted out */
2841 if (!(flags & XFS_BMAPI_NORMAP)) {
2842 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
2843 if (error)
2844 goto done;
2845 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10002846
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002847 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002848 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002849 int tmp_logflags; /* partial log flag return val */
2850
Christoph Hellwig6d045582017-04-11 16:45:54 -07002851 ASSERT(cur == NULL);
2852 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002853 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002854 *logflagsp |= tmp_logflags;
2855 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002856 if (error)
2857 goto done;
2858 }
2859
2860 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002861 if (cur)
2862 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002863
Christoph Hellwig6d045582017-04-11 16:45:54 -07002864 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002865done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002866 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002867 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868}
2869
Nathan Scottdd9f4382006-01-11 15:28:28 +11002870/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11002871 * Functions used in the extent read, allocate and remove paths
2872 */
2873
2874/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002875 * Adjust the size of the new extent based on di_extsize and rt extsize.
2876 */
Dave Chinner68988112013-08-12 20:49:42 +10002877int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002878xfs_bmap_extsize_align(
2879 xfs_mount_t *mp,
2880 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2881 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2882 xfs_extlen_t extsz, /* align to this extent size */
2883 int rt, /* is this a realtime inode? */
2884 int eof, /* is extent at end-of-file? */
2885 int delay, /* creating delalloc extent? */
2886 int convert, /* overwriting unwritten extent? */
2887 xfs_fileoff_t *offp, /* in/out: aligned offset */
2888 xfs_extlen_t *lenp) /* in/out: aligned length */
2889{
2890 xfs_fileoff_t orig_off; /* original offset */
2891 xfs_extlen_t orig_alen; /* original length */
2892 xfs_fileoff_t orig_end; /* original off+len */
2893 xfs_fileoff_t nexto; /* next file offset */
2894 xfs_fileoff_t prevo; /* previous file offset */
2895 xfs_fileoff_t align_off; /* temp for offset */
2896 xfs_extlen_t align_alen; /* temp for length */
2897 xfs_extlen_t temp; /* temp for calculations */
2898
2899 if (convert)
2900 return 0;
2901
2902 orig_off = align_off = *offp;
2903 orig_alen = align_alen = *lenp;
2904 orig_end = orig_off + orig_alen;
2905
2906 /*
2907 * If this request overlaps an existing extent, then don't
2908 * attempt to perform any additional alignment.
2909 */
2910 if (!delay && !eof &&
2911 (orig_off >= gotp->br_startoff) &&
2912 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2913 return 0;
2914 }
2915
2916 /*
2917 * If the file offset is unaligned vs. the extent size
2918 * we need to align it. This will be possible unless
2919 * the file was previously written with a kernel that didn't
2920 * perform this alignment, or if a truncate shot us in the
2921 * foot.
2922 */
Dave Chinner0703a8e2018-06-08 09:54:22 -07002923 div_u64_rem(orig_off, extsz, &temp);
Nathan Scottdd9f4382006-01-11 15:28:28 +11002924 if (temp) {
2925 align_alen += temp;
2926 align_off -= temp;
2927 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10002928
2929 /* Same adjustment for the end of the requested area. */
2930 temp = (align_alen % extsz);
2931 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11002932 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10002933
2934 /*
2935 * For large extent hint sizes, the aligned extent might be larger than
2936 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2937 * the length back under MAXEXTLEN. The outer allocation loops handle
2938 * short allocation just fine, so it is safe to do this. We only want to
2939 * do it when we are forced to, though, because it means more allocation
2940 * operations are required.
2941 */
2942 while (align_alen > MAXEXTLEN)
2943 align_alen -= extsz;
2944 ASSERT(align_alen <= MAXEXTLEN);
2945
Nathan Scottdd9f4382006-01-11 15:28:28 +11002946 /*
2947 * If the previous block overlaps with this proposed allocation
2948 * then move the start forward without adjusting the length.
2949 */
2950 if (prevp->br_startoff != NULLFILEOFF) {
2951 if (prevp->br_startblock == HOLESTARTBLOCK)
2952 prevo = prevp->br_startoff;
2953 else
2954 prevo = prevp->br_startoff + prevp->br_blockcount;
2955 } else
2956 prevo = 0;
2957 if (align_off != orig_off && align_off < prevo)
2958 align_off = prevo;
2959 /*
2960 * If the next block overlaps with this proposed allocation
2961 * then move the start back without adjusting the length,
2962 * but not before offset 0.
2963 * This may of course make the start overlap previous block,
2964 * and if we hit the offset 0 limit then the next block
2965 * can still overlap too.
2966 */
2967 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2968 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2969 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2970 nexto = gotp->br_startoff + gotp->br_blockcount;
2971 else
2972 nexto = gotp->br_startoff;
2973 } else
2974 nexto = NULLFILEOFF;
2975 if (!eof &&
2976 align_off + align_alen != orig_end &&
2977 align_off + align_alen > nexto)
2978 align_off = nexto > align_alen ? nexto - align_alen : 0;
2979 /*
2980 * If we're now overlapping the next or previous extent that
2981 * means we can't fit an extsz piece in this hole. Just move
2982 * the start forward to the first valid spot and set
2983 * the length so we hit the end.
2984 */
2985 if (align_off != orig_off && align_off < prevo)
2986 align_off = prevo;
2987 if (align_off + align_alen != orig_end &&
2988 align_off + align_alen > nexto &&
2989 nexto != NULLFILEOFF) {
2990 ASSERT(nexto > prevo);
2991 align_alen = nexto - align_off;
2992 }
2993
2994 /*
2995 * If realtime, and the result isn't a multiple of the realtime
2996 * extent size we need to remove blocks until it is.
2997 */
2998 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2999 /*
3000 * We're not covering the original request, or
3001 * we won't be able to once we fix the length.
3002 */
3003 if (orig_off < align_off ||
3004 orig_end > align_off + align_alen ||
3005 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003006 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003007 /*
3008 * Try to fix it by moving the start up.
3009 */
3010 if (align_off + temp <= orig_off) {
3011 align_alen -= temp;
3012 align_off += temp;
3013 }
3014 /*
3015 * Try to fix it by moving the end in.
3016 */
3017 else if (align_off + align_alen - temp >= orig_end)
3018 align_alen -= temp;
3019 /*
3020 * Set the start to the minimum then trim the length.
3021 */
3022 else {
3023 align_alen -= orig_off - align_off;
3024 align_off = orig_off;
3025 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3026 }
3027 /*
3028 * Result doesn't cover the request, fail it.
3029 */
3030 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003031 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003032 } else {
3033 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003034 /* see MAXEXTLEN handling above */
3035 ASSERT(orig_end <= align_off + align_alen ||
3036 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003037 }
3038
3039#ifdef DEBUG
3040 if (!eof && gotp->br_startoff != NULLFILEOFF)
3041 ASSERT(align_off + align_alen <= gotp->br_startoff);
3042 if (prevp->br_startoff != NULLFILEOFF)
3043 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3044#endif
3045
3046 *lenp = align_alen;
3047 *offp = align_off;
3048 return 0;
3049}
3050
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051#define XFS_ALLOC_GAP_UNITS 4
3052
Dave Chinner68988112013-08-12 20:49:42 +10003053void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003054xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003055 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
3057 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3059 xfs_mount_t *mp; /* mount point structure */
3060 int nullfb; /* true if ap->firstblock isn't set */
3061 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
3063#define ISVALID(x,y) \
3064 (rt ? \
3065 (x) < mp->m_sb.sb_rblocks : \
3066 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3067 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3068 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3069
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003071 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003072 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3073 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003074 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 /*
3076 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003077 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003079 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3080 !isnullstartblock(ap->prev.br_startblock) &&
3081 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3082 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003083 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 /*
3085 * Adjust for the gap between prevp and us.
3086 */
Dave Chinner3a756672011-09-18 20:40:58 +00003087 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003088 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003090 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3091 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 }
3093 /*
3094 * If not at eof, then compare the two neighbor blocks.
3095 * Figure out whether either one gives us a good starting point,
3096 * and pick the better one.
3097 */
3098 else if (!ap->eof) {
3099 xfs_fsblock_t gotbno; /* right side block number */
3100 xfs_fsblock_t gotdiff=0; /* right side difference */
3101 xfs_fsblock_t prevbno; /* left side block number */
3102 xfs_fsblock_t prevdiff=0; /* left side difference */
3103
3104 /*
3105 * If there's a previous (left) block, select a requested
3106 * start block based on it.
3107 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003108 if (ap->prev.br_startoff != NULLFILEOFF &&
3109 !isnullstartblock(ap->prev.br_startblock) &&
3110 (prevbno = ap->prev.br_startblock +
3111 ap->prev.br_blockcount) &&
3112 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 /*
3114 * Calculate gap to end of previous block.
3115 */
Dave Chinner3a756672011-09-18 20:40:58 +00003116 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003117 (ap->prev.br_startoff +
3118 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 /*
3120 * Figure the startblock based on the previous block's
3121 * end and the gap size.
3122 * Heuristic!
3123 * If the gap is large relative to the piece we're
3124 * allocating, or using it gives us an invalid block
3125 * number, then just use the end of the previous block.
3126 */
Dave Chinner3a756672011-09-18 20:40:58 +00003127 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003129 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 prevbno += adjust;
3131 else
3132 prevdiff += adjust;
3133 /*
3134 * If the firstblock forbids it, can't use it,
3135 * must use default.
3136 */
3137 if (!rt && !nullfb &&
3138 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3139 prevbno = NULLFSBLOCK;
3140 }
3141 /*
3142 * No previous block or can't follow it, just default.
3143 */
3144 else
3145 prevbno = NULLFSBLOCK;
3146 /*
3147 * If there's a following (right) block, select a requested
3148 * start block based on it.
3149 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003150 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 /*
3152 * Calculate gap to start of next block.
3153 */
Dave Chinner3a756672011-09-18 20:40:58 +00003154 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 /*
3156 * Figure the startblock based on the next block's
3157 * start and the gap size.
3158 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003159 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 /*
3161 * Heuristic!
3162 * If the gap is large relative to the piece we're
3163 * allocating, or using it gives us an invalid block
3164 * number, then just use the start of the next block
3165 * offset by our length.
3166 */
Dave Chinner3a756672011-09-18 20:40:58 +00003167 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 ISVALID(gotbno - gotdiff, gotbno))
3169 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003170 else if (ISVALID(gotbno - ap->length, gotbno)) {
3171 gotbno -= ap->length;
3172 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 } else
3174 gotdiff += adjust;
3175 /*
3176 * If the firstblock forbids it, can't use it,
3177 * must use default.
3178 */
3179 if (!rt && !nullfb &&
3180 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3181 gotbno = NULLFSBLOCK;
3182 }
3183 /*
3184 * No next block, just default.
3185 */
3186 else
3187 gotbno = NULLFSBLOCK;
3188 /*
3189 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003190 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 */
3192 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003193 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003195 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003197 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003199#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003200}
3201
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003202static int
3203xfs_bmap_longest_free_extent(
3204 struct xfs_trans *tp,
3205 xfs_agnumber_t ag,
3206 xfs_extlen_t *blen,
3207 int *notinit)
3208{
3209 struct xfs_mount *mp = tp->t_mountp;
3210 struct xfs_perag *pag;
3211 xfs_extlen_t longest;
3212 int error = 0;
3213
3214 pag = xfs_perag_get(mp, ag);
3215 if (!pag->pagf_init) {
3216 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3217 if (error)
3218 goto out;
3219
3220 if (!pag->pagf_init) {
3221 *notinit = 1;
3222 goto out;
3223 }
3224 }
3225
Eric Sandeena1f69412018-04-06 10:09:42 -07003226 longest = xfs_alloc_longest_free_extent(pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003227 xfs_alloc_min_freelist(mp, pag),
3228 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003229 if (*blen < longest)
3230 *blen = longest;
3231
3232out:
3233 xfs_perag_put(pag);
3234 return error;
3235}
3236
3237static void
3238xfs_bmap_select_minlen(
3239 struct xfs_bmalloca *ap,
3240 struct xfs_alloc_arg *args,
3241 xfs_extlen_t *blen,
3242 int notinit)
3243{
3244 if (notinit || *blen < ap->minlen) {
3245 /*
3246 * Since we did a BUF_TRYLOCK above, it is possible that
3247 * there is space for this request.
3248 */
3249 args->minlen = ap->minlen;
3250 } else if (*blen < args->maxlen) {
3251 /*
3252 * If the best seen length is less than the request length,
3253 * use the best as the minimum.
3254 */
3255 args->minlen = *blen;
3256 } else {
3257 /*
3258 * Otherwise we've seen an extent as big as maxlen, use that
3259 * as the minimum.
3260 */
3261 args->minlen = args->maxlen;
3262 }
3263}
3264
Nathan Scotta365bdd2006-03-14 13:34:16 +11003265STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003266xfs_bmap_btalloc_nullfb(
3267 struct xfs_bmalloca *ap,
3268 struct xfs_alloc_arg *args,
3269 xfs_extlen_t *blen)
3270{
3271 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003272 xfs_agnumber_t ag, startag;
3273 int notinit = 0;
3274 int error;
3275
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003276 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003277 args->total = ap->total;
3278
Christoph Hellwigc467c042010-02-15 23:34:42 +00003279 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3280 if (startag == NULLAGNUMBER)
3281 startag = ag = 0;
3282
Dave Chinner14b064c2011-01-27 12:16:28 +11003283 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003284 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3285 &notinit);
3286 if (error)
3287 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003288
Christoph Hellwigc467c042010-02-15 23:34:42 +00003289 if (++ag == mp->m_sb.sb_agcount)
3290 ag = 0;
3291 if (ag == startag)
3292 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003293 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003294
3295 xfs_bmap_select_minlen(ap, args, blen, notinit);
3296 return 0;
3297}
3298
3299STATIC int
3300xfs_bmap_btalloc_filestreams(
3301 struct xfs_bmalloca *ap,
3302 struct xfs_alloc_arg *args,
3303 xfs_extlen_t *blen)
3304{
3305 struct xfs_mount *mp = ap->ip->i_mount;
3306 xfs_agnumber_t ag;
3307 int notinit = 0;
3308 int error;
3309
3310 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3311 args->total = ap->total;
3312
3313 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3314 if (ag == NULLAGNUMBER)
3315 ag = 0;
3316
3317 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3318 if (error)
3319 return error;
3320
3321 if (*blen < args->maxlen) {
3322 error = xfs_filestream_new_ag(ap, &ag);
3323 if (error)
3324 return error;
3325
3326 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3327 &notinit);
3328 if (error)
3329 return error;
3330
3331 }
3332
3333 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003334
3335 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003336 * Set the failure fallback case to look in the selected AG as stream
3337 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003338 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003339 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003340 return 0;
3341}
3342
Darrick J. Wong751f3762018-01-25 13:58:13 -08003343/* Update all inode and quota accounting for the allocation we just did. */
3344static void
3345xfs_bmap_btalloc_accounting(
3346 struct xfs_bmalloca *ap,
3347 struct xfs_alloc_arg *args)
3348{
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003349 if (ap->flags & XFS_BMAPI_COWFORK) {
3350 /*
3351 * COW fork blocks are in-core only and thus are treated as
3352 * in-core quota reservation (like delalloc blocks) even when
3353 * converted to real blocks. The quota reservation is not
3354 * accounted to disk until blocks are remapped to the data
3355 * fork. So if these blocks were previously delalloc, we
3356 * already have quota reservation and there's nothing to do
3357 * yet.
3358 */
3359 if (ap->wasdel)
3360 return;
3361
3362 /*
3363 * Otherwise, we've allocated blocks in a hole. The transaction
3364 * has acquired in-core quota reservation for this extent.
3365 * Rather than account these as real blocks, however, we reduce
3366 * the transaction quota reservation based on the allocation.
3367 * This essentially transfers the transaction quota reservation
3368 * to that of a delalloc extent.
3369 */
3370 ap->ip->i_delayed_blks += args->len;
3371 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3372 -(long)args->len);
3373 return;
3374 }
3375
3376 /* data/attr fork only */
3377 ap->ip->i_d.di_nblocks += args->len;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003378 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3379 if (ap->wasdel)
3380 ap->ip->i_delayed_blks -= args->len;
3381 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3382 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3383 args->len);
3384}
3385
Christoph Hellwigc467c042010-02-15 23:34:42 +00003386STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003387xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003388 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003389{
3390 xfs_mount_t *mp; /* mount point structure */
3391 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003392 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003393 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003394 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003395 xfs_alloc_arg_t args;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003396 xfs_fileoff_t orig_offset;
3397 xfs_extlen_t orig_length;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003398 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003399 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003400 int nullfb; /* true if ap->firstblock isn't set */
3401 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003402 int tryagain;
3403 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003404 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003405
Dave Chinnera99ebf42011-12-01 11:24:20 +00003406 ASSERT(ap->length);
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003407 orig_offset = ap->offset;
3408 orig_length = ap->length;
Dave Chinnera99ebf42011-12-01 11:24:20 +00003409
Nathan Scotta365bdd2006-03-14 13:34:16 +11003410 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003411
3412 /* stripe alignment for allocation is determined by mount parameters */
3413 stripe_align = 0;
3414 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3415 stripe_align = mp->m_swidth;
3416 else if (mp->m_dalign)
3417 stripe_align = mp->m_dalign;
3418
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003419 if (ap->flags & XFS_BMAPI_COWFORK)
3420 align = xfs_get_cowextsz_hint(ap->ip);
3421 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003422 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003423 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003424 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003425 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003426 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003427 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003428 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003429 }
Dave Chinner33177f052013-12-12 16:34:36 +11003430
3431
Dave Chinner0937e0f2011-09-18 20:40:57 +00003432 nullfb = *ap->firstblock == NULLFSBLOCK;
3433 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003434 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003435 if (xfs_alloc_is_userdata(ap->datatype) &&
3436 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003437 ag = xfs_filestream_lookup_ag(ap->ip);
3438 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003439 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003440 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003441 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003442 }
3443 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003444 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003445
3446 xfs_bmap_adjacent(ap);
3447
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003449 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 * it's in the right allocation group.
3451 */
Dave Chinner3a756672011-09-18 20:40:58 +00003452 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 ;
3454 else
Dave Chinner3a756672011-09-18 20:40:58 +00003455 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 * Normal allocation, done through xfs_alloc_vextent.
3458 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003459 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003460 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003461 args.tp = ap->tp;
3462 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003463 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003464 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003465
3466 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner9bb54cb2018-06-07 07:54:02 -07003467 args.maxlen = min(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003468 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003469 blen = 0;
3470 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003471 /*
3472 * Search for an allocation group with a single extent large
3473 * enough for the request. If one isn't found, then adjust
3474 * the minimum allocation size to the largest space found.
3475 */
Dave Chinner292378e2016-09-26 08:21:28 +10003476 if (xfs_alloc_is_userdata(ap->datatype) &&
3477 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003478 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3479 else
3480 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003481 if (error)
3482 return error;
Brian Foster3e3673e2018-07-11 22:26:14 -07003483 } else if (ap->tp->t_dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003484 if (xfs_inode_is_filestream(ap->ip))
3485 args.type = XFS_ALLOCTYPE_FIRST_AG;
3486 else
3487 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003488 args.total = args.minlen = ap->minlen;
3489 } else {
3490 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3491 args.total = ap->total;
3492 args.minlen = ap->minlen;
3493 }
David Chinner957d0eb2007-06-18 16:50:37 +10003494 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003495 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003496 args.prod = align;
Dave Chinner0703a8e2018-06-08 09:54:22 -07003497 div_u64_rem(ap->offset, args.prod, &args.mod);
3498 if (args.mod)
3499 args.mod = args.prod - args.mod;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003500 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003501 args.prod = 1;
3502 args.mod = 0;
3503 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003504 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner0703a8e2018-06-08 09:54:22 -07003505 div_u64_rem(ap->offset, args.prod, &args.mod);
3506 if (args.mod)
3507 args.mod = args.prod - args.mod;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003508 }
3509 /*
3510 * If we are not low on available data blocks, and the
3511 * underlying logical volume manager is a stripe, and
3512 * the file offset is zero then try to allocate data
3513 * blocks on stripe unit boundary.
3514 * NOTE: ap->aeof is only set if the allocation length
3515 * is >= the stripe unit and the allocation offset is
3516 * at the end of file.
3517 */
Brian Foster3e3673e2018-07-11 22:26:14 -07003518 if (!ap->tp->t_dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003519 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003520 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003521 atype = args.type;
3522 isaligned = 1;
3523 /*
3524 * Adjust for alignment
3525 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003526 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003527 args.minlen = blen - args.alignment;
3528 args.minalignslop = 0;
3529 } else {
3530 /*
3531 * First try an exact bno allocation.
3532 * If it fails then do a near or start bno
3533 * allocation with alignment turned on.
3534 */
3535 atype = args.type;
3536 tryagain = 1;
3537 args.type = XFS_ALLOCTYPE_THIS_BNO;
3538 args.alignment = 1;
3539 /*
3540 * Compute the minlen+alignment for the
3541 * next case. Set slop so that the value
3542 * of minlen+alignment+slop doesn't go up
3543 * between the calls.
3544 */
Dave Chinner33177f052013-12-12 16:34:36 +11003545 if (blen > stripe_align && blen <= args.maxlen)
3546 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003547 else
3548 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003549 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003550 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003551 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003552 args.minlen - 1;
3553 else
3554 args.minalignslop = 0;
3555 }
3556 } else {
3557 args.alignment = 1;
3558 args.minalignslop = 0;
3559 }
3560 args.minleft = ap->minleft;
3561 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003562 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003563 args.datatype = ap->datatype;
3564 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003565 args.ip = ap->ip;
3566
3567 error = xfs_alloc_vextent(&args);
3568 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003569 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003570
Nathan Scotta365bdd2006-03-14 13:34:16 +11003571 if (tryagain && args.fsbno == NULLFSBLOCK) {
3572 /*
3573 * Exact allocation failed. Now try with alignment
3574 * turned on.
3575 */
3576 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003577 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003578 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003579 args.minlen = nextminlen;
3580 args.minalignslop = 0;
3581 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 if ((error = xfs_alloc_vextent(&args)))
3583 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003584 }
3585 if (isaligned && args.fsbno == NULLFSBLOCK) {
3586 /*
3587 * allocation failed, so turn off alignment and
3588 * try again.
3589 */
3590 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003591 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003592 args.alignment = 0;
3593 if ((error = xfs_alloc_vextent(&args)))
3594 return error;
3595 }
3596 if (args.fsbno == NULLFSBLOCK && nullfb &&
3597 args.minlen > ap->minlen) {
3598 args.minlen = ap->minlen;
3599 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003600 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003601 if ((error = xfs_alloc_vextent(&args)))
3602 return error;
3603 }
3604 if (args.fsbno == NULLFSBLOCK && nullfb) {
3605 args.fsbno = 0;
3606 args.type = XFS_ALLOCTYPE_FIRST_AG;
3607 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003608 if ((error = xfs_alloc_vextent(&args)))
3609 return error;
Brian Foster3e3673e2018-07-11 22:26:14 -07003610 ap->tp->t_dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003611 }
3612 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003613 /*
3614 * check the allocation happened at the same or higher AG than
3615 * the first block that was allocated.
3616 */
3617 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003618 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3619 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003620
Dave Chinner3a756672011-09-18 20:40:58 +00003621 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003622 if (*ap->firstblock == NULLFSBLOCK)
3623 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003624 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003625 ap->length = args.len;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003626 /*
3627 * If the extent size hint is active, we tried to round the
3628 * caller's allocation request offset down to extsz and the
3629 * length up to another extsz boundary. If we found a free
3630 * extent we mapped it in starting at this new offset. If the
3631 * newly mapped space isn't long enough to cover any of the
3632 * range of offsets that was originally requested, move the
3633 * mapping up so that we can fill as much of the caller's
3634 * original request as possible. Free space is apparently
3635 * very fragmented so we're unlikely to be able to satisfy the
3636 * hints anyway.
3637 */
3638 if (ap->length <= orig_length)
3639 ap->offset = orig_offset;
3640 else if (ap->offset + ap->length < orig_offset + orig_length)
3641 ap->offset = orig_offset + orig_length - ap->length;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003642 xfs_bmap_btalloc_accounting(ap, &args);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003643 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003644 ap->blkno = NULLFSBLOCK;
3645 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 }
3647 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003648}
3649
3650/*
3651 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3652 * It figures out where to ask the underlying allocator to put the new extent.
3653 */
3654STATIC int
3655xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003656 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003657{
Dave Chinner292378e2016-09-26 08:21:28 +10003658 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3659 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003660 return xfs_bmap_rtalloc(ap);
3661 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662}
3663
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003664/* Trim extent to fit a logical block range. */
3665void
3666xfs_trim_extent(
3667 struct xfs_bmbt_irec *irec,
3668 xfs_fileoff_t bno,
3669 xfs_filblks_t len)
3670{
3671 xfs_fileoff_t distance;
3672 xfs_fileoff_t end = bno + len;
3673
3674 if (irec->br_startoff + irec->br_blockcount <= bno ||
3675 irec->br_startoff >= end) {
3676 irec->br_blockcount = 0;
3677 return;
3678 }
3679
3680 if (irec->br_startoff < bno) {
3681 distance = bno - irec->br_startoff;
3682 if (isnullstartblock(irec->br_startblock))
3683 irec->br_startblock = DELAYSTARTBLOCK;
3684 if (irec->br_startblock != DELAYSTARTBLOCK &&
3685 irec->br_startblock != HOLESTARTBLOCK)
3686 irec->br_startblock += distance;
3687 irec->br_startoff += distance;
3688 irec->br_blockcount -= distance;
3689 }
3690
3691 if (end < irec->br_startoff + irec->br_blockcount) {
3692 distance = irec->br_startoff + irec->br_blockcount - end;
3693 irec->br_blockcount -= distance;
3694 }
3695}
3696
Brian Foster40214d12017-10-13 09:47:46 -07003697/* trim extent to within eof */
3698void
3699xfs_trim_extent_eof(
3700 struct xfs_bmbt_irec *irec,
3701 struct xfs_inode *ip)
3702
3703{
3704 xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
3705 i_size_read(VFS_I(ip))));
3706}
3707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003709 * Trim the returned map to the required bounds
3710 */
3711STATIC void
3712xfs_bmapi_trim_map(
3713 struct xfs_bmbt_irec *mval,
3714 struct xfs_bmbt_irec *got,
3715 xfs_fileoff_t *bno,
3716 xfs_filblks_t len,
3717 xfs_fileoff_t obno,
3718 xfs_fileoff_t end,
3719 int n,
3720 int flags)
3721{
3722 if ((flags & XFS_BMAPI_ENTIRE) ||
3723 got->br_startoff + got->br_blockcount <= obno) {
3724 *mval = *got;
3725 if (isnullstartblock(got->br_startblock))
3726 mval->br_startblock = DELAYSTARTBLOCK;
3727 return;
3728 }
3729
3730 if (obno > *bno)
3731 *bno = obno;
3732 ASSERT((*bno >= obno) || (n == 0));
3733 ASSERT(*bno < end);
3734 mval->br_startoff = *bno;
3735 if (isnullstartblock(got->br_startblock))
3736 mval->br_startblock = DELAYSTARTBLOCK;
3737 else
3738 mval->br_startblock = got->br_startblock +
3739 (*bno - got->br_startoff);
3740 /*
3741 * Return the minimum of what we got and what we asked for for
3742 * the length. We can use the len variable here because it is
3743 * modified below and we could have been there before coming
3744 * here if the first part of the allocation didn't overlap what
3745 * was asked for.
3746 */
3747 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3748 got->br_blockcount - (*bno - got->br_startoff));
3749 mval->br_state = got->br_state;
3750 ASSERT(mval->br_blockcount <= len);
3751 return;
3752}
3753
3754/*
3755 * Update and validate the extent map to return
3756 */
3757STATIC void
3758xfs_bmapi_update_map(
3759 struct xfs_bmbt_irec **map,
3760 xfs_fileoff_t *bno,
3761 xfs_filblks_t *len,
3762 xfs_fileoff_t obno,
3763 xfs_fileoff_t end,
3764 int *n,
3765 int flags)
3766{
3767 xfs_bmbt_irec_t *mval = *map;
3768
3769 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3770 ((mval->br_startoff + mval->br_blockcount) <= end));
3771 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3772 (mval->br_startoff < obno));
3773
3774 *bno = mval->br_startoff + mval->br_blockcount;
3775 *len = end - *bno;
3776 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3777 /* update previous map with new information */
3778 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3779 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3780 ASSERT(mval->br_state == mval[-1].br_state);
3781 mval[-1].br_blockcount = mval->br_blockcount;
3782 mval[-1].br_state = mval->br_state;
3783 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3784 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3785 mval[-1].br_startblock != HOLESTARTBLOCK &&
3786 mval->br_startblock == mval[-1].br_startblock +
3787 mval[-1].br_blockcount &&
Christoph Hellwigc3a2f9f2018-07-11 22:26:01 -07003788 mval[-1].br_state == mval->br_state) {
Dave Chinneraef9a892011-09-18 20:40:44 +00003789 ASSERT(mval->br_startoff ==
3790 mval[-1].br_startoff + mval[-1].br_blockcount);
3791 mval[-1].br_blockcount += mval->br_blockcount;
3792 } else if (*n > 0 &&
3793 mval->br_startblock == DELAYSTARTBLOCK &&
3794 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3795 mval->br_startoff ==
3796 mval[-1].br_startoff + mval[-1].br_blockcount) {
3797 mval[-1].br_blockcount += mval->br_blockcount;
3798 mval[-1].br_state = mval->br_state;
3799 } else if (!((*n == 0) &&
3800 ((mval->br_startoff + mval->br_blockcount) <=
3801 obno))) {
3802 mval++;
3803 (*n)++;
3804 }
3805 *map = mval;
3806}
3807
3808/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003809 * Map file blocks to filesystem blocks without allocation.
3810 */
3811int
3812xfs_bmapi_read(
3813 struct xfs_inode *ip,
3814 xfs_fileoff_t bno,
3815 xfs_filblks_t len,
3816 struct xfs_bmbt_irec *mval,
3817 int *nmap,
3818 int flags)
3819{
3820 struct xfs_mount *mp = ip->i_mount;
3821 struct xfs_ifork *ifp;
3822 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003823 xfs_fileoff_t obno;
3824 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003825 struct xfs_iext_cursor icur;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003826 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003827 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003828 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003829 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003830
3831 ASSERT(*nmap >= 1);
3832 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Christoph Hellwigc3a2f9f2018-07-11 22:26:01 -07003833 XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003834 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003835
3836 if (unlikely(XFS_TEST_ERROR(
3837 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3838 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003839 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003840 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003841 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003842 }
3843
3844 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003845 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003846
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003847 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003848
3849 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003850
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003851 /* No CoW fork? Return a hole. */
3852 if (whichfork == XFS_COW_FORK && !ifp) {
3853 mval->br_startoff = bno;
3854 mval->br_startblock = HOLESTARTBLOCK;
3855 mval->br_blockcount = len;
3856 mval->br_state = XFS_EXT_NORM;
3857 *nmap = 1;
3858 return 0;
3859 }
3860
Dave Chinner5c8ed202011-09-18 20:40:45 +00003861 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3862 error = xfs_iread_extents(NULL, ip, whichfork);
3863 if (error)
3864 return error;
3865 }
3866
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003867 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003868 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003869 end = bno + len;
3870 obno = bno;
3871
3872 while (bno < end && n < *nmap) {
3873 /* Reading past eof, act as though there's a hole up to end. */
3874 if (eof)
3875 got.br_startoff = end;
3876 if (got.br_startoff > bno) {
3877 /* Reading in a hole. */
3878 mval->br_startoff = bno;
3879 mval->br_startblock = HOLESTARTBLOCK;
3880 mval->br_blockcount =
3881 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3882 mval->br_state = XFS_EXT_NORM;
3883 bno += mval->br_blockcount;
3884 len -= mval->br_blockcount;
3885 mval++;
3886 n++;
3887 continue;
3888 }
3889
3890 /* set up the extent map to return. */
3891 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3892 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3893
3894 /* If we're done, stop now. */
3895 if (bno >= end || n >= *nmap)
3896 break;
3897
3898 /* Else go on to the next record. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003899 if (!xfs_iext_next_extent(ifp, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003900 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003901 }
3902 *nmap = n;
3903 return 0;
3904}
3905
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003906/*
3907 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3908 * global pool and the extent inserted into the inode in-core extent tree.
3909 *
3910 * On entry, got refers to the first extent beyond the offset of the extent to
3911 * allocate or eof is specified if no such extent exists. On return, got refers
3912 * to the extent record that was inserted to the inode fork.
3913 *
3914 * Note that the allocated extent may have been merged with contiguous extents
3915 * during insertion into the inode fork. Thus, got does not reflect the current
3916 * state of the inode fork on return. If necessary, the caller can use lastx to
3917 * look up the updated record in the inode fork.
3918 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10003919int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003920xfs_bmapi_reserve_delalloc(
3921 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003922 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11003923 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003924 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11003925 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003926 struct xfs_bmbt_irec *got,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003927 struct xfs_iext_cursor *icur,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003928 int eof)
3929{
3930 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003931 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003932 xfs_extlen_t alen;
3933 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003934 int error;
Brian Foster974ae922016-11-28 14:57:42 +11003935 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003936
Brian Foster974ae922016-11-28 14:57:42 +11003937 /*
3938 * Cap the alloc length. Keep track of prealloc so we know whether to
3939 * tag the inode before we return.
3940 */
3941 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003942 if (!eof)
3943 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11003944 if (prealloc && alen >= len)
3945 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003946
3947 /* Figure out the extent size, adjust alen */
Shan Hai6ca30722018-01-23 13:56:11 -08003948 if (whichfork == XFS_COW_FORK) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003949 struct xfs_bmbt_irec prev;
Shan Hai6ca30722018-01-23 13:56:11 -08003950 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003951
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003952 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003953 prev.br_startoff = NULLFILEOFF;
3954
Shan Hai6ca30722018-01-23 13:56:11 -08003955 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003956 1, 0, &aoff, &alen);
3957 ASSERT(!error);
3958 }
3959
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003960 /*
3961 * Make a transaction-less quota reservation for delayed allocation
3962 * blocks. This number gets adjusted later. We return if we haven't
3963 * allocated blocks already inside this loop.
3964 */
3965 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
Shan Hai6ca30722018-01-23 13:56:11 -08003966 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003967 if (error)
3968 return error;
3969
3970 /*
3971 * Split changing sb for alen and indlen since they could be coming
3972 * from different places.
3973 */
3974 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
3975 ASSERT(indlen > 0);
3976
Shan Hai6ca30722018-01-23 13:56:11 -08003977 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003978 if (error)
3979 goto out_unreserve_quota;
3980
Dave Chinner0d485ad2015-02-23 21:22:03 +11003981 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003982 if (error)
3983 goto out_unreserve_blocks;
3984
3985
3986 ip->i_delayed_blks += alen;
3987
3988 got->br_startoff = aoff;
3989 got->br_startblock = nullstartblock(indlen);
3990 got->br_blockcount = alen;
3991 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003992
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003993 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003994
Brian Foster974ae922016-11-28 14:57:42 +11003995 /*
3996 * Tag the inode if blocks were preallocated. Note that COW fork
3997 * preallocation can occur at the start or end of the extent, even when
3998 * prealloc == 0, so we must also check the aligned offset and length.
3999 */
4000 if (whichfork == XFS_DATA_FORK && prealloc)
4001 xfs_inode_set_eofblocks_tag(ip);
4002 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4003 xfs_inode_set_cowblocks_tag(ip);
4004
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004005 return 0;
4006
4007out_unreserve_blocks:
Shan Hai6ca30722018-01-23 13:56:11 -08004008 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004009out_unreserve_quota:
4010 if (XFS_IS_QUOTA_ON(mp))
Shan Hai6ca30722018-01-23 13:56:11 -08004011 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0,
4012 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004013 return error;
4014}
4015
Dave Chinnercf11da92014-07-15 07:08:24 +10004016static int
4017xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004018 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004019{
4020 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004021 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004022 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004023 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004024 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004025
Dave Chinnera99ebf42011-12-01 11:24:20 +00004026 ASSERT(bma->length > 0);
4027
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004028 /*
4029 * For the wasdelay case, we could also just allocate the stuff asked
4030 * for in this bmap call but that wouldn't be as good.
4031 */
4032 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004033 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4034 bma->offset = bma->got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004035 xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004036 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004037 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004038 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004039 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004040 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004041 }
4042
4043 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004044 * Set the data type being allocated. For the data fork, the first data
4045 * in the file is treated differently to all other allocations. For the
4046 * attribute fork, we only need to ensure the allocated range is not on
4047 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004048 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004049 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004050 bma->datatype = XFS_ALLOC_NOBUSY;
4051 if (whichfork == XFS_DATA_FORK) {
4052 if (bma->offset == 0)
4053 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4054 else
4055 bma->datatype |= XFS_ALLOC_USERDATA;
4056 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004057 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004058 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004059 }
4060
Dave Chinnere04426b2012-10-05 11:06:59 +10004061 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004062
4063 /*
4064 * Only want to do the alignment at the eof if it is userdata and
4065 * allocation length is larger than a stripe unit.
4066 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004067 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004068 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004069 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004070 if (error)
4071 return error;
4072 }
4073
4074 error = xfs_bmap_alloc(bma);
4075 if (error)
4076 return error;
4077
Dave Chinner29c8d172011-09-18 20:41:00 +00004078 if (bma->cur)
4079 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004080 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004081 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004082 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4083 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4084 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Brian Foster3e3673e2018-07-11 22:26:14 -07004085 bma->cur->bc_private.b.dfops = bma->tp->t_dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004086 }
4087 /*
4088 * Bump the number of extents we've allocated
4089 * in this call.
4090 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004091 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004092
Dave Chinner29c8d172011-09-18 20:41:00 +00004093 if (bma->cur)
4094 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004095 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4096
Dave Chinner963c30c2011-09-18 20:40:59 +00004097 bma->got.br_startoff = bma->offset;
4098 bma->got.br_startblock = bma->blkno;
4099 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004100 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004101
4102 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004103 * In the data fork, a wasdelay extent has been initialized, so
4104 * shouldn't be flagged as unwritten.
4105 *
4106 * For the cow fork, however, we convert delalloc reservations
4107 * (extents allocated for speculative preallocation) to
4108 * allocated unwritten extents, and only convert the unwritten
4109 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004110 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004111 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4112 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004113 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004114 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004115
Christoph Hellwigc6534242011-09-18 20:41:05 +00004116 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004117 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004118 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004119 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004120 whichfork, &bma->icur, &bma->cur, &bma->got,
Brian Foster3e3673e2018-07-11 22:26:14 -07004121 bma->firstblock, bma->tp->t_dfops,
4122 &bma->logflags, bma->flags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004123
Christoph Hellwigc315c902011-09-18 20:41:02 +00004124 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004125 if (error)
4126 return error;
4127
4128 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004129 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4130 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4131 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004132 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004133 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004134
Dave Chinner963c30c2011-09-18 20:40:59 +00004135 ASSERT(bma->got.br_startoff <= bma->offset);
4136 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4137 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004138 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4139 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004140 return 0;
4141}
4142
Dave Chinnerb447fe52011-09-18 20:40:51 +00004143STATIC int
4144xfs_bmapi_convert_unwritten(
4145 struct xfs_bmalloca *bma,
4146 struct xfs_bmbt_irec *mval,
4147 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004148 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004149{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004150 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004151 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004152 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004153 int error;
4154
Dave Chinnerb447fe52011-09-18 20:40:51 +00004155 /* check if we need to do unwritten->real conversion */
4156 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4157 (flags & XFS_BMAPI_PREALLOC))
4158 return 0;
4159
4160 /* check if we need to do real->unwritten conversion */
4161 if (mval->br_state == XFS_EXT_NORM &&
4162 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4163 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4164 return 0;
4165
4166 /*
4167 * Modify (by adding) the state flag, if writing.
4168 */
4169 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004170 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4171 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004172 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004173 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Brian Foster3e3673e2018-07-11 22:26:14 -07004174 bma->cur->bc_private.b.dfops = bma->tp->t_dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004175 }
4176 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4177 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4178
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004179 /*
4180 * Before insertion into the bmbt, zero the range being converted
4181 * if required.
4182 */
4183 if (flags & XFS_BMAPI_ZERO) {
4184 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4185 mval->br_blockcount);
4186 if (error)
4187 return error;
4188 }
4189
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004190 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004191 &bma->icur, &bma->cur, mval, bma->firstblock,
Brian Foster3e3673e2018-07-11 22:26:14 -07004192 bma->tp->t_dfops, &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004193 /*
4194 * Log the inode core unconditionally in the unwritten extent conversion
4195 * path because the conversion might not have done so (e.g., if the
4196 * extent count hasn't changed). We need to make sure the inode is dirty
4197 * in the transaction for the sake of fsync(), even if nothing has
4198 * changed, because fsync() will not force the log for this transaction
4199 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004200 *
4201 * Note: If we're only converting cow fork extents, there aren't
4202 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004203 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004204 if (whichfork != XFS_COW_FORK)
4205 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004206 if (error)
4207 return error;
4208
4209 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004210 * Update our extent pointer, given that
4211 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4212 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004213 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004214 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004215
4216 /*
4217 * We may have combined previously unwritten space with written space,
4218 * so generate another request.
4219 */
4220 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004221 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004222 return 0;
4223}
4224
Christoph Hellwig44032802011-09-18 20:40:48 +00004225/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004226 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4227 * extent state if necessary. Details behaviour is controlled by the flags
4228 * parameter. Only allocates blocks from a single allocation group, to avoid
4229 * locking problems.
4230 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 * The returned value in "firstblock" from the first call in a transaction
4232 * must be remembered and presented to subsequent calls in "firstblock".
4233 * An upper bound for the number of blocks to be allocated is supplied to
4234 * the first call in "total"; if no allocation group has that many free
4235 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4236 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004237int
4238xfs_bmapi_write(
4239 struct xfs_trans *tp, /* transaction pointer */
4240 struct xfs_inode *ip, /* incore inode */
4241 xfs_fileoff_t bno, /* starting file offs. mapped */
4242 xfs_filblks_t len, /* length to map in file */
4243 int flags, /* XFS_BMAPI_... */
4244 xfs_fsblock_t *firstblock, /* first allocated block
4245 controls a.g. for allocs */
4246 xfs_extlen_t total, /* total blocks needed */
4247 struct xfs_bmbt_irec *mval, /* output: map values */
Brian Foster6e702a52018-07-11 22:26:12 -07004248 int *nmap) /* i/o: mval size/count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004250 struct xfs_mount *mp = ip->i_mount;
4251 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004252 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004253 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004254 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004255 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004256 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004257 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004258 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004259
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004261 xfs_fileoff_t orig_bno; /* original block number value */
4262 int orig_flags; /* original flags arg value */
4263 xfs_filblks_t orig_len; /* original value of len arg */
4264 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4265 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
4267 orig_bno = bno;
4268 orig_len = len;
4269 orig_flags = flags;
4270 orig_mval = mval;
4271 orig_nmap = *nmap;
4272#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004273 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004274
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004276 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004277 ASSERT(tp != NULL ||
4278 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4279 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004280 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004281 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004282 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004283 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004284
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004285 /* zeroing is for currently only for data extents, not metadata */
4286 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4287 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4288 /*
4289 * we can allocate unwritten extents or pre-zero allocated blocks,
4290 * but it makes no sense to do both at once. This would result in
4291 * zeroing the unwritten extent twice, but it still being an
4292 * unwritten extent....
4293 */
4294 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4295 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4296
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 if (unlikely(XFS_TEST_ERROR(
4298 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004299 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004300 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004301 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004302 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004304
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004306 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004307
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004309
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004310 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004311
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004312 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004314 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004316 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004317 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004318 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004319 }
4320
4321 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4322 error = xfs_iread_extents(tp, ip, whichfork);
4323 if (error)
4324 goto error0;
4325 }
4326
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 n = 0;
4328 end = bno + len;
4329 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004330
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004331 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004332 eof = true;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004333 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004334 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004335 bma.tp = tp;
4336 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004337 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004338 bma.datatype = 0;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004339 bma.firstblock = firstblock;
Brian Foster3e3673e2018-07-11 22:26:14 -07004340 ASSERT(!tp || tp->t_dfops);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004341
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004343 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004344
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004345 /* in hole or beyond EOF? */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004346 if (eof || bma.got.br_startoff > bno) {
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004347 /*
4348 * CoW fork conversions should /never/ hit EOF or
4349 * holes. There should always be something for us
4350 * to work on.
4351 */
4352 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4353 (flags & XFS_BMAPI_COWFORK)));
4354
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004355 if (flags & XFS_BMAPI_DELALLOC) {
4356 /*
4357 * For the COW fork we can reasonably get a
4358 * request for converting an extent that races
4359 * with other threads already having converted
4360 * part of it, as there converting COW to
4361 * regular blocks is not protected using the
4362 * IOLOCK.
4363 */
4364 ASSERT(flags & XFS_BMAPI_COWFORK);
4365 if (!(flags & XFS_BMAPI_COWFORK)) {
4366 error = -EIO;
4367 goto error0;
4368 }
4369
4370 if (eof || bno >= end)
4371 break;
4372 } else {
4373 need_alloc = true;
4374 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004375 } else if (isnullstartblock(bma.got.br_startblock)) {
4376 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004377 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004378
4379 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 * First, deal with the hole before the allocated space
4381 * that we found, if any.
4382 */
Christoph Hellwigb1214592017-11-03 10:34:44 -07004383 if ((need_alloc || wasdelay) &&
4384 !(flags & XFS_BMAPI_CONVERT_ONLY)) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004385 bma.eof = eof;
4386 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4387 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004388 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004389 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004390
Dave Chinnera99ebf42011-12-01 11:24:20 +00004391 /*
4392 * There's a 32/64 bit type mismatch between the
4393 * allocation length request (which can be 64 bits in
4394 * length) and the bma length request, which is
4395 * xfs_extlen_t and therefore 32 bits. Hence we have to
4396 * check for 32-bit overflows and handle them here.
4397 */
4398 if (len > (xfs_filblks_t)MAXEXTLEN)
4399 bma.length = MAXEXTLEN;
4400 else
4401 bma.length = len;
4402
4403 ASSERT(len > 0);
4404 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004405 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 if (error)
4407 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004408 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004409 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004410
4411 /*
4412 * If this is a CoW allocation, record the data in
4413 * the refcount btree for orphan recovery.
4414 */
4415 if (whichfork == XFS_COW_FORK) {
Brian Foster6e702a52018-07-11 22:26:12 -07004416 error = xfs_refcount_alloc_cow_extent(mp,
4417 tp->t_dfops, bma.blkno,
4418 bma.length);
Darrick J. Wong174edb02016-10-03 09:11:39 -07004419 if (error)
4420 goto error0;
4421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004423
Dave Chinneraef9a892011-09-18 20:40:44 +00004424 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004425 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4426 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
Dave Chinnerb447fe52011-09-18 20:40:51 +00004428 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004429 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004430 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004431 continue;
4432 if (error)
4433 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
Dave Chinneraef9a892011-09-18 20:40:44 +00004435 /* update the extent map to return */
4436 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4437
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 /*
4439 * If we're done, stop now. Stop when we've allocated
4440 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4441 * the transaction may get too big.
4442 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004443 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004445
4446 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004447 bma.prev = bma.got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004448 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004449 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004452
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 /*
4454 * Transform from btree to extents, give it cur.
4455 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004456 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004457 int tmp_logflags = 0;
4458
Dave Chinner29c8d172011-09-18 20:41:00 +00004459 ASSERT(bma.cur);
4460 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004462 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 if (error)
4464 goto error0;
4465 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004466
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004468 XFS_IFORK_NEXTENTS(ip, whichfork) >
4469 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471error0:
4472 /*
4473 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004474 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004476 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004478 bma.logflags &= ~xfs_ilog_fext(whichfork);
4479 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004481 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 /*
4483 * Log whatever the flags say, even if error. Otherwise we might miss
4484 * detecting a case where the data is changed, there's an error,
4485 * and it's not logged so we don't shutdown when we should.
4486 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004487 if (bma.logflags)
4488 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004489
Dave Chinner29c8d172011-09-18 20:41:00 +00004490 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 if (!error) {
4492 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004493 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004495 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004496 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004498 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4500 }
4501 if (!error)
4502 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4503 orig_nmap, *nmap);
4504 return error;
4505}
4506
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004507int
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004508xfs_bmapi_remap(
4509 struct xfs_trans *tp,
4510 struct xfs_inode *ip,
4511 xfs_fileoff_t bno,
4512 xfs_filblks_t len,
4513 xfs_fsblock_t startblock,
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004514 int flags)
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004515{
4516 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004517 struct xfs_ifork *ifp;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004518 struct xfs_btree_cur *cur = NULL;
4519 xfs_fsblock_t firstblock = NULLFSBLOCK;
4520 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004521 struct xfs_iext_cursor icur;
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004522 int whichfork = xfs_bmapi_whichfork(flags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004523 int logflags = 0, error;
4524
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004525 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004526 ASSERT(len > 0);
4527 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4528 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Darrick J. Wong7644bd92018-05-14 06:34:35 -07004529 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4530 XFS_BMAPI_NORMAP)));
4531 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4532 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004533
4534 if (unlikely(XFS_TEST_ERROR(
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004535 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4536 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004537 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004538 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4539 return -EFSCORRUPTED;
4540 }
4541
4542 if (XFS_FORCED_SHUTDOWN(mp))
4543 return -EIO;
4544
4545 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004546 error = xfs_iread_extents(tp, ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004547 if (error)
4548 return error;
4549 }
4550
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004551 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004552 /* make sure we only reflink into a hole. */
4553 ASSERT(got.br_startoff > bno);
4554 ASSERT(got.br_startoff - bno >= len);
4555 }
4556
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004557 ip->i_d.di_nblocks += len;
4558 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004559
4560 if (ifp->if_flags & XFS_IFBROOT) {
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004561 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004562 cur->bc_private.b.firstblock = firstblock;
Brian Fosterff3edf22018-07-11 22:26:14 -07004563 cur->bc_private.b.dfops = tp->t_dfops;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004564 cur->bc_private.b.flags = 0;
4565 }
4566
4567 got.br_startoff = bno;
4568 got.br_startblock = startblock;
4569 got.br_blockcount = len;
Darrick J. Wong7644bd92018-05-14 06:34:35 -07004570 if (flags & XFS_BMAPI_PREALLOC)
4571 got.br_state = XFS_EXT_UNWRITTEN;
4572 else
4573 got.br_state = XFS_EXT_NORM;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004574
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004575 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
Brian Fosterff3edf22018-07-11 22:26:14 -07004576 &cur, &got, &firstblock, tp->t_dfops, &logflags, flags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004577 if (error)
4578 goto error0;
4579
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004580 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004581 int tmp_logflags = 0;
4582
4583 error = xfs_bmap_btree_to_extents(tp, ip, cur,
Darrick J. Wong7cf199b2018-05-14 06:34:34 -07004584 &tmp_logflags, whichfork);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004585 logflags |= tmp_logflags;
4586 }
4587
4588error0:
4589 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4590 logflags &= ~XFS_ILOG_DEXT;
4591 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4592 logflags &= ~XFS_ILOG_DBROOT;
4593
4594 if (logflags)
4595 xfs_trans_log_inode(tp, ip, logflags);
4596 if (cur) {
4597 xfs_btree_del_cursor(cur,
4598 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4599 }
4600 return error;
4601}
4602
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004604 * When a delalloc extent is split (e.g., due to a hole punch), the original
4605 * indlen reservation must be shared across the two new extents that are left
4606 * behind.
4607 *
4608 * Given the original reservation and the worst case indlen for the two new
4609 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004610 * reservation fairly across the two new extents. If necessary, steal available
4611 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4612 * ores == 1). The number of stolen blocks is returned. The availability and
4613 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004614 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004615static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004616xfs_bmap_split_indlen(
4617 xfs_filblks_t ores, /* original res. */
4618 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004619 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4620 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004621{
4622 xfs_filblks_t len1 = *indlen1;
4623 xfs_filblks_t len2 = *indlen2;
4624 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004625 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004626 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004627
4628 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004629 * Steal as many blocks as we can to try and satisfy the worst case
4630 * indlen for both new extents.
4631 */
Brian Foster75d65362017-02-13 22:48:30 -08004632 if (ores < nres && avail)
4633 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4634 ores += stolen;
4635
4636 /* nothing else to do if we've satisfied the new reservation */
4637 if (ores >= nres)
4638 return stolen;
Brian Fosterd34999c2016-03-15 11:42:47 +11004639
4640 /*
Brian Foster75d65362017-02-13 22:48:30 -08004641 * We can't meet the total required reservation for the two extents.
4642 * Calculate the percent of the overall shortage between both extents
4643 * and apply this percentage to each of the requested indlen values.
4644 * This distributes the shortage fairly and reduces the chances that one
4645 * of the two extents is left with nothing when extents are repeatedly
4646 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004647 */
Brian Foster75d65362017-02-13 22:48:30 -08004648 resfactor = (ores * 100);
4649 do_div(resfactor, nres);
4650 len1 *= resfactor;
4651 do_div(len1, 100);
4652 len2 *= resfactor;
4653 do_div(len2, 100);
4654 ASSERT(len1 + len2 <= ores);
4655 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4656
4657 /*
4658 * Hand out the remainder to each extent. If one of the two reservations
4659 * is zero, we want to make sure that one gets a block first. The loop
4660 * below starts with len1, so hand len2 a block right off the bat if it
4661 * is zero.
4662 */
4663 ores -= (len1 + len2);
4664 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4665 if (ores && !len2 && *indlen2) {
4666 len2++;
4667 ores--;
4668 }
4669 while (ores) {
4670 if (len1 < *indlen1) {
4671 len1++;
4672 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004673 }
Brian Foster75d65362017-02-13 22:48:30 -08004674 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004675 break;
Brian Foster75d65362017-02-13 22:48:30 -08004676 if (len2 < *indlen2) {
4677 len2++;
4678 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004679 }
4680 }
4681
4682 *indlen1 = len1;
4683 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004684
4685 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004686}
4687
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004688int
4689xfs_bmap_del_extent_delay(
4690 struct xfs_inode *ip,
4691 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004692 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004693 struct xfs_bmbt_irec *got,
4694 struct xfs_bmbt_irec *del)
4695{
4696 struct xfs_mount *mp = ip->i_mount;
4697 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4698 struct xfs_bmbt_irec new;
4699 int64_t da_old, da_new, da_diff = 0;
4700 xfs_fileoff_t del_endoff, got_endoff;
4701 xfs_filblks_t got_indlen, new_indlen, stolen;
Christoph Hellwig060ea652017-10-19 11:02:29 -07004702 int state = xfs_bmap_fork_to_state(whichfork);
4703 int error = 0;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004704 bool isrt;
4705
4706 XFS_STATS_INC(mp, xs_del_exlist);
4707
4708 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4709 del_endoff = del->br_startoff + del->br_blockcount;
4710 got_endoff = got->br_startoff + got->br_blockcount;
4711 da_old = startblockval(got->br_startblock);
4712 da_new = 0;
4713
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004714 ASSERT(del->br_blockcount > 0);
4715 ASSERT(got->br_startoff <= del->br_startoff);
4716 ASSERT(got_endoff >= del_endoff);
4717
4718 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004719 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004720
4721 do_div(rtexts, mp->m_sb.sb_rextsize);
4722 xfs_mod_frextents(mp, rtexts);
4723 }
4724
4725 /*
4726 * Update the inode delalloc counter now and wait to update the
4727 * sb counters as we might have to borrow some blocks for the
4728 * indirect block accounting.
4729 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004730 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4731 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004732 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004733 if (error)
4734 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004735 ip->i_delayed_blks -= del->br_blockcount;
4736
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004737 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004738 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004739 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004740 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004741
Christoph Hellwig0173c682017-10-17 14:16:22 -07004742 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4743 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004744 /*
4745 * Matches the whole extent. Delete the entry.
4746 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004747 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004748 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004749 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004750 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004751 /*
4752 * Deleting the first part of the extent.
4753 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004754 got->br_startoff = del_endoff;
4755 got->br_blockcount -= del->br_blockcount;
4756 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4757 got->br_blockcount), da_old);
4758 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004759 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004760 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004761 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004762 /*
4763 * Deleting the last part of the extent.
4764 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004765 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4766 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4767 got->br_blockcount), da_old);
4768 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004769 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004770 break;
4771 case 0:
4772 /*
4773 * Deleting the middle of the extent.
4774 *
4775 * Distribute the original indlen reservation across the two new
4776 * extents. Steal blocks from the deleted extent if necessary.
4777 * Stealing blocks simply fudges the fdblocks accounting below.
4778 * Warn if either of the new indlen reservations is zero as this
4779 * can lead to delalloc problems.
4780 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004781 got->br_blockcount = del->br_startoff - got->br_startoff;
4782 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4783
4784 new.br_blockcount = got_endoff - del_endoff;
4785 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4786
4787 WARN_ON_ONCE(!got_indlen || !new_indlen);
4788 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4789 del->br_blockcount);
4790
4791 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004792
4793 new.br_startoff = del_endoff;
4794 new.br_state = got->br_state;
4795 new.br_startblock = nullstartblock((int)new_indlen);
4796
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004797 xfs_iext_update_extent(ip, state, icur, got);
4798 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004799 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004800
4801 da_new = got_indlen + new_indlen - stolen;
4802 del->br_blockcount -= stolen;
4803 break;
4804 }
4805
4806 ASSERT(da_old >= da_new);
4807 da_diff = da_old - da_new;
4808 if (!isrt)
4809 da_diff += del->br_blockcount;
4810 if (da_diff)
4811 xfs_mod_fdblocks(mp, da_diff, false);
4812 return error;
4813}
4814
4815void
4816xfs_bmap_del_extent_cow(
4817 struct xfs_inode *ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004818 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004819 struct xfs_bmbt_irec *got,
4820 struct xfs_bmbt_irec *del)
4821{
4822 struct xfs_mount *mp = ip->i_mount;
4823 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4824 struct xfs_bmbt_irec new;
4825 xfs_fileoff_t del_endoff, got_endoff;
4826 int state = BMAP_COWFORK;
4827
4828 XFS_STATS_INC(mp, xs_del_exlist);
4829
4830 del_endoff = del->br_startoff + del->br_blockcount;
4831 got_endoff = got->br_startoff + got->br_blockcount;
4832
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004833 ASSERT(del->br_blockcount > 0);
4834 ASSERT(got->br_startoff <= del->br_startoff);
4835 ASSERT(got_endoff >= del_endoff);
4836 ASSERT(!isnullstartblock(got->br_startblock));
4837
4838 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004839 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004840 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004841 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004842
Christoph Hellwig0173c682017-10-17 14:16:22 -07004843 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4844 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004845 /*
4846 * Matches the whole extent. Delete the entry.
4847 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004848 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004849 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004850 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004851 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004852 /*
4853 * Deleting the first part of the extent.
4854 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004855 got->br_startoff = del_endoff;
4856 got->br_blockcount -= del->br_blockcount;
4857 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004858 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004859 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004860 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004861 /*
4862 * Deleting the last part of the extent.
4863 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004864 got->br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004865 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004866 break;
4867 case 0:
4868 /*
4869 * Deleting the middle of the extent.
4870 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004871 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004872
4873 new.br_startoff = del_endoff;
4874 new.br_blockcount = got_endoff - del_endoff;
4875 new.br_state = got->br_state;
4876 new.br_startblock = del->br_startblock + del->br_blockcount;
4877
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004878 xfs_iext_update_extent(ip, state, icur, got);
4879 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004880 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004881 break;
4882 }
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08004883 ip->i_delayed_blks -= del->br_blockcount;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004884}
4885
Brian Fostera9bd24a2016-03-15 11:42:46 +11004886/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11004887 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07004888 * after removing space.
Dave Chinner9e5987a72013-02-25 12:31:26 +11004889 */
4890STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07004891xfs_bmap_del_extent_real(
Dave Chinner9e5987a72013-02-25 12:31:26 +11004892 xfs_inode_t *ip, /* incore inode pointer */
4893 xfs_trans_t *tp, /* current transaction pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004894 struct xfs_iext_cursor *icur,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004895 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004896 xfs_btree_cur_t *cur, /* if null, not a btree */
4897 xfs_bmbt_irec_t *del, /* data to remove from extents */
4898 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07004899 int whichfork, /* data or attr fork */
4900 int bflags) /* bmapi flags */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004901{
Dave Chinner9e5987a72013-02-25 12:31:26 +11004902 xfs_fsblock_t del_endblock=0; /* first block past del */
4903 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004904 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004905 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004906 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004907 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004908 xfs_fileoff_t got_endoff; /* first offset past got */
4909 int i; /* temp state */
4910 xfs_ifork_t *ifp; /* inode fork pointer */
4911 xfs_mount_t *mp; /* mount structure */
4912 xfs_filblks_t nblks; /* quota/sb block count */
4913 xfs_bmbt_irec_t new; /* new record to be inserted */
4914 /* REFERENCED */
4915 uint qfield; /* quota field to update */
Christoph Hellwig060ea652017-10-19 11:02:29 -07004916 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004917 struct xfs_bmbt_irec old;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004918
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004919 mp = ip->i_mount;
4920 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004921
Dave Chinner9e5987a72013-02-25 12:31:26 +11004922 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004923 ASSERT(del->br_blockcount > 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004924 xfs_iext_get_extent(ifp, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004925 ASSERT(got.br_startoff <= del->br_startoff);
4926 del_endoff = del->br_startoff + del->br_blockcount;
4927 got_endoff = got.br_startoff + got.br_blockcount;
4928 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004929 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a72013-02-25 12:31:26 +11004930 qfield = 0;
4931 error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004932
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004933 /*
4934 * If it's the case where the directory code is running with no block
4935 * reservation, and the deleted block is in the middle of its extent,
4936 * and the resulting insert of an extent would cause transformation to
4937 * btree format, then reject it. The calling code will then swap blocks
4938 * around instead. We have to do this now, rather than waiting for the
4939 * conversion to btree format, since the transaction will be dirty then.
4940 */
4941 if (tp->t_blk_res == 0 &&
4942 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4943 XFS_IFORK_NEXTENTS(ip, whichfork) >=
4944 XFS_IFORK_MAXEXT(ip, whichfork) &&
4945 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
4946 return -ENOSPC;
4947
4948 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004949 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4950 xfs_fsblock_t bno;
4951 xfs_filblks_t len;
Dave Chinner0703a8e2018-06-08 09:54:22 -07004952 xfs_extlen_t mod;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004953
Dave Chinner0703a8e2018-06-08 09:54:22 -07004954 bno = div_u64_rem(del->br_startblock, mp->m_sb.sb_rextsize,
4955 &mod);
4956 ASSERT(mod == 0);
4957 len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize,
4958 &mod);
4959 ASSERT(mod == 0);
4960
Christoph Hellwige1d75532017-10-17 14:16:21 -07004961 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4962 if (error)
4963 goto done;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004964 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004965 nblks = len * mp->m_sb.sb_rextsize;
4966 qfield = XFS_TRANS_DQ_RTBCOUNT;
4967 } else {
4968 do_fx = 1;
4969 nblks = del->br_blockcount;
4970 qfield = XFS_TRANS_DQ_BCOUNT;
4971 }
4972
4973 del_endblock = del->br_startblock + del->br_blockcount;
4974 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07004975 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004976 if (error)
4977 goto done;
4978 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004979 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10004980
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004981 if (got.br_startoff == del->br_startoff)
4982 state |= BMAP_LEFT_FILLING;
4983 if (got_endoff == del_endoff)
4984 state |= BMAP_RIGHT_FILLING;
4985
4986 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4987 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11004988 /*
4989 * Matches the whole extent. Delete the entry.
4990 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004991 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004992 xfs_iext_prev(ifp, icur);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004993 XFS_IFORK_NEXT_SET(ip, whichfork,
4994 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4995 flags |= XFS_ILOG_CORE;
4996 if (!cur) {
4997 flags |= xfs_ilog_fext(whichfork);
4998 break;
4999 }
5000 if ((error = xfs_btree_delete(cur, &i)))
5001 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005002 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005003 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005004 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005005 /*
5006 * Deleting the first part of the extent.
5007 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005008 got.br_startoff = del_endoff;
5009 got.br_startblock = del_endblock;
5010 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005011 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005012 if (!cur) {
5013 flags |= xfs_ilog_fext(whichfork);
5014 break;
5015 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005016 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005017 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005018 goto done;
5019 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005020 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005021 /*
5022 * Deleting the last part of the extent.
5023 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005024 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005025 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005026 if (!cur) {
5027 flags |= xfs_ilog_fext(whichfork);
5028 break;
5029 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005030 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005031 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005032 goto done;
5033 break;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005034 case 0:
5035 /*
5036 * Deleting the middle of the extent.
5037 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005038 old = got;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07005039
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005040 got.br_blockcount = del->br_startoff - got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005041 xfs_iext_update_extent(ip, state, icur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005042
Dave Chinner9e5987a72013-02-25 12:31:26 +11005043 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005044 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005045 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005046 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005047
Christoph Hellwige1d75532017-10-17 14:16:21 -07005048 flags |= XFS_ILOG_CORE;
5049 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005050 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005051 if (error)
5052 goto done;
5053 error = xfs_btree_increment(cur, 0, &i);
5054 if (error)
5055 goto done;
5056 cur->bc_rec.b = new;
5057 error = xfs_btree_insert(cur, &i);
5058 if (error && error != -ENOSPC)
5059 goto done;
5060 /*
5061 * If get no-space back from btree insert, it tried a
5062 * split, and we have a zero block reservation. Fix up
5063 * our state and return the error.
5064 */
5065 if (error == -ENOSPC) {
5066 /*
5067 * Reset the cursor, don't trust it after any
5068 * insert operation.
5069 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005070 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005071 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005072 goto done;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005073 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5074 /*
5075 * Update the btree record back
5076 * to the original value.
5077 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005078 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005079 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005080 goto done;
5081 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005082 * Reset the extent record back
5083 * to the original value.
Dave Chinner9e5987a72013-02-25 12:31:26 +11005084 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005085 xfs_iext_update_extent(ip, state, icur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005086 flags = 0;
5087 error = -ENOSPC;
5088 goto done;
5089 }
5090 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5091 } else
5092 flags |= xfs_ilog_fext(whichfork);
5093 XFS_IFORK_NEXT_SET(ip, whichfork,
5094 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005095 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005096 xfs_iext_insert(ip, icur, &new, state);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005097 break;
5098 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005099
5100 /* remove reverse mapping */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005101 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5102 if (error)
5103 goto done;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005104
Dave Chinner9e5987a72013-02-25 12:31:26 +11005105 /*
5106 * If we need to, add to list of extents to delete.
5107 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005108 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005109 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5110 error = xfs_refcount_decrease_extent(mp, dfops, del);
5111 if (error)
5112 goto done;
Brian Fosterfcb762f2018-05-09 08:45:04 -07005113 } else {
Brian Foster4e529332018-05-10 09:35:42 -07005114 __xfs_bmap_add_free(mp, dfops, del->br_startblock,
5115 del->br_blockcount, NULL,
5116 (bflags & XFS_BMAPI_NODISCARD) ||
5117 del->br_state == XFS_EXT_UNWRITTEN);
Brian Fosterfcb762f2018-05-09 08:45:04 -07005118 }
Darrick J. Wong62aab202016-10-03 09:11:23 -07005119 }
5120
Dave Chinner9e5987a72013-02-25 12:31:26 +11005121 /*
5122 * Adjust inode # blocks in the file.
5123 */
5124 if (nblks)
5125 ip->i_d.di_nblocks -= nblks;
5126 /*
5127 * Adjust quota data.
5128 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005129 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a72013-02-25 12:31:26 +11005130 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5131
Dave Chinner9e5987a72013-02-25 12:31:26 +11005132done:
5133 *logflagsp = flags;
5134 return error;
5135}
5136
5137/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 * Unmap (remove) blocks from a file.
5139 * If nexts is nonzero then the number of extents to remove is limited to
5140 * that value. If not all extents in the block range can be removed then
5141 * *done is set.
5142 */
5143int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005144__xfs_bunmapi(
Brian Fosterccd9d912018-07-11 22:26:13 -07005145 struct xfs_trans *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005147 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005148 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 int flags, /* misc flags */
5150 xfs_extnum_t nexts, /* number of extents max */
Brian Fosterccd9d912018-07-11 22:26:13 -07005151 xfs_fsblock_t *firstblock) /* first allocated block
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 controls a.g. for allocs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153{
Brian Fosterccd9d912018-07-11 22:26:13 -07005154 struct xfs_defer_ops *dfops = tp ? tp->t_dfops : NULL;
5155 struct xfs_btree_cur *cur; /* bmap btree cursor */
5156 struct xfs_bmbt_irec del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 int error; /* error return value */
5158 xfs_extnum_t extno; /* extent number in list */
Brian Fosterccd9d912018-07-11 22:26:13 -07005159 struct xfs_bmbt_irec got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 xfs_ifork_t *ifp; /* inode fork pointer */
5161 int isrt; /* freeing in rt area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 int logflags; /* transaction logging flags */
5163 xfs_extlen_t mod; /* rt extent offset */
Brian Fosterccd9d912018-07-11 22:26:13 -07005164 struct xfs_mount *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 int tmp_logflags; /* partial logging flags */
5166 int wasdel; /* was a delayed alloc extent */
5167 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005169 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005170 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005171 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005172 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005173 struct xfs_iext_cursor icur;
5174 bool done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005176 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005177
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005178 whichfork = xfs_bmapi_whichfork(flags);
5179 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 ifp = XFS_IFORK_PTR(ip, whichfork);
5181 if (unlikely(
5182 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5183 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5184 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5185 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005186 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 }
5188 mp = ip->i_mount;
5189 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005190 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005191
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005192 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 ASSERT(len > 0);
5194 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005195
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005196 /*
5197 * Guesstimate how many blocks we can unmap without running the risk of
5198 * blowing out the transaction with a mix of EFIs and reflink
5199 * adjustments.
5200 */
Darrick J. Wong8c57b882017-12-10 18:03:53 -08005201 if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005202 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5203 else
5204 max_len = len;
5205
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5207 (error = xfs_iread_extents(tp, ip, whichfork)))
5208 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005209 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005210 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 return 0;
5212 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005213 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005214 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwigdc560152017-10-23 16:32:39 -07005215 end = start + len;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005216
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005217 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
Christoph Hellwigdc560152017-10-23 16:32:39 -07005218 *rlen = 0;
5219 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 }
Christoph Hellwigdc560152017-10-23 16:32:39 -07005221 end--;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005222
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 logflags = 0;
5224 if (ifp->if_flags & XFS_IFBROOT) {
5225 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005226 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005228 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 cur->bc_private.b.flags = 0;
5230 } else
5231 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005232
5233 if (isrt) {
5234 /*
5235 * Synchronize by locking the bitmap inode.
5236 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005237 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005238 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005239 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5240 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005241 }
5242
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 extno = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005244 while (end != (xfs_fileoff_t)-1 && end >= start &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005245 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005247 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 * Just back up to the previous extent, if so.
5249 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005250 if (got.br_startoff > end &&
5251 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5252 done = true;
5253 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 }
5255 /*
5256 * Is the last block of this extent before the range
5257 * we're supposed to delete? If so, we're done.
5258 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005259 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005261 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 break;
5263 /*
5264 * Then deal with the (possibly delayed) allocated space
5265 * we found.
5266 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005268 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005269
5270 /*
5271 * Make sure we don't touch multiple AGF headers out of order
5272 * in a single transaction, as that could cause AB-BA deadlocks.
5273 */
5274 if (!wasdel) {
5275 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5276 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5277 break;
5278 prev_agno = agno;
5279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 if (got.br_startoff < start) {
5281 del.br_startoff = start;
5282 del.br_blockcount -= start - got.br_startoff;
5283 if (!wasdel)
5284 del.br_startblock += start - got.br_startoff;
5285 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005286 if (del.br_startoff + del.br_blockcount > end + 1)
5287 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005288
5289 /* How much can we safely unmap? */
5290 if (max_len < del.br_blockcount) {
5291 del.br_startoff += del.br_blockcount - max_len;
5292 if (!wasdel)
5293 del.br_startblock += del.br_blockcount - max_len;
5294 del.br_blockcount = max_len;
5295 }
5296
Dave Chinner0703a8e2018-06-08 09:54:22 -07005297 if (!isrt)
5298 goto delete;
5299
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 sum = del.br_startblock + del.br_blockcount;
Dave Chinner0703a8e2018-06-08 09:54:22 -07005301 div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod);
5302 if (mod) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 /*
5304 * Realtime extent not lined up at the end.
5305 * The extent could have been split into written
5306 * and unwritten pieces, or we could just be
5307 * unmapping part of it. But we can't really
5308 * get rid of part of a realtime extent.
5309 */
5310 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005311 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 /*
5313 * This piece is unwritten, or we're not
5314 * using unwritten extents. Skip over it.
5315 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005316 ASSERT(end >= mod);
5317 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 del.br_blockcount : mod;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005319 if (end < got.br_startoff &&
5320 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5321 done = true;
5322 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 }
5324 continue;
5325 }
5326 /*
5327 * It's written, turn it unwritten.
5328 * This is better than zeroing it.
5329 */
5330 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005331 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 /*
5333 * If this spans a realtime extent boundary,
5334 * chop it back to the start of the one we end at.
5335 */
5336 if (del.br_blockcount > mod) {
5337 del.br_startoff += del.br_blockcount - mod;
5338 del.br_startblock += del.br_blockcount - mod;
5339 del.br_blockcount = mod;
5340 }
5341 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005342 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005343 whichfork, &icur, &cur, &del,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005344 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 if (error)
5346 goto error0;
5347 goto nodelete;
5348 }
Dave Chinner0703a8e2018-06-08 09:54:22 -07005349 div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod);
5350 if (mod) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 /*
5352 * Realtime extent is lined up at the end but not
5353 * at the front. We'll get rid of full extents if
5354 * we can.
5355 */
5356 mod = mp->m_sb.sb_rextsize - mod;
5357 if (del.br_blockcount > mod) {
5358 del.br_blockcount -= mod;
5359 del.br_startoff += mod;
5360 del.br_startblock += mod;
5361 } else if ((del.br_startoff == start &&
5362 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005363 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005364 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 /*
5366 * Can't make it unwritten. There isn't
5367 * a full extent here so just skip it.
5368 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005369 ASSERT(end >= del.br_blockcount);
5370 end -= del.br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005371 if (got.br_startoff > end &&
5372 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5373 done = true;
5374 break;
5375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 continue;
5377 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005378 struct xfs_bmbt_irec prev;
5379
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 /*
5381 * This one is already unwritten.
5382 * It must have a written left neighbor.
5383 * Unwrite the killed part of that one and
5384 * try again.
5385 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005386 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5387 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005389 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 ASSERT(del.br_startblock ==
5391 prev.br_startblock + prev.br_blockcount);
5392 if (prev.br_startoff < start) {
5393 mod = start - prev.br_startoff;
5394 prev.br_blockcount -= mod;
5395 prev.br_startblock += mod;
5396 prev.br_startoff = start;
5397 }
5398 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005399 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005400 ip, whichfork, &icur, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005401 &prev, firstblock, dfops,
5402 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 if (error)
5404 goto error0;
5405 goto nodelete;
5406 } else {
5407 ASSERT(del.br_state == XFS_EXT_NORM);
5408 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005409 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005410 ip, whichfork, &icur, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005411 &del, firstblock, dfops,
5412 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 if (error)
5414 goto error0;
5415 goto nodelete;
5416 }
5417 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005418
Dave Chinner0703a8e2018-06-08 09:54:22 -07005419delete:
Brian Fosterb2706a02016-03-15 11:42:46 +11005420 if (wasdel) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005421 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005422 &got, &del);
5423 } else {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005424 error = xfs_bmap_del_extent_real(ip, tp, &icur, dfops,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005425 cur, &del, &tmp_logflags, whichfork,
5426 flags);
5427 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005428 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005429
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 if (error)
5431 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005432
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005433 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005434 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 /*
5437 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005439 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005440 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5441 (got.br_startoff > end &&
5442 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5443 done = true;
5444 break;
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 extno++;
5447 }
5448 }
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005449 if (done || end == (xfs_fileoff_t)-1 || end < start)
Darrick J. Wong44535932016-10-03 09:11:29 -07005450 *rlen = 0;
5451 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005452 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005453
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 /*
5455 * Convert to a btree if necessary.
5456 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005457 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005459 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 &cur, 0, &tmp_logflags, whichfork);
5461 logflags |= tmp_logflags;
5462 if (error)
5463 goto error0;
5464 }
5465 /*
5466 * transform from btree to extents, give it cur
5467 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005468 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 ASSERT(cur != NULL);
5470 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5471 whichfork);
5472 logflags |= tmp_logflags;
5473 if (error)
5474 goto error0;
5475 }
5476 /*
5477 * transform from extents to local?
5478 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 error = 0;
5480error0:
5481 /*
5482 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005483 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005485 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005487 logflags &= ~xfs_ilog_fext(whichfork);
5488 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005490 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 /*
5492 * Log inode even in the error case, if the transaction
5493 * is dirty we'll need to shut down the filesystem.
5494 */
5495 if (logflags)
5496 xfs_trans_log_inode(tp, ip, logflags);
5497 if (cur) {
5498 if (!error) {
5499 *firstblock = cur->bc_private.b.firstblock;
5500 cur->bc_private.b.allocated = 0;
5501 }
5502 xfs_btree_del_cursor(cur,
5503 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5504 }
5505 return error;
5506}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005507
Darrick J. Wong44535932016-10-03 09:11:29 -07005508/* Unmap a range of a file. */
5509int
5510xfs_bunmapi(
5511 xfs_trans_t *tp,
5512 struct xfs_inode *ip,
5513 xfs_fileoff_t bno,
5514 xfs_filblks_t len,
5515 int flags,
5516 xfs_extnum_t nexts,
5517 xfs_fsblock_t *firstblock,
Darrick J. Wong44535932016-10-03 09:11:29 -07005518 int *done)
5519{
5520 int error;
5521
Brian Fosterccd9d912018-07-11 22:26:13 -07005522 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock);
Darrick J. Wong44535932016-10-03 09:11:29 -07005523 *done = (len == 0);
5524 return error;
5525}
5526
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005527/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005528 * Determine whether an extent shift can be accomplished by a merge with the
5529 * extent that precedes the target hole of the shift.
5530 */
5531STATIC bool
5532xfs_bmse_can_merge(
5533 struct xfs_bmbt_irec *left, /* preceding extent */
5534 struct xfs_bmbt_irec *got, /* current extent to shift */
5535 xfs_fileoff_t shift) /* shift fsb */
5536{
5537 xfs_fileoff_t startoff;
5538
5539 startoff = got->br_startoff - shift;
5540
5541 /*
5542 * The extent, once shifted, must be adjacent in-file and on-disk with
5543 * the preceding extent.
5544 */
5545 if ((left->br_startoff + left->br_blockcount != startoff) ||
5546 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5547 (left->br_state != got->br_state) ||
5548 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5549 return false;
5550
5551 return true;
5552}
5553
5554/*
5555 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5556 * hole in the file. If an extent shift would result in the extent being fully
5557 * adjacent to the extent that currently precedes the hole, we can merge with
5558 * the preceding extent rather than do the shift.
5559 *
5560 * This function assumes the caller has verified a shift-by-merge is possible
5561 * with the provided extents via xfs_bmse_can_merge().
5562 */
5563STATIC int
5564xfs_bmse_merge(
5565 struct xfs_inode *ip,
5566 int whichfork,
5567 xfs_fileoff_t shift, /* shift fsb */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005568 struct xfs_iext_cursor *icur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005569 struct xfs_bmbt_irec *got, /* extent to shift */
5570 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005571 struct xfs_btree_cur *cur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005572 int *logflags, /* output */
5573 struct xfs_defer_ops *dfops)
Brian Fosterddb19e32014-09-23 15:38:09 +10005574{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005575 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005576 xfs_filblks_t blockcount;
5577 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005578 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005579
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005580 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005581
5582 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5583 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005584 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005585
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005586 new = *left;
5587 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005588
5589 /*
5590 * Update the on-disk extent count, the btree if necessary and log the
5591 * inode.
5592 */
5593 XFS_IFORK_NEXT_SET(ip, whichfork,
5594 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5595 *logflags |= XFS_ILOG_CORE;
5596 if (!cur) {
5597 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005598 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005599 }
5600
5601 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005602 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005603 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005604 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005605 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005606
5607 error = xfs_btree_delete(cur, &i);
5608 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005609 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005610 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005611
5612 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005613 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005614 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005615 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005616 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005617
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005618 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005619 if (error)
5620 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005621
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005622done:
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005623 xfs_iext_remove(ip, icur, 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005624 xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur);
5625 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5626 &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005627
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005628 /* update reverse mapping. rmap functions merge the rmaps for us */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005629 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
5630 if (error)
5631 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005632 memcpy(&new, got, sizeof(new));
5633 new.br_startoff = left->br_startoff + left->br_blockcount;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005634 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005635}
5636
Christoph Hellwigbf806282017-10-19 11:07:34 -07005637static int
5638xfs_bmap_shift_update_extent(
5639 struct xfs_inode *ip,
5640 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005641 struct xfs_iext_cursor *icur,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005642 struct xfs_bmbt_irec *got,
5643 struct xfs_btree_cur *cur,
5644 int *logflags,
5645 struct xfs_defer_ops *dfops,
5646 xfs_fileoff_t startoff)
Brian Fostera979bdf2014-09-23 15:39:04 +10005647{
Christoph Hellwigbf806282017-10-19 11:07:34 -07005648 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005649 struct xfs_bmbt_irec prev = *got;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005650 int error, i;
Brian Fostera979bdf2014-09-23 15:39:04 +10005651
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005652 *logflags |= XFS_ILOG_CORE;
5653
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005654 got->br_startoff = startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005655
5656 if (cur) {
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005657 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005658 if (error)
5659 return error;
5660 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5661
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005662 error = xfs_bmbt_update(cur, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005663 if (error)
5664 return error;
5665 } else {
5666 *logflags |= XFS_ILOG_DEXT;
5667 }
5668
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005669 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5670 got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005671
Darrick J. Wong9c194642016-08-03 12:16:05 +10005672 /* update reverse mapping */
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005673 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &prev);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005674 if (error)
5675 return error;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005676 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005677}
5678
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005679int
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005680xfs_bmap_collapse_extents(
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005681 struct xfs_trans *tp,
5682 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005683 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005684 xfs_fileoff_t offset_shift_fsb,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005685 bool *done,
Brian Fosterf4a9cf972018-07-11 22:26:15 -07005686 xfs_fsblock_t *firstblock)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005687{
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005688 int whichfork = XFS_DATA_FORK;
5689 struct xfs_mount *mp = ip->i_mount;
5690 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5691 struct xfs_btree_cur *cur = NULL;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005692 struct xfs_bmbt_irec got, prev;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005693 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005694 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005695 int error = 0;
5696 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005697
5698 if (unlikely(XFS_TEST_ERROR(
5699 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5700 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005701 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005702 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005703 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005704 }
5705
5706 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005707 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005708
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005709 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005710
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005711 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005712 error = xfs_iread_extents(tp, ip, whichfork);
5713 if (error)
5714 return error;
5715 }
5716
Brian Fosterddb19e32014-09-23 15:38:09 +10005717 if (ifp->if_flags & XFS_IFBROOT) {
5718 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5719 cur->bc_private.b.firstblock = *firstblock;
Brian Fosterf4a9cf972018-07-11 22:26:15 -07005720 cur->bc_private.b.dfops = tp->t_dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10005721 cur->bc_private.b.flags = 0;
5722 }
5723
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005724 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005725 *done = true;
5726 goto del_cursor;
5727 }
Eric Sandeend41c6172017-11-27 18:23:32 -08005728 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5729 del_cursor);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005730
Christoph Hellwigbf806282017-10-19 11:07:34 -07005731 new_startoff = got.br_startoff - offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005732 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005733 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5734 error = -EINVAL;
5735 goto del_cursor;
5736 }
5737
Christoph Hellwigbf806282017-10-19 11:07:34 -07005738 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5739 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005740 &icur, &got, &prev, cur, &logflags,
Brian Fosterf4a9cf972018-07-11 22:26:15 -07005741 tp->t_dfops);
Christoph Hellwigbf806282017-10-19 11:07:34 -07005742 if (error)
5743 goto del_cursor;
5744 goto done;
5745 }
5746 } else {
5747 if (got.br_startoff < offset_shift_fsb) {
5748 error = -EINVAL;
5749 goto del_cursor;
5750 }
5751 }
5752
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005753 error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur,
Brian Fosterf4a9cf972018-07-11 22:26:15 -07005754 &logflags, tp->t_dfops, new_startoff);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005755 if (error)
5756 goto del_cursor;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005757
Christoph Hellwig42630362017-11-03 10:34:41 -07005758done:
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005759 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5760 *done = true;
5761 goto del_cursor;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005762 }
5763
Christoph Hellwigbf806282017-10-19 11:07:34 -07005764 *next_fsb = got.br_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005765del_cursor:
5766 if (cur)
5767 xfs_btree_del_cursor(cur,
5768 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005769 if (logflags)
5770 xfs_trans_log_inode(tp, ip, logflags);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005771 return error;
5772}
5773
Darrick J. Wongf62cb482018-06-21 23:26:57 -07005774/* Make sure we won't be right-shifting an extent past the maximum bound. */
5775int
5776xfs_bmap_can_insert_extents(
5777 struct xfs_inode *ip,
5778 xfs_fileoff_t off,
5779 xfs_fileoff_t shift)
5780{
5781 struct xfs_bmbt_irec got;
5782 int is_empty;
5783 int error = 0;
5784
5785 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5786
5787 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5788 return -EIO;
5789
5790 xfs_ilock(ip, XFS_ILOCK_EXCL);
5791 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
5792 if (!error && !is_empty && got.br_startoff >= off &&
5793 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
5794 error = -EINVAL;
5795 xfs_iunlock(ip, XFS_ILOCK_EXCL);
5796
5797 return error;
5798}
5799
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005800int
5801xfs_bmap_insert_extents(
5802 struct xfs_trans *tp,
5803 struct xfs_inode *ip,
5804 xfs_fileoff_t *next_fsb,
5805 xfs_fileoff_t offset_shift_fsb,
5806 bool *done,
5807 xfs_fileoff_t stop_fsb,
Brian Fosterf4a9cf972018-07-11 22:26:15 -07005808 xfs_fsblock_t *firstblock)
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005809{
5810 int whichfork = XFS_DATA_FORK;
5811 struct xfs_mount *mp = ip->i_mount;
5812 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5813 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005814 struct xfs_bmbt_irec got, next;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005815 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005816 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005817 int error = 0;
5818 int logflags = 0;
5819
5820 if (unlikely(XFS_TEST_ERROR(
5821 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5822 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5823 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5824 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
5825 return -EFSCORRUPTED;
5826 }
5827
5828 if (XFS_FORCED_SHUTDOWN(mp))
5829 return -EIO;
5830
5831 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5832
5833 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5834 error = xfs_iread_extents(tp, ip, whichfork);
5835 if (error)
5836 return error;
5837 }
5838
5839 if (ifp->if_flags & XFS_IFBROOT) {
5840 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5841 cur->bc_private.b.firstblock = *firstblock;
Brian Fosterf4a9cf972018-07-11 22:26:15 -07005842 cur->bc_private.b.dfops = tp->t_dfops;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005843 cur->bc_private.b.flags = 0;
5844 }
5845
Namjae Jeona904b1c2015-03-25 15:08:56 +11005846 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005847 xfs_iext_last(ifp, &icur);
5848 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005849 stop_fsb > got.br_startoff) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005850 *done = true;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005851 goto del_cursor;
5852 }
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005853 } else {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005854 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005855 *done = true;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005856 goto del_cursor;
5857 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005858 }
Eric Sandeend41c6172017-11-27 18:23:32 -08005859 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5860 del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005861
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005862 if (stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005863 error = -EIO;
5864 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005865 }
5866
Christoph Hellwigbf806282017-10-19 11:07:34 -07005867 new_startoff = got.br_startoff + offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005868 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005869 if (new_startoff + got.br_blockcount > next.br_startoff) {
5870 error = -EINVAL;
5871 goto del_cursor;
5872 }
5873
5874 /*
5875 * Unlike a left shift (which involves a hole punch), a right
5876 * shift does not modify extent neighbors in any way. We should
5877 * never find mergeable extents in this scenario. Check anyways
5878 * and warn if we encounter two extents that could be one.
5879 */
5880 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5881 WARN_ON_ONCE(1);
5882 }
5883
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005884 error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur,
Brian Fosterf4a9cf972018-07-11 22:26:15 -07005885 &logflags, tp->t_dfops, new_startoff);
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005886 if (error)
5887 goto del_cursor;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005888
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005889 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005890 stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005891 *done = true;
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005892 goto del_cursor;
5893 }
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005894
5895 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005896del_cursor:
5897 if (cur)
5898 xfs_btree_del_cursor(cur,
5899 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Brian Fosterca446d82014-09-02 12:12:53 +10005900 if (logflags)
5901 xfs_trans_log_inode(tp, ip, logflags);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005902 return error;
5903}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005904
5905/*
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005906 * Splits an extent into two extents at split_fsb block such that it is the
5907 * first block of the current_ext. @ext is a target extent to be split.
5908 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5909 * hole or the first block of extents, just return 0.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005910 */
5911STATIC int
5912xfs_bmap_split_extent_at(
5913 struct xfs_trans *tp,
5914 struct xfs_inode *ip,
5915 xfs_fileoff_t split_fsb,
Brian Fosterd76e6ce2018-07-11 22:26:10 -07005916 xfs_fsblock_t *firstfsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005917{
5918 int whichfork = XFS_DATA_FORK;
5919 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005920 struct xfs_bmbt_irec got;
5921 struct xfs_bmbt_irec new; /* split extent */
5922 struct xfs_mount *mp = ip->i_mount;
5923 struct xfs_ifork *ifp;
5924 xfs_fsblock_t gotblkcnt; /* new block count for got */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005925 struct xfs_iext_cursor icur;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005926 int error = 0;
5927 int logflags = 0;
5928 int i = 0;
5929
5930 if (unlikely(XFS_TEST_ERROR(
5931 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5932 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005933 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11005934 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5935 XFS_ERRLEVEL_LOW, mp);
5936 return -EFSCORRUPTED;
5937 }
5938
5939 if (XFS_FORCED_SHUTDOWN(mp))
5940 return -EIO;
5941
5942 ifp = XFS_IFORK_PTR(ip, whichfork);
5943 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5944 /* Read in all the extents */
5945 error = xfs_iread_extents(tp, ip, whichfork);
5946 if (error)
5947 return error;
5948 }
5949
5950 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005951 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005952 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005953 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005954 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005955 return 0;
5956
5957 gotblkcnt = split_fsb - got.br_startoff;
5958 new.br_startoff = split_fsb;
5959 new.br_startblock = got.br_startblock + gotblkcnt;
5960 new.br_blockcount = got.br_blockcount - gotblkcnt;
5961 new.br_state = got.br_state;
5962
5963 if (ifp->if_flags & XFS_IFBROOT) {
5964 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5965 cur->bc_private.b.firstblock = *firstfsb;
Brian Fosterd76e6ce2018-07-11 22:26:10 -07005966 cur->bc_private.b.dfops = tp->t_dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005967 cur->bc_private.b.flags = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005968 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005969 if (error)
5970 goto del_cursor;
5971 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5972 }
5973
Namjae Jeona904b1c2015-03-25 15:08:56 +11005974 got.br_blockcount = gotblkcnt;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005975 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
5976 &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005977
5978 logflags = XFS_ILOG_CORE;
5979 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005980 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005981 if (error)
5982 goto del_cursor;
5983 } else
5984 logflags |= XFS_ILOG_DEXT;
5985
5986 /* Add new extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005987 xfs_iext_next(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005988 xfs_iext_insert(ip, &icur, &new, 0);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005989 XFS_IFORK_NEXT_SET(ip, whichfork,
5990 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5991
5992 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005993 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005994 if (error)
5995 goto del_cursor;
5996 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005997 error = xfs_btree_insert(cur, &i);
5998 if (error)
5999 goto del_cursor;
6000 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6001 }
6002
6003 /*
6004 * Convert to a btree if necessary.
6005 */
6006 if (xfs_bmap_needs_btree(ip, whichfork)) {
6007 int tmp_logflags; /* partial log flag return val */
6008
6009 ASSERT(cur == NULL);
Brian Fosterd76e6ce2018-07-11 22:26:10 -07006010 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, tp->t_dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006011 &cur, 0, &tmp_logflags, whichfork);
6012 logflags |= tmp_logflags;
6013 }
6014
6015del_cursor:
6016 if (cur) {
6017 cur->bc_private.b.allocated = 0;
6018 xfs_btree_del_cursor(cur,
6019 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6020 }
6021
6022 if (logflags)
6023 xfs_trans_log_inode(tp, ip, logflags);
6024 return error;
6025}
6026
6027int
6028xfs_bmap_split_extent(
6029 struct xfs_inode *ip,
6030 xfs_fileoff_t split_fsb)
6031{
6032 struct xfs_mount *mp = ip->i_mount;
6033 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006034 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006035 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006036 int error;
6037
Christoph Hellwig253f4912016-04-06 09:19:55 +10006038 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6039 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6040 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006041 return error;
Brian Fosterd76e6ce2018-07-11 22:26:10 -07006042 xfs_defer_init(&dfops, &firstfsb);
6043 tp->t_dfops = &dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006044
6045 xfs_ilock(ip, XFS_ILOCK_EXCL);
6046 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6047
Namjae Jeona904b1c2015-03-25 15:08:56 +11006048 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Brian Fosterd76e6ce2018-07-11 22:26:10 -07006049 &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006050 if (error)
6051 goto out;
6052
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07006053 error = xfs_defer_finish(&tp, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006054 if (error)
6055 goto out;
6056
Christoph Hellwig70393312015-06-04 13:48:08 +10006057 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006058
6059out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006060 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006061 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006062 return error;
6063}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006064
6065/* Deferred mapping is only for real extents in the data fork. */
6066static bool
6067xfs_bmap_is_update_needed(
6068 struct xfs_bmbt_irec *bmap)
6069{
6070 return bmap->br_startblock != HOLESTARTBLOCK &&
6071 bmap->br_startblock != DELAYSTARTBLOCK;
6072}
6073
6074/* Record a bmap intent. */
6075static int
6076__xfs_bmap_add(
6077 struct xfs_mount *mp,
6078 struct xfs_defer_ops *dfops,
6079 enum xfs_bmap_intent_type type,
6080 struct xfs_inode *ip,
6081 int whichfork,
6082 struct xfs_bmbt_irec *bmap)
6083{
6084 int error;
6085 struct xfs_bmap_intent *bi;
6086
6087 trace_xfs_bmap_defer(mp,
6088 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6089 type,
6090 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6091 ip->i_ino, whichfork,
6092 bmap->br_startoff,
6093 bmap->br_blockcount,
6094 bmap->br_state);
6095
6096 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6097 INIT_LIST_HEAD(&bi->bi_list);
6098 bi->bi_type = type;
6099 bi->bi_owner = ip;
6100 bi->bi_whichfork = whichfork;
6101 bi->bi_bmap = *bmap;
6102
Christoph Hellwig882d8782017-08-28 10:21:03 -07006103 error = xfs_defer_ijoin(dfops, bi->bi_owner);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006104 if (error) {
6105 kmem_free(bi);
6106 return error;
6107 }
6108
6109 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6110 return 0;
6111}
6112
6113/* Map an extent into a file. */
6114int
6115xfs_bmap_map_extent(
6116 struct xfs_mount *mp,
6117 struct xfs_defer_ops *dfops,
6118 struct xfs_inode *ip,
6119 struct xfs_bmbt_irec *PREV)
6120{
6121 if (!xfs_bmap_is_update_needed(PREV))
6122 return 0;
6123
6124 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6125 XFS_DATA_FORK, PREV);
6126}
6127
6128/* Unmap an extent out of a file. */
6129int
6130xfs_bmap_unmap_extent(
6131 struct xfs_mount *mp,
6132 struct xfs_defer_ops *dfops,
6133 struct xfs_inode *ip,
6134 struct xfs_bmbt_irec *PREV)
6135{
6136 if (!xfs_bmap_is_update_needed(PREV))
6137 return 0;
6138
6139 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6140 XFS_DATA_FORK, PREV);
6141}
6142
6143/*
6144 * Process one of the deferred bmap operations. We pass back the
6145 * btree cursor to maintain our lock on the bmapbt between calls.
6146 */
6147int
6148xfs_bmap_finish_one(
6149 struct xfs_trans *tp,
6150 struct xfs_defer_ops *dfops,
6151 struct xfs_inode *ip,
6152 enum xfs_bmap_intent_type type,
6153 int whichfork,
6154 xfs_fileoff_t startoff,
6155 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006156 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006157 xfs_exntst_t state)
6158{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006159 xfs_fsblock_t firstfsb;
6160 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006161
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006162 /*
6163 * firstfsb is tied to the transaction lifetime and is used to
6164 * ensure correct AG locking order and schedule work item
6165 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6166 * to only making one bmap call per transaction, so it should
6167 * be safe to have it as a local variable here.
6168 */
6169 firstfsb = NULLFSBLOCK;
6170
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006171 trace_xfs_bmap_deferred(tp->t_mountp,
6172 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6173 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006174 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006175
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006176 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006177 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006178
6179 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006180 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006181 return -EIO;
6182
6183 switch (type) {
6184 case XFS_BMAP_MAP:
Brian Fosterff3edf22018-07-11 22:26:14 -07006185 ASSERT(dfops == tp->t_dfops);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006186 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Brian Fosterff3edf22018-07-11 22:26:14 -07006187 startblock, 0);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006188 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006189 break;
6190 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006191 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
Brian Fosterccd9d912018-07-11 22:26:13 -07006192 XFS_BMAPI_REMAP, 1, &firstfsb);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006193 break;
6194 default:
6195 ASSERT(0);
6196 error = -EFSCORRUPTED;
6197 }
6198
6199 return error;
6200}
Darrick J. Wong30b09842018-03-23 10:06:52 -07006201
6202/* Check that an inode's extent does not have invalid flags or bad ranges. */
6203xfs_failaddr_t
6204xfs_bmap_validate_extent(
6205 struct xfs_inode *ip,
6206 int whichfork,
6207 struct xfs_bmbt_irec *irec)
6208{
6209 struct xfs_mount *mp = ip->i_mount;
6210 xfs_fsblock_t endfsb;
6211 bool isrt;
6212
6213 isrt = XFS_IS_REALTIME_INODE(ip);
6214 endfsb = irec->br_startblock + irec->br_blockcount - 1;
6215 if (isrt) {
6216 if (!xfs_verify_rtbno(mp, irec->br_startblock))
6217 return __this_address;
6218 if (!xfs_verify_rtbno(mp, endfsb))
6219 return __this_address;
6220 } else {
6221 if (!xfs_verify_fsbno(mp, irec->br_startblock))
6222 return __this_address;
6223 if (!xfs_verify_fsbno(mp, endfsb))
6224 return __this_address;
6225 if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) !=
6226 XFS_FSB_TO_AGNO(mp, endfsb))
6227 return __this_address;
6228 }
6229 if (irec->br_state != XFS_EXT_NORM) {
6230 if (whichfork != XFS_DATA_FORK)
6231 return __this_address;
6232 if (!xfs_sb_version_hasextflgbit(&mp->m_sb))
6233 return __this_address;
6234 }
6235 return NULL;
6236}