blob: 534ffb856c4ae6e11214df81012a1230782aedfc [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
Brian Fosterfcb762f2018-05-09 08:45:04 -0700547__xfs_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,
Brian Fosterfcb762f2018-05-09 08:45:04 -0700552 struct xfs_owner_info *oinfo,
553 bool skip_discard)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100554{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000555 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100556#ifdef DEBUG
557 xfs_agnumber_t agno;
558 xfs_agblock_t agbno;
559
560 ASSERT(bno != NULLFSBLOCK);
561 ASSERT(len > 0);
562 ASSERT(len <= MAXEXTLEN);
563 ASSERT(!isnullstartblock(bno));
564 agno = XFS_FSB_TO_AGNO(mp, bno);
565 agbno = XFS_FSB_TO_AGBNO(mp, bno);
566 ASSERT(agno < mp->m_sb.sb_agcount);
567 ASSERT(agbno < mp->m_sb.sb_agblocks);
568 ASSERT(len < mp->m_sb.sb_agblocks);
569 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
570#endif
571 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000572
Dave Chinner9e5987a2013-02-25 12:31:26 +1100573 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000574 new->xefi_startblock = bno;
575 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000576 if (oinfo)
577 new->xefi_oinfo = *oinfo;
578 else
579 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
Brian Fosterfcb762f2018-05-09 08:45:04 -0700580 new->xefi_skip_discard = skip_discard;
Darrick J. Wongba9e7802016-08-03 11:26:33 +1000581 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
582 XFS_FSB_TO_AGBNO(mp, bno), len);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000583 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100584}
585
586/*
587 * Inode fork format manipulation functions
588 */
589
590/*
591 * Transform a btree format file with only one leaf node, where the
592 * extents list will fit in the inode, into an extents format file.
593 * Since the file extents are already in-core, all we have to do is
594 * give up the space for the btree root and pitch the leaf block.
595 */
596STATIC int /* error */
597xfs_bmap_btree_to_extents(
598 xfs_trans_t *tp, /* transaction pointer */
599 xfs_inode_t *ip, /* incore inode pointer */
600 xfs_btree_cur_t *cur, /* btree cursor */
601 int *logflagsp, /* inode logging flags */
602 int whichfork) /* data or attr fork */
603{
604 /* REFERENCED */
605 struct xfs_btree_block *cblock;/* child btree block */
606 xfs_fsblock_t cbno; /* child block number */
607 xfs_buf_t *cbp; /* child block's buffer */
608 int error; /* error return value */
609 xfs_ifork_t *ifp; /* inode fork data */
610 xfs_mount_t *mp; /* mount point structure */
611 __be64 *pp; /* ptr to block address */
612 struct xfs_btree_block *rblock;/* root btree block */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000613 struct xfs_owner_info oinfo;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100614
615 mp = ip->i_mount;
616 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700617 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100618 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
619 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
620 rblock = ifp->if_broot;
621 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
622 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
623 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
624 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
625 cbno = be64_to_cpu(*pp);
626 *logflagsp = 0;
627#ifdef DEBUG
Darrick J. Wongf1357612017-10-17 21:37:33 -0700628 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
629 xfs_btree_check_lptr(cur, cbno, 1));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100630#endif
631 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
632 &xfs_bmbt_buf_ops);
633 if (error)
634 return error;
635 cblock = XFS_BUF_TO_BLOCK(cbp);
636 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
637 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000638 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
639 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100640 ip->i_d.di_nblocks--;
641 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
642 xfs_trans_binval(tp, cbp);
643 if (cur->bc_bufs[0] == cbp)
644 cur->bc_bufs[0] = NULL;
645 xfs_iroot_realloc(ip, -1, whichfork);
646 ASSERT(ifp->if_broot == NULL);
647 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
648 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
649 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
650 return 0;
651}
652
653/*
654 * Convert an extents-format file into a btree-format file.
655 * The new file will have a root block (in the inode) and a single child block.
656 */
657STATIC int /* error */
658xfs_bmap_extents_to_btree(
659 xfs_trans_t *tp, /* transaction pointer */
660 xfs_inode_t *ip, /* incore inode pointer */
661 xfs_fsblock_t *firstblock, /* first-block-allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000662 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100663 xfs_btree_cur_t **curp, /* cursor returned to caller */
664 int wasdel, /* converting a delayed alloc */
665 int *logflagsp, /* inode logging flags */
666 int whichfork) /* data or attr fork */
667{
668 struct xfs_btree_block *ablock; /* allocated (child) bt block */
669 xfs_buf_t *abp; /* buffer for ablock */
670 xfs_alloc_arg_t args; /* allocation arguments */
671 xfs_bmbt_rec_t *arp; /* child record pointer */
672 struct xfs_btree_block *block; /* btree root block */
673 xfs_btree_cur_t *cur; /* bmap btree cursor */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100674 int error; /* error return value */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100675 xfs_ifork_t *ifp; /* inode fork pointer */
676 xfs_bmbt_key_t *kp; /* root block key pointer */
677 xfs_mount_t *mp; /* mount structure */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100678 xfs_bmbt_ptr_t *pp; /* root block address pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700679 struct xfs_iext_cursor icur;
Christoph Hellwig906abed2017-11-03 10:34:43 -0700680 struct xfs_bmbt_irec rec;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700681 xfs_extnum_t cnt = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100682
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500683 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700684 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100685 ifp = XFS_IFORK_PTR(ip, whichfork);
686 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
687
688 /*
689 * Make space in the inode incore.
690 */
691 xfs_iroot_realloc(ip, 1, whichfork);
692 ifp->if_flags |= XFS_IFBROOT;
693
694 /*
695 * Fill in the root.
696 */
697 block = ifp->if_broot;
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800698 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
699 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
Eric Sandeenf88ae462017-01-27 23:16:37 -0800700 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100701 /*
702 * Need a cursor. Can't allocate until bb_level is filled in.
703 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100704 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
705 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000706 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100707 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
708 /*
709 * Convert to a btree with two levels, one record in root.
710 */
711 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
712 memset(&args, 0, sizeof(args));
713 args.tp = tp;
714 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000715 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100716 args.firstblock = *firstblock;
717 if (*firstblock == NULLFSBLOCK) {
718 args.type = XFS_ALLOCTYPE_START_BNO;
719 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000720 } else if (dfops->dop_low) {
Dave Chinner9e5987a2013-02-25 12:31:26 +1100721 args.type = XFS_ALLOCTYPE_START_BNO;
722 args.fsbno = *firstblock;
723 } else {
724 args.type = XFS_ALLOCTYPE_NEAR_BNO;
725 args.fsbno = *firstblock;
726 }
727 args.minlen = args.maxlen = args.prod = 1;
728 args.wasdel = wasdel;
729 *logflagsp = 0;
730 if ((error = xfs_alloc_vextent(&args))) {
731 xfs_iroot_realloc(ip, -1, whichfork);
Eric Sandeen2c4306f2018-04-16 23:07:27 -0700732 ASSERT(ifp->if_broot == NULL);
733 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100734 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
735 return error;
736 }
Darrick J. Wong90e20562016-10-03 09:11:45 -0700737
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800738 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
739 xfs_iroot_realloc(ip, -1, whichfork);
Eric Sandeen2c4306f2018-04-16 23:07:27 -0700740 ASSERT(ifp->if_broot == NULL);
741 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800742 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
743 return -ENOSPC;
744 }
Dave Chinner9e5987a2013-02-25 12:31:26 +1100745 /*
746 * Allocation can't fail, the space was reserved.
747 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100748 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -0800749 args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100750 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
751 cur->bc_private.b.allocated++;
752 ip->i_d.di_nblocks++;
753 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
754 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
755 /*
756 * Fill in the child block.
757 */
758 abp->b_ops = &xfs_bmbt_buf_ops;
759 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800760 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
761 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500762 XFS_BTREE_LONG_PTRS);
763
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700764 for_each_xfs_iext(ifp, &icur, &rec) {
Christoph Hellwig906abed2017-11-03 10:34:43 -0700765 if (isnullstartblock(rec.br_startblock))
766 continue;
767 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
768 xfs_bmbt_disk_set_all(arp, &rec);
769 cnt++;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100770 }
771 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
772 xfs_btree_set_numrecs(ablock, cnt);
773
774 /*
775 * Fill in the root key and pointer.
776 */
777 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
778 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
779 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
780 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
781 be16_to_cpu(block->bb_level)));
782 *pp = cpu_to_be64(args.fsbno);
783
784 /*
785 * Do all this logging at the end so that
786 * the root is at the right level.
787 */
788 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
789 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
790 ASSERT(*curp == NULL);
791 *curp = cur;
792 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
793 return 0;
794}
795
796/*
797 * Convert a local file to an extents file.
798 * This code is out of bounds for data forks of regular files,
799 * since the file data needs to get logged so things will stay consistent.
800 * (The bmap-level manipulations are ok, though).
801 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000802void
803xfs_bmap_local_to_extents_empty(
804 struct xfs_inode *ip,
805 int whichfork)
806{
807 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
808
Darrick J. Wong60b49842016-10-03 09:11:34 -0700809 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000810 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
811 ASSERT(ifp->if_bytes == 0);
812 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
813
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000814 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000815 ifp->if_flags &= ~XFS_IFINLINE;
816 ifp->if_flags |= XFS_IFEXTENTS;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700817 ifp->if_u1.if_root = NULL;
818 ifp->if_height = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000819 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
820}
821
822
Dave Chinner9e5987a2013-02-25 12:31:26 +1100823STATIC int /* error */
824xfs_bmap_local_to_extents(
825 xfs_trans_t *tp, /* transaction pointer */
826 xfs_inode_t *ip, /* incore inode pointer */
827 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
828 xfs_extlen_t total, /* total blocks needed by transaction */
829 int *logflagsp, /* inode logging flags */
830 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500831 void (*init_fn)(struct xfs_trans *tp,
832 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100833 struct xfs_inode *ip,
834 struct xfs_ifork *ifp))
835{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000836 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100837 int flags; /* logging flags returned */
838 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000839 xfs_alloc_arg_t args; /* allocation arguments */
840 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700841 struct xfs_bmbt_irec rec;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700842 struct xfs_iext_cursor icur;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100843
844 /*
845 * We don't want to deal with the case of keeping inode data inline yet.
846 * So sending the data fork of a regular inode is invalid.
847 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100848 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100849 ifp = XFS_IFORK_PTR(ip, whichfork);
850 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000851
852 if (!ifp->if_bytes) {
853 xfs_bmap_local_to_extents_empty(ip, whichfork);
854 flags = XFS_ILOG_CORE;
855 goto done;
856 }
857
Dave Chinner9e5987a2013-02-25 12:31:26 +1100858 flags = 0;
859 error = 0;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700860 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000861 memset(&args, 0, sizeof(args));
862 args.tp = tp;
863 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000864 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000865 args.firstblock = *firstblock;
866 /*
867 * Allocate a block. We know we need only one, since the
868 * file currently fits in an inode.
869 */
870 if (*firstblock == NULLFSBLOCK) {
871 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
872 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100873 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000874 args.fsbno = *firstblock;
875 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100876 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000877 args.total = total;
878 args.minlen = args.maxlen = args.prod = 1;
879 error = xfs_alloc_vextent(&args);
880 if (error)
881 goto done;
882
883 /* Can't fail, the space was reserved. */
884 ASSERT(args.fsbno != NULLFSBLOCK);
885 ASSERT(args.len == 1);
886 *firstblock = args.fsbno;
887 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
888
Dave Chinnerfe22d552015-01-22 09:30:06 +1100889 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100890 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100891 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100892 * The callout is responsible for logging because the remote format
893 * might differ from the local format and thus we don't know how much to
894 * log here. Note that init_fn must also set the buffer log item type
895 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100896 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000897 init_fn(tp, bp, ip, ifp);
898
Brian Fosterb7cdc662015-10-12 15:40:24 +1100899 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000900 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
901 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100902 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000903
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700904 ifp->if_u1.if_root = NULL;
905 ifp->if_height = 0;
906
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700907 rec.br_startoff = 0;
908 rec.br_startblock = args.fsbno;
909 rec.br_blockcount = 1;
910 rec.br_state = XFS_EXT_NORM;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700911 xfs_iext_first(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -0700912 xfs_iext_insert(ip, &icur, &rec, 0);
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700913
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000914 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
915 ip->i_d.di_nblocks = 1;
916 xfs_trans_mod_dquot_byino(tp, ip,
917 XFS_TRANS_DQ_BCOUNT, 1L);
918 flags |= xfs_ilog_fext(whichfork);
919
Dave Chinner9e5987a2013-02-25 12:31:26 +1100920done:
921 *logflagsp = flags;
922 return error;
923}
924
925/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 * Called from xfs_bmap_add_attrfork to handle btree format files.
927 */
928STATIC int /* error */
929xfs_bmap_add_attrfork_btree(
930 xfs_trans_t *tp, /* transaction pointer */
931 xfs_inode_t *ip, /* incore inode pointer */
932 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000933 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 int *flags) /* inode logging flags */
935{
936 xfs_btree_cur_t *cur; /* btree cursor */
937 int error; /* error return value */
938 xfs_mount_t *mp; /* file system mount struct */
939 int stat; /* newroot status */
940
941 mp = ip->i_mount;
942 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
943 *flags |= XFS_ILOG_DBROOT;
944 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100945 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000946 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 cur->bc_private.b.firstblock = *firstblock;
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700948 error = xfs_bmbt_lookup_first(cur, &stat);
949 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000951 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100952 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100953 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 goto error0;
955 if (stat == 0) {
956 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000957 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959 *firstblock = cur->bc_private.b.firstblock;
960 cur->bc_private.b.allocated = 0;
961 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
962 }
963 return 0;
964error0:
965 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
966 return error;
967}
968
969/*
970 * Called from xfs_bmap_add_attrfork to handle extents format files.
971 */
972STATIC int /* error */
973xfs_bmap_add_attrfork_extents(
974 xfs_trans_t *tp, /* transaction pointer */
975 xfs_inode_t *ip, /* incore inode pointer */
976 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000977 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 int *flags) /* inode logging flags */
979{
980 xfs_btree_cur_t *cur; /* bmap btree cursor */
981 int error; /* error return value */
982
983 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
984 return 0;
985 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000986 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 flags, XFS_DATA_FORK);
988 if (cur) {
989 cur->bc_private.b.allocated = 0;
990 xfs_btree_del_cursor(cur,
991 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
992 }
993 return error;
994}
995
996/*
Dave Chinner1e823792013-02-11 15:58:13 +1100997 * Called from xfs_bmap_add_attrfork to handle local format files. Each
998 * different data fork content type needs a different callout to do the
999 * conversion. Some are basic and only require special block initialisation
1000 * callouts for the data formating, others (directories) are so specialised they
1001 * handle everything themselves.
1002 *
1003 * XXX (dgc): investigate whether directory conversion can use the generic
1004 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001005 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 */
1007STATIC int /* error */
1008xfs_bmap_add_attrfork_local(
1009 xfs_trans_t *tp, /* transaction pointer */
1010 xfs_inode_t *ip, /* incore inode pointer */
1011 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001012 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 int *flags) /* inode logging flags */
1014{
1015 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1018 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001019
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001020 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001022 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 dargs.dp = ip;
1024 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001025 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001026 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 dargs.whichfork = XFS_DATA_FORK;
1028 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001029 return xfs_dir2_sf_to_block(&dargs);
1030 }
1031
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001032 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001033 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1034 flags, XFS_DATA_FORK,
1035 xfs_symlink_local_to_remote);
1036
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001037 /* should only be called for types that support local format data */
1038 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001039 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
1042/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001043 * Convert inode from non-attributed to attributed.
1044 * Must not be in a transaction, ip must not be locked.
1045 */
1046int /* error code */
1047xfs_bmap_add_attrfork(
1048 xfs_inode_t *ip, /* incore inode pointer */
1049 int size, /* space new attribute needs */
1050 int rsvd) /* xact may use reserved blks */
1051{
1052 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001053 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001054 xfs_mount_t *mp; /* mount structure */
1055 xfs_trans_t *tp; /* transaction pointer */
1056 int blks; /* space reservation */
1057 int version = 1; /* superblock attr version */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001058 int logflags; /* logging flags */
1059 int error; /* error return value */
1060
1061 ASSERT(XFS_IFORK_Q(ip) == 0);
1062
1063 mp = ip->i_mount;
1064 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001065
Dave Chinner9e5987a2013-02-25 12:31:26 +11001066 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001067
1068 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1069 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1070 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001071 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001072
Dave Chinner9e5987a2013-02-25 12:31:26 +11001073 xfs_ilock(ip, XFS_ILOCK_EXCL);
1074 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1075 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1076 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001077 if (error)
1078 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001079 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001080 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001081 if (ip->i_d.di_anextents != 0) {
1082 error = -EFSCORRUPTED;
1083 goto trans_cancel;
1084 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001085 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1086 /*
1087 * For inodes coming from pre-6.2 filesystems.
1088 */
1089 ASSERT(ip->i_d.di_aformat == 0);
1090 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1091 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001092
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001093 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001094 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1095
1096 switch (ip->i_d.di_format) {
1097 case XFS_DINODE_FMT_DEV:
1098 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1099 break;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001100 case XFS_DINODE_FMT_LOCAL:
1101 case XFS_DINODE_FMT_EXTENTS:
1102 case XFS_DINODE_FMT_BTREE:
1103 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1104 if (!ip->i_d.di_forkoff)
1105 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1106 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1107 version = 2;
1108 break;
1109 default:
1110 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001111 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001112 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001113 }
1114
1115 ASSERT(ip->i_afp == NULL);
1116 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1117 ip->i_afp->if_flags = XFS_IFEXTENTS;
1118 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001119 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001120 switch (ip->i_d.di_format) {
1121 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001122 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001123 &logflags);
1124 break;
1125 case XFS_DINODE_FMT_EXTENTS:
1126 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001127 &dfops, &logflags);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001128 break;
1129 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001130 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001131 &logflags);
1132 break;
1133 default:
1134 error = 0;
1135 break;
1136 }
1137 if (logflags)
1138 xfs_trans_log_inode(tp, ip, logflags);
1139 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001140 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001141 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1142 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001143 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001144
1145 spin_lock(&mp->m_sb_lock);
1146 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1147 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001148 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001149 }
1150 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1151 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001152 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001153 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001154 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001155 if (log_sb)
1156 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001157 }
1158
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001159 error = xfs_defer_finish(&tp, &dfops);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001160 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001161 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001162 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001163 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001164 return error;
1165
1166bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001167 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001168trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001169 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001170 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001171 return error;
1172}
1173
1174/*
1175 * Internal and external extent tree search functions.
1176 */
1177
1178/*
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001179 * Read in extents from a btree-format inode.
Dave Chinner9e5987a2013-02-25 12:31:26 +11001180 */
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001181int
1182xfs_iread_extents(
1183 struct xfs_trans *tp,
1184 struct xfs_inode *ip,
1185 int whichfork)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001186{
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001187 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwige8e0e172017-10-19 11:06:29 -07001188 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001189 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1190 xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
1191 struct xfs_btree_block *block = ifp->if_broot;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001192 struct xfs_iext_cursor icur;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -07001193 struct xfs_bmbt_irec new;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001194 xfs_fsblock_t bno;
1195 struct xfs_buf *bp;
1196 xfs_extnum_t i, j;
1197 int level;
1198 __be64 *pp;
1199 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001200
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001201 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1202
1203 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1204 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1205 return -EFSCORRUPTED;
1206 }
1207
Dave Chinner9e5987a2013-02-25 12:31:26 +11001208 /*
1209 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1210 */
1211 level = be16_to_cpu(block->bb_level);
1212 ASSERT(level > 0);
1213 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1214 bno = be64_to_cpu(*pp);
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -08001215
Dave Chinner9e5987a2013-02-25 12:31:26 +11001216 /*
1217 * Go down the tree until leaf level is reached, following the first
1218 * pointer (leftmost) at each level.
1219 */
1220 while (level-- > 0) {
1221 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1222 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1223 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001224 goto out;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001225 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001226 if (level == 0)
1227 break;
1228 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1229 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001230 XFS_WANT_CORRUPTED_GOTO(mp,
Darrick J. Wong59f6fec2018-01-08 10:51:00 -08001231 xfs_verify_fsbno(mp, bno), out_brelse);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001232 xfs_trans_brelse(tp, bp);
1233 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001234
Dave Chinner9e5987a2013-02-25 12:31:26 +11001235 /*
1236 * Here with bp and block set to the leftmost leaf node in the tree.
1237 */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001238 i = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001239 xfs_iext_first(ifp, &icur);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001240
Dave Chinner9e5987a2013-02-25 12:31:26 +11001241 /*
1242 * Loop over all leaf nodes. Copy information to the extent records.
1243 */
1244 for (;;) {
1245 xfs_bmbt_rec_t *frp;
1246 xfs_fsblock_t nextbno;
1247 xfs_extnum_t num_recs;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001248
1249 num_recs = xfs_btree_get_numrecs(block);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001250 if (unlikely(i + num_recs > nextents)) {
1251 ASSERT(i + num_recs <= nextents);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001252 xfs_warn(ip->i_mount,
1253 "corrupt dinode %Lu, (btree extents).",
1254 (unsigned long long) ip->i_ino);
Darrick J. Wong90a58f92018-03-23 10:06:52 -07001255 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1256 __func__, block, sizeof(*block),
1257 __this_address);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001258 error = -EFSCORRUPTED;
1259 goto out_brelse;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001260 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001261 /*
1262 * Read-ahead the next leaf block, if any.
1263 */
1264 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1265 if (nextbno != NULLFSBLOCK)
1266 xfs_btree_reada_bufl(mp, nextbno, 1,
1267 &xfs_bmbt_buf_ops);
1268 /*
1269 * Copy records into the extent records.
1270 */
1271 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Christoph Hellwig6bdcf262017-11-03 10:34:46 -07001272 for (j = 0; j < num_recs; j++, frp++, i++) {
Darrick J. Wong30b09842018-03-23 10:06:52 -07001273 xfs_failaddr_t fa;
1274
Christoph Hellwigdac9c9b2017-11-03 10:34:47 -07001275 xfs_bmbt_disk_get_all(frp, &new);
Darrick J. Wong30b09842018-03-23 10:06:52 -07001276 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1277 if (fa) {
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001278 error = -EFSCORRUPTED;
Darrick J. Wong30b09842018-03-23 10:06:52 -07001279 xfs_inode_verifier_error(ip, error,
1280 "xfs_iread_extents(2)",
1281 frp, sizeof(*frp), fa);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001282 goto out_brelse;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001283 }
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001284 xfs_iext_insert(ip, &icur, &new, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001285 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
1286 xfs_iext_next(ifp, &icur);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001287 }
1288 xfs_trans_brelse(tp, bp);
1289 bno = nextbno;
1290 /*
1291 * If we've reached the end, stop.
1292 */
1293 if (bno == NULLFSBLOCK)
1294 break;
1295 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1296 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1297 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001298 goto out;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001299 block = XFS_BUF_TO_BLOCK(bp);
1300 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001301
1302 if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) {
1303 error = -EFSCORRUPTED;
1304 goto out;
1305 }
Eric Sandeen5d829302016-11-08 12:59:42 +11001306 ASSERT(i == xfs_iext_count(ifp));
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001307
1308 ifp->if_flags |= XFS_IFEXTENTS;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001309 return 0;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001310
1311out_brelse:
Dave Chinner9e5987a2013-02-25 12:31:26 +11001312 xfs_trans_brelse(tp, bp);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001313out:
1314 xfs_iext_destroy(ifp);
1315 return error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001316}
1317
Dave Chinner9e5987a2013-02-25 12:31:26 +11001318/*
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001319 * Returns the relative block number of the first unused block(s) in the given
1320 * fork with at least "len" logically contiguous blocks free. This is the
1321 * lowest-address hole if the fork has holes, else the first block past the end
1322 * of fork. Return 0 if the fork is currently local (in-inode).
Dave Chinner9e5987a2013-02-25 12:31:26 +11001323 */
1324int /* error */
1325xfs_bmap_first_unused(
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001326 struct xfs_trans *tp, /* transaction pointer */
1327 struct xfs_inode *ip, /* incore inode */
1328 xfs_extlen_t len, /* size of hole to find */
1329 xfs_fileoff_t *first_unused, /* unused block */
1330 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001331{
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001332 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1333 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001334 struct xfs_iext_cursor icur;
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001335 xfs_fileoff_t lastaddr = 0;
1336 xfs_fileoff_t lowest, max;
1337 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001338
1339 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1340 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1341 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001342
Dave Chinner9e5987a2013-02-25 12:31:26 +11001343 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1344 *first_unused = 0;
1345 return 0;
1346 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001347
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001348 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1349 error = xfs_iread_extents(tp, ip, whichfork);
1350 if (error)
1351 return error;
1352 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001353
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001354 lowest = max = *first_unused;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001355 for_each_xfs_iext(ifp, &icur, &got) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11001356 /*
1357 * See if the hole before this extent will work.
1358 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001359 if (got.br_startoff >= lowest + len &&
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001360 got.br_startoff - max >= len)
1361 break;
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001362 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001363 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1364 }
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001365
Dave Chinner9e5987a2013-02-25 12:31:26 +11001366 *first_unused = max;
1367 return 0;
1368}
1369
1370/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001371 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001372 * last_block (input value) in the file.
1373 * This is not based on i_size, it is based on the extent records.
1374 * Returns 0 for local files, as they do not have extent records.
1375 */
1376int /* error */
1377xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001378 struct xfs_trans *tp, /* transaction pointer */
1379 struct xfs_inode *ip, /* incore inode */
1380 xfs_fileoff_t *last_block, /* last block */
1381 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001382{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001383 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1384 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001385 struct xfs_iext_cursor icur;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001386 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001387
Christoph Hellwig86685f72016-11-24 11:39:38 +11001388 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1389 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a2013-02-25 12:31:26 +11001390 *last_block = 0;
1391 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001392 case XFS_DINODE_FMT_BTREE:
1393 case XFS_DINODE_FMT_EXTENTS:
1394 break;
1395 default:
1396 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001397 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001398
1399 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1400 error = xfs_iread_extents(tp, ip, whichfork);
1401 if (error)
1402 return error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001403 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001404
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001405 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
Christoph Hellwigdc560152017-10-23 16:32:39 -07001406 *last_block = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001407 return 0;
1408}
1409
Dave Chinner68988112013-08-12 20:49:42 +10001410int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001411xfs_bmap_last_extent(
1412 struct xfs_trans *tp,
1413 struct xfs_inode *ip,
1414 int whichfork,
1415 struct xfs_bmbt_irec *rec,
1416 int *is_empty)
1417{
1418 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001419 struct xfs_iext_cursor icur;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001420 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001421
1422 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1423 error = xfs_iread_extents(tp, ip, whichfork);
1424 if (error)
1425 return error;
1426 }
1427
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001428 xfs_iext_last(ifp, &icur);
1429 if (!xfs_iext_get_extent(ifp, &icur, rec))
Dave Chinner9e5987a2013-02-25 12:31:26 +11001430 *is_empty = 1;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001431 else
1432 *is_empty = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001433 return 0;
1434}
1435
1436/*
1437 * Check the last inode extent to determine whether this allocation will result
1438 * in blocks being allocated at the end of the file. When we allocate new data
1439 * blocks at the end of the file which do not start at the previous data block,
1440 * we will try to align the new blocks at stripe unit boundaries.
1441 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001442 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001443 * at, or past the EOF.
1444 */
1445STATIC int
1446xfs_bmap_isaeof(
1447 struct xfs_bmalloca *bma,
1448 int whichfork)
1449{
1450 struct xfs_bmbt_irec rec;
1451 int is_empty;
1452 int error;
1453
Thomas Meyer749f24f2017-10-09 11:38:54 -07001454 bma->aeof = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001455 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1456 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001457 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001458 return error;
1459
Dave Chinner6e708bc2013-11-22 10:41:16 +11001460 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001461 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001462 return 0;
1463 }
1464
Dave Chinner9e5987a2013-02-25 12:31:26 +11001465 /*
1466 * Check if we are allocation or past the last extent, or at least into
1467 * the last delayed allocated extent.
1468 */
1469 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1470 (bma->offset >= rec.br_startoff &&
1471 isnullstartblock(rec.br_startblock));
1472 return 0;
1473}
1474
1475/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001476 * Returns the file-relative block number of the first block past eof in
1477 * the file. This is not based on i_size, it is based on the extent records.
1478 * Returns 0 for local files, as they do not have extent records.
1479 */
1480int
1481xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001482 struct xfs_inode *ip,
1483 xfs_fileoff_t *last_block,
1484 int whichfork)
1485{
1486 struct xfs_bmbt_irec rec;
1487 int is_empty;
1488 int error;
1489
1490 *last_block = 0;
1491
1492 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1493 return 0;
1494
1495 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1496 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001497 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001498
1499 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1500 if (error || is_empty)
1501 return error;
1502
1503 *last_block = rec.br_startoff + rec.br_blockcount;
1504 return 0;
1505}
1506
1507/*
1508 * Returns whether the selected fork of the inode has exactly one
1509 * block or not. For the data fork we check this matches di_size,
1510 * implying the file's range is 0..bsize-1.
1511 */
1512int /* 1=>1 block, 0=>otherwise */
1513xfs_bmap_one_block(
1514 xfs_inode_t *ip, /* incore inode */
1515 int whichfork) /* data or attr fork */
1516{
Dave Chinner9e5987a2013-02-25 12:31:26 +11001517 xfs_ifork_t *ifp; /* inode fork pointer */
1518 int rval; /* return value */
1519 xfs_bmbt_irec_t s; /* internal version of extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001520 struct xfs_iext_cursor icur;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001521
1522#ifndef DEBUG
1523 if (whichfork == XFS_DATA_FORK)
1524 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1525#endif /* !DEBUG */
1526 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1527 return 0;
1528 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1529 return 0;
1530 ifp = XFS_IFORK_PTR(ip, whichfork);
1531 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001532 xfs_iext_first(ifp, &icur);
1533 xfs_iext_get_extent(ifp, &icur, &s);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001534 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1535 if (rval && whichfork == XFS_DATA_FORK)
1536 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1537 return rval;
1538}
1539
1540/*
1541 * Extent tree manipulation functions used during allocation.
1542 */
1543
1544/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001545 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 */
1547STATIC int /* error */
1548xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001549 struct xfs_bmalloca *bma,
1550 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001552 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001555 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 xfs_fileoff_t new_endoff; /* end offset of new entry */
1557 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1558 /* left is 0, right is 1, prev is 2 */
1559 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07001560 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001561 xfs_filblks_t da_new; /* new count del alloc blocks used */
1562 xfs_filblks_t da_old; /* old count del alloc blocks used */
1563 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001565 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001566 xfs_extnum_t *nextents;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001567 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001569 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001570 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001571 ASSERT(whichfork != XFS_ATTR_FORK);
1572 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1573 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001574
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001575 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001576 ASSERT(!bma->cur ||
1577 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001578
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001579 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581#define LEFT r[0]
1582#define RIGHT r[1]
1583#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 /*
1586 * Set up a bunch of variables to make the tests simpler.
1587 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001588 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001590 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 ASSERT(PREV.br_startoff <= new->br_startoff);
1592 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001593
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001594 da_old = startblockval(PREV.br_startblock);
1595 da_new = 0;
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 /*
1598 * Set flags determining what part of the previous delayed allocation
1599 * extent is being replaced by a real allocation.
1600 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001601 if (PREV.br_startoff == new->br_startoff)
1602 state |= BMAP_LEFT_FILLING;
1603 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1604 state |= BMAP_RIGHT_FILLING;
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 /*
1607 * Check and set flags if this segment has a left neighbor.
1608 * Don't set contiguous if the combined extent would be too large.
1609 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001610 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001611 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001612 if (isnullstartblock(LEFT.br_startblock))
1613 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001615
1616 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1617 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1618 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1619 LEFT.br_state == new->br_state &&
1620 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1621 state |= BMAP_LEFT_CONTIG;
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 /*
1624 * Check and set flags if this segment has a right neighbor.
1625 * Don't set contiguous if the combined extent would be too large.
1626 * Also check for all-three-contiguous being too large.
1627 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001628 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001629 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001630 if (isnullstartblock(RIGHT.br_startblock))
1631 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001633
1634 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1635 new_endoff == RIGHT.br_startoff &&
1636 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1637 new->br_state == RIGHT.br_state &&
1638 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1639 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1640 BMAP_RIGHT_FILLING)) !=
1641 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1642 BMAP_RIGHT_FILLING) ||
1643 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1644 <= MAXEXTLEN))
1645 state |= BMAP_RIGHT_CONTIG;
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 error = 0;
1648 /*
1649 * Switch out based on the FILLING and CONTIG state bits.
1650 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001651 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1652 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1653 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1654 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 /*
1656 * Filling in all of a previously delayed allocation extent.
1657 * The left and right neighbors are both contiguous with new.
1658 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001659 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001660
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001661 xfs_iext_remove(bma->ip, &bma->icur, state);
1662 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001663 xfs_iext_prev(ifp, &bma->icur);
1664 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001665 (*nextents)--;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001666
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001667 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1669 else {
1670 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001671 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001672 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001674 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001675 error = xfs_btree_delete(bma->cur, &i);
1676 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001678 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001679 error = xfs_btree_decrement(bma->cur, 0, &i);
1680 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001682 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001683 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001684 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 goto done;
1686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 break;
1688
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001689 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 /*
1691 * Filling in all of a previously delayed allocation extent.
1692 * The left neighbor is contiguous, the right is not.
1693 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001694 old = LEFT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001695 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001696
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001697 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001698 xfs_iext_prev(ifp, &bma->icur);
1699 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001700
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001701 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 rval = XFS_ILOG_DEXT;
1703 else {
1704 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001705 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001706 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001708 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001709 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001710 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 goto done;
1712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 break;
1714
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001715 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 /*
1717 * Filling in all of a previously delayed allocation extent.
1718 * The right neighbor is contiguous, the left is not.
1719 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001720 PREV.br_startblock = new->br_startblock;
1721 PREV.br_blockcount += RIGHT.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001722
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001723 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001724 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001725 xfs_iext_prev(ifp, &bma->icur);
1726 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001727
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001728 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 rval = XFS_ILOG_DEXT;
1730 else {
1731 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001732 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001733 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001735 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001736 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001737 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 goto done;
1739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 break;
1741
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001742 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 /*
1744 * Filling in all of a previously delayed allocation extent.
1745 * Neither the left nor right neighbors are contiguous with
1746 * the new one.
1747 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001748 PREV.br_startblock = new->br_startblock;
1749 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001750 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001751
Darrick J. Wong60b49842016-10-03 09:11:34 -07001752 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001753 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1755 else {
1756 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001757 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001758 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001760 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001761 error = xfs_btree_insert(bma->cur, &i);
1762 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001764 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 break;
1767
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001768 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 /*
1770 * Filling in the first part of a previous delayed allocation.
1771 * The left neighbor is contiguous.
1772 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001773 old = LEFT;
1774 temp = PREV.br_blockcount - new->br_blockcount;
1775 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1776 startblockval(PREV.br_startblock));
1777
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001778 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001779
Christoph Hellwigbf999712017-11-03 10:34:38 -07001780 PREV.br_blockcount = temp;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001781 PREV.br_startoff += new->br_blockcount;
1782 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwig0d045542017-11-03 10:34:39 -07001783
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001784 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1785 xfs_iext_prev(ifp, &bma->icur);
1786 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001787
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001788 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 rval = XFS_ILOG_DEXT;
1790 else {
1791 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001792 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001793 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001795 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001796 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001797 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 goto done;
1799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 break;
1801
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001802 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 /*
1804 * Filling in the first part of a previous delayed allocation.
1805 * The left neighbor is not contiguous.
1806 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001807 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001808 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001809 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1811 else {
1812 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001813 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001814 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001816 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001817 error = xfs_btree_insert(bma->cur, &i);
1818 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001820 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001822
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001823 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001824 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001825 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001826 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 rval |= tmp_rval;
1828 if (error)
1829 goto done;
1830 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001831
1832 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001833 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001834 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001835 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001836
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001837 PREV.br_startoff = new_endoff;
1838 PREV.br_blockcount = temp;
1839 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001840 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001841 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001842 xfs_iext_prev(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 break;
1844
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001845 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 /*
1847 * Filling in the last part of a previous delayed allocation.
1848 * The right neighbor is contiguous with the new allocation.
1849 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001850 old = RIGHT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001851 RIGHT.br_startoff = new->br_startoff;
1852 RIGHT.br_startblock = new->br_startblock;
1853 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001854
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001855 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 rval = XFS_ILOG_DEXT;
1857 else {
1858 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001859 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001860 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001862 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001863 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001864 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 goto done;
1866 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001867
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001868 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001869 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001870 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001871
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001872 PREV.br_blockcount = temp;
1873 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001874
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001875 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1876 xfs_iext_next(ifp, &bma->icur);
1877 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 break;
1879
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001880 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 /*
1882 * Filling in the last part of a previous delayed allocation.
1883 * The right neighbor is not contiguous.
1884 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001885 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001886 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001887 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1889 else {
1890 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001891 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001892 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001894 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001895 error = xfs_btree_insert(bma->cur, &i);
1896 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001898 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001900
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001901 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001902 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001903 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001904 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 rval |= tmp_rval;
1906 if (error)
1907 goto done;
1908 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001909
1910 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001911 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001912 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001913 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001914
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001915 PREV.br_startblock = nullstartblock(da_new);
1916 PREV.br_blockcount = temp;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001917 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001918 xfs_iext_next(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 break;
1920
1921 case 0:
1922 /*
1923 * Filling in the middle part of a previous delayed allocation.
1924 * Contiguity is impossible here.
1925 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001926 *
1927 * We start with a delayed allocation:
1928 *
1929 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1930 * PREV @ idx
1931 *
1932 * and we are allocating:
1933 * +rrrrrrrrrrrrrrrrr+
1934 * new
1935 *
1936 * and we set it up for insertion as:
1937 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1938 * new
1939 * PREV @ idx LEFT RIGHT
1940 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001942 old = PREV;
1943
1944 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001945 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001946
1947 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001948 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001949 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001950 RIGHT.br_blockcount =
1951 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1952 RIGHT.br_startblock =
1953 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1954 RIGHT.br_blockcount));
1955
1956 /* truncate PREV */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001957 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1958 PREV.br_startblock =
1959 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1960 PREV.br_blockcount));
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001961 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001962
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001963 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001964 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1965 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001966 (*nextents)++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001967
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001968 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1970 else {
1971 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001972 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001973 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001975 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001976 error = xfs_btree_insert(bma->cur, &i);
1977 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001979 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001981
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001982 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001983 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001984 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001985 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 rval |= tmp_rval;
1987 if (error)
1988 goto done;
1989 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001990
1991 da_new = startblockval(PREV.br_startblock) +
1992 startblockval(RIGHT.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 break;
1994
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001995 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1996 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1997 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1998 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1999 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2000 case BMAP_LEFT_CONTIG:
2001 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 /*
2003 * These cases are all impossible.
2004 */
2005 ASSERT(0);
2006 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002007
Darrick J. Wong9c194642016-08-03 12:16:05 +10002008 /* add reverse mapping */
2009 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2010 if (error)
2011 goto done;
2012
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002013 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002014 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002015 int tmp_logflags; /* partial log flag return val */
2016
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002017 ASSERT(bma->cur == NULL);
2018 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002019 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002020 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002021 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002022 if (error)
2023 goto done;
2024 }
2025
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002026 if (bma->cur) {
2027 da_new += bma->cur->bc_private.b.allocated;
2028 bma->cur->bc_private.b.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002029 }
2030
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002031 /* adjust for changes in reserved delayed indirect blocks */
2032 if (da_new != da_old) {
2033 ASSERT(state == 0 || da_new < da_old);
2034 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2035 false);
2036 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002037
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002038 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002040 if (whichfork != XFS_COW_FORK)
2041 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return error;
2043#undef LEFT
2044#undef RIGHT
2045#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046}
2047
2048/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002049 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 */
2051STATIC int /* error */
2052xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002053 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002055 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002056 struct xfs_iext_cursor *icur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002058 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002059 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002060 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002061 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002066 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2069 /* left is 0, right is 1, prev is 2 */
2070 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002071 int state = xfs_bmap_fork_to_state(whichfork);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002072 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002073 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002075 *logflagsp = 0;
2076
2077 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002078 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002079
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002080 ASSERT(!isnullstartblock(new->br_startblock));
2081
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002082 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084#define LEFT r[0]
2085#define RIGHT r[1]
2086#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 /*
2089 * Set up a bunch of variables to make the tests simpler.
2090 */
2091 error = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002092 xfs_iext_get_extent(ifp, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002093 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 new_endoff = new->br_startoff + new->br_blockcount;
2095 ASSERT(PREV.br_startoff <= new->br_startoff);
2096 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 /*
2099 * Set flags determining what part of the previous oldext allocation
2100 * extent is being replaced by a newext allocation.
2101 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002102 if (PREV.br_startoff == new->br_startoff)
2103 state |= BMAP_LEFT_FILLING;
2104 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2105 state |= BMAP_RIGHT_FILLING;
2106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 /*
2108 * Check and set flags if this segment has a left neighbor.
2109 * Don't set contiguous if the combined extent would be too large.
2110 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002111 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002112 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002113 if (isnullstartblock(LEFT.br_startblock))
2114 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002116
2117 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2118 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2119 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002120 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002121 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2122 state |= BMAP_LEFT_CONTIG;
2123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 /*
2125 * Check and set flags if this segment has a right neighbor.
2126 * Don't set contiguous if the combined extent would be too large.
2127 * Also check for all-three-contiguous being too large.
2128 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002129 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002130 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002131 if (isnullstartblock(RIGHT.br_startblock))
2132 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002134
2135 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2136 new_endoff == RIGHT.br_startoff &&
2137 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002138 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002139 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2140 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2141 BMAP_RIGHT_FILLING)) !=
2142 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2143 BMAP_RIGHT_FILLING) ||
2144 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2145 <= MAXEXTLEN))
2146 state |= BMAP_RIGHT_CONTIG;
2147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 /*
2149 * Switch out based on the FILLING and CONTIG state bits.
2150 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002151 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2152 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2153 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2154 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 /*
2156 * Setting all of a previous oldext extent to newext.
2157 * The left and right neighbors are both contiguous with new.
2158 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002159 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002160
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002161 xfs_iext_remove(ip, icur, state);
2162 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002163 xfs_iext_prev(ifp, icur);
2164 xfs_iext_update_extent(ip, state, icur, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002165 XFS_IFORK_NEXT_SET(ip, whichfork,
2166 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if (cur == NULL)
2168 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2169 else {
2170 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002171 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2172 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002174 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002175 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002177 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002178 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002180 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002181 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002183 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002184 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002186 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002187 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002188 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 goto done;
2190 }
2191 break;
2192
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002193 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 /*
2195 * Setting all of a previous oldext extent to newext.
2196 * The left neighbor is contiguous, the right is not.
2197 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002198 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002199
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002200 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002201 xfs_iext_prev(ifp, icur);
2202 xfs_iext_update_extent(ip, state, icur, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002203 XFS_IFORK_NEXT_SET(ip, whichfork,
2204 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 if (cur == NULL)
2206 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2207 else {
2208 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002209 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2210 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002212 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002213 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002215 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002216 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002218 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002219 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002220 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 goto done;
2222 }
2223 break;
2224
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002225 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 /*
2227 * Setting all of a previous oldext extent to newext.
2228 * The right neighbor is contiguous, the left is not.
2229 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002230 PREV.br_blockcount += RIGHT.br_blockcount;
2231 PREV.br_state = new->br_state;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002232
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002233 xfs_iext_next(ifp, icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002234 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002235 xfs_iext_prev(ifp, icur);
2236 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002237
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002238 XFS_IFORK_NEXT_SET(ip, whichfork,
2239 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (cur == NULL)
2241 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2242 else {
2243 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002244 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2245 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002247 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002248 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002250 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002251 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002253 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002254 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002255 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 goto done;
2257 }
2258 break;
2259
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002260 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 /*
2262 * Setting all of a previous oldext extent to newext.
2263 * Neither the left nor right neighbors are contiguous with
2264 * the new one.
2265 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002266 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002267 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002268
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 if (cur == NULL)
2270 rval = XFS_ILOG_DEXT;
2271 else {
2272 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002273 error = xfs_bmbt_lookup_eq(cur, new, &i);
2274 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002276 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002277 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002278 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 goto done;
2280 }
2281 break;
2282
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002283 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 /*
2285 * Setting the first part of a previous oldext extent to newext.
2286 * The left neighbor is contiguous.
2287 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002288 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002289
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002290 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002291 PREV.br_startoff += new->br_blockcount;
2292 PREV.br_startblock += new->br_blockcount;
2293 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002294
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002295 xfs_iext_update_extent(ip, state, icur, &PREV);
2296 xfs_iext_prev(ifp, icur);
2297 xfs_iext_update_extent(ip, state, icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002298
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 if (cur == NULL)
2300 rval = XFS_ILOG_DEXT;
2301 else {
2302 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002303 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002304 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002306 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002307 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002308 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002310 error = xfs_btree_decrement(cur, 0, &i);
2311 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002313 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002314 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 goto done;
2316 }
2317 break;
2318
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002319 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 /*
2321 * Setting the first part of a previous oldext extent to newext.
2322 * The left neighbor is not contiguous.
2323 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002324 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002325 PREV.br_startoff += new->br_blockcount;
2326 PREV.br_startblock += new->br_blockcount;
2327 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002328
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002329 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002330 xfs_iext_insert(ip, icur, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002331 XFS_IFORK_NEXT_SET(ip, whichfork,
2332 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 if (cur == NULL)
2334 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2335 else {
2336 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002337 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002338 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002340 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002341 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002342 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 goto done;
2344 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002345 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002347 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
2349 break;
2350
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002351 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 /*
2353 * Setting the last part of a previous oldext extent to newext.
2354 * The right neighbor is contiguous with the new allocation.
2355 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002356 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002357 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002358
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002359 RIGHT.br_startoff = new->br_startoff;
2360 RIGHT.br_startblock = new->br_startblock;
2361 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002362
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002363 xfs_iext_update_extent(ip, state, icur, &PREV);
2364 xfs_iext_next(ifp, icur);
2365 xfs_iext_update_extent(ip, state, icur, &RIGHT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (cur == NULL)
2368 rval = XFS_ILOG_DEXT;
2369 else {
2370 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002371 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002372 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002374 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002375 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002376 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002378 error = xfs_btree_increment(cur, 0, &i);
2379 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002381 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002382 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 goto done;
2384 }
2385 break;
2386
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002387 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 /*
2389 * Setting the last part of a previous oldext extent to newext.
2390 * The right neighbor is not contiguous.
2391 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002392 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002393 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002394
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002395 xfs_iext_update_extent(ip, state, icur, &PREV);
2396 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002397 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002398
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002399 XFS_IFORK_NEXT_SET(ip, whichfork,
2400 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 if (cur == NULL)
2402 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2403 else {
2404 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002405 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002406 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002408 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002409 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002410 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002412 error = xfs_bmbt_lookup_eq(cur, new, &i);
2413 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002415 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002416 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002418 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 }
2420 break;
2421
2422 case 0:
2423 /*
2424 * Setting the middle part of a previous oldext extent to
2425 * newext. Contiguity is impossible here.
2426 * One extent becomes three extents.
2427 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002428 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002429 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 r[0] = *new;
2432 r[1].br_startoff = new_endoff;
2433 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002434 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002436 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002437
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002438 xfs_iext_update_extent(ip, state, icur, &PREV);
2439 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002440 xfs_iext_insert(ip, icur, &r[1], state);
2441 xfs_iext_insert(ip, icur, &r[0], state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002442
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002443 XFS_IFORK_NEXT_SET(ip, whichfork,
2444 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 if (cur == NULL)
2446 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2447 else {
2448 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002449 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002450 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002452 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002454 error = xfs_bmbt_update(cur, &r[1]);
2455 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 goto done;
2457 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002459 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002461 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002462 /*
2463 * Reset the cursor to the position of the new extent
2464 * we are about to insert as we can't trust it after
2465 * the previous insert.
2466 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002467 error = xfs_bmbt_lookup_eq(cur, new, &i);
2468 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002470 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002472 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002474 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476 break;
2477
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002478 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2479 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2480 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2481 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2482 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2483 case BMAP_LEFT_CONTIG:
2484 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 /*
2486 * These cases are all impossible.
2487 */
2488 ASSERT(0);
2489 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002490
Darrick J. Wong9c194642016-08-03 12:16:05 +10002491 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002492 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002493 if (error)
2494 goto done;
2495
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002496 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002497 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002498 int tmp_logflags; /* partial log flag return val */
2499
2500 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002501 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002502 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002503 *logflagsp |= tmp_logflags;
2504 if (error)
2505 goto done;
2506 }
2507
2508 /* clear out the allocated field, done with it now in any case. */
2509 if (cur) {
2510 cur->bc_private.b.allocated = 0;
2511 *curp = cur;
2512 }
2513
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002514 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002516 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return error;
2518#undef LEFT
2519#undef RIGHT
2520#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521}
2522
2523/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002524 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002526STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527xfs_bmap_add_extent_hole_delay(
2528 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002529 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002530 struct xfs_iext_cursor *icur,
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002531 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002533 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2535 xfs_filblks_t newlen=0; /* new indirect size */
2536 xfs_filblks_t oldlen=0; /* old indirect size */
2537 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002538 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002539 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002541 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002542 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 /*
2545 * Check and set flags if this segment has a left neighbor
2546 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002547 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002548 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002549 if (isnullstartblock(left.br_startblock))
2550 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 /*
2554 * Check and set flags if the current (right) segment exists.
2555 * If it doesn't exist, we're converting the hole at end-of-file.
2556 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002557 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002558 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002559 if (isnullstartblock(right.br_startblock))
2560 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 /*
2564 * Set contiguity flags on the left and right neighbors.
2565 * Don't let extents get too large, even if the pieces are contiguous.
2566 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002567 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2568 left.br_startoff + left.br_blockcount == new->br_startoff &&
2569 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2570 state |= BMAP_LEFT_CONTIG;
2571
2572 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2573 new->br_startoff + new->br_blockcount == right.br_startoff &&
2574 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2575 (!(state & BMAP_LEFT_CONTIG) ||
2576 (left.br_blockcount + new->br_blockcount +
2577 right.br_blockcount <= MAXEXTLEN)))
2578 state |= BMAP_RIGHT_CONTIG;
2579
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 /*
2581 * Switch out based on the contiguity flags.
2582 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002583 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2584 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 /*
2586 * New allocation is contiguous with delayed allocations
2587 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002588 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 */
2590 temp = left.br_blockcount + new->br_blockcount +
2591 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002592
Eric Sandeen9d87c312009-01-14 23:22:07 -06002593 oldlen = startblockval(left.br_startblock) +
2594 startblockval(new->br_startblock) +
2595 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002596 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2597 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002598 left.br_startblock = nullstartblock(newlen);
2599 left.br_blockcount = temp;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002600
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002601 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002602 xfs_iext_prev(ifp, icur);
2603 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 break;
2605
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002606 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 /*
2608 * New allocation is contiguous with a delayed allocation
2609 * on the left.
2610 * Merge the new allocation with the left neighbor.
2611 */
2612 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002613
Eric Sandeen9d87c312009-01-14 23:22:07 -06002614 oldlen = startblockval(left.br_startblock) +
2615 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002616 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2617 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002618 left.br_blockcount = temp;
2619 left.br_startblock = nullstartblock(newlen);
Christoph Hellwig41d196f2017-11-03 10:34:39 -07002620
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002621 xfs_iext_prev(ifp, icur);
2622 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 break;
2624
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002625 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 /*
2627 * New allocation is contiguous with a delayed allocation
2628 * on the right.
2629 * Merge the new allocation with the right neighbor.
2630 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002632 oldlen = startblockval(new->br_startblock) +
2633 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002634 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2635 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002636 right.br_startoff = new->br_startoff;
2637 right.br_startblock = nullstartblock(newlen);
2638 right.br_blockcount = temp;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002639 xfs_iext_update_extent(ip, state, icur, &right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 break;
2641
2642 case 0:
2643 /*
2644 * New allocation is not contiguous with another
2645 * delayed allocation.
2646 * Insert a new entry.
2647 */
2648 oldlen = newlen = 0;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002649 xfs_iext_insert(ip, icur, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 break;
2651 }
2652 if (oldlen != newlen) {
2653 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002654 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2655 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 /*
2657 * Nothing to do for disk quota accounting here.
2658 */
2659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660}
2661
2662/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002663 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 */
2665STATIC int /* error */
2666xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002667 struct xfs_trans *tp,
2668 struct xfs_inode *ip,
2669 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002670 struct xfs_iext_cursor *icur,
Christoph Hellwig6d045582017-04-11 16:45:54 -07002671 struct xfs_btree_cur **curp,
2672 struct xfs_bmbt_irec *new,
2673 xfs_fsblock_t *first,
2674 struct xfs_defer_ops *dfops,
2675 int *logflagsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002677 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2678 struct xfs_mount *mp = ip->i_mount;
2679 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2683 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002684 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002685 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002686 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002688 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002689 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002690
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002691 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 /*
2694 * Check and set flags if this segment has a left neighbor.
2695 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002696 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002697 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002698 if (isnullstartblock(left.br_startblock))
2699 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002701
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 /*
2703 * Check and set flags if this segment has a current value.
2704 * Not true if we're inserting into the "hole" at eof.
2705 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002706 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002707 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002708 if (isnullstartblock(right.br_startblock))
2709 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002711
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 /*
2713 * We're inserting a real allocation between "left" and "right".
2714 * Set the contiguity flags. Don't let extents get too large.
2715 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002716 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2717 left.br_startoff + left.br_blockcount == new->br_startoff &&
2718 left.br_startblock + left.br_blockcount == new->br_startblock &&
2719 left.br_state == new->br_state &&
2720 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2721 state |= BMAP_LEFT_CONTIG;
2722
2723 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2724 new->br_startoff + new->br_blockcount == right.br_startoff &&
2725 new->br_startblock + new->br_blockcount == right.br_startblock &&
2726 new->br_state == right.br_state &&
2727 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2728 (!(state & BMAP_LEFT_CONTIG) ||
2729 left.br_blockcount + new->br_blockcount +
2730 right.br_blockcount <= MAXEXTLEN))
2731 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002733 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 /*
2735 * Select which case we're in here, and implement it.
2736 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002737 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2738 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 /*
2740 * New allocation is contiguous with real allocations on the
2741 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002742 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002744 left.br_blockcount += new->br_blockcount + right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002745
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002746 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002747 xfs_iext_prev(ifp, icur);
2748 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002749
Christoph Hellwig6d045582017-04-11 16:45:54 -07002750 XFS_IFORK_NEXT_SET(ip, whichfork,
2751 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2752 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002753 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002754 } else {
2755 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002756 error = xfs_bmbt_lookup_eq(cur, &right, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002757 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002758 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002759 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002760 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002761 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002762 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002763 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002764 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002765 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002766 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002767 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002768 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002769 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002770 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002772 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002774 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 /*
2776 * New allocation is contiguous with a real allocation
2777 * on the left.
2778 * Merge the new allocation with the left neighbor.
2779 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002780 old = left;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002781 left.br_blockcount += new->br_blockcount;
Christoph Hellwig1d2e0082017-11-03 10:34:40 -07002782
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002783 xfs_iext_prev(ifp, icur);
2784 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002785
Christoph Hellwig6d045582017-04-11 16:45:54 -07002786 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002787 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002788 } else {
2789 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002790 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002791 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002792 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002793 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002794 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002795 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002796 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002798 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002800 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 /*
2802 * New allocation is contiguous with a real allocation
2803 * on the right.
2804 * Merge the new allocation with the right neighbor.
2805 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002806 old = right;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002807
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002808 right.br_startoff = new->br_startoff;
2809 right.br_startblock = new->br_startblock;
2810 right.br_blockcount += new->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002811 xfs_iext_update_extent(ip, state, icur, &right);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002812
Christoph Hellwig6d045582017-04-11 16:45:54 -07002813 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002814 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002815 } else {
2816 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002817 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002818 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002819 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002820 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002821 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002822 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002823 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002825 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
2827 case 0:
2828 /*
2829 * New allocation is not contiguous with another
2830 * real allocation.
2831 * Insert a new entry.
2832 */
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002833 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002834 XFS_IFORK_NEXT_SET(ip, whichfork,
2835 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2836 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002837 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002838 } else {
2839 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002840 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002841 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002842 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002843 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002844 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002845 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002846 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002847 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002849 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002851
Darrick J. Wong9c194642016-08-03 12:16:05 +10002852 /* add reverse mapping */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002853 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002854 if (error)
2855 goto done;
2856
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002857 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002858 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002859 int tmp_logflags; /* partial log flag return val */
2860
Christoph Hellwig6d045582017-04-11 16:45:54 -07002861 ASSERT(cur == NULL);
2862 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002863 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002864 *logflagsp |= tmp_logflags;
2865 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002866 if (error)
2867 goto done;
2868 }
2869
2870 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002871 if (cur)
2872 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002873
Christoph Hellwig6d045582017-04-11 16:45:54 -07002874 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002875done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002876 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002877 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878}
2879
Nathan Scottdd9f4382006-01-11 15:28:28 +11002880/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11002881 * Functions used in the extent read, allocate and remove paths
2882 */
2883
2884/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002885 * Adjust the size of the new extent based on di_extsize and rt extsize.
2886 */
Dave Chinner68988112013-08-12 20:49:42 +10002887int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002888xfs_bmap_extsize_align(
2889 xfs_mount_t *mp,
2890 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2891 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2892 xfs_extlen_t extsz, /* align to this extent size */
2893 int rt, /* is this a realtime inode? */
2894 int eof, /* is extent at end-of-file? */
2895 int delay, /* creating delalloc extent? */
2896 int convert, /* overwriting unwritten extent? */
2897 xfs_fileoff_t *offp, /* in/out: aligned offset */
2898 xfs_extlen_t *lenp) /* in/out: aligned length */
2899{
2900 xfs_fileoff_t orig_off; /* original offset */
2901 xfs_extlen_t orig_alen; /* original length */
2902 xfs_fileoff_t orig_end; /* original off+len */
2903 xfs_fileoff_t nexto; /* next file offset */
2904 xfs_fileoff_t prevo; /* previous file offset */
2905 xfs_fileoff_t align_off; /* temp for offset */
2906 xfs_extlen_t align_alen; /* temp for length */
2907 xfs_extlen_t temp; /* temp for calculations */
2908
2909 if (convert)
2910 return 0;
2911
2912 orig_off = align_off = *offp;
2913 orig_alen = align_alen = *lenp;
2914 orig_end = orig_off + orig_alen;
2915
2916 /*
2917 * If this request overlaps an existing extent, then don't
2918 * attempt to perform any additional alignment.
2919 */
2920 if (!delay && !eof &&
2921 (orig_off >= gotp->br_startoff) &&
2922 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2923 return 0;
2924 }
2925
2926 /*
2927 * If the file offset is unaligned vs. the extent size
2928 * we need to align it. This will be possible unless
2929 * the file was previously written with a kernel that didn't
2930 * perform this alignment, or if a truncate shot us in the
2931 * foot.
2932 */
2933 temp = do_mod(orig_off, extsz);
2934 if (temp) {
2935 align_alen += temp;
2936 align_off -= temp;
2937 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10002938
2939 /* Same adjustment for the end of the requested area. */
2940 temp = (align_alen % extsz);
2941 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11002942 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10002943
2944 /*
2945 * For large extent hint sizes, the aligned extent might be larger than
2946 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2947 * the length back under MAXEXTLEN. The outer allocation loops handle
2948 * short allocation just fine, so it is safe to do this. We only want to
2949 * do it when we are forced to, though, because it means more allocation
2950 * operations are required.
2951 */
2952 while (align_alen > MAXEXTLEN)
2953 align_alen -= extsz;
2954 ASSERT(align_alen <= MAXEXTLEN);
2955
Nathan Scottdd9f4382006-01-11 15:28:28 +11002956 /*
2957 * If the previous block overlaps with this proposed allocation
2958 * then move the start forward without adjusting the length.
2959 */
2960 if (prevp->br_startoff != NULLFILEOFF) {
2961 if (prevp->br_startblock == HOLESTARTBLOCK)
2962 prevo = prevp->br_startoff;
2963 else
2964 prevo = prevp->br_startoff + prevp->br_blockcount;
2965 } else
2966 prevo = 0;
2967 if (align_off != orig_off && align_off < prevo)
2968 align_off = prevo;
2969 /*
2970 * If the next block overlaps with this proposed allocation
2971 * then move the start back without adjusting the length,
2972 * but not before offset 0.
2973 * This may of course make the start overlap previous block,
2974 * and if we hit the offset 0 limit then the next block
2975 * can still overlap too.
2976 */
2977 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2978 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2979 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2980 nexto = gotp->br_startoff + gotp->br_blockcount;
2981 else
2982 nexto = gotp->br_startoff;
2983 } else
2984 nexto = NULLFILEOFF;
2985 if (!eof &&
2986 align_off + align_alen != orig_end &&
2987 align_off + align_alen > nexto)
2988 align_off = nexto > align_alen ? nexto - align_alen : 0;
2989 /*
2990 * If we're now overlapping the next or previous extent that
2991 * means we can't fit an extsz piece in this hole. Just move
2992 * the start forward to the first valid spot and set
2993 * the length so we hit the end.
2994 */
2995 if (align_off != orig_off && align_off < prevo)
2996 align_off = prevo;
2997 if (align_off + align_alen != orig_end &&
2998 align_off + align_alen > nexto &&
2999 nexto != NULLFILEOFF) {
3000 ASSERT(nexto > prevo);
3001 align_alen = nexto - align_off;
3002 }
3003
3004 /*
3005 * If realtime, and the result isn't a multiple of the realtime
3006 * extent size we need to remove blocks until it is.
3007 */
3008 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3009 /*
3010 * We're not covering the original request, or
3011 * we won't be able to once we fix the length.
3012 */
3013 if (orig_off < align_off ||
3014 orig_end > align_off + align_alen ||
3015 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003016 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003017 /*
3018 * Try to fix it by moving the start up.
3019 */
3020 if (align_off + temp <= orig_off) {
3021 align_alen -= temp;
3022 align_off += temp;
3023 }
3024 /*
3025 * Try to fix it by moving the end in.
3026 */
3027 else if (align_off + align_alen - temp >= orig_end)
3028 align_alen -= temp;
3029 /*
3030 * Set the start to the minimum then trim the length.
3031 */
3032 else {
3033 align_alen -= orig_off - align_off;
3034 align_off = orig_off;
3035 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3036 }
3037 /*
3038 * Result doesn't cover the request, fail it.
3039 */
3040 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003041 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003042 } else {
3043 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003044 /* see MAXEXTLEN handling above */
3045 ASSERT(orig_end <= align_off + align_alen ||
3046 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003047 }
3048
3049#ifdef DEBUG
3050 if (!eof && gotp->br_startoff != NULLFILEOFF)
3051 ASSERT(align_off + align_alen <= gotp->br_startoff);
3052 if (prevp->br_startoff != NULLFILEOFF)
3053 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3054#endif
3055
3056 *lenp = align_alen;
3057 *offp = align_off;
3058 return 0;
3059}
3060
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061#define XFS_ALLOC_GAP_UNITS 4
3062
Dave Chinner68988112013-08-12 20:49:42 +10003063void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003064xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003065 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066{
3067 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3069 xfs_mount_t *mp; /* mount point structure */
3070 int nullfb; /* true if ap->firstblock isn't set */
3071 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
3073#define ISVALID(x,y) \
3074 (rt ? \
3075 (x) < mp->m_sb.sb_rblocks : \
3076 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3077 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3078 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3079
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003081 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003082 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3083 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003084 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 /*
3086 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003087 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003089 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3090 !isnullstartblock(ap->prev.br_startblock) &&
3091 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3092 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003093 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 /*
3095 * Adjust for the gap between prevp and us.
3096 */
Dave Chinner3a756672011-09-18 20:40:58 +00003097 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003098 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003100 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3101 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 }
3103 /*
3104 * If not at eof, then compare the two neighbor blocks.
3105 * Figure out whether either one gives us a good starting point,
3106 * and pick the better one.
3107 */
3108 else if (!ap->eof) {
3109 xfs_fsblock_t gotbno; /* right side block number */
3110 xfs_fsblock_t gotdiff=0; /* right side difference */
3111 xfs_fsblock_t prevbno; /* left side block number */
3112 xfs_fsblock_t prevdiff=0; /* left side difference */
3113
3114 /*
3115 * If there's a previous (left) block, select a requested
3116 * start block based on it.
3117 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003118 if (ap->prev.br_startoff != NULLFILEOFF &&
3119 !isnullstartblock(ap->prev.br_startblock) &&
3120 (prevbno = ap->prev.br_startblock +
3121 ap->prev.br_blockcount) &&
3122 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 /*
3124 * Calculate gap to end of previous block.
3125 */
Dave Chinner3a756672011-09-18 20:40:58 +00003126 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003127 (ap->prev.br_startoff +
3128 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 /*
3130 * Figure the startblock based on the previous block's
3131 * end and the gap size.
3132 * Heuristic!
3133 * If the gap is large relative to the piece we're
3134 * allocating, or using it gives us an invalid block
3135 * number, then just use the end of the previous block.
3136 */
Dave Chinner3a756672011-09-18 20:40:58 +00003137 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003139 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 prevbno += adjust;
3141 else
3142 prevdiff += adjust;
3143 /*
3144 * If the firstblock forbids it, can't use it,
3145 * must use default.
3146 */
3147 if (!rt && !nullfb &&
3148 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3149 prevbno = NULLFSBLOCK;
3150 }
3151 /*
3152 * No previous block or can't follow it, just default.
3153 */
3154 else
3155 prevbno = NULLFSBLOCK;
3156 /*
3157 * If there's a following (right) block, select a requested
3158 * start block based on it.
3159 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003160 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 /*
3162 * Calculate gap to start of next block.
3163 */
Dave Chinner3a756672011-09-18 20:40:58 +00003164 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 /*
3166 * Figure the startblock based on the next block's
3167 * start and the gap size.
3168 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003169 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 /*
3171 * Heuristic!
3172 * If the gap is large relative to the piece we're
3173 * allocating, or using it gives us an invalid block
3174 * number, then just use the start of the next block
3175 * offset by our length.
3176 */
Dave Chinner3a756672011-09-18 20:40:58 +00003177 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 ISVALID(gotbno - gotdiff, gotbno))
3179 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003180 else if (ISVALID(gotbno - ap->length, gotbno)) {
3181 gotbno -= ap->length;
3182 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 } else
3184 gotdiff += adjust;
3185 /*
3186 * If the firstblock forbids it, can't use it,
3187 * must use default.
3188 */
3189 if (!rt && !nullfb &&
3190 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3191 gotbno = NULLFSBLOCK;
3192 }
3193 /*
3194 * No next block, just default.
3195 */
3196 else
3197 gotbno = NULLFSBLOCK;
3198 /*
3199 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003200 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 */
3202 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003203 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003205 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003207 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003209#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003210}
3211
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003212static int
3213xfs_bmap_longest_free_extent(
3214 struct xfs_trans *tp,
3215 xfs_agnumber_t ag,
3216 xfs_extlen_t *blen,
3217 int *notinit)
3218{
3219 struct xfs_mount *mp = tp->t_mountp;
3220 struct xfs_perag *pag;
3221 xfs_extlen_t longest;
3222 int error = 0;
3223
3224 pag = xfs_perag_get(mp, ag);
3225 if (!pag->pagf_init) {
3226 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3227 if (error)
3228 goto out;
3229
3230 if (!pag->pagf_init) {
3231 *notinit = 1;
3232 goto out;
3233 }
3234 }
3235
Eric Sandeena1f69412018-04-06 10:09:42 -07003236 longest = xfs_alloc_longest_free_extent(pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003237 xfs_alloc_min_freelist(mp, pag),
3238 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003239 if (*blen < longest)
3240 *blen = longest;
3241
3242out:
3243 xfs_perag_put(pag);
3244 return error;
3245}
3246
3247static void
3248xfs_bmap_select_minlen(
3249 struct xfs_bmalloca *ap,
3250 struct xfs_alloc_arg *args,
3251 xfs_extlen_t *blen,
3252 int notinit)
3253{
3254 if (notinit || *blen < ap->minlen) {
3255 /*
3256 * Since we did a BUF_TRYLOCK above, it is possible that
3257 * there is space for this request.
3258 */
3259 args->minlen = ap->minlen;
3260 } else if (*blen < args->maxlen) {
3261 /*
3262 * If the best seen length is less than the request length,
3263 * use the best as the minimum.
3264 */
3265 args->minlen = *blen;
3266 } else {
3267 /*
3268 * Otherwise we've seen an extent as big as maxlen, use that
3269 * as the minimum.
3270 */
3271 args->minlen = args->maxlen;
3272 }
3273}
3274
Nathan Scotta365bdd2006-03-14 13:34:16 +11003275STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003276xfs_bmap_btalloc_nullfb(
3277 struct xfs_bmalloca *ap,
3278 struct xfs_alloc_arg *args,
3279 xfs_extlen_t *blen)
3280{
3281 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003282 xfs_agnumber_t ag, startag;
3283 int notinit = 0;
3284 int error;
3285
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003286 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003287 args->total = ap->total;
3288
Christoph Hellwigc467c042010-02-15 23:34:42 +00003289 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3290 if (startag == NULLAGNUMBER)
3291 startag = ag = 0;
3292
Dave Chinner14b064c2011-01-27 12:16:28 +11003293 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003294 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3295 &notinit);
3296 if (error)
3297 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003298
Christoph Hellwigc467c042010-02-15 23:34:42 +00003299 if (++ag == mp->m_sb.sb_agcount)
3300 ag = 0;
3301 if (ag == startag)
3302 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003303 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003304
3305 xfs_bmap_select_minlen(ap, args, blen, notinit);
3306 return 0;
3307}
3308
3309STATIC int
3310xfs_bmap_btalloc_filestreams(
3311 struct xfs_bmalloca *ap,
3312 struct xfs_alloc_arg *args,
3313 xfs_extlen_t *blen)
3314{
3315 struct xfs_mount *mp = ap->ip->i_mount;
3316 xfs_agnumber_t ag;
3317 int notinit = 0;
3318 int error;
3319
3320 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3321 args->total = ap->total;
3322
3323 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3324 if (ag == NULLAGNUMBER)
3325 ag = 0;
3326
3327 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3328 if (error)
3329 return error;
3330
3331 if (*blen < args->maxlen) {
3332 error = xfs_filestream_new_ag(ap, &ag);
3333 if (error)
3334 return error;
3335
3336 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3337 &notinit);
3338 if (error)
3339 return error;
3340
3341 }
3342
3343 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003344
3345 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003346 * Set the failure fallback case to look in the selected AG as stream
3347 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003348 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003349 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003350 return 0;
3351}
3352
Darrick J. Wong751f3762018-01-25 13:58:13 -08003353/* Update all inode and quota accounting for the allocation we just did. */
3354static void
3355xfs_bmap_btalloc_accounting(
3356 struct xfs_bmalloca *ap,
3357 struct xfs_alloc_arg *args)
3358{
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003359 if (ap->flags & XFS_BMAPI_COWFORK) {
3360 /*
3361 * COW fork blocks are in-core only and thus are treated as
3362 * in-core quota reservation (like delalloc blocks) even when
3363 * converted to real blocks. The quota reservation is not
3364 * accounted to disk until blocks are remapped to the data
3365 * fork. So if these blocks were previously delalloc, we
3366 * already have quota reservation and there's nothing to do
3367 * yet.
3368 */
3369 if (ap->wasdel)
3370 return;
3371
3372 /*
3373 * Otherwise, we've allocated blocks in a hole. The transaction
3374 * has acquired in-core quota reservation for this extent.
3375 * Rather than account these as real blocks, however, we reduce
3376 * the transaction quota reservation based on the allocation.
3377 * This essentially transfers the transaction quota reservation
3378 * to that of a delalloc extent.
3379 */
3380 ap->ip->i_delayed_blks += args->len;
3381 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3382 -(long)args->len);
3383 return;
3384 }
3385
3386 /* data/attr fork only */
3387 ap->ip->i_d.di_nblocks += args->len;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003388 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3389 if (ap->wasdel)
3390 ap->ip->i_delayed_blks -= args->len;
3391 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3392 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3393 args->len);
3394}
3395
Christoph Hellwigc467c042010-02-15 23:34:42 +00003396STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003397xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003398 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003399{
3400 xfs_mount_t *mp; /* mount point structure */
3401 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003402 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003403 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003404 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003405 xfs_alloc_arg_t args;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003406 xfs_fileoff_t orig_offset;
3407 xfs_extlen_t orig_length;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003408 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003409 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003410 int nullfb; /* true if ap->firstblock isn't set */
3411 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003412 int tryagain;
3413 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003414 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003415
Dave Chinnera99ebf42011-12-01 11:24:20 +00003416 ASSERT(ap->length);
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003417 orig_offset = ap->offset;
3418 orig_length = ap->length;
Dave Chinnera99ebf42011-12-01 11:24:20 +00003419
Nathan Scotta365bdd2006-03-14 13:34:16 +11003420 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003421
3422 /* stripe alignment for allocation is determined by mount parameters */
3423 stripe_align = 0;
3424 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3425 stripe_align = mp->m_swidth;
3426 else if (mp->m_dalign)
3427 stripe_align = mp->m_dalign;
3428
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003429 if (ap->flags & XFS_BMAPI_COWFORK)
3430 align = xfs_get_cowextsz_hint(ap->ip);
3431 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003432 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003433 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003434 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003435 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003436 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003437 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003438 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003439 }
Dave Chinner33177f052013-12-12 16:34:36 +11003440
3441
Dave Chinner0937e0f2011-09-18 20:40:57 +00003442 nullfb = *ap->firstblock == NULLFSBLOCK;
3443 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003444 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003445 if (xfs_alloc_is_userdata(ap->datatype) &&
3446 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003447 ag = xfs_filestream_lookup_ag(ap->ip);
3448 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003449 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003450 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003451 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003452 }
3453 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003454 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003455
3456 xfs_bmap_adjacent(ap);
3457
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003459 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 * it's in the right allocation group.
3461 */
Dave Chinner3a756672011-09-18 20:40:58 +00003462 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 ;
3464 else
Dave Chinner3a756672011-09-18 20:40:58 +00003465 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 * Normal allocation, done through xfs_alloc_vextent.
3468 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003469 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003470 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003471 args.tp = ap->tp;
3472 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003473 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003474 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003475
3476 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003477 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003478 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003479 blen = 0;
3480 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003481 /*
3482 * Search for an allocation group with a single extent large
3483 * enough for the request. If one isn't found, then adjust
3484 * the minimum allocation size to the largest space found.
3485 */
Dave Chinner292378e2016-09-26 08:21:28 +10003486 if (xfs_alloc_is_userdata(ap->datatype) &&
3487 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003488 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3489 else
3490 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003491 if (error)
3492 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003493 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003494 if (xfs_inode_is_filestream(ap->ip))
3495 args.type = XFS_ALLOCTYPE_FIRST_AG;
3496 else
3497 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003498 args.total = args.minlen = ap->minlen;
3499 } else {
3500 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3501 args.total = ap->total;
3502 args.minlen = ap->minlen;
3503 }
David Chinner957d0eb2007-06-18 16:50:37 +10003504 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003505 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003506 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003507 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003508 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003509 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003510 args.prod = 1;
3511 args.mod = 0;
3512 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003513 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003514 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003515 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3516 }
3517 /*
3518 * If we are not low on available data blocks, and the
3519 * underlying logical volume manager is a stripe, and
3520 * the file offset is zero then try to allocate data
3521 * blocks on stripe unit boundary.
3522 * NOTE: ap->aeof is only set if the allocation length
3523 * is >= the stripe unit and the allocation offset is
3524 * at the end of file.
3525 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003526 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003527 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003528 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003529 atype = args.type;
3530 isaligned = 1;
3531 /*
3532 * Adjust for alignment
3533 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003534 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003535 args.minlen = blen - args.alignment;
3536 args.minalignslop = 0;
3537 } else {
3538 /*
3539 * First try an exact bno allocation.
3540 * If it fails then do a near or start bno
3541 * allocation with alignment turned on.
3542 */
3543 atype = args.type;
3544 tryagain = 1;
3545 args.type = XFS_ALLOCTYPE_THIS_BNO;
3546 args.alignment = 1;
3547 /*
3548 * Compute the minlen+alignment for the
3549 * next case. Set slop so that the value
3550 * of minlen+alignment+slop doesn't go up
3551 * between the calls.
3552 */
Dave Chinner33177f052013-12-12 16:34:36 +11003553 if (blen > stripe_align && blen <= args.maxlen)
3554 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003555 else
3556 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003557 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003558 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003559 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003560 args.minlen - 1;
3561 else
3562 args.minalignslop = 0;
3563 }
3564 } else {
3565 args.alignment = 1;
3566 args.minalignslop = 0;
3567 }
3568 args.minleft = ap->minleft;
3569 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003570 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003571 args.datatype = ap->datatype;
3572 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003573 args.ip = ap->ip;
3574
3575 error = xfs_alloc_vextent(&args);
3576 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003577 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003578
Nathan Scotta365bdd2006-03-14 13:34:16 +11003579 if (tryagain && args.fsbno == NULLFSBLOCK) {
3580 /*
3581 * Exact allocation failed. Now try with alignment
3582 * turned on.
3583 */
3584 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003585 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003586 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003587 args.minlen = nextminlen;
3588 args.minalignslop = 0;
3589 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 if ((error = xfs_alloc_vextent(&args)))
3591 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003592 }
3593 if (isaligned && args.fsbno == NULLFSBLOCK) {
3594 /*
3595 * allocation failed, so turn off alignment and
3596 * try again.
3597 */
3598 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003599 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003600 args.alignment = 0;
3601 if ((error = xfs_alloc_vextent(&args)))
3602 return error;
3603 }
3604 if (args.fsbno == NULLFSBLOCK && nullfb &&
3605 args.minlen > ap->minlen) {
3606 args.minlen = ap->minlen;
3607 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003608 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003609 if ((error = xfs_alloc_vextent(&args)))
3610 return error;
3611 }
3612 if (args.fsbno == NULLFSBLOCK && nullfb) {
3613 args.fsbno = 0;
3614 args.type = XFS_ALLOCTYPE_FIRST_AG;
3615 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003616 if ((error = xfs_alloc_vextent(&args)))
3617 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003618 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003619 }
3620 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003621 /*
3622 * check the allocation happened at the same or higher AG than
3623 * the first block that was allocated.
3624 */
3625 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003626 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3627 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003628
Dave Chinner3a756672011-09-18 20:40:58 +00003629 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003630 if (*ap->firstblock == NULLFSBLOCK)
3631 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003632 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003633 ap->length = args.len;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003634 /*
3635 * If the extent size hint is active, we tried to round the
3636 * caller's allocation request offset down to extsz and the
3637 * length up to another extsz boundary. If we found a free
3638 * extent we mapped it in starting at this new offset. If the
3639 * newly mapped space isn't long enough to cover any of the
3640 * range of offsets that was originally requested, move the
3641 * mapping up so that we can fill as much of the caller's
3642 * original request as possible. Free space is apparently
3643 * very fragmented so we're unlikely to be able to satisfy the
3644 * hints anyway.
3645 */
3646 if (ap->length <= orig_length)
3647 ap->offset = orig_offset;
3648 else if (ap->offset + ap->length < orig_offset + orig_length)
3649 ap->offset = orig_offset + orig_length - ap->length;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003650 xfs_bmap_btalloc_accounting(ap, &args);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003651 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003652 ap->blkno = NULLFSBLOCK;
3653 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 }
3655 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003656}
3657
3658/*
3659 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3660 * It figures out where to ask the underlying allocator to put the new extent.
3661 */
3662STATIC int
3663xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003664 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003665{
Dave Chinner292378e2016-09-26 08:21:28 +10003666 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3667 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003668 return xfs_bmap_rtalloc(ap);
3669 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670}
3671
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003672/* Trim extent to fit a logical block range. */
3673void
3674xfs_trim_extent(
3675 struct xfs_bmbt_irec *irec,
3676 xfs_fileoff_t bno,
3677 xfs_filblks_t len)
3678{
3679 xfs_fileoff_t distance;
3680 xfs_fileoff_t end = bno + len;
3681
3682 if (irec->br_startoff + irec->br_blockcount <= bno ||
3683 irec->br_startoff >= end) {
3684 irec->br_blockcount = 0;
3685 return;
3686 }
3687
3688 if (irec->br_startoff < bno) {
3689 distance = bno - irec->br_startoff;
3690 if (isnullstartblock(irec->br_startblock))
3691 irec->br_startblock = DELAYSTARTBLOCK;
3692 if (irec->br_startblock != DELAYSTARTBLOCK &&
3693 irec->br_startblock != HOLESTARTBLOCK)
3694 irec->br_startblock += distance;
3695 irec->br_startoff += distance;
3696 irec->br_blockcount -= distance;
3697 }
3698
3699 if (end < irec->br_startoff + irec->br_blockcount) {
3700 distance = irec->br_startoff + irec->br_blockcount - end;
3701 irec->br_blockcount -= distance;
3702 }
3703}
3704
Brian Foster40214d12017-10-13 09:47:46 -07003705/* trim extent to within eof */
3706void
3707xfs_trim_extent_eof(
3708 struct xfs_bmbt_irec *irec,
3709 struct xfs_inode *ip)
3710
3711{
3712 xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
3713 i_size_read(VFS_I(ip))));
3714}
3715
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003717 * Trim the returned map to the required bounds
3718 */
3719STATIC void
3720xfs_bmapi_trim_map(
3721 struct xfs_bmbt_irec *mval,
3722 struct xfs_bmbt_irec *got,
3723 xfs_fileoff_t *bno,
3724 xfs_filblks_t len,
3725 xfs_fileoff_t obno,
3726 xfs_fileoff_t end,
3727 int n,
3728 int flags)
3729{
3730 if ((flags & XFS_BMAPI_ENTIRE) ||
3731 got->br_startoff + got->br_blockcount <= obno) {
3732 *mval = *got;
3733 if (isnullstartblock(got->br_startblock))
3734 mval->br_startblock = DELAYSTARTBLOCK;
3735 return;
3736 }
3737
3738 if (obno > *bno)
3739 *bno = obno;
3740 ASSERT((*bno >= obno) || (n == 0));
3741 ASSERT(*bno < end);
3742 mval->br_startoff = *bno;
3743 if (isnullstartblock(got->br_startblock))
3744 mval->br_startblock = DELAYSTARTBLOCK;
3745 else
3746 mval->br_startblock = got->br_startblock +
3747 (*bno - got->br_startoff);
3748 /*
3749 * Return the minimum of what we got and what we asked for for
3750 * the length. We can use the len variable here because it is
3751 * modified below and we could have been there before coming
3752 * here if the first part of the allocation didn't overlap what
3753 * was asked for.
3754 */
3755 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3756 got->br_blockcount - (*bno - got->br_startoff));
3757 mval->br_state = got->br_state;
3758 ASSERT(mval->br_blockcount <= len);
3759 return;
3760}
3761
3762/*
3763 * Update and validate the extent map to return
3764 */
3765STATIC void
3766xfs_bmapi_update_map(
3767 struct xfs_bmbt_irec **map,
3768 xfs_fileoff_t *bno,
3769 xfs_filblks_t *len,
3770 xfs_fileoff_t obno,
3771 xfs_fileoff_t end,
3772 int *n,
3773 int flags)
3774{
3775 xfs_bmbt_irec_t *mval = *map;
3776
3777 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3778 ((mval->br_startoff + mval->br_blockcount) <= end));
3779 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3780 (mval->br_startoff < obno));
3781
3782 *bno = mval->br_startoff + mval->br_blockcount;
3783 *len = end - *bno;
3784 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3785 /* update previous map with new information */
3786 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3787 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3788 ASSERT(mval->br_state == mval[-1].br_state);
3789 mval[-1].br_blockcount = mval->br_blockcount;
3790 mval[-1].br_state = mval->br_state;
3791 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3792 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3793 mval[-1].br_startblock != HOLESTARTBLOCK &&
3794 mval->br_startblock == mval[-1].br_startblock +
3795 mval[-1].br_blockcount &&
3796 ((flags & XFS_BMAPI_IGSTATE) ||
3797 mval[-1].br_state == mval->br_state)) {
3798 ASSERT(mval->br_startoff ==
3799 mval[-1].br_startoff + mval[-1].br_blockcount);
3800 mval[-1].br_blockcount += mval->br_blockcount;
3801 } else if (*n > 0 &&
3802 mval->br_startblock == DELAYSTARTBLOCK &&
3803 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3804 mval->br_startoff ==
3805 mval[-1].br_startoff + mval[-1].br_blockcount) {
3806 mval[-1].br_blockcount += mval->br_blockcount;
3807 mval[-1].br_state = mval->br_state;
3808 } else if (!((*n == 0) &&
3809 ((mval->br_startoff + mval->br_blockcount) <=
3810 obno))) {
3811 mval++;
3812 (*n)++;
3813 }
3814 *map = mval;
3815}
3816
3817/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003818 * Map file blocks to filesystem blocks without allocation.
3819 */
3820int
3821xfs_bmapi_read(
3822 struct xfs_inode *ip,
3823 xfs_fileoff_t bno,
3824 xfs_filblks_t len,
3825 struct xfs_bmbt_irec *mval,
3826 int *nmap,
3827 int flags)
3828{
3829 struct xfs_mount *mp = ip->i_mount;
3830 struct xfs_ifork *ifp;
3831 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003832 xfs_fileoff_t obno;
3833 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003834 struct xfs_iext_cursor icur;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003835 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003836 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003837 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003838 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003839
3840 ASSERT(*nmap >= 1);
3841 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003842 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003843 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003844
3845 if (unlikely(XFS_TEST_ERROR(
3846 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3847 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003848 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003849 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003850 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003851 }
3852
3853 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003854 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003855
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003856 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003857
3858 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003859
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003860 /* No CoW fork? Return a hole. */
3861 if (whichfork == XFS_COW_FORK && !ifp) {
3862 mval->br_startoff = bno;
3863 mval->br_startblock = HOLESTARTBLOCK;
3864 mval->br_blockcount = len;
3865 mval->br_state = XFS_EXT_NORM;
3866 *nmap = 1;
3867 return 0;
3868 }
3869
Dave Chinner5c8ed202011-09-18 20:40:45 +00003870 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3871 error = xfs_iread_extents(NULL, ip, whichfork);
3872 if (error)
3873 return error;
3874 }
3875
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003876 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003877 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003878 end = bno + len;
3879 obno = bno;
3880
3881 while (bno < end && n < *nmap) {
3882 /* Reading past eof, act as though there's a hole up to end. */
3883 if (eof)
3884 got.br_startoff = end;
3885 if (got.br_startoff > bno) {
3886 /* Reading in a hole. */
3887 mval->br_startoff = bno;
3888 mval->br_startblock = HOLESTARTBLOCK;
3889 mval->br_blockcount =
3890 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3891 mval->br_state = XFS_EXT_NORM;
3892 bno += mval->br_blockcount;
3893 len -= mval->br_blockcount;
3894 mval++;
3895 n++;
3896 continue;
3897 }
3898
3899 /* set up the extent map to return. */
3900 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3901 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3902
3903 /* If we're done, stop now. */
3904 if (bno >= end || n >= *nmap)
3905 break;
3906
3907 /* Else go on to the next record. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003908 if (!xfs_iext_next_extent(ifp, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003909 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003910 }
3911 *nmap = n;
3912 return 0;
3913}
3914
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003915/*
3916 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3917 * global pool and the extent inserted into the inode in-core extent tree.
3918 *
3919 * On entry, got refers to the first extent beyond the offset of the extent to
3920 * allocate or eof is specified if no such extent exists. On return, got refers
3921 * to the extent record that was inserted to the inode fork.
3922 *
3923 * Note that the allocated extent may have been merged with contiguous extents
3924 * during insertion into the inode fork. Thus, got does not reflect the current
3925 * state of the inode fork on return. If necessary, the caller can use lastx to
3926 * look up the updated record in the inode fork.
3927 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10003928int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003929xfs_bmapi_reserve_delalloc(
3930 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003931 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11003932 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003933 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11003934 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003935 struct xfs_bmbt_irec *got,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003936 struct xfs_iext_cursor *icur,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003937 int eof)
3938{
3939 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003940 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003941 xfs_extlen_t alen;
3942 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003943 int error;
Brian Foster974ae922016-11-28 14:57:42 +11003944 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003945
Brian Foster974ae922016-11-28 14:57:42 +11003946 /*
3947 * Cap the alloc length. Keep track of prealloc so we know whether to
3948 * tag the inode before we return.
3949 */
3950 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003951 if (!eof)
3952 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11003953 if (prealloc && alen >= len)
3954 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003955
3956 /* Figure out the extent size, adjust alen */
Shan Hai6ca30722018-01-23 13:56:11 -08003957 if (whichfork == XFS_COW_FORK) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003958 struct xfs_bmbt_irec prev;
Shan Hai6ca30722018-01-23 13:56:11 -08003959 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003960
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003961 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003962 prev.br_startoff = NULLFILEOFF;
3963
Shan Hai6ca30722018-01-23 13:56:11 -08003964 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003965 1, 0, &aoff, &alen);
3966 ASSERT(!error);
3967 }
3968
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003969 /*
3970 * Make a transaction-less quota reservation for delayed allocation
3971 * blocks. This number gets adjusted later. We return if we haven't
3972 * allocated blocks already inside this loop.
3973 */
3974 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
Shan Hai6ca30722018-01-23 13:56:11 -08003975 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003976 if (error)
3977 return error;
3978
3979 /*
3980 * Split changing sb for alen and indlen since they could be coming
3981 * from different places.
3982 */
3983 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
3984 ASSERT(indlen > 0);
3985
Shan Hai6ca30722018-01-23 13:56:11 -08003986 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003987 if (error)
3988 goto out_unreserve_quota;
3989
Dave Chinner0d485ad2015-02-23 21:22:03 +11003990 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003991 if (error)
3992 goto out_unreserve_blocks;
3993
3994
3995 ip->i_delayed_blks += alen;
3996
3997 got->br_startoff = aoff;
3998 got->br_startblock = nullstartblock(indlen);
3999 got->br_blockcount = alen;
4000 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004001
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004002 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004003
Brian Foster974ae922016-11-28 14:57:42 +11004004 /*
4005 * Tag the inode if blocks were preallocated. Note that COW fork
4006 * preallocation can occur at the start or end of the extent, even when
4007 * prealloc == 0, so we must also check the aligned offset and length.
4008 */
4009 if (whichfork == XFS_DATA_FORK && prealloc)
4010 xfs_inode_set_eofblocks_tag(ip);
4011 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4012 xfs_inode_set_cowblocks_tag(ip);
4013
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004014 return 0;
4015
4016out_unreserve_blocks:
Shan Hai6ca30722018-01-23 13:56:11 -08004017 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004018out_unreserve_quota:
4019 if (XFS_IS_QUOTA_ON(mp))
Shan Hai6ca30722018-01-23 13:56:11 -08004020 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0,
4021 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004022 return error;
4023}
4024
Dave Chinnercf11da92014-07-15 07:08:24 +10004025static int
4026xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004027 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004028{
4029 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004030 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004031 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004032 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004033 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004034
Dave Chinnera99ebf42011-12-01 11:24:20 +00004035 ASSERT(bma->length > 0);
4036
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004037 /*
4038 * For the wasdelay case, we could also just allocate the stuff asked
4039 * for in this bmap call but that wouldn't be as good.
4040 */
4041 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004042 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4043 bma->offset = bma->got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004044 xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004045 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004046 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004047 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004048 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004049 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004050 }
4051
4052 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004053 * Set the data type being allocated. For the data fork, the first data
4054 * in the file is treated differently to all other allocations. For the
4055 * attribute fork, we only need to ensure the allocated range is not on
4056 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004057 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004058 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004059 bma->datatype = XFS_ALLOC_NOBUSY;
4060 if (whichfork == XFS_DATA_FORK) {
4061 if (bma->offset == 0)
4062 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4063 else
4064 bma->datatype |= XFS_ALLOC_USERDATA;
4065 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004066 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004067 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004068 }
4069
Dave Chinnere04426b2012-10-05 11:06:59 +10004070 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004071
4072 /*
4073 * Only want to do the alignment at the eof if it is userdata and
4074 * allocation length is larger than a stripe unit.
4075 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004076 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004077 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004078 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004079 if (error)
4080 return error;
4081 }
4082
4083 error = xfs_bmap_alloc(bma);
4084 if (error)
4085 return error;
4086
Dave Chinner29c8d172011-09-18 20:41:00 +00004087 if (bma->cur)
4088 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004089 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004090 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004091 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4092 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4093 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004094 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004095 }
4096 /*
4097 * Bump the number of extents we've allocated
4098 * in this call.
4099 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004100 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004101
Dave Chinner29c8d172011-09-18 20:41:00 +00004102 if (bma->cur)
4103 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004104 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4105
Dave Chinner963c30c2011-09-18 20:40:59 +00004106 bma->got.br_startoff = bma->offset;
4107 bma->got.br_startblock = bma->blkno;
4108 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004109 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004110
4111 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004112 * In the data fork, a wasdelay extent has been initialized, so
4113 * shouldn't be flagged as unwritten.
4114 *
4115 * For the cow fork, however, we convert delalloc reservations
4116 * (extents allocated for speculative preallocation) to
4117 * allocated unwritten extents, and only convert the unwritten
4118 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004119 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004120 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4121 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004122 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004123 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004124
Christoph Hellwigc6534242011-09-18 20:41:05 +00004125 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004126 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004127 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004128 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004129 whichfork, &bma->icur, &bma->cur, &bma->got,
Christoph Hellwig6d045582017-04-11 16:45:54 -07004130 bma->firstblock, bma->dfops, &bma->logflags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004131
Christoph Hellwigc315c902011-09-18 20:41:02 +00004132 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004133 if (error)
4134 return error;
4135
4136 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004137 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4138 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4139 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004140 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004141 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004142
Dave Chinner963c30c2011-09-18 20:40:59 +00004143 ASSERT(bma->got.br_startoff <= bma->offset);
4144 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4145 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004146 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4147 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004148 return 0;
4149}
4150
Dave Chinnerb447fe52011-09-18 20:40:51 +00004151STATIC int
4152xfs_bmapi_convert_unwritten(
4153 struct xfs_bmalloca *bma,
4154 struct xfs_bmbt_irec *mval,
4155 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004156 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004157{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004158 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004159 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004160 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004161 int error;
4162
Dave Chinnerb447fe52011-09-18 20:40:51 +00004163 /* check if we need to do unwritten->real conversion */
4164 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4165 (flags & XFS_BMAPI_PREALLOC))
4166 return 0;
4167
4168 /* check if we need to do real->unwritten conversion */
4169 if (mval->br_state == XFS_EXT_NORM &&
4170 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4171 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4172 return 0;
4173
4174 /*
4175 * Modify (by adding) the state flag, if writing.
4176 */
4177 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004178 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4179 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004180 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004181 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004182 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004183 }
4184 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4185 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4186
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004187 /*
4188 * Before insertion into the bmbt, zero the range being converted
4189 * if required.
4190 */
4191 if (flags & XFS_BMAPI_ZERO) {
4192 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4193 mval->br_blockcount);
4194 if (error)
4195 return error;
4196 }
4197
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004198 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004199 &bma->icur, &bma->cur, mval, bma->firstblock,
4200 bma->dfops, &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004201 /*
4202 * Log the inode core unconditionally in the unwritten extent conversion
4203 * path because the conversion might not have done so (e.g., if the
4204 * extent count hasn't changed). We need to make sure the inode is dirty
4205 * in the transaction for the sake of fsync(), even if nothing has
4206 * changed, because fsync() will not force the log for this transaction
4207 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004208 *
4209 * Note: If we're only converting cow fork extents, there aren't
4210 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004211 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004212 if (whichfork != XFS_COW_FORK)
4213 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004214 if (error)
4215 return error;
4216
4217 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004218 * Update our extent pointer, given that
4219 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4220 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004221 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004222 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004223
4224 /*
4225 * We may have combined previously unwritten space with written space,
4226 * so generate another request.
4227 */
4228 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004229 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004230 return 0;
4231}
4232
Christoph Hellwig44032802011-09-18 20:40:48 +00004233/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004234 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4235 * extent state if necessary. Details behaviour is controlled by the flags
4236 * parameter. Only allocates blocks from a single allocation group, to avoid
4237 * locking problems.
4238 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 * The returned value in "firstblock" from the first call in a transaction
4240 * must be remembered and presented to subsequent calls in "firstblock".
4241 * An upper bound for the number of blocks to be allocated is supplied to
4242 * the first call in "total"; if no allocation group has that many free
4243 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4244 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004245int
4246xfs_bmapi_write(
4247 struct xfs_trans *tp, /* transaction pointer */
4248 struct xfs_inode *ip, /* incore inode */
4249 xfs_fileoff_t bno, /* starting file offs. mapped */
4250 xfs_filblks_t len, /* length to map in file */
4251 int flags, /* XFS_BMAPI_... */
4252 xfs_fsblock_t *firstblock, /* first allocated block
4253 controls a.g. for allocs */
4254 xfs_extlen_t total, /* total blocks needed */
4255 struct xfs_bmbt_irec *mval, /* output: map values */
4256 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004257 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004259 struct xfs_mount *mp = ip->i_mount;
4260 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004261 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004262 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004263 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004264 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004265 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004266 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004267 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004268
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004270 xfs_fileoff_t orig_bno; /* original block number value */
4271 int orig_flags; /* original flags arg value */
4272 xfs_filblks_t orig_len; /* original value of len arg */
4273 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4274 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
4276 orig_bno = bno;
4277 orig_len = len;
4278 orig_flags = flags;
4279 orig_mval = mval;
4280 orig_nmap = *nmap;
4281#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004282 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004283
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004285 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4286 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004287 ASSERT(tp != NULL ||
4288 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4289 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004290 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004291 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004292 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004293 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004294
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004295 /* zeroing is for currently only for data extents, not metadata */
4296 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4297 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4298 /*
4299 * we can allocate unwritten extents or pre-zero allocated blocks,
4300 * but it makes no sense to do both at once. This would result in
4301 * zeroing the unwritten extent twice, but it still being an
4302 * unwritten extent....
4303 */
4304 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4305 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4306
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 if (unlikely(XFS_TEST_ERROR(
4308 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004309 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004310 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004311 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004312 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004314
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004316 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004317
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004319
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004320 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004321
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004322 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004324 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004326 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004327 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004328 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004329 }
4330
4331 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4332 error = xfs_iread_extents(tp, ip, whichfork);
4333 if (error)
4334 goto error0;
4335 }
4336
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 n = 0;
4338 end = bno + len;
4339 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004340
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004341 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004342 eof = true;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004343 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004344 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004345 bma.tp = tp;
4346 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004347 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004348 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004349 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004350 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004351
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004353 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004354
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004355 /* in hole or beyond EOF? */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004356 if (eof || bma.got.br_startoff > bno) {
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004357 /*
4358 * CoW fork conversions should /never/ hit EOF or
4359 * holes. There should always be something for us
4360 * to work on.
4361 */
4362 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4363 (flags & XFS_BMAPI_COWFORK)));
4364
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004365 if (flags & XFS_BMAPI_DELALLOC) {
4366 /*
4367 * For the COW fork we can reasonably get a
4368 * request for converting an extent that races
4369 * with other threads already having converted
4370 * part of it, as there converting COW to
4371 * regular blocks is not protected using the
4372 * IOLOCK.
4373 */
4374 ASSERT(flags & XFS_BMAPI_COWFORK);
4375 if (!(flags & XFS_BMAPI_COWFORK)) {
4376 error = -EIO;
4377 goto error0;
4378 }
4379
4380 if (eof || bno >= end)
4381 break;
4382 } else {
4383 need_alloc = true;
4384 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004385 } else if (isnullstartblock(bma.got.br_startblock)) {
4386 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004387 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004388
4389 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 * First, deal with the hole before the allocated space
4391 * that we found, if any.
4392 */
Christoph Hellwigb1214592017-11-03 10:34:44 -07004393 if ((need_alloc || wasdelay) &&
4394 !(flags & XFS_BMAPI_CONVERT_ONLY)) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004395 bma.eof = eof;
4396 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4397 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004398 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004399 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004400
Dave Chinnera99ebf42011-12-01 11:24:20 +00004401 /*
4402 * There's a 32/64 bit type mismatch between the
4403 * allocation length request (which can be 64 bits in
4404 * length) and the bma length request, which is
4405 * xfs_extlen_t and therefore 32 bits. Hence we have to
4406 * check for 32-bit overflows and handle them here.
4407 */
4408 if (len > (xfs_filblks_t)MAXEXTLEN)
4409 bma.length = MAXEXTLEN;
4410 else
4411 bma.length = len;
4412
4413 ASSERT(len > 0);
4414 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004415 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 if (error)
4417 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004418 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004419 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004420
4421 /*
4422 * If this is a CoW allocation, record the data in
4423 * the refcount btree for orphan recovery.
4424 */
4425 if (whichfork == XFS_COW_FORK) {
4426 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4427 bma.blkno, bma.length);
4428 if (error)
4429 goto error0;
4430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004432
Dave Chinneraef9a892011-09-18 20:40:44 +00004433 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004434 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4435 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
Dave Chinnerb447fe52011-09-18 20:40:51 +00004437 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004438 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004439 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004440 continue;
4441 if (error)
4442 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
Dave Chinneraef9a892011-09-18 20:40:44 +00004444 /* update the extent map to return */
4445 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4446
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 /*
4448 * If we're done, stop now. Stop when we've allocated
4449 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4450 * the transaction may get too big.
4451 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004452 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004454
4455 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004456 bma.prev = bma.got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004457 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004458 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004461
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 /*
4463 * Transform from btree to extents, give it cur.
4464 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004465 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004466 int tmp_logflags = 0;
4467
Dave Chinner29c8d172011-09-18 20:41:00 +00004468 ASSERT(bma.cur);
4469 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004471 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 if (error)
4473 goto error0;
4474 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004475
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004477 XFS_IFORK_NEXTENTS(ip, whichfork) >
4478 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480error0:
4481 /*
4482 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004483 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004485 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004487 bma.logflags &= ~xfs_ilog_fext(whichfork);
4488 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004490 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 /*
4492 * Log whatever the flags say, even if error. Otherwise we might miss
4493 * detecting a case where the data is changed, there's an error,
4494 * and it's not logged so we don't shutdown when we should.
4495 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004496 if (bma.logflags)
4497 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004498
Dave Chinner29c8d172011-09-18 20:41:00 +00004499 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 if (!error) {
4501 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004502 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004504 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004505 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004507 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4509 }
4510 if (!error)
4511 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4512 orig_nmap, *nmap);
4513 return error;
4514}
4515
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004516static int
4517xfs_bmapi_remap(
4518 struct xfs_trans *tp,
4519 struct xfs_inode *ip,
4520 xfs_fileoff_t bno,
4521 xfs_filblks_t len,
4522 xfs_fsblock_t startblock,
4523 struct xfs_defer_ops *dfops)
4524{
4525 struct xfs_mount *mp = ip->i_mount;
4526 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4527 struct xfs_btree_cur *cur = NULL;
4528 xfs_fsblock_t firstblock = NULLFSBLOCK;
4529 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004530 struct xfs_iext_cursor icur;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004531 int logflags = 0, error;
4532
4533 ASSERT(len > 0);
4534 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4535 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4536
4537 if (unlikely(XFS_TEST_ERROR(
4538 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4539 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004540 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004541 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4542 return -EFSCORRUPTED;
4543 }
4544
4545 if (XFS_FORCED_SHUTDOWN(mp))
4546 return -EIO;
4547
4548 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4549 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4550 if (error)
4551 return error;
4552 }
4553
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004554 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004555 /* make sure we only reflink into a hole. */
4556 ASSERT(got.br_startoff > bno);
4557 ASSERT(got.br_startoff - bno >= len);
4558 }
4559
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004560 ip->i_d.di_nblocks += len;
4561 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004562
4563 if (ifp->if_flags & XFS_IFBROOT) {
4564 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4565 cur->bc_private.b.firstblock = firstblock;
4566 cur->bc_private.b.dfops = dfops;
4567 cur->bc_private.b.flags = 0;
4568 }
4569
4570 got.br_startoff = bno;
4571 got.br_startblock = startblock;
4572 got.br_blockcount = len;
4573 got.br_state = XFS_EXT_NORM;
4574
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004575 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &icur,
4576 &cur, &got, &firstblock, dfops, &logflags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004577 if (error)
4578 goto error0;
4579
4580 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4581 int tmp_logflags = 0;
4582
4583 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4584 &tmp_logflags, XFS_DATA_FORK);
4585 logflags |= tmp_logflags;
4586 }
4587
4588error0:
4589 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4590 logflags &= ~XFS_ILOG_DEXT;
4591 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4592 logflags &= ~XFS_ILOG_DBROOT;
4593
4594 if (logflags)
4595 xfs_trans_log_inode(tp, ip, logflags);
4596 if (cur) {
4597 xfs_btree_del_cursor(cur,
4598 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4599 }
4600 return error;
4601}
4602
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004604 * When a delalloc extent is split (e.g., due to a hole punch), the original
4605 * indlen reservation must be shared across the two new extents that are left
4606 * behind.
4607 *
4608 * Given the original reservation and the worst case indlen for the two new
4609 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c92016-03-15 11:42:47 +11004610 * reservation fairly across the two new extents. If necessary, steal available
4611 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4612 * ores == 1). The number of stolen blocks is returned. The availability and
4613 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004614 */
Brian Fosterd34999c92016-03-15 11:42:47 +11004615static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004616xfs_bmap_split_indlen(
4617 xfs_filblks_t ores, /* original res. */
4618 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c92016-03-15 11:42:47 +11004619 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4620 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004621{
4622 xfs_filblks_t len1 = *indlen1;
4623 xfs_filblks_t len2 = *indlen2;
4624 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c92016-03-15 11:42:47 +11004625 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004626 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004627
4628 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11004629 * Steal as many blocks as we can to try and satisfy the worst case
4630 * indlen for both new extents.
4631 */
Brian Foster75d65362017-02-13 22:48:30 -08004632 if (ores < nres && avail)
4633 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4634 ores += stolen;
4635
4636 /* nothing else to do if we've satisfied the new reservation */
4637 if (ores >= nres)
4638 return stolen;
Brian Fosterd34999c92016-03-15 11:42:47 +11004639
4640 /*
Brian Foster75d65362017-02-13 22:48:30 -08004641 * We can't meet the total required reservation for the two extents.
4642 * Calculate the percent of the overall shortage between both extents
4643 * and apply this percentage to each of the requested indlen values.
4644 * This distributes the shortage fairly and reduces the chances that one
4645 * of the two extents is left with nothing when extents are repeatedly
4646 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004647 */
Brian Foster75d65362017-02-13 22:48:30 -08004648 resfactor = (ores * 100);
4649 do_div(resfactor, nres);
4650 len1 *= resfactor;
4651 do_div(len1, 100);
4652 len2 *= resfactor;
4653 do_div(len2, 100);
4654 ASSERT(len1 + len2 <= ores);
4655 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4656
4657 /*
4658 * Hand out the remainder to each extent. If one of the two reservations
4659 * is zero, we want to make sure that one gets a block first. The loop
4660 * below starts with len1, so hand len2 a block right off the bat if it
4661 * is zero.
4662 */
4663 ores -= (len1 + len2);
4664 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4665 if (ores && !len2 && *indlen2) {
4666 len2++;
4667 ores--;
4668 }
4669 while (ores) {
4670 if (len1 < *indlen1) {
4671 len1++;
4672 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004673 }
Brian Foster75d65362017-02-13 22:48:30 -08004674 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004675 break;
Brian Foster75d65362017-02-13 22:48:30 -08004676 if (len2 < *indlen2) {
4677 len2++;
4678 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004679 }
4680 }
4681
4682 *indlen1 = len1;
4683 *indlen2 = len2;
Brian Fosterd34999c92016-03-15 11:42:47 +11004684
4685 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004686}
4687
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004688int
4689xfs_bmap_del_extent_delay(
4690 struct xfs_inode *ip,
4691 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004692 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004693 struct xfs_bmbt_irec *got,
4694 struct xfs_bmbt_irec *del)
4695{
4696 struct xfs_mount *mp = ip->i_mount;
4697 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4698 struct xfs_bmbt_irec new;
4699 int64_t da_old, da_new, da_diff = 0;
4700 xfs_fileoff_t del_endoff, got_endoff;
4701 xfs_filblks_t got_indlen, new_indlen, stolen;
Christoph Hellwig060ea652017-10-19 11:02:29 -07004702 int state = xfs_bmap_fork_to_state(whichfork);
4703 int error = 0;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004704 bool isrt;
4705
4706 XFS_STATS_INC(mp, xs_del_exlist);
4707
4708 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4709 del_endoff = del->br_startoff + del->br_blockcount;
4710 got_endoff = got->br_startoff + got->br_blockcount;
4711 da_old = startblockval(got->br_startblock);
4712 da_new = 0;
4713
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004714 ASSERT(del->br_blockcount > 0);
4715 ASSERT(got->br_startoff <= del->br_startoff);
4716 ASSERT(got_endoff >= del_endoff);
4717
4718 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004719 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004720
4721 do_div(rtexts, mp->m_sb.sb_rextsize);
4722 xfs_mod_frextents(mp, rtexts);
4723 }
4724
4725 /*
4726 * Update the inode delalloc counter now and wait to update the
4727 * sb counters as we might have to borrow some blocks for the
4728 * indirect block accounting.
4729 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004730 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4731 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004732 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004733 if (error)
4734 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004735 ip->i_delayed_blks -= del->br_blockcount;
4736
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004737 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004738 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004739 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004740 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004741
Christoph Hellwig0173c682017-10-17 14:16:22 -07004742 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4743 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004744 /*
4745 * Matches the whole extent. Delete the entry.
4746 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004747 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004748 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004749 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004750 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004751 /*
4752 * Deleting the first part of the extent.
4753 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004754 got->br_startoff = del_endoff;
4755 got->br_blockcount -= del->br_blockcount;
4756 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4757 got->br_blockcount), da_old);
4758 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004759 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004760 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004761 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004762 /*
4763 * Deleting the last part of the extent.
4764 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004765 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4766 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4767 got->br_blockcount), da_old);
4768 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004769 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004770 break;
4771 case 0:
4772 /*
4773 * Deleting the middle of the extent.
4774 *
4775 * Distribute the original indlen reservation across the two new
4776 * extents. Steal blocks from the deleted extent if necessary.
4777 * Stealing blocks simply fudges the fdblocks accounting below.
4778 * Warn if either of the new indlen reservations is zero as this
4779 * can lead to delalloc problems.
4780 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004781 got->br_blockcount = del->br_startoff - got->br_startoff;
4782 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4783
4784 new.br_blockcount = got_endoff - del_endoff;
4785 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4786
4787 WARN_ON_ONCE(!got_indlen || !new_indlen);
4788 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4789 del->br_blockcount);
4790
4791 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004792
4793 new.br_startoff = del_endoff;
4794 new.br_state = got->br_state;
4795 new.br_startblock = nullstartblock((int)new_indlen);
4796
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004797 xfs_iext_update_extent(ip, state, icur, got);
4798 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004799 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004800
4801 da_new = got_indlen + new_indlen - stolen;
4802 del->br_blockcount -= stolen;
4803 break;
4804 }
4805
4806 ASSERT(da_old >= da_new);
4807 da_diff = da_old - da_new;
4808 if (!isrt)
4809 da_diff += del->br_blockcount;
4810 if (da_diff)
4811 xfs_mod_fdblocks(mp, da_diff, false);
4812 return error;
4813}
4814
4815void
4816xfs_bmap_del_extent_cow(
4817 struct xfs_inode *ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004818 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004819 struct xfs_bmbt_irec *got,
4820 struct xfs_bmbt_irec *del)
4821{
4822 struct xfs_mount *mp = ip->i_mount;
4823 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4824 struct xfs_bmbt_irec new;
4825 xfs_fileoff_t del_endoff, got_endoff;
4826 int state = BMAP_COWFORK;
4827
4828 XFS_STATS_INC(mp, xs_del_exlist);
4829
4830 del_endoff = del->br_startoff + del->br_blockcount;
4831 got_endoff = got->br_startoff + got->br_blockcount;
4832
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004833 ASSERT(del->br_blockcount > 0);
4834 ASSERT(got->br_startoff <= del->br_startoff);
4835 ASSERT(got_endoff >= del_endoff);
4836 ASSERT(!isnullstartblock(got->br_startblock));
4837
4838 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004839 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004840 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004841 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004842
Christoph Hellwig0173c682017-10-17 14:16:22 -07004843 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4844 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004845 /*
4846 * Matches the whole extent. Delete the entry.
4847 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004848 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004849 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004850 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004851 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004852 /*
4853 * Deleting the first part of the extent.
4854 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004855 got->br_startoff = del_endoff;
4856 got->br_blockcount -= del->br_blockcount;
4857 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004858 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004859 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004860 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004861 /*
4862 * Deleting the last part of the extent.
4863 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004864 got->br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004865 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004866 break;
4867 case 0:
4868 /*
4869 * Deleting the middle of the extent.
4870 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004871 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004872
4873 new.br_startoff = del_endoff;
4874 new.br_blockcount = got_endoff - del_endoff;
4875 new.br_state = got->br_state;
4876 new.br_startblock = del->br_startblock + del->br_blockcount;
4877
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004878 xfs_iext_update_extent(ip, state, icur, got);
4879 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004880 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004881 break;
4882 }
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08004883 ip->i_delayed_blks -= del->br_blockcount;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004884}
4885
Brian Fostera9bd24a2016-03-15 11:42:46 +11004886/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11004887 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07004888 * after removing space.
Dave Chinner9e5987a2013-02-25 12:31:26 +11004889 */
4890STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07004891xfs_bmap_del_extent_real(
Dave Chinner9e5987a2013-02-25 12:31:26 +11004892 xfs_inode_t *ip, /* incore inode pointer */
4893 xfs_trans_t *tp, /* current transaction pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004894 struct xfs_iext_cursor *icur,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004895 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004896 xfs_btree_cur_t *cur, /* if null, not a btree */
4897 xfs_bmbt_irec_t *del, /* data to remove from extents */
4898 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07004899 int whichfork, /* data or attr fork */
4900 int bflags) /* bmapi flags */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004901{
Dave Chinner9e5987a2013-02-25 12:31:26 +11004902 xfs_fsblock_t del_endblock=0; /* first block past del */
4903 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004904 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004905 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004906 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004907 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004908 xfs_fileoff_t got_endoff; /* first offset past got */
4909 int i; /* temp state */
4910 xfs_ifork_t *ifp; /* inode fork pointer */
4911 xfs_mount_t *mp; /* mount structure */
4912 xfs_filblks_t nblks; /* quota/sb block count */
4913 xfs_bmbt_irec_t new; /* new record to be inserted */
4914 /* REFERENCED */
4915 uint qfield; /* quota field to update */
Christoph Hellwig060ea652017-10-19 11:02:29 -07004916 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004917 struct xfs_bmbt_irec old;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004918
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004919 mp = ip->i_mount;
4920 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004921
Dave Chinner9e5987a2013-02-25 12:31:26 +11004922 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004923 ASSERT(del->br_blockcount > 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004924 xfs_iext_get_extent(ifp, icur, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004925 ASSERT(got.br_startoff <= del->br_startoff);
4926 del_endoff = del->br_startoff + del->br_blockcount;
4927 got_endoff = got.br_startoff + got.br_blockcount;
4928 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004929 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +11004930 qfield = 0;
4931 error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004932
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004933 /*
4934 * If it's the case where the directory code is running with no block
4935 * reservation, and the deleted block is in the middle of its extent,
4936 * and the resulting insert of an extent would cause transformation to
4937 * btree format, then reject it. The calling code will then swap blocks
4938 * around instead. We have to do this now, rather than waiting for the
4939 * conversion to btree format, since the transaction will be dirty then.
4940 */
4941 if (tp->t_blk_res == 0 &&
4942 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4943 XFS_IFORK_NEXTENTS(ip, whichfork) >=
4944 XFS_IFORK_MAXEXT(ip, whichfork) &&
4945 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
4946 return -ENOSPC;
4947
4948 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004949 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4950 xfs_fsblock_t bno;
4951 xfs_filblks_t len;
4952
4953 ASSERT(do_mod(del->br_blockcount, mp->m_sb.sb_rextsize) == 0);
4954 ASSERT(do_mod(del->br_startblock, mp->m_sb.sb_rextsize) == 0);
4955 bno = del->br_startblock;
4956 len = del->br_blockcount;
4957 do_div(bno, mp->m_sb.sb_rextsize);
4958 do_div(len, mp->m_sb.sb_rextsize);
4959 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4960 if (error)
4961 goto done;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004962 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004963 nblks = len * mp->m_sb.sb_rextsize;
4964 qfield = XFS_TRANS_DQ_RTBCOUNT;
4965 } else {
4966 do_fx = 1;
4967 nblks = del->br_blockcount;
4968 qfield = XFS_TRANS_DQ_BCOUNT;
4969 }
4970
4971 del_endblock = del->br_startblock + del->br_blockcount;
4972 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07004973 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004974 if (error)
4975 goto done;
4976 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004977 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10004978
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004979 if (got.br_startoff == del->br_startoff)
4980 state |= BMAP_LEFT_FILLING;
4981 if (got_endoff == del_endoff)
4982 state |= BMAP_RIGHT_FILLING;
4983
4984 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4985 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11004986 /*
4987 * Matches the whole extent. Delete the entry.
4988 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004989 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004990 xfs_iext_prev(ifp, icur);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004991 XFS_IFORK_NEXT_SET(ip, whichfork,
4992 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4993 flags |= XFS_ILOG_CORE;
4994 if (!cur) {
4995 flags |= xfs_ilog_fext(whichfork);
4996 break;
4997 }
4998 if ((error = xfs_btree_delete(cur, &i)))
4999 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005000 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005001 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005002 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11005003 /*
5004 * Deleting the first part of the extent.
5005 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005006 got.br_startoff = del_endoff;
5007 got.br_startblock = del_endblock;
5008 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005009 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005010 if (!cur) {
5011 flags |= xfs_ilog_fext(whichfork);
5012 break;
5013 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005014 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005015 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005016 goto done;
5017 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005018 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11005019 /*
5020 * Deleting the last part of the extent.
5021 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005022 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005023 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005024 if (!cur) {
5025 flags |= xfs_ilog_fext(whichfork);
5026 break;
5027 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005028 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005029 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005030 goto done;
5031 break;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005032 case 0:
5033 /*
5034 * Deleting the middle of the extent.
5035 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005036 old = got;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07005037
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005038 got.br_blockcount = del->br_startoff - got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005039 xfs_iext_update_extent(ip, state, icur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005040
Dave Chinner9e5987a2013-02-25 12:31:26 +11005041 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005042 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005043 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005044 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005045
Christoph Hellwige1d75532017-10-17 14:16:21 -07005046 flags |= XFS_ILOG_CORE;
5047 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005048 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005049 if (error)
5050 goto done;
5051 error = xfs_btree_increment(cur, 0, &i);
5052 if (error)
5053 goto done;
5054 cur->bc_rec.b = new;
5055 error = xfs_btree_insert(cur, &i);
5056 if (error && error != -ENOSPC)
5057 goto done;
5058 /*
5059 * If get no-space back from btree insert, it tried a
5060 * split, and we have a zero block reservation. Fix up
5061 * our state and return the error.
5062 */
5063 if (error == -ENOSPC) {
5064 /*
5065 * Reset the cursor, don't trust it after any
5066 * insert operation.
5067 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005068 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005069 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005070 goto done;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005071 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5072 /*
5073 * Update the btree record back
5074 * to the original value.
5075 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005076 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005077 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005078 goto done;
5079 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005080 * Reset the extent record back
5081 * to the original value.
Dave Chinner9e5987a2013-02-25 12:31:26 +11005082 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005083 xfs_iext_update_extent(ip, state, icur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005084 flags = 0;
5085 error = -ENOSPC;
5086 goto done;
5087 }
5088 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5089 } else
5090 flags |= xfs_ilog_fext(whichfork);
5091 XFS_IFORK_NEXT_SET(ip, whichfork,
5092 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005093 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005094 xfs_iext_insert(ip, icur, &new, state);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005095 break;
5096 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005097
5098 /* remove reverse mapping */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005099 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5100 if (error)
5101 goto done;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005102
Dave Chinner9e5987a2013-02-25 12:31:26 +11005103 /*
5104 * If we need to, add to list of extents to delete.
5105 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005106 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005107 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5108 error = xfs_refcount_decrease_extent(mp, dfops, del);
5109 if (error)
5110 goto done;
Brian Fosterfcb762f2018-05-09 08:45:04 -07005111 } else {
5112 if (bflags & XFS_BMAPI_NODISCARD) {
5113 xfs_bmap_add_free_nodiscard(mp, dfops,
5114 del->br_startblock, del->br_blockcount,
5115 NULL);
5116 } else {
5117 xfs_bmap_add_free(mp, dfops, del->br_startblock,
Darrick J. Wong62aab202016-10-03 09:11:23 -07005118 del->br_blockcount, NULL);
Brian Fosterfcb762f2018-05-09 08:45:04 -07005119 }
5120 }
Darrick J. Wong62aab202016-10-03 09:11:23 -07005121 }
5122
Dave Chinner9e5987a2013-02-25 12:31:26 +11005123 /*
5124 * Adjust inode # blocks in the file.
5125 */
5126 if (nblks)
5127 ip->i_d.di_nblocks -= nblks;
5128 /*
5129 * Adjust quota data.
5130 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005131 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a2013-02-25 12:31:26 +11005132 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5133
Dave Chinner9e5987a2013-02-25 12:31:26 +11005134done:
5135 *logflagsp = flags;
5136 return error;
5137}
5138
5139/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 * Unmap (remove) blocks from a file.
5141 * If nexts is nonzero then the number of extents to remove is limited to
5142 * that value. If not all extents in the block range can be removed then
5143 * *done is set.
5144 */
5145int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005146__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 xfs_trans_t *tp, /* transaction pointer */
5148 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005149 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005150 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 int flags, /* misc flags */
5152 xfs_extnum_t nexts, /* number of extents max */
5153 xfs_fsblock_t *firstblock, /* first allocated block
5154 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005155 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156{
5157 xfs_btree_cur_t *cur; /* bmap btree cursor */
5158 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 int error; /* error return value */
5160 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005161 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 xfs_ifork_t *ifp; /* inode fork pointer */
5163 int isrt; /* freeing in rt area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 int logflags; /* transaction logging flags */
5165 xfs_extlen_t mod; /* rt extent offset */
5166 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 int tmp_logflags; /* partial logging flags */
5168 int wasdel; /* was a delayed alloc extent */
5169 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005171 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005172 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005173 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005174 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005175 struct xfs_iext_cursor icur;
5176 bool done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005178 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005179
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005180 whichfork = xfs_bmapi_whichfork(flags);
5181 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 ifp = XFS_IFORK_PTR(ip, whichfork);
5183 if (unlikely(
5184 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5185 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5186 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5187 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005188 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 }
5190 mp = ip->i_mount;
5191 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005192 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005193
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005194 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 ASSERT(len > 0);
5196 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005197
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005198 /*
5199 * Guesstimate how many blocks we can unmap without running the risk of
5200 * blowing out the transaction with a mix of EFIs and reflink
5201 * adjustments.
5202 */
Darrick J. Wong8c57b882017-12-10 18:03:53 -08005203 if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005204 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5205 else
5206 max_len = len;
5207
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5209 (error = xfs_iread_extents(tp, ip, whichfork)))
5210 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005211 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005212 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 return 0;
5214 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005215 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005216 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwigdc560152017-10-23 16:32:39 -07005217 end = start + len;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005218
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005219 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
Christoph Hellwigdc560152017-10-23 16:32:39 -07005220 *rlen = 0;
5221 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 }
Christoph Hellwigdc560152017-10-23 16:32:39 -07005223 end--;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005224
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 logflags = 0;
5226 if (ifp->if_flags & XFS_IFBROOT) {
5227 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005228 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005230 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 cur->bc_private.b.flags = 0;
5232 } else
5233 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005234
5235 if (isrt) {
5236 /*
5237 * Synchronize by locking the bitmap inode.
5238 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005239 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005240 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005241 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5242 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005243 }
5244
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 extno = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005246 while (end != (xfs_fileoff_t)-1 && end >= start &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005247 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005249 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 * Just back up to the previous extent, if so.
5251 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005252 if (got.br_startoff > end &&
5253 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5254 done = true;
5255 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 }
5257 /*
5258 * Is the last block of this extent before the range
5259 * we're supposed to delete? If so, we're done.
5260 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005261 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005263 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 break;
5265 /*
5266 * Then deal with the (possibly delayed) allocated space
5267 * we found.
5268 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005270 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005271
5272 /*
5273 * Make sure we don't touch multiple AGF headers out of order
5274 * in a single transaction, as that could cause AB-BA deadlocks.
5275 */
5276 if (!wasdel) {
5277 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5278 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5279 break;
5280 prev_agno = agno;
5281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 if (got.br_startoff < start) {
5283 del.br_startoff = start;
5284 del.br_blockcount -= start - got.br_startoff;
5285 if (!wasdel)
5286 del.br_startblock += start - got.br_startoff;
5287 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005288 if (del.br_startoff + del.br_blockcount > end + 1)
5289 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005290
5291 /* How much can we safely unmap? */
5292 if (max_len < del.br_blockcount) {
5293 del.br_startoff += del.br_blockcount - max_len;
5294 if (!wasdel)
5295 del.br_startblock += del.br_blockcount - max_len;
5296 del.br_blockcount = max_len;
5297 }
5298
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 sum = del.br_startblock + del.br_blockcount;
5300 if (isrt &&
5301 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5302 /*
5303 * Realtime extent not lined up at the end.
5304 * The extent could have been split into written
5305 * and unwritten pieces, or we could just be
5306 * unmapping part of it. But we can't really
5307 * get rid of part of a realtime extent.
5308 */
5309 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005310 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 /*
5312 * This piece is unwritten, or we're not
5313 * using unwritten extents. Skip over it.
5314 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005315 ASSERT(end >= mod);
5316 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 del.br_blockcount : mod;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005318 if (end < got.br_startoff &&
5319 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5320 done = true;
5321 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 }
5323 continue;
5324 }
5325 /*
5326 * It's written, turn it unwritten.
5327 * This is better than zeroing it.
5328 */
5329 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005330 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 /*
5332 * If this spans a realtime extent boundary,
5333 * chop it back to the start of the one we end at.
5334 */
5335 if (del.br_blockcount > mod) {
5336 del.br_startoff += del.br_blockcount - mod;
5337 del.br_startblock += del.br_blockcount - mod;
5338 del.br_blockcount = mod;
5339 }
5340 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005341 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005342 whichfork, &icur, &cur, &del,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005343 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 if (error)
5345 goto error0;
5346 goto nodelete;
5347 }
5348 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5349 /*
5350 * Realtime extent is lined up at the end but not
5351 * at the front. We'll get rid of full extents if
5352 * we can.
5353 */
5354 mod = mp->m_sb.sb_rextsize - mod;
5355 if (del.br_blockcount > mod) {
5356 del.br_blockcount -= mod;
5357 del.br_startoff += mod;
5358 del.br_startblock += mod;
5359 } else if ((del.br_startoff == start &&
5360 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005361 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005362 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 /*
5364 * Can't make it unwritten. There isn't
5365 * a full extent here so just skip it.
5366 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005367 ASSERT(end >= del.br_blockcount);
5368 end -= del.br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005369 if (got.br_startoff > end &&
5370 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5371 done = true;
5372 break;
5373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 continue;
5375 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005376 struct xfs_bmbt_irec prev;
5377
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 /*
5379 * This one is already unwritten.
5380 * It must have a written left neighbor.
5381 * Unwrite the killed part of that one and
5382 * try again.
5383 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005384 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5385 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005387 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 ASSERT(del.br_startblock ==
5389 prev.br_startblock + prev.br_blockcount);
5390 if (prev.br_startoff < start) {
5391 mod = start - prev.br_startoff;
5392 prev.br_blockcount -= mod;
5393 prev.br_startblock += mod;
5394 prev.br_startoff = start;
5395 }
5396 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005397 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005398 ip, whichfork, &icur, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005399 &prev, firstblock, dfops,
5400 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 if (error)
5402 goto error0;
5403 goto nodelete;
5404 } else {
5405 ASSERT(del.br_state == XFS_EXT_NORM);
5406 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005407 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005408 ip, whichfork, &icur, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005409 &del, firstblock, dfops,
5410 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 if (error)
5412 goto error0;
5413 goto nodelete;
5414 }
5415 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005416
Brian Fosterb2706a02016-03-15 11:42:46 +11005417 if (wasdel) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005418 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005419 &got, &del);
5420 } else {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005421 error = xfs_bmap_del_extent_real(ip, tp, &icur, dfops,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005422 cur, &del, &tmp_logflags, whichfork,
5423 flags);
5424 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005425 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005426
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 if (error)
5428 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005429
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005430 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005431 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 /*
5434 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005436 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005437 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5438 (got.br_startoff > end &&
5439 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5440 done = true;
5441 break;
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 extno++;
5444 }
5445 }
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005446 if (done || end == (xfs_fileoff_t)-1 || end < start)
Darrick J. Wong44535932016-10-03 09:11:29 -07005447 *rlen = 0;
5448 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005449 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005450
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 /*
5452 * Convert to a btree if necessary.
5453 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005454 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005456 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 &cur, 0, &tmp_logflags, whichfork);
5458 logflags |= tmp_logflags;
5459 if (error)
5460 goto error0;
5461 }
5462 /*
5463 * transform from btree to extents, give it cur
5464 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005465 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 ASSERT(cur != NULL);
5467 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5468 whichfork);
5469 logflags |= tmp_logflags;
5470 if (error)
5471 goto error0;
5472 }
5473 /*
5474 * transform from extents to local?
5475 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 error = 0;
5477error0:
5478 /*
5479 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005480 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005482 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005484 logflags &= ~xfs_ilog_fext(whichfork);
5485 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005487 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 /*
5489 * Log inode even in the error case, if the transaction
5490 * is dirty we'll need to shut down the filesystem.
5491 */
5492 if (logflags)
5493 xfs_trans_log_inode(tp, ip, logflags);
5494 if (cur) {
5495 if (!error) {
5496 *firstblock = cur->bc_private.b.firstblock;
5497 cur->bc_private.b.allocated = 0;
5498 }
5499 xfs_btree_del_cursor(cur,
5500 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5501 }
5502 return error;
5503}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005504
Darrick J. Wong44535932016-10-03 09:11:29 -07005505/* Unmap a range of a file. */
5506int
5507xfs_bunmapi(
5508 xfs_trans_t *tp,
5509 struct xfs_inode *ip,
5510 xfs_fileoff_t bno,
5511 xfs_filblks_t len,
5512 int flags,
5513 xfs_extnum_t nexts,
5514 xfs_fsblock_t *firstblock,
5515 struct xfs_defer_ops *dfops,
5516 int *done)
5517{
5518 int error;
5519
5520 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5521 dfops);
5522 *done = (len == 0);
5523 return error;
5524}
5525
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005526/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005527 * Determine whether an extent shift can be accomplished by a merge with the
5528 * extent that precedes the target hole of the shift.
5529 */
5530STATIC bool
5531xfs_bmse_can_merge(
5532 struct xfs_bmbt_irec *left, /* preceding extent */
5533 struct xfs_bmbt_irec *got, /* current extent to shift */
5534 xfs_fileoff_t shift) /* shift fsb */
5535{
5536 xfs_fileoff_t startoff;
5537
5538 startoff = got->br_startoff - shift;
5539
5540 /*
5541 * The extent, once shifted, must be adjacent in-file and on-disk with
5542 * the preceding extent.
5543 */
5544 if ((left->br_startoff + left->br_blockcount != startoff) ||
5545 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5546 (left->br_state != got->br_state) ||
5547 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5548 return false;
5549
5550 return true;
5551}
5552
5553/*
5554 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5555 * hole in the file. If an extent shift would result in the extent being fully
5556 * adjacent to the extent that currently precedes the hole, we can merge with
5557 * the preceding extent rather than do the shift.
5558 *
5559 * This function assumes the caller has verified a shift-by-merge is possible
5560 * with the provided extents via xfs_bmse_can_merge().
5561 */
5562STATIC int
5563xfs_bmse_merge(
5564 struct xfs_inode *ip,
5565 int whichfork,
5566 xfs_fileoff_t shift, /* shift fsb */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005567 struct xfs_iext_cursor *icur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005568 struct xfs_bmbt_irec *got, /* extent to shift */
5569 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005570 struct xfs_btree_cur *cur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005571 int *logflags, /* output */
5572 struct xfs_defer_ops *dfops)
Brian Fosterddb19e32014-09-23 15:38:09 +10005573{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005574 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005575 xfs_filblks_t blockcount;
5576 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005577 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005578
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005579 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005580
5581 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5582 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005583 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005584
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005585 new = *left;
5586 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005587
5588 /*
5589 * Update the on-disk extent count, the btree if necessary and log the
5590 * inode.
5591 */
5592 XFS_IFORK_NEXT_SET(ip, whichfork,
5593 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5594 *logflags |= XFS_ILOG_CORE;
5595 if (!cur) {
5596 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005597 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005598 }
5599
5600 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005601 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005602 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005603 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005604 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005605
5606 error = xfs_btree_delete(cur, &i);
5607 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005608 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005609 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005610
5611 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005612 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005613 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005614 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005615 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005616
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005617 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005618 if (error)
5619 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005620
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005621done:
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005622 xfs_iext_remove(ip, icur, 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005623 xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur);
5624 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5625 &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005626
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005627 /* update reverse mapping. rmap functions merge the rmaps for us */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005628 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
5629 if (error)
5630 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005631 memcpy(&new, got, sizeof(new));
5632 new.br_startoff = left->br_startoff + left->br_blockcount;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005633 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005634}
5635
Christoph Hellwigbf806282017-10-19 11:07:34 -07005636static int
5637xfs_bmap_shift_update_extent(
5638 struct xfs_inode *ip,
5639 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005640 struct xfs_iext_cursor *icur,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005641 struct xfs_bmbt_irec *got,
5642 struct xfs_btree_cur *cur,
5643 int *logflags,
5644 struct xfs_defer_ops *dfops,
5645 xfs_fileoff_t startoff)
Brian Fostera979bdf2014-09-23 15:39:04 +10005646{
Christoph Hellwigbf806282017-10-19 11:07:34 -07005647 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005648 struct xfs_bmbt_irec prev = *got;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005649 int error, i;
Brian Fostera979bdf2014-09-23 15:39:04 +10005650
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005651 *logflags |= XFS_ILOG_CORE;
5652
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005653 got->br_startoff = startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005654
5655 if (cur) {
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005656 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005657 if (error)
5658 return error;
5659 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5660
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005661 error = xfs_bmbt_update(cur, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005662 if (error)
5663 return error;
5664 } else {
5665 *logflags |= XFS_ILOG_DEXT;
5666 }
5667
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005668 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5669 got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005670
Darrick J. Wong9c194642016-08-03 12:16:05 +10005671 /* update reverse mapping */
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005672 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &prev);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005673 if (error)
5674 return error;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005675 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005676}
5677
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005678int
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005679xfs_bmap_collapse_extents(
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005680 struct xfs_trans *tp,
5681 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005682 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005683 xfs_fileoff_t offset_shift_fsb,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005684 bool *done,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005685 xfs_fsblock_t *firstblock,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005686 struct xfs_defer_ops *dfops)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005687{
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005688 int whichfork = XFS_DATA_FORK;
5689 struct xfs_mount *mp = ip->i_mount;
5690 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5691 struct xfs_btree_cur *cur = NULL;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005692 struct xfs_bmbt_irec got, prev;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005693 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005694 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005695 int error = 0;
5696 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005697
5698 if (unlikely(XFS_TEST_ERROR(
5699 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5700 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005701 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005702 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005703 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005704 }
5705
5706 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005707 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005708
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005709 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005710
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005711 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005712 error = xfs_iread_extents(tp, ip, whichfork);
5713 if (error)
5714 return error;
5715 }
5716
Brian Fosterddb19e32014-09-23 15:38:09 +10005717 if (ifp->if_flags & XFS_IFBROOT) {
5718 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5719 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005720 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10005721 cur->bc_private.b.flags = 0;
5722 }
5723
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005724 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005725 *done = true;
5726 goto del_cursor;
5727 }
Eric Sandeend41c6172017-11-27 18:23:32 -08005728 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5729 del_cursor);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005730
Christoph Hellwigbf806282017-10-19 11:07:34 -07005731 new_startoff = got.br_startoff - offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005732 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005733 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5734 error = -EINVAL;
5735 goto del_cursor;
5736 }
5737
Christoph Hellwigbf806282017-10-19 11:07:34 -07005738 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5739 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005740 &icur, &got, &prev, cur, &logflags,
5741 dfops);
Christoph Hellwigbf806282017-10-19 11:07:34 -07005742 if (error)
5743 goto del_cursor;
5744 goto done;
5745 }
5746 } else {
5747 if (got.br_startoff < offset_shift_fsb) {
5748 error = -EINVAL;
5749 goto del_cursor;
5750 }
5751 }
5752
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005753 error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur,
5754 &logflags, dfops, new_startoff);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005755 if (error)
5756 goto del_cursor;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005757
Christoph Hellwig42630362017-11-03 10:34:41 -07005758done:
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005759 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5760 *done = true;
5761 goto del_cursor;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005762 }
5763
Christoph Hellwigbf806282017-10-19 11:07:34 -07005764 *next_fsb = got.br_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005765del_cursor:
5766 if (cur)
5767 xfs_btree_del_cursor(cur,
5768 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005769 if (logflags)
5770 xfs_trans_log_inode(tp, ip, logflags);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005771 return error;
5772}
5773
5774int
5775xfs_bmap_insert_extents(
5776 struct xfs_trans *tp,
5777 struct xfs_inode *ip,
5778 xfs_fileoff_t *next_fsb,
5779 xfs_fileoff_t offset_shift_fsb,
5780 bool *done,
5781 xfs_fileoff_t stop_fsb,
5782 xfs_fsblock_t *firstblock,
5783 struct xfs_defer_ops *dfops)
5784{
5785 int whichfork = XFS_DATA_FORK;
5786 struct xfs_mount *mp = ip->i_mount;
5787 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5788 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005789 struct xfs_bmbt_irec got, next;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005790 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005791 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005792 int error = 0;
5793 int logflags = 0;
5794
5795 if (unlikely(XFS_TEST_ERROR(
5796 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5797 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5798 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5799 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
5800 return -EFSCORRUPTED;
5801 }
5802
5803 if (XFS_FORCED_SHUTDOWN(mp))
5804 return -EIO;
5805
5806 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5807
5808 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5809 error = xfs_iread_extents(tp, ip, whichfork);
5810 if (error)
5811 return error;
5812 }
5813
5814 if (ifp->if_flags & XFS_IFBROOT) {
5815 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5816 cur->bc_private.b.firstblock = *firstblock;
5817 cur->bc_private.b.dfops = dfops;
5818 cur->bc_private.b.flags = 0;
5819 }
5820
Namjae Jeona904b1c2015-03-25 15:08:56 +11005821 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005822 xfs_iext_last(ifp, &icur);
5823 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005824 stop_fsb > got.br_startoff) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005825 *done = true;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005826 goto del_cursor;
5827 }
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005828 } else {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005829 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005830 *done = true;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005831 goto del_cursor;
5832 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005833 }
Eric Sandeend41c6172017-11-27 18:23:32 -08005834 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5835 del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005836
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005837 if (stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005838 error = -EIO;
5839 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005840 }
5841
Christoph Hellwigbf806282017-10-19 11:07:34 -07005842 new_startoff = got.br_startoff + offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005843 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005844 if (new_startoff + got.br_blockcount > next.br_startoff) {
5845 error = -EINVAL;
5846 goto del_cursor;
5847 }
5848
5849 /*
5850 * Unlike a left shift (which involves a hole punch), a right
5851 * shift does not modify extent neighbors in any way. We should
5852 * never find mergeable extents in this scenario. Check anyways
5853 * and warn if we encounter two extents that could be one.
5854 */
5855 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5856 WARN_ON_ONCE(1);
5857 }
5858
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005859 error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur,
5860 &logflags, dfops, new_startoff);
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005861 if (error)
5862 goto del_cursor;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005863
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005864 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005865 stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005866 *done = true;
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005867 goto del_cursor;
5868 }
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005869
5870 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005871del_cursor:
5872 if (cur)
5873 xfs_btree_del_cursor(cur,
5874 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Brian Fosterca446d82014-09-02 12:12:53 +10005875 if (logflags)
5876 xfs_trans_log_inode(tp, ip, logflags);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005877 return error;
5878}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005879
5880/*
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005881 * Splits an extent into two extents at split_fsb block such that it is the
5882 * first block of the current_ext. @ext is a target extent to be split.
5883 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5884 * hole or the first block of extents, just return 0.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005885 */
5886STATIC int
5887xfs_bmap_split_extent_at(
5888 struct xfs_trans *tp,
5889 struct xfs_inode *ip,
5890 xfs_fileoff_t split_fsb,
5891 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005892 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005893{
5894 int whichfork = XFS_DATA_FORK;
5895 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005896 struct xfs_bmbt_irec got;
5897 struct xfs_bmbt_irec new; /* split extent */
5898 struct xfs_mount *mp = ip->i_mount;
5899 struct xfs_ifork *ifp;
5900 xfs_fsblock_t gotblkcnt; /* new block count for got */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005901 struct xfs_iext_cursor icur;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005902 int error = 0;
5903 int logflags = 0;
5904 int i = 0;
5905
5906 if (unlikely(XFS_TEST_ERROR(
5907 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5908 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005909 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11005910 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5911 XFS_ERRLEVEL_LOW, mp);
5912 return -EFSCORRUPTED;
5913 }
5914
5915 if (XFS_FORCED_SHUTDOWN(mp))
5916 return -EIO;
5917
5918 ifp = XFS_IFORK_PTR(ip, whichfork);
5919 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5920 /* Read in all the extents */
5921 error = xfs_iread_extents(tp, ip, whichfork);
5922 if (error)
5923 return error;
5924 }
5925
5926 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005927 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005928 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005929 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005930 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005931 return 0;
5932
5933 gotblkcnt = split_fsb - got.br_startoff;
5934 new.br_startoff = split_fsb;
5935 new.br_startblock = got.br_startblock + gotblkcnt;
5936 new.br_blockcount = got.br_blockcount - gotblkcnt;
5937 new.br_state = got.br_state;
5938
5939 if (ifp->if_flags & XFS_IFBROOT) {
5940 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5941 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005942 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005943 cur->bc_private.b.flags = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005944 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005945 if (error)
5946 goto del_cursor;
5947 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5948 }
5949
Namjae Jeona904b1c2015-03-25 15:08:56 +11005950 got.br_blockcount = gotblkcnt;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005951 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
5952 &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005953
5954 logflags = XFS_ILOG_CORE;
5955 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005956 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005957 if (error)
5958 goto del_cursor;
5959 } else
5960 logflags |= XFS_ILOG_DEXT;
5961
5962 /* Add new extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005963 xfs_iext_next(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005964 xfs_iext_insert(ip, &icur, &new, 0);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005965 XFS_IFORK_NEXT_SET(ip, whichfork,
5966 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5967
5968 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005969 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005970 if (error)
5971 goto del_cursor;
5972 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005973 error = xfs_btree_insert(cur, &i);
5974 if (error)
5975 goto del_cursor;
5976 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5977 }
5978
5979 /*
5980 * Convert to a btree if necessary.
5981 */
5982 if (xfs_bmap_needs_btree(ip, whichfork)) {
5983 int tmp_logflags; /* partial log flag return val */
5984
5985 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005986 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005987 &cur, 0, &tmp_logflags, whichfork);
5988 logflags |= tmp_logflags;
5989 }
5990
5991del_cursor:
5992 if (cur) {
5993 cur->bc_private.b.allocated = 0;
5994 xfs_btree_del_cursor(cur,
5995 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5996 }
5997
5998 if (logflags)
5999 xfs_trans_log_inode(tp, ip, logflags);
6000 return error;
6001}
6002
6003int
6004xfs_bmap_split_extent(
6005 struct xfs_inode *ip,
6006 xfs_fileoff_t split_fsb)
6007{
6008 struct xfs_mount *mp = ip->i_mount;
6009 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006010 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006011 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006012 int error;
6013
Christoph Hellwig253f4912016-04-06 09:19:55 +10006014 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6015 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6016 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006017 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006018
6019 xfs_ilock(ip, XFS_ILOCK_EXCL);
6020 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6021
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006022 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006023
6024 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006025 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006026 if (error)
6027 goto out;
6028
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07006029 error = xfs_defer_finish(&tp, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006030 if (error)
6031 goto out;
6032
Christoph Hellwig70393312015-06-04 13:48:08 +10006033 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006034
6035out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006036 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006037 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006038 return error;
6039}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006040
6041/* Deferred mapping is only for real extents in the data fork. */
6042static bool
6043xfs_bmap_is_update_needed(
6044 struct xfs_bmbt_irec *bmap)
6045{
6046 return bmap->br_startblock != HOLESTARTBLOCK &&
6047 bmap->br_startblock != DELAYSTARTBLOCK;
6048}
6049
6050/* Record a bmap intent. */
6051static int
6052__xfs_bmap_add(
6053 struct xfs_mount *mp,
6054 struct xfs_defer_ops *dfops,
6055 enum xfs_bmap_intent_type type,
6056 struct xfs_inode *ip,
6057 int whichfork,
6058 struct xfs_bmbt_irec *bmap)
6059{
6060 int error;
6061 struct xfs_bmap_intent *bi;
6062
6063 trace_xfs_bmap_defer(mp,
6064 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6065 type,
6066 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6067 ip->i_ino, whichfork,
6068 bmap->br_startoff,
6069 bmap->br_blockcount,
6070 bmap->br_state);
6071
6072 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6073 INIT_LIST_HEAD(&bi->bi_list);
6074 bi->bi_type = type;
6075 bi->bi_owner = ip;
6076 bi->bi_whichfork = whichfork;
6077 bi->bi_bmap = *bmap;
6078
Christoph Hellwig882d8782017-08-28 10:21:03 -07006079 error = xfs_defer_ijoin(dfops, bi->bi_owner);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006080 if (error) {
6081 kmem_free(bi);
6082 return error;
6083 }
6084
6085 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6086 return 0;
6087}
6088
6089/* Map an extent into a file. */
6090int
6091xfs_bmap_map_extent(
6092 struct xfs_mount *mp,
6093 struct xfs_defer_ops *dfops,
6094 struct xfs_inode *ip,
6095 struct xfs_bmbt_irec *PREV)
6096{
6097 if (!xfs_bmap_is_update_needed(PREV))
6098 return 0;
6099
6100 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6101 XFS_DATA_FORK, PREV);
6102}
6103
6104/* Unmap an extent out of a file. */
6105int
6106xfs_bmap_unmap_extent(
6107 struct xfs_mount *mp,
6108 struct xfs_defer_ops *dfops,
6109 struct xfs_inode *ip,
6110 struct xfs_bmbt_irec *PREV)
6111{
6112 if (!xfs_bmap_is_update_needed(PREV))
6113 return 0;
6114
6115 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6116 XFS_DATA_FORK, PREV);
6117}
6118
6119/*
6120 * Process one of the deferred bmap operations. We pass back the
6121 * btree cursor to maintain our lock on the bmapbt between calls.
6122 */
6123int
6124xfs_bmap_finish_one(
6125 struct xfs_trans *tp,
6126 struct xfs_defer_ops *dfops,
6127 struct xfs_inode *ip,
6128 enum xfs_bmap_intent_type type,
6129 int whichfork,
6130 xfs_fileoff_t startoff,
6131 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006132 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006133 xfs_exntst_t state)
6134{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006135 xfs_fsblock_t firstfsb;
6136 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006137
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006138 /*
6139 * firstfsb is tied to the transaction lifetime and is used to
6140 * ensure correct AG locking order and schedule work item
6141 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6142 * to only making one bmap call per transaction, so it should
6143 * be safe to have it as a local variable here.
6144 */
6145 firstfsb = NULLFSBLOCK;
6146
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006147 trace_xfs_bmap_deferred(tp->t_mountp,
6148 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6149 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006150 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006151
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006152 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006153 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006154
6155 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006156 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006157 return -EIO;
6158
6159 switch (type) {
6160 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006161 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006162 startblock, dfops);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006163 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006164 break;
6165 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006166 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6167 XFS_BMAPI_REMAP, 1, &firstfsb, dfops);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006168 break;
6169 default:
6170 ASSERT(0);
6171 error = -EFSCORRUPTED;
6172 }
6173
6174 return error;
6175}
Darrick J. Wong30b09842018-03-23 10:06:52 -07006176
6177/* Check that an inode's extent does not have invalid flags or bad ranges. */
6178xfs_failaddr_t
6179xfs_bmap_validate_extent(
6180 struct xfs_inode *ip,
6181 int whichfork,
6182 struct xfs_bmbt_irec *irec)
6183{
6184 struct xfs_mount *mp = ip->i_mount;
6185 xfs_fsblock_t endfsb;
6186 bool isrt;
6187
6188 isrt = XFS_IS_REALTIME_INODE(ip);
6189 endfsb = irec->br_startblock + irec->br_blockcount - 1;
6190 if (isrt) {
6191 if (!xfs_verify_rtbno(mp, irec->br_startblock))
6192 return __this_address;
6193 if (!xfs_verify_rtbno(mp, endfsb))
6194 return __this_address;
6195 } else {
6196 if (!xfs_verify_fsbno(mp, irec->br_startblock))
6197 return __this_address;
6198 if (!xfs_verify_fsbno(mp, endfsb))
6199 return __this_address;
6200 if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) !=
6201 XFS_FSB_TO_AGNO(mp, endfsb))
6202 return __this_address;
6203 }
6204 if (irec->br_state != XFS_EXT_NORM) {
6205 if (whichfork != XFS_DATA_FORK)
6206 return __this_address;
6207 if (!xfs_sb_version_hasextflgbit(&mp->m_sb))
6208 return __this_address;
6209 }
6210 return NULL;
6211}