blob: 3e5e9a667bc87ffc57740eb392156cfd7ce0d3a7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_sb.h"
Dave Chinnerf5ea1102013-04-24 18:58:02 +100026#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100027#include "xfs_defer.h"
Dave Chinner57062782013-10-15 09:17:51 +110028#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_da_btree.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100030#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_btree.h"
Dave Chinner239880e2013-10-23 10:50:10 +110033#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_extfree_item.h"
36#include "xfs_alloc.h"
37#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100038#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110039#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_rtalloc.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070041#include "xfs_errortag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_quota.h"
44#include "xfs_trans_space.h"
45#include "xfs_buf_item.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000046#include "xfs_trace.h"
Dave Chinner19de7352013-04-03 16:11:18 +110047#include "xfs_symlink.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110048#include "xfs_attr_leaf.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110049#include "xfs_filestream.h"
Darrick J. Wong340785c2016-08-03 11:33:42 +100050#include "xfs_rmap.h"
Darrick J. Wong3fd129b2016-09-19 10:30:52 +100051#include "xfs_ag_resv.h"
Darrick J. Wong62aab202016-10-03 09:11:23 -070052#include "xfs_refcount.h"
Brian Foster974ae922016-11-28 14:57:42 +110053#include "xfs_icache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056kmem_zone_t *xfs_bmap_free_item_zone;
57
58/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110059 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 */
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110063 * Compute and fill in the value of the maximum depth of a bmap btree
64 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
Dave Chinner9e5987a2013-02-25 12:31:26 +110066void
67xfs_bmap_compute_maxlevels(
68 xfs_mount_t *mp, /* file system mount structure */
69 int whichfork) /* data or attr fork */
70{
71 int level; /* btree level */
72 uint maxblocks; /* max blocks at this level */
73 uint maxleafents; /* max leaf entries possible */
74 int maxrootrecs; /* max records in root block */
75 int minleafrecs; /* min records in leaf block */
76 int minnoderecs; /* min records in node block */
77 int sz; /* root block size */
78
79 /*
80 * The maximum number of extents in a file, hence the maximum
81 * number of leaf entries, is controlled by the type of di_nextents
82 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
83 * (a signed 16-bit number, xfs_aextnum_t).
84 *
85 * Note that we can no longer assume that if we are in ATTR1 that
86 * the fork offset of all the inodes will be
87 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
88 * with ATTR2 and then mounted back with ATTR1, keeping the
89 * di_forkoff's fixed but probably at various positions. Therefore,
90 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
91 * of a minimum size available.
92 */
93 if (whichfork == XFS_DATA_FORK) {
94 maxleafents = MAXEXTNUM;
95 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
96 } else {
97 maxleafents = MAXAEXTNUM;
98 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
99 }
Eric Sandeen152d93b2014-04-14 18:58:51 +1000100 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100101 minleafrecs = mp->m_bmap_dmnr[0];
102 minnoderecs = mp->m_bmap_dmnr[1];
103 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
104 for (level = 1; maxblocks > 1; level++) {
105 if (maxblocks <= maxrootrecs)
106 maxblocks = 1;
107 else
108 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
109 }
110 mp->m_bm_maxlevels[whichfork] = level;
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100113STATIC int /* error */
114xfs_bmbt_lookup_eq(
115 struct xfs_btree_cur *cur,
Christoph Hellwige16cf9b2017-10-17 14:16:26 -0700116 struct xfs_bmbt_irec *irec,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100117 int *stat) /* success/failure */
118{
Christoph Hellwige16cf9b2017-10-17 14:16:26 -0700119 cur->bc_rec.b = *irec;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100120 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
121}
122
123STATIC int /* error */
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700124xfs_bmbt_lookup_first(
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100125 struct xfs_btree_cur *cur,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100126 int *stat) /* success/failure */
127{
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700128 cur->bc_rec.b.br_startoff = 0;
129 cur->bc_rec.b.br_startblock = 0;
130 cur->bc_rec.b.br_blockcount = 0;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100131 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
132}
133
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100134/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000135 * Check if the inode needs to be converted to btree format.
136 */
137static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
138{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700139 return whichfork != XFS_COW_FORK &&
140 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000141 XFS_IFORK_NEXTENTS(ip, whichfork) >
142 XFS_IFORK_MAXEXT(ip, whichfork);
143}
144
145/*
146 * Check if the inode should be converted to extent format.
147 */
148static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
149{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700150 return whichfork != XFS_COW_FORK &&
151 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000152 XFS_IFORK_NEXTENTS(ip, whichfork) <=
153 XFS_IFORK_MAXEXT(ip, whichfork);
154}
155
156/*
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700157 * Update the record referred to by cur to the value given by irec
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100158 * This either works (return 0) or gets an EFSCORRUPTED error.
159 */
160STATIC int
161xfs_bmbt_update(
162 struct xfs_btree_cur *cur,
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700163 struct xfs_bmbt_irec *irec)
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100164{
165 union xfs_btree_rec rec;
166
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700167 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100168 return xfs_btree_update(cur, &rec);
169}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100172 * Compute the worst-case number of indirect blocks that will be used
173 * for ip's delayed extent of length "len".
174 */
175STATIC xfs_filblks_t
176xfs_bmap_worst_indlen(
177 xfs_inode_t *ip, /* incore inode pointer */
178 xfs_filblks_t len) /* delayed extent length */
179{
180 int level; /* btree level number */
181 int maxrecs; /* maximum record count at this level */
182 xfs_mount_t *mp; /* mount structure */
183 xfs_filblks_t rval; /* return value */
184
185 mp = ip->i_mount;
186 maxrecs = mp->m_bmap_dmxr[0];
187 for (level = 0, rval = 0;
188 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
189 level++) {
190 len += maxrecs - 1;
191 do_div(len, maxrecs);
192 rval += len;
Darrick J. Wong5e5c9432017-09-18 09:41:17 -0700193 if (len == 1)
194 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100195 level - 1;
196 if (level == 0)
197 maxrecs = mp->m_bmap_dmxr[1];
198 }
199 return rval;
200}
201
202/*
203 * Calculate the default attribute fork offset for newly created inodes.
204 */
205uint
206xfs_default_attroffset(
207 struct xfs_inode *ip)
208{
209 struct xfs_mount *mp = ip->i_mount;
210 uint offset;
211
212 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100213 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100214 XFS_BMDR_SPACE_CALC(MINABTPTRS);
215 } else {
216 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
217 }
218
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100219 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100220 return offset;
221}
222
223/*
224 * Helper routine to reset inode di_forkoff field when switching
225 * attribute fork from local to extent format - we reset it where
226 * possible to make space available for inline data fork extents.
227 */
228STATIC void
229xfs_bmap_forkoff_reset(
Dave Chinner9e5987a2013-02-25 12:31:26 +1100230 xfs_inode_t *ip,
231 int whichfork)
232{
233 if (whichfork == XFS_ATTR_FORK &&
234 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
Dave Chinner9e5987a2013-02-25 12:31:26 +1100235 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
236 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
237
238 if (dfl_forkoff > ip->i_d.di_forkoff)
239 ip->i_d.di_forkoff = dfl_forkoff;
240 }
241}
242
Dave Chinner9e5987a2013-02-25 12:31:26 +1100243#ifdef DEBUG
244STATIC struct xfs_buf *
245xfs_bmap_get_bp(
246 struct xfs_btree_cur *cur,
247 xfs_fsblock_t bno)
248{
Dave Chinnere6631f82018-05-09 07:49:37 -0700249 struct xfs_log_item *lip;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100250 int i;
251
252 if (!cur)
253 return NULL;
254
255 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
256 if (!cur->bc_bufs[i])
257 break;
258 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
259 return cur->bc_bufs[i];
260 }
261
262 /* Chase down all the log items to see if the bp is there */
Dave Chinnere6631f82018-05-09 07:49:37 -0700263 list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
264 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
265
Dave Chinner9e5987a2013-02-25 12:31:26 +1100266 if (bip->bli_item.li_type == XFS_LI_BUF &&
267 XFS_BUF_ADDR(bip->bli_buf) == bno)
268 return bip->bli_buf;
269 }
270
271 return NULL;
272}
273
274STATIC void
275xfs_check_block(
276 struct xfs_btree_block *block,
277 xfs_mount_t *mp,
278 int root,
279 short sz)
280{
281 int i, j, dmxr;
282 __be64 *pp, *thispa; /* pointer to block address */
283 xfs_bmbt_key_t *prevp, *keyp;
284
285 ASSERT(be16_to_cpu(block->bb_level) > 0);
286
287 prevp = NULL;
288 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
289 dmxr = mp->m_bmap_dmxr[0];
290 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
291
292 if (prevp) {
293 ASSERT(be64_to_cpu(prevp->br_startoff) <
294 be64_to_cpu(keyp->br_startoff));
295 }
296 prevp = keyp;
297
298 /*
299 * Compare the block numbers to see if there are dups.
300 */
301 if (root)
302 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
303 else
304 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
305
306 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
307 if (root)
308 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
309 else
310 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
311 if (*thispa == *pp) {
312 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
313 __func__, j, i,
314 (unsigned long long)be64_to_cpu(*thispa));
Darrick J. Wongcec57252018-05-04 15:31:21 -0700315 xfs_err(mp, "%s: ptrs are equal in node\n",
Dave Chinner9e5987a2013-02-25 12:31:26 +1100316 __func__);
Darrick J. Wongcec57252018-05-04 15:31:21 -0700317 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100318 }
319 }
320 }
321}
322
323/*
324 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100325 * btree leaves. THis becomes prohibitively expensive for large extent count
326 * files, so don't bother with inodes that have more than 10,000 extents in
327 * them. The btree record ordering checks will still be done, so for such large
328 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a2013-02-25 12:31:26 +1100329 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100330STATIC void
331xfs_bmap_check_leaf_extents(
332 xfs_btree_cur_t *cur, /* btree cursor or null */
333 xfs_inode_t *ip, /* incore inode pointer */
334 int whichfork) /* data or attr fork */
335{
336 struct xfs_btree_block *block; /* current btree block */
337 xfs_fsblock_t bno; /* block # of "block" */
338 xfs_buf_t *bp; /* buffer for "block" */
339 int error; /* error return value */
340 xfs_extnum_t i=0, j; /* index into the extents list */
341 xfs_ifork_t *ifp; /* fork structure */
342 int level; /* btree level, for checking */
343 xfs_mount_t *mp; /* file system mount structure */
344 __be64 *pp; /* pointer to block address */
345 xfs_bmbt_rec_t *ep; /* pointer to current extent */
346 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
347 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
348 int bp_release = 0;
349
350 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
351 return;
352 }
353
Dave Chinnere3543812016-01-08 11:28:49 +1100354 /* skip large extent count inodes */
355 if (ip->i_d.di_nextents > 10000)
356 return;
357
Dave Chinner9e5987a2013-02-25 12:31:26 +1100358 bno = NULLFSBLOCK;
359 mp = ip->i_mount;
360 ifp = XFS_IFORK_PTR(ip, whichfork);
361 block = ifp->if_broot;
362 /*
363 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
364 */
365 level = be16_to_cpu(block->bb_level);
366 ASSERT(level > 0);
367 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
368 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
369 bno = be64_to_cpu(*pp);
370
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000371 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100372 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
373 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
374
375 /*
376 * Go down the tree until leaf level is reached, following the first
377 * pointer (leftmost) at each level.
378 */
379 while (level-- > 0) {
380 /* See if buf is in cur first */
381 bp_release = 0;
382 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
383 if (!bp) {
384 bp_release = 1;
385 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
386 XFS_BMAP_BTREE_REF,
387 &xfs_bmbt_buf_ops);
388 if (error)
389 goto error_norelse;
390 }
391 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100392 if (level == 0)
393 break;
394
395 /*
396 * Check this block for basic sanity (increasing keys and
397 * no duplicate blocks).
398 */
399
400 xfs_check_block(block, mp, 0, 0);
401 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
402 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100403 XFS_WANT_CORRUPTED_GOTO(mp,
Darrick J. Wong59f6fec2018-01-08 10:51:00 -0800404 xfs_verify_fsbno(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100405 if (bp_release) {
406 bp_release = 0;
407 xfs_trans_brelse(NULL, bp);
408 }
409 }
410
411 /*
412 * Here with bp and block set to the leftmost leaf node in the tree.
413 */
414 i = 0;
415
416 /*
417 * Loop over all leaf nodes checking that all extents are in the right order.
418 */
419 for (;;) {
420 xfs_fsblock_t nextbno;
421 xfs_extnum_t num_recs;
422
423
424 num_recs = xfs_btree_get_numrecs(block);
425
426 /*
427 * Read-ahead the next leaf block, if any.
428 */
429
430 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
431
432 /*
433 * Check all the extents to make sure they are OK.
434 * If we had a previous block, the last entry should
435 * conform with the first entry in this one.
436 */
437
438 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
439 if (i) {
440 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
441 xfs_bmbt_disk_get_blockcount(&last) <=
442 xfs_bmbt_disk_get_startoff(ep));
443 }
444 for (j = 1; j < num_recs; j++) {
445 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
446 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
447 xfs_bmbt_disk_get_blockcount(ep) <=
448 xfs_bmbt_disk_get_startoff(nextp));
449 ep = nextp;
450 }
451
452 last = *ep;
453 i += num_recs;
454 if (bp_release) {
455 bp_release = 0;
456 xfs_trans_brelse(NULL, bp);
457 }
458 bno = nextbno;
459 /*
460 * If we've reached the end, stop.
461 */
462 if (bno == NULLFSBLOCK)
463 break;
464
465 bp_release = 0;
466 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
467 if (!bp) {
468 bp_release = 1;
469 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
470 XFS_BMAP_BTREE_REF,
471 &xfs_bmbt_buf_ops);
472 if (error)
473 goto error_norelse;
474 }
475 block = XFS_BUF_TO_BLOCK(bp);
476 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100477
Dave Chinner9e5987a2013-02-25 12:31:26 +1100478 return;
479
480error0:
481 xfs_warn(mp, "%s: at error0", __func__);
482 if (bp_release)
483 xfs_trans_brelse(NULL, bp);
484error_norelse:
485 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
486 __func__, i);
Darrick J. Wongcec57252018-05-04 15:31:21 -0700487 xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
488 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100489 return;
490}
491
492/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100493 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000494 * given the caller's original parameters. Specifically check the
495 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a2013-02-25 12:31:26 +1100496 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
497 */
498STATIC void
499xfs_bmap_validate_ret(
500 xfs_fileoff_t bno,
501 xfs_filblks_t len,
502 int flags,
503 xfs_bmbt_irec_t *mval,
504 int nmap,
505 int ret_nmap)
506{
507 int i; /* index to map values */
508
509 ASSERT(ret_nmap <= nmap);
510
511 for (i = 0; i < ret_nmap; i++) {
512 ASSERT(mval[i].br_blockcount > 0);
513 if (!(flags & XFS_BMAPI_ENTIRE)) {
514 ASSERT(mval[i].br_startoff >= bno);
515 ASSERT(mval[i].br_blockcount <= len);
516 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
517 bno + len);
518 } else {
519 ASSERT(mval[i].br_startoff < bno + len);
520 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
521 bno);
522 }
523 ASSERT(i == 0 ||
524 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
525 mval[i].br_startoff);
526 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
527 mval[i].br_startblock != HOLESTARTBLOCK);
528 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
529 mval[i].br_state == XFS_EXT_UNWRITTEN);
530 }
531}
532
533#else
534#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
Darrick J. Wong7bf7a192017-08-31 15:11:06 -0700535#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100536#endif /* DEBUG */
537
538/*
539 * bmap free list manipulation functions
540 */
541
542/*
543 * Add the extent to the list of extents to be free at transaction end.
544 * The list is maintained sorted (by block number).
545 */
546void
547xfs_bmap_add_free(
Darrick J. Wong340785c2016-08-03 11:33:42 +1000548 struct xfs_mount *mp,
549 struct xfs_defer_ops *dfops,
550 xfs_fsblock_t bno,
551 xfs_filblks_t len,
552 struct xfs_owner_info *oinfo)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100553{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000554 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100555#ifdef DEBUG
556 xfs_agnumber_t agno;
557 xfs_agblock_t agbno;
558
559 ASSERT(bno != NULLFSBLOCK);
560 ASSERT(len > 0);
561 ASSERT(len <= MAXEXTLEN);
562 ASSERT(!isnullstartblock(bno));
563 agno = XFS_FSB_TO_AGNO(mp, bno);
564 agbno = XFS_FSB_TO_AGBNO(mp, bno);
565 ASSERT(agno < mp->m_sb.sb_agcount);
566 ASSERT(agbno < mp->m_sb.sb_agblocks);
567 ASSERT(len < mp->m_sb.sb_agblocks);
568 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
569#endif
570 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000571
Dave Chinner9e5987a2013-02-25 12:31:26 +1100572 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000573 new->xefi_startblock = bno;
574 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000575 if (oinfo)
576 new->xefi_oinfo = *oinfo;
577 else
578 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
Darrick J. Wongba9e7802016-08-03 11:26:33 +1000579 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
580 XFS_FSB_TO_AGBNO(mp, bno), len);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000581 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100582}
583
584/*
585 * Inode fork format manipulation functions
586 */
587
588/*
589 * Transform a btree format file with only one leaf node, where the
590 * extents list will fit in the inode, into an extents format file.
591 * Since the file extents are already in-core, all we have to do is
592 * give up the space for the btree root and pitch the leaf block.
593 */
594STATIC int /* error */
595xfs_bmap_btree_to_extents(
596 xfs_trans_t *tp, /* transaction pointer */
597 xfs_inode_t *ip, /* incore inode pointer */
598 xfs_btree_cur_t *cur, /* btree cursor */
599 int *logflagsp, /* inode logging flags */
600 int whichfork) /* data or attr fork */
601{
602 /* REFERENCED */
603 struct xfs_btree_block *cblock;/* child btree block */
604 xfs_fsblock_t cbno; /* child block number */
605 xfs_buf_t *cbp; /* child block's buffer */
606 int error; /* error return value */
607 xfs_ifork_t *ifp; /* inode fork data */
608 xfs_mount_t *mp; /* mount point structure */
609 __be64 *pp; /* ptr to block address */
610 struct xfs_btree_block *rblock;/* root btree block */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000611 struct xfs_owner_info oinfo;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100612
613 mp = ip->i_mount;
614 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700615 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100616 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
617 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
618 rblock = ifp->if_broot;
619 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
620 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
621 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
622 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
623 cbno = be64_to_cpu(*pp);
624 *logflagsp = 0;
625#ifdef DEBUG
Darrick J. Wongf1357612017-10-17 21:37:33 -0700626 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
627 xfs_btree_check_lptr(cur, cbno, 1));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100628#endif
629 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
630 &xfs_bmbt_buf_ops);
631 if (error)
632 return error;
633 cblock = XFS_BUF_TO_BLOCK(cbp);
634 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
635 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000636 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
637 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100638 ip->i_d.di_nblocks--;
639 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
640 xfs_trans_binval(tp, cbp);
641 if (cur->bc_bufs[0] == cbp)
642 cur->bc_bufs[0] = NULL;
643 xfs_iroot_realloc(ip, -1, whichfork);
644 ASSERT(ifp->if_broot == NULL);
645 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
646 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
647 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
648 return 0;
649}
650
651/*
652 * Convert an extents-format file into a btree-format file.
653 * The new file will have a root block (in the inode) and a single child block.
654 */
655STATIC int /* error */
656xfs_bmap_extents_to_btree(
657 xfs_trans_t *tp, /* transaction pointer */
658 xfs_inode_t *ip, /* incore inode pointer */
659 xfs_fsblock_t *firstblock, /* first-block-allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000660 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100661 xfs_btree_cur_t **curp, /* cursor returned to caller */
662 int wasdel, /* converting a delayed alloc */
663 int *logflagsp, /* inode logging flags */
664 int whichfork) /* data or attr fork */
665{
666 struct xfs_btree_block *ablock; /* allocated (child) bt block */
667 xfs_buf_t *abp; /* buffer for ablock */
668 xfs_alloc_arg_t args; /* allocation arguments */
669 xfs_bmbt_rec_t *arp; /* child record pointer */
670 struct xfs_btree_block *block; /* btree root block */
671 xfs_btree_cur_t *cur; /* bmap btree cursor */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100672 int error; /* error return value */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100673 xfs_ifork_t *ifp; /* inode fork pointer */
674 xfs_bmbt_key_t *kp; /* root block key pointer */
675 xfs_mount_t *mp; /* mount structure */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100676 xfs_bmbt_ptr_t *pp; /* root block address pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700677 struct xfs_iext_cursor icur;
Christoph Hellwig906abed2017-11-03 10:34:43 -0700678 struct xfs_bmbt_irec rec;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700679 xfs_extnum_t cnt = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100680
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500681 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700682 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100683 ifp = XFS_IFORK_PTR(ip, whichfork);
684 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
685
686 /*
687 * Make space in the inode incore.
688 */
689 xfs_iroot_realloc(ip, 1, whichfork);
690 ifp->if_flags |= XFS_IFBROOT;
691
692 /*
693 * Fill in the root.
694 */
695 block = ifp->if_broot;
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800696 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
697 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
Eric Sandeenf88ae462017-01-27 23:16:37 -0800698 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100699 /*
700 * Need a cursor. Can't allocate until bb_level is filled in.
701 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100702 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
703 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000704 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100705 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
706 /*
707 * Convert to a btree with two levels, one record in root.
708 */
709 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
710 memset(&args, 0, sizeof(args));
711 args.tp = tp;
712 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000713 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100714 args.firstblock = *firstblock;
715 if (*firstblock == NULLFSBLOCK) {
716 args.type = XFS_ALLOCTYPE_START_BNO;
717 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000718 } else if (dfops->dop_low) {
Dave Chinner9e5987a2013-02-25 12:31:26 +1100719 args.type = XFS_ALLOCTYPE_START_BNO;
720 args.fsbno = *firstblock;
721 } else {
722 args.type = XFS_ALLOCTYPE_NEAR_BNO;
723 args.fsbno = *firstblock;
724 }
725 args.minlen = args.maxlen = args.prod = 1;
726 args.wasdel = wasdel;
727 *logflagsp = 0;
728 if ((error = xfs_alloc_vextent(&args))) {
729 xfs_iroot_realloc(ip, -1, whichfork);
Eric Sandeen2c4306f2018-04-16 23:07:27 -0700730 ASSERT(ifp->if_broot == NULL);
731 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100732 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
733 return error;
734 }
Darrick J. Wong90e20562016-10-03 09:11:45 -0700735
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800736 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
737 xfs_iroot_realloc(ip, -1, whichfork);
Eric Sandeen2c4306f2018-04-16 23:07:27 -0700738 ASSERT(ifp->if_broot == NULL);
739 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800740 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
741 return -ENOSPC;
742 }
Dave Chinner9e5987a2013-02-25 12:31:26 +1100743 /*
744 * Allocation can't fail, the space was reserved.
745 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100746 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -0800747 args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100748 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
749 cur->bc_private.b.allocated++;
750 ip->i_d.di_nblocks++;
751 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
752 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
753 /*
754 * Fill in the child block.
755 */
756 abp->b_ops = &xfs_bmbt_buf_ops;
757 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800758 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
759 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500760 XFS_BTREE_LONG_PTRS);
761
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700762 for_each_xfs_iext(ifp, &icur, &rec) {
Christoph Hellwig906abed2017-11-03 10:34:43 -0700763 if (isnullstartblock(rec.br_startblock))
764 continue;
765 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
766 xfs_bmbt_disk_set_all(arp, &rec);
767 cnt++;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100768 }
769 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
770 xfs_btree_set_numrecs(ablock, cnt);
771
772 /*
773 * Fill in the root key and pointer.
774 */
775 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
776 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
777 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
778 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
779 be16_to_cpu(block->bb_level)));
780 *pp = cpu_to_be64(args.fsbno);
781
782 /*
783 * Do all this logging at the end so that
784 * the root is at the right level.
785 */
786 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
787 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
788 ASSERT(*curp == NULL);
789 *curp = cur;
790 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
791 return 0;
792}
793
794/*
795 * Convert a local file to an extents file.
796 * This code is out of bounds for data forks of regular files,
797 * since the file data needs to get logged so things will stay consistent.
798 * (The bmap-level manipulations are ok, though).
799 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000800void
801xfs_bmap_local_to_extents_empty(
802 struct xfs_inode *ip,
803 int whichfork)
804{
805 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
806
Darrick J. Wong60b49842016-10-03 09:11:34 -0700807 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000808 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
809 ASSERT(ifp->if_bytes == 0);
810 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
811
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000812 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000813 ifp->if_flags &= ~XFS_IFINLINE;
814 ifp->if_flags |= XFS_IFEXTENTS;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700815 ifp->if_u1.if_root = NULL;
816 ifp->if_height = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000817 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
818}
819
820
Dave Chinner9e5987a2013-02-25 12:31:26 +1100821STATIC int /* error */
822xfs_bmap_local_to_extents(
823 xfs_trans_t *tp, /* transaction pointer */
824 xfs_inode_t *ip, /* incore inode pointer */
825 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
826 xfs_extlen_t total, /* total blocks needed by transaction */
827 int *logflagsp, /* inode logging flags */
828 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500829 void (*init_fn)(struct xfs_trans *tp,
830 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100831 struct xfs_inode *ip,
832 struct xfs_ifork *ifp))
833{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000834 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100835 int flags; /* logging flags returned */
836 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000837 xfs_alloc_arg_t args; /* allocation arguments */
838 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700839 struct xfs_bmbt_irec rec;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700840 struct xfs_iext_cursor icur;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100841
842 /*
843 * We don't want to deal with the case of keeping inode data inline yet.
844 * So sending the data fork of a regular inode is invalid.
845 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100846 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100847 ifp = XFS_IFORK_PTR(ip, whichfork);
848 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000849
850 if (!ifp->if_bytes) {
851 xfs_bmap_local_to_extents_empty(ip, whichfork);
852 flags = XFS_ILOG_CORE;
853 goto done;
854 }
855
Dave Chinner9e5987a2013-02-25 12:31:26 +1100856 flags = 0;
857 error = 0;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700858 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000859 memset(&args, 0, sizeof(args));
860 args.tp = tp;
861 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000862 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000863 args.firstblock = *firstblock;
864 /*
865 * Allocate a block. We know we need only one, since the
866 * file currently fits in an inode.
867 */
868 if (*firstblock == NULLFSBLOCK) {
869 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
870 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100871 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000872 args.fsbno = *firstblock;
873 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100874 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000875 args.total = total;
876 args.minlen = args.maxlen = args.prod = 1;
877 error = xfs_alloc_vextent(&args);
878 if (error)
879 goto done;
880
881 /* Can't fail, the space was reserved. */
882 ASSERT(args.fsbno != NULLFSBLOCK);
883 ASSERT(args.len == 1);
884 *firstblock = args.fsbno;
885 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
886
Dave Chinnerfe22d552015-01-22 09:30:06 +1100887 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100888 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100889 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100890 * The callout is responsible for logging because the remote format
891 * might differ from the local format and thus we don't know how much to
892 * log here. Note that init_fn must also set the buffer log item type
893 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100894 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000895 init_fn(tp, bp, ip, ifp);
896
Brian Fosterb7cdc662015-10-12 15:40:24 +1100897 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000898 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
899 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100900 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000901
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700902 ifp->if_u1.if_root = NULL;
903 ifp->if_height = 0;
904
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700905 rec.br_startoff = 0;
906 rec.br_startblock = args.fsbno;
907 rec.br_blockcount = 1;
908 rec.br_state = XFS_EXT_NORM;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700909 xfs_iext_first(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -0700910 xfs_iext_insert(ip, &icur, &rec, 0);
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700911
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000912 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
913 ip->i_d.di_nblocks = 1;
914 xfs_trans_mod_dquot_byino(tp, ip,
915 XFS_TRANS_DQ_BCOUNT, 1L);
916 flags |= xfs_ilog_fext(whichfork);
917
Dave Chinner9e5987a2013-02-25 12:31:26 +1100918done:
919 *logflagsp = flags;
920 return error;
921}
922
923/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 * Called from xfs_bmap_add_attrfork to handle btree format files.
925 */
926STATIC int /* error */
927xfs_bmap_add_attrfork_btree(
928 xfs_trans_t *tp, /* transaction pointer */
929 xfs_inode_t *ip, /* incore inode pointer */
930 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000931 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 int *flags) /* inode logging flags */
933{
934 xfs_btree_cur_t *cur; /* btree cursor */
935 int error; /* error return value */
936 xfs_mount_t *mp; /* file system mount struct */
937 int stat; /* newroot status */
938
939 mp = ip->i_mount;
940 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
941 *flags |= XFS_ILOG_DBROOT;
942 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100943 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000944 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 cur->bc_private.b.firstblock = *firstblock;
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700946 error = xfs_bmbt_lookup_first(cur, &stat);
947 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000949 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100950 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100951 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 goto error0;
953 if (stat == 0) {
954 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000955 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957 *firstblock = cur->bc_private.b.firstblock;
958 cur->bc_private.b.allocated = 0;
959 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
960 }
961 return 0;
962error0:
963 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
964 return error;
965}
966
967/*
968 * Called from xfs_bmap_add_attrfork to handle extents format files.
969 */
970STATIC int /* error */
971xfs_bmap_add_attrfork_extents(
972 xfs_trans_t *tp, /* transaction pointer */
973 xfs_inode_t *ip, /* incore inode pointer */
974 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000975 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 int *flags) /* inode logging flags */
977{
978 xfs_btree_cur_t *cur; /* bmap btree cursor */
979 int error; /* error return value */
980
981 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
982 return 0;
983 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000984 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 flags, XFS_DATA_FORK);
986 if (cur) {
987 cur->bc_private.b.allocated = 0;
988 xfs_btree_del_cursor(cur,
989 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
990 }
991 return error;
992}
993
994/*
Dave Chinner1e823792013-02-11 15:58:13 +1100995 * Called from xfs_bmap_add_attrfork to handle local format files. Each
996 * different data fork content type needs a different callout to do the
997 * conversion. Some are basic and only require special block initialisation
998 * callouts for the data formating, others (directories) are so specialised they
999 * handle everything themselves.
1000 *
1001 * XXX (dgc): investigate whether directory conversion can use the generic
1002 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001003 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 */
1005STATIC int /* error */
1006xfs_bmap_add_attrfork_local(
1007 xfs_trans_t *tp, /* transaction pointer */
1008 xfs_inode_t *ip, /* incore inode pointer */
1009 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001010 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 int *flags) /* inode logging flags */
1012{
1013 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1016 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001017
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001018 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001020 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 dargs.dp = ip;
1022 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001023 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001024 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 dargs.whichfork = XFS_DATA_FORK;
1026 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001027 return xfs_dir2_sf_to_block(&dargs);
1028 }
1029
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001030 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001031 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1032 flags, XFS_DATA_FORK,
1033 xfs_symlink_local_to_remote);
1034
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001035 /* should only be called for types that support local format data */
1036 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001037 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
1040/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001041 * Convert inode from non-attributed to attributed.
1042 * Must not be in a transaction, ip must not be locked.
1043 */
1044int /* error code */
1045xfs_bmap_add_attrfork(
1046 xfs_inode_t *ip, /* incore inode pointer */
1047 int size, /* space new attribute needs */
1048 int rsvd) /* xact may use reserved blks */
1049{
1050 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001051 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001052 xfs_mount_t *mp; /* mount structure */
1053 xfs_trans_t *tp; /* transaction pointer */
1054 int blks; /* space reservation */
1055 int version = 1; /* superblock attr version */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001056 int logflags; /* logging flags */
1057 int error; /* error return value */
1058
1059 ASSERT(XFS_IFORK_Q(ip) == 0);
1060
1061 mp = ip->i_mount;
1062 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001063
Dave Chinner9e5987a2013-02-25 12:31:26 +11001064 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001065
1066 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1067 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1068 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001069 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001070
Dave Chinner9e5987a2013-02-25 12:31:26 +11001071 xfs_ilock(ip, XFS_ILOCK_EXCL);
1072 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1073 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1074 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001075 if (error)
1076 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001077 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001078 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001079 if (ip->i_d.di_anextents != 0) {
1080 error = -EFSCORRUPTED;
1081 goto trans_cancel;
1082 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001083 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1084 /*
1085 * For inodes coming from pre-6.2 filesystems.
1086 */
1087 ASSERT(ip->i_d.di_aformat == 0);
1088 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1089 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001090
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001091 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001092 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1093
1094 switch (ip->i_d.di_format) {
1095 case XFS_DINODE_FMT_DEV:
1096 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1097 break;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001098 case XFS_DINODE_FMT_LOCAL:
1099 case XFS_DINODE_FMT_EXTENTS:
1100 case XFS_DINODE_FMT_BTREE:
1101 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1102 if (!ip->i_d.di_forkoff)
1103 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1104 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1105 version = 2;
1106 break;
1107 default:
1108 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001109 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001110 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001111 }
1112
1113 ASSERT(ip->i_afp == NULL);
1114 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1115 ip->i_afp->if_flags = XFS_IFEXTENTS;
1116 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001117 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001118 switch (ip->i_d.di_format) {
1119 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001120 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001121 &logflags);
1122 break;
1123 case XFS_DINODE_FMT_EXTENTS:
1124 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001125 &dfops, &logflags);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001126 break;
1127 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001128 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001129 &logflags);
1130 break;
1131 default:
1132 error = 0;
1133 break;
1134 }
1135 if (logflags)
1136 xfs_trans_log_inode(tp, ip, logflags);
1137 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001138 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001139 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1140 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001141 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001142
1143 spin_lock(&mp->m_sb_lock);
1144 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1145 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001146 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001147 }
1148 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1149 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001150 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001151 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001152 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001153 if (log_sb)
1154 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001155 }
1156
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001157 error = xfs_defer_finish(&tp, &dfops);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001158 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001159 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001160 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001161 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001162 return error;
1163
1164bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001165 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001166trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001167 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001168 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001169 return error;
1170}
1171
1172/*
1173 * Internal and external extent tree search functions.
1174 */
1175
1176/*
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001177 * Read in extents from a btree-format inode.
Dave Chinner9e5987a2013-02-25 12:31:26 +11001178 */
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001179int
1180xfs_iread_extents(
1181 struct xfs_trans *tp,
1182 struct xfs_inode *ip,
1183 int whichfork)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001184{
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001185 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwige8e0e172017-10-19 11:06:29 -07001186 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001187 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1188 xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
1189 struct xfs_btree_block *block = ifp->if_broot;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001190 struct xfs_iext_cursor icur;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -07001191 struct xfs_bmbt_irec new;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001192 xfs_fsblock_t bno;
1193 struct xfs_buf *bp;
1194 xfs_extnum_t i, j;
1195 int level;
1196 __be64 *pp;
1197 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001198
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001199 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1200
1201 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1202 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1203 return -EFSCORRUPTED;
1204 }
1205
Dave Chinner9e5987a2013-02-25 12:31:26 +11001206 /*
1207 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1208 */
1209 level = be16_to_cpu(block->bb_level);
1210 ASSERT(level > 0);
1211 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1212 bno = be64_to_cpu(*pp);
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -08001213
Dave Chinner9e5987a2013-02-25 12:31:26 +11001214 /*
1215 * Go down the tree until leaf level is reached, following the first
1216 * pointer (leftmost) at each level.
1217 */
1218 while (level-- > 0) {
1219 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1220 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1221 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001222 goto out;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001223 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001224 if (level == 0)
1225 break;
1226 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1227 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001228 XFS_WANT_CORRUPTED_GOTO(mp,
Darrick J. Wong59f6fec2018-01-08 10:51:00 -08001229 xfs_verify_fsbno(mp, bno), out_brelse);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001230 xfs_trans_brelse(tp, bp);
1231 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001232
Dave Chinner9e5987a2013-02-25 12:31:26 +11001233 /*
1234 * Here with bp and block set to the leftmost leaf node in the tree.
1235 */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001236 i = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001237 xfs_iext_first(ifp, &icur);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001238
Dave Chinner9e5987a2013-02-25 12:31:26 +11001239 /*
1240 * Loop over all leaf nodes. Copy information to the extent records.
1241 */
1242 for (;;) {
1243 xfs_bmbt_rec_t *frp;
1244 xfs_fsblock_t nextbno;
1245 xfs_extnum_t num_recs;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001246
1247 num_recs = xfs_btree_get_numrecs(block);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001248 if (unlikely(i + num_recs > nextents)) {
1249 ASSERT(i + num_recs <= nextents);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001250 xfs_warn(ip->i_mount,
1251 "corrupt dinode %Lu, (btree extents).",
1252 (unsigned long long) ip->i_ino);
Darrick J. Wong90a58f92018-03-23 10:06:52 -07001253 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1254 __func__, block, sizeof(*block),
1255 __this_address);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001256 error = -EFSCORRUPTED;
1257 goto out_brelse;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001258 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001259 /*
1260 * Read-ahead the next leaf block, if any.
1261 */
1262 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1263 if (nextbno != NULLFSBLOCK)
1264 xfs_btree_reada_bufl(mp, nextbno, 1,
1265 &xfs_bmbt_buf_ops);
1266 /*
1267 * Copy records into the extent records.
1268 */
1269 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Christoph Hellwig6bdcf262017-11-03 10:34:46 -07001270 for (j = 0; j < num_recs; j++, frp++, i++) {
Darrick J. Wong30b09842018-03-23 10:06:52 -07001271 xfs_failaddr_t fa;
1272
Christoph Hellwigdac9c9b2017-11-03 10:34:47 -07001273 xfs_bmbt_disk_get_all(frp, &new);
Darrick J. Wong30b09842018-03-23 10:06:52 -07001274 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1275 if (fa) {
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001276 error = -EFSCORRUPTED;
Darrick J. Wong30b09842018-03-23 10:06:52 -07001277 xfs_inode_verifier_error(ip, error,
1278 "xfs_iread_extents(2)",
1279 frp, sizeof(*frp), fa);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001280 goto out_brelse;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001281 }
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001282 xfs_iext_insert(ip, &icur, &new, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001283 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
1284 xfs_iext_next(ifp, &icur);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001285 }
1286 xfs_trans_brelse(tp, bp);
1287 bno = nextbno;
1288 /*
1289 * If we've reached the end, stop.
1290 */
1291 if (bno == NULLFSBLOCK)
1292 break;
1293 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1294 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1295 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001296 goto out;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001297 block = XFS_BUF_TO_BLOCK(bp);
1298 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001299
1300 if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) {
1301 error = -EFSCORRUPTED;
1302 goto out;
1303 }
Eric Sandeen5d829302016-11-08 12:59:42 +11001304 ASSERT(i == xfs_iext_count(ifp));
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001305
1306 ifp->if_flags |= XFS_IFEXTENTS;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001307 return 0;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001308
1309out_brelse:
Dave Chinner9e5987a2013-02-25 12:31:26 +11001310 xfs_trans_brelse(tp, bp);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001311out:
1312 xfs_iext_destroy(ifp);
1313 return error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001314}
1315
Dave Chinner9e5987a2013-02-25 12:31:26 +11001316/*
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001317 * Returns the relative block number of the first unused block(s) in the given
1318 * fork with at least "len" logically contiguous blocks free. This is the
1319 * lowest-address hole if the fork has holes, else the first block past the end
1320 * of fork. Return 0 if the fork is currently local (in-inode).
Dave Chinner9e5987a2013-02-25 12:31:26 +11001321 */
1322int /* error */
1323xfs_bmap_first_unused(
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001324 struct xfs_trans *tp, /* transaction pointer */
1325 struct xfs_inode *ip, /* incore inode */
1326 xfs_extlen_t len, /* size of hole to find */
1327 xfs_fileoff_t *first_unused, /* unused block */
1328 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001329{
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001330 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1331 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001332 struct xfs_iext_cursor icur;
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001333 xfs_fileoff_t lastaddr = 0;
1334 xfs_fileoff_t lowest, max;
1335 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001336
1337 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1338 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1339 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001340
Dave Chinner9e5987a2013-02-25 12:31:26 +11001341 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1342 *first_unused = 0;
1343 return 0;
1344 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001345
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001346 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1347 error = xfs_iread_extents(tp, ip, whichfork);
1348 if (error)
1349 return error;
1350 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001351
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001352 lowest = max = *first_unused;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001353 for_each_xfs_iext(ifp, &icur, &got) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11001354 /*
1355 * See if the hole before this extent will work.
1356 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001357 if (got.br_startoff >= lowest + len &&
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001358 got.br_startoff - max >= len)
1359 break;
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001360 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001361 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1362 }
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001363
Dave Chinner9e5987a2013-02-25 12:31:26 +11001364 *first_unused = max;
1365 return 0;
1366}
1367
1368/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001369 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001370 * last_block (input value) in the file.
1371 * This is not based on i_size, it is based on the extent records.
1372 * Returns 0 for local files, as they do not have extent records.
1373 */
1374int /* error */
1375xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001376 struct xfs_trans *tp, /* transaction pointer */
1377 struct xfs_inode *ip, /* incore inode */
1378 xfs_fileoff_t *last_block, /* last block */
1379 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001380{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001381 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1382 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001383 struct xfs_iext_cursor icur;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001384 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001385
Christoph Hellwig86685f72016-11-24 11:39:38 +11001386 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1387 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a2013-02-25 12:31:26 +11001388 *last_block = 0;
1389 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001390 case XFS_DINODE_FMT_BTREE:
1391 case XFS_DINODE_FMT_EXTENTS:
1392 break;
1393 default:
1394 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001395 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001396
1397 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1398 error = xfs_iread_extents(tp, ip, whichfork);
1399 if (error)
1400 return error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001401 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001402
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001403 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
Christoph Hellwigdc560152017-10-23 16:32:39 -07001404 *last_block = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001405 return 0;
1406}
1407
Dave Chinner68988112013-08-12 20:49:42 +10001408int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001409xfs_bmap_last_extent(
1410 struct xfs_trans *tp,
1411 struct xfs_inode *ip,
1412 int whichfork,
1413 struct xfs_bmbt_irec *rec,
1414 int *is_empty)
1415{
1416 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001417 struct xfs_iext_cursor icur;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001418 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001419
1420 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1421 error = xfs_iread_extents(tp, ip, whichfork);
1422 if (error)
1423 return error;
1424 }
1425
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001426 xfs_iext_last(ifp, &icur);
1427 if (!xfs_iext_get_extent(ifp, &icur, rec))
Dave Chinner9e5987a2013-02-25 12:31:26 +11001428 *is_empty = 1;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001429 else
1430 *is_empty = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001431 return 0;
1432}
1433
1434/*
1435 * Check the last inode extent to determine whether this allocation will result
1436 * in blocks being allocated at the end of the file. When we allocate new data
1437 * blocks at the end of the file which do not start at the previous data block,
1438 * we will try to align the new blocks at stripe unit boundaries.
1439 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001440 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001441 * at, or past the EOF.
1442 */
1443STATIC int
1444xfs_bmap_isaeof(
1445 struct xfs_bmalloca *bma,
1446 int whichfork)
1447{
1448 struct xfs_bmbt_irec rec;
1449 int is_empty;
1450 int error;
1451
Thomas Meyer749f24f2017-10-09 11:38:54 -07001452 bma->aeof = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001453 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1454 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001455 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001456 return error;
1457
Dave Chinner6e708bc2013-11-22 10:41:16 +11001458 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001459 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001460 return 0;
1461 }
1462
Dave Chinner9e5987a2013-02-25 12:31:26 +11001463 /*
1464 * Check if we are allocation or past the last extent, or at least into
1465 * the last delayed allocated extent.
1466 */
1467 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1468 (bma->offset >= rec.br_startoff &&
1469 isnullstartblock(rec.br_startblock));
1470 return 0;
1471}
1472
1473/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001474 * Returns the file-relative block number of the first block past eof in
1475 * the file. This is not based on i_size, it is based on the extent records.
1476 * Returns 0 for local files, as they do not have extent records.
1477 */
1478int
1479xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001480 struct xfs_inode *ip,
1481 xfs_fileoff_t *last_block,
1482 int whichfork)
1483{
1484 struct xfs_bmbt_irec rec;
1485 int is_empty;
1486 int error;
1487
1488 *last_block = 0;
1489
1490 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1491 return 0;
1492
1493 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1494 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001495 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001496
1497 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1498 if (error || is_empty)
1499 return error;
1500
1501 *last_block = rec.br_startoff + rec.br_blockcount;
1502 return 0;
1503}
1504
1505/*
1506 * Returns whether the selected fork of the inode has exactly one
1507 * block or not. For the data fork we check this matches di_size,
1508 * implying the file's range is 0..bsize-1.
1509 */
1510int /* 1=>1 block, 0=>otherwise */
1511xfs_bmap_one_block(
1512 xfs_inode_t *ip, /* incore inode */
1513 int whichfork) /* data or attr fork */
1514{
Dave Chinner9e5987a2013-02-25 12:31:26 +11001515 xfs_ifork_t *ifp; /* inode fork pointer */
1516 int rval; /* return value */
1517 xfs_bmbt_irec_t s; /* internal version of extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001518 struct xfs_iext_cursor icur;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001519
1520#ifndef DEBUG
1521 if (whichfork == XFS_DATA_FORK)
1522 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1523#endif /* !DEBUG */
1524 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1525 return 0;
1526 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1527 return 0;
1528 ifp = XFS_IFORK_PTR(ip, whichfork);
1529 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001530 xfs_iext_first(ifp, &icur);
1531 xfs_iext_get_extent(ifp, &icur, &s);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001532 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1533 if (rval && whichfork == XFS_DATA_FORK)
1534 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1535 return rval;
1536}
1537
1538/*
1539 * Extent tree manipulation functions used during allocation.
1540 */
1541
1542/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001543 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 */
1545STATIC int /* error */
1546xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001547 struct xfs_bmalloca *bma,
1548 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001550 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001553 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 xfs_fileoff_t new_endoff; /* end offset of new entry */
1555 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1556 /* left is 0, right is 1, prev is 2 */
1557 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07001558 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001559 xfs_filblks_t da_new; /* new count del alloc blocks used */
1560 xfs_filblks_t da_old; /* old count del alloc blocks used */
1561 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001563 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001564 xfs_extnum_t *nextents;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001565 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001567 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001568 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001569 ASSERT(whichfork != XFS_ATTR_FORK);
1570 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1571 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001572
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001573 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001574 ASSERT(!bma->cur ||
1575 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001576
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001577 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579#define LEFT r[0]
1580#define RIGHT r[1]
1581#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 /*
1584 * Set up a bunch of variables to make the tests simpler.
1585 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001586 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001588 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 ASSERT(PREV.br_startoff <= new->br_startoff);
1590 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001591
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001592 da_old = startblockval(PREV.br_startblock);
1593 da_new = 0;
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 /*
1596 * Set flags determining what part of the previous delayed allocation
1597 * extent is being replaced by a real allocation.
1598 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001599 if (PREV.br_startoff == new->br_startoff)
1600 state |= BMAP_LEFT_FILLING;
1601 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1602 state |= BMAP_RIGHT_FILLING;
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 /*
1605 * Check and set flags if this segment has a left neighbor.
1606 * Don't set contiguous if the combined extent would be too large.
1607 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001608 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001609 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001610 if (isnullstartblock(LEFT.br_startblock))
1611 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001613
1614 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1615 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1616 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1617 LEFT.br_state == new->br_state &&
1618 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1619 state |= BMAP_LEFT_CONTIG;
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 /*
1622 * Check and set flags if this segment has a right neighbor.
1623 * Don't set contiguous if the combined extent would be too large.
1624 * Also check for all-three-contiguous being too large.
1625 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001626 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001627 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001628 if (isnullstartblock(RIGHT.br_startblock))
1629 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001631
1632 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1633 new_endoff == RIGHT.br_startoff &&
1634 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1635 new->br_state == RIGHT.br_state &&
1636 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1637 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1638 BMAP_RIGHT_FILLING)) !=
1639 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1640 BMAP_RIGHT_FILLING) ||
1641 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1642 <= MAXEXTLEN))
1643 state |= BMAP_RIGHT_CONTIG;
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 error = 0;
1646 /*
1647 * Switch out based on the FILLING and CONTIG state bits.
1648 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001649 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1650 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1651 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1652 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 /*
1654 * Filling in all of a previously delayed allocation extent.
1655 * The left and right neighbors are both contiguous with new.
1656 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001657 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001658
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001659 xfs_iext_remove(bma->ip, &bma->icur, state);
1660 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001661 xfs_iext_prev(ifp, &bma->icur);
1662 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001663 (*nextents)--;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001664
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001665 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1667 else {
1668 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001669 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001670 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001672 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001673 error = xfs_btree_delete(bma->cur, &i);
1674 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001676 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001677 error = xfs_btree_decrement(bma->cur, 0, &i);
1678 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001680 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001681 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001682 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 goto done;
1684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 break;
1686
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001687 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 /*
1689 * Filling in all of a previously delayed allocation extent.
1690 * The left neighbor is contiguous, the right is not.
1691 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001692 old = LEFT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001693 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001694
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001695 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001696 xfs_iext_prev(ifp, &bma->icur);
1697 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001698
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001699 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 rval = XFS_ILOG_DEXT;
1701 else {
1702 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001703 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001704 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001706 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001707 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001708 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 goto done;
1710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 break;
1712
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001713 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 /*
1715 * Filling in all of a previously delayed allocation extent.
1716 * The right neighbor is contiguous, the left is not.
1717 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001718 PREV.br_startblock = new->br_startblock;
1719 PREV.br_blockcount += RIGHT.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001720
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001721 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001722 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001723 xfs_iext_prev(ifp, &bma->icur);
1724 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001725
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001726 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 rval = XFS_ILOG_DEXT;
1728 else {
1729 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001730 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001731 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001733 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001734 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001735 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 goto done;
1737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 break;
1739
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001740 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 /*
1742 * Filling in all of a previously delayed allocation extent.
1743 * Neither the left nor right neighbors are contiguous with
1744 * the new one.
1745 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001746 PREV.br_startblock = new->br_startblock;
1747 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001748 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001749
Darrick J. Wong60b49842016-10-03 09:11:34 -07001750 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001751 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1753 else {
1754 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001755 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001756 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001758 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001759 error = xfs_btree_insert(bma->cur, &i);
1760 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001762 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 break;
1765
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001766 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 /*
1768 * Filling in the first part of a previous delayed allocation.
1769 * The left neighbor is contiguous.
1770 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001771 old = LEFT;
1772 temp = PREV.br_blockcount - new->br_blockcount;
1773 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1774 startblockval(PREV.br_startblock));
1775
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001776 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001777
Christoph Hellwigbf999712017-11-03 10:34:38 -07001778 PREV.br_blockcount = temp;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001779 PREV.br_startoff += new->br_blockcount;
1780 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwig0d045542017-11-03 10:34:39 -07001781
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001782 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1783 xfs_iext_prev(ifp, &bma->icur);
1784 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001785
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001786 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 rval = XFS_ILOG_DEXT;
1788 else {
1789 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001790 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001791 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001793 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001794 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001795 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 goto done;
1797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 break;
1799
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001800 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 /*
1802 * Filling in the first part of a previous delayed allocation.
1803 * The left neighbor is not contiguous.
1804 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001805 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001806 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001807 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1809 else {
1810 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001811 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001812 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001814 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001815 error = xfs_btree_insert(bma->cur, &i);
1816 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001818 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001820
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001821 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001822 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001823 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001824 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 rval |= tmp_rval;
1826 if (error)
1827 goto done;
1828 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001829
1830 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001831 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001832 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001833 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001834
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001835 PREV.br_startoff = new_endoff;
1836 PREV.br_blockcount = temp;
1837 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001838 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001839 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001840 xfs_iext_prev(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 break;
1842
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001843 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 /*
1845 * Filling in the last part of a previous delayed allocation.
1846 * The right neighbor is contiguous with the new allocation.
1847 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001848 old = RIGHT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001849 RIGHT.br_startoff = new->br_startoff;
1850 RIGHT.br_startblock = new->br_startblock;
1851 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001852
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001853 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 rval = XFS_ILOG_DEXT;
1855 else {
1856 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001857 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001858 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001860 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001861 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001862 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 goto done;
1864 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001865
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001866 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001867 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001868 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001869
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001870 PREV.br_blockcount = temp;
1871 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001872
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001873 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1874 xfs_iext_next(ifp, &bma->icur);
1875 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 break;
1877
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001878 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 /*
1880 * Filling in the last part of a previous delayed allocation.
1881 * The right neighbor is not contiguous.
1882 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001883 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001884 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001885 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1887 else {
1888 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001889 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001890 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001892 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001893 error = xfs_btree_insert(bma->cur, &i);
1894 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001896 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001898
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001899 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001900 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001901 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001902 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 rval |= tmp_rval;
1904 if (error)
1905 goto done;
1906 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001907
1908 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001909 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001910 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001911 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001912
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001913 PREV.br_startblock = nullstartblock(da_new);
1914 PREV.br_blockcount = temp;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001915 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001916 xfs_iext_next(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 break;
1918
1919 case 0:
1920 /*
1921 * Filling in the middle part of a previous delayed allocation.
1922 * Contiguity is impossible here.
1923 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001924 *
1925 * We start with a delayed allocation:
1926 *
1927 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1928 * PREV @ idx
1929 *
1930 * and we are allocating:
1931 * +rrrrrrrrrrrrrrrrr+
1932 * new
1933 *
1934 * and we set it up for insertion as:
1935 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1936 * new
1937 * PREV @ idx LEFT RIGHT
1938 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001940 old = PREV;
1941
1942 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001943 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001944
1945 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001946 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001947 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001948 RIGHT.br_blockcount =
1949 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1950 RIGHT.br_startblock =
1951 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1952 RIGHT.br_blockcount));
1953
1954 /* truncate PREV */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001955 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1956 PREV.br_startblock =
1957 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1958 PREV.br_blockcount));
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001959 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001960
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001961 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001962 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1963 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001964 (*nextents)++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001965
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001966 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1968 else {
1969 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001970 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001971 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001973 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001974 error = xfs_btree_insert(bma->cur, &i);
1975 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001977 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001979
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001980 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001981 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001982 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001983 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 rval |= tmp_rval;
1985 if (error)
1986 goto done;
1987 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001988
1989 da_new = startblockval(PREV.br_startblock) +
1990 startblockval(RIGHT.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 break;
1992
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001993 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1994 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1995 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1996 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1997 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1998 case BMAP_LEFT_CONTIG:
1999 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 /*
2001 * These cases are all impossible.
2002 */
2003 ASSERT(0);
2004 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002005
Darrick J. Wong9c194642016-08-03 12:16:05 +10002006 /* add reverse mapping */
2007 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2008 if (error)
2009 goto done;
2010
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002011 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002012 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002013 int tmp_logflags; /* partial log flag return val */
2014
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002015 ASSERT(bma->cur == NULL);
2016 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002017 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002018 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002019 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002020 if (error)
2021 goto done;
2022 }
2023
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002024 if (bma->cur) {
2025 da_new += bma->cur->bc_private.b.allocated;
2026 bma->cur->bc_private.b.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002027 }
2028
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002029 /* adjust for changes in reserved delayed indirect blocks */
2030 if (da_new != da_old) {
2031 ASSERT(state == 0 || da_new < da_old);
2032 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2033 false);
2034 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002035
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002036 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002038 if (whichfork != XFS_COW_FORK)
2039 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return error;
2041#undef LEFT
2042#undef RIGHT
2043#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
2046/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002047 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 */
2049STATIC int /* error */
2050xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002051 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002053 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002054 struct xfs_iext_cursor *icur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002056 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002057 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002058 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002059 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002064 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2067 /* left is 0, right is 1, prev is 2 */
2068 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002069 int state = xfs_bmap_fork_to_state(whichfork);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002070 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002071 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002073 *logflagsp = 0;
2074
2075 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002076 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002077
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002078 ASSERT(!isnullstartblock(new->br_startblock));
2079
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002080 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082#define LEFT r[0]
2083#define RIGHT r[1]
2084#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 /*
2087 * Set up a bunch of variables to make the tests simpler.
2088 */
2089 error = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002090 xfs_iext_get_extent(ifp, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002091 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 new_endoff = new->br_startoff + new->br_blockcount;
2093 ASSERT(PREV.br_startoff <= new->br_startoff);
2094 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 /*
2097 * Set flags determining what part of the previous oldext allocation
2098 * extent is being replaced by a newext allocation.
2099 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002100 if (PREV.br_startoff == new->br_startoff)
2101 state |= BMAP_LEFT_FILLING;
2102 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2103 state |= BMAP_RIGHT_FILLING;
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 /*
2106 * Check and set flags if this segment has a left neighbor.
2107 * Don't set contiguous if the combined extent would be too large.
2108 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002109 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002110 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002111 if (isnullstartblock(LEFT.br_startblock))
2112 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002114
2115 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2116 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2117 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002118 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002119 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2120 state |= BMAP_LEFT_CONTIG;
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 /*
2123 * Check and set flags if this segment has a right neighbor.
2124 * Don't set contiguous if the combined extent would be too large.
2125 * Also check for all-three-contiguous being too large.
2126 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002127 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002128 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002129 if (isnullstartblock(RIGHT.br_startblock))
2130 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002132
2133 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2134 new_endoff == RIGHT.br_startoff &&
2135 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002136 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002137 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2138 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2139 BMAP_RIGHT_FILLING)) !=
2140 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2141 BMAP_RIGHT_FILLING) ||
2142 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2143 <= MAXEXTLEN))
2144 state |= BMAP_RIGHT_CONTIG;
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 /*
2147 * Switch out based on the FILLING and CONTIG state bits.
2148 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002149 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2150 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2151 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2152 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 /*
2154 * Setting all of a previous oldext extent to newext.
2155 * The left and right neighbors are both contiguous with new.
2156 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002157 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002158
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002159 xfs_iext_remove(ip, icur, state);
2160 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002161 xfs_iext_prev(ifp, icur);
2162 xfs_iext_update_extent(ip, state, icur, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002163 XFS_IFORK_NEXT_SET(ip, whichfork,
2164 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 if (cur == NULL)
2166 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2167 else {
2168 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002169 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2170 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002172 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002173 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002175 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002176 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002178 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002179 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002181 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002182 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002184 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002185 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002186 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 goto done;
2188 }
2189 break;
2190
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002191 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 /*
2193 * Setting all of a previous oldext extent to newext.
2194 * The left neighbor is contiguous, the right is not.
2195 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002196 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002197
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002198 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002199 xfs_iext_prev(ifp, icur);
2200 xfs_iext_update_extent(ip, state, icur, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002201 XFS_IFORK_NEXT_SET(ip, whichfork,
2202 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 if (cur == NULL)
2204 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2205 else {
2206 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002207 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2208 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002210 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002211 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002213 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002214 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002216 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002217 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002218 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 goto done;
2220 }
2221 break;
2222
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002223 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 /*
2225 * Setting all of a previous oldext extent to newext.
2226 * The right neighbor is contiguous, the left is not.
2227 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002228 PREV.br_blockcount += RIGHT.br_blockcount;
2229 PREV.br_state = new->br_state;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002230
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002231 xfs_iext_next(ifp, icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002232 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002233 xfs_iext_prev(ifp, icur);
2234 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002235
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002236 XFS_IFORK_NEXT_SET(ip, whichfork,
2237 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 if (cur == NULL)
2239 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2240 else {
2241 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002242 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2243 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002245 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002246 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002248 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002249 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002251 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002252 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002253 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 goto done;
2255 }
2256 break;
2257
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002258 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 /*
2260 * Setting all of a previous oldext extent to newext.
2261 * Neither the left nor right neighbors are contiguous with
2262 * the new one.
2263 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002264 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002265 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (cur == NULL)
2268 rval = XFS_ILOG_DEXT;
2269 else {
2270 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002271 error = xfs_bmbt_lookup_eq(cur, new, &i);
2272 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002274 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002275 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002276 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 goto done;
2278 }
2279 break;
2280
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002281 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 /*
2283 * Setting the first part of a previous oldext extent to newext.
2284 * The left neighbor is contiguous.
2285 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002286 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002287
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002288 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002289 PREV.br_startoff += new->br_blockcount;
2290 PREV.br_startblock += new->br_blockcount;
2291 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002292
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002293 xfs_iext_update_extent(ip, state, icur, &PREV);
2294 xfs_iext_prev(ifp, icur);
2295 xfs_iext_update_extent(ip, state, icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 if (cur == NULL)
2298 rval = XFS_ILOG_DEXT;
2299 else {
2300 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002301 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002302 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002304 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002305 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002306 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002308 error = xfs_btree_decrement(cur, 0, &i);
2309 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002311 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002312 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 goto done;
2314 }
2315 break;
2316
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002317 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 /*
2319 * Setting the first part of a previous oldext extent to newext.
2320 * The left neighbor is not contiguous.
2321 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002322 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002323 PREV.br_startoff += new->br_blockcount;
2324 PREV.br_startblock += new->br_blockcount;
2325 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002326
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002327 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002328 xfs_iext_insert(ip, icur, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002329 XFS_IFORK_NEXT_SET(ip, whichfork,
2330 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 if (cur == NULL)
2332 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2333 else {
2334 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002335 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002336 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002338 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002339 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002340 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 goto done;
2342 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002343 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002345 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
2347 break;
2348
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002349 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 /*
2351 * Setting the last part of a previous oldext extent to newext.
2352 * The right neighbor is contiguous with the new allocation.
2353 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002354 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002355 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002356
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002357 RIGHT.br_startoff = new->br_startoff;
2358 RIGHT.br_startblock = new->br_startblock;
2359 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002360
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002361 xfs_iext_update_extent(ip, state, icur, &PREV);
2362 xfs_iext_next(ifp, icur);
2363 xfs_iext_update_extent(ip, state, icur, &RIGHT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (cur == NULL)
2366 rval = XFS_ILOG_DEXT;
2367 else {
2368 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002369 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002370 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002372 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002373 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002374 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002376 error = xfs_btree_increment(cur, 0, &i);
2377 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002379 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002380 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 goto done;
2382 }
2383 break;
2384
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002385 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 /*
2387 * Setting the last part of a previous oldext extent to newext.
2388 * The right neighbor is not contiguous.
2389 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002390 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002391 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002392
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002393 xfs_iext_update_extent(ip, state, icur, &PREV);
2394 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002395 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002396
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002397 XFS_IFORK_NEXT_SET(ip, whichfork,
2398 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 if (cur == NULL)
2400 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2401 else {
2402 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002403 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002404 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002406 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002407 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002408 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002410 error = xfs_bmbt_lookup_eq(cur, new, &i);
2411 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002413 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002414 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002416 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 }
2418 break;
2419
2420 case 0:
2421 /*
2422 * Setting the middle part of a previous oldext extent to
2423 * newext. Contiguity is impossible here.
2424 * One extent becomes three extents.
2425 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002426 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002427 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 r[0] = *new;
2430 r[1].br_startoff = new_endoff;
2431 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002432 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002434 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002435
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002436 xfs_iext_update_extent(ip, state, icur, &PREV);
2437 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002438 xfs_iext_insert(ip, icur, &r[1], state);
2439 xfs_iext_insert(ip, icur, &r[0], state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002440
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002441 XFS_IFORK_NEXT_SET(ip, whichfork,
2442 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 if (cur == NULL)
2444 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2445 else {
2446 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002447 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002448 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002450 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002452 error = xfs_bmbt_update(cur, &r[1]);
2453 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 goto done;
2455 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002457 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002459 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002460 /*
2461 * Reset the cursor to the position of the new extent
2462 * we are about to insert as we can't trust it after
2463 * the previous insert.
2464 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002465 error = xfs_bmbt_lookup_eq(cur, new, &i);
2466 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002468 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002470 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002472 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 }
2474 break;
2475
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002476 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2477 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2478 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2479 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2480 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2481 case BMAP_LEFT_CONTIG:
2482 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 /*
2484 * These cases are all impossible.
2485 */
2486 ASSERT(0);
2487 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002488
Darrick J. Wong9c194642016-08-03 12:16:05 +10002489 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002490 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002491 if (error)
2492 goto done;
2493
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002494 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002495 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002496 int tmp_logflags; /* partial log flag return val */
2497
2498 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002499 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002500 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002501 *logflagsp |= tmp_logflags;
2502 if (error)
2503 goto done;
2504 }
2505
2506 /* clear out the allocated field, done with it now in any case. */
2507 if (cur) {
2508 cur->bc_private.b.allocated = 0;
2509 *curp = cur;
2510 }
2511
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002512 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002514 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 return error;
2516#undef LEFT
2517#undef RIGHT
2518#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519}
2520
2521/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002522 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002524STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525xfs_bmap_add_extent_hole_delay(
2526 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002527 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002528 struct xfs_iext_cursor *icur,
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002529 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002531 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2533 xfs_filblks_t newlen=0; /* new indirect size */
2534 xfs_filblks_t oldlen=0; /* old indirect size */
2535 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002536 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002537 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002539 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002540 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002541
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 /*
2543 * Check and set flags if this segment has a left neighbor
2544 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002545 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002546 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002547 if (isnullstartblock(left.br_startblock))
2548 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002550
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 /*
2552 * Check and set flags if the current (right) segment exists.
2553 * If it doesn't exist, we're converting the hole at end-of-file.
2554 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002555 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002556 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002557 if (isnullstartblock(right.br_startblock))
2558 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 /*
2562 * Set contiguity flags on the left and right neighbors.
2563 * Don't let extents get too large, even if the pieces are contiguous.
2564 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002565 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2566 left.br_startoff + left.br_blockcount == new->br_startoff &&
2567 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2568 state |= BMAP_LEFT_CONTIG;
2569
2570 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2571 new->br_startoff + new->br_blockcount == right.br_startoff &&
2572 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2573 (!(state & BMAP_LEFT_CONTIG) ||
2574 (left.br_blockcount + new->br_blockcount +
2575 right.br_blockcount <= MAXEXTLEN)))
2576 state |= BMAP_RIGHT_CONTIG;
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 /*
2579 * Switch out based on the contiguity flags.
2580 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002581 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2582 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 /*
2584 * New allocation is contiguous with delayed allocations
2585 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002586 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 */
2588 temp = left.br_blockcount + new->br_blockcount +
2589 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002590
Eric Sandeen9d87c312009-01-14 23:22:07 -06002591 oldlen = startblockval(left.br_startblock) +
2592 startblockval(new->br_startblock) +
2593 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002594 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2595 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002596 left.br_startblock = nullstartblock(newlen);
2597 left.br_blockcount = temp;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002598
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002599 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002600 xfs_iext_prev(ifp, icur);
2601 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 break;
2603
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002604 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 /*
2606 * New allocation is contiguous with a delayed allocation
2607 * on the left.
2608 * Merge the new allocation with the left neighbor.
2609 */
2610 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002611
Eric Sandeen9d87c312009-01-14 23:22:07 -06002612 oldlen = startblockval(left.br_startblock) +
2613 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002614 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2615 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002616 left.br_blockcount = temp;
2617 left.br_startblock = nullstartblock(newlen);
Christoph Hellwig41d196f2017-11-03 10:34:39 -07002618
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002619 xfs_iext_prev(ifp, icur);
2620 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 break;
2622
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002623 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 /*
2625 * New allocation is contiguous with a delayed allocation
2626 * on the right.
2627 * Merge the new allocation with the right neighbor.
2628 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002630 oldlen = startblockval(new->br_startblock) +
2631 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002632 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2633 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002634 right.br_startoff = new->br_startoff;
2635 right.br_startblock = nullstartblock(newlen);
2636 right.br_blockcount = temp;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002637 xfs_iext_update_extent(ip, state, icur, &right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 break;
2639
2640 case 0:
2641 /*
2642 * New allocation is not contiguous with another
2643 * delayed allocation.
2644 * Insert a new entry.
2645 */
2646 oldlen = newlen = 0;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002647 xfs_iext_insert(ip, icur, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 break;
2649 }
2650 if (oldlen != newlen) {
2651 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002652 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2653 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 /*
2655 * Nothing to do for disk quota accounting here.
2656 */
2657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
2659
2660/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002661 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 */
2663STATIC int /* error */
2664xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002665 struct xfs_trans *tp,
2666 struct xfs_inode *ip,
2667 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002668 struct xfs_iext_cursor *icur,
Christoph Hellwig6d045582017-04-11 16:45:54 -07002669 struct xfs_btree_cur **curp,
2670 struct xfs_bmbt_irec *new,
2671 xfs_fsblock_t *first,
2672 struct xfs_defer_ops *dfops,
2673 int *logflagsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002675 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2676 struct xfs_mount *mp = ip->i_mount;
2677 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2681 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002682 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002683 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002684 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002686 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002687 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002688
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002689 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 /*
2692 * Check and set flags if this segment has a left neighbor.
2693 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002694 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002695 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002696 if (isnullstartblock(left.br_startblock))
2697 state |= BMAP_LEFT_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 * Check and set flags if this segment has a current value.
2702 * Not true if we're inserting into the "hole" at eof.
2703 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002704 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002705 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002706 if (isnullstartblock(right.br_startblock))
2707 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 /*
2711 * We're inserting a real allocation between "left" and "right".
2712 * Set the contiguity flags. Don't let extents get too large.
2713 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002714 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2715 left.br_startoff + left.br_blockcount == new->br_startoff &&
2716 left.br_startblock + left.br_blockcount == new->br_startblock &&
2717 left.br_state == new->br_state &&
2718 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2719 state |= BMAP_LEFT_CONTIG;
2720
2721 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2722 new->br_startoff + new->br_blockcount == right.br_startoff &&
2723 new->br_startblock + new->br_blockcount == right.br_startblock &&
2724 new->br_state == right.br_state &&
2725 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2726 (!(state & BMAP_LEFT_CONTIG) ||
2727 left.br_blockcount + new->br_blockcount +
2728 right.br_blockcount <= MAXEXTLEN))
2729 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002731 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 /*
2733 * Select which case we're in here, and implement it.
2734 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002735 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2736 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 /*
2738 * New allocation is contiguous with real allocations on the
2739 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002740 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002742 left.br_blockcount += new->br_blockcount + right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002743
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002744 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002745 xfs_iext_prev(ifp, icur);
2746 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002747
Christoph Hellwig6d045582017-04-11 16:45:54 -07002748 XFS_IFORK_NEXT_SET(ip, whichfork,
2749 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2750 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002751 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002752 } else {
2753 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002754 error = xfs_bmbt_lookup_eq(cur, &right, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002755 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002756 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002757 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002758 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002759 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002760 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002761 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002762 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002763 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002764 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002765 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002766 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002767 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002768 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002770 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002772 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 /*
2774 * New allocation is contiguous with a real allocation
2775 * on the left.
2776 * Merge the new allocation with the left neighbor.
2777 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002778 old = left;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002779 left.br_blockcount += new->br_blockcount;
Christoph Hellwig1d2e0082017-11-03 10:34:40 -07002780
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002781 xfs_iext_prev(ifp, icur);
2782 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002783
Christoph Hellwig6d045582017-04-11 16:45:54 -07002784 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002785 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002786 } else {
2787 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002788 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002789 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002790 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002791 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002792 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002793 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002794 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002796 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002798 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 /*
2800 * New allocation is contiguous with a real allocation
2801 * on the right.
2802 * Merge the new allocation with the right neighbor.
2803 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002804 old = right;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002805
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002806 right.br_startoff = new->br_startoff;
2807 right.br_startblock = new->br_startblock;
2808 right.br_blockcount += new->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002809 xfs_iext_update_extent(ip, state, icur, &right);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002810
Christoph Hellwig6d045582017-04-11 16:45:54 -07002811 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002812 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002813 } else {
2814 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002815 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002816 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002817 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002818 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002819 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002820 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002821 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002823 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
2825 case 0:
2826 /*
2827 * New allocation is not contiguous with another
2828 * real allocation.
2829 * Insert a new entry.
2830 */
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002831 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002832 XFS_IFORK_NEXT_SET(ip, whichfork,
2833 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2834 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002835 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002836 } else {
2837 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002838 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002839 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002840 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002841 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002842 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002843 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002844 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002845 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002847 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002849
Darrick J. Wong9c194642016-08-03 12:16:05 +10002850 /* add reverse mapping */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002851 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002852 if (error)
2853 goto done;
2854
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002855 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002856 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002857 int tmp_logflags; /* partial log flag return val */
2858
Christoph Hellwig6d045582017-04-11 16:45:54 -07002859 ASSERT(cur == NULL);
2860 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002861 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002862 *logflagsp |= tmp_logflags;
2863 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002864 if (error)
2865 goto done;
2866 }
2867
2868 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002869 if (cur)
2870 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002871
Christoph Hellwig6d045582017-04-11 16:45:54 -07002872 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002873done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002874 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002875 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876}
2877
Nathan Scottdd9f4382006-01-11 15:28:28 +11002878/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11002879 * Functions used in the extent read, allocate and remove paths
2880 */
2881
2882/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002883 * Adjust the size of the new extent based on di_extsize and rt extsize.
2884 */
Dave Chinner68988112013-08-12 20:49:42 +10002885int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002886xfs_bmap_extsize_align(
2887 xfs_mount_t *mp,
2888 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2889 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2890 xfs_extlen_t extsz, /* align to this extent size */
2891 int rt, /* is this a realtime inode? */
2892 int eof, /* is extent at end-of-file? */
2893 int delay, /* creating delalloc extent? */
2894 int convert, /* overwriting unwritten extent? */
2895 xfs_fileoff_t *offp, /* in/out: aligned offset */
2896 xfs_extlen_t *lenp) /* in/out: aligned length */
2897{
2898 xfs_fileoff_t orig_off; /* original offset */
2899 xfs_extlen_t orig_alen; /* original length */
2900 xfs_fileoff_t orig_end; /* original off+len */
2901 xfs_fileoff_t nexto; /* next file offset */
2902 xfs_fileoff_t prevo; /* previous file offset */
2903 xfs_fileoff_t align_off; /* temp for offset */
2904 xfs_extlen_t align_alen; /* temp for length */
2905 xfs_extlen_t temp; /* temp for calculations */
2906
2907 if (convert)
2908 return 0;
2909
2910 orig_off = align_off = *offp;
2911 orig_alen = align_alen = *lenp;
2912 orig_end = orig_off + orig_alen;
2913
2914 /*
2915 * If this request overlaps an existing extent, then don't
2916 * attempt to perform any additional alignment.
2917 */
2918 if (!delay && !eof &&
2919 (orig_off >= gotp->br_startoff) &&
2920 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2921 return 0;
2922 }
2923
2924 /*
2925 * If the file offset is unaligned vs. the extent size
2926 * we need to align it. This will be possible unless
2927 * the file was previously written with a kernel that didn't
2928 * perform this alignment, or if a truncate shot us in the
2929 * foot.
2930 */
2931 temp = do_mod(orig_off, extsz);
2932 if (temp) {
2933 align_alen += temp;
2934 align_off -= temp;
2935 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10002936
2937 /* Same adjustment for the end of the requested area. */
2938 temp = (align_alen % extsz);
2939 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11002940 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10002941
2942 /*
2943 * For large extent hint sizes, the aligned extent might be larger than
2944 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2945 * the length back under MAXEXTLEN. The outer allocation loops handle
2946 * short allocation just fine, so it is safe to do this. We only want to
2947 * do it when we are forced to, though, because it means more allocation
2948 * operations are required.
2949 */
2950 while (align_alen > MAXEXTLEN)
2951 align_alen -= extsz;
2952 ASSERT(align_alen <= MAXEXTLEN);
2953
Nathan Scottdd9f4382006-01-11 15:28:28 +11002954 /*
2955 * If the previous block overlaps with this proposed allocation
2956 * then move the start forward without adjusting the length.
2957 */
2958 if (prevp->br_startoff != NULLFILEOFF) {
2959 if (prevp->br_startblock == HOLESTARTBLOCK)
2960 prevo = prevp->br_startoff;
2961 else
2962 prevo = prevp->br_startoff + prevp->br_blockcount;
2963 } else
2964 prevo = 0;
2965 if (align_off != orig_off && align_off < prevo)
2966 align_off = prevo;
2967 /*
2968 * If the next block overlaps with this proposed allocation
2969 * then move the start back without adjusting the length,
2970 * but not before offset 0.
2971 * This may of course make the start overlap previous block,
2972 * and if we hit the offset 0 limit then the next block
2973 * can still overlap too.
2974 */
2975 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2976 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2977 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2978 nexto = gotp->br_startoff + gotp->br_blockcount;
2979 else
2980 nexto = gotp->br_startoff;
2981 } else
2982 nexto = NULLFILEOFF;
2983 if (!eof &&
2984 align_off + align_alen != orig_end &&
2985 align_off + align_alen > nexto)
2986 align_off = nexto > align_alen ? nexto - align_alen : 0;
2987 /*
2988 * If we're now overlapping the next or previous extent that
2989 * means we can't fit an extsz piece in this hole. Just move
2990 * the start forward to the first valid spot and set
2991 * the length so we hit the end.
2992 */
2993 if (align_off != orig_off && align_off < prevo)
2994 align_off = prevo;
2995 if (align_off + align_alen != orig_end &&
2996 align_off + align_alen > nexto &&
2997 nexto != NULLFILEOFF) {
2998 ASSERT(nexto > prevo);
2999 align_alen = nexto - align_off;
3000 }
3001
3002 /*
3003 * If realtime, and the result isn't a multiple of the realtime
3004 * extent size we need to remove blocks until it is.
3005 */
3006 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3007 /*
3008 * We're not covering the original request, or
3009 * we won't be able to once we fix the length.
3010 */
3011 if (orig_off < align_off ||
3012 orig_end > align_off + align_alen ||
3013 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003014 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003015 /*
3016 * Try to fix it by moving the start up.
3017 */
3018 if (align_off + temp <= orig_off) {
3019 align_alen -= temp;
3020 align_off += temp;
3021 }
3022 /*
3023 * Try to fix it by moving the end in.
3024 */
3025 else if (align_off + align_alen - temp >= orig_end)
3026 align_alen -= temp;
3027 /*
3028 * Set the start to the minimum then trim the length.
3029 */
3030 else {
3031 align_alen -= orig_off - align_off;
3032 align_off = orig_off;
3033 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3034 }
3035 /*
3036 * Result doesn't cover the request, fail it.
3037 */
3038 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003039 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003040 } else {
3041 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003042 /* see MAXEXTLEN handling above */
3043 ASSERT(orig_end <= align_off + align_alen ||
3044 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003045 }
3046
3047#ifdef DEBUG
3048 if (!eof && gotp->br_startoff != NULLFILEOFF)
3049 ASSERT(align_off + align_alen <= gotp->br_startoff);
3050 if (prevp->br_startoff != NULLFILEOFF)
3051 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3052#endif
3053
3054 *lenp = align_alen;
3055 *offp = align_off;
3056 return 0;
3057}
3058
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059#define XFS_ALLOC_GAP_UNITS 4
3060
Dave Chinner68988112013-08-12 20:49:42 +10003061void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003062xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003063 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
3065 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3067 xfs_mount_t *mp; /* mount point structure */
3068 int nullfb; /* true if ap->firstblock isn't set */
3069 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
3071#define ISVALID(x,y) \
3072 (rt ? \
3073 (x) < mp->m_sb.sb_rblocks : \
3074 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3075 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3076 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3077
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003079 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003080 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3081 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003082 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 /*
3084 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003085 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003087 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3088 !isnullstartblock(ap->prev.br_startblock) &&
3089 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3090 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003091 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 /*
3093 * Adjust for the gap between prevp and us.
3094 */
Dave Chinner3a756672011-09-18 20:40:58 +00003095 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003096 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003098 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3099 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 }
3101 /*
3102 * If not at eof, then compare the two neighbor blocks.
3103 * Figure out whether either one gives us a good starting point,
3104 * and pick the better one.
3105 */
3106 else if (!ap->eof) {
3107 xfs_fsblock_t gotbno; /* right side block number */
3108 xfs_fsblock_t gotdiff=0; /* right side difference */
3109 xfs_fsblock_t prevbno; /* left side block number */
3110 xfs_fsblock_t prevdiff=0; /* left side difference */
3111
3112 /*
3113 * If there's a previous (left) block, select a requested
3114 * start block based on it.
3115 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003116 if (ap->prev.br_startoff != NULLFILEOFF &&
3117 !isnullstartblock(ap->prev.br_startblock) &&
3118 (prevbno = ap->prev.br_startblock +
3119 ap->prev.br_blockcount) &&
3120 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 /*
3122 * Calculate gap to end of previous block.
3123 */
Dave Chinner3a756672011-09-18 20:40:58 +00003124 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003125 (ap->prev.br_startoff +
3126 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 /*
3128 * Figure the startblock based on the previous block's
3129 * end and the gap size.
3130 * Heuristic!
3131 * If the gap is large relative to the piece we're
3132 * allocating, or using it gives us an invalid block
3133 * number, then just use the end of the previous block.
3134 */
Dave Chinner3a756672011-09-18 20:40:58 +00003135 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003137 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 prevbno += adjust;
3139 else
3140 prevdiff += adjust;
3141 /*
3142 * If the firstblock forbids it, can't use it,
3143 * must use default.
3144 */
3145 if (!rt && !nullfb &&
3146 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3147 prevbno = NULLFSBLOCK;
3148 }
3149 /*
3150 * No previous block or can't follow it, just default.
3151 */
3152 else
3153 prevbno = NULLFSBLOCK;
3154 /*
3155 * If there's a following (right) block, select a requested
3156 * start block based on it.
3157 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003158 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 /*
3160 * Calculate gap to start of next block.
3161 */
Dave Chinner3a756672011-09-18 20:40:58 +00003162 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 /*
3164 * Figure the startblock based on the next block's
3165 * start and the gap size.
3166 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003167 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 /*
3169 * Heuristic!
3170 * If the gap is large relative to the piece we're
3171 * allocating, or using it gives us an invalid block
3172 * number, then just use the start of the next block
3173 * offset by our length.
3174 */
Dave Chinner3a756672011-09-18 20:40:58 +00003175 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 ISVALID(gotbno - gotdiff, gotbno))
3177 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003178 else if (ISVALID(gotbno - ap->length, gotbno)) {
3179 gotbno -= ap->length;
3180 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 } else
3182 gotdiff += adjust;
3183 /*
3184 * If the firstblock forbids it, can't use it,
3185 * must use default.
3186 */
3187 if (!rt && !nullfb &&
3188 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3189 gotbno = NULLFSBLOCK;
3190 }
3191 /*
3192 * No next block, just default.
3193 */
3194 else
3195 gotbno = NULLFSBLOCK;
3196 /*
3197 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003198 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 */
3200 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003201 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003203 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003205 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003207#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003208}
3209
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003210static int
3211xfs_bmap_longest_free_extent(
3212 struct xfs_trans *tp,
3213 xfs_agnumber_t ag,
3214 xfs_extlen_t *blen,
3215 int *notinit)
3216{
3217 struct xfs_mount *mp = tp->t_mountp;
3218 struct xfs_perag *pag;
3219 xfs_extlen_t longest;
3220 int error = 0;
3221
3222 pag = xfs_perag_get(mp, ag);
3223 if (!pag->pagf_init) {
3224 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3225 if (error)
3226 goto out;
3227
3228 if (!pag->pagf_init) {
3229 *notinit = 1;
3230 goto out;
3231 }
3232 }
3233
Eric Sandeena1f69412018-04-06 10:09:42 -07003234 longest = xfs_alloc_longest_free_extent(pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003235 xfs_alloc_min_freelist(mp, pag),
3236 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003237 if (*blen < longest)
3238 *blen = longest;
3239
3240out:
3241 xfs_perag_put(pag);
3242 return error;
3243}
3244
3245static void
3246xfs_bmap_select_minlen(
3247 struct xfs_bmalloca *ap,
3248 struct xfs_alloc_arg *args,
3249 xfs_extlen_t *blen,
3250 int notinit)
3251{
3252 if (notinit || *blen < ap->minlen) {
3253 /*
3254 * Since we did a BUF_TRYLOCK above, it is possible that
3255 * there is space for this request.
3256 */
3257 args->minlen = ap->minlen;
3258 } else if (*blen < args->maxlen) {
3259 /*
3260 * If the best seen length is less than the request length,
3261 * use the best as the minimum.
3262 */
3263 args->minlen = *blen;
3264 } else {
3265 /*
3266 * Otherwise we've seen an extent as big as maxlen, use that
3267 * as the minimum.
3268 */
3269 args->minlen = args->maxlen;
3270 }
3271}
3272
Nathan Scotta365bdd2006-03-14 13:34:16 +11003273STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003274xfs_bmap_btalloc_nullfb(
3275 struct xfs_bmalloca *ap,
3276 struct xfs_alloc_arg *args,
3277 xfs_extlen_t *blen)
3278{
3279 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003280 xfs_agnumber_t ag, startag;
3281 int notinit = 0;
3282 int error;
3283
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003284 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003285 args->total = ap->total;
3286
Christoph Hellwigc467c042010-02-15 23:34:42 +00003287 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3288 if (startag == NULLAGNUMBER)
3289 startag = ag = 0;
3290
Dave Chinner14b064c2011-01-27 12:16:28 +11003291 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003292 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3293 &notinit);
3294 if (error)
3295 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003296
Christoph Hellwigc467c042010-02-15 23:34:42 +00003297 if (++ag == mp->m_sb.sb_agcount)
3298 ag = 0;
3299 if (ag == startag)
3300 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003301 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003302
3303 xfs_bmap_select_minlen(ap, args, blen, notinit);
3304 return 0;
3305}
3306
3307STATIC int
3308xfs_bmap_btalloc_filestreams(
3309 struct xfs_bmalloca *ap,
3310 struct xfs_alloc_arg *args,
3311 xfs_extlen_t *blen)
3312{
3313 struct xfs_mount *mp = ap->ip->i_mount;
3314 xfs_agnumber_t ag;
3315 int notinit = 0;
3316 int error;
3317
3318 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3319 args->total = ap->total;
3320
3321 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3322 if (ag == NULLAGNUMBER)
3323 ag = 0;
3324
3325 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3326 if (error)
3327 return error;
3328
3329 if (*blen < args->maxlen) {
3330 error = xfs_filestream_new_ag(ap, &ag);
3331 if (error)
3332 return error;
3333
3334 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3335 &notinit);
3336 if (error)
3337 return error;
3338
3339 }
3340
3341 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003342
3343 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003344 * Set the failure fallback case to look in the selected AG as stream
3345 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003346 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003347 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003348 return 0;
3349}
3350
Darrick J. Wong751f3762018-01-25 13:58:13 -08003351/* Update all inode and quota accounting for the allocation we just did. */
3352static void
3353xfs_bmap_btalloc_accounting(
3354 struct xfs_bmalloca *ap,
3355 struct xfs_alloc_arg *args)
3356{
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003357 if (ap->flags & XFS_BMAPI_COWFORK) {
3358 /*
3359 * COW fork blocks are in-core only and thus are treated as
3360 * in-core quota reservation (like delalloc blocks) even when
3361 * converted to real blocks. The quota reservation is not
3362 * accounted to disk until blocks are remapped to the data
3363 * fork. So if these blocks were previously delalloc, we
3364 * already have quota reservation and there's nothing to do
3365 * yet.
3366 */
3367 if (ap->wasdel)
3368 return;
3369
3370 /*
3371 * Otherwise, we've allocated blocks in a hole. The transaction
3372 * has acquired in-core quota reservation for this extent.
3373 * Rather than account these as real blocks, however, we reduce
3374 * the transaction quota reservation based on the allocation.
3375 * This essentially transfers the transaction quota reservation
3376 * to that of a delalloc extent.
3377 */
3378 ap->ip->i_delayed_blks += args->len;
3379 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3380 -(long)args->len);
3381 return;
3382 }
3383
3384 /* data/attr fork only */
3385 ap->ip->i_d.di_nblocks += args->len;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003386 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3387 if (ap->wasdel)
3388 ap->ip->i_delayed_blks -= args->len;
3389 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3390 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3391 args->len);
3392}
3393
Christoph Hellwigc467c042010-02-15 23:34:42 +00003394STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003395xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003396 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003397{
3398 xfs_mount_t *mp; /* mount point structure */
3399 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003400 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003401 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003402 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003403 xfs_alloc_arg_t args;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003404 xfs_fileoff_t orig_offset;
3405 xfs_extlen_t orig_length;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003406 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003407 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003408 int nullfb; /* true if ap->firstblock isn't set */
3409 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003410 int tryagain;
3411 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003412 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003413
Dave Chinnera99ebf42011-12-01 11:24:20 +00003414 ASSERT(ap->length);
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003415 orig_offset = ap->offset;
3416 orig_length = ap->length;
Dave Chinnera99ebf42011-12-01 11:24:20 +00003417
Nathan Scotta365bdd2006-03-14 13:34:16 +11003418 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003419
3420 /* stripe alignment for allocation is determined by mount parameters */
3421 stripe_align = 0;
3422 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3423 stripe_align = mp->m_swidth;
3424 else if (mp->m_dalign)
3425 stripe_align = mp->m_dalign;
3426
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003427 if (ap->flags & XFS_BMAPI_COWFORK)
3428 align = xfs_get_cowextsz_hint(ap->ip);
3429 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003430 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003431 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003432 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003433 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003434 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003435 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003436 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003437 }
Dave Chinner33177f052013-12-12 16:34:36 +11003438
3439
Dave Chinner0937e0f2011-09-18 20:40:57 +00003440 nullfb = *ap->firstblock == NULLFSBLOCK;
3441 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003442 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003443 if (xfs_alloc_is_userdata(ap->datatype) &&
3444 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003445 ag = xfs_filestream_lookup_ag(ap->ip);
3446 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003447 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003448 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003449 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003450 }
3451 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003452 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003453
3454 xfs_bmap_adjacent(ap);
3455
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003457 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 * it's in the right allocation group.
3459 */
Dave Chinner3a756672011-09-18 20:40:58 +00003460 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 ;
3462 else
Dave Chinner3a756672011-09-18 20:40:58 +00003463 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 * Normal allocation, done through xfs_alloc_vextent.
3466 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003467 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003468 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003469 args.tp = ap->tp;
3470 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003471 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003472 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003473
3474 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003475 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003476 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003477 blen = 0;
3478 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003479 /*
3480 * Search for an allocation group with a single extent large
3481 * enough for the request. If one isn't found, then adjust
3482 * the minimum allocation size to the largest space found.
3483 */
Dave Chinner292378e2016-09-26 08:21:28 +10003484 if (xfs_alloc_is_userdata(ap->datatype) &&
3485 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003486 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3487 else
3488 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003489 if (error)
3490 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003491 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003492 if (xfs_inode_is_filestream(ap->ip))
3493 args.type = XFS_ALLOCTYPE_FIRST_AG;
3494 else
3495 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003496 args.total = args.minlen = ap->minlen;
3497 } else {
3498 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3499 args.total = ap->total;
3500 args.minlen = ap->minlen;
3501 }
David Chinner957d0eb2007-06-18 16:50:37 +10003502 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003503 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003504 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003505 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003506 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003507 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003508 args.prod = 1;
3509 args.mod = 0;
3510 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003511 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003512 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003513 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3514 }
3515 /*
3516 * If we are not low on available data blocks, and the
3517 * underlying logical volume manager is a stripe, and
3518 * the file offset is zero then try to allocate data
3519 * blocks on stripe unit boundary.
3520 * NOTE: ap->aeof is only set if the allocation length
3521 * is >= the stripe unit and the allocation offset is
3522 * at the end of file.
3523 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003524 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003525 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003526 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003527 atype = args.type;
3528 isaligned = 1;
3529 /*
3530 * Adjust for alignment
3531 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003532 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003533 args.minlen = blen - args.alignment;
3534 args.minalignslop = 0;
3535 } else {
3536 /*
3537 * First try an exact bno allocation.
3538 * If it fails then do a near or start bno
3539 * allocation with alignment turned on.
3540 */
3541 atype = args.type;
3542 tryagain = 1;
3543 args.type = XFS_ALLOCTYPE_THIS_BNO;
3544 args.alignment = 1;
3545 /*
3546 * Compute the minlen+alignment for the
3547 * next case. Set slop so that the value
3548 * of minlen+alignment+slop doesn't go up
3549 * between the calls.
3550 */
Dave Chinner33177f052013-12-12 16:34:36 +11003551 if (blen > stripe_align && blen <= args.maxlen)
3552 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003553 else
3554 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003555 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003556 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003557 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003558 args.minlen - 1;
3559 else
3560 args.minalignslop = 0;
3561 }
3562 } else {
3563 args.alignment = 1;
3564 args.minalignslop = 0;
3565 }
3566 args.minleft = ap->minleft;
3567 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003568 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003569 args.datatype = ap->datatype;
3570 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003571 args.ip = ap->ip;
3572
3573 error = xfs_alloc_vextent(&args);
3574 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003575 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003576
Nathan Scotta365bdd2006-03-14 13:34:16 +11003577 if (tryagain && args.fsbno == NULLFSBLOCK) {
3578 /*
3579 * Exact allocation failed. Now try with alignment
3580 * turned on.
3581 */
3582 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003583 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003584 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003585 args.minlen = nextminlen;
3586 args.minalignslop = 0;
3587 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 if ((error = xfs_alloc_vextent(&args)))
3589 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003590 }
3591 if (isaligned && args.fsbno == NULLFSBLOCK) {
3592 /*
3593 * allocation failed, so turn off alignment and
3594 * try again.
3595 */
3596 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003597 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003598 args.alignment = 0;
3599 if ((error = xfs_alloc_vextent(&args)))
3600 return error;
3601 }
3602 if (args.fsbno == NULLFSBLOCK && nullfb &&
3603 args.minlen > ap->minlen) {
3604 args.minlen = ap->minlen;
3605 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003606 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003607 if ((error = xfs_alloc_vextent(&args)))
3608 return error;
3609 }
3610 if (args.fsbno == NULLFSBLOCK && nullfb) {
3611 args.fsbno = 0;
3612 args.type = XFS_ALLOCTYPE_FIRST_AG;
3613 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003614 if ((error = xfs_alloc_vextent(&args)))
3615 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003616 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003617 }
3618 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003619 /*
3620 * check the allocation happened at the same or higher AG than
3621 * the first block that was allocated.
3622 */
3623 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003624 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3625 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003626
Dave Chinner3a756672011-09-18 20:40:58 +00003627 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003628 if (*ap->firstblock == NULLFSBLOCK)
3629 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003630 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003631 ap->length = args.len;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003632 /*
3633 * If the extent size hint is active, we tried to round the
3634 * caller's allocation request offset down to extsz and the
3635 * length up to another extsz boundary. If we found a free
3636 * extent we mapped it in starting at this new offset. If the
3637 * newly mapped space isn't long enough to cover any of the
3638 * range of offsets that was originally requested, move the
3639 * mapping up so that we can fill as much of the caller's
3640 * original request as possible. Free space is apparently
3641 * very fragmented so we're unlikely to be able to satisfy the
3642 * hints anyway.
3643 */
3644 if (ap->length <= orig_length)
3645 ap->offset = orig_offset;
3646 else if (ap->offset + ap->length < orig_offset + orig_length)
3647 ap->offset = orig_offset + orig_length - ap->length;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003648 xfs_bmap_btalloc_accounting(ap, &args);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003649 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003650 ap->blkno = NULLFSBLOCK;
3651 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 }
3653 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003654}
3655
3656/*
3657 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3658 * It figures out where to ask the underlying allocator to put the new extent.
3659 */
3660STATIC int
3661xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003662 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003663{
Dave Chinner292378e2016-09-26 08:21:28 +10003664 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3665 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003666 return xfs_bmap_rtalloc(ap);
3667 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668}
3669
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003670/* Trim extent to fit a logical block range. */
3671void
3672xfs_trim_extent(
3673 struct xfs_bmbt_irec *irec,
3674 xfs_fileoff_t bno,
3675 xfs_filblks_t len)
3676{
3677 xfs_fileoff_t distance;
3678 xfs_fileoff_t end = bno + len;
3679
3680 if (irec->br_startoff + irec->br_blockcount <= bno ||
3681 irec->br_startoff >= end) {
3682 irec->br_blockcount = 0;
3683 return;
3684 }
3685
3686 if (irec->br_startoff < bno) {
3687 distance = bno - irec->br_startoff;
3688 if (isnullstartblock(irec->br_startblock))
3689 irec->br_startblock = DELAYSTARTBLOCK;
3690 if (irec->br_startblock != DELAYSTARTBLOCK &&
3691 irec->br_startblock != HOLESTARTBLOCK)
3692 irec->br_startblock += distance;
3693 irec->br_startoff += distance;
3694 irec->br_blockcount -= distance;
3695 }
3696
3697 if (end < irec->br_startoff + irec->br_blockcount) {
3698 distance = irec->br_startoff + irec->br_blockcount - end;
3699 irec->br_blockcount -= distance;
3700 }
3701}
3702
Brian Foster40214d12017-10-13 09:47:46 -07003703/* trim extent to within eof */
3704void
3705xfs_trim_extent_eof(
3706 struct xfs_bmbt_irec *irec,
3707 struct xfs_inode *ip)
3708
3709{
3710 xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
3711 i_size_read(VFS_I(ip))));
3712}
3713
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003715 * Trim the returned map to the required bounds
3716 */
3717STATIC void
3718xfs_bmapi_trim_map(
3719 struct xfs_bmbt_irec *mval,
3720 struct xfs_bmbt_irec *got,
3721 xfs_fileoff_t *bno,
3722 xfs_filblks_t len,
3723 xfs_fileoff_t obno,
3724 xfs_fileoff_t end,
3725 int n,
3726 int flags)
3727{
3728 if ((flags & XFS_BMAPI_ENTIRE) ||
3729 got->br_startoff + got->br_blockcount <= obno) {
3730 *mval = *got;
3731 if (isnullstartblock(got->br_startblock))
3732 mval->br_startblock = DELAYSTARTBLOCK;
3733 return;
3734 }
3735
3736 if (obno > *bno)
3737 *bno = obno;
3738 ASSERT((*bno >= obno) || (n == 0));
3739 ASSERT(*bno < end);
3740 mval->br_startoff = *bno;
3741 if (isnullstartblock(got->br_startblock))
3742 mval->br_startblock = DELAYSTARTBLOCK;
3743 else
3744 mval->br_startblock = got->br_startblock +
3745 (*bno - got->br_startoff);
3746 /*
3747 * Return the minimum of what we got and what we asked for for
3748 * the length. We can use the len variable here because it is
3749 * modified below and we could have been there before coming
3750 * here if the first part of the allocation didn't overlap what
3751 * was asked for.
3752 */
3753 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3754 got->br_blockcount - (*bno - got->br_startoff));
3755 mval->br_state = got->br_state;
3756 ASSERT(mval->br_blockcount <= len);
3757 return;
3758}
3759
3760/*
3761 * Update and validate the extent map to return
3762 */
3763STATIC void
3764xfs_bmapi_update_map(
3765 struct xfs_bmbt_irec **map,
3766 xfs_fileoff_t *bno,
3767 xfs_filblks_t *len,
3768 xfs_fileoff_t obno,
3769 xfs_fileoff_t end,
3770 int *n,
3771 int flags)
3772{
3773 xfs_bmbt_irec_t *mval = *map;
3774
3775 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3776 ((mval->br_startoff + mval->br_blockcount) <= end));
3777 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3778 (mval->br_startoff < obno));
3779
3780 *bno = mval->br_startoff + mval->br_blockcount;
3781 *len = end - *bno;
3782 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3783 /* update previous map with new information */
3784 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3785 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3786 ASSERT(mval->br_state == mval[-1].br_state);
3787 mval[-1].br_blockcount = mval->br_blockcount;
3788 mval[-1].br_state = mval->br_state;
3789 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3790 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3791 mval[-1].br_startblock != HOLESTARTBLOCK &&
3792 mval->br_startblock == mval[-1].br_startblock +
3793 mval[-1].br_blockcount &&
3794 ((flags & XFS_BMAPI_IGSTATE) ||
3795 mval[-1].br_state == mval->br_state)) {
3796 ASSERT(mval->br_startoff ==
3797 mval[-1].br_startoff + mval[-1].br_blockcount);
3798 mval[-1].br_blockcount += mval->br_blockcount;
3799 } else if (*n > 0 &&
3800 mval->br_startblock == DELAYSTARTBLOCK &&
3801 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3802 mval->br_startoff ==
3803 mval[-1].br_startoff + mval[-1].br_blockcount) {
3804 mval[-1].br_blockcount += mval->br_blockcount;
3805 mval[-1].br_state = mval->br_state;
3806 } else if (!((*n == 0) &&
3807 ((mval->br_startoff + mval->br_blockcount) <=
3808 obno))) {
3809 mval++;
3810 (*n)++;
3811 }
3812 *map = mval;
3813}
3814
3815/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003816 * Map file blocks to filesystem blocks without allocation.
3817 */
3818int
3819xfs_bmapi_read(
3820 struct xfs_inode *ip,
3821 xfs_fileoff_t bno,
3822 xfs_filblks_t len,
3823 struct xfs_bmbt_irec *mval,
3824 int *nmap,
3825 int flags)
3826{
3827 struct xfs_mount *mp = ip->i_mount;
3828 struct xfs_ifork *ifp;
3829 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003830 xfs_fileoff_t obno;
3831 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003832 struct xfs_iext_cursor icur;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003833 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003834 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003835 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003836 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003837
3838 ASSERT(*nmap >= 1);
3839 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003840 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003841 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003842
3843 if (unlikely(XFS_TEST_ERROR(
3844 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3845 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003846 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003847 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003848 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003849 }
3850
3851 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003852 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003853
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003854 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003855
3856 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003857
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003858 /* No CoW fork? Return a hole. */
3859 if (whichfork == XFS_COW_FORK && !ifp) {
3860 mval->br_startoff = bno;
3861 mval->br_startblock = HOLESTARTBLOCK;
3862 mval->br_blockcount = len;
3863 mval->br_state = XFS_EXT_NORM;
3864 *nmap = 1;
3865 return 0;
3866 }
3867
Dave Chinner5c8ed202011-09-18 20:40:45 +00003868 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3869 error = xfs_iread_extents(NULL, ip, whichfork);
3870 if (error)
3871 return error;
3872 }
3873
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003874 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003875 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003876 end = bno + len;
3877 obno = bno;
3878
3879 while (bno < end && n < *nmap) {
3880 /* Reading past eof, act as though there's a hole up to end. */
3881 if (eof)
3882 got.br_startoff = end;
3883 if (got.br_startoff > bno) {
3884 /* Reading in a hole. */
3885 mval->br_startoff = bno;
3886 mval->br_startblock = HOLESTARTBLOCK;
3887 mval->br_blockcount =
3888 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3889 mval->br_state = XFS_EXT_NORM;
3890 bno += mval->br_blockcount;
3891 len -= mval->br_blockcount;
3892 mval++;
3893 n++;
3894 continue;
3895 }
3896
3897 /* set up the extent map to return. */
3898 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3899 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3900
3901 /* If we're done, stop now. */
3902 if (bno >= end || n >= *nmap)
3903 break;
3904
3905 /* Else go on to the next record. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003906 if (!xfs_iext_next_extent(ifp, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003907 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003908 }
3909 *nmap = n;
3910 return 0;
3911}
3912
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003913/*
3914 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3915 * global pool and the extent inserted into the inode in-core extent tree.
3916 *
3917 * On entry, got refers to the first extent beyond the offset of the extent to
3918 * allocate or eof is specified if no such extent exists. On return, got refers
3919 * to the extent record that was inserted to the inode fork.
3920 *
3921 * Note that the allocated extent may have been merged with contiguous extents
3922 * during insertion into the inode fork. Thus, got does not reflect the current
3923 * state of the inode fork on return. If necessary, the caller can use lastx to
3924 * look up the updated record in the inode fork.
3925 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10003926int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003927xfs_bmapi_reserve_delalloc(
3928 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003929 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11003930 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003931 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11003932 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003933 struct xfs_bmbt_irec *got,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003934 struct xfs_iext_cursor *icur,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003935 int eof)
3936{
3937 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003938 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003939 xfs_extlen_t alen;
3940 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003941 int error;
Brian Foster974ae922016-11-28 14:57:42 +11003942 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003943
Brian Foster974ae922016-11-28 14:57:42 +11003944 /*
3945 * Cap the alloc length. Keep track of prealloc so we know whether to
3946 * tag the inode before we return.
3947 */
3948 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003949 if (!eof)
3950 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11003951 if (prealloc && alen >= len)
3952 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003953
3954 /* Figure out the extent size, adjust alen */
Shan Hai6ca30722018-01-23 13:56:11 -08003955 if (whichfork == XFS_COW_FORK) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003956 struct xfs_bmbt_irec prev;
Shan Hai6ca30722018-01-23 13:56:11 -08003957 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003958
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003959 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003960 prev.br_startoff = NULLFILEOFF;
3961
Shan Hai6ca30722018-01-23 13:56:11 -08003962 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003963 1, 0, &aoff, &alen);
3964 ASSERT(!error);
3965 }
3966
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003967 /*
3968 * Make a transaction-less quota reservation for delayed allocation
3969 * blocks. This number gets adjusted later. We return if we haven't
3970 * allocated blocks already inside this loop.
3971 */
3972 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
Shan Hai6ca30722018-01-23 13:56:11 -08003973 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003974 if (error)
3975 return error;
3976
3977 /*
3978 * Split changing sb for alen and indlen since they could be coming
3979 * from different places.
3980 */
3981 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
3982 ASSERT(indlen > 0);
3983
Shan Hai6ca30722018-01-23 13:56:11 -08003984 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003985 if (error)
3986 goto out_unreserve_quota;
3987
Dave Chinner0d485ad2015-02-23 21:22:03 +11003988 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003989 if (error)
3990 goto out_unreserve_blocks;
3991
3992
3993 ip->i_delayed_blks += alen;
3994
3995 got->br_startoff = aoff;
3996 got->br_startblock = nullstartblock(indlen);
3997 got->br_blockcount = alen;
3998 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003999
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004000 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004001
Brian Foster974ae922016-11-28 14:57:42 +11004002 /*
4003 * Tag the inode if blocks were preallocated. Note that COW fork
4004 * preallocation can occur at the start or end of the extent, even when
4005 * prealloc == 0, so we must also check the aligned offset and length.
4006 */
4007 if (whichfork == XFS_DATA_FORK && prealloc)
4008 xfs_inode_set_eofblocks_tag(ip);
4009 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4010 xfs_inode_set_cowblocks_tag(ip);
4011
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004012 return 0;
4013
4014out_unreserve_blocks:
Shan Hai6ca30722018-01-23 13:56:11 -08004015 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004016out_unreserve_quota:
4017 if (XFS_IS_QUOTA_ON(mp))
Shan Hai6ca30722018-01-23 13:56:11 -08004018 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0,
4019 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004020 return error;
4021}
4022
Dave Chinnercf11da92014-07-15 07:08:24 +10004023static int
4024xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004025 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004026{
4027 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004028 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004029 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004030 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004031 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004032
Dave Chinnera99ebf42011-12-01 11:24:20 +00004033 ASSERT(bma->length > 0);
4034
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004035 /*
4036 * For the wasdelay case, we could also just allocate the stuff asked
4037 * for in this bmap call but that wouldn't be as good.
4038 */
4039 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004040 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4041 bma->offset = bma->got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004042 xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004043 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004044 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004045 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004046 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004047 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004048 }
4049
4050 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004051 * Set the data type being allocated. For the data fork, the first data
4052 * in the file is treated differently to all other allocations. For the
4053 * attribute fork, we only need to ensure the allocated range is not on
4054 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004055 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004056 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004057 bma->datatype = XFS_ALLOC_NOBUSY;
4058 if (whichfork == XFS_DATA_FORK) {
4059 if (bma->offset == 0)
4060 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4061 else
4062 bma->datatype |= XFS_ALLOC_USERDATA;
4063 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004064 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004065 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004066 }
4067
Dave Chinnere04426b2012-10-05 11:06:59 +10004068 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004069
4070 /*
4071 * Only want to do the alignment at the eof if it is userdata and
4072 * allocation length is larger than a stripe unit.
4073 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004074 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004075 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004076 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004077 if (error)
4078 return error;
4079 }
4080
4081 error = xfs_bmap_alloc(bma);
4082 if (error)
4083 return error;
4084
Dave Chinner29c8d172011-09-18 20:41:00 +00004085 if (bma->cur)
4086 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004087 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004088 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004089 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4090 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4091 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004092 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004093 }
4094 /*
4095 * Bump the number of extents we've allocated
4096 * in this call.
4097 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004098 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004099
Dave Chinner29c8d172011-09-18 20:41:00 +00004100 if (bma->cur)
4101 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004102 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4103
Dave Chinner963c30c2011-09-18 20:40:59 +00004104 bma->got.br_startoff = bma->offset;
4105 bma->got.br_startblock = bma->blkno;
4106 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004107 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004108
4109 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004110 * In the data fork, a wasdelay extent has been initialized, so
4111 * shouldn't be flagged as unwritten.
4112 *
4113 * For the cow fork, however, we convert delalloc reservations
4114 * (extents allocated for speculative preallocation) to
4115 * allocated unwritten extents, and only convert the unwritten
4116 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004117 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004118 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4119 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004120 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004121 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004122
Christoph Hellwigc6534242011-09-18 20:41:05 +00004123 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004124 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004125 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004126 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004127 whichfork, &bma->icur, &bma->cur, &bma->got,
Christoph Hellwig6d045582017-04-11 16:45:54 -07004128 bma->firstblock, bma->dfops, &bma->logflags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004129
Christoph Hellwigc315c902011-09-18 20:41:02 +00004130 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004131 if (error)
4132 return error;
4133
4134 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004135 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4136 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4137 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004138 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004139 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004140
Dave Chinner963c30c2011-09-18 20:40:59 +00004141 ASSERT(bma->got.br_startoff <= bma->offset);
4142 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4143 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004144 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4145 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004146 return 0;
4147}
4148
Dave Chinnerb447fe52011-09-18 20:40:51 +00004149STATIC int
4150xfs_bmapi_convert_unwritten(
4151 struct xfs_bmalloca *bma,
4152 struct xfs_bmbt_irec *mval,
4153 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004154 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004155{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004156 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004157 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004158 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004159 int error;
4160
Dave Chinnerb447fe52011-09-18 20:40:51 +00004161 /* check if we need to do unwritten->real conversion */
4162 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4163 (flags & XFS_BMAPI_PREALLOC))
4164 return 0;
4165
4166 /* check if we need to do real->unwritten conversion */
4167 if (mval->br_state == XFS_EXT_NORM &&
4168 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4169 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4170 return 0;
4171
4172 /*
4173 * Modify (by adding) the state flag, if writing.
4174 */
4175 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004176 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4177 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004178 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004179 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004180 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004181 }
4182 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4183 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4184
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004185 /*
4186 * Before insertion into the bmbt, zero the range being converted
4187 * if required.
4188 */
4189 if (flags & XFS_BMAPI_ZERO) {
4190 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4191 mval->br_blockcount);
4192 if (error)
4193 return error;
4194 }
4195
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004196 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004197 &bma->icur, &bma->cur, mval, bma->firstblock,
4198 bma->dfops, &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004199 /*
4200 * Log the inode core unconditionally in the unwritten extent conversion
4201 * path because the conversion might not have done so (e.g., if the
4202 * extent count hasn't changed). We need to make sure the inode is dirty
4203 * in the transaction for the sake of fsync(), even if nothing has
4204 * changed, because fsync() will not force the log for this transaction
4205 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004206 *
4207 * Note: If we're only converting cow fork extents, there aren't
4208 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004209 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004210 if (whichfork != XFS_COW_FORK)
4211 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004212 if (error)
4213 return error;
4214
4215 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004216 * Update our extent pointer, given that
4217 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4218 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004219 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004220 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004221
4222 /*
4223 * We may have combined previously unwritten space with written space,
4224 * so generate another request.
4225 */
4226 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004227 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004228 return 0;
4229}
4230
Christoph Hellwig44032802011-09-18 20:40:48 +00004231/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004232 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4233 * extent state if necessary. Details behaviour is controlled by the flags
4234 * parameter. Only allocates blocks from a single allocation group, to avoid
4235 * locking problems.
4236 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 * The returned value in "firstblock" from the first call in a transaction
4238 * must be remembered and presented to subsequent calls in "firstblock".
4239 * An upper bound for the number of blocks to be allocated is supplied to
4240 * the first call in "total"; if no allocation group has that many free
4241 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4242 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004243int
4244xfs_bmapi_write(
4245 struct xfs_trans *tp, /* transaction pointer */
4246 struct xfs_inode *ip, /* incore inode */
4247 xfs_fileoff_t bno, /* starting file offs. mapped */
4248 xfs_filblks_t len, /* length to map in file */
4249 int flags, /* XFS_BMAPI_... */
4250 xfs_fsblock_t *firstblock, /* first allocated block
4251 controls a.g. for allocs */
4252 xfs_extlen_t total, /* total blocks needed */
4253 struct xfs_bmbt_irec *mval, /* output: map values */
4254 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004255 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004257 struct xfs_mount *mp = ip->i_mount;
4258 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004259 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004260 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004261 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004262 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004263 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004264 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004265 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004266
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004268 xfs_fileoff_t orig_bno; /* original block number value */
4269 int orig_flags; /* original flags arg value */
4270 xfs_filblks_t orig_len; /* original value of len arg */
4271 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4272 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
4274 orig_bno = bno;
4275 orig_len = len;
4276 orig_flags = flags;
4277 orig_mval = mval;
4278 orig_nmap = *nmap;
4279#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004280 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004281
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004283 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4284 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004285 ASSERT(tp != NULL ||
4286 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4287 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004288 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004289 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004290 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004291 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004292
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004293 /* zeroing is for currently only for data extents, not metadata */
4294 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4295 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4296 /*
4297 * we can allocate unwritten extents or pre-zero allocated blocks,
4298 * but it makes no sense to do both at once. This would result in
4299 * zeroing the unwritten extent twice, but it still being an
4300 * unwritten extent....
4301 */
4302 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4303 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4304
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 if (unlikely(XFS_TEST_ERROR(
4306 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004307 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004308 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004309 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004310 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004312
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004314 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004315
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004317
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004318 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004319
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004320 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004322 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004324 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004325 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004326 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004327 }
4328
4329 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4330 error = xfs_iread_extents(tp, ip, whichfork);
4331 if (error)
4332 goto error0;
4333 }
4334
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 n = 0;
4336 end = bno + len;
4337 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004338
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004339 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004340 eof = true;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004341 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004342 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004343 bma.tp = tp;
4344 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004345 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004346 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004347 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004348 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004349
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004351 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004352
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004353 /* in hole or beyond EOF? */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004354 if (eof || bma.got.br_startoff > bno) {
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004355 /*
4356 * CoW fork conversions should /never/ hit EOF or
4357 * holes. There should always be something for us
4358 * to work on.
4359 */
4360 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4361 (flags & XFS_BMAPI_COWFORK)));
4362
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004363 if (flags & XFS_BMAPI_DELALLOC) {
4364 /*
4365 * For the COW fork we can reasonably get a
4366 * request for converting an extent that races
4367 * with other threads already having converted
4368 * part of it, as there converting COW to
4369 * regular blocks is not protected using the
4370 * IOLOCK.
4371 */
4372 ASSERT(flags & XFS_BMAPI_COWFORK);
4373 if (!(flags & XFS_BMAPI_COWFORK)) {
4374 error = -EIO;
4375 goto error0;
4376 }
4377
4378 if (eof || bno >= end)
4379 break;
4380 } else {
4381 need_alloc = true;
4382 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004383 } else if (isnullstartblock(bma.got.br_startblock)) {
4384 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004385 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004386
4387 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 * First, deal with the hole before the allocated space
4389 * that we found, if any.
4390 */
Christoph Hellwigb1214592017-11-03 10:34:44 -07004391 if ((need_alloc || wasdelay) &&
4392 !(flags & XFS_BMAPI_CONVERT_ONLY)) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004393 bma.eof = eof;
4394 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4395 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004396 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004397 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004398
Dave Chinnera99ebf42011-12-01 11:24:20 +00004399 /*
4400 * There's a 32/64 bit type mismatch between the
4401 * allocation length request (which can be 64 bits in
4402 * length) and the bma length request, which is
4403 * xfs_extlen_t and therefore 32 bits. Hence we have to
4404 * check for 32-bit overflows and handle them here.
4405 */
4406 if (len > (xfs_filblks_t)MAXEXTLEN)
4407 bma.length = MAXEXTLEN;
4408 else
4409 bma.length = len;
4410
4411 ASSERT(len > 0);
4412 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004413 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 if (error)
4415 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004416 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004417 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004418
4419 /*
4420 * If this is a CoW allocation, record the data in
4421 * the refcount btree for orphan recovery.
4422 */
4423 if (whichfork == XFS_COW_FORK) {
4424 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4425 bma.blkno, bma.length);
4426 if (error)
4427 goto error0;
4428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004430
Dave Chinneraef9a892011-09-18 20:40:44 +00004431 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004432 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4433 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
Dave Chinnerb447fe52011-09-18 20:40:51 +00004435 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004436 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004437 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004438 continue;
4439 if (error)
4440 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441
Dave Chinneraef9a892011-09-18 20:40:44 +00004442 /* update the extent map to return */
4443 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4444
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 /*
4446 * If we're done, stop now. Stop when we've allocated
4447 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4448 * the transaction may get too big.
4449 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004450 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004452
4453 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004454 bma.prev = bma.got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004455 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004456 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004459
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 /*
4461 * Transform from btree to extents, give it cur.
4462 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004463 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004464 int tmp_logflags = 0;
4465
Dave Chinner29c8d172011-09-18 20:41:00 +00004466 ASSERT(bma.cur);
4467 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004469 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 if (error)
4471 goto error0;
4472 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004473
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004475 XFS_IFORK_NEXTENTS(ip, whichfork) >
4476 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478error0:
4479 /*
4480 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004481 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004483 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004485 bma.logflags &= ~xfs_ilog_fext(whichfork);
4486 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004488 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 /*
4490 * Log whatever the flags say, even if error. Otherwise we might miss
4491 * detecting a case where the data is changed, there's an error,
4492 * and it's not logged so we don't shutdown when we should.
4493 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004494 if (bma.logflags)
4495 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004496
Dave Chinner29c8d172011-09-18 20:41:00 +00004497 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 if (!error) {
4499 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004500 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004502 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004503 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004505 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4507 }
4508 if (!error)
4509 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4510 orig_nmap, *nmap);
4511 return error;
4512}
4513
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004514static int
4515xfs_bmapi_remap(
4516 struct xfs_trans *tp,
4517 struct xfs_inode *ip,
4518 xfs_fileoff_t bno,
4519 xfs_filblks_t len,
4520 xfs_fsblock_t startblock,
4521 struct xfs_defer_ops *dfops)
4522{
4523 struct xfs_mount *mp = ip->i_mount;
4524 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4525 struct xfs_btree_cur *cur = NULL;
4526 xfs_fsblock_t firstblock = NULLFSBLOCK;
4527 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004528 struct xfs_iext_cursor icur;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004529 int logflags = 0, error;
4530
4531 ASSERT(len > 0);
4532 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4533 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4534
4535 if (unlikely(XFS_TEST_ERROR(
4536 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4537 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004538 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004539 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4540 return -EFSCORRUPTED;
4541 }
4542
4543 if (XFS_FORCED_SHUTDOWN(mp))
4544 return -EIO;
4545
4546 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4547 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4548 if (error)
4549 return error;
4550 }
4551
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004552 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004553 /* make sure we only reflink into a hole. */
4554 ASSERT(got.br_startoff > bno);
4555 ASSERT(got.br_startoff - bno >= len);
4556 }
4557
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004558 ip->i_d.di_nblocks += len;
4559 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004560
4561 if (ifp->if_flags & XFS_IFBROOT) {
4562 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4563 cur->bc_private.b.firstblock = firstblock;
4564 cur->bc_private.b.dfops = dfops;
4565 cur->bc_private.b.flags = 0;
4566 }
4567
4568 got.br_startoff = bno;
4569 got.br_startblock = startblock;
4570 got.br_blockcount = len;
4571 got.br_state = XFS_EXT_NORM;
4572
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004573 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &icur,
4574 &cur, &got, &firstblock, dfops, &logflags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004575 if (error)
4576 goto error0;
4577
4578 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4579 int tmp_logflags = 0;
4580
4581 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4582 &tmp_logflags, XFS_DATA_FORK);
4583 logflags |= tmp_logflags;
4584 }
4585
4586error0:
4587 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4588 logflags &= ~XFS_ILOG_DEXT;
4589 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4590 logflags &= ~XFS_ILOG_DBROOT;
4591
4592 if (logflags)
4593 xfs_trans_log_inode(tp, ip, logflags);
4594 if (cur) {
4595 xfs_btree_del_cursor(cur,
4596 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4597 }
4598 return error;
4599}
4600
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004602 * When a delalloc extent is split (e.g., due to a hole punch), the original
4603 * indlen reservation must be shared across the two new extents that are left
4604 * behind.
4605 *
4606 * Given the original reservation and the worst case indlen for the two new
4607 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c92016-03-15 11:42:47 +11004608 * reservation fairly across the two new extents. If necessary, steal available
4609 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4610 * ores == 1). The number of stolen blocks is returned. The availability and
4611 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004612 */
Brian Fosterd34999c92016-03-15 11:42:47 +11004613static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004614xfs_bmap_split_indlen(
4615 xfs_filblks_t ores, /* original res. */
4616 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c92016-03-15 11:42:47 +11004617 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4618 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004619{
4620 xfs_filblks_t len1 = *indlen1;
4621 xfs_filblks_t len2 = *indlen2;
4622 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c92016-03-15 11:42:47 +11004623 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004624 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004625
4626 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11004627 * Steal as many blocks as we can to try and satisfy the worst case
4628 * indlen for both new extents.
4629 */
Brian Foster75d65362017-02-13 22:48:30 -08004630 if (ores < nres && avail)
4631 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4632 ores += stolen;
4633
4634 /* nothing else to do if we've satisfied the new reservation */
4635 if (ores >= nres)
4636 return stolen;
Brian Fosterd34999c92016-03-15 11:42:47 +11004637
4638 /*
Brian Foster75d65362017-02-13 22:48:30 -08004639 * We can't meet the total required reservation for the two extents.
4640 * Calculate the percent of the overall shortage between both extents
4641 * and apply this percentage to each of the requested indlen values.
4642 * This distributes the shortage fairly and reduces the chances that one
4643 * of the two extents is left with nothing when extents are repeatedly
4644 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004645 */
Brian Foster75d65362017-02-13 22:48:30 -08004646 resfactor = (ores * 100);
4647 do_div(resfactor, nres);
4648 len1 *= resfactor;
4649 do_div(len1, 100);
4650 len2 *= resfactor;
4651 do_div(len2, 100);
4652 ASSERT(len1 + len2 <= ores);
4653 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4654
4655 /*
4656 * Hand out the remainder to each extent. If one of the two reservations
4657 * is zero, we want to make sure that one gets a block first. The loop
4658 * below starts with len1, so hand len2 a block right off the bat if it
4659 * is zero.
4660 */
4661 ores -= (len1 + len2);
4662 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4663 if (ores && !len2 && *indlen2) {
4664 len2++;
4665 ores--;
4666 }
4667 while (ores) {
4668 if (len1 < *indlen1) {
4669 len1++;
4670 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004671 }
Brian Foster75d65362017-02-13 22:48:30 -08004672 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004673 break;
Brian Foster75d65362017-02-13 22:48:30 -08004674 if (len2 < *indlen2) {
4675 len2++;
4676 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004677 }
4678 }
4679
4680 *indlen1 = len1;
4681 *indlen2 = len2;
Brian Fosterd34999c92016-03-15 11:42:47 +11004682
4683 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004684}
4685
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004686int
4687xfs_bmap_del_extent_delay(
4688 struct xfs_inode *ip,
4689 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004690 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004691 struct xfs_bmbt_irec *got,
4692 struct xfs_bmbt_irec *del)
4693{
4694 struct xfs_mount *mp = ip->i_mount;
4695 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4696 struct xfs_bmbt_irec new;
4697 int64_t da_old, da_new, da_diff = 0;
4698 xfs_fileoff_t del_endoff, got_endoff;
4699 xfs_filblks_t got_indlen, new_indlen, stolen;
Christoph Hellwig060ea652017-10-19 11:02:29 -07004700 int state = xfs_bmap_fork_to_state(whichfork);
4701 int error = 0;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004702 bool isrt;
4703
4704 XFS_STATS_INC(mp, xs_del_exlist);
4705
4706 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4707 del_endoff = del->br_startoff + del->br_blockcount;
4708 got_endoff = got->br_startoff + got->br_blockcount;
4709 da_old = startblockval(got->br_startblock);
4710 da_new = 0;
4711
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004712 ASSERT(del->br_blockcount > 0);
4713 ASSERT(got->br_startoff <= del->br_startoff);
4714 ASSERT(got_endoff >= del_endoff);
4715
4716 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004717 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004718
4719 do_div(rtexts, mp->m_sb.sb_rextsize);
4720 xfs_mod_frextents(mp, rtexts);
4721 }
4722
4723 /*
4724 * Update the inode delalloc counter now and wait to update the
4725 * sb counters as we might have to borrow some blocks for the
4726 * indirect block accounting.
4727 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004728 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4729 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004730 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004731 if (error)
4732 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004733 ip->i_delayed_blks -= del->br_blockcount;
4734
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004735 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004736 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004737 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004738 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004739
Christoph Hellwig0173c682017-10-17 14:16:22 -07004740 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4741 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004742 /*
4743 * Matches the whole extent. Delete the entry.
4744 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004745 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004746 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004747 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004748 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004749 /*
4750 * Deleting the first part of the extent.
4751 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004752 got->br_startoff = del_endoff;
4753 got->br_blockcount -= del->br_blockcount;
4754 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4755 got->br_blockcount), da_old);
4756 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004757 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004758 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004759 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004760 /*
4761 * Deleting the last part of the extent.
4762 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004763 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4764 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4765 got->br_blockcount), da_old);
4766 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004767 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004768 break;
4769 case 0:
4770 /*
4771 * Deleting the middle of the extent.
4772 *
4773 * Distribute the original indlen reservation across the two new
4774 * extents. Steal blocks from the deleted extent if necessary.
4775 * Stealing blocks simply fudges the fdblocks accounting below.
4776 * Warn if either of the new indlen reservations is zero as this
4777 * can lead to delalloc problems.
4778 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004779 got->br_blockcount = del->br_startoff - got->br_startoff;
4780 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4781
4782 new.br_blockcount = got_endoff - del_endoff;
4783 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4784
4785 WARN_ON_ONCE(!got_indlen || !new_indlen);
4786 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4787 del->br_blockcount);
4788
4789 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004790
4791 new.br_startoff = del_endoff;
4792 new.br_state = got->br_state;
4793 new.br_startblock = nullstartblock((int)new_indlen);
4794
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004795 xfs_iext_update_extent(ip, state, icur, got);
4796 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004797 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004798
4799 da_new = got_indlen + new_indlen - stolen;
4800 del->br_blockcount -= stolen;
4801 break;
4802 }
4803
4804 ASSERT(da_old >= da_new);
4805 da_diff = da_old - da_new;
4806 if (!isrt)
4807 da_diff += del->br_blockcount;
4808 if (da_diff)
4809 xfs_mod_fdblocks(mp, da_diff, false);
4810 return error;
4811}
4812
4813void
4814xfs_bmap_del_extent_cow(
4815 struct xfs_inode *ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004816 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004817 struct xfs_bmbt_irec *got,
4818 struct xfs_bmbt_irec *del)
4819{
4820 struct xfs_mount *mp = ip->i_mount;
4821 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4822 struct xfs_bmbt_irec new;
4823 xfs_fileoff_t del_endoff, got_endoff;
4824 int state = BMAP_COWFORK;
4825
4826 XFS_STATS_INC(mp, xs_del_exlist);
4827
4828 del_endoff = del->br_startoff + del->br_blockcount;
4829 got_endoff = got->br_startoff + got->br_blockcount;
4830
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004831 ASSERT(del->br_blockcount > 0);
4832 ASSERT(got->br_startoff <= del->br_startoff);
4833 ASSERT(got_endoff >= del_endoff);
4834 ASSERT(!isnullstartblock(got->br_startblock));
4835
4836 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004837 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004838 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004839 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004840
Christoph Hellwig0173c682017-10-17 14:16:22 -07004841 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4842 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004843 /*
4844 * Matches the whole extent. Delete the entry.
4845 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004846 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004847 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004848 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004849 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004850 /*
4851 * Deleting the first part of the extent.
4852 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004853 got->br_startoff = del_endoff;
4854 got->br_blockcount -= del->br_blockcount;
4855 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004856 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004857 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004858 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004859 /*
4860 * Deleting the last part of the extent.
4861 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004862 got->br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004863 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004864 break;
4865 case 0:
4866 /*
4867 * Deleting the middle of the extent.
4868 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004869 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004870
4871 new.br_startoff = del_endoff;
4872 new.br_blockcount = got_endoff - del_endoff;
4873 new.br_state = got->br_state;
4874 new.br_startblock = del->br_startblock + del->br_blockcount;
4875
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004876 xfs_iext_update_extent(ip, state, icur, got);
4877 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004878 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004879 break;
4880 }
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08004881 ip->i_delayed_blks -= del->br_blockcount;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004882}
4883
Brian Fostera9bd24a2016-03-15 11:42:46 +11004884/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11004885 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07004886 * after removing space.
Dave Chinner9e5987a2013-02-25 12:31:26 +11004887 */
4888STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07004889xfs_bmap_del_extent_real(
Dave Chinner9e5987a2013-02-25 12:31:26 +11004890 xfs_inode_t *ip, /* incore inode pointer */
4891 xfs_trans_t *tp, /* current transaction pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004892 struct xfs_iext_cursor *icur,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004893 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004894 xfs_btree_cur_t *cur, /* if null, not a btree */
4895 xfs_bmbt_irec_t *del, /* data to remove from extents */
4896 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07004897 int whichfork, /* data or attr fork */
4898 int bflags) /* bmapi flags */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004899{
Dave Chinner9e5987a2013-02-25 12:31:26 +11004900 xfs_fsblock_t del_endblock=0; /* first block past del */
4901 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004902 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004903 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004904 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004905 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004906 xfs_fileoff_t got_endoff; /* first offset past got */
4907 int i; /* temp state */
4908 xfs_ifork_t *ifp; /* inode fork pointer */
4909 xfs_mount_t *mp; /* mount structure */
4910 xfs_filblks_t nblks; /* quota/sb block count */
4911 xfs_bmbt_irec_t new; /* new record to be inserted */
4912 /* REFERENCED */
4913 uint qfield; /* quota field to update */
Christoph Hellwig060ea652017-10-19 11:02:29 -07004914 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004915 struct xfs_bmbt_irec old;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004916
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004917 mp = ip->i_mount;
4918 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004919
Dave Chinner9e5987a2013-02-25 12:31:26 +11004920 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004921 ASSERT(del->br_blockcount > 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004922 xfs_iext_get_extent(ifp, icur, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004923 ASSERT(got.br_startoff <= del->br_startoff);
4924 del_endoff = del->br_startoff + del->br_blockcount;
4925 got_endoff = got.br_startoff + got.br_blockcount;
4926 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004927 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +11004928 qfield = 0;
4929 error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004930
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004931 /*
4932 * If it's the case where the directory code is running with no block
4933 * reservation, and the deleted block is in the middle of its extent,
4934 * and the resulting insert of an extent would cause transformation to
4935 * btree format, then reject it. The calling code will then swap blocks
4936 * around instead. We have to do this now, rather than waiting for the
4937 * conversion to btree format, since the transaction will be dirty then.
4938 */
4939 if (tp->t_blk_res == 0 &&
4940 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4941 XFS_IFORK_NEXTENTS(ip, whichfork) >=
4942 XFS_IFORK_MAXEXT(ip, whichfork) &&
4943 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
4944 return -ENOSPC;
4945
4946 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004947 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4948 xfs_fsblock_t bno;
4949 xfs_filblks_t len;
4950
4951 ASSERT(do_mod(del->br_blockcount, mp->m_sb.sb_rextsize) == 0);
4952 ASSERT(do_mod(del->br_startblock, mp->m_sb.sb_rextsize) == 0);
4953 bno = del->br_startblock;
4954 len = del->br_blockcount;
4955 do_div(bno, mp->m_sb.sb_rextsize);
4956 do_div(len, mp->m_sb.sb_rextsize);
4957 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4958 if (error)
4959 goto done;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004960 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004961 nblks = len * mp->m_sb.sb_rextsize;
4962 qfield = XFS_TRANS_DQ_RTBCOUNT;
4963 } else {
4964 do_fx = 1;
4965 nblks = del->br_blockcount;
4966 qfield = XFS_TRANS_DQ_BCOUNT;
4967 }
4968
4969 del_endblock = del->br_startblock + del->br_blockcount;
4970 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07004971 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004972 if (error)
4973 goto done;
4974 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004975 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10004976
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004977 if (got.br_startoff == del->br_startoff)
4978 state |= BMAP_LEFT_FILLING;
4979 if (got_endoff == del_endoff)
4980 state |= BMAP_RIGHT_FILLING;
4981
4982 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4983 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11004984 /*
4985 * Matches the whole extent. Delete the entry.
4986 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004987 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004988 xfs_iext_prev(ifp, icur);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004989 XFS_IFORK_NEXT_SET(ip, whichfork,
4990 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4991 flags |= XFS_ILOG_CORE;
4992 if (!cur) {
4993 flags |= xfs_ilog_fext(whichfork);
4994 break;
4995 }
4996 if ((error = xfs_btree_delete(cur, &i)))
4997 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004998 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004999 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005000 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11005001 /*
5002 * Deleting the first part of the extent.
5003 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005004 got.br_startoff = del_endoff;
5005 got.br_startblock = del_endblock;
5006 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005007 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005008 if (!cur) {
5009 flags |= xfs_ilog_fext(whichfork);
5010 break;
5011 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005012 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005013 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005014 goto done;
5015 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005016 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11005017 /*
5018 * Deleting the last part of the extent.
5019 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005020 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005021 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005022 if (!cur) {
5023 flags |= xfs_ilog_fext(whichfork);
5024 break;
5025 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005026 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005027 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005028 goto done;
5029 break;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005030 case 0:
5031 /*
5032 * Deleting the middle of the extent.
5033 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005034 old = got;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07005035
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005036 got.br_blockcount = del->br_startoff - got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005037 xfs_iext_update_extent(ip, state, icur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005038
Dave Chinner9e5987a2013-02-25 12:31:26 +11005039 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005040 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005041 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005042 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005043
Christoph Hellwige1d75532017-10-17 14:16:21 -07005044 flags |= XFS_ILOG_CORE;
5045 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005046 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005047 if (error)
5048 goto done;
5049 error = xfs_btree_increment(cur, 0, &i);
5050 if (error)
5051 goto done;
5052 cur->bc_rec.b = new;
5053 error = xfs_btree_insert(cur, &i);
5054 if (error && error != -ENOSPC)
5055 goto done;
5056 /*
5057 * If get no-space back from btree insert, it tried a
5058 * split, and we have a zero block reservation. Fix up
5059 * our state and return the error.
5060 */
5061 if (error == -ENOSPC) {
5062 /*
5063 * Reset the cursor, don't trust it after any
5064 * insert operation.
5065 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005066 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005067 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005068 goto done;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005069 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5070 /*
5071 * Update the btree record back
5072 * to the original value.
5073 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005074 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005075 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005076 goto done;
5077 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005078 * Reset the extent record back
5079 * to the original value.
Dave Chinner9e5987a2013-02-25 12:31:26 +11005080 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005081 xfs_iext_update_extent(ip, state, icur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005082 flags = 0;
5083 error = -ENOSPC;
5084 goto done;
5085 }
5086 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5087 } else
5088 flags |= xfs_ilog_fext(whichfork);
5089 XFS_IFORK_NEXT_SET(ip, whichfork,
5090 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005091 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005092 xfs_iext_insert(ip, icur, &new, state);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005093 break;
5094 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005095
5096 /* remove reverse mapping */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005097 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5098 if (error)
5099 goto done;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005100
Dave Chinner9e5987a2013-02-25 12:31:26 +11005101 /*
5102 * If we need to, add to list of extents to delete.
5103 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005104 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005105 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5106 error = xfs_refcount_decrease_extent(mp, dfops, del);
5107 if (error)
5108 goto done;
5109 } else
5110 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5111 del->br_blockcount, NULL);
5112 }
5113
Dave Chinner9e5987a2013-02-25 12:31:26 +11005114 /*
5115 * Adjust inode # blocks in the file.
5116 */
5117 if (nblks)
5118 ip->i_d.di_nblocks -= nblks;
5119 /*
5120 * Adjust quota data.
5121 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005122 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a2013-02-25 12:31:26 +11005123 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5124
Dave Chinner9e5987a2013-02-25 12:31:26 +11005125done:
5126 *logflagsp = flags;
5127 return error;
5128}
5129
5130/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 * Unmap (remove) blocks from a file.
5132 * If nexts is nonzero then the number of extents to remove is limited to
5133 * that value. If not all extents in the block range can be removed then
5134 * *done is set.
5135 */
5136int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005137__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 xfs_trans_t *tp, /* transaction pointer */
5139 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005140 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005141 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 int flags, /* misc flags */
5143 xfs_extnum_t nexts, /* number of extents max */
5144 xfs_fsblock_t *firstblock, /* first allocated block
5145 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005146 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147{
5148 xfs_btree_cur_t *cur; /* bmap btree cursor */
5149 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 int error; /* error return value */
5151 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005152 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 xfs_ifork_t *ifp; /* inode fork pointer */
5154 int isrt; /* freeing in rt area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 int logflags; /* transaction logging flags */
5156 xfs_extlen_t mod; /* rt extent offset */
5157 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 int tmp_logflags; /* partial logging flags */
5159 int wasdel; /* was a delayed alloc extent */
5160 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005162 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005163 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005164 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005165 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005166 struct xfs_iext_cursor icur;
5167 bool done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005169 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005170
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005171 whichfork = xfs_bmapi_whichfork(flags);
5172 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 ifp = XFS_IFORK_PTR(ip, whichfork);
5174 if (unlikely(
5175 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5176 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5177 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5178 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005179 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 }
5181 mp = ip->i_mount;
5182 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005183 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005184
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005185 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 ASSERT(len > 0);
5187 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005188
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005189 /*
5190 * Guesstimate how many blocks we can unmap without running the risk of
5191 * blowing out the transaction with a mix of EFIs and reflink
5192 * adjustments.
5193 */
Darrick J. Wong8c57b882017-12-10 18:03:53 -08005194 if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005195 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5196 else
5197 max_len = len;
5198
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5200 (error = xfs_iread_extents(tp, ip, whichfork)))
5201 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005202 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005203 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 return 0;
5205 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005206 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005207 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwigdc560152017-10-23 16:32:39 -07005208 end = start + len;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005209
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005210 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
Christoph Hellwigdc560152017-10-23 16:32:39 -07005211 *rlen = 0;
5212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 }
Christoph Hellwigdc560152017-10-23 16:32:39 -07005214 end--;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005215
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 logflags = 0;
5217 if (ifp->if_flags & XFS_IFBROOT) {
5218 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005219 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005221 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 cur->bc_private.b.flags = 0;
5223 } else
5224 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005225
5226 if (isrt) {
5227 /*
5228 * Synchronize by locking the bitmap inode.
5229 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005230 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005231 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005232 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5233 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005234 }
5235
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 extno = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005237 while (end != (xfs_fileoff_t)-1 && end >= start &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005238 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005240 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 * Just back up to the previous extent, if so.
5242 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005243 if (got.br_startoff > end &&
5244 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5245 done = true;
5246 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 }
5248 /*
5249 * Is the last block of this extent before the range
5250 * we're supposed to delete? If so, we're done.
5251 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005252 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005254 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 break;
5256 /*
5257 * Then deal with the (possibly delayed) allocated space
5258 * we found.
5259 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005261 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005262
5263 /*
5264 * Make sure we don't touch multiple AGF headers out of order
5265 * in a single transaction, as that could cause AB-BA deadlocks.
5266 */
5267 if (!wasdel) {
5268 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5269 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5270 break;
5271 prev_agno = agno;
5272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 if (got.br_startoff < start) {
5274 del.br_startoff = start;
5275 del.br_blockcount -= start - got.br_startoff;
5276 if (!wasdel)
5277 del.br_startblock += start - got.br_startoff;
5278 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005279 if (del.br_startoff + del.br_blockcount > end + 1)
5280 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005281
5282 /* How much can we safely unmap? */
5283 if (max_len < del.br_blockcount) {
5284 del.br_startoff += del.br_blockcount - max_len;
5285 if (!wasdel)
5286 del.br_startblock += del.br_blockcount - max_len;
5287 del.br_blockcount = max_len;
5288 }
5289
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 sum = del.br_startblock + del.br_blockcount;
5291 if (isrt &&
5292 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5293 /*
5294 * Realtime extent not lined up at the end.
5295 * The extent could have been split into written
5296 * and unwritten pieces, or we could just be
5297 * unmapping part of it. But we can't really
5298 * get rid of part of a realtime extent.
5299 */
5300 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005301 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 /*
5303 * This piece is unwritten, or we're not
5304 * using unwritten extents. Skip over it.
5305 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005306 ASSERT(end >= mod);
5307 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 del.br_blockcount : mod;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005309 if (end < got.br_startoff &&
5310 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5311 done = true;
5312 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 }
5314 continue;
5315 }
5316 /*
5317 * It's written, turn it unwritten.
5318 * This is better than zeroing it.
5319 */
5320 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005321 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 /*
5323 * If this spans a realtime extent boundary,
5324 * chop it back to the start of the one we end at.
5325 */
5326 if (del.br_blockcount > mod) {
5327 del.br_startoff += del.br_blockcount - mod;
5328 del.br_startblock += del.br_blockcount - mod;
5329 del.br_blockcount = mod;
5330 }
5331 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005332 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005333 whichfork, &icur, &cur, &del,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005334 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 if (error)
5336 goto error0;
5337 goto nodelete;
5338 }
5339 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5340 /*
5341 * Realtime extent is lined up at the end but not
5342 * at the front. We'll get rid of full extents if
5343 * we can.
5344 */
5345 mod = mp->m_sb.sb_rextsize - mod;
5346 if (del.br_blockcount > mod) {
5347 del.br_blockcount -= mod;
5348 del.br_startoff += mod;
5349 del.br_startblock += mod;
5350 } else if ((del.br_startoff == start &&
5351 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005352 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005353 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 /*
5355 * Can't make it unwritten. There isn't
5356 * a full extent here so just skip it.
5357 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005358 ASSERT(end >= del.br_blockcount);
5359 end -= del.br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005360 if (got.br_startoff > end &&
5361 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5362 done = true;
5363 break;
5364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 continue;
5366 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005367 struct xfs_bmbt_irec prev;
5368
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 /*
5370 * This one is already unwritten.
5371 * It must have a written left neighbor.
5372 * Unwrite the killed part of that one and
5373 * try again.
5374 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005375 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5376 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005378 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 ASSERT(del.br_startblock ==
5380 prev.br_startblock + prev.br_blockcount);
5381 if (prev.br_startoff < start) {
5382 mod = start - prev.br_startoff;
5383 prev.br_blockcount -= mod;
5384 prev.br_startblock += mod;
5385 prev.br_startoff = start;
5386 }
5387 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005388 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005389 ip, whichfork, &icur, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005390 &prev, firstblock, dfops,
5391 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 if (error)
5393 goto error0;
5394 goto nodelete;
5395 } else {
5396 ASSERT(del.br_state == XFS_EXT_NORM);
5397 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005398 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005399 ip, whichfork, &icur, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005400 &del, firstblock, dfops,
5401 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 if (error)
5403 goto error0;
5404 goto nodelete;
5405 }
5406 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005407
Brian Fosterb2706a02016-03-15 11:42:46 +11005408 if (wasdel) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005409 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005410 &got, &del);
5411 } else {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005412 error = xfs_bmap_del_extent_real(ip, tp, &icur, dfops,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005413 cur, &del, &tmp_logflags, whichfork,
5414 flags);
5415 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005416 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005417
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 if (error)
5419 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005420
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005421 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005422 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 /*
5425 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005427 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005428 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5429 (got.br_startoff > end &&
5430 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5431 done = true;
5432 break;
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 extno++;
5435 }
5436 }
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005437 if (done || end == (xfs_fileoff_t)-1 || end < start)
Darrick J. Wong44535932016-10-03 09:11:29 -07005438 *rlen = 0;
5439 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005440 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005441
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 /*
5443 * Convert to a btree if necessary.
5444 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005445 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005447 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 &cur, 0, &tmp_logflags, whichfork);
5449 logflags |= tmp_logflags;
5450 if (error)
5451 goto error0;
5452 }
5453 /*
5454 * transform from btree to extents, give it cur
5455 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005456 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 ASSERT(cur != NULL);
5458 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5459 whichfork);
5460 logflags |= tmp_logflags;
5461 if (error)
5462 goto error0;
5463 }
5464 /*
5465 * transform from extents to local?
5466 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 error = 0;
5468error0:
5469 /*
5470 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005471 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005473 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005475 logflags &= ~xfs_ilog_fext(whichfork);
5476 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005478 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 /*
5480 * Log inode even in the error case, if the transaction
5481 * is dirty we'll need to shut down the filesystem.
5482 */
5483 if (logflags)
5484 xfs_trans_log_inode(tp, ip, logflags);
5485 if (cur) {
5486 if (!error) {
5487 *firstblock = cur->bc_private.b.firstblock;
5488 cur->bc_private.b.allocated = 0;
5489 }
5490 xfs_btree_del_cursor(cur,
5491 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5492 }
5493 return error;
5494}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005495
Darrick J. Wong44535932016-10-03 09:11:29 -07005496/* Unmap a range of a file. */
5497int
5498xfs_bunmapi(
5499 xfs_trans_t *tp,
5500 struct xfs_inode *ip,
5501 xfs_fileoff_t bno,
5502 xfs_filblks_t len,
5503 int flags,
5504 xfs_extnum_t nexts,
5505 xfs_fsblock_t *firstblock,
5506 struct xfs_defer_ops *dfops,
5507 int *done)
5508{
5509 int error;
5510
5511 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5512 dfops);
5513 *done = (len == 0);
5514 return error;
5515}
5516
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005517/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005518 * Determine whether an extent shift can be accomplished by a merge with the
5519 * extent that precedes the target hole of the shift.
5520 */
5521STATIC bool
5522xfs_bmse_can_merge(
5523 struct xfs_bmbt_irec *left, /* preceding extent */
5524 struct xfs_bmbt_irec *got, /* current extent to shift */
5525 xfs_fileoff_t shift) /* shift fsb */
5526{
5527 xfs_fileoff_t startoff;
5528
5529 startoff = got->br_startoff - shift;
5530
5531 /*
5532 * The extent, once shifted, must be adjacent in-file and on-disk with
5533 * the preceding extent.
5534 */
5535 if ((left->br_startoff + left->br_blockcount != startoff) ||
5536 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5537 (left->br_state != got->br_state) ||
5538 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5539 return false;
5540
5541 return true;
5542}
5543
5544/*
5545 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5546 * hole in the file. If an extent shift would result in the extent being fully
5547 * adjacent to the extent that currently precedes the hole, we can merge with
5548 * the preceding extent rather than do the shift.
5549 *
5550 * This function assumes the caller has verified a shift-by-merge is possible
5551 * with the provided extents via xfs_bmse_can_merge().
5552 */
5553STATIC int
5554xfs_bmse_merge(
5555 struct xfs_inode *ip,
5556 int whichfork,
5557 xfs_fileoff_t shift, /* shift fsb */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005558 struct xfs_iext_cursor *icur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005559 struct xfs_bmbt_irec *got, /* extent to shift */
5560 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005561 struct xfs_btree_cur *cur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005562 int *logflags, /* output */
5563 struct xfs_defer_ops *dfops)
Brian Fosterddb19e32014-09-23 15:38:09 +10005564{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005565 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005566 xfs_filblks_t blockcount;
5567 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005568 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005569
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005570 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005571
5572 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5573 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005574 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005575
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005576 new = *left;
5577 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005578
5579 /*
5580 * Update the on-disk extent count, the btree if necessary and log the
5581 * inode.
5582 */
5583 XFS_IFORK_NEXT_SET(ip, whichfork,
5584 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5585 *logflags |= XFS_ILOG_CORE;
5586 if (!cur) {
5587 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005588 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005589 }
5590
5591 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005592 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005593 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005594 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005595 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005596
5597 error = xfs_btree_delete(cur, &i);
5598 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005599 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005600 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005601
5602 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005603 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005604 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005605 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005606 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005607
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005608 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005609 if (error)
5610 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005611
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005612done:
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005613 xfs_iext_remove(ip, icur, 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005614 xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur);
5615 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5616 &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005617
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005618 /* update reverse mapping. rmap functions merge the rmaps for us */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005619 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
5620 if (error)
5621 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005622 memcpy(&new, got, sizeof(new));
5623 new.br_startoff = left->br_startoff + left->br_blockcount;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005624 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005625}
5626
Christoph Hellwigbf806282017-10-19 11:07:34 -07005627static int
5628xfs_bmap_shift_update_extent(
5629 struct xfs_inode *ip,
5630 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005631 struct xfs_iext_cursor *icur,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005632 struct xfs_bmbt_irec *got,
5633 struct xfs_btree_cur *cur,
5634 int *logflags,
5635 struct xfs_defer_ops *dfops,
5636 xfs_fileoff_t startoff)
Brian Fostera979bdf2014-09-23 15:39:04 +10005637{
Christoph Hellwigbf806282017-10-19 11:07:34 -07005638 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005639 struct xfs_bmbt_irec prev = *got;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005640 int error, i;
Brian Fostera979bdf2014-09-23 15:39:04 +10005641
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005642 *logflags |= XFS_ILOG_CORE;
5643
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005644 got->br_startoff = startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005645
5646 if (cur) {
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005647 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005648 if (error)
5649 return error;
5650 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5651
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005652 error = xfs_bmbt_update(cur, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005653 if (error)
5654 return error;
5655 } else {
5656 *logflags |= XFS_ILOG_DEXT;
5657 }
5658
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005659 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5660 got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005661
Darrick J. Wong9c194642016-08-03 12:16:05 +10005662 /* update reverse mapping */
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005663 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &prev);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005664 if (error)
5665 return error;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005666 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005667}
5668
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005669int
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005670xfs_bmap_collapse_extents(
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005671 struct xfs_trans *tp,
5672 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005673 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005674 xfs_fileoff_t offset_shift_fsb,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005675 bool *done,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005676 xfs_fsblock_t *firstblock,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005677 struct xfs_defer_ops *dfops)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005678{
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005679 int whichfork = XFS_DATA_FORK;
5680 struct xfs_mount *mp = ip->i_mount;
5681 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5682 struct xfs_btree_cur *cur = NULL;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005683 struct xfs_bmbt_irec got, prev;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005684 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005685 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005686 int error = 0;
5687 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005688
5689 if (unlikely(XFS_TEST_ERROR(
5690 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5691 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005692 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005693 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005694 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005695 }
5696
5697 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005698 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005699
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005700 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005701
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005702 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005703 error = xfs_iread_extents(tp, ip, whichfork);
5704 if (error)
5705 return error;
5706 }
5707
Brian Fosterddb19e32014-09-23 15:38:09 +10005708 if (ifp->if_flags & XFS_IFBROOT) {
5709 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5710 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005711 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10005712 cur->bc_private.b.flags = 0;
5713 }
5714
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005715 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005716 *done = true;
5717 goto del_cursor;
5718 }
Eric Sandeend41c6172017-11-27 18:23:32 -08005719 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5720 del_cursor);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005721
Christoph Hellwigbf806282017-10-19 11:07:34 -07005722 new_startoff = got.br_startoff - offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005723 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005724 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5725 error = -EINVAL;
5726 goto del_cursor;
5727 }
5728
Christoph Hellwigbf806282017-10-19 11:07:34 -07005729 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5730 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005731 &icur, &got, &prev, cur, &logflags,
5732 dfops);
Christoph Hellwigbf806282017-10-19 11:07:34 -07005733 if (error)
5734 goto del_cursor;
5735 goto done;
5736 }
5737 } else {
5738 if (got.br_startoff < offset_shift_fsb) {
5739 error = -EINVAL;
5740 goto del_cursor;
5741 }
5742 }
5743
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005744 error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur,
5745 &logflags, dfops, new_startoff);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005746 if (error)
5747 goto del_cursor;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005748
Christoph Hellwig42630362017-11-03 10:34:41 -07005749done:
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005750 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5751 *done = true;
5752 goto del_cursor;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005753 }
5754
Christoph Hellwigbf806282017-10-19 11:07:34 -07005755 *next_fsb = got.br_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005756del_cursor:
5757 if (cur)
5758 xfs_btree_del_cursor(cur,
5759 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005760 if (logflags)
5761 xfs_trans_log_inode(tp, ip, logflags);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005762 return error;
5763}
5764
5765int
5766xfs_bmap_insert_extents(
5767 struct xfs_trans *tp,
5768 struct xfs_inode *ip,
5769 xfs_fileoff_t *next_fsb,
5770 xfs_fileoff_t offset_shift_fsb,
5771 bool *done,
5772 xfs_fileoff_t stop_fsb,
5773 xfs_fsblock_t *firstblock,
5774 struct xfs_defer_ops *dfops)
5775{
5776 int whichfork = XFS_DATA_FORK;
5777 struct xfs_mount *mp = ip->i_mount;
5778 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5779 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005780 struct xfs_bmbt_irec got, next;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005781 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005782 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005783 int error = 0;
5784 int logflags = 0;
5785
5786 if (unlikely(XFS_TEST_ERROR(
5787 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5788 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5789 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5790 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
5791 return -EFSCORRUPTED;
5792 }
5793
5794 if (XFS_FORCED_SHUTDOWN(mp))
5795 return -EIO;
5796
5797 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5798
5799 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5800 error = xfs_iread_extents(tp, ip, whichfork);
5801 if (error)
5802 return error;
5803 }
5804
5805 if (ifp->if_flags & XFS_IFBROOT) {
5806 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5807 cur->bc_private.b.firstblock = *firstblock;
5808 cur->bc_private.b.dfops = dfops;
5809 cur->bc_private.b.flags = 0;
5810 }
5811
Namjae Jeona904b1c2015-03-25 15:08:56 +11005812 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005813 xfs_iext_last(ifp, &icur);
5814 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005815 stop_fsb > got.br_startoff) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005816 *done = true;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005817 goto del_cursor;
5818 }
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005819 } else {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005820 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005821 *done = true;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005822 goto del_cursor;
5823 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005824 }
Eric Sandeend41c6172017-11-27 18:23:32 -08005825 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5826 del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005827
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005828 if (stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005829 error = -EIO;
5830 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005831 }
5832
Christoph Hellwigbf806282017-10-19 11:07:34 -07005833 new_startoff = got.br_startoff + offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005834 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005835 if (new_startoff + got.br_blockcount > next.br_startoff) {
5836 error = -EINVAL;
5837 goto del_cursor;
5838 }
5839
5840 /*
5841 * Unlike a left shift (which involves a hole punch), a right
5842 * shift does not modify extent neighbors in any way. We should
5843 * never find mergeable extents in this scenario. Check anyways
5844 * and warn if we encounter two extents that could be one.
5845 */
5846 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5847 WARN_ON_ONCE(1);
5848 }
5849
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005850 error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur,
5851 &logflags, dfops, new_startoff);
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005852 if (error)
5853 goto del_cursor;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005854
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005855 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005856 stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005857 *done = true;
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005858 goto del_cursor;
5859 }
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005860
5861 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005862del_cursor:
5863 if (cur)
5864 xfs_btree_del_cursor(cur,
5865 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Brian Fosterca446d82014-09-02 12:12:53 +10005866 if (logflags)
5867 xfs_trans_log_inode(tp, ip, logflags);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005868 return error;
5869}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005870
5871/*
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005872 * Splits an extent into two extents at split_fsb block such that it is the
5873 * first block of the current_ext. @ext is a target extent to be split.
5874 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5875 * hole or the first block of extents, just return 0.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005876 */
5877STATIC int
5878xfs_bmap_split_extent_at(
5879 struct xfs_trans *tp,
5880 struct xfs_inode *ip,
5881 xfs_fileoff_t split_fsb,
5882 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005883 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005884{
5885 int whichfork = XFS_DATA_FORK;
5886 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005887 struct xfs_bmbt_irec got;
5888 struct xfs_bmbt_irec new; /* split extent */
5889 struct xfs_mount *mp = ip->i_mount;
5890 struct xfs_ifork *ifp;
5891 xfs_fsblock_t gotblkcnt; /* new block count for got */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005892 struct xfs_iext_cursor icur;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005893 int error = 0;
5894 int logflags = 0;
5895 int i = 0;
5896
5897 if (unlikely(XFS_TEST_ERROR(
5898 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5899 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005900 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11005901 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5902 XFS_ERRLEVEL_LOW, mp);
5903 return -EFSCORRUPTED;
5904 }
5905
5906 if (XFS_FORCED_SHUTDOWN(mp))
5907 return -EIO;
5908
5909 ifp = XFS_IFORK_PTR(ip, whichfork);
5910 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5911 /* Read in all the extents */
5912 error = xfs_iread_extents(tp, ip, whichfork);
5913 if (error)
5914 return error;
5915 }
5916
5917 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005918 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005919 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005920 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005921 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005922 return 0;
5923
5924 gotblkcnt = split_fsb - got.br_startoff;
5925 new.br_startoff = split_fsb;
5926 new.br_startblock = got.br_startblock + gotblkcnt;
5927 new.br_blockcount = got.br_blockcount - gotblkcnt;
5928 new.br_state = got.br_state;
5929
5930 if (ifp->if_flags & XFS_IFBROOT) {
5931 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5932 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005933 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005934 cur->bc_private.b.flags = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005935 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005936 if (error)
5937 goto del_cursor;
5938 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5939 }
5940
Namjae Jeona904b1c2015-03-25 15:08:56 +11005941 got.br_blockcount = gotblkcnt;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005942 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
5943 &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005944
5945 logflags = XFS_ILOG_CORE;
5946 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005947 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005948 if (error)
5949 goto del_cursor;
5950 } else
5951 logflags |= XFS_ILOG_DEXT;
5952
5953 /* Add new extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005954 xfs_iext_next(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005955 xfs_iext_insert(ip, &icur, &new, 0);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005956 XFS_IFORK_NEXT_SET(ip, whichfork,
5957 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5958
5959 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005960 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005961 if (error)
5962 goto del_cursor;
5963 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005964 error = xfs_btree_insert(cur, &i);
5965 if (error)
5966 goto del_cursor;
5967 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5968 }
5969
5970 /*
5971 * Convert to a btree if necessary.
5972 */
5973 if (xfs_bmap_needs_btree(ip, whichfork)) {
5974 int tmp_logflags; /* partial log flag return val */
5975
5976 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005977 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005978 &cur, 0, &tmp_logflags, whichfork);
5979 logflags |= tmp_logflags;
5980 }
5981
5982del_cursor:
5983 if (cur) {
5984 cur->bc_private.b.allocated = 0;
5985 xfs_btree_del_cursor(cur,
5986 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5987 }
5988
5989 if (logflags)
5990 xfs_trans_log_inode(tp, ip, logflags);
5991 return error;
5992}
5993
5994int
5995xfs_bmap_split_extent(
5996 struct xfs_inode *ip,
5997 xfs_fileoff_t split_fsb)
5998{
5999 struct xfs_mount *mp = ip->i_mount;
6000 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006001 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006002 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006003 int error;
6004
Christoph Hellwig253f4912016-04-06 09:19:55 +10006005 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6006 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6007 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006008 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006009
6010 xfs_ilock(ip, XFS_ILOCK_EXCL);
6011 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6012
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006013 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006014
6015 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006016 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006017 if (error)
6018 goto out;
6019
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07006020 error = xfs_defer_finish(&tp, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006021 if (error)
6022 goto out;
6023
Christoph Hellwig70393312015-06-04 13:48:08 +10006024 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006025
6026out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006027 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006028 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006029 return error;
6030}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006031
6032/* Deferred mapping is only for real extents in the data fork. */
6033static bool
6034xfs_bmap_is_update_needed(
6035 struct xfs_bmbt_irec *bmap)
6036{
6037 return bmap->br_startblock != HOLESTARTBLOCK &&
6038 bmap->br_startblock != DELAYSTARTBLOCK;
6039}
6040
6041/* Record a bmap intent. */
6042static int
6043__xfs_bmap_add(
6044 struct xfs_mount *mp,
6045 struct xfs_defer_ops *dfops,
6046 enum xfs_bmap_intent_type type,
6047 struct xfs_inode *ip,
6048 int whichfork,
6049 struct xfs_bmbt_irec *bmap)
6050{
6051 int error;
6052 struct xfs_bmap_intent *bi;
6053
6054 trace_xfs_bmap_defer(mp,
6055 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6056 type,
6057 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6058 ip->i_ino, whichfork,
6059 bmap->br_startoff,
6060 bmap->br_blockcount,
6061 bmap->br_state);
6062
6063 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6064 INIT_LIST_HEAD(&bi->bi_list);
6065 bi->bi_type = type;
6066 bi->bi_owner = ip;
6067 bi->bi_whichfork = whichfork;
6068 bi->bi_bmap = *bmap;
6069
Christoph Hellwig882d8782017-08-28 10:21:03 -07006070 error = xfs_defer_ijoin(dfops, bi->bi_owner);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006071 if (error) {
6072 kmem_free(bi);
6073 return error;
6074 }
6075
6076 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6077 return 0;
6078}
6079
6080/* Map an extent into a file. */
6081int
6082xfs_bmap_map_extent(
6083 struct xfs_mount *mp,
6084 struct xfs_defer_ops *dfops,
6085 struct xfs_inode *ip,
6086 struct xfs_bmbt_irec *PREV)
6087{
6088 if (!xfs_bmap_is_update_needed(PREV))
6089 return 0;
6090
6091 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6092 XFS_DATA_FORK, PREV);
6093}
6094
6095/* Unmap an extent out of a file. */
6096int
6097xfs_bmap_unmap_extent(
6098 struct xfs_mount *mp,
6099 struct xfs_defer_ops *dfops,
6100 struct xfs_inode *ip,
6101 struct xfs_bmbt_irec *PREV)
6102{
6103 if (!xfs_bmap_is_update_needed(PREV))
6104 return 0;
6105
6106 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6107 XFS_DATA_FORK, PREV);
6108}
6109
6110/*
6111 * Process one of the deferred bmap operations. We pass back the
6112 * btree cursor to maintain our lock on the bmapbt between calls.
6113 */
6114int
6115xfs_bmap_finish_one(
6116 struct xfs_trans *tp,
6117 struct xfs_defer_ops *dfops,
6118 struct xfs_inode *ip,
6119 enum xfs_bmap_intent_type type,
6120 int whichfork,
6121 xfs_fileoff_t startoff,
6122 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006123 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006124 xfs_exntst_t state)
6125{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006126 xfs_fsblock_t firstfsb;
6127 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006128
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006129 /*
6130 * firstfsb is tied to the transaction lifetime and is used to
6131 * ensure correct AG locking order and schedule work item
6132 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6133 * to only making one bmap call per transaction, so it should
6134 * be safe to have it as a local variable here.
6135 */
6136 firstfsb = NULLFSBLOCK;
6137
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006138 trace_xfs_bmap_deferred(tp->t_mountp,
6139 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6140 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006141 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006142
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006143 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006144 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006145
6146 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006147 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006148 return -EIO;
6149
6150 switch (type) {
6151 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006152 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006153 startblock, dfops);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006154 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006155 break;
6156 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006157 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6158 XFS_BMAPI_REMAP, 1, &firstfsb, dfops);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006159 break;
6160 default:
6161 ASSERT(0);
6162 error = -EFSCORRUPTED;
6163 }
6164
6165 return error;
6166}
Darrick J. Wong30b09842018-03-23 10:06:52 -07006167
6168/* Check that an inode's extent does not have invalid flags or bad ranges. */
6169xfs_failaddr_t
6170xfs_bmap_validate_extent(
6171 struct xfs_inode *ip,
6172 int whichfork,
6173 struct xfs_bmbt_irec *irec)
6174{
6175 struct xfs_mount *mp = ip->i_mount;
6176 xfs_fsblock_t endfsb;
6177 bool isrt;
6178
6179 isrt = XFS_IS_REALTIME_INODE(ip);
6180 endfsb = irec->br_startblock + irec->br_blockcount - 1;
6181 if (isrt) {
6182 if (!xfs_verify_rtbno(mp, irec->br_startblock))
6183 return __this_address;
6184 if (!xfs_verify_rtbno(mp, endfsb))
6185 return __this_address;
6186 } else {
6187 if (!xfs_verify_fsbno(mp, irec->br_startblock))
6188 return __this_address;
6189 if (!xfs_verify_fsbno(mp, endfsb))
6190 return __this_address;
6191 if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) !=
6192 XFS_FSB_TO_AGNO(mp, endfsb))
6193 return __this_address;
6194 }
6195 if (irec->br_state != XFS_EXT_NORM) {
6196 if (whichfork != XFS_DATA_FORK)
6197 return __this_address;
6198 if (!xfs_sb_version_hasextflgbit(&mp->m_sb))
6199 return __this_address;
6200 }
6201 return NULL;
6202}